From 7449f8d861f9fb57c7e82b9808b6081bfa14d6f1 Mon Sep 17 00:00:00 2001 From: Glenn Ruben Bakke Date: Sun, 30 Apr 2017 19:42:19 +0200 Subject: [PATCH] nrf5/hal/timer: Changing hardcoded hal timer instance base to a lookup, so that IRQ num can be detected automatically without the need of using struct param on it. Size of binary does not increase when using Os. --- nrf5/hal/hal_timer.h | 45 ++++++++++++++++++------------------ nrf5/modules/machine/timer.c | 6 ++--- 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/nrf5/hal/hal_timer.h b/nrf5/hal/hal_timer.h index 18905c9096..a9dffad2aa 100644 --- a/nrf5/hal/hal_timer.h +++ b/nrf5/hal/hal_timer.h @@ -30,31 +30,30 @@ #include "nrf.h" #if NRF51 - -#define TIMER0 ((NRF_TIMER_Type *) NRF_TIMER0) -#define TIMER0_IRQ_NUM TIMER0_IRQn -#define TIMER1 ((NRF_TIMER_Type *) NRF_TIMER1) -#define TIMER1_IRQ_NUM TIMER1_IRQn -#define TIMER2 ((NRF_TIMER_Type *) NRF_TIMER2) -#define TIMER2_IRQ_NUM TIMER2_IRQn - -#elif NRF52 - -#define TIMER0 ((NRF_TIMER_Type *) NRF_TIMER0) -#define TIMER0_IRQ_NUM TIMER0_IRQn -#define TIMER1 ((NRF_TIMER_Type *) NRF_TIMER1) -#define TIMER1_IRQ_NUM TIMER1_IRQn -#define TIMER2 ((NRF_TIMER_Type *) NRF_TIMER2) -#define TIMER2_IRQ_NUM TIMER2_IRQn -#define TIMER3 ((NRF_TIMER_Type *) NRF_TIMER3) -#define TIMER3_IRQ_NUM TIMER3_IRQn -#define TIMER4 ((NRF_TIMER_Type *) NRF_TIMER4) -#define TIMER4_IRQ_NUM TIMER4_IRQn - -#else -#error "Device not supported." + #define TIMER_BASE_POINTERS (const uint32_t[]){NRF_TIMER0_BASE, \ + NRF_TIMER1_BASE, \ + NRF_TIMER2_BASE} + #define TIMER_IRQ_VALUES (const uint32_t[]){TIMER0_IRQn, \ + TIMER1_IRQn, \ + TIMER2_IRQn} #endif +#if NRF52 + #define TIMER_BASE_POINTERS (const uint32_t[]){NRF_TIMER0_BASE, \ + NRF_TIMER1_BASE, \ + NRF_TIMER1_BASE, \ + NRF_TIMER1_BASE, \ + NRF_TIMER2_BASE} + #define TIMER_IRQ_VALUES (const uint32_t[]){TIMER0_IRQn, \ + TIMER1_IRQn, \ + TIMER2_IRQn, \ + TIMER3_IRQn, \ + TIMER4_IRQn} +#endif + +#define TIMER_BASE(x) ((NRF_TIMER_Type *)TIMER_BASE_POINTERS[x]) +#define TIMER_IRQ_NUM(x) (TIMER_IRQ_VALUES[x]) + /** * @brief Timer Configuration Structure definition */ diff --git a/nrf5/modules/machine/timer.c b/nrf5/modules/machine/timer.c index b973800963..6329aaab3d 100644 --- a/nrf5/modules/machine/timer.c +++ b/nrf5/modules/machine/timer.c @@ -53,11 +53,11 @@ STATIC const machine_timer_obj_t machine_timer_obj[] = { void timer_init0(void) { // reset the Timer handles memset(&TimerHandle0, 0, sizeof(Timer_HandleTypeDef)); - TimerHandle0.instance = TIMER0; + TimerHandle0.instance = TIMER_BASE(0); memset(&TimerHandle1, 0, sizeof(Timer_HandleTypeDef)); - TimerHandle0.instance = TIMER1; + TimerHandle0.instance = TIMER_BASE(1); memset(&TimerHandle2, 0, sizeof(Timer_HandleTypeDef)); - TimerHandle0.instance = TIMER2; + TimerHandle0.instance = TIMER_BASE(2); } STATIC int timer_find(mp_obj_t id) {