ChibiOS  21.6.0
hal_icu.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_icu.c
19  * @brief ICU Driver code.
20  *
21  * @addtogroup ICU
22  * @{
23  */
24 
25 #include "hal.h"
26 
27 #if (HAL_USE_ICU == 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 ICU 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 icuInit(void) {
57 
58  icu_lld_init();
59 }
60 
61 /**
62  * @brief Initializes the standard part of a @p ICUDriver structure.
63  *
64  * @param[out] icup pointer to the @p ICUDriver object
65  *
66  * @init
67  */
68 void icuObjectInit(ICUDriver *icup) {
69 
70  icup->state = ICU_STOP;
71  icup->config = NULL;
72 }
73 
74 /**
75  * @brief Configures and activates the ICU peripheral.
76  *
77  * @param[in] icup pointer to the @p ICUDriver object
78  * @param[in] config pointer to the @p ICUConfig object
79  *
80  * @api
81  */
82 void icuStart(ICUDriver *icup, const ICUConfig *config) {
83 
84  osalDbgCheck((icup != NULL) && (config != NULL));
85 
86  osalSysLock();
87  osalDbgAssert((icup->state == ICU_STOP) || (icup->state == ICU_READY),
88  "invalid state");
89  icup->config = config;
90  icu_lld_start(icup);
91  icup->state = ICU_READY;
92  osalSysUnlock();
93 }
94 
95 /**
96  * @brief Deactivates the ICU peripheral.
97  *
98  * @param[in] icup pointer to the @p ICUDriver object
99  *
100  * @api
101  */
102 void icuStop(ICUDriver *icup) {
103 
104  osalDbgCheck(icup != NULL);
105 
106  osalSysLock();
107 
108  osalDbgAssert((icup->state == ICU_STOP) || (icup->state == ICU_READY),
109  "invalid state");
110 
111  icu_lld_stop(icup);
112  icup->config = NULL;
113  icup->state = ICU_STOP;
114 
115  osalSysUnlock();
116 }
117 
118 /**
119  * @brief Starts the input capture.
120  *
121  * @param[in] icup pointer to the @p ICUDriver object
122  *
123  * @api
124  */
126 
127  osalDbgCheck(icup != NULL);
128 
129  osalSysLock();
130  osalDbgAssert(icup->state == ICU_READY, "invalid state");
131  icuStartCaptureI(icup);
132  osalSysUnlock();
133 }
134 
135 /**
136  * @brief Waits for a completed capture.
137  * @note The operation could be performed in polled mode depending on.
138  * @note In order to use this function notifications must be disabled.
139  * @pre The driver must be in @p ICU_WAITING or @p ICU_ACTIVE states.
140  * @post After the capture is available the driver is in @p ICU_ACTIVE
141  * state. If a capture fails then the driver is in @p ICU_WAITING
142  * state.
143  *
144  * @param[in] icup pointer to the @p ICUDriver object
145  * @return The capture status.
146  * @retval false if the capture is successful.
147  * @retval true if a timer overflow occurred.
148  *
149  * @api
150  */
152  bool result;
153 
154  osalDbgCheck(icup != NULL);
155 
156  osalSysLock();
157  osalDbgAssert((icup->state == ICU_WAITING) || (icup->state == ICU_ACTIVE),
158  "invalid state");
160  "notifications enabled");
161  result = icu_lld_wait_capture(icup);
162  icup->state = result ? ICU_WAITING : ICU_ACTIVE;
163  osalSysUnlock();
164 
165  return result;
166 }
167 
168 /**
169  * @brief Stops the input capture.
170  *
171  * @param[in] icup pointer to the @p ICUDriver object
172  *
173  * @api
174  */
176 
177  osalDbgCheck(icup != NULL);
178 
179  osalSysLock();
180  osalDbgAssert((icup->state == ICU_READY) || (icup->state == ICU_WAITING) ||
181  (icup->state == ICU_ACTIVE),
182  "invalid state");
183  icuStopCaptureI(icup);
184  osalSysUnlock();
185 }
186 
187 /**
188  * @brief Enables notifications.
189  * @pre The ICU unit must have been activated using @p icuStart() and the
190  * capture started using @p icuStartCapture().
191  * @note If the notification is already enabled then the call has no effect.
192  *
193  * @param[in] icup pointer to the @p ICUDriver object
194  *
195  * @api
196  */
198 
199  osalDbgCheck(icup != NULL);
200 
201  osalSysLock();
202  osalDbgAssert((icup->state == ICU_WAITING) || (icup->state == ICU_ACTIVE),
203  "invalid state");
205  osalSysUnlock();
206 }
207 
208 /**
209  * @brief Disables notifications.
210  * @pre The ICU unit must have been activated using @p icuStart() and the
211  * capture started using @p icuStartCapture().
212  * @note If the notification is already disabled then the call has no effect.
213  *
214  * @param[in] icup pointer to the @p ICUDriver object
215  *
216  * @api
217  */
219 
220  osalDbgCheck(icup != NULL);
221 
222  osalSysLock();
223  osalDbgAssert((icup->state == ICU_WAITING) || (icup->state == ICU_ACTIVE),
224  "invalid state");
226  osalSysUnlock();
227 }
228 
229 #endif /* HAL_USE_ICU == TRUE */
230 
231 /** @} */
icuDisableNotificationsI
#define icuDisableNotificationsI(icup)
Disables notifications.
Definition: hal_icu.h:125
ICU_WAITING
@ ICU_WAITING
Definition: hal_icu.h:53
icuAreNotificationsEnabledX
#define icuAreNotificationsEnabledX(icup)
Check on notifications status.
Definition: hal_icu.h:137
icu_lld_stop
void icu_lld_stop(ICUDriver *icup)
Deactivates the ICU peripheral.
Definition: hal_icu_lld.c:100
ICUDriver::state
icustate_t state
Driver state.
Definition: hal_icu_lld.h:115
icuObjectInit
void icuObjectInit(ICUDriver *icup)
Initializes the standard part of a ICUDriver structure.
Definition: hal_icu.c:68
hal.h
HAL subsystem header.
icuStartCapture
void icuStartCapture(ICUDriver *icup)
Starts the input capture.
Definition: hal_icu.c:125
osalSysUnlock
static void osalSysUnlock(void)
Leaves a critical zone from thread context.
Definition: osal.h:611
ICUDriver::config
const ICUConfig * config
Current configuration data.
Definition: hal_icu_lld.h:119
icuStopCaptureI
#define icuStopCaptureI(icup)
Stops the input capture.
Definition: hal_icu.h:98
icu_lld_start
void icu_lld_start(ICUDriver *icup)
Configures and activates the ICU peripheral.
Definition: hal_icu_lld.c:81
icuWaitCapture
bool icuWaitCapture(ICUDriver *icup)
Waits for a completed capture.
Definition: hal_icu.c:151
icuEnableNotificationsI
#define icuEnableNotificationsI(icup)
Enables notifications.
Definition: hal_icu.h:113
icuDisableNotifications
void icuDisableNotifications(ICUDriver *icup)
Disables notifications.
Definition: hal_icu.c:218
ICU_ACTIVE
@ ICU_ACTIVE
Definition: hal_icu.h:54
ICU_STOP
@ ICU_STOP
Definition: hal_icu.h:51
icuStartCaptureI
#define icuStartCaptureI(icup)
Starts the input capture.
Definition: hal_icu.h:86
icuStopCapture
void icuStopCapture(ICUDriver *icup)
Stops the input capture.
Definition: hal_icu.c:175
ICUDriver
Structure representing an ICU driver.
Definition: hal_icu_lld.h:111
osalDbgCheck
#define osalDbgCheck(c)
Function parameters check.
Definition: osal.h:284
ICUConfig
Driver configuration structure.
Definition: hal_icu_lld.h:82
icu_lld_init
void icu_lld_init(void)
Low level ICU driver initialization.
Definition: hal_icu_lld.c:66
osalSysLock
static void osalSysLock(void)
Enters a critical zone from thread context.
Definition: osal.h:601
icuStop
void icuStop(ICUDriver *icup)
Deactivates the ICU peripheral.
Definition: hal_icu.c:102
ICU_READY
@ ICU_READY
Definition: hal_icu.h:52
icu_lld_wait_capture
bool icu_lld_wait_capture(ICUDriver *icup)
Waits for a completed capture.
Definition: hal_icu_lld.c:136
osalDbgAssert
#define osalDbgAssert(c, remark)
Condition assertion.
Definition: osal.h:264
icuStart
void icuStart(ICUDriver *icup, const ICUConfig *config)
Configures and activates the ICU peripheral.
Definition: hal_icu.c:82
icuEnableNotifications
void icuEnableNotifications(ICUDriver *icup)
Enables notifications.
Definition: hal_icu.c:197
icuInit
void icuInit(void)
ICU Driver initialization.
Definition: hal_icu.c:56