Browse Source

nrf5/rtc: move RTC hardware resource allocation to board config

Signed-off-by: Joshua Wise <joshua@joshuawise.com>
Joshua Wise 2 tháng trước cách đây
mục cha
commit
7270262dce

+ 3 - 0
src/fw/board/boards/board_asterix.c

@@ -10,6 +10,7 @@
 #include "drivers/nrf5/uart_definitions.h"
 #include "drivers/pwm.h"
 #include "drivers/qspi_definitions.h"
+#include "drivers/rtc.h"
 #include "drivers/temperature.h"
 #include "drivers/voltage_monitor.h"
 #include "flash_region/flash_region.h"
@@ -149,6 +150,8 @@ IRQ_MAP_NRFX(SPI0_SPIM0_SPIS0_TWI0_TWIM0_TWIS0, nrfx_twim_0_irq_handler);
 PwmState BACKLIGHT_PWM_STATE;
 IRQ_MAP_NRFX(PWM0, nrfx_pwm_0_irq_handler);
 
+IRQ_MAP_NRFX(RTC1, rtc_irq_handler);
+
 void board_early_init(void) {
   PBL_LOG(LOG_LEVEL_ERROR, "asterix early init");
 

+ 3 - 1
src/fw/board/boards/board_asterix.h

@@ -6,6 +6,8 @@
 
 #define BOARD_LSE_MODE RCC_LSE_Bypass
 
+#define BOARD_RTC_INST NRF_RTC1
+
 static const BoardConfig BOARD_CONFIG = {
   .ambient_light_dark_threshold = 150,
   .ambient_k_delta_threshold = 50,
@@ -247,4 +249,4 @@ extern QSPIFlash * const QSPI_FLASH;
 
 extern MicDevice * const MIC;
 
-extern I2CSlavePort * const I2C_NPM1300;
+extern I2CSlavePort * const I2C_NPM1300;

+ 14 - 18
src/fw/drivers/nrf5/rtc.c

@@ -28,10 +28,6 @@
 #include <stdio.h>
 #include <string.h>
 
-#define OS_RTC_INST NRF_RTC1
-static void prv_rtc_irq_handler(void);
-IRQ_MAP_NRFX(RTC1, prv_rtc_irq_handler);
-
 //! The type of a raw reading from the RTC (masked to 0xFFFFFF).
 typedef uint32_t RtcIntervalTicks;
 
@@ -51,7 +47,7 @@ static RtcIntervalTicks prv_elapsed_ticks(RtcIntervalTicks before, RtcIntervalTi
 }
 
 static RtcIntervalTicks prv_get_rtc_interval_ticks(void) {
-  return nrf_rtc_counter_get(OS_RTC_INST);
+  return nrf_rtc_counter_get(BOARD_RTC_INST);
 }
 
 /***
@@ -317,8 +313,8 @@ void rtc_init(void) {
     PBL_LOG(LOG_LEVEL_INFO, "RTC appears to have been reset :( hope you have your phone connected");
   }
 
-  nrf_rtc_prescaler_set(OS_RTC_INST, NRF_RTC_FREQ_TO_PRESCALER(RTC_TICKS_HZ));
-  nrf_rtc_task_trigger(OS_RTC_INST, NRF_RTC_TASK_START);
+  nrf_rtc_prescaler_set(BOARD_RTC_INST, NRF_RTC_FREQ_TO_PRESCALER(RTC_TICKS_HZ));
+  nrf_rtc_task_trigger(BOARD_RTC_INST, NRF_RTC_TASK_START);
   
   prv_restore_rtc_time_state();
   s_did_init_rtc = true;
@@ -342,16 +338,16 @@ static RtcTicks s_alarm_expiry_time = 0;
 static bool s_tick_alarm_initialized = false;
 
 void rtc_alarm_init(void) {
-  nrf_rtc_event_disable(OS_RTC_INST, NRF_RTC_EVENT_COMPARE_0);
-  nrf_rtc_int_disable(OS_RTC_INST, NRF_RTC_INT_COMPARE0_MASK);
+  nrf_rtc_event_disable(BOARD_RTC_INST, NRF_RTC_EVENT_COMPARE_0);
+  nrf_rtc_int_disable(BOARD_RTC_INST, NRF_RTC_INT_COMPARE0_MASK);
   s_tick_alarm_initialized = true;
 }
 
 void rtc_alarm_set(RtcTicks num_ticks) {
   PBL_ASSERTN(s_tick_alarm_initialized);
   
-  nrf_rtc_event_disable(OS_RTC_INST, NRF_RTC_EVENT_COMPARE_0);
-  nrf_rtc_event_clear(OS_RTC_INST, NRF_RTC_EVENT_COMPARE_0);
+  nrf_rtc_event_disable(BOARD_RTC_INST, NRF_RTC_EVENT_COMPARE_0);
+  nrf_rtc_event_clear(BOARD_RTC_INST, NRF_RTC_EVENT_COMPARE_0);
   
   s_alarm_set_time = rtc_get_ticks();
   s_alarm_expiry_time = s_alarm_set_time + num_ticks;
@@ -360,10 +356,10 @@ void rtc_alarm_set(RtcTicks num_ticks) {
    * rtc_alarm_set, so we're not going to wrap around more than once -- one
    * minute is always less than 4.5 hours.
    */
-  nrf_rtc_cc_set(OS_RTC_INST, 0, s_alarm_expiry_time & RTC_COUNTER_COUNTER_Msk);
+  nrf_rtc_cc_set(BOARD_RTC_INST, 0, s_alarm_expiry_time & RTC_COUNTER_COUNTER_Msk);
   
-  nrf_rtc_event_enable(OS_RTC_INST, NRF_RTC_EVENT_COMPARE_0);
-  nrf_rtc_int_enable(OS_RTC_INST, NRF_RTC_INT_COMPARE0_MASK);
+  nrf_rtc_event_enable(BOARD_RTC_INST, NRF_RTC_EVENT_COMPARE_0);
+  nrf_rtc_int_enable(BOARD_RTC_INST, NRF_RTC_INT_COMPARE0_MASK);
 }
 
 RtcTicks rtc_alarm_get_elapsed_ticks(void) {
@@ -376,8 +372,8 @@ bool rtc_alarm_is_initialized(void) {
 
 //! Handler for the RTC alarm interrupt. We don't actually have to do anything in this handler,
 //! just the interrupt firing is enough to bring us out of stop mode.
-static void prv_rtc_irq_handler(void) {
-  nrf_rtc_event_disable(OS_RTC_INST, NRF_RTC_EVENT_COMPARE_0);
-  nrf_rtc_event_clear(OS_RTC_INST, NRF_RTC_EVENT_COMPARE_0);
-  nrf_rtc_int_disable(OS_RTC_INST, NRF_RTC_INT_COMPARE0_MASK);
+void rtc_irq_handler(void) {
+  nrf_rtc_event_disable(BOARD_RTC_INST, NRF_RTC_EVENT_COMPARE_0);
+  nrf_rtc_event_clear(BOARD_RTC_INST, NRF_RTC_EVENT_COMPARE_0);
+  nrf_rtc_int_disable(BOARD_RTC_INST, NRF_RTC_INT_COMPARE0_MASK);
 }

+ 3 - 0
src/fw/drivers/rtc.h

@@ -115,3 +115,6 @@ bool rtc_alarm_is_initialized(void);
 //! @param buffer Buffer used to write the string into. Must be at least TIME_STRING_BUFFER_SIZE
 const char* time_t_to_string(char* buffer, time_t t);
 
+#if MICRO_FAMILY_NRF5
+void rtc_irq_handler(void);
+#endif