Skip to content

Commit 64af1b2

Browse files
committed
Added a function to check if a oneshot timer is done (max32660 == WIP)
1 parent 03a8f7a commit 64af1b2

File tree

3 files changed

+79
-3
lines changed

3 files changed

+79
-3
lines changed

targets/chip/max32660/io/timer.hpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,28 @@ namespace klib::max32660::io {
249249
* @tparam Timer
250250
*/
251251
template <typename Timer>
252-
using oneshot_timer = detail::timer::base_timer<Timer, detail::timer::mode::one_shot>;
252+
class oneshot_timer: public detail::timer::base_timer<Timer, detail::timer::mode::one_shot> {
253+
public:
254+
/**
255+
* @brief Flag if the oneshot timer is done
256+
*
257+
* @return true
258+
* @return false
259+
*/
260+
static bool done() {
261+
// get the current counter
262+
const uint32_t c = detail::timer::base_timer<
263+
Timer, detail::timer::mode::one_shot
264+
>::get_counter();
265+
266+
// get the maximum value of the counter (this is calculated in
267+
// init/set_frequency)
268+
const uint32_t m = Timer::port->CMP;
269+
270+
// return if the register matches the RC register (match register 2)
271+
return c >= m;
272+
}
273+
};
253274

254275
/**
255276
* @brief Pin that uses a timer to toggle the output.

targets/core/atmel/atsam4s/timer.hpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@ namespace klib::core::atsam4s::io::detail::timer {
7979
}
8080
}
8181

82+
/**
83+
* @brief Set the A/B/C register based on the channel
84+
*
85+
*/
86+
static uint32_t get_register() {
87+
// get a pointer to the first register
88+
volatile uint32_t* reg = &(Timer::port->CH[Channel].RA);
89+
90+
// use the channel as a offset to the correct channel register
91+
return reg[Match];
92+
}
93+
8294
/**
8395
* @brief Set the A/B/C register based on the channel
8496
*
@@ -232,7 +244,7 @@ namespace klib::core::atsam4s::io {
232244
* @tparam Timer
233245
*/
234246
template <typename Timer, uint32_t Channel, uint32_t Match = 2, uint32_t Div = 2>
235-
class oneshot_timer: public detail::timer::base_timer<Timer, Channel, 2, detail::timer::mode::one_shot, Div> {
247+
class oneshot_timer: public detail::timer::base_timer<Timer, Channel, Match, detail::timer::mode::one_shot, Div> {
236248
protected:
237249
// make sure the correct match register is used. For one shot we need to
238250
// turn off the channel. This can only be done when match register 2 is
@@ -255,6 +267,28 @@ namespace klib::core::atsam4s::io {
255267
Timer, Channel, Match, detail::timer::mode::one_shot, Div
256268
>::init_impl(irq, frequency);
257269
}
270+
271+
/**
272+
* @brief Flag if the oneshot timer is done
273+
*
274+
* @return true
275+
* @return false
276+
*/
277+
static bool done() {
278+
// get the current counter
279+
const uint32_t c = detail::timer::base_timer<
280+
Timer, Channel, Match, detail::timer::mode::one_shot, Div
281+
>::get_counter();
282+
283+
// get the maximum value of the counter (this is calculated in
284+
// init/set_frequency)
285+
const uint32_t m = detail::timer::base_timer<
286+
Timer, Channel, Match, detail::timer::mode::one_shot, Div
287+
>::get_register();
288+
289+
// return if the register matches the RC register (match register 2)
290+
return c >= m;
291+
}
258292
};
259293

260294
/**

targets/core/nxp/lpc175x/timer.hpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,28 @@ namespace klib::core::lpc175x::io {
171171
* @tparam Timer
172172
*/
173173
template <typename Timer, uint32_t Channel>
174-
using oneshot_timer = detail::timer::base_timer<Timer, Channel, detail::timer::mode::one_shot>;
174+
class oneshot_timer: public detail::timer::base_timer<Timer, Channel, detail::timer::mode::one_shot> {
175+
public:
176+
/**
177+
* @brief Flag if the oneshot timer is done
178+
*
179+
* @return true
180+
* @return false
181+
*/
182+
static bool done() {
183+
// get the current counter
184+
const uint32_t c = detail::timer::base_timer<
185+
Timer, Channel, detail::timer::mode::one_shot
186+
>::get_counter();
187+
188+
// get the maximum value of the counter (this is calculated in
189+
// init/set_frequency)
190+
const uint32_t m = Timer::port->MR[Channel];
191+
192+
// return if the register matches the RC register (match register 2)
193+
return c >= m;
194+
}
195+
};
175196

176197
/**
177198
* @brief Pwm timer.

0 commit comments

Comments
 (0)