Merge pull request #55 from glennrub/align_rtc_and_timer

nrf5/hal/rtc: Aligning RTC (real-time counter) HAL driver with Timer …
This commit is contained in:
Daniel Tralamazza 2017-06-03 22:52:35 +02:00 committed by GitHub
commit baedc380bf
3 changed files with 110 additions and 157 deletions

View File

@ -30,15 +30,20 @@
#ifdef HAL_RTC_MODULE_ENABLED
#define HAL_LFCLK_FREQ (32768UL)
#define HAL_RTC_FREQ (10UL)
#define HAL_RTC_COUNTER_PRESCALER ((HAL_LFCLK_FREQ/HAL_RTC_FREQ)-1)
static hal_rtc_app_callback m_callback;
static uint32_t m_period[sizeof(RTC_BASE_POINTERS) / sizeof(uint32_t)];
void hal_rtc_callback_set(hal_rtc_app_callback callback) {
m_callback = callback;
}
void hal_rtc_init(hal_rtc_conf_t const * p_rtc_conf) {
p_rtc_conf->p_instance->PRESCALER = (32768 / p_rtc_conf->frequency) - 1; // approx correct.
hal_irq_priority(p_rtc_conf->irq_num, p_rtc_conf->irq_priority);
NRF_RTC_Type * p_rtc = RTC_BASE(p_rtc_conf->id);
// start LFCLK if not already started
if (NRF_CLOCK->LFCLKSTAT == 0) {
@ -46,70 +51,70 @@ void hal_rtc_init(hal_rtc_conf_t const * p_rtc_conf) {
while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0);
NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
}
m_period[p_rtc_conf->id] = p_rtc_conf->period;
p_rtc->PRESCALER = HAL_RTC_COUNTER_PRESCALER;
hal_irq_priority(RTC_IRQ_NUM(p_rtc_conf->id), p_rtc_conf->irq_priority);
}
void hal_rtc_start(hal_rtc_conf_t const * p_rtc_conf, uint16_t period) {
uint32_t counter = p_rtc_conf->p_instance->COUNTER;
void hal_rtc_start(uint8_t id) {
NRF_RTC_Type * p_rtc = RTC_BASE(id);
p_rtc_conf->p_instance->CC[0] = counter + period;
uint32_t period = HAL_RTC_FREQ * m_period[id];
uint32_t counter = p_rtc->COUNTER;
p_rtc_conf->p_instance->EVTENSET = RTC_EVTEN_COMPARE0_Msk;
p_rtc_conf->p_instance->INTENSET = RTC_INTENSET_COMPARE0_Msk;
p_rtc->CC[0] = counter + period;
hal_irq_clear(p_rtc_conf->irq_num);
hal_irq_enable(p_rtc_conf->irq_num);
p_rtc->EVTENSET = RTC_EVTEN_COMPARE0_Msk;
p_rtc->INTENSET = RTC_INTENSET_COMPARE0_Msk;
p_rtc_conf->p_instance->TASKS_START = 1;
hal_irq_clear(RTC_IRQ_NUM(id));
hal_irq_enable(RTC_IRQ_NUM(id));
p_rtc->TASKS_START = 1;
}
void hal_rtc_stop(hal_rtc_conf_t const * p_rtc_conf) {
p_rtc_conf->p_instance->EVTENCLR = RTC_EVTEN_COMPARE0_Msk;
p_rtc_conf->p_instance->INTENCLR = RTC_INTENSET_COMPARE0_Msk;
void hal_rtc_stop(uint8_t id) {
NRF_RTC_Type * p_rtc = RTC_BASE(id);
hal_irq_disable(p_rtc_conf->irq_num);
p_rtc->EVTENCLR = RTC_EVTEN_COMPARE0_Msk;
p_rtc->INTENCLR = RTC_INTENSET_COMPARE0_Msk;
p_rtc_conf->p_instance->TASKS_STOP = 1;
hal_irq_disable(RTC_IRQ_NUM(id));
p_rtc->TASKS_STOP = 1;
}
static void common_irq_handler(uint8_t id) {
NRF_RTC_Type * p_rtc = RTC_BASE(id);
// clear all events
p_rtc->EVENTS_COMPARE[0] = 0;
p_rtc->EVENTS_COMPARE[1] = 0;
p_rtc->EVENTS_COMPARE[2] = 0;
p_rtc->EVENTS_COMPARE[3] = 0;
p_rtc->EVENTS_TICK = 0;
p_rtc->EVENTS_OVRFLW = 0;
m_callback(id);
}
void RTC0_IRQHandler(void)
{
// clear all events
NRF_RTC0->EVENTS_COMPARE[0] = 0;
NRF_RTC0->EVENTS_COMPARE[1] = 0;
NRF_RTC0->EVENTS_COMPARE[2] = 0;
NRF_RTC0->EVENTS_COMPARE[3] = 0;
NRF_RTC0->EVENTS_TICK = 0;
NRF_RTC0->EVENTS_OVRFLW = 0;
m_callback(NRF_RTC0);
common_irq_handler(0);
}
void RTC1_IRQHandler(void)
{
// clear all events
NRF_RTC1->EVENTS_COMPARE[0] = 0;
NRF_RTC1->EVENTS_COMPARE[1] = 0;
NRF_RTC1->EVENTS_COMPARE[2] = 0;
NRF_RTC1->EVENTS_COMPARE[3] = 0;
NRF_RTC1->EVENTS_TICK = 0;
NRF_RTC1->EVENTS_OVRFLW = 0;
m_callback(NRF_RTC1);
common_irq_handler(1);
}
#if NRF52
void RTC2_IRQHandler(void)
{
// clear all events
NRF_RTC2->EVENTS_COMPARE[0] = 0;
NRF_RTC2->EVENTS_COMPARE[1] = 0;
NRF_RTC2->EVENTS_COMPARE[2] = 0;
NRF_RTC2->EVENTS_COMPARE[3] = 0;
NRF_RTC2->EVENTS_TICK = 0;
NRF_RTC2->EVENTS_OVRFLW = 0;
m_callback(NRF_RTC2);
common_irq_handler(2);
}
#endif // NRF52

View File

@ -30,52 +30,41 @@
#include "nrf.h"
#if NRF51
#define RTC0 ((NRF_RTC_Type *) NRF_RTC0)
#define RTC0_IRQ_NUM RTC0_IRQn
#define RTC1 ((NRF_RTC_Type *) NRF_RTC1)
#define RTC1_IRQ_NUM RTC1_IRQn
#elif NRF52
#define RTC0 ((NRF_RTC_Type *) NRF_RTC0)
#define RTC0_IRQ_NUM RTC0_IRQn
#define RTC1 ((NRF_RTC_Type *) NRF_RTC1)
#define RTC1_IRQ_NUM RTC1_IRQn
#define RTC2 ((NRF_RTC_Type *) NRF_RTC2)
#define RTC2_IRQ_NUM RTC2_IRQn
#else
#error "Device not supported."
#define RTC_BASE_POINTERS (const uint32_t[]){NRF_RTC0_BASE, \
NRF_RTC1_BASE}
#define RTC_IRQ_VALUES (const uint32_t[]){RTC0_IRQn, \
RTC1_IRQn}
#endif
typedef void (*hal_rtc_app_callback)(NRF_RTC_Type * p_instance);
#if NRF52
#define RTC_BASE_POINTERS (const uint32_t[]){NRF_RTC0_BASE, \
NRF_RTC1_BASE, \
NRF_RTC2_BASE}
#define RTC_IRQ_VALUES (const uint32_t[]){RTC0_IRQn, \
RTC1_IRQn, \
RTC2_IRQn}
#endif
#define RTC_BASE(x) ((NRF_RTC_Type *)RTC_BASE_POINTERS[x])
#define RTC_IRQ_NUM(x) (RTC_IRQ_VALUES[x])
typedef void (*hal_rtc_app_callback)(uint8_t id);
/**
* @brief RTC Configuration Structure definition
*/
typedef struct {
NRF_RTC_Type * p_instance; /* RTC registers base address */
uint32_t irq_num; /* RTC IRQ num */
uint32_t irq_priority; /* RTC IRQ priority */
uint16_t frequency; /* RTC frequency in Hz */
uint8_t id; /* RTC instance id */
uint32_t period; /* RTC period in ms */
uint32_t irq_priority; /* RTC IRQ priority */
} hal_rtc_conf_t;
/**
* @brief RTC handle Structure definition
*/
typedef struct __RTC_HandleTypeDef
{
uint8_t id; /* RTC instance id */
hal_rtc_conf_t config; /* RTC config */
} RTC_HandleTypeDef;
void hal_rtc_callback_set(hal_rtc_app_callback callback);
void hal_rtc_init(hal_rtc_conf_t const * p_rtc_config);
void hal_rtc_start(hal_rtc_conf_t const * p_rtc_conf, uint16_t period);
void hal_rtc_start(uint8_t id);
void hal_rtc_stop(hal_rtc_conf_t const * p_rtc_conf);
void hal_rtc_stop(uint8_t id);
#endif // HAL_RTC_H__

View File

@ -36,73 +36,49 @@
#if MICROPY_PY_MACHINE_RTC
typedef struct _machine_rtc_obj_t {
mp_obj_base_t base;
RTC_HandleTypeDef *rtc;
mp_obj_base_t base;
hal_rtc_conf_t * p_config;
mp_obj_t callback;
mp_int_t period;
mp_int_t mode;
} machine_rtc_obj_t;
RTC_HandleTypeDef RTCHandle0 = {.config.p_instance = NULL, .id = 0};
RTC_HandleTypeDef RTCHandle1 = {.config.p_instance = NULL, .id = 1};
STATIC machine_rtc_obj_t machine_rtc_obj[] = {
{{&machine_rtc_type}, &RTCHandle0},
{{&machine_rtc_type}, &RTCHandle1},
};
STATIC void hal_interrupt_handle(NRF_RTC_Type * p_instance) {
const machine_rtc_obj_t * self = NULL;
if (p_instance == RTC0) {
self = &machine_rtc_obj[0];
mp_call_function_0(self->callback);
} else if (p_instance == RTC1) {
self = &machine_rtc_obj[1];
mp_call_function_0(self->callback);
}
static hal_rtc_conf_t rtc_config0 = {.id = 0};
static hal_rtc_conf_t rtc_config1 = {.id = 1};
#if NRF52
else if (p_instance == RTC2) {
self = &machine_rtc_obj[2];
mp_call_function_0(self->callback);
}
static hal_rtc_conf_t rtc_config2 = {.id = 2};
#endif
STATIC machine_rtc_obj_t machine_rtc_obj[] = {
{{&machine_rtc_type}, &rtc_config0},
{{&machine_rtc_type}, &rtc_config1},
#if NRF52
{{&machine_rtc_type}, &rtc_config2},
#endif
};
STATIC void hal_interrupt_handle(uint8_t id) {
machine_rtc_obj_t * self = &machine_rtc_obj[id];;
mp_call_function_1(self->callback, self);
if (self != NULL) {
hal_rtc_stop(&self->rtc->config);
hal_rtc_stop(id);
if (self->mode == 1) {
hal_rtc_start(&self->rtc->config, self->period);
hal_rtc_start(id);
}
}
}
void rtc_init0(void) {
hal_rtc_callback_set(hal_interrupt_handle);
// reset the RTC handles
memset(&RTCHandle0, 0, sizeof(RTC_HandleTypeDef));
RTCHandle0.config.p_instance = RTC0;
RTCHandle0.config.irq_num = RTC0_IRQ_NUM;
#if (BLUETOOTH_SD == 100)
RTCHandle0.config.irq_priority = 3;
#else
RTCHandle0.config.irq_priority = 6;
#endif
memset(&RTCHandle1, 0, sizeof(RTC_HandleTypeDef));
RTCHandle1.config.p_instance = RTC1;
RTCHandle1.config.irq_num = RTC1_IRQ_NUM;
#if (BLUETOOTH_SD == 100)
RTCHandle1.config.irq_priority = 3;
#else
RTCHandle1.config.irq_priority = 6;
#endif
}
STATIC int rtc_find(mp_obj_t id) {
// given an integer id
int rtc_id = mp_obj_get_int(id);
if (rtc_id >= 0 && rtc_id <= MP_ARRAY_SIZE(machine_rtc_obj)
&& machine_rtc_obj[rtc_id].rtc != NULL) {
&& machine_rtc_obj[rtc_id].p_config != NULL) {
return rtc_id;
}
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
@ -111,59 +87,45 @@ STATIC int rtc_find(mp_obj_t id) {
STATIC void rtc_print(const mp_print_t *print, mp_obj_t o, mp_print_kind_t kind) {
machine_rtc_obj_t *self = o;
mp_printf(print, "RTC(%u)", self->rtc->id);
mp_printf(print, "RTC(%u)", self->p_config->id);
}
/******************************************************************************/
/* MicroPython bindings for machine API */
/*
from machine import RTC
def cb():
print("Callback")
r = RTC(0, 8, cb, mode=RTC.PERIODIC)
r.start(16)
*/
STATIC mp_obj_t machine_rtc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) {
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = mp_const_none} },
{ MP_QSTR_frequency, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = mp_const_none} },
{ MP_QSTR_callback, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = mp_const_none} },
{ MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_id, MP_ARG_OBJ, {.u_obj = MP_OBJ_NEW_SMALL_INT(-1)} },
{ MP_QSTR_period, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1000} },
{ MP_QSTR_mode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} },
{ MP_QSTR_callback, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
};
// parse args
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
if (args[0].u_obj == MP_OBJ_NEW_SMALL_INT(-1)) {
// index -1 does not exist
return mp_const_none;
// TODO: raise exception
}
// get static peripheral object
int rtc_id = rtc_find(args[0].u_obj);
// unconst machine object in order to set a callback.
machine_rtc_obj_t * self = (machine_rtc_obj_t *)&machine_rtc_obj[rtc_id];
mp_obj_t freq_obj = args[1].u_obj;
self->p_config->period = args[1].u_int;
if (freq_obj != mp_const_none && MP_OBJ_IS_INT(freq_obj)) {
self->rtc->config.frequency = mp_obj_get_int(freq_obj);
} else {
// raise exception
self->mode = args[2].u_int;
if (args[3].u_obj != mp_const_none) {
self->callback = args[3].u_obj;
}
if (args[2].u_obj != mp_const_none) {
self->callback = args[2].u_obj;
}
#ifdef NRF51
self->p_config->irq_priority = 3;
#else
self->p_config->irq_priority = 6;
#endif
self->mode = args[3].u_int;
hal_rtc_init(&self->rtc->config);
hal_rtc_init(self->p_config);
return MP_OBJ_FROM_PTR(self);
}
@ -172,17 +134,14 @@ STATIC mp_obj_t machine_rtc_make_new(const mp_obj_type_t *type, size_t n_args, s
/// Start the RTC timer. Timeout occurs after number of periods
/// in the configured frequency has been reached.
///
STATIC mp_obj_t machine_rtc_start(mp_obj_t self_in, mp_obj_t period_in) {
STATIC mp_obj_t machine_rtc_start(mp_obj_t self_in) {
machine_rtc_obj_t * self = MP_OBJ_TO_PTR(self_in);
mp_int_t period = mp_obj_get_int(period_in);
self->period = mp_obj_get_int(period_in);
hal_rtc_start(&self->rtc->config, period);
hal_rtc_start(self->p_config->id);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_rtc_start_obj, machine_rtc_start);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_rtc_start_obj, machine_rtc_start);
/// \method stop()
/// Stop the RTC timer.
@ -190,7 +149,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_rtc_start_obj, machine_rtc_start);
STATIC mp_obj_t machine_rtc_stop(mp_obj_t self_in) {
machine_rtc_obj_t * self = MP_OBJ_TO_PTR(self_in);
hal_rtc_stop(&self->rtc->config);
hal_rtc_stop(self->p_config->id);
return mp_const_none;
}