ChibiOS/HAL  6.1.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  */
56 void gptInit(void) {
57 
58  gpt_lld_init();
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  */
68 void gptObjectInit(GPTDriver *gptp) {
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  *
80  * @api
81  */
82 void gptStart(GPTDriver *gptp, const GPTConfig *config) {
83 
84  osalDbgCheck((gptp != NULL) && (config != NULL));
85 
86  osalSysLock();
87  osalDbgAssert((gptp->state == GPT_STOP) || (gptp->state == GPT_READY),
88  "invalid state");
89  gptp->config = config;
90  gpt_lld_start(gptp);
91  gptp->state = GPT_READY;
92  osalSysUnlock();
93 }
94 
95 /**
96  * @brief Deactivates the GPT peripheral.
97  *
98  * @param[in] gptp pointer to the @p GPTDriver object
99  *
100  * @api
101  */
102 void gptStop(GPTDriver *gptp) {
103 
104  osalDbgCheck(gptp != NULL);
105 
106  osalSysLock();
107 
108  osalDbgAssert((gptp->state == GPT_STOP) || (gptp->state == GPT_READY),
109  "invalid state");
110 
111  gpt_lld_stop(gptp);
112  gptp->config = NULL;
113  gptp->state = GPT_STOP;
114 
115  osalSysUnlock();
116 }
117 
118 /**
119  * @brief Changes the interval of GPT peripheral.
120  * @details This function changes the interval of a running GPT unit.
121  * @pre The GPT unit must be running in continuous mode.
122  * @post The GPT unit interval is changed to the new value.
123  *
124  * @param[in] gptp pointer to a @p GPTDriver object
125  * @param[in] interval new cycle time in timer ticks
126  *
127  * @api
128  */
129 void gptChangeInterval(GPTDriver *gptp, gptcnt_t interval) {
130 
131  osalDbgCheck(gptp != NULL);
132 
133  osalSysLock();
135  "invalid state");
136  gptChangeIntervalI(gptp, interval);
137  osalSysUnlock();
138 }
139 
140 /**
141  * @brief Starts the timer in continuous mode.
142  *
143  * @param[in] gptp pointer to the @p GPTDriver object
144  * @param[in] interval period in ticks
145  *
146  * @api
147  */
148 void gptStartContinuous(GPTDriver *gptp, gptcnt_t interval) {
149 
150  osalSysLock();
151  gptStartContinuousI(gptp, interval);
152  osalSysUnlock();
153 }
154 
155 /**
156  * @brief Starts the timer in continuous mode.
157  *
158  * @param[in] gptp pointer to the @p GPTDriver object
159  * @param[in] interval period in ticks
160  *
161  * @iclass
162  */
163 void gptStartContinuousI(GPTDriver *gptp, gptcnt_t interval) {
164 
166  osalDbgCheck(gptp != NULL);
167  osalDbgAssert(gptp->state == GPT_READY,
168  "invalid state");
169 
170  gptp->state = GPT_CONTINUOUS;
171  gpt_lld_start_timer(gptp, interval);
172 }
173 
174 /**
175  * @brief Starts the timer in one shot mode.
176  *
177  * @param[in] gptp pointer to the @p GPTDriver object
178  * @param[in] interval time interval in ticks
179  *
180  * @api
181  */
182 void gptStartOneShot(GPTDriver *gptp, gptcnt_t interval) {
183 
184  osalSysLock();
185  gptStartOneShotI(gptp, interval);
186  osalSysUnlock();
187 }
188 
189 /**
190  * @brief Starts the timer in one shot mode.
191  *
192  * @param[in] gptp pointer to the @p GPTDriver object
193  * @param[in] interval time interval in ticks
194  *
195  * @api
196  */
197 void gptStartOneShotI(GPTDriver *gptp, gptcnt_t interval) {
198 
200  osalDbgCheck(gptp != NULL);
201  osalDbgCheck(gptp->config->callback != NULL);
202  osalDbgAssert(gptp->state == GPT_READY,
203  "invalid state");
204 
205  gptp->state = GPT_ONESHOT;
206  gpt_lld_start_timer(gptp, interval);
207 }
208 
209 /**
210  * @brief Stops the timer.
211  *
212  * @param[in] gptp pointer to the @p GPTDriver object
213  *
214  * @api
215  */
216 void gptStopTimer(GPTDriver *gptp) {
217 
218  osalSysLock();
219  gptStopTimerI(gptp);
220  osalSysUnlock();
221 }
222 
223 /**
224  * @brief Stops the timer.
225  *
226  * @param[in] gptp pointer to the @p GPTDriver object
227  *
228  * @api
229  */
231 
233  osalDbgCheck(gptp != NULL);
234  osalDbgAssert((gptp->state == GPT_READY) || (gptp->state == GPT_CONTINUOUS) ||
235  (gptp->state == GPT_ONESHOT),
236  "invalid state");
237 
238  gptp->state = GPT_READY;
239  gpt_lld_stop_timer(gptp);
240 }
241 
242 /**
243  * @brief Starts the timer in one shot mode and waits for completion.
244  * @details This function specifically polls the timer waiting for completion
245  * in order to not have extra delays caused by interrupt servicing,
246  * this function is only recommended for short delays.
247  * @note The configured callback is not invoked when using this function.
248  *
249  * @param[in] gptp pointer to the @p GPTDriver object
250  * @param[in] interval time interval in ticks
251  *
252  * @api
253  */
254 void gptPolledDelay(GPTDriver *gptp, gptcnt_t interval) {
255 
256  osalDbgAssert(gptp->state == GPT_READY,
257  "invalid state");
258 
259  gptp->state = GPT_ONESHOT;
260  gpt_lld_polled_delay(gptp, interval);
261  gptp->state = GPT_READY;
262 }
263 
264 #endif /* HAL_USE_GPT == TRUE */
265 
266 /** @} */
void gpt_lld_polled_delay(GPTDriver *gptp, gptcnt_t interval)
Starts the timer in one shot mode and waits for completion.
Definition: hal_gpt_lld.c:154
void gpt_lld_init(void)
Low level GPT driver initialization.
Definition: hal_gpt_lld.c:65
void gptStopTimerI(GPTDriver *gptp)
Stops the timer.
Definition: hal_gpt.c:230
void gptStartContinuous(GPTDriver *gptp, gptcnt_t interval)
Starts the timer in continuous mode.
Definition: hal_gpt.c:148
void gptStopTimer(GPTDriver *gptp)
Stops the timer.
Definition: hal_gpt.c:216
HAL subsystem header.
#define osalDbgCheckClassI()
I-Class state check.
Definition: osal.h:292
void gpt_lld_stop(GPTDriver *gptp)
Deactivates the GPT peripheral.
Definition: hal_gpt_lld.c:101
#define gptChangeIntervalI(gptp, interval)
Changes the interval of GPT peripheral.
Definition: hal_gpt.h:86
static void osalSysUnlock(void)
Leaves a critical zone from thread context.
Definition: osal.h:540
void gpt_lld_start(GPTDriver *gptp)
Configures and activates the GPT peripheral.
Definition: hal_gpt_lld.c:80
Structure representing a GPT driver.
Definition: hal_gpt_lld.h:92
void gptInit(void)
GPT Driver initialization.
Definition: hal_gpt.c:56
void gptStart(GPTDriver *gptp, const GPTConfig *config)
Configures and activates the GPT peripheral.
Definition: hal_gpt.c:82
gptcallback_t callback
Timer callback pointer.
Definition: hal_gpt_lld.h:85
Driver configuration structure.
Definition: hal_gpt_lld.h:74
const GPTConfig * config
Current configuration data.
Definition: hal_gpt_lld.h:100
void gptStop(GPTDriver *gptp)
Deactivates the GPT peripheral.
Definition: hal_gpt.c:102
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:129
gptstate_t state
Driver state.
Definition: hal_gpt_lld.h:96
#define osalDbgCheck(c)
Function parameters check.
Definition: osal.h:278
void gpt_lld_start_timer(GPTDriver *gptp, gptcnt_t interval)
Starts the timer in continuous mode.
Definition: hal_gpt_lld.c:123
void gptObjectInit(GPTDriver *gptp)
Initializes the standard part of a GPTDriver structure.
Definition: hal_gpt.c:68
void gptPolledDelay(GPTDriver *gptp, gptcnt_t interval)
Starts the timer in one shot mode and waits for completion.
Definition: hal_gpt.c:254
static void osalSysLock(void)
Enters a critical zone from thread context.
Definition: osal.h:530
void gptStartOneShotI(GPTDriver *gptp, gptcnt_t interval)
Starts the timer in one shot mode.
Definition: hal_gpt.c:197
#define osalDbgAssert(c, remark)
Condition assertion.
Definition: osal.h:258
void gpt_lld_stop_timer(GPTDriver *gptp)
Stops the timer.
Definition: hal_gpt_lld.c:137
void gptStartContinuousI(GPTDriver *gptp, gptcnt_t interval)
Starts the timer in continuous mode.
Definition: hal_gpt.c:163
void gptStartOneShot(GPTDriver *gptp, gptcnt_t interval)
Starts the timer in one shot mode.
Definition: hal_gpt.c:182