From 26a58c25f93a1af4e2e6d9f27d7a676d45ab8e2a Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 10 Jan 2017 16:13:38 +0700 Subject: [PATCH] use RTOS get tick() API for millis() and micros() function. --- cores/nRF5/delay.c | 41 +------------------ cores/nRF5/freertos/source/include/projdefs.h | 8 ++-- cores/nRF5/main.cpp | 10 +---- cores/nRF5/wiring.c | 11 +---- 4 files changed, 10 insertions(+), 60 deletions(-) diff --git a/cores/nRF5/delay.c b/cores/nRF5/delay.c index 0e0b53c2..010f0be3 100644 --- a/cores/nRF5/delay.c +++ b/cores/nRF5/delay.c @@ -25,39 +25,16 @@ extern "C" { #endif -static volatile uint32_t overflows = 0; - uint32_t millis( void ) { - uint64_t ticks = (uint64_t)((uint64_t)overflows << (uint64_t)32) | (uint64_t)(NRF_RTC1->COUNTER); - - return (ticks * 1000) / 32768; + return tick2ms(xTaskGetTickCount()); } uint32_t micros( void ) { - uint64_t ticks = (uint64_t)((uint64_t)overflows << (uint64_t)32) | (uint64_t)(NRF_RTC1->COUNTER); - - return (ticks * 1000000) / 32768; + return tick2us(xTaskGetTickCount()); } -#if 0 -void delay( uint32_t ms ) -{ - if ( ms == 0 ) - { - return ; - } - - uint32_t start = millis() ; - - do - { - yield() ; - } while ( millis() - start < ms ) ; -} -#else - void delay_blocking(uint32_t ms) { if ( ms == 0 ) @@ -78,20 +55,6 @@ void delay( uint32_t ms ) vTaskDelay(ms2tick(ms)); } -#endif - -void RTC1_IRQHandler(void) -{ - NRF_RTC1->EVENTS_OVRFLW = 0; - -#if __CORTEX_M == 0x04 - volatile uint32_t dummy = NRF_RTC1->EVENTS_OVRFLW; - (void)dummy; -#endif - - overflows = (overflows + 1) & 0xff; -} - #ifdef __cplusplus } #endif diff --git a/cores/nRF5/freertos/source/include/projdefs.h b/cores/nRF5/freertos/source/include/projdefs.h index ed21aa72..6caf84e2 100644 --- a/cores/nRF5/freertos/source/include/projdefs.h +++ b/cores/nRF5/freertos/source/include/projdefs.h @@ -78,11 +78,13 @@ typedef void (*TaskFunction_t)( void * ); /* Converts a time in milliseconds to a time in ticks. */ #if configTICK_RATE_HZ == 1000 -#define ms2tick(ms) ms -#define tick2ms(tck) tck +#define ms2tick(ms) (ms) +#define tick2ms(tck) (tck) +#define tick2us(tck) (1000*(tck)) #else #define ms2tick( xTimeInMs ) ( ( TickType_t ) ( ( ( TickType_t ) ( xTimeInMs ) * ( TickType_t ) configTICK_RATE_HZ ) / ( TickType_t ) 1000 ) ) -#define tick2ms(tck) ( ((tck) * 1000) / configTICK_RATE_HZ ) +#define tick2ms(tck) ( ( ((uint64_t)(tck)) * 1000) / configTICK_RATE_HZ ) +#define tick2us(tck) ( ( ((uint64_t)(tck)) * 1000000) / configTICK_RATE_HZ ) #endif diff --git a/cores/nRF5/main.cpp b/cores/nRF5/main.cpp index d8a3c74f..4a9cd94d 100644 --- a/cores/nRF5/main.cpp +++ b/cores/nRF5/main.cpp @@ -35,6 +35,8 @@ static void loop_task(void* arg) { loop(); +// if (serialEventRun) serialEventRun(); + // To compatible with most code where loop is not rtos-aware vTaskDelay(1); //taskYIELD(); } @@ -49,8 +51,6 @@ int main( void ) initVariant(); - delay_blocking(1); - if (setLoopStacksize) { _loopStacksize = setLoopStacksize(); @@ -65,11 +65,5 @@ int main( void ) NVIC_SystemReset(); -// for (;;) -// { -// loop(); -// if (serialEventRun) serialEventRun(); -// } - return 0; } diff --git a/cores/nRF5/wiring.c b/cores/nRF5/wiring.c index e9efa719..81f448ca 100644 --- a/cores/nRF5/wiring.c +++ b/cores/nRF5/wiring.c @@ -27,19 +27,10 @@ extern "C" { void init( void ) { - NVIC_SetPriority(RTC1_IRQn, 15); - NVIC_ClearPendingIRQ(RTC1_IRQn); - NVIC_EnableIRQ(RTC1_IRQn); - NRF_CLOCK->LFCLKSRC = (uint32_t)((CLOCK_LFCLKSRC_SRC_RC << CLOCK_LFCLKSRC_SRC_Pos) & CLOCK_LFCLKSRC_SRC_Msk); NRF_CLOCK->TASKS_LFCLKSTART = 1UL; - - NRF_RTC1->PRESCALER = 0; - NRF_RTC1->EVTENSET = offsetof(NRF_RTC_Type, EVENTS_OVRFLW); - NRF_RTC1->INTENSET = offsetof(NRF_RTC_Type, EVENTS_OVRFLW); - NRF_RTC1->TASKS_START = 1; } #ifdef __cplusplus } -#endif \ No newline at end of file +#endif