ChibiOS/HAL 9.0.0
hal_st.c
Go to the documentation of this file.
1/*
2 ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17/**
18 * @file hal_st.c
19 * @brief ST Driver code.
20 *
21 * @addtogroup ST
22 * @{
23 */
24
25#include "hal.h"
26
27#if (OSAL_ST_MODE != OSAL_ST_MODE_NONE) || defined(__DOXYGEN__)
28
29/*===========================================================================*/
30/* Driver local definitions. */
31/*===========================================================================*/
32
33/*===========================================================================*/
34/* Driver exported variables. */
35/*===========================================================================*/
36
37/*===========================================================================*/
38/* Driver local types. */
39/*===========================================================================*/
40
41/*===========================================================================*/
42/* Driver local variables. */
43/*===========================================================================*/
44
45/**
46 * @brief Callback pointers for each alarm.
47 * @note If some alarms have static callbacks defined in the LLD then
48 * some of the pointers might be unused (never called through).
49 */
50#if (ST_LLD_NUM_ALARMS > 1) || defined(__DOXYGEN__)
52#endif
53
54/*===========================================================================*/
55/* Driver local functions. */
56/*===========================================================================*/
57
58/*===========================================================================*/
59/* Driver exported functions. */
60/*===========================================================================*/
61
62/**
63 * @brief ST Driver initialization.
64 * @note This function is implicitly invoked by @p halInit(), there is
65 * no need to explicitly initialize the driver.
66 *
67 * @init
68 */
69void stInit(void) {
70#if ST_LLD_NUM_ALARMS > 1
71 unsigned i;
72
73 for (i = 0U; i < (unsigned)ST_LLD_NUM_ALARMS; i++) {
74 st_callbacks[i] = NULL;
75 }
76#endif
78}
79
80#if defined(ST_LLD_MULTICORE_SUPPORT) || defined(__DOXYGEN__)
81/**
82 * @brief Enables an alarm interrupt on the invoking core.
83 * @note Must be called before any other alarm-related function.
84 *
85 * @api
86 */
87void stBind(void) {
88
89 st_lld_bind();
90}
91#endif /* defined(ST_LLD_MULTICORE_SUPPORT) */
92
93#if (OSAL_ST_MODE == OSAL_ST_MODE_FREERUNNING) || defined(__DOXYGEN__)
94/**
95 * @brief Returns the time counter value.
96 * @note This functionality is only available in free running mode, the
97 * behaviour in periodic mode is undefined.
98 *
99 * @return The counter value.
100 *
101 * @api
102 */
104
105 return st_lld_get_counter();
106}
107
108/**
109 * @brief Starts the alarm zero.
110 * @note Makes sure that no spurious alarms are triggered after
111 * this call.
112 * @note This functionality is only available in free running mode, the
113 * behavior in periodic mode is undefined.
114 *
115 * @param[in] abstime the time to be set for the first alarm
116 *
117 * @api
118 */
119void stStartAlarm(systime_t abstime) {
120
121 osalDbgAssert(stIsAlarmActive() == false, "already active");
122
123 st_lld_start_alarm(abstime);
124}
125
126/**
127 * @brief Stops the alarm zero interrupt.
128 * @note This functionality is only available in free running mode, the
129 * behavior in periodic mode is undefined.
130 *
131 * @api
132 */
133void stStopAlarm(void) {
134
136}
137
138/**
139 * @brief Sets the alarm zero time.
140 * @note This functionality is only available in free running mode, the
141 * behavior in periodic mode is undefined.
142 *
143 * @param[in] abstime the time to be set for the next alarm
144 *
145 * @api
146 */
147void stSetAlarm(systime_t abstime) {
148
149 osalDbgAssert(stIsAlarmActive() != false, "not active");
150
151 st_lld_set_alarm(abstime);
152}
153
154/**
155 * @brief Returns the alarm zero current time.
156 * @note This functionality is only available in free running mode, the
157 * behavior in periodic mode is undefined.
158 *
159 * @return The currently set alarm time.
160 *
161 * @api
162 */
164
165 osalDbgAssert(stIsAlarmActive() != false, "not active");
166
167 return st_lld_get_alarm();
168}
169
170/**
171 * @brief Determines if the alarm zero is active.
172 *
173 * @return The alarm status.
174 * @retval false if the alarm is not active.
175 * @retval true is the alarm is active
176 *
177 * @api
178 */
179bool stIsAlarmActive(void) {
180
181 return st_lld_is_alarm_active();
182}
183
184#if (ST_LLD_NUM_ALARMS > 1) || defined(__DOXYGEN__)
185/**
186 * @brief Associates a callback to an alarm.
187 * @note Makes sure that no spurious alarms are triggered after
188 * this call.
189 * @note This functionality is only available in free running mode, the
190 * behavior in periodic mode is undefined.
191 *
192 * @param[in] alarm alarm channel number (0..ST_LLD_NUM_ALARMS-1)
193 * @param[in] cb alarm callback or @p NULL
194 *
195 * @api
196 */
197void stSetCallback(unsigned alarm, st_callback_t cb) {
198
199 osalDbgCheck(alarm < (unsigned)ST_LLD_NUM_ALARMS);
200
201 st_callbacks[alarm] = cb;
202}
203
204#if defined(ST_LLD_MULTICORE_SUPPORT) || defined(__DOXYGEN__)
205/**
206 * @brief Enables an alarm interrupt on the invoking core.
207 * @note Must be called before any other alarm-related function.
208 *
209 * @param[in] alarm alarm channel number (0..ST_LLD_NUM_ALARMS-1)
210 *
211 * @api
212 */
213void stBindAlarmN(unsigned alarm) {
214
215 osalDbgCheck(alarm < (unsigned)ST_LLD_NUM_ALARMS);
216 osalDbgAssert(stIsAlarmActive() == false, "already active");
217
218 st_lld_bind_alarm_n(alarm);
219}
220#endif /* defined(ST_LLD_MULTICORE_SUPPORT) */
221
222/**
223 * @brief Starts an additional alarm.
224 * @note Makes sure that no spurious alarms are triggered after
225 * this call.
226 * @note This functionality is only available in free running mode, the
227 * behavior in periodic mode is undefined.
228 *
229 * @param[in] alarm alarm channel number (0..ST_LLD_NUM_ALARMS-1)
230 * @param[in] abstime the time to be set for the first alarm
231 *
232 * @api
233 */
234void stStartAlarmN(unsigned alarm, systime_t abstime) {
235
236 osalDbgCheck(alarm < (unsigned)ST_LLD_NUM_ALARMS);
237 osalDbgAssert(stIsAlarmActiveN(alarm) == false, "already active");
238
239 st_lld_start_alarm_n(alarm, abstime);
240}
241
242/**
243 * @brief Stops an additional alarm.
244 * @note This functionality is only available in free running mode, the
245 * behavior in periodic mode is undefined.
246 *
247 * @param[in] alarm alarm channel number (0..ST_LLD_NUM_ALARMS-1)
248 *
249 * @api
250 */
251void stStopAlarmN(unsigned alarm) {
252
253 osalDbgCheck(alarm < (unsigned)ST_LLD_NUM_ALARMS);
254
255 st_lld_stop_alarm_n(alarm);
256}
257
258/**
259 * @brief Sets an additional alarm time.
260 * @note This functionality is only available in free running mode, the
261 * behavior in periodic mode is undefined.
262 *
263 * @param[in] alarm alarm channel number (0..ST_LLD_NUM_ALARMS-1)
264 * @param[in] abstime the time to be set for the next alarm
265 *
266 * @api
267 */
268void stSetAlarmN(unsigned alarm, systime_t abstime) {
269
270 osalDbgCheck(alarm < (unsigned)ST_LLD_NUM_ALARMS);
271 osalDbgAssert(stIsAlarmActiveN(alarm) != false, "not active");
272
273 st_lld_set_alarm_n(alarm, abstime);
274}
275
276/**
277 * @brief Returns an additional alarm current time.
278 * @note This functionality is only available in free running mode, the
279 * behavior in periodic mode is undefined.
280 *
281 * @param[in] alarm alarm channel number (0..ST_LLD_NUM_ALARMS-1)
282 * @return The currently set alarm time.
283 *
284 * @api
285 */
286systime_t stGetAlarmN(unsigned alarm) {
287
288 osalDbgCheck(alarm < (unsigned)ST_LLD_NUM_ALARMS);
289 osalDbgAssert(stIsAlarmActiveN(alarm) != false, "not active");
290
291 return st_lld_get_alarm_n(alarm);
292}
293
294/**
295 * @brief Determines if the specified alarm is active.
296 *
297 * @param[in] alarm alarm channel number (0..ST_LLD_NUM_ALARMS-1)
298 * @return The alarm status.
299 * @retval false if the alarm is not active.
300 * @retval true is the alarm is active
301 *
302 * @api
303 */
304bool stIsAlarmActiveN(unsigned alarm) {
305
306 osalDbgCheck(alarm < (unsigned)ST_LLD_NUM_ALARMS);
307
308 return st_lld_is_alarm_active_n(alarm);
309}
310#endif /* ST_LLD_NUM_ALARMS > 1 */
311#endif /* OSAL_ST_MODE == OSAL_ST_MODE_FREERUNNING */
312
313#endif /* OSAL_ST_MODE != OSAL_ST_MODE_NONE */
314
315/** @} */
#define osalDbgAssert(c, remark)
Condition assertion.
Definition osal.h:264
#define osalDbgCheck(c)
Function parameters check.
Definition osal.h:284
uint32_t systime_t
Type of system time counter.
Definition osal.h:164
void stSetAlarm(systime_t abstime)
Sets the alarm zero time.
Definition hal_st.c:147
systime_t stGetCounter(void)
Returns the time counter value.
Definition hal_st.c:103
#define ST_LLD_NUM_ALARMS
Definition hal_st.h:46
systime_t stGetAlarm(void)
Returns the alarm zero current time.
Definition hal_st.c:163
void stSetCallback(unsigned alarm, st_callback_t cb)
Associates a callback to an alarm.
Definition hal_st.c:197
static bool st_lld_is_alarm_active(void)
Determines if the alarm is active.
Definition hal_st_lld.h:134
void stSetAlarmN(unsigned alarm, systime_t abstime)
Sets an additional alarm time.
Definition hal_st.c:268
void(* st_callback_t)(unsigned alarm)
Definition hal_st.h:53
static void st_lld_start_alarm(systime_t abstime)
Starts the alarm.
Definition hal_st_lld.h:87
bool stIsAlarmActiveN(unsigned alarm)
Determines if the specified alarm is active.
Definition hal_st.c:304
bool stIsAlarmActive(void)
Determines if the alarm zero is active.
Definition hal_st.c:179
void stBindAlarmN(unsigned alarm)
Enables an alarm interrupt on the invoking core.
Definition hal_st.c:213
void stBind(void)
Enables an alarm interrupt on the invoking core.
Definition hal_st.c:87
static void st_lld_stop_alarm(void)
Stops the alarm interrupt.
Definition hal_st_lld.h:97
static systime_t st_lld_get_alarm(void)
Returns the current alarm time.
Definition hal_st_lld.h:120
void stStopAlarmN(unsigned alarm)
Stops an additional alarm.
Definition hal_st.c:251
st_callback_t st_callbacks[ST_LLD_NUM_ALARMS]
Callback pointers for each alarm.
Definition hal_st.c:51
static void st_lld_set_alarm(systime_t abstime)
Sets the alarm time.
Definition hal_st_lld.h:108
void st_lld_init(void)
Low level ST driver initialization.
Definition hal_st_lld.c:62
void stStopAlarm(void)
Stops the alarm zero interrupt.
Definition hal_st.c:133
static systime_t st_lld_get_counter(void)
Returns the time counter value.
Definition hal_st_lld.h:73
void stStartAlarmN(unsigned alarm, systime_t abstime)
Starts an additional alarm.
Definition hal_st.c:234
systime_t stGetAlarmN(unsigned alarm)
Returns an additional alarm current time.
Definition hal_st.c:286
void stInit(void)
ST Driver initialization.
Definition hal_st.c:69
void stStartAlarm(systime_t abstime)
Starts the alarm zero.
Definition hal_st.c:119
HAL subsystem header.