Skip to content

Commit baa22b0

Browse files
stracciofpistm
authored andcommitted
Added ability to bind std:function on interrupts (#159)
* Added ability to bind std:function on interrupts The function attachInterrupt now also accept std:function<void(void)> in order to bind object method to an interrupt.
1 parent 73dd420 commit baa22b0

File tree

6 files changed

+57
-43
lines changed

6 files changed

+57
-43
lines changed

cores/arduino/WInterrupts.c renamed to cores/arduino/WInterrupts.cpp

+8-6
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,10 @@
1919
#include "WInterrupts.h"
2020
#include "Arduino.h"
2121

22-
#ifdef __cplusplus
23-
extern "C" {
24-
#endif
25-
2622
#include "PinAF_STM32F1.h"
23+
#include "interrupt.h"
2724

28-
void attachInterrupt(uint32_t pin, void (*callback)(void), uint32_t mode)
29-
{
25+
void attachInterrupt(uint32_t pin, callback_function_t callback, uint32_t mode){
3026
uint32_t it_mode;
3127
PinName p = digitalPinToPinName(pin);
3228
GPIO_TypeDef* port = set_GPIO_Port_Clock(STM_PORT(p));
@@ -57,6 +53,12 @@ void attachInterrupt(uint32_t pin, void (*callback)(void), uint32_t mode)
5753
stm32_interrupt_enable(port, STM_GPIO_PIN(p), callback, it_mode);
5854
}
5955

56+
void attachInterrupt(uint32_t pin, void (*callback)(void), uint32_t mode)
57+
{
58+
callback_function_t _c = callback;
59+
attachInterrupt(pin,_c,mode);
60+
}
61+
6062
void detachInterrupt(uint32_t pin)
6163
{
6264
PinName p = digitalPinToPinName(pin);

cores/arduino/WInterrupts.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@
2222
#include <stdint.h>
2323

2424
#ifdef __cplusplus
25-
extern "C" {
25+
#include <functional>
26+
27+
typedef std::function<void(void)> callback_function_t;
28+
void attachInterrupt(uint32_t pin, callback_function_t callback, uint32_t mode);
29+
2630
#endif
2731

2832
void attachInterrupt(uint32_t pin, void (*callback)(void), uint32_t mode);
2933

3034
void detachInterrupt(uint32_t pin);
3135

32-
#ifdef __cplusplus
33-
}
34-
#endif
35-
3636
#endif /* _WIRING_INTERRUPTS_ */

cores/arduino/board.h

+8-3
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33

44
/*
55
* Core and peripherals registers definitions
6-
*/
6+
*/
7+
#include "interrupt.h"
8+
#ifdef __cplusplus
9+
extern "C"{
10+
#endif // __cplusplus
711
#include "analog.h"
812
#include "clock.h"
913
#include "core_callback.h"
1014
#include "digital_io.h"
1115
#include "hal_uart_emul.h"
1216
#include "hw_config.h"
13-
#include "interrupt.h"
1417
#include "spi_com.h"
1518
#include "stm32_eeprom.h"
1619
#include "timer.h"
@@ -22,5 +25,7 @@
2225
#endif //USBCON
2326

2427
void init( void ) ;
25-
28+
#ifdef __cplusplus
29+
}
30+
#endif // __cplusplus
2631
#endif /* _BOARD_H_ */

cores/arduino/stm32/interrupt.c renamed to cores/arduino/stm32/interrupt.cpp

+32-19
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@
4949
#include "stm32_def.h"
5050
#include "interrupt.h"
5151

52-
#ifdef __cplusplus
53-
extern "C" {
54-
#endif
5552
/**
5653
* @}
5754
*/
@@ -62,8 +59,8 @@
6259

6360
/*As we can have only one interrupt/pin id, don't need to get the port info*/
6461
typedef struct {
65-
uint32_t irqnb;
66-
void (*callback)(void);
62+
IRQn_Type irqnb;
63+
std::function<void(void)> callback;
6764
uint32_t mode;
6865
}gpio_irq_conf_str;
6966

@@ -156,16 +153,7 @@ uint8_t get_pin_id(uint16_t pin)
156153

157154
return id;
158155
}
159-
/**
160-
* @brief This function enable the interruption on the selected port/pin
161-
* @param port : one of the gpio port
162-
* @param pin : one of the gpio pin
163-
**@param callback : callback to call when the interrupt falls
164-
* @param mode : one of the supported interrupt mode defined in stm32_hal_gpio
165-
* @retval None
166-
*/
167-
void stm32_interrupt_enable(GPIO_TypeDef *port, uint16_t pin, void (*callback)(void), uint32_t mode)
168-
{
156+
void stm32_interrupt_enable(GPIO_TypeDef *port, uint16_t pin, callback_function_t callback, uint32_t mode){
169157
GPIO_InitTypeDef GPIO_InitStruct;
170158
uint8_t id = get_pin_id(pin);
171159

@@ -227,6 +215,21 @@ void stm32_interrupt_enable(GPIO_TypeDef *port, uint16_t pin, void (*callback)(v
227215
HAL_NVIC_EnableIRQ(gpio_irq_conf[id].irqnb);
228216
}
229217

218+
/**
219+
* @brief This function enable the interruption on the selected port/pin
220+
* @param port : one of the gpio port
221+
* @param pin : one of the gpio pin
222+
**@param callback : callback to call when the interrupt falls
223+
* @param mode : one of the supported interrupt mode defined in stm32_hal_gpio
224+
* @retval None
225+
*/
226+
void stm32_interrupt_enable(GPIO_TypeDef *port, uint16_t pin, void (*callback)(void), uint32_t mode)
227+
{
228+
std::function<void(void)> _c = callback;
229+
stm32_interrupt_enable(port,pin,_c,mode);
230+
231+
}
232+
230233
/**
231234
* @brief This function disable the interruption on the selected port/pin
232235
* @param port : one of the gpio port
@@ -263,6 +266,10 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
263266
}
264267

265268
#if defined (STM32F0xx) || defined (STM32L0xx)
269+
#ifdef __cplusplus
270+
extern "C" {
271+
#endif
272+
266273
/**
267274
* @brief This function handles external line 0 to 1 interrupt request.
268275
* @param None
@@ -302,7 +309,13 @@ void EXTI4_15_IRQHandler(void)
302309
HAL_GPIO_EXTI_IRQHandler(pin);
303310
}
304311
}
312+
#ifdef __cplusplus
313+
}
314+
#endif
305315
#else
316+
#ifdef __cplusplus
317+
extern "C" {
318+
#endif
306319
/**
307320
* @brief This function handles external line 0 interrupt request.
308321
* @param None
@@ -379,6 +392,10 @@ void EXTI15_10_IRQHandler(void)
379392
HAL_GPIO_EXTI_IRQHandler(pin);
380393
}
381394
}
395+
396+
#ifdef __cplusplus
397+
}
398+
#endif
382399
#endif
383400
/**
384401
* @}
@@ -391,8 +408,4 @@ void EXTI15_10_IRQHandler(void)
391408
/**
392409
* @}
393410
*/
394-
#ifdef __cplusplus
395-
}
396-
#endif
397-
398411
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

cores/arduino/stm32/interrupt.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@
4444
#include "PinNames.h"
4545

4646
#ifdef __cplusplus
47-
extern "C" {
47+
#include <functional>
48+
49+
typedef std::function<void(void)> callback_function_t;
50+
void stm32_interrupt_enable(GPIO_TypeDef *port, uint16_t pin, callback_function_t callback, uint32_t mode);
4851
#endif
4952

5053
/* Exported types ------------------------------------------------------------*/
@@ -53,9 +56,6 @@
5356
/* Exported functions ------------------------------------------------------- */
5457
void stm32_interrupt_enable(GPIO_TypeDef *port, uint16_t pin, void (*callback)(void), uint32_t mode);
5558
void stm32_interrupt_disable(GPIO_TypeDef *port, uint16_t pin);
56-
#ifdef __cplusplus
57-
}
58-
#endif
5959

6060
#endif /* __INTERRUPT_H */
6161

cores/arduino/wiring.h

-6
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,7 @@
3838
#include "wiring_time.h"
3939
#include "WInterrupts.h"
4040

41-
#ifdef __cplusplus
42-
extern "C"{
43-
#endif // __cplusplus
4441
#include <board.h>
45-
#ifdef __cplusplus
46-
}
47-
#endif
4842

4943
#ifdef __cplusplus
5044
#include "HardwareSerial.h"

0 commit comments

Comments
 (0)