STEVAL-MKI218V1 + Nucleo F401RE I2C communication
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();
}
