8000 nrf5/hal: Updating hal RTC implementation. · godlygeek/circuitpython@ff75415 · GitHub
[go: up one dir, main page]

Skip to content

Commit ff75415

Browse files
committed
nrf5/hal: Updating hal RTC implementation.
1 parent 852aaba commit ff75415

File tree

2 files changed

+89
-6
lines changed

2 files changed

+89
-6
lines changed

nrf5/hal/hal_rtc.c

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,83 @@
2626

2727
#include "mphalport.h"
2828
#include "hal_rtc.h"
29+
#include "hal_irq.h"
2930

3031
#ifdef HAL_RTC_MODULE_ENABLED
3132

32-
void hal_rtc_init(NRF_RTC_Type * p_instance, hal_rtc_init_t const * p_rtc_init) {
33+
static hal_rtc_app_callback m_callback;
34+
35+
void hal_rtc_callback_set(hal_rtc_app_callback callback) {
36+
m_callback = callback;
37+
}
38+
39+
void hal_rtc_init(hal_rtc_conf_t const * p_rtc_conf) {
40+
p_rtc_conf->p_instance->PRESCALER = (32768 / p_rtc_conf->frequency) - 1; // approx correct.
41+
hal_irq_priority(p_rtc_conf->irq_num, p_rtc_conf->irq_priority);
42+
}
43+
44+
void hal_rtc_start(hal_rtc_conf_t const * p_rtc_conf, uint16_t period) {
45+
p_rtc_conf->p_instance->CC[0] = period;
46+
p_rtc_conf->p_instance->EVTENSET = RTC_EVTEN_COMPARE0_Msk;
47+
p_rtc_conf->p_instance->INTENSET = RTC_INTENSET_COMPARE0_Msk;
48+
49+
hal_irq_clear(p_rtc_conf->irq_num);
50+
hal_irq_enable(p_rtc_conf->irq_num);
51+
52+
p_rtc_conf->p_instance->TASKS_START = 1;
53+
}
54+
55+
void hal_rtc_stop(hal_rtc_conf_t const * p_rtc_conf) {
56+
p_rtc_conf->p_instance->TASKS_STOP = 1;
57+
58+
p_rtc_conf->p_instance->EVTENCLR = RTC_EVTEN_COMPARE0_Msk;
59+
p_rtc_conf->p_instance->INTENCLR = RTC_INTENSET_COMPARE0_Msk;
60+
61+
hal_irq_disable(p_rtc_conf->irq_num);
3362
}
3463

64+
void RTC0_IRQHandler(void)
65+
{
66+
// clear all events
67+
NRF_RTC0->EVENTS_COMPARE[0] = 0;
68+
NRF_RTC0->EVENTS_COMPARE[1] = 0;
69+
NRF_RTC0->EVENTS_COMPARE[2] = 0;
70+
NRF_RTC0->EVENTS_COMPARE[3] = 0;
71+
NRF_RTC0->EVENTS_TICK = 0;
72+
NRF_RTC0->EVENTS_OVRFLW = 0;
73+
74+
m_callback(NRF_RTC0);
75+
}
76+
77+
void RTC1_IRQHandler(void)
78+
{
79+
// clear all events
80+
NRF_RTC1->EVENTS_COMPARE[0] = 0;
81+
NRF_RTC1->EVENTS_COMPARE[1] = 0;
82+
NRF_RTC1->EVENTS_COMPARE[2] = 0;
83+
NRF_RTC1->EVENTS_COMPARE[3] = 0;
84+
NRF_RTC1->EVENTS_TICK = 0;
85+
NRF_RTC1->EVENTS_OVRFLW = 0;
86+
87+
m_callback(NRF_RTC1);
88+
}
89+
90+
#if NRF52
91+
92+
void RTC2_IRQHandler(void)
93+
{
94+
// clear all events
95+
NRF_RTC2->EVENTS_COMPARE[0] = 0;
96+
NRF_RTC2->EVENTS_COMPARE[1] = 0;
97+
NRF_RTC2->EVENTS_COMPARE[2] = 0;
98+
NRF_RTC2->EVENTS_COMPARE[3] = 0;
99+
NRF_RTC2->EVENTS_TICK = 0;
100+
NRF_RTC2->EVENTS_OVRFLW = 0;
101+
102+
m_callback(NRF_RTC2);
103+
}
104+
105+
#endif // NRF52
106+
35107
#endif // HAL_RTC_MODULE_ENABLED
36108

nrf5/hal/hal_rtc.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,33 @@
4949
#error "Device not supported."
5050
#endif
5151

52+
typedef void (*hal_rtc_app_callback)(NRF_RTC_Type * p_instance);
53+
5254
/**
5355
* @brief RTC Configuration Structure definition
5456
*/
5557
typedef struct {
56-
} hal_rtc_init_t;
58+
NRF_RTC_Type * p_instance; /* RTC registers base address */
59+
uint32_t irq_num; /* RTC IRQ num */
60+
uint32_t irq_priority; /* RTC IRQ priority */
61+
uint16_t frequency; /* RTC frequency in Hz */
62+
} hal_rtc_conf_t;
5763

5864
/**
5965
* @brief RTC handle Structure definition
6066
*/
6167
typedef struct __RTC_HandleTypeDef
6268
{
63-
NRF_RTC_Type *instance; /* RTC registers base address */
64-
hal_rtc_init_t init; /* RTC initialization parameters */
65-
uint8_t id; /* RTC instance id */
69+
uint8_t id; /* RTC instance id */
70+
hal_rtc_conf_t config; /* RTC config */
6671
} RTC_HandleTypeDef;
6772

68-
void hal_rtc_init(NRF_RTC_Type * p_instance, hal_rtc_init_t const * p_rtc_init);
73+
void hal_rtc_callback_set(hal_rtc_app_callback callback);
74+
75+
void hal_rtc_init(hal_rtc_conf_t const * p_rtc_config);
76+
77+
void hal_rtc_start(hal_rtc_conf_t const * p_rtc_conf, uint16_t period);
78+
79+
void hal_rtc_stop(hal_rtc_conf_t const * p_rtc_conf);
6980

7081
#endif // HAL_RTC_H__

0 commit comments

Comments
 (0)
0