2017-01-30 07:38 PM
I know HAL should work with all STM32F4xx MCUs but there are no examples for it in the STM32Cube_FW_F4_V1.14.0 folder for the nucleo-f446re, and there is no sign of any connection or success when trying to communicate via I2C to a slave device. I'm having trouble getting data from the ADXL345 accelerometer. It is returning zeros for the data and the return for the IsDeviceReady is always 0x01 even when I disconnect the SDA and SCL lines. Furthermore, the return for the device id (dev_id) should be a fixed value of 0xE5 according to the ADXL345 datasheet but it is always 0x01. I know the hardware is setup is correct because it works with my Arduino but I can't get it working with the stm32 nucleo-f446re. I connected the SDA and SCL lines to the corresponding pins on the nucleo which correspond to I2C1. Here is the code:
&sharpdefine ADXL345_ADDR 0xA6 //write i2c address since i'm grounding SDO pin
&sharpdefine ADXL345_INIT 0x2D //POWER_CTL
&sharpdefine ADXL345_DATA 0x32
void SystemClock_Config(void);
void Error_Handler(void);
static void MX_GPIO_Init(void);
s
tatic void MX_I2C1_Init(void);
uint8_t values[6];
int ADXL_STATUS;
struct axes { int x_val; int y_val; int z_val; } axes_vals;
uint8_t dev_id;
int main(void) {
uint8_t command_data[2];
uint8_t get_id = 0x00;
command_data[0] = ADXL345_INIT;
command_data[1] = 0x00;
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_I2C1_Init();
ADXL_STATUS = HAL_I2C_IsDeviceReady(&hi2c1, ADXL345_ADDR, 5, 100);
dev_id = HAL_I2C_Master_Receive(&hi2c1, ADXL345_ADDR | 0x01, &get_id, 1, 50);
//clear 0x2D
HAL_I2C_Master_Transmit(&hi2c1, ADXL345_ADDR, command_data, 2, 50);
command_data[0] = 0x10;
//write 16 to 0x2D
HAL_I2C_Master_Transmit(&hi2c1, ADXL345_ADDR, command_data, 1, 50);
command_data[0] |= 0x08;
//write 8 to 0x2D
HAL_I2C_Master_Transmit(&hi2c1, ADXL345_ADDR, command_data, 1, 50);
while (1) {
uint8_t request_data = ADXL345_DATA;
//trigger data request
HAL_I2C_Master_Transmit(&hi2c1, ADXL345_ADDR, &request_data, 1, 50);
HAL_I2C_Master_Receive(&hi2c1, ADXL345_ADDR | 0x01, values, 6, 50);
axes_vals.x_val = ((int)values[1] << 8) | values[0];
axes_vals.y_val = ((int)values[3] << 8) | values[2];
axes_vals.z_val = ((int)values[5] << 8) | values[4];
HAL_Delay(100);
}
}
I've also tried using the Mem_Read and Mem_Write functions of HAL but to no avail. If the hal_i2c module does indeed work with my board, please tell me what I'm doing wrong. I used Cube to generate the I2C configurations. Thanks a lot.
#i2c #hal-driver #nucleo-f446re #nucleo:2017-05-03 10:18 AM
Hello
I have checked similar code to Your on Nucleo 401 RE.
I get data from all axis by this function HAL_I2C_Mem_Read(&hi2c3,0xA7,0x32,1,values,6,50);
The best way is to use logic to scan ports and see if any feedback is comming or if the Nucleo is working.
2017-05-03 01:19 PM
the HAL i2c driver wants a left justified address. if your device is at 0xA6 and you try to talk to it at 0xA6 it won't work. try using the address (0x53<<1) instead.