From aa9ad6be014e83aa77e39f1cf0915178d6e2005c Mon Sep 17 00:00:00 2001 From: Glenn Ruben Bakke Date: Wed, 24 May 2017 23:30:27 +0200 Subject: [PATCH] nrf5/modules/music: Update ticker and modmusic to share global ticks counter as a volatile variable. Use Timer1 hardware peripheral instead of instance 0. Timer0 is not free if used in combination with a bluetooth stack. Update IRQ priority to levels that are compatible in use with a bluetooth stack for both nrf51 and nrf52. Apply nrf51 PAN fixes for Timer1 instead of original Timer0. --- nrf5/drivers/ticker.c | 29 ++++++++++++++++++++--------- nrf5/modules/music/modmusic.c | 2 +- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/nrf5/drivers/ticker.c b/nrf5/drivers/ticker.c index 3e439a96d8..bbba231213 100644 --- a/nrf5/drivers/ticker.c +++ b/nrf5/drivers/ticker.c @@ -31,21 +31,21 @@ #include "ticker.h" #include "hal_irq.h" -#define FastTicker NRF_TIMER0 -#define FastTicker_IRQn TIMER0_IRQn -#define FastTicker_IRQHandler TIMER0_IRQHandler +#define FastTicker NRF_TIMER1 +#define FastTicker_IRQn TIMER1_IRQn +#define FastTicker_IRQHandler TIMER1_IRQHandler #define SlowTicker_IRQn SWI0_IRQn #define SlowTicker_IRQHandler SWI0_IRQHandler // Ticker callback function called every MACRO_TICK -static callback_ptr slow_ticker; +static volatile callback_ptr slow_ticker; void ticker_init(callback_ptr slow_ticker_callback) { slow_ticker = slow_ticker_callback; NRF_TIMER_Type *ticker = FastTicker; -#if NRF51 +#ifdef NRF51 ticker->POWER = 1; #endif __NOP(); @@ -58,31 +58,42 @@ void ticker_init(callback_ptr slow_ticker_callback) { ticker->INTENSET = TIMER_INTENSET_COMPARE3_Msk; ticker->SHORTS = 0; +#ifdef NRF51 hal_irq_priority(FastTicker_IRQn, 1); +#else + hal_irq_priority(FastTicker_IRQn, 2); +#endif + hal_irq_priority(SlowTicker_IRQn, 3); + hal_irq_priority(SlowTicker_IRQn, 3); + hal_irq_enable(SlowTicker_IRQn); } -/* Start and stop timer 0 including workarounds for Anomaly 73 for Timer +/* Start and stop timer 1 including workarounds for Anomaly 73 for Timer * http://www.nordicsemi.com/eng/content/download/29490/494569/file/nRF51822-PAN%20v3.0.pdf */ void ticker_start(void) { hal_irq_enable(FastTicker_IRQn); - *(uint32_t *)0x40008C0C = 1; //for Timer 0 +#ifdef NRF51 + *(uint32_t *)0x40009C0C = 1; // for Timer 1 +#endif FastTicker->TASKS_START = 1; } void ticker_stop(void) { hal_irq_disable(FastTicker_IRQn); FastTicker->TASKS_STOP = 1; - *(uint32_t *)0x40008C0C = 0; //for Timer 0 +#ifdef NRF51 + *(uint32_t *)0x40009C0C = 0; // for Timer 1 +#endif } int32_t noop(void) { return -1; } -uint32_t ticks; +volatile uint32_t ticks; static ticker_callback_ptr callbacks[3] = { noop, noop, noop }; diff --git a/nrf5/modules/music/modmusic.c b/nrf5/modules/music/modmusic.c index 7e8ff30dbd..029200ea9d 100644 --- a/nrf5/modules/music/modmusic.c +++ b/nrf5/modules/music/modmusic.c @@ -72,7 +72,7 @@ enum { #define music_data MP_STATE_PORT(music_data) -extern uint32_t ticks; +extern volatile uint32_t ticks; STATIC uint32_t start_note(const char *note_str, size_t note_len, const pin_obj_t *pin);