Intel® Quark™ Microcontroller Software Interface  1.4.0
Intel® Quark™ Microcontroller BSP
qm_ss_timer.c
1 /*
2  * {% copyright %}
3  */
4 
5 #include "qm_ss_timer.h"
6 
7 static void (*callback[QM_SS_TIMER_NUM])(void *data);
8 static void *callback_data[QM_SS_TIMER_NUM];
9 static uint32_t qm_ss_timer_base[QM_SS_TIMER_NUM] = {QM_SS_TIMER_0_BASE};
10 
11 static __inline__ void qm_ss_timer_isr(qm_ss_timer_t timer)
12 {
13  uint32_t ctrl = 0;
14 
15  if (callback[timer]) {
16  callback[timer](callback_data[timer]);
17  }
18 
19  ctrl = __builtin_arc_lr(qm_ss_timer_base[timer] + QM_SS_TIMER_CONTROL);
20  ctrl &= ~BIT(QM_SS_TIMER_CONTROL_INT_PENDING_OFFSET);
21  __builtin_arc_sr(ctrl, qm_ss_timer_base[timer] + QM_SS_TIMER_CONTROL);
22 }
23 
24 QM_ISR_DECLARE(qm_ss_timer_0_isr)
25 {
26  qm_ss_timer_isr(QM_SS_TIMER_0);
27 }
28 
30  const qm_ss_timer_config_t *const cfg)
31 {
32  uint32_t ctrl = 0;
33  QM_CHECK(cfg != NULL, -EINVAL);
34  QM_CHECK(timer < QM_SS_TIMER_NUM, -EINVAL);
35 
36  ctrl = cfg->watchdog_mode << QM_SS_TIMER_CONTROL_WATCHDOG_OFFSET;
37  ctrl |= cfg->inc_run_only << QM_SS_TIMER_CONTROL_NON_HALTED_OFFSET;
38  ctrl |= cfg->int_en << QM_SS_TIMER_CONTROL_INT_EN_OFFSET;
39 
40  __builtin_arc_sr(ctrl, qm_ss_timer_base[timer] + QM_SS_TIMER_CONTROL);
41  __builtin_arc_sr(cfg->count,
42  qm_ss_timer_base[timer] + QM_SS_TIMER_LIMIT);
43 
44  callback[timer] = cfg->callback;
45  callback_data[timer] = cfg->callback_data;
46 
47  return 0;
48 }
49 
50 int qm_ss_timer_set(const qm_ss_timer_t timer, const uint32_t count)
51 {
52  QM_CHECK(timer < QM_SS_TIMER_NUM, -EINVAL);
53 
54  __builtin_arc_sr(count, qm_ss_timer_base[timer] + QM_SS_TIMER_COUNT);
55 
56  return 0;
57 }
58 
59 int qm_ss_timer_get(const qm_ss_timer_t timer, uint32_t *const count)
60 {
61  QM_CHECK(timer < QM_SS_TIMER_NUM, -EINVAL);
62  QM_CHECK(count != NULL, -EINVAL);
63 
64  *count = __builtin_arc_lr(qm_ss_timer_base[timer] + QM_SS_TIMER_COUNT);
65 
66  return 0;
67 }
68 
69 #if (ENABLE_RESTORE_CONTEXT)
70 int qm_ss_timer_save_context(const qm_ss_timer_t timer,
71  qm_ss_timer_context_t *const ctx)
72 {
73  uint32_t controller;
74 
75  QM_CHECK(timer < QM_SS_TIMER_NUM, -EINVAL);
76  QM_CHECK(ctx != NULL, -EINVAL);
77 
78  controller = qm_ss_timer_base[timer];
79 
80  ctx->timer_control = __builtin_arc_lr(controller + QM_SS_TIMER_CONTROL);
81  ctx->timer_limit = __builtin_arc_lr(controller + QM_SS_TIMER_LIMIT);
82  ctx->timer_count = __builtin_arc_lr(controller + QM_SS_TIMER_COUNT);
83 
84  return 0;
85 }
86 
87 int qm_ss_timer_restore_context(const qm_ss_timer_t timer,
88  const qm_ss_timer_context_t *const ctx)
89 {
90  uint32_t controller;
91 
92  QM_CHECK(timer < QM_SS_TIMER_NUM, -EINVAL);
93  QM_CHECK(ctx != NULL, -EINVAL);
94 
95  controller = qm_ss_timer_base[timer];
96 
97  __builtin_arc_sr(ctx->timer_control, controller + QM_SS_TIMER_CONTROL);
98  __builtin_arc_sr(ctx->timer_limit, controller + QM_SS_TIMER_LIMIT);
99  __builtin_arc_sr(ctx->timer_count, controller + QM_SS_TIMER_COUNT);
100 
101  return 0;
102 }
103 #else
104 int qm_ss_timer_save_context(const qm_ss_timer_t timer,
105  qm_ss_timer_context_t *const ctx)
106 {
107  (void)timer;
108  (void)ctx;
109 
110  return 0;
111 }
112 
113 int qm_ss_timer_restore_context(const qm_ss_timer_t timer,
114  const qm_ss_timer_context_t *const ctx)
115 {
116  (void)timer;
117  (void)ctx;
118 
119  return 0;
120 }
121 #endif /* ENABLE_RESTORE_CONTEXT */
bool inc_run_only
Increments in run state only.
Definition: qm_ss_timer.h:32
QM_ISR_DECLARE(qm_ss_timer_0_isr)
ISR for SS Timer 0 interrupt.
Definition: qm_ss_timer.c:24
void(* callback)(void *data)
User callback.
Definition: qm_ss_timer.h:43
int qm_ss_timer_get(const qm_ss_timer_t timer, uint32_t *const count)
Get SS timer count value.
Definition: qm_ss_timer.c:59
qm_ss_timer_t
Sensor Subsystem Timers.
uint32_t count
Final count value.
Definition: qm_ss_timer.h:34
bool watchdog_mode
Watchdog mode.
Definition: qm_ss_timer.h:22
Sensor Subsystem Timer Configuration Type.
Definition: qm_ss_timer.h:21
int qm_ss_timer_set_config(const qm_ss_timer_t timer, const qm_ss_timer_config_t *const cfg)
Set the SS timer configuration.
Definition: qm_ss_timer.c:29
int qm_ss_timer_set(const qm_ss_timer_t timer, const uint32_t count)
Set SS timer count value.
Definition: qm_ss_timer.c:50
bool int_en
Interrupt enable.
Definition: qm_ss_timer.h:33
void * callback_data
Callback user data.
Definition: qm_ss_timer.h:44