1 /*
2 * Copyright (c) 2023 STMicroelectronics
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/kernel.h>
8 #include <zephyr/sys_clock.h>
9
10 #include "stm32_timer.h"
11
12 #define TICKS_PER_MS (CONFIG_SYS_CLOCK_TICKS_PER_SEC / MSEC_PER_SEC)
13
14 static struct k_timer timer;
15
UTIL_TIMER_Create(UTIL_TIMER_Object_t * timer_object,uint32_t period,UTIL_TIMER_Mode_t mode,void (* callback)(void *),void * argument)16 UTIL_TIMER_Status_t UTIL_TIMER_Create(UTIL_TIMER_Object_t *timer_object,
17 uint32_t period, UTIL_TIMER_Mode_t mode,
18 void (*callback)(void *), void *argument)
19 {
20 if (timer_object == NULL) {
21 return UTIL_TIMER_INVALID_PARAM;
22 }
23
24 timer_object->ReloadValue = period * TICKS_PER_MS;
25 timer_object->Mode = mode;
26 timer_object->Callback = callback;
27 timer_object->argument = argument;
28 timer_object->IsRunning = false;
29 timer_object->IsReloadStopped = false;
30 timer_object->Next = NULL;
31
32 return UTIL_TIMER_OK;
33 }
34
UTIL_TIMER_Start(UTIL_TIMER_Object_t * timer_object)35 UTIL_TIMER_Status_t UTIL_TIMER_Start(UTIL_TIMER_Object_t *timer_object)
36 {
37 if (timer_object == NULL) {
38 return UTIL_TIMER_INVALID_PARAM;
39 }
40
41 k_timer_user_data_set(&timer, timer_object);
42 k_timer_start(&timer, K_TICKS(timer_object->ReloadValue),
43 K_TICKS(timer_object->ReloadValue));
44 timer_object->IsRunning = true;
45
46 return UTIL_TIMER_OK;
47 }
48
UTIL_TIMER_Stop(UTIL_TIMER_Object_t * timer_object)49 UTIL_TIMER_Status_t UTIL_TIMER_Stop(UTIL_TIMER_Object_t *timer_object)
50 {
51 if (timer_object == NULL) {
52 return UTIL_TIMER_INVALID_PARAM;
53 }
54
55 k_timer_stop(&timer);
56 timer_object->IsRunning = false;
57
58 return UTIL_TIMER_OK;
59 }
60