23 #include "gpio_level_handler.h" 25 #include "hal_clocks.h" 26 #include "common_util.h" 28 #define RTC_USED CONCAT_2(NRF_RTC, AUX_CLK_LFCLK_RTC_USED) 35 #define RTC_IRQ_Handler_a(n) RTC_IRQ_Handler_b(n) 36 #define RTC_IRQ_Handler_b(n) RTC##n##_IRQHandler 38 #define RTC_IRQN_a(n) RTC_IRQN_b(n) 39 #define RTC_IRQN_b(n) RTC##n##_IRQn 41 #define RTC_IRQN RTC_IRQN_a(AUX_CLK_LFCLK_RTC_USED) 42 #define RTC_IRQ_Handler RTC_IRQ_Handler_a(AUX_CLK_LFCLK_RTC_USED) 44 #define RTC_TICKS_MS(x) (uint32_t)(ROUNDED_DIV((32768 * x),1000)) 47 #define TIMER_USED CONCAT_2(NRF_TIMER, AUX_CLK_HFCLK_TIMER_USED) 50 #define TIMER_IRQ_Handler TIMER_IRQ_Handler_a(AUX_CLK_HFCLK_TIMER_USED) 51 #define TIMER_IRQ_Handler_a(n) TIMER_IRQ_Handler_b(n) 52 #define TIMER_IRQ_Handler_b(n) TIMER##n##_IRQHandler 54 #define TIMER_IRQN TIMER_IRQN_a(AUX_CLK_HFCLK_TIMER_USED) 55 #define TIMER_IRQN_a(n) TIMER_IRQN_b(n) 56 #define TIMER_IRQN_b(n) TIMER##n##_IRQn 58 #define TIMER_TICKS_MS(x) (x * 1000) 62 void (*callbac_buffer) (uint8_t events);
69 static aux_clk_ppi_t g_arr_ppi_cnf[AUX_CLK_PPI_CHANNELS_USED];
72 void aux_clk_rtc_handler (
void)
74 void RTC_IRQ_Handler(
void)
78 if(RTC_USED->EVENTS_COMPARE[0] == 1)
82 RTC_USED->EVENTS_COMPARE[0] = 0;
85 if(RTC_USED->EVENTS_COMPARE[1] == 1)
89 RTC_USED->EVENTS_COMPARE[1] = 0;
92 if(RTC_USED->EVENTS_COMPARE[2] == 1)
96 RTC_USED->EVENTS_COMPARE[2] = 0;
99 if(RTC_USED->EVENTS_COMPARE[3] == 1)
103 RTC_USED->EVENTS_COMPARE[3] = 0;
106 callbac_buffer (events);
110 void aux_clk_timer_handler (
void)
112 void TIMER_IRQ_Handler (
void)
116 if(TIMER_USED->EVENTS_COMPARE[0] == 1)
120 TIMER_USED->EVENTS_COMPARE[0] = 0;
123 if(TIMER_USED->EVENTS_COMPARE[1] == 1)
127 TIMER_USED->EVENTS_COMPARE[1] = 0;
130 if(TIMER_USED->EVENTS_COMPARE[2] == 1)
134 TIMER_USED->EVENTS_COMPARE[2] = 0;
137 if(TIMER_USED->EVENTS_COMPARE[3] == 1)
141 TIMER_USED->EVENTS_COMPARE[3] = 0;
144 callbac_buffer (events);
149 #if AUX_CLK_HFCLK_SOLO_MODULE == 1 152 if(g_irq_priority != APP_IRQ_PRIORITY_THREAD)
154 NVIC_SetPriority (TIMER_IRQN, g_irq_priority);
155 NVIC_EnableIRQ (TIMER_IRQN);
157 TIMER_USED->TASKS_START = 1;
158 (void) TIMER_USED->TASKS_START;
163 #if AUX_CLK_HFCLK_SOLO_MODULE == 1 166 if(g_irq_priority != APP_IRQ_PRIORITY_THREAD)
168 NVIC_SetPriority (RTC_IRQN, g_irq_priority);
169 NVIC_EnableIRQ (RTC_IRQN);
171 RTC_USED->TASKS_START = 1;
172 (void) RTC_USED->TASKS_START;
175 uint32_t select_event (uint32_t evt)
187 &RTC_USED->EVENTS_COMPARE[0] :
188 &TIMER_USED->EVENTS_COMPARE[0]);
193 &RTC_USED->EVENTS_COMPARE[1] :
194 &TIMER_USED->EVENTS_COMPARE[1]);
199 &RTC_USED->EVENTS_COMPARE[2] :
200 &TIMER_USED->EVENTS_COMPARE[2]);
205 &RTC_USED->EVENTS_COMPARE[3] :
206 &TIMER_USED->EVENTS_COMPARE[3]);
216 uint32_t select_task (uint32_t tsk)
223 &RTC_USED->TASKS_START :
224 &TIMER_USED->TASKS_START);
229 &RTC_USED->TASKS_STOP :
230 &TIMER_USED->TASKS_STOP);
235 (uint32_t) &RTC_USED->TASKS_CLEAR :
236 (uint32_t) &TIMER_USED->TASKS_CLEAR);
250 for(uint32_t cnt = 0; cnt < AUX_CLK_PPI_CHANNELS_USED; cnt++)
252 ppi_setup.
ppi_id = AUX_CLK_PPI_CHANNEL_BASE + cnt;
253 ppi_setup.
event = select_event (g_arr_ppi_cnf[cnt].event);
254 ppi_setup.
task = select_task (g_arr_ppi_cnf[cnt].task1);
255 ppi_setup.
fork = select_task (g_arr_ppi_cnf[cnt].task2);
267 NVIC_DisableIRQ (RTC_IRQN);
268 NVIC_DisableIRQ (TIMER_IRQN);
269 RTC_USED->TASKS_CLEAR = 1;
270 TIMER_USED->TASKS_CLEAR =1;
271 for(uint32_t cnt = 0; cnt < AUX_CLK_MAX_CHANNELS; cnt++)
273 RTC_USED->EVENTS_COMPARE[cnt] = 0;
274 TIMER_USED->EVENTS_COMPARE[cnt] = 0;
283 g_source = aux_clk->
source;
288 RTC_USED->PRESCALER = 0;
290 TIMER_USED->PRESCALER = 4;
291 TIMER_USED->BITMODE = TIMER_BITMODE_BITMODE_32Bit;
292 TIMER_USED->MODE = TIMER_MODE_MODE_Timer;
294 for(uint32_t cnt = 0; cnt < AUX_CLK_MAX_CHANNELS; cnt++)
296 RTC_USED->CC[cnt] = RTC_TICKS_MS(aux_clk->
arr_cc_ms[cnt]);
297 TIMER_USED->CC[cnt] = TIMER_TICKS_MS(aux_clk->
arr_cc_ms[cnt]);
298 if((aux_clk->
events_en & (uint8_t)(1<<cnt)))
300 RTC_USED->EVTENSET |= 1 << (16 + cnt);
301 RTC_USED->INTENSET |= 1 << (16 + cnt);
302 TIMER_USED->INTENSET |= 1 << (16 + cnt);
316 NVIC_DisableIRQ (RTC_IRQN);
317 RTC_USED->TASKS_CLEAR = 1;
318 RTC_USED->TASKS_STOP = 1;
319 NVIC_DisableIRQ (TIMER_IRQN);
320 TIMER_USED->TASKS_CLEAR = 1;
321 TIMER_USED->TASKS_STOP = 1;
322 TIMER_USED->TASKS_SHUTDOWN = 1;
323 (void) TIMER_USED->TASKS_SHUTDOWN;
328 TIMER_USED->TASKS_CLEAR = 1;
329 RTC_USED->TASKS_CLEAR = 1;
334 RTC_USED->EVTENSET |= (events << 16);
335 RTC_USED->INTENSET |= (events << 16);
336 TIMER_USED->INTENSET |= (events << 16);
341 RTC_USED->EVTENCLR |= (events << 16);
342 RTC_USED->INTENCLR |= (events << 16);
343 TIMER_USED->INTENCLR |= (events << 16);
348 memcpy (&g_arr_ppi_cnf[ppi_channel - AUX_CLK_PPI_CHANNEL_BASE],
353 .event = select_event (new_ppi->
event),
354 .task = select_task (new_ppi->
task1),
355 .fork = select_task (new_ppi->
task2),
362 TIMER_USED->CC[cc_id] = TIMER_TICKS_MS(new_val_ms);
363 RTC_USED->CC[cc_id] = RTC_TICKS_MS(new_val_ms);
368 g_irq_priority = new_priority;
377 ms = (RTC_USED->COUNTER * 1000)/32768;
381 uint32_t cc0 = TIMER_USED->CC[0];
382 TIMER_USED->TASKS_CAPTURE[0] = 1;
383 uint32_t counter = TIMER_USED->CC[0];
384 TIMER_USED->CC[0] = cc0;
void aux_clk_en_evt(uint8_t events)
Function to enable one or more events from aux_clk_evt_t.
void aux_clk_set(aux_clk_setup_t *aux_clk)
Function to setup the Auxiliary clock module.
uint32_t aux_clk_get_ms(void)
Function to get ms since Auxiliary clock has started or cleared.
void hfclk_xtal_deinit(void)
Function to de-initialize the HF clock from using the crystal. RC oscillator will be used to generate...
void(* callback_handler)(uint8_t events)
void aux_clk_select_src(aux_clk_source_t source)
Function to select the clock source for auxiliary clock module.
app_irq_priority_t
Priority levels that the application can use based on whether the SoftDevice (SD) is used.
void aux_clk_dis_evt(uint8_t events)
Function to disable one or more events from aux_clk_evt_t.
void aux_clk_stop()
Function to stop clock.
ppi_setup_status_t hal_ppi_set(hal_ppi_setup_t *setup)
Function to setup a PPI.
void aux_clk_update_irq_priority(app_irq_priority_t new_priority)
Function to change IRQ Priority if needed.
void aux_clk_clear()
Function to clear clock counter value.
aux_clk_ppi_t arr_ppi_cnf[AUX_CLK_PPI_CHANNELS_USED]
void hal_ppi_en_ch(uint32_t ppi_id)
Function to Enable given PPI channel.
void aux_clk_update_cc(uint32_t cc_id, uint32_t new_val_ms)
Function to update the given CC value for clock channel.
uint32_t arr_cc_ms[AUX_CLK_MAX_CHANNELS]
void hfclk_xtal_init_blocking(void)
Function to start the crystal oscillator to be used for HF clock. This function blocks until the crys...
void aux_clk_update_ppi(uint32_t ppi_channel, aux_clk_ppi_t *new_ppi)
Function to Update PPI settings for given ppi channel.