How to handle several I2C data buffer transmission/reception between a master and a slave device using an interrupt.
Board: NUCLEO-C031C6 (embeds a STM32C031C6 device)
- SCL Pin: PB8 (CN5, pin10)
- SDA Pin: PB9 (CN5, pin9)
At the beginning of the main program the HAL_Init() function is called to reset all the peripherals, initialize the Flash interface and the systick. Then the SystemClock_Config() function is used to configure the system clock (SYSCLK) to run at 48 MHz.
The I2C peripheral configuration is ensured by the HAL_I2C_Init() function. This later is calling the HAL_I2C_MspInit()function which core is implementing the configuration of the needed I2C resources according to the used hardware (CLOCK, GPIO and NVIC). You may update this function to change I2C configuration.
The I2C communication is then initiated.
The project is split in two parts: the Master Board and the Slave Board :
Master Board : The HAL_I2C_Master_Receive_IT() and the HAL_I2C_Master_Transmit_IT() functions allow respectively the reception and the transmission of a predefined data buffer in Master mode using interrupt.
Slave Board : The HAL_I2C_Slave_Receive_IT() and the HAL_I2C_Slave_Transmit_IT() functions allow respectively the reception and the transmission of a predefined data buffer in Slave mode using interrupt.
The user can choose between Master and Slave through “#define MASTER_BOARD” in the “main.c” file:
For this example two buffers are used :
Note that both buffers have same size
On Master board side:
On Slave board side:
These operations are repeated periodically and the start of communication is triggered by pushing the key button of the Master board.
In Master side, only Acknowledge failure error is handled. When this error occurs Master restart the current operation until Slave acknowledges it’s address.
Care must be taken when using HAL_Delay(), this function provides accurate delay (in milliseconds) based on variable incremented in SysTick ISR. This implies that if HAL_Delay() is called from a peripheral ISR process, then the SysTick interrupt must have higher priority (numerically lower) than the peripheral interrupt. Otherwise the caller ISR process will be blocked. To change the SysTick interrupt priority you have to use HAL_NVIC_SetPriority() function.
The application need to ensure that the SysTick time base is always set to 1 millisecond to have correct HAL operation.
Connectivity, Communication, I2C, Interrupt, Master, Slave, Transmission, Reception, Fast mode, Command, Acknowledge
This example runs on STM32C031xx devices.
This example has been tested with NUCLEO-C031C6 board and can be easily tailored to any other supported device and development board.
NUCLEO-C031C6 Set-up
In order to make the program work, you must do the following :