nrf5/hal/timer: Implementing hal_timer to 1us prescaler. Multiplier inside to get to millisecond resolution. Callback must be registered before starting a timer.

This commit is contained in:
Glenn Ruben Bakke 2017-05-31 22:25:23 +02:00 committed by glennrub
parent ac7fae2e0a
commit f49f20a24e
2 changed files with 46 additions and 6 deletions

View File

@ -26,41 +26,76 @@
#include "mphalport.h"
#include "hal_timer.h"
#include "hal_irq.h"
#ifdef HAL_TIMER_MODULE_ENABLED
void hal_timer_init(hal_timer_conf_t const * p_timer_conf) {
static hal_timer_app_callback m_callback;
void hal_timer_callback_set(hal_timer_app_callback callback) {
m_callback = callback;
}
void hal_timer_init(hal_timer_conf_t const * p_timer_conf) {
NRF_TIMER_Type * p_timer = TIMER_BASE(p_timer_conf->id);
p_timer->CC[0] = 1000 * p_timer_conf->period;
p_timer->MODE = TIMER_MODE_MODE_Timer;
p_timer->BITMODE = TIMER_BITMODE_BITMODE_24Bit << TIMER_BITMODE_BITMODE_Pos;
p_timer->PRESCALER = 4; // 1 us
p_timer->INTENSET = TIMER_INTENSET_COMPARE0_Msk;
p_timer->SHORTS = (TIMER_SHORTS_COMPARE0_CLEAR_Enabled << TIMER_SHORTS_COMPARE0_CLEAR_Pos);
p_timer->TASKS_CLEAR = 1;
hal_irq_priority(TIMER_IRQ_NUM(p_timer_conf->id), 3);
}
void hal_timer_start(uint8_t id) {
NRF_TIMER_Type * p_timer = TIMER_BASE(id);
p_timer->TASKS_CLEAR = 1;
hal_irq_enable(TIMER_IRQ_NUM(id));
p_timer->TASKS_START = 1;
}
void hal_timer_stop(uint8_t id) {
NRF_TIMER_Type * p_timer = TIMER_BASE(id);
hal_irq_disable(TIMER_IRQ_NUM(id));
p_timer->TASKS_STOP = 1;
}
static void common_irq_handler(uint8_t id) {
NRF_TIMER_Type * p_timer = TIMER_BASE(id);
if (p_timer->EVENTS_COMPARE[0]) {
p_timer->EVENTS_COMPARE[0] = 0;
m_callback(id);
}
}
void TIMER0_IRQHandler(void) {
common_irq_handler(0);
}
#if (MICROPY_PY_MACHINE_SOFT_PWM != 1)
void TIMER1_IRQHandler(void) {
common_irq_handler(1);
}
#endif
void TIMER2_IRQHandler(void) {
common_irq_handler(2);
}
#if NRF52
void TIMER3_IRQHandler(void) {
common_irq_handler(3);
}
void TIMER4_IRQHandler(void) {
common_irq_handler(4);
}
#endif

View File

@ -54,14 +54,19 @@
#define TIMER_BASE(x) ((NRF_TIMER_Type *)TIMER_BASE_POINTERS[x])
#define TIMER_IRQ_NUM(x) (TIMER_IRQ_VALUES[x])
typedef void (*hal_timer_app_callback)(uint8_t id);
/**
* @brief Timer Configuration Structure definition
*/
typedef struct {
uint8_t id;
uint32_t period;
uint8_t irq_priority;
} hal_timer_conf_t;
void hal_timer_callback_set(hal_timer_app_callback callback);
void hal_timer_init(hal_timer_conf_t const * p_timer_config);
void hal_timer_start(uint8_t id);