I have been trying for the last day to interface ADXL345 to STM32F4 discovery and has been unsuccessful before the last moment. The code was simple, as I was just testing it and it includes just to read device ID stored on ADXL345. I came to know that I2CD1 driver was not working but for the same exact code I2CD2 is working. I just want to bring this problem to your attention and ask whether I have made any mistakes in the process.
I have used two 4k7 pull-ups for SDA and SCL lines and the wave form has been observed using a Logic analyzer.
Code: Select all
#define ADXL345_REG_DEVID (0x00) // Device ID
#define ADXL345_DEFAULT_ADDRESS (0x53)
The I2C driver configuration can be seen below.
Code: Select all
/*
* I2C Driver Configuration
*/
static const I2CConfig i2cConfig = {
OPMODE_I2C,
100000,
STD_DUTY_CYCLE,
};
A Simple while loop which reads the device address can be seen below
Code: Select all
while (true) {
bool value = I2CreadBytes(ADXL345_DEFAULT_ADDRESS, ADXL345_REG_DEVID, 1, returnData);
if(value == TRUE)
{
chprintf((BaseSequentialStream*)&SD2, "Sucessfully transmitted\n");
}
else
{
chprintf((BaseSequentialStream*)&SD2, "Failure\n");
}
chThdSleepMilliseconds(1000);
The function for reading can be seen below.
Code: Select all
int8_t I2CreadBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data) {
msg_t rdymsg;
if(length > I2CDEV_BUFFER_LENGTH) {
return FALSE;
}
i2cAcquireBus(&I2CD1);
rdymsg = i2cMasterTransmitTimeout(&I2CD2, devAddr, ®Addr, 1, data, length, TIME_INFINITE);
i2cReleaseBus(&I2CD1);
if(rdymsg == MSG_TIMEOUT || rdymsg == MSG_RESET) {
chprintf((BaseSequentialStream*)&SD2, "\n%d\n", rdymsg);
chprintf((BaseSequentialStream*)&SD2,"I2C ERROR: %d\n", i2cGetErrors(&I2CD2));
chprintf((BaseSequentialStream*)&SD2, "Received : 0x%02X", data[0]);
return FALSE;
}
return TRUE;
}
I have tested with the same connections(but with different example code) on NodeMCU as well and it seems to be working well. All the pictures were taken while testing will be attached below. The strange thing is that STM32F4 doesn't transmit a "Read" message, even though it has received ACK from the slave.
STM32F4 - ChibiOS I2CD1
[img]
[/img]
STM32F4 - Chibios I2CD2
[img]
[/img]
NodeMCU