Skip to content
Snippets Groups Projects
Commit b3398c81 authored by Thibaut VIARD's avatar Thibaut VIARD
Browse files

First draft for Wiring.c

parent 336de714
No related branches found
No related tags found
No related merge requests found
...@@ -22,81 +22,51 @@ ...@@ -22,81 +22,51 @@
extern "C" { extern "C" {
#endif #endif
uint32_t millis( void ) /*
{ * System Core Clock is at 1MHz at Reset.
// todo: ensure no interrupts * It is switched to 48MHz in the Reset Handler (startup.c)
return GetTickCount() ; */
} extern uint32_t SystemCoreClock=1000000ul ;
// 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
}
// original function: void __libc_init_array(void);
// 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);
}
#if defined ( __ICCARM__ ) /* IAR Ewarm 5.41+ */ /*
extern signed int putchar( signed int c ) ; * Arduino Zero board initialization
/**
* \brief
*
* \param c Character to output.
* *
* \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 #ifdef __cplusplus
} }
#endif #endif
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
This library is distributed in the hope that it will be useful, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of 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. See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public You should have received a copy of the GNU Lesser General Public
...@@ -23,56 +23,12 @@ ...@@ -23,56 +23,12 @@
extern "C" { extern "C" {
#endif #endif
/** extern uint32_t SystemCoreClock ;
*
*/
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 ) ;
/** /**
* \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)); extern void init( void ) ;
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) :
);
}
#ifdef __cplusplus #ifdef __cplusplus
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment