cancel
Showing results for 
Search instead for 
Did you mean: 

Handling interrupts within library passing peripheral HandleTypeDef

ilmalavoglia
Associate

Hello,

I'm trying to develop a library for a device using I2C for multiple projects. To use the sequential transmit functions, I need to handle Interrupts for the correct I2Cx channel. When I initialize the device configuration struct from main.c, among other stuff, I pass the correct I2Cx handler, depending if the device is connected to channel 1, 2, 3 and so on.

In my library.c I have a function called myDevice_EV_IRQHandler(HAL_I2C_HandleTypeDef *hi2c) called by I2Cx_EV_IRQHandler() from main.c file where x is the channel I physically wired my device to. 

I was wondering if I could do it in a more elegant way at run-time having the correct I2Cx handler passed during initialization. The goal is to substitute the function pointer of I2Cx_EV_IRQHandler with the one of myDevice_EV_IRQHandler at run-time based on which channel is the hi2c I passed initially.

I hope my question is clear, thank you in advance.

1 REPLY 1
Pavel A.
Evangelist III

Of course you can. Each I2C controller of STM32 has its unique interrupt vector and interrupt handler function. From these you call the common myDevice_EV_IRQHandler with the proper I2C argument (whatever you prefer, the HAL "handle" pointer or registers pointer). The mapping of I2C controllers to your "instances" is static and does not need any special initialization - unless you want something unusual or overly abstract.