diff --git a/cores/arduino/wiring.c b/cores/arduino/wiring.c index f508f7c6b64889ccbdef89303498dce5d6f9f4e1..0f7d19f27a069e2548c9c45d31f510ae148e1af6 100644 --- a/cores/arduino/wiring.c +++ b/cores/arduino/wiring.c @@ -22,81 +22,51 @@ extern "C" { #endif -uint32_t millis( void ) -{ -// todo: ensure no interrupts - return GetTickCount() ; -} - -// Interrupt-compatible version of micros -// Theory: repeatedly take readings of SysTick counter, millis counter and SysTick interrupt pending flag. -// When it appears that millis counter and pending is stable and SysTick hasn't rolled over, use these -// values to calculate micros. If there is a pending SysTick, add one to the millis counter in the calculation. -uint32_t micros( void ) -{ - uint32_t ticks, ticks2; - uint32_t pend, pend2; - uint32_t count, count2; - - ticks2 = SysTick->VAL; - pend2 = !!((SCB->ICSR & SCB_ICSR_PENDSTSET_Msk)||((SCB->SHCSR & SCB_SHCSR_SYSTICKACT_Msk))) ; - count2 = GetTickCount(); - - do { - ticks=ticks2; - pend=pend2; - count=count2; - ticks2 = SysTick->VAL; - pend2 = !!((SCB->ICSR & SCB_ICSR_PENDSTSET_Msk)||((SCB->SHCSR & SCB_SHCSR_SYSTICKACT_Msk))) ; - count2 = GetTickCount(); - } while ((pend != pend2) || (count != count2) || (ticks < ticks2)); - - return ((count+pend) * 1000) + (((SysTick->LOAD - ticks)*(1048576/(F_CPU/1000000)))>>20) ; - // this is an optimization to turn a runtime division into two compile-time divisions and - // a runtime multiplication and shift, saving a few cycles -} +/* + * System Core Clock is at 1MHz at Reset. + * It is switched to 48MHz in the Reset Handler (startup.c) + */ +extern uint32_t SystemCoreClock=1000000ul ; -// original function: -// uint32_t micros( void ) -// { -// uint32_t ticks ; -// uint32_t count ; -// -// SysTick->CTRL; -// do { -// ticks = SysTick->VAL; -// count = GetTickCount(); -// } while (SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk); -// -// return count * 1000 + (SysTick->LOAD + 1 - ticks) / (SystemCoreClock/1000000) ; -// } - - -void delay( uint32_t ms ) -{ - if (ms == 0) - return; - uint32_t start = GetTickCount(); - do { - yield(); - } while (GetTickCount() - start < ms); -} +void __libc_init_array(void); -#if defined ( __ICCARM__ ) /* IAR Ewarm 5.41+ */ -extern signed int putchar( signed int c ) ; -/** - * \brief - * - * \param c Character to output. +/* + * Arduino Zero board initialization * - * \return The character that was output. + * Good to know: + * - Watchdog is disabled by default, unless someone plays with NVM User page + * - */ -extern WEAK signed int putchar( signed int c ) +void init( void ) { - return c ; + // Set Systick to 1ms interval, common to all SAM3 variants + if (SysTick_Config(SystemCoreClock / 1000)) + { + // Capture error + while (true); + } + + + // Initialize C library +// __libc_init_array(); + + // Disable pull-up on every pin + for ( int i = 0 ; i < PINS_COUNT ; i++ ) + { + digitalWrite( i, LOW ) ; + } + + // Initialize Serial port U(S)ART pins + // Todo + + // Initialize USB pins + // Todo + + // Initialize Analog Controller + // Todo } -#endif /* __ICCARM__ */ #ifdef __cplusplus } #endif + diff --git a/cores/arduino/wiring.h b/cores/arduino/wiring.h index b3edbea42d240d019768427f0746440e68bf8dd5..f40c34f33fe82a1bb97c84f7f58d65026bb770cf 100644 --- a/cores/arduino/wiring.h +++ b/cores/arduino/wiring.h @@ -8,7 +8,7 @@ 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. + 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 @@ -23,56 +23,12 @@ extern "C" { #endif -/** - * - */ -extern void init( void ) ; - -/** - * \brief Returns the number of milliseconds since the Arduino board began running the current program. - * - * This number will overflow (go back to zero), after approximately 50 days. - * - * \return Number of milliseconds since the program started (uint32_t) - */ -extern uint32_t millis( void ) ; - -/** - * \brief Returns the number of microseconds since the Arduino board began running the current program. - * - * This number will overflow (go back to zero), after approximately 70 minutes. On 16 MHz Arduino boards - * (e.g. Duemilanove and Nano), this function has a resolution of four microseconds (i.e. the value returned is - * always a multiple of four). On 8 MHz Arduino boards (e.g. the LilyPad), this function has a resolution - * of eight microseconds. - * - * \note There are 1,000 microseconds in a millisecond and 1,000,000 microseconds in a second. - */ -extern uint32_t micros( void ) ; - -/** - * \brief Pauses the program for the amount of time (in miliseconds) specified as parameter. - * (There are 1000 milliseconds in a second.) - * - * \param dwMs the number of milliseconds to pause (uint32_t) - */ -extern void delay( uint32_t dwMs ) ; +extern uint32_t SystemCoreClock ; /** - * \brief Pauses the program for the amount of time (in microseconds) specified as parameter. * - * \param dwUs the number of microseconds to pause (uint32_t) */ -static inline void delayMicroseconds(uint32_t) __attribute__((always_inline, unused)); -static inline void delayMicroseconds(uint32_t usec){ - if (usec == 0) return; - uint32_t n = usec * (VARIANT_MCK / 3000000); - asm volatile( - "L_%=_delayMicroseconds:" "\n\t" - "subs %0, #1" "\n\t" - "bne L_%=_delayMicroseconds" "\n" - : "+r" (n) : - ); -} +extern void init( void ) ; #ifdef __cplusplus }