ChibiOS/HAL 9.0.0
hal_gpt.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_gpt.c
19 * @brief GPT Driver code.
20 *
21 * @addtogroup GPT
22 * @{
23 */
24
25#include "hal.h"
26
27#if (HAL_USE_GPT == TRUE) || defined(__DOXYGEN__)
28
29/*===========================================================================*/
30/* Driver local definitions. */
31/*===========================================================================*/
32
33/*===========================================================================*/
34/* Driver exported variables. */
35/*===========================================================================*/
36
37/*===========================================================================*/
38/* Driver local variables and types. */
39/*===========================================================================*/
40
41/*===========================================================================*/
42/* Driver local functions. */
43/*===========================================================================*/
44
45/*===========================================================================*/
46/* Driver exported functions. */
47/*===========================================================================*/
48
49/**
50 * @brief GPT Driver initialization.
51 * @note This function is implicitly invoked by @p halInit(), there is
52 * no need to explicitly initialize the driver.
53 *
54 * @init
55 */
56void gptInit(void) {
57
59}
60
61/**
62 * @brief Initializes the standard part of a @p GPTDriver structure.
63 *
64 * @param[out] gptp pointer to the @p GPTDriver object
65 *
66 * @init
67 */
69
70 gptp->state = GPT_STOP;
71 gptp->config = NULL;
72}
73
74/**
75 * @brief Configures and activates the GPT peripheral.
76 *
77 * @param[in] gptp pointer to the @p GPTDriver object
78 * @param[in] config pointer to the @p GPTConfig object
79 * @return The operation status.
80 *
81 * @api
82 */
83msg_t gptStart(GPTDriver *gptp, const GPTConfig *config) {
84 msg_t msg;
85
86 osalDbgCheck((gptp != NULL) && (config != NULL));
87
89
90 osalDbgAssert((gptp->state == GPT_STOP) || (gptp->state == GPT_READY),
91 "invalid state");
92
93 gptp->config = config;
94
95#if defined(GPT_LLD_ENHANCED_API)
96 msg = gpt_lld_start(gptp);
97 if (msg == HAL_RET_SUCCESS) {
98 gptp->state = GPT_READY;
99 }
100 else {
101 gptp->state = GPT_STOP;
102 }
103#else
104 gpt_lld_start(gptp);
105 gptp->state = GPT_READY;
106 msg = HAL_RET_SUCCESS;
107#endif
108
110
111 return msg;
112}
113
114/**
115 * @brief Deactivates the GPT peripheral.
116 *
117 * @param[in] gptp pointer to the @p GPTDriver object
118 *
119 * @api
120 */
121void gptStop(GPTDriver *gptp) {
122
123 osalDbgCheck(gptp != NULL);
124
125 osalSysLock();
126
127 osalDbgAssert((gptp->state == GPT_STOP) || (gptp->state == GPT_READY),
128 "invalid state");
129
130 gpt_lld_stop(gptp);
131 gptp->config = NULL;
132 gptp->state = GPT_STOP;
133
135}
136
137/**
138 * @brief Changes the interval of GPT peripheral.
139 * @details This function changes the interval of a running GPT unit.
140 * @pre The GPT unit must be running in continuous mode.
141 * @post The GPT unit interval is changed to the new value.
142 *
143 * @param[in] gptp pointer to a @p GPTDriver object
144 * @param[in] interval new cycle time in timer ticks
145 *
146 * @api
147 */
148void gptChangeInterval(GPTDriver *gptp, gptcnt_t interval) {
149
150 osalDbgCheck(gptp != NULL);
151
152 osalSysLock();
154 "invalid state");
155 gptChangeIntervalI(gptp, interval);
157}
158
159/**
160 * @brief Starts the timer in continuous mode.
161 *
162 * @param[in] gptp pointer to the @p GPTDriver object
163 * @param[in] interval period in ticks
164 *
165 * @api
166 */
167void gptStartContinuous(GPTDriver *gptp, gptcnt_t interval) {
168
169 osalSysLock();
170 gptStartContinuousI(gptp, interval);
172}
173
174/**
175 * @brief Starts the timer in continuous mode.
176 *
177 * @param[in] gptp pointer to the @p GPTDriver object
178 * @param[in] interval period in ticks
179 *
180 * @iclass
181 */
183
185 osalDbgCheck(gptp != NULL);
187 "invalid state");
188
189 gptp->state = GPT_CONTINUOUS;
190 gpt_lld_start_timer(gptp, interval);
191}
192
193/**
194 * @brief Starts the timer in one shot mode.
195 *
196 * @param[in] gptp pointer to the @p GPTDriver object
197 * @param[in] interval time interval in ticks
198 *
199 * @api
200 */
201void gptStartOneShot(GPTDriver *gptp, gptcnt_t interval) {
202
203 osalSysLock();
204 gptStartOneShotI(gptp, interval);
206}
207
208/**
209 * @brief Starts the timer in one shot mode.
210 *
211 * @param[in] gptp pointer to the @p GPTDriver object
212 * @param[in] interval time interval in ticks
213 *
214 * @api
215 */
216void gptStartOneShotI(GPTDriver *gptp, gptcnt_t interval) {
217
219 osalDbgCheck(gptp != NULL);
220 osalDbgCheck(gptp->config->callback != NULL);
222 "invalid state");
223
224 gptp->state = GPT_ONESHOT;
225 gpt_lld_start_timer(gptp, interval);
226}
227
228/**
229 * @brief Stops the timer.
230 *
231 * @param[in] gptp pointer to the @p GPTDriver object
232 *
233 * @api
234 */
236
237 osalSysLock();
238 gptStopTimerI(gptp);
240}
241
242/**
243 * @brief Stops the timer.
244 *
245 * @param[in] gptp pointer to the @p GPTDriver object
246 *
247 * @api
248 */
250
252 osalDbgCheck(gptp != NULL);
253 osalDbgAssert((gptp->state == GPT_READY) || (gptp->state == GPT_CONTINUOUS) ||
254 (gptp->state == GPT_ONESHOT),
255 "invalid state");
256
257 gptp->state = GPT_READY;
258 gpt_lld_stop_timer(gptp);
259}
260
261/**
262 * @brief Starts the timer in one shot mode and waits for completion.
263 * @details This function specifically polls the timer waiting for completion
264 * in order to not have extra delays caused by interrupt servicing,
265 * this function is only recommended for short delays.
266 * @note The configured callback is not invoked when using this function.
267 *
268 * @param[in] gptp pointer to the @p GPTDriver object
269 * @param[in] interval time interval in ticks
270 *
271 * @api
272 */
273void gptPolledDelay(GPTDriver *gptp, gptcnt_t interval) {
274
276 "invalid state");
277
278 gptp->state = GPT_ONESHOT;
279 gpt_lld_polled_delay(gptp, interval);
280 gptp->state = GPT_READY;
281}
282
283#endif /* HAL_USE_GPT == TRUE */
284
285/** @} */
void gpt_lld_start(GPTDriver *gptp)
Configures and activates the GPT peripheral.
Definition hal_gpt_lld.c:80
void gpt_lld_polled_delay(GPTDriver *gptp, gptcnt_t interval)
Starts the timer in one shot mode and waits for completion.
void gpt_lld_stop(GPTDriver *gptp)
Deactivates the GPT peripheral.
void gptPolledDelay(GPTDriver *gptp, gptcnt_t interval)
Starts the timer in one shot mode and waits for completion.
Definition hal_gpt.c:273
void gpt_lld_start_timer(GPTDriver *gptp, gptcnt_t interval)
Starts the timer in continuous mode.
void gptObjectInit(GPTDriver *gptp)
Initializes the standard part of a GPTDriver structure.
Definition hal_gpt.c:68
#define gptChangeIntervalI(gptp, interval)
Changes the interval of GPT peripheral.
Definition hal_gpt.h:86
void gptInit(void)
GPT Driver initialization.
Definition hal_gpt.c:56
void gpt_lld_stop_timer(GPTDriver *gptp)
Stops the timer.
void gptStop(GPTDriver *gptp)
Deactivates the GPT peripheral.
Definition hal_gpt.c:121
void gptStartOneShot(GPTDriver *gptp, gptcnt_t interval)
Starts the timer in one shot mode.
Definition hal_gpt.c:201
void gptStartOneShotI(GPTDriver *gptp, gptcnt_t interval)
Starts the timer in one shot mode.
Definition hal_gpt.c:216
uint16_t gptcnt_t
GPT counter type.
Definition hal_gpt_lld.h:68
void gptChangeInterval(GPTDriver *gptp, gptcnt_t interval)
Changes the interval of GPT peripheral.
Definition hal_gpt.c:148
void gptStopTimer(GPTDriver *gptp)
Stops the timer.
Definition hal_gpt.c:235
void gpt_lld_init(void)
Low level GPT driver initialization.
Definition hal_gpt_lld.c:65
void gptStartContinuous(GPTDriver *gptp, gptcnt_t interval)
Starts the timer in continuous mode.
Definition hal_gpt.c:167
void gptStopTimerI(GPTDriver *gptp)
Stops the timer.
Definition hal_gpt.c:249
void gptStartContinuousI(GPTDriver *gptp, gptcnt_t interval)
Starts the timer in continuous mode.
Definition hal_gpt.c:182
msg_t gptStart(GPTDriver *gptp, const GPTConfig *config)
Configures and activates the GPT peripheral.
Definition hal_gpt.c:83
@ GPT_READY
Definition hal_gpt.h:52
@ GPT_ONESHOT
Definition hal_gpt.h:54
@ GPT_STOP
Definition hal_gpt.h:51
@ GPT_CONTINUOUS
Definition hal_gpt.h:53
#define HAL_RET_SUCCESS
Definition hal.h:93
static void osalSysLock(void)
Enters a critical zone from thread context.
Definition osal.h:601
static void osalSysUnlock(void)
Leaves a critical zone from thread context.
Definition osal.h:611
int32_t msg_t
Type of a message.
Definition osal.h:159
#define osalDbgAssert(c, remark)
Condition assertion.
Definition osal.h:264
#define osalDbgCheck(c)
Function parameters check.
Definition osal.h:284
#define osalDbgCheckClassI()
I-Class state check.
Definition osal.h:298
HAL subsystem header.
Driver configuration structure.
Definition hal_gpt_lld.h:74
gptcallback_t callback
Timer callback pointer.
Definition hal_gpt_lld.h:85
Structure representing a GPT driver.
Definition hal_gpt_lld.h:92
const GPTConfig * config
Current configuration data.
gptstate_t state
Driver state.
Definition hal_gpt_lld.h:96