From eb345ec106a0314efd3518137cbf56a304119ebb Mon Sep 17 00:00:00 2001 From: straccio Date: Tue, 28 Nov 2017 09:26:18 +0100 Subject: [PATCH 1/5] Added ability to bind std:function on interrupts The function attachInterrupt now also accept std:function in order to bind object method to an interrupt. --- .../{WInterrupts.c => WInterrupts.cpp} | 19 +++++-- cores/arduino/WInterrupts.h | 20 +++++-- cores/arduino/board.h | 15 ++++- .../stm32/{interrupt.c => interrupt.cpp} | 57 +++++++++++++------ cores/arduino/stm32/interrupt.h | 16 ++++-- cores/arduino/wiring.h | 12 ++-- 6 files changed, 100 insertions(+), 39 deletions(-) rename cores/arduino/{WInterrupts.c => WInterrupts.cpp} (86%) rename cores/arduino/stm32/{interrupt.c => interrupt.cpp} (96%) diff --git a/cores/arduino/WInterrupts.c b/cores/arduino/WInterrupts.cpp similarity index 86% rename from cores/arduino/WInterrupts.c rename to cores/arduino/WInterrupts.cpp index 6ea0f333a8..cdb78e264b 100644 --- a/cores/arduino/WInterrupts.c +++ b/cores/arduino/WInterrupts.cpp @@ -19,14 +19,14 @@ #include "WInterrupts.h" #include "Arduino.h" -#ifdef __cplusplus - extern "C" { -#endif +// #ifdef __cplusplus +// extern "C" { +// #endif #include "PinAF_STM32F1.h" +#include "interrupt.h" -void attachInterrupt(uint32_t pin, void (*callback)(void), uint32_t mode) -{ +void attachInterrupt(uint32_t pin, callback_function_t callback, uint32_t mode){ uint32_t it_mode; PinName p = digitalPinToPinName(pin); GPIO_TypeDef* port = set_GPIO_Port_Clock(STM_PORT(p)); @@ -57,6 +57,12 @@ void attachInterrupt(uint32_t pin, void (*callback)(void), uint32_t mode) stm32_interrupt_enable(port, STM_GPIO_PIN(p), callback, it_mode); } +void attachInterrupt(uint32_t pin, void (*callback)(void), uint32_t mode) +{ + callback_function_t _c = callback; + attachInterrupt(pin,_c,mode); +} + void detachInterrupt(uint32_t pin) { PinName p = digitalPinToPinName(pin); @@ -65,3 +71,6 @@ void detachInterrupt(uint32_t pin) return; stm32_interrupt_disable(port, STM_GPIO_PIN(p)); } +// #ifdef __cplusplus +// } +// #endif diff --git a/cores/arduino/WInterrupts.h b/cores/arduino/WInterrupts.h index ce09703880..e33a148eee 100644 --- a/cores/arduino/WInterrupts.h +++ b/cores/arduino/WInterrupts.h @@ -20,17 +20,29 @@ #define _WIRING_INTERRUPTS_ #include +// #include "interrupt.h" + #ifdef __cplusplus -extern "C" { +#include +typedef std::function callback_function_t; + +// extern "C" { +// #endif + +void attachInterrupt(uint32_t pin, callback_function_t callback, uint32_t mode); +// } #endif +// #ifdef __cplusplus +// extern "C" { +// #endif void attachInterrupt(uint32_t pin, void (*callback)(void), uint32_t mode); void detachInterrupt(uint32_t pin); -#ifdef __cplusplus -} -#endif +// #ifdef __cplusplus +// } +// #endif #endif /* _WIRING_INTERRUPTS_ */ diff --git a/cores/arduino/board.h b/cores/arduino/board.h index b1787550c4..f7c3a3cb57 100644 --- a/cores/arduino/board.h +++ b/cores/arduino/board.h @@ -3,14 +3,23 @@ /* * Core and peripherals registers definitions - */ +*/ +#ifdef __cplusplus +extern "C"{ +#endif // __cplusplus #include "analog.h" #include "clock.h" #include "core_callback.h" #include "digital_io.h" #include "hal_uart_emul.h" #include "hw_config.h" +#ifdef __cplusplus +} +#endif // __cplusplus #include "interrupt.h" +#ifdef __cplusplus +extern "C"{ +#endif // __cplusplus #include "spi_com.h" #include "stm32_eeprom.h" #include "timer.h" @@ -22,5 +31,7 @@ #endif //USBCON void init( void ) ; - +#ifdef __cplusplus +} +#endif // __cplusplus #endif /* _BOARD_H_ */ diff --git a/cores/arduino/stm32/interrupt.c b/cores/arduino/stm32/interrupt.cpp similarity index 96% rename from cores/arduino/stm32/interrupt.c rename to cores/arduino/stm32/interrupt.cpp index 745c61fcb3..138bf545bf 100644 --- a/cores/arduino/stm32/interrupt.c +++ b/cores/arduino/stm32/interrupt.cpp @@ -49,9 +49,9 @@ #include "stm32_def.h" #include "interrupt.h" -#ifdef __cplusplus - extern "C" { -#endif +// #ifdef __cplusplus +// extern "C" { +// #endif /** * @} */ @@ -62,8 +62,9 @@ /*As we can have only one interrupt/pin id, don't need to get the port info*/ typedef struct { - uint32_t irqnb; - void (*callback)(void); + IRQn_Type irqnb; + // void (*callback)(void); + std::function callback; uint32_t mode; }gpio_irq_conf_str; @@ -156,16 +157,7 @@ uint8_t get_pin_id(uint16_t pin) return id; } -/** - * @brief This function enable the interruption on the selected port/pin - * @param port : one of the gpio port - * @param pin : one of the gpio pin - **@param callback : callback to call when the interrupt falls - * @param mode : one of the supported interrupt mode defined in stm32_hal_gpio - * @retval None - */ -void stm32_interrupt_enable(GPIO_TypeDef *port, uint16_t pin, void (*callback)(void), uint32_t mode) -{ +void stm32_interrupt_enable(GPIO_TypeDef *port, uint16_t pin, callback_function_t callback, uint32_t mode){ GPIO_InitTypeDef GPIO_InitStruct; uint8_t id = get_pin_id(pin); @@ -227,6 +219,21 @@ void stm32_interrupt_enable(GPIO_TypeDef *port, uint16_t pin, void (*callback)(v HAL_NVIC_EnableIRQ(gpio_irq_conf[id].irqnb); } +/** + * @brief This function enable the interruption on the selected port/pin + * @param port : one of the gpio port + * @param pin : one of the gpio pin + **@param callback : callback to call when the interrupt falls + * @param mode : one of the supported interrupt mode defined in stm32_hal_gpio + * @retval None + */ +void stm32_interrupt_enable(GPIO_TypeDef *port, uint16_t pin, void (*callback)(void), uint32_t mode) +{ + std::function _c = callback; + stm32_interrupt_enable(port,pin,_c,mode); + +} + /** * @brief This function disable the interruption on the selected port/pin * @param port : one of the gpio port @@ -263,6 +270,10 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) } #if defined (STM32F0xx) || defined (STM32L0xx) +#ifdef __cplusplus +extern "C" { +#endif + /** * @brief This function handles external line 0 to 1 interrupt request. * @param None @@ -302,7 +313,13 @@ void EXTI4_15_IRQHandler(void) HAL_GPIO_EXTI_IRQHandler(pin); } } +#ifdef __cplusplus +} +#endif #else +#ifdef __cplusplus +extern "C" { +#endif /** * @brief This function handles external line 0 interrupt request. * @param None @@ -379,6 +396,10 @@ void EXTI15_10_IRQHandler(void) HAL_GPIO_EXTI_IRQHandler(pin); } } + +#ifdef __cplusplus +} +#endif #endif /** * @} @@ -391,8 +412,8 @@ void EXTI15_10_IRQHandler(void) /** * @} */ -#ifdef __cplusplus -} -#endif +// #ifdef __cplusplus +// } +// #endif /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/cores/arduino/stm32/interrupt.h b/cores/arduino/stm32/interrupt.h index 29ab3cba8b..d9c0a61441 100644 --- a/cores/arduino/stm32/interrupt.h +++ b/cores/arduino/stm32/interrupt.h @@ -44,8 +44,16 @@ #include "PinNames.h" #ifdef __cplusplus - extern "C" { +#include +// extern "C" { + +typedef std::function callback_function_t; +void stm32_interrupt_enable(GPIO_TypeDef *port, uint16_t pin, callback_function_t callback, uint32_t mode); +// } #endif +// #ifdef __cplusplus +// extern "C" { +// #endif /* Exported types ------------------------------------------------------------*/ /* Exported constants --------------------------------------------------------*/ @@ -53,9 +61,9 @@ /* Exported functions ------------------------------------------------------- */ void stm32_interrupt_enable(GPIO_TypeDef *port, uint16_t pin, void (*callback)(void), uint32_t mode); void stm32_interrupt_disable(GPIO_TypeDef *port, uint16_t pin); -#ifdef __cplusplus -} -#endif +// #ifdef __cplusplus +// } +// #endif #endif /* __INTERRUPT_H */ diff --git a/cores/arduino/wiring.h b/cores/arduino/wiring.h index a3f13ee0c4..9767b9bbea 100644 --- a/cores/arduino/wiring.h +++ b/cores/arduino/wiring.h @@ -38,13 +38,13 @@ #include "wiring_time.h" #include "WInterrupts.h" -#ifdef __cplusplus -extern "C"{ -#endif // __cplusplus +// #ifdef __cplusplus +// extern "C"{ +// #endif // __cplusplus #include -#ifdef __cplusplus -} -#endif +// #ifdef __cplusplus +// } +// #endif #ifdef __cplusplus #include "HardwareSerial.h" From e8400cbd9d92b8580c2e555506bef7d5d99b6c2f Mon Sep 17 00:00:00 2001 From: straccio Date: Tue, 28 Nov 2017 14:21:06 +0100 Subject: [PATCH 2/5] Removed comments --- cores/arduino/WInterrupts.cpp | 7 ------- cores/arduino/WInterrupts.h | 16 ++-------------- 2 files changed, 2 insertions(+), 21 deletions(-) diff --git a/cores/arduino/WInterrupts.cpp b/cores/arduino/WInterrupts.cpp index cdb78e264b..dbb3e71852 100644 --- a/cores/arduino/WInterrupts.cpp +++ b/cores/arduino/WInterrupts.cpp @@ -19,10 +19,6 @@ #include "WInterrupts.h" #include "Arduino.h" -// #ifdef __cplusplus -// extern "C" { -// #endif - #include "PinAF_STM32F1.h" #include "interrupt.h" @@ -71,6 +67,3 @@ void detachInterrupt(uint32_t pin) return; stm32_interrupt_disable(port, STM_GPIO_PIN(p)); } -// #ifdef __cplusplus -// } -// #endif diff --git a/cores/arduino/WInterrupts.h b/cores/arduino/WInterrupts.h index e33a148eee..7294e90fbe 100644 --- a/cores/arduino/WInterrupts.h +++ b/cores/arduino/WInterrupts.h @@ -20,29 +20,17 @@ #define _WIRING_INTERRUPTS_ #include -// #include "interrupt.h" - #ifdef __cplusplus #include -typedef std::function callback_function_t; - -// extern "C" { -// #endif +typedef std::function callback_function_t; void attachInterrupt(uint32_t pin, callback_function_t callback, uint32_t mode); -// } + #endif -// #ifdef __cplusplus -// extern "C" { -// #endif void attachInterrupt(uint32_t pin, void (*callback)(void), uint32_t mode); void detachInterrupt(uint32_t pin); -// #ifdef __cplusplus -// } -// #endif - #endif /* _WIRING_INTERRUPTS_ */ From c24d0e1885cbc96ce912563cc044999bfa48cdee Mon Sep 17 00:00:00 2001 From: straccio Date: Wed, 29 Nov 2017 08:55:44 +0100 Subject: [PATCH 3/5] Remove comments --- cores/arduino/stm32/interrupt.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/cores/arduino/stm32/interrupt.cpp b/cores/arduino/stm32/interrupt.cpp index 138bf545bf..267174b6f9 100644 --- a/cores/arduino/stm32/interrupt.cpp +++ b/cores/arduino/stm32/interrupt.cpp @@ -63,7 +63,6 @@ /*As we can have only one interrupt/pin id, don't need to get the port info*/ typedef struct { IRQn_Type irqnb; - // void (*callback)(void); std::function callback; uint32_t mode; }gpio_irq_conf_str; From 9240bcf9848b185b9d61bc5e61587f6cdfdb427d Mon Sep 17 00:00:00 2001 From: straccio Date: Wed, 29 Nov 2017 09:40:40 +0100 Subject: [PATCH 4/5] remove comments --- cores/arduino/stm32/interrupt.cpp | 7 ------- 1 file changed, 7 deletions(-) diff --git a/cores/arduino/stm32/interrupt.cpp b/cores/arduino/stm32/interrupt.cpp index 267174b6f9..f2a1e3e12e 100644 --- a/cores/arduino/stm32/interrupt.cpp +++ b/cores/arduino/stm32/interrupt.cpp @@ -49,9 +49,6 @@ #include "stm32_def.h" #include "interrupt.h" -// #ifdef __cplusplus -// extern "C" { -// #endif /** * @} */ @@ -411,8 +408,4 @@ void EXTI15_10_IRQHandler(void) /** * @} */ -// #ifdef __cplusplus -// } -// #endif - /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ From 1441708a77776f33e9112c3a991f506bdd48ed66 Mon Sep 17 00:00:00 2001 From: straccio Date: Wed, 29 Nov 2017 14:54:14 +0100 Subject: [PATCH 5/5] Removed comments --- cores/arduino/board.h | 8 +------- cores/arduino/stm32/interrupt.h | 8 -------- cores/arduino/wiring.h | 6 ------ 3 files changed, 1 insertion(+), 21 deletions(-) diff --git a/cores/arduino/board.h b/cores/arduino/board.h index f7c3a3cb57..194f4ebd7a 100644 --- a/cores/arduino/board.h +++ b/cores/arduino/board.h @@ -4,6 +4,7 @@ /* * Core and peripherals registers definitions */ +#include "interrupt.h" #ifdef __cplusplus extern "C"{ #endif // __cplusplus @@ -13,13 +14,6 @@ extern "C"{ #include "digital_io.h" #include "hal_uart_emul.h" #include "hw_config.h" -#ifdef __cplusplus -} -#endif // __cplusplus -#include "interrupt.h" -#ifdef __cplusplus -extern "C"{ -#endif // __cplusplus #include "spi_com.h" #include "stm32_eeprom.h" #include "timer.h" diff --git a/cores/arduino/stm32/interrupt.h b/cores/arduino/stm32/interrupt.h index d9c0a61441..dd95293c68 100644 --- a/cores/arduino/stm32/interrupt.h +++ b/cores/arduino/stm32/interrupt.h @@ -45,15 +45,10 @@ #ifdef __cplusplus #include -// extern "C" { typedef std::function callback_function_t; void stm32_interrupt_enable(GPIO_TypeDef *port, uint16_t pin, callback_function_t callback, uint32_t mode); -// } #endif -// #ifdef __cplusplus -// extern "C" { -// #endif /* Exported types ------------------------------------------------------------*/ /* Exported constants --------------------------------------------------------*/ @@ -61,9 +56,6 @@ void stm32_interrupt_enable(GPIO_TypeDef *port, uint16_t pin, callback_function_ /* Exported functions ------------------------------------------------------- */ void stm32_interrupt_enable(GPIO_TypeDef *port, uint16_t pin, void (*callback)(void), uint32_t mode); void stm32_interrupt_disable(GPIO_TypeDef *port, uint16_t pin); -// #ifdef __cplusplus -// } -// #endif #endif /* __INTERRUPT_H */ diff --git a/cores/arduino/wiring.h b/cores/arduino/wiring.h index 9767b9bbea..af6cc34621 100644 --- a/cores/arduino/wiring.h +++ b/cores/arduino/wiring.h @@ -38,13 +38,7 @@ #include "wiring_time.h" #include "WInterrupts.h" -// #ifdef __cplusplus -// extern "C"{ -// #endif // __cplusplus #include -// #ifdef __cplusplus -// } -// #endif #ifdef __cplusplus #include "HardwareSerial.h"