52 lines
1.1 KiB
C++
52 lines
1.1 KiB
C++
#include "timer_usec.h"
|
|
|
|
Timer_usec::Timer_usec() //= default;
|
|
: callback(nullptr),
|
|
callback_context(nullptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
void Timer_usec::set_callback(
|
|
callback_t callback_function,
|
|
void *user_data)
|
|
{
|
|
callback = callback_function;
|
|
callback_context = user_data;
|
|
}
|
|
|
|
|
|
// void Timer_usec::init(uint16_t period_us) { // for custom duty cycle duration in ms in part 1
|
|
// TCCR1A = 0;
|
|
// TCCR1B = 0;
|
|
// TCNT1 = 0;
|
|
|
|
// OCR1A = period_us * 2 - 1;
|
|
// TCCR1B |= (1<<WGM12);
|
|
// TIMSK1 |= (1<<OCIE1A);
|
|
// TCCR1B |= (1<<CS11);
|
|
// }
|
|
|
|
void Timer_usec::init(uint16_t period_us)
|
|
{
|
|
if (period_us == 0) {
|
|
return;
|
|
}
|
|
|
|
k_timer_init(&timer, expiry_handler, nullptr);
|
|
k_timer_user_data_set(&timer, this);
|
|
|
|
// The timer fires first after period_us, then repeats every period_us.
|
|
k_timer_start(&timer, K_USEC(period_us), K_USEC(period_us));
|
|
}
|
|
|
|
void Timer_usec::expiry_handler(struct k_timer *expired_timer)
|
|
{
|
|
auto *self =
|
|
static_cast<Timer_usec *>(k_timer_user_data_get(expired_timer));
|
|
|
|
if (self != nullptr && self->callback != nullptr) {
|
|
self->callback(self->callback_context);
|
|
}
|
|
} |