ChibiOS/HAL  6.1.0
hal_ext.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_ext.c
19  * @brief EXT Driver code.
20  *
21  * @addtogroup EXT
22  * @{
23  */
24 
25 #include "hal.h"
26 
27 #if (HAL_USE_EXT == 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 EXT 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 extInit(void) {
57 
58  ext_lld_init();
59 }
60 
61 /**
62  * @brief Initializes the standard part of a @p EXTDriver structure.
63  *
64  * @param[out] extp pointer to the @p EXTDriver object
65  *
66  * @init
67  */
68 void extObjectInit(EXTDriver *extp) {
69 
70  extp->state = EXT_STOP;
71  extp->config = NULL;
72 }
73 
74 /**
75  * @brief Configures and activates the EXT peripheral.
76  * @post After activation all EXT channels are in the disabled state,
77  * use @p extChannelEnable() in order to activate them.
78  *
79  * @param[in] extp pointer to the @p EXTDriver object
80  * @param[in] config pointer to the @p EXTConfig object
81  *
82  * @api
83  */
84 void extStart(EXTDriver *extp, const EXTConfig *config) {
85 
86  osalDbgCheck((extp != NULL) && (config != NULL));
87 
88  osalSysLock();
89  osalDbgAssert((extp->state == EXT_STOP) || (extp->state == EXT_ACTIVE),
90  "invalid state");
91  extp->config = config;
92  ext_lld_start(extp);
93  extp->state = EXT_ACTIVE;
94  osalSysUnlock();
95 }
96 
97 /**
98  * @brief Deactivates the EXT peripheral.
99  *
100  * @param[in] extp pointer to the @p EXTDriver object
101  *
102  * @api
103  */
104 void extStop(EXTDriver *extp) {
105 
106  osalDbgCheck(extp != NULL);
107 
108  osalSysLock();
109 
110  osalDbgAssert((extp->state == EXT_STOP) || (extp->state == EXT_ACTIVE),
111  "invalid state");
112 
113  ext_lld_stop(extp);
114  extp->config = NULL;
115  extp->state = EXT_STOP;
116 
117  osalSysUnlock();
118 }
119 
120 /**
121  * @brief Enables an EXT channel.
122  * @pre The channel must not be in @p EXT_CH_MODE_DISABLED mode.
123  *
124  * @param[in] extp pointer to the @p EXTDriver object
125  * @param[in] channel channel to be enabled
126  *
127  * @api
128  */
130 
131  osalDbgCheck((extp != NULL) && (channel < (expchannel_t)EXT_MAX_CHANNELS));
132 
133  osalSysLock();
134  osalDbgAssert((extp->state == EXT_ACTIVE) &&
135  ((extp->config->channels[channel].mode &
137  "invalid state");
138  extChannelEnableI(extp, channel);
139  osalSysUnlock();
140 }
141 
142 /**
143  * @brief Disables an EXT channel.
144  * @pre The channel must not be in @p EXT_CH_MODE_DISABLED mode.
145  *
146  * @param[in] extp pointer to the @p EXTDriver object
147  * @param[in] channel channel to be disabled
148  *
149  * @api
150  */
152 
153  osalDbgCheck((extp != NULL) && (channel < (expchannel_t)EXT_MAX_CHANNELS));
154 
155  osalSysLock();
156  osalDbgAssert((extp->state == EXT_ACTIVE) &&
157  ((extp->config->channels[channel].mode &
159  "invalid state");
160  extChannelDisableI(extp, channel);
161  osalSysUnlock();
162 }
163 
164 /**
165  * @brief Changes the operation mode of a channel.
166  * @note This function attempts to write over the current configuration
167  * structure that must have been not declared constant. This
168  * violates the @p const qualifier in @p extStart() but it is
169  * intentional.
170  * @note This function cannot be used if the configuration structure is
171  * declared @p const.
172  * @note The effect of this function on constant configuration structures
173  * is not defined.
174  *
175  * @param[in] extp pointer to the @p EXTDriver object
176  * @param[in] channel channel to be changed
177  * @param[in] extcp new configuration for the channel
178  *
179  * @iclass
180  */
182  expchannel_t channel,
183  const EXTChannelConfig *extcp) {
184  EXTChannelConfig *oldcp;
185 
186  osalDbgCheck((extp != NULL) &&
187  (channel < (expchannel_t)EXT_MAX_CHANNELS) &&
188  (extcp != NULL));
189 
190  osalDbgAssert(extp->state == EXT_ACTIVE, "invalid state");
191 
192  /* Note that here the access is enforced as non-const, known access
193  violation.*/
194  /*lint -save -e9005 [11.8] Known issue, the driver needs rework here.*/
195  oldcp = (EXTChannelConfig *)&extp->config->channels[channel];
196  /*lint -restore*/
197 
198  /* Overwriting the old channels configuration then the channel is
199  reconfigured by the low level driver.*/
200  *oldcp = *extcp;
201  ext_lld_channel_enable(extp, channel);
202 }
203 
204 #endif /* HAL_USE_EXT == TRUE */
205 
206 /** @} */
Channel configuration structure.
Definition: hal_ext_lld.h:81
uint32_t expchannel_t
EXT channel identifier.
Definition: hal_ext_lld.h:68
void extInit(void)
EXT Driver initialization.
Definition: hal_ext.c:56
void extChannelEnable(EXTDriver *extp, expchannel_t channel)
Enables an EXT channel.
Definition: hal_ext.c:129
const EXTConfig * config
Current configuration data.
Definition: hal_ext_lld.h:118
#define extChannelEnableI(extp, channel)
Enables an EXT channel.
Definition: hal_ext.h:94
void extStart(EXTDriver *extp, const EXTConfig *config)
Configures and activates the EXT peripheral.
Definition: hal_ext.c:84
void ext_lld_stop(EXTDriver *extp)
Deactivates the EXT peripheral.
Definition: hal_ext_lld.c:101
uint32_t mode
Channel mode.
Definition: hal_ext_lld.h:85
HAL subsystem header.
void extStop(EXTDriver *extp)
Deactivates the EXT peripheral.
Definition: hal_ext.c:104
#define extChannelDisableI(extp, channel)
Disables an EXT channel.
Definition: hal_ext.h:104
static void osalSysUnlock(void)
Leaves a critical zone from thread context.
Definition: osal.h:540
EXTChannelConfig channels[EXT_MAX_CHANNELS]
Channel configurations.
Definition: hal_ext_lld.h:103
void extChannelDisable(EXTDriver *extp, expchannel_t channel)
Disables an EXT channel.
Definition: hal_ext.c:151
void ext_lld_start(EXTDriver *extp)
Configures and activates the EXT peripheral.
Definition: hal_ext_lld.c:80
#define EXT_CH_MODE_DISABLED
Channel disabled.
Definition: hal_ext.h:39
#define EXT_MAX_CHANNELS
Available number of EXT channels.
Definition: hal_ext_lld.h:37
void ext_lld_init(void)
Low level EXT driver initialization.
Definition: hal_ext_lld.c:65
#define osalDbgCheck(c)
Function parameters check.
Definition: osal.h:278
#define EXT_CH_MODE_EDGES_MASK
Mask of edges field.
Definition: hal_ext.h:38
extstate_t state
Driver state.
Definition: hal_ext_lld.h:114
Driver configuration structure.
Definition: hal_ext_lld.h:99
static void osalSysLock(void)
Enters a critical zone from thread context.
Definition: osal.h:530
Structure representing an EXT driver.
Definition: hal_ext_lld.h:110
#define osalDbgAssert(c, remark)
Condition assertion.
Definition: osal.h:258
void ext_lld_channel_enable(EXTDriver *extp, expchannel_t channel)
Enables an EXT channel.
Definition: hal_ext_lld.c:123
void extSetChannelModeI(EXTDriver *extp, expchannel_t channel, const EXTChannelConfig *extcp)
Changes the operation mode of a channel.
Definition: hal_ext.c:181
void extObjectInit(EXTDriver *extp)
Initializes the standard part of a EXTDriver structure.
Definition: hal_ext.c:68