Skip to content

Commit 5f689b1

Browse files
drivers: counter: stm32: introduce counter set value api
Introduce a counter set value api to set the ticks for 32b and 64b. Signed-off-by: Ryan McClelland <ryanmcclelland@meta.com>
1 parent bb30acf commit 5f689b1

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

drivers/counter/counter_ll_stm32_timer.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,16 @@ static int counter_stm32_reset_timer(const struct device *dev)
550550
return 0;
551551
}
552552

553+
static int counter_stm32_set_value(const struct device *dev, uint32_t ticks)
554+
{
555+
const struct counter_stm32_config *config = dev->config;
556+
TIM_TypeDef *timer = config->timer;
557+
558+
LL_TIM_SetCounter(timer, ticks);
559+
560+
return 0;
561+
}
562+
553563
static void counter_stm32_top_irq_handle(const struct device *dev)
554564
{
555565
struct counter_stm32_data *data = dev->data;
@@ -596,6 +606,7 @@ static DEVICE_API(counter, counter_stm32_driver_api) = {
596606
.set_guard_period = counter_stm32_set_guard_period,
597607
.get_freq = counter_stm32_get_freq,
598608
.reset = counter_stm32_reset_timer,
609+
.set_value = counter_stm32_set_value,
599610
};
600611

601612
#define TIM_IRQ_HANDLE_CC(timx, cc) \

include/zephyr/drivers/counter.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,10 @@ typedef int (*counter_api_get_value)(const struct device *dev,
216216
typedef int (*counter_api_get_value_64)(const struct device *dev,
217217
uint64_t *ticks);
218218
typedef int (*counter_api_reset)(const struct device *dev);
219+
typedef int (*counter_api_set_value)(const struct device *dev,
220+
uint32_t ticks);
221+
typedef int (*counter_api_set_value_64)(const struct device *dev,
222+
uint64_t ticks);
219223
typedef int (*counter_api_set_alarm)(const struct device *dev,
220224
uint8_t chan_id,
221225
const struct counter_alarm_cfg *alarm_cfg);
@@ -238,6 +242,8 @@ __subsystem struct counter_driver_api {
238242
counter_api_get_value get_value;
239243
counter_api_get_value_64 get_value_64;
240244
counter_api_reset reset;
245+
counter_api_set_value set_value;
246+
counter_api_set_value_64 set_value_64;
241247
counter_api_set_alarm set_alarm;
242248
counter_api_cancel_alarm cancel_alarm;
243249
counter_api_set_top_value set_top_value;
@@ -454,6 +460,53 @@ static inline int z_impl_counter_reset(const struct device *dev)
454460
return api->reset(dev);
455461
}
456462

463+
/**
464+
* @brief Set current counter value.
465+
* @param dev Pointer to the device structure for the driver instance.
466+
* @param ticks Tick value to set
467+
*
468+
* @retval 0 If successful.
469+
* @retval Negative error code on failure getting the counter value
470+
*/
471+
__syscall int counter_set_value(const struct device *dev, uint32_t ticks);
472+
473+
static inline int z_impl_counter_set_value(const struct device *dev,
474+
uint32_t ticks)
475+
{
476+
const struct counter_driver_api *api =
477+
(struct counter_driver_api *)dev->api;
478+
479+
if (!api->set_value) {
480+
return -ENOSYS;
481+
}
482+
483+
return api->set_value(dev, ticks);
484+
}
485+
486+
/**
487+
* @brief Set current counter 64-bit value.
488+
* @param dev Pointer to the device structure for the driver instance.
489+
* @param ticks Tick value to set
490+
*
491+
* @retval 0 If successful.
492+
* @retval Negative error code on failure getting the counter value
493+
*/
494+
__syscall int counter_set_value_64(const struct device *dev, uint64_t ticks);
495+
496+
static inline int z_impl_counter_set_value_64(const struct device *dev,
497+
uint64_t ticks)
498+
{
499+
const struct counter_driver_api *api =
500+
(struct counter_driver_api *)dev->api;
501+
502+
if (!api->set_value_64) {
503+
return -ENOSYS;
504+
}
505+
506+
return api->set_value_64(dev, ticks);
507+
}
508+
509+
457510
/**
458511
* @brief Set a single shot alarm on a channel.
459512
*

0 commit comments

Comments
 (0)