cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L4 SMBus example code

Niks
Associate II
Posted on May 10, 2017 at 15:17

Hello All,

I want to use the SMBus library written by MCD team. Could you please help me out with sample code to communicate with two microcontrollers?

Here is the snippet of my code:

/* SCL */

GPIO_InitStruct.Pin = GPIO_PIN_7;

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(GPIOA, &GPIO_InitStruct);

HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_2);

HAL_NVIC_SetPriority(I2C3_ER_IRQn, 0, 2);

HAL_NVIC_EnableIRQ(I2C3_ER_IRQn);

HAL_NVIC_SetPriority(I2C3_EV_IRQn, 0, 1);

HAL_NVIC_EnableIRQ(I2C3_EV_IRQn);

SMBus_Handle.Instance = I2C3;

SMBus_Handle.Init.Timing = ((uint32_t)0x1042C3C7); // for 10KHz

SMBus_Handle.Init.AnalogFilter = SMBUS_ANALOGFILTER_ENABLE;

SMBus_Handle.Init.OwnAddress1 = 0x55;

SMBus_Handle.Init.AddressingMode = SMBUS_ADDRESSINGMODE_7BIT;

SMBus_Handle.Init.DualAddressMode = SMBUS_DUALADDRESS_DISABLED;

SMBus_Handle.Init.OwnAddress2 = 0;

SMBus_Handle.Init.OwnAddress2Masks = SMBUS_OA2_NOMASK;

SMBus_Handle.Init.GeneralCallMode = SMBUS_GENERALCALL_DISABLED;

SMBus_Handle.Init.NoStretchMode = SMBUS_NOSTRETCH_DISABLED;

SMBus_Handle.Init.PacketErrorCheckMode = SMBUS_PEC_DISABLED;

#ifdef SLAVE_MODE

SMBus_Handle.Init.PeripheralMode = SMBUS_PERIPHERAL_MODE_SMBUS_SLAVE;

#elif defined MASTER_MODE

SMBus_Handle.Init.PeripheralMode = SMBUS_PERIPHERAL_MODE_SMBUS_HOST;

#endif

SMBus_Handle.Init.SMBusTimeout = SMBUS_TIMEOUT_DEFAULT;

HAL_SMBUS_Init(&SMBus_Handle);

Main code :

HAL_SMBUS_Master_Transmit_IT(&SMBus_Handle,SLAVE_DEVICE_ADDRESS,Master_Data,sizeof(Master_Data),SMBUS_AUTOEND_MODE);

while( HAL_SMBUS_GetState(&

SMBus_Handle

) != HAL_SMBUS_STATE_READY);

if(Flag_In_Tx_ISR == true)

{

  // transfer completed 

Flag_In_Tx_ISR

= false;

}

// expecting some string from slave receiver 

HAL_SMBUS_Master_Receive_IT(&SMBus_Handle,SLAVE_DEVICE_ADDRESS,(uint8_t*)Buffer,sizeof(Slave_Data),SMBUS_AUTOEND_MODE);

while( HAL_SMBUS_GetState(&

SMBus_Handle

) != HAL_SMBUS_STATE_READY);

if(Flag_In_Rx_ISR == true)

{

// receive the string and do something

Flag_In_Rx_ISR

= false;

}

I have just added snippet of code... just let me know if anything is wrong in this code.

Thanks, 

Nikhil

0 REPLIES 0