From d9df90830ef6fa80d3a8cb29c039d85793e3b037 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Thu, 25 Jun 2015 20:07:24 +0200 Subject: [PATCH] Fixed cortex-M hooks for RTOS Fixes #4 --- cores/arduino/Tone.cpp | 3 +- cores/arduino/cortex_handlers.c | 178 ++++++++++++++++++++++++++++++++ cores/arduino/delay.c | 4 +- cores/arduino/startup.c | 173 ------------------------------- 4 files changed, 181 insertions(+), 177 deletions(-) create mode 100644 cores/arduino/cortex_handlers.c diff --git a/cores/arduino/Tone.cpp b/cores/arduino/Tone.cpp index 84b8bae10..1dff8ab37 100644 --- a/cores/arduino/Tone.cpp +++ b/cores/arduino/Tone.cpp @@ -34,6 +34,7 @@ volatile bool toneIsActive = false; #define TONE_TC_IRQn TC5_IRQn #define TONE_TC_TOP 0xFFFF #define TONE_TC_CHANNEL 0 +void TC5_Handler (void) __attribute__ ((weak, alias("Tone_Handler"))); static inline void resetTC (Tc* TCx) { @@ -183,8 +184,6 @@ void Tone_Handler (void) } } -void TC5_Handler (void) __attribute__ ((weak, alias("Tone_Handler"))); - #ifdef __cplusplus } #endif diff --git a/cores/arduino/cortex_handlers.c b/cores/arduino/cortex_handlers.c new file mode 100644 index 000000000..fed128c44 --- /dev/null +++ b/cores/arduino/cortex_handlers.c @@ -0,0 +1,178 @@ +/* + Copyright (c) 2015 Arduino LLC. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "sam.h" +#include "variant.h" + +/* RTOS Hooks */ +extern void svcHook(void); +extern void pendSVHook(void); +extern int sysTickHook(void); + +/* Default empty handler */ + +void Dummy_Handler(void) +{ +#if defined DEBUG + __BKPT(3); +#endif + for (;;) { } +} + +/* Cortex-M0+ core handlers */ +void HardFault_Handler(void) __attribute__ ((weak, alias("Dummy_Handler"))); +void Reest_Handler (void); +void NMI_Handler (void) __attribute__ ((weak, alias("Dummy_Handler"))); +void SVC_Handler (void) __attribute__ ((weak, alias("Dummy_Handler"))); +void PendSV_Handler (void) __attribute__ ((weak, alias("Dummy_Handler"))); +void SysTick_Handler (void); + +/* Peripherals handlers */ +void PM_Handler (void) __attribute__ ((weak, alias("Dummy_Handler"))); +void SYSCTRL_Handler (void) __attribute__ ((weak, alias("Dummy_Handler"))); +void WDT_Handler (void) __attribute__ ((weak, alias("Dummy_Handler"))); +void RTC_Handler (void) __attribute__ ((weak, alias("Dummy_Handler"))); +void EIC_Handler (void) __attribute__ ((weak, alias("Dummy_Handler"))); +void NVMCTRL_Handler (void) __attribute__ ((weak, alias("Dummy_Handler"))); +void DMAC_Handler (void) __attribute__ ((weak, alias("Dummy_Handler"))); +void USB_Handler (void) __attribute__ ((weak, alias("Dummy_Handler"))); +void EVSYS_Handler (void) __attribute__ ((weak, alias("Dummy_Handler"))); +void SERCOM0_Handler (void) __attribute__ ((weak, alias("Dummy_Handler"))); +void SERCOM1_Handler (void) __attribute__ ((weak, alias("Dummy_Handler"))); +void SERCOM2_Handler (void) __attribute__ ((weak, alias("Dummy_Handler"))); +void SERCOM3_Handler (void) __attribute__ ((weak, alias("Dummy_Handler"))); +void SERCOM4_Handler (void) __attribute__ ((weak, alias("Dummy_Handler"))); +void SERCOM5_Handler (void) __attribute__ ((weak, alias("Dummy_Handler"))); +void TCC0_Handler (void) __attribute__ ((weak, alias("Dummy_Handler"))); +void TCC1_Handler (void) __attribute__ ((weak, alias("Dummy_Handler"))); +void TCC2_Handler (void) __attribute__ ((weak, alias("Dummy_Handler"))); +void TC3_Handler (void) __attribute__ ((weak, alias("Dummy_Handler"))); +void TC4_Handler (void) __attribute__ ((weak, alias("Dummy_Handler"))); +void TC5_Handler (void) __attribute__ ((weak)); // Used in Tone.cpp +void TC6_Handler (void) __attribute__ ((weak, alias("Dummy_Handler"))); +void TC7_Handler (void) __attribute__ ((weak, alias("Dummy_Handler"))); +void ADC_Handler (void) __attribute__ ((weak, alias("Dummy_Handler"))); +void AC_Handler (void) __attribute__ ((weak, alias("Dummy_Handler"))); +void DAC_Handler (void) __attribute__ ((weak, alias("Dummy_Handler"))); +void PTC_Handler (void) __attribute__ ((weak, alias("Dummy_Handler"))); +void I2S_Handler (void) __attribute__ ((weak, alias("Dummy_Handler"))); + +/* Initialize segments */ +extern uint32_t __etext; +extern uint32_t __data_start__; +extern uint32_t __data_end__; +extern uint32_t __bss_start__; +extern uint32_t __bss_end__; +extern uint32_t __StackTop; + +/* Exception Table */ +__attribute__ ((section(".isr_vector"))) const DeviceVectors exception_table = +{ + /* Configure Initial Stack Pointer, using linker-generated symbols */ + (void*) (&__StackTop), + + (void*) Reset_Handler, + (void*) NMI_Handler, + (void*) HardFault_Handler, + (void*) (0UL), /* Reserved */ + (void*) (0UL), /* Reserved */ + (void*) (0UL), /* Reserved */ + (void*) (0UL), /* Reserved */ + (void*) (0UL), /* Reserved */ + (void*) (0UL), /* Reserved */ + (void*) (0UL), /* Reserved */ + (void*) SVC_Handler, + (void*) (0UL), /* Reserved */ + (void*) (0UL), /* Reserved */ + (void*) PendSV_Handler, + (void*) SysTick_Handler, + + /* Configurable interrupts */ + (void*) PM_Handler, /* 0 Power Manager */ + (void*) SYSCTRL_Handler, /* 1 System Control */ + (void*) WDT_Handler, /* 2 Watchdog Timer */ + (void*) RTC_Handler, /* 3 Real-Time Counter */ + (void*) EIC_Handler, /* 4 External Interrupt Controller */ + (void*) NVMCTRL_Handler, /* 5 Non-Volatile Memory Controller */ + (void*) DMAC_Handler, /* 6 Direct Memory Access Controller */ + (void*) USB_Handler, /* 7 Universal Serial Bus */ + (void*) EVSYS_Handler, /* 8 Event System Interface */ + (void*) SERCOM0_Handler, /* 9 Serial Communication Interface 0 */ + (void*) SERCOM1_Handler, /* 10 Serial Communication Interface 1 */ + (void*) SERCOM2_Handler, /* 11 Serial Communication Interface 2 */ + (void*) SERCOM3_Handler, /* 12 Serial Communication Interface 3 */ + (void*) SERCOM4_Handler, /* 13 Serial Communication Interface 4 */ + (void*) SERCOM5_Handler, /* 14 Serial Communication Interface 5 */ + (void*) TCC0_Handler, /* 15 Timer Counter Control 0 */ + (void*) TCC1_Handler, /* 16 Timer Counter Control 1 */ + (void*) TCC2_Handler, /* 17 Timer Counter Control 2 */ + (void*) TC3_Handler, /* 18 Basic Timer Counter 0 */ + (void*) TC4_Handler, /* 19 Basic Timer Counter 1 */ + (void*) TC5_Handler, /* 20 Basic Timer Counter 2 */ + (void*) TC6_Handler, /* 21 Basic Timer Counter 3 */ + (void*) TC7_Handler, /* 22 Basic Timer Counter 4 */ + (void*) ADC_Handler, /* 23 Analog Digital Converter */ + (void*) AC_Handler, /* 24 Analog Comparators */ + (void*) DAC_Handler, /* 25 Digital Analog Converter */ + (void*) PTC_Handler, /* 26 Peripheral Touch Controller */ + (void*) I2S_Handler /* 27 Inter-IC Sound Interface */ +}; + +extern int main(void); +extern void __libc_init_array(void); + +/* This is called on processor reset to initialize the device and call main() */ +void Reset_Handler(void) +{ + uint32_t *pSrc, *pDest; + + /* Initialize the initialized data section */ + pSrc = &__etext; + pDest = &__data_start__; + + if ((&__data_start__ != &__data_end__) && (pSrc != pDest)) { + for (; pDest < &__data_end__; pDest++, pSrc++) + *pDest = *pSrc; + } + + /* Clear the zero section */ + if ((&__data_start__ != &__data_end__) && (pSrc != pDest)) { + for (pDest = &__bss_start__; pDest < &__bss_end__; pDest++) + *pDest = 0; + } + + /* Initialize the C library */ + __libc_init_array(); + + SystemInit(); + + main(); + + while (1) + ; +} + +/* Default Arduino systick handler */ +extern void SysTick_DefaultHandler(void); + +void SysTick_Handler(void) +{ + if (sysTickHook()) + return; + SysTick_DefaultHandler(); +} diff --git a/cores/arduino/delay.c b/cores/arduino/delay.c index 98aa097ef..e43131f21 100644 --- a/cores/arduino/delay.c +++ b/cores/arduino/delay.c @@ -78,10 +78,10 @@ void delay( uint32_t ms ) #include "Reset.h" // for tickReset() -void SysTick_Handler( void ) +void SysTick_DefaultHandler(void) { // Increment tick count each ms - _ulTickCount++ ; + _ulTickCount++; tickReset(); } diff --git a/cores/arduino/startup.c b/cores/arduino/startup.c index a62cb08ca..9a43b4a8e 100644 --- a/cores/arduino/startup.c +++ b/cores/arduino/startup.c @@ -19,123 +19,6 @@ #include "sam.h" #include "variant.h" -/* Initialize segments */ -extern uint32_t __etext ; -extern uint32_t __data_start__ ; -extern uint32_t __data_end__ ; -extern uint32_t __bss_start__ ; -extern uint32_t __bss_end__ ; -extern uint32_t __StackTop; - -extern int main( void ) ; -extern void __libc_init_array(void); - -/* Default empty handler */ -void Dummy_Handler(void); - -/* Cortex-M0+ core handlers */ -#if defined DEBUG -void HardFault_Handler( void ) -{ - __BKPT( 3 ) ; - - while ( 1 ) - { - } -} -#else -void HardFault_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -#endif //DEBUG - -void NMI_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void SVC_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void PendSV_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void SysTick_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); - -/* Peripherals handlers */ -void PM_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void SYSCTRL_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void WDT_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void RTC_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void EIC_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void NVMCTRL_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void DMAC_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void USB_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void EVSYS_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void SERCOM0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void SERCOM1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void SERCOM2_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void SERCOM3_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void SERCOM4_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void SERCOM5_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void TCC0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void TCC1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void TCC2_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void TC3_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void TC4_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void TC5_Handler ( void ) __attribute__ ((weak)); -void TC6_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void TC7_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void ADC_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void AC_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void DAC_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void PTC_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); -void I2S_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler"))); - -/* Exception Table */ -__attribute__ ((section(".isr_vector"))) -const DeviceVectors exception_table= -{ - /* Configure Initial Stack Pointer, using linker-generated symbols */ - (void*) (&__StackTop), - - (void*) Reset_Handler, - (void*) NMI_Handler, - (void*) HardFault_Handler, - (void*) (0UL), /* Reserved */ - (void*) (0UL), /* Reserved */ - (void*) (0UL), /* Reserved */ - (void*) (0UL), /* Reserved */ - (void*) (0UL), /* Reserved */ - (void*) (0UL), /* Reserved */ - (void*) (0UL), /* Reserved */ - (void*) SVC_Handler, - (void*) (0UL), /* Reserved */ - (void*) (0UL), /* Reserved */ - (void*) PendSV_Handler, - (void*) SysTick_Handler, - - /* Configurable interrupts */ - (void*) PM_Handler, /* 0 Power Manager */ - (void*) SYSCTRL_Handler, /* 1 System Control */ - (void*) WDT_Handler, /* 2 Watchdog Timer */ - (void*) RTC_Handler, /* 3 Real-Time Counter */ - (void*) EIC_Handler, /* 4 External Interrupt Controller */ - (void*) NVMCTRL_Handler, /* 5 Non-Volatile Memory Controller */ - (void*) DMAC_Handler, /* 6 Direct Memory Access Controller */ - (void*) USB_Handler, /* 7 Universal Serial Bus */ - (void*) EVSYS_Handler, /* 8 Event System Interface */ - (void*) SERCOM0_Handler, /* 9 Serial Communication Interface 0 */ - (void*) SERCOM1_Handler, /* 10 Serial Communication Interface 1 */ - (void*) SERCOM2_Handler, /* 11 Serial Communication Interface 2 */ - (void*) SERCOM3_Handler, /* 12 Serial Communication Interface 3 */ - (void*) SERCOM4_Handler, /* 13 Serial Communication Interface 4 */ - (void*) SERCOM5_Handler, /* 14 Serial Communication Interface 5 */ - (void*) TCC0_Handler, /* 15 Timer Counter Control 0 */ - (void*) TCC1_Handler, /* 16 Timer Counter Control 1 */ - (void*) TCC2_Handler, /* 17 Timer Counter Control 2 */ - (void*) TC3_Handler, /* 18 Basic Timer Counter 0 */ - (void*) TC4_Handler, /* 19 Basic Timer Counter 1 */ - (void*) TC5_Handler, /* 20 Basic Timer Counter 2 */ - (void*) TC6_Handler, /* 21 Basic Timer Counter 3 */ - (void*) TC7_Handler, /* 22 Basic Timer Counter 4 */ - (void*) ADC_Handler, /* 23 Analog Digital Converter */ - (void*) AC_Handler, /* 24 Analog Comparators */ - (void*) DAC_Handler, /* 25 Digital Analog Converter */ - (void*) PTC_Handler, /* 26 Peripheral Touch Controller */ - (void*) I2S_Handler /* 27 Inter-IC Sound Interface */ -} ; - /** * \brief SystemInit() configures the needed clocks and according Flash Read Wait States. * At reset: @@ -340,59 +223,3 @@ void SystemInit( void ) ADC->CALIB.reg = ADC_CALIB_BIAS_CAL(bias) | ADC_CALIB_LINEARITY_CAL(linearity); } -/** - * \brief This is the code that gets called on processor reset. - * To initialize the device, and call the main() routine. - */ -void Reset_Handler( void ) -{ - uint32_t *pSrc, *pDest; - - /* Initialize the initialized data section */ - pSrc = &__etext; - pDest = &__data_start__; - - if ( (&__data_start__ != &__data_end__) && (pSrc != pDest) ) - { - for (; pDest < &__data_end__ ; pDest++, pSrc++ ) - { - *pDest = *pSrc ; - } - } - - /* Clear the zero section */ - if ( (&__data_start__ != &__data_end__) && (pSrc != pDest) ) - { - for ( pDest = &__bss_start__ ; pDest < &__bss_end__ ; pDest++ ) - { - *pDest = 0 ; - } - } - - /* Initialize the C library */ - __libc_init_array(); - - SystemInit() ; - - /* Branch to main function */ - main() ; - - /* Infinite loop */ - while ( 1 ) - { - } -} - -/** - * \brief Default interrupt handler for unused IRQs. - */ -void Dummy_Handler( void ) -{ -#if defined DEBUG - __BKPT( 3 ) ; -#endif // DEBUG - - while ( 1 ) - { - } -}