YAHAL
Yet Another Hardware Abstraction Library
Loading...
Searching...
No Matches
timer_msp432.cpp
1/*
2 * timer_msp432.cpp
3 *
4 * Created on: 09.07.2016
5 * Author: aterstegge
6 */
7
8#include "timer_msp432.h"
9
10extern uint32_t SystemCoreClock;
11
12function<void()> timer_msp432::_intHandler1 = 0;
13function<void()> timer_msp432::_intHandler2 = 0;
14
15
16// We use the TIMER32 instances here (they might not
17// be used for other purposes anyway...)
18
19timer_msp432::timer_msp432(Timer32_Type *timer)
20 : _timer(timer) {
21 // initialize the ctrl register
22 _timer->CONTROL = TIMER32_CONTROL_MODE | // periodic mode
23 TIMER32_CONTROL_IE | // enable irq
24 TIMER32_CONTROL_SIZE; // use 32 bit counter
25 // calculate factor (counts for 1us)
26 _factor = ((float) SystemCoreClock / 1000000.0) + 0.5;
27 _period_us = 0;
28 _period_ns = 0;
29 _period_load = 0;
30 // enable IRQ in NVIC
31 NVIC_EnableIRQ((timer == TIMER32_1) ? T32_INT1_IRQn : T32_INT2_IRQn);
32}
33
34timer_msp432::~timer_msp432() {
35 _timer->CONTROL = 0; // reset ctrl to default value
36}
37
38void timer_msp432::setPeriod(uint32_t us, TIMER::timer_mode mode) {
39 _period_us = us;
40 // set load register
41 _timer->LOAD = _period_load = us * _factor;
42 // set oneshot
43 if (mode == TIMER::ONE_SHOT) {
45 } else {
46 _timer->CONTROL &= ~TIMER32_CONTROL_ONESHOT;
47 }
48}
49
50uint32_t timer_msp432::getPeriod() {
51 return _period_us;
52}
53
54void timer_msp432::setCallback(function<void()> f) {
55 if (_timer == TIMER32_1) {
56 timer_msp432::_intHandler1 = f;
57 } else {
58 timer_msp432::_intHandler2 = f;
59 }
60}
61
62void timer_msp432::start() {
63 _timer->CONTROL |= TIMER32_CONTROL_ENABLE;
64}
65
66void timer_msp432::stop() {
67 _timer->CONTROL &= ~TIMER32_CONTROL_ENABLE;
68}
69
70bool timer_msp432::isRunning() {
71 return _timer->CONTROL & TIMER32_CONTROL_ENABLE;
72}
73
74void timer_msp432::reset() {
75 _timer->LOAD = _period_load;
76}
77
78void timer_msp432::setNanoPeriod(uint32_t ns, TIMER::timer_mode mode) {
79 _period_ns = ns;
80 // set load register
81 _timer->LOAD = _period_load = (ns * _factor) / 1000;
82 // set onshot
83 if (mode == TIMER::ONE_SHOT) {
85 } else {
86 _timer->CONTROL &= ~TIMER32_CONTROL_ONESHOT;
87 }
88}
89
90uint32_t timer_msp432::getNanoPeriod() {
91 return _period_ns;
92}
93
94// Interrupt handler
96extern "C" {
97
98void T32_INT1_IRQHandler(void) {
99 // clear the interrupt
100 TIMER32_1->INTCLR = 1;
101 // disable timer if one-shot was selected
102 if (TIMER32_1->CONTROL & TIMER32_CONTROL_ONESHOT)
103 TIMER32_1->CONTROL &= ~TIMER32_CONTROL_ENABLE;
104 // call the user handler
105 if (timer_msp432::_intHandler1) {
106 timer_msp432::_intHandler1();
107 }
108}
109void T32_INT2_IRQHandler(void) {
110 // clear the interrupt
111 TIMER32_2->INTCLR = 1;
112 // disable timer if one-shot was selected
113 if (TIMER32_2->CONTROL & TIMER32_CONTROL_ONESHOT)
114 TIMER32_2->CONTROL &= ~TIMER32_CONTROL_ENABLE;
115 // call the user handler
116 if (timer_msp432::_intHandler2) {
117 timer_msp432::_intHandler2();
118 }
119}
120
121} // extern "C"
__IO uint32_t CONTROL
#define TIMER32_CONTROL_SIZE
#define TIMER32_CONTROL_ONESHOT
#define TIMER32_CONTROL_IE
__IO uint32_t LOAD
#define TIMER32_CONTROL_MODE