Skip to main content
Graduate
August 8, 2023
Solved

STEVAL-MKI218V1 + Nucleo F401RE I2C communication

  • August 8, 2023
  • 4 replies
  • 2605 views

Hi,

I am on a steep learning curve here. There are huge gaps in my understanding of the field, so any help will be most appreciated. Basically I am trying to connect to AIS2ih accelerometer (on Steval-mki218v1) with my Nucleo board, and try to communicate with it. So far I do fail miserably, and have no idea what am I missing. Basically I am getting the i2c slave address of the AIS2ih, and some register addresses (CTRL1, and CTRL6), than I try to write to them to start the accelerometer, after that I am trying to read Who_am_I register just to get some feedback that the concept is working, but it is not.

Here is my code (not really mine, but an adaptation really), and photo of my wiring. Thanks!

 

#include <stdio.h>
#include "stm32f4xx_hal.h"
#include "uart.h"
#include "adc.h"


#define DEVICE_ADDR_I2C 0011000


#define CTR1_Reg				0x20
#define CTR6_Reg				0x25
#define Who_Am_I_Reg			0x0F


I2C_HandleTypeDef ais2ih_i2c1;

void i2c1_init(void);


void adxl_write (uint8_t reg, uint8_t value);
void adxl_read_values (uint8_t reg);


void ais2ih_read_register (uint8_t reg);
void ais2ih_init(void);

uint8_t data_rec[8];
uint8_t device_id[1];


int main()
{

 HAL_Init();
 i2c1_init();
 ais2ih_init();


 while(1)
 {
	 ais2ih_read_register (Who_Am_I_Reg);
 }
}

void adxl_write (uint8_t reg, uint8_t value)
{
	uint8_t data[2];
	data[0] = reg;
	data[1] = value;
	HAL_I2C_Master_Transmit (&ais2ih_i2c1, DEVICE_ADDR_I2C, data, 2, 100);
}


void ais2ih_init(void)
{
	//1. Turn ON the ACC
	adxl_write (CTR1_Reg, 0x64);

	//2. Set up work mode
	adxl_write (CTR6_Reg, 0x04);
}
void ais2ih_read_register (uint8_t reg_adress)
{
	HAL_I2C_Mem_Read (&ais2ih_i2c1, DEVICE_ADDR_I2C, reg_adress, I2C_MEMADD_SIZE_8BIT, (uint8_t *) device_id, 1, 100);
}


void i2c1_init(void)
{
 /* PB8 ------> I2C1_SCL
 PB9 ------> I2C1_SDA */

	GPIO_InitTypeDef GPIO_InitStruct;

	__HAL_RCC_GPIOB_CLK_ENABLE();

 GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9;
 GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
 GPIO_InitStruct.Pull = GPIO_PULLUP;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
 GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;

 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

 //Configure I2C

 __HAL_RCC_I2C1_CLK_ENABLE();

 ais2ih_i2c1.Instance = I2C1;
 ais2ih_i2c1.Init.ClockSpeed = 400000;
 ais2ih_i2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
 ais2ih_i2c1.Init.OwnAddress1 = 0;
 ais2ih_i2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
 ais2ih_i2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
 ais2ih_i2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
 ais2ih_i2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;

 HAL_I2C_Init(&ais2ih_i2c1);

}


void SysTick_Handler(void)
{
	HAL_IncTick();
}

 

 

    This topic has been closed for replies.
    Best answer by TDK

    The address is likely 0b110000.

    I don't recommend using ChatGPT for programming advice. It will produce some very confident-sounding but absolutely incorrect answers.

    4 replies

    Super User
    August 8, 2023

    > #define DEVICE_ADDR_I2C 0011000

    11000 is not a valid I2C address. Perhaps you meant 0b11000? Be aware that 7-bit addresses should be left-shifted in the 8-bit address field.

    Graduate
    August 8, 2023

    Hey TDK,

    Tthank you for your advice! I am just not sure. I took this address from the datasheet of AIS2IH, there was this: " If the SA0/SDO pin is connected to the supply voltage, the address is
    0011001b, otherwise if the SA0/SDO pin is connected to ground, the address is 0011000b." So my SA0 pin is not connected to anything and I think that the address is 0011000 however I must admit that I took this "b" at the end as a mark of "bit" but I am probably wrong :) seeing it now on totally different place. As I understood (from ChatGPT) the HAL function (HAL_I2C_Mem_Read) will take care for the additional read\write bit automatically and my 0011000 address should be valid. So I do not really know. Do you advice me to try with "0b11000" address?

    Thanks!

    TDKAnswer
    Super User
    August 8, 2023

    The address is likely 0b110000.

    I don't recommend using ChatGPT for programming advice. It will produce some very confident-sounding but absolutely incorrect answers.

    Graduate
    August 8, 2023

    Thank you again TDK,

    I did try with 0b110000 as an address but still could not read the Who_am_I register.

    Graduate
    August 11, 2023

    It was an address issue after all, the addresses were: 0b00110010 for write, and 0b00110011 for read. I had also an issue with no or bad contact with jumper wires, when adjusted them a bit the reading values started to emerge.