How to establish SMBUS communication on STM32F091RCT6 using HAL libraries?
I want to configure one STM32F091RCT6 as master and 2nd as slave.
I have used Cubemx to generate code.
In Master I use
HAL_StatusTypeDef HAL_SMBUS_Master_Transmit_IT(SMBUS_HandleTypeDef *hsmbus, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t XferOptions)
I call the above function with required parameters in while(1).
So should I use
HAL_SMBUS_IsDeviceReady(SMBUS_HandleTypeDef *hsmbus, uint16_t DevAddress, uint32_t Trials, uint32_t Timeout)
To check if slave is ready or not and then call the master transmit but IsDeviceready is a blocking function?
Also I am not able to understand what is Xferoptions?
SMBUS_NEXT_FRAME ((uint32_t)(SMBUS_RELOAD_MODE | SMBUS_SOFTEND_MODE))
#define SMBUS_FIRST_AND_LAST_FRAME_NO_PEC SMBUS_AUTOEND_MODE
#define SMBUS_LAST_FRAME_NO_PEC SMBUS_AUTOEND_MODE
SMBUS_FIRST_FRAME_WITH_PE ((uint32_t(SMBUS_SOFTEND_MODE|SMBUS_SENDPE_MODE))
SMBUS_FIRST_AND_LAST_FRAME_WITH_PEC ((uint32_t)(SMBUS_AUTOEND_MODE | SMBUS_SENDPEC_MODE))
SMBUS_LAST_FRAME_WITH_PEC ((uint32_t)(SMBUS_AUTOEND_MODE | SMBUS_SENDPEC_MODE))
The above underlined have same SMBUS_AUTOEND_MODE but different names.
In Slave Mode
I was using HAL_SMBUS_Slave_Receive_IT(SMBUS_HandleTypeDef *hsmbus, uint8_t *pData, uint16_t Size,uint32_t XferOptions)
But in the function definition of above function
if ((hsmbus->State & HAL_SMBUS_STATE_LISTEN) == HAL_SMBUS_STATE_LISTEN)
{
body
return HAL_OKAY
}
else
{
return HAL_BUSY
}
By default the hsmbus->State is in HAL_SMBUS_STATE_READY and it always reurns hal_busy. So I called HAL_SMBUS_EnableListen_IT(SMBUS_HandleTypeDef *hsmbus) to set the state to HAL_SMBUS_STATE_LISTEN and then called the HAL_SMBUS_Slave_Receive_IT().
This was able to generate the interrupt on address match. But next iteration the interrupt didn't generate.(because I only called HAL_SMBUS_EnableListen_IT() once outside while(1).
I read the technical reference manual and the code written in "stm32f0xx_hal_smbus.c". I have few questions regarding it.
How to set the controller in slave mode transmit and receive data?(any example code)
How to set controller in master mode and transmitted and read data?(any example code)
How to initiate a restart condition with change in direction (from write to read of N bytes(read block data protocol)?
Can you share an example code for both master and slave?
