Home › Forums › Mastering STM32 book support forum › I2C Sequential Read
- This topic has 1 reply, 1 voice, and was last updated 8 years, 8 months ago by
olivier.cornez@me.com.
-
AuthorPosts
-
February 15, 2017 at 3:47 pm #6152
olivier.cornez@me.com
ParticipantHi Carmine,
I trying to read register(s) from my MPC9808 (Temp sensor).
While I can read with HAL_I2C_Mem_Read with this code:for (int ii = 0x0; ii < 0x9; ++ii) {
while(HAL_I2C_IsDeviceReady(&hi2c1, 0x30, 1, HAL_MAX_DELAY) != HAL_OK);
I2C_Status = HAL_I2C_Mem_Read(&hi2c1, 0x30, ii, I2C_MEMADD_SIZE_8BIT, pData,\
sizeof(pData), HAL_MAX_DELAY);
}
I can read 8 registers as documented in the MPC data sheet ( with correct values).But I cannot succeed with
I2C_Status = HAL_I2C_Master_Sequential_Transmit_IT(&hi2c1, 0x30, pData, \
sizeof(pData), I2C_FIRST_FRAME);
while(HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY);
I2C_Status = HAL_I2C_Master_Sequential_Receive_IT(&hi2c1, 0x30, pData, sizeof(pData), I2C_LAST_FRAME);The data sheet specifies;
Start -> dev addr -> W bit-> ACK -> Register pointer (0x5 for temp) -> ACKThen
Restart -> Dev addr -> R bit -> ACK -> MSB data -> ACK -> LSB data -> NACK
With HAL_I2C_Master_Sequential_Receive_IT I get the I2C1_EV_IRQHandler(void) fired but with
HAL_I2C_Master_Sequential_Receive_IT and the I2C_LAST_FRAME option I do not get any interrupt fired.
If I change to FIRST AND LAST FRAME then I get an infinite number of interlocks fired and stay blocked in
I2C1_EV_IRQHandler(void).Could you help me clarify this?
If I look at the data sheet it seems to match what is described in the book for sequential transmission. Correct?Thank you very much for your help.
Kind regards,
OlivierFebruary 16, 2017 at 3:12 pm #6168olivier.cornez@me.com
ParticipantOk I found my mistake.
The config pointer as named by Microship ( Register addr) is only 1 byte long.
I was splitting MSB/LSB from the register address to pData[2].So I can read with
Flush_Buffer(pData, sizeof(pData));
for (int i = 0; i < 9; ++i) {
reg = (uint8_t)i;
I2C_Status = HAL_I2C_Master_Transmit(&hi2c1, (uint16_t)MCP9808_DEV_ADDR, (uint8_t*)®, sizeof(reg), HAL_MAX_DELAY);
while(HAL_I2C_IsDeviceReady(&hi2c1, (uint16_t)MCP9808_DEV_ADDR, 1, HAL_MAX_DELAY) != HAL_OK);
I2C_Status = HAL_I2C_Master_Receive(&hi2c1, (uint16_t)MCP9808_DEV_ADDR, (uint8_t*)&pData, sizeof(pData), HAL_MAX_DELAY);
}Let see if I can write 😉
-
AuthorPosts
- You must be logged in to reply to this topic.
