I am just starting to use stm32 in chibistudio and found the forums and examples very useful. Currently I am trying to set up an I2C communication between the microcontroller and AIS328DQ. I tried using I2Cdev library as well as the native example code for I2c, modifying the device slave address and testing with the WHO_AM_I register of the AIS328DQ. But i am not able to figure out why the communication is not happening.The code i used is this :
Code: Select all
#include <stdlib.h>
#include "ch.h"
#include "hal.h"
static SerialConfig sd2cfg = {
115200,
0,
USART_CR2_STOP1_BITS | USART_CR2_LINEN,
0
};
static void print(char *p) {
while (*p) {
sdPut(&SD2, *p++);
}
}
static const I2CConfig i2cfg2 = {
OPMODE_I2C,
400000,
FAST_DUTY_CYCLE_2,
};
int main(void) {
msg_t status = RDY_OK;
systime_t tmo = MS2ST(4);
halInit();
chSysInit();
/*
* Starts I2C
*/
palSetPadMode(GPIOB, 9, PAL_MODE_ALTERNATE(0));
i2cStart(&I2CD1, &i2cfg2);
palSetPadMode(GPIOB, 6, PAL_MODE_ALTERNATE(4) | PAL_STM32_OTYPE_OPENDRAIN);
palSetPadMode(GPIOB, 7, PAL_MODE_ALTERNATE(4) | PAL_STM32_OTYPE_OPENDRAIN);
/*
* Prepares the Serial driver 2
*/
sdStart(&SD2, &sd2cfg); /* Default is 38400-8-N-1.*/
palSetPadMode(GPIOA, 2, PAL_MODE_ALTERNATE(7));
palSetPadMode(GPIOA, 3, PAL_MODE_ALTERNATE(7));
i2cAcquireBus(&I2CD1);
txbuf[0] = 0x0F;
status = i2cMasterTransmitTimeout(&I2CD1, 0x19, txbuf, 1, rxbuf, 1, tmo); //0x19 being address of AIS32DQ and 0x0F is WHO_AM_I
i2cReleaseBus(&I2CD1);
char msg1;
if (status != RDY_OK){
errors = i2cGetErrors(&I2CD1);
print("i2c fault");
}
while(1){}
}
What here i am doing is, if the i2c communication has an error, it enters the if block and if it does, it should print "i2c fault" and everytime it does print it. I connected the SCL pin to PA6 and SDx(SDA) pin to PA7 and Vdd to 3V and GND t0 GND. Please help me find the problem with this setup. I am only a beginner and hence also please try to keep it simple while explaining. Thank you.
Regards,
Sid