amiro-os / periphery-lld / periphAL.h @ 47e89ebf
History | View | Annotate | Download (22.652 KB)
1 |
/*
|
---|---|
2 |
AMiRo-OS is an operating system designed for the Autonomous Mini Robot (AMiRo) platform.
|
3 |
Copyright (C) 2016..2019 Thomas Schöpping et al.
|
4 |
|
5 |
This program is free software: you can redistribute it and/or modify
|
6 |
it under the terms of the GNU General Public License as published by
|
7 |
the Free Software Foundation, either version 3 of the License, or
|
8 |
(at your option) any later version.
|
9 |
|
10 |
This program is distributed in the hope that it will be useful,
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
GNU General Public License for more details.
|
14 |
|
15 |
You should have received a copy of the GNU General Public License
|
16 |
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17 |
*/
|
18 |
|
19 |
#ifndef AMIROOS_PERIPHAL_H
|
20 |
#define AMIROOS_PERIPHAL_H
|
21 |
|
22 |
#include <amiro-lld.h> |
23 |
|
24 |
/*============================================================================*/
|
25 |
/* VERSION */
|
26 |
/*============================================================================*/
|
27 |
|
28 |
/**
|
29 |
* @brief The periphery abstraction layer interface major version.
|
30 |
* @note Changes of the major version imply incompatibilities.
|
31 |
*/
|
32 |
#define PERIPHAL_VERSION_MAJOR 1 |
33 |
|
34 |
/**
|
35 |
* @brief The periphery abstraction layer interface minor version.
|
36 |
* @note A higher minor version implies new functionalty, but all old interfaces are still available.
|
37 |
*/
|
38 |
#define PERIPHAL_VERSION_MINOR 1 |
39 |
|
40 |
/*============================================================================*/
|
41 |
/* DEPENDENCIES */
|
42 |
/*============================================================================*/
|
43 |
|
44 |
#include <aosconf.h> |
45 |
#include <hal.h> |
46 |
|
47 |
/*============================================================================*/
|
48 |
/* DEBUG */
|
49 |
/*============================================================================*/
|
50 |
|
51 |
#if (AMIROOS_CFG_DBG == true) || defined(__DOXYGEN__) |
52 |
|
53 |
#if defined(__cplusplus)
|
54 |
extern "C" { |
55 |
#endif /* defined(__cplusplus) */ |
56 |
void _apalDbgAssertMsg(const bool c, const char* fmt, ...); |
57 |
void apalDbgPrintf(const char* fmt, ...); |
58 |
#if defined(__cplusplus)
|
59 |
} |
60 |
#endif /* defined(__cplusplus) */ |
61 |
|
62 |
/**
|
63 |
* @brief Assert function to check a given condition.
|
64 |
*
|
65 |
* @param[in] c The condition to check.
|
66 |
*/
|
67 |
#define apalDbgAssert(c) _apalDbgAssertMsg(c, "%s(%u): apalDbgAssert failed", __FILE__, __LINE__) |
68 |
|
69 |
|
70 |
#else /* (AMIROOS_CFG_DBG != true) */ |
71 |
|
72 |
#define apalDbgAssert(constition)
|
73 |
#define apalDbgAssertMsg(condition, fmt, ...)
|
74 |
#define apalDbgPrintf(fmt, ...)
|
75 |
|
76 |
#endif /* (AMIROOS_CFG_DBG == true) */ |
77 |
|
78 |
/*============================================================================*/
|
79 |
/* GENERAL */
|
80 |
/*============================================================================*/
|
81 |
|
82 |
/**
|
83 |
* @brief Delay execution by a specific number of microseconds.
|
84 |
*
|
85 |
* @param[in] us Time to sleep until execution continues in microseconds.
|
86 |
*/
|
87 |
static inline void apalSleep(apalTime_t us) |
88 |
{ |
89 |
// check if the specified time can be represented by the system
|
90 |
apalDbgAssert(us <= chTimeI2US(TIME_INFINITE)); |
91 |
|
92 |
const sysinterval_t interval = chTimeUS2I(us);
|
93 |
// TIME_IMMEDIATE makes no sense and would even cause system halt
|
94 |
if (interval != TIME_IMMEDIATE) {
|
95 |
chThdSleep(interval); |
96 |
} |
97 |
return;
|
98 |
} |
99 |
|
100 |
/*============================================================================*/
|
101 |
/* GPIO */
|
102 |
/*============================================================================*/
|
103 |
|
104 |
#if (HAL_USE_PAL == TRUE) || defined (__DOXYGEN__)
|
105 |
|
106 |
/**
|
107 |
* @brief GPIO driver type.
|
108 |
*/
|
109 |
struct apalGpio_t {
|
110 |
ioline_t line; |
111 |
} PACKED_VAR; |
112 |
|
113 |
/**
|
114 |
* @brief Read the current value of a GPIO pin.
|
115 |
*
|
116 |
* @param[in] gpio GPIO to read.
|
117 |
* @param[out] val Current value of the GPIO.
|
118 |
*
|
119 |
* @return The status indicates whether the function call was successful.
|
120 |
*/
|
121 |
static inline apalExitStatus_t apalGpioRead(apalGpio_t* gpio, apalGpioState_t* const val) |
122 |
{ |
123 |
apalDbgAssert(gpio != NULL);
|
124 |
apalDbgAssert(val != NULL);
|
125 |
|
126 |
*val = (palReadLine(gpio->line) == PAL_HIGH) ? APAL_GPIO_HIGH : APAL_GPIO_LOW; |
127 |
return APAL_STATUS_OK;
|
128 |
} |
129 |
|
130 |
/**
|
131 |
* @brief Set the value of a GPIO pin.
|
132 |
*
|
133 |
* @param[in] gpio GPIO to write.
|
134 |
* @param[in] val Value to set for the GPIO.
|
135 |
*
|
136 |
* @return The status indicates whether the function call was successful.
|
137 |
*/
|
138 |
static inline apalExitStatus_t apalGpioWrite(apalGpio_t* gpio, const apalGpioState_t val) |
139 |
{ |
140 |
apalDbgAssert(gpio != NULL);
|
141 |
|
142 |
// palWriteLine() is not guaranteed to be atomic, thus the scheduler is locked.
|
143 |
syssts_t sysstatus = chSysGetStatusAndLockX(); |
144 |
palWriteLine(gpio->line, (val == APAL_GPIO_HIGH) ? PAL_HIGH : PAL_LOW); |
145 |
chSysRestoreStatusX(sysstatus); |
146 |
return APAL_STATUS_OK;
|
147 |
} |
148 |
|
149 |
/**
|
150 |
* @brief Toggle the output of a GPIO.
|
151 |
*
|
152 |
* @param[in] gpio GPIO to toggle.
|
153 |
*
|
154 |
* @return The status indicates whether the function call was successful.
|
155 |
*/
|
156 |
static inline apalExitStatus_t apalGpioToggle(apalGpio_t* gpio) |
157 |
{ |
158 |
apalDbgAssert(gpio != NULL);
|
159 |
|
160 |
// palWriteLine() is not guaranteed to be atomic, thus the scheduler is locked.
|
161 |
syssts_t sysstatus = chSysGetStatusAndLockX(); |
162 |
palWriteLine(gpio->line, (palReadLine(gpio->line) == PAL_HIGH) ? PAL_LOW : PAL_HIGH); |
163 |
chSysRestoreStatusX(sysstatus); |
164 |
return APAL_STATUS_OK;
|
165 |
} |
166 |
|
167 |
/**
|
168 |
* @brief Return the interrupt enable status of the GPIO.
|
169 |
*
|
170 |
* @param[in] gpio GPIO to check.
|
171 |
* @param[out] enabled Flag, indicating whether interrupt is enabled for the GPIO.
|
172 |
*
|
173 |
* @return The status indicates whether the function call was successful.
|
174 |
*/
|
175 |
static inline apalExitStatus_t apalGpioIsInterruptEnabled(apalGpio_t* gpio, bool* const enabled) |
176 |
{ |
177 |
apalDbgAssert(gpio != NULL);
|
178 |
apalDbgAssert(enabled != NULL);
|
179 |
|
180 |
*enabled = palIsLineEventEnabledX(gpio->line); |
181 |
return APAL_STATUS_OK;
|
182 |
} |
183 |
|
184 |
/**
|
185 |
* @brief Get the current on/off state of a control GPIO.
|
186 |
*
|
187 |
* @param[in] gpio Control GPIO to read.
|
188 |
* @param[out] val Current activation status of the control GPIO.
|
189 |
*
|
190 |
* @return The status indicates whether the function call was successful.
|
191 |
*/
|
192 |
static inline apalExitStatus_t apalControlGpioGet(const apalControlGpio_t* const cgpio, apalControlGpioState_t* const val) |
193 |
{ |
194 |
apalDbgAssert(cgpio != NULL);
|
195 |
apalDbgAssert(cgpio->gpio != NULL);
|
196 |
apalDbgAssert(val != NULL);
|
197 |
|
198 |
*val = ((palReadLine(cgpio->gpio->line) == PAL_HIGH) ^ (cgpio->meta.active == APAL_GPIO_ACTIVE_HIGH)) ? APAL_GPIO_OFF : APAL_GPIO_ON; |
199 |
return APAL_STATUS_OK;
|
200 |
} |
201 |
|
202 |
/**
|
203 |
* @brief Turn a control GPIO 'on' or 'off' respectively.
|
204 |
*
|
205 |
* @param[in] gpio Control GPIO to set.
|
206 |
* @param[in] val Activation value to set for the control GPIO.
|
207 |
*
|
208 |
* @return The status indicates whether the function call was successful.
|
209 |
*/
|
210 |
static inline apalExitStatus_t apalControlGpioSet(const apalControlGpio_t* const cgpio, const apalControlGpioState_t val) |
211 |
{ |
212 |
apalDbgAssert(cgpio != NULL);
|
213 |
apalDbgAssert(cgpio->gpio != NULL);
|
214 |
apalDbgAssert(cgpio->meta.direction == APAL_GPIO_DIRECTION_OUTPUT || cgpio->meta.direction == APAL_GPIO_DIRECTION_BIDIRECTIONAL); |
215 |
|
216 |
// palWriteLine() is not guaranteed to be atomic, thus the scheduler is locked.
|
217 |
syssts_t sysstatus = chSysGetStatusAndLockX(); |
218 |
palWriteLine(cgpio->gpio->line, ((cgpio->meta.active == APAL_GPIO_ACTIVE_HIGH) ^ (val == APAL_GPIO_ON)) ? PAL_LOW : PAL_HIGH); |
219 |
chSysRestoreStatusX(sysstatus); |
220 |
return APAL_STATUS_OK;
|
221 |
} |
222 |
|
223 |
/**
|
224 |
* @brief Converts an apalGpioEdge_t to an ChibiOS PAL edge.
|
225 |
*/
|
226 |
#define APAL2CH_EDGE(edge) \
|
227 |
((edge == APAL_GPIO_EDGE_RISING) ? PAL_EVENT_MODE_RISING_EDGE : \ |
228 |
(edge == APAL_GPIO_EDGE_FALLING) ? PAL_EVENT_MODE_FALLING_EDGE : \ |
229 |
(edge == APAL_GPIO_EDGE_BOTH) ? PAL_EVENT_MODE_BOTH_EDGES : \ |
230 |
PAL_EVENT_MODE_DISABLED) |
231 |
|
232 |
/**
|
233 |
* @brief Enable or disable the interrupt event functionality.
|
234 |
*
|
235 |
* @param[in] cgpio Control GPIO to set.
|
236 |
* @param[in] enable Flag, indicating whether the interrupt shall be activated (true) or deactivated (false).
|
237 |
*
|
238 |
* @return The status indicates whether the function call was successful.
|
239 |
*/
|
240 |
static inline apalExitStatus_t apalControlGpioSetInterrupt(const apalControlGpio_t* const cgpio, const bool enable) |
241 |
{ |
242 |
apalDbgAssert(cgpio != NULL);
|
243 |
apalDbgAssert(cgpio->gpio != NULL);
|
244 |
|
245 |
if (enable) {
|
246 |
apalDbgAssert(pal_lld_get_line_event(cgpio->gpio->line) != NULL);
|
247 |
palEnableLineEvent(cgpio->gpio->line, APAL2CH_EDGE(cgpio->meta.edge)); |
248 |
} else {
|
249 |
palDisableLineEvent(cgpio->gpio->line); |
250 |
} |
251 |
|
252 |
return APAL_STATUS_OK;
|
253 |
} |
254 |
|
255 |
#endif /* (HAL_USE_PAL == TRUE) */ |
256 |
|
257 |
/*============================================================================*/
|
258 |
/* PWM */
|
259 |
/*============================================================================*/
|
260 |
|
261 |
#if (HAL_USE_PWM == TRUE) || defined (__DOXYGEN__)
|
262 |
|
263 |
/**
|
264 |
* @brief PWM driver type.
|
265 |
*/
|
266 |
typedef PWMDriver apalPWMDriver_t;
|
267 |
|
268 |
/**
|
269 |
* @brief Set the PWM with given parameters.
|
270 |
*
|
271 |
* @param[in] pwm PWM driver to set.
|
272 |
* @param[in] channel Channel of the PWM driver to set.
|
273 |
* @param[in] width Width to set the channel to.
|
274 |
*
|
275 |
* @return The status indicates whether the function call was successful.
|
276 |
*/
|
277 |
static inline apalExitStatus_t apalPWMSet(apalPWMDriver_t* pwm, const apalPWMchannel_t channel, const apalPWMwidth_t width) |
278 |
{ |
279 |
apalDbgAssert(pwm != NULL);
|
280 |
|
281 |
pwmEnableChannel(pwm, (pwmchannel_t)channel, pwm->period * ((float)width / (float)APAL_PWM_WIDTH_MAX) + 0.5f); |
282 |
return APAL_STATUS_OK;
|
283 |
} |
284 |
|
285 |
/**
|
286 |
* @brief Retrieve the current frequency of the PWM.
|
287 |
*
|
288 |
* @param[in] pwm PWM driver to read.
|
289 |
* @param[out] frequency The currently set frequency.
|
290 |
*
|
291 |
* @return The status indicates whether the function call was successful.
|
292 |
*/
|
293 |
static inline apalExitStatus_t apalPWMGetFrequency(apalPWMDriver_t* pwm, apalPWMfrequency_t* const frequency) |
294 |
{ |
295 |
apalDbgAssert(pwm != NULL);
|
296 |
apalDbgAssert(frequency != NULL);
|
297 |
|
298 |
*frequency = pwm->config->frequency; |
299 |
return APAL_STATUS_OK;
|
300 |
} |
301 |
|
302 |
/**
|
303 |
* @brief Retrieve the current period of the PWM.
|
304 |
*
|
305 |
* @param[in] pwm PWM driver to read.
|
306 |
* @param[out] period The currently set period.
|
307 |
*
|
308 |
* @return The status indicates whether the function call was successful.
|
309 |
*/
|
310 |
static inline apalExitStatus_t apalPWMGetPeriod(apalPWMDriver_t* pwm, apalPWMperiod_t* const period) |
311 |
{ |
312 |
apalDbgAssert(pwm != NULL);
|
313 |
apalDbgAssert(period != NULL);
|
314 |
|
315 |
*period = pwm->period; |
316 |
return APAL_STATUS_OK;
|
317 |
} |
318 |
|
319 |
#endif /* (HAL_USE_PWM == TRUE) */ |
320 |
|
321 |
/*============================================================================*/
|
322 |
/* QEI */
|
323 |
/*============================================================================*/
|
324 |
|
325 |
#if (HAL_USE_QEI == TRUE) || defined (__DOXYGEN__)
|
326 |
|
327 |
/**
|
328 |
* @brief QEI driver type.
|
329 |
*/
|
330 |
typedef QEIDriver apalQEIDriver_t;
|
331 |
|
332 |
/**
|
333 |
* @brief Gets the direction of the last transition.
|
334 |
*
|
335 |
* @param[in] qei The QEI driver to use.
|
336 |
* @param[out] direction The direction of the last transition.
|
337 |
*
|
338 |
* @return The status indicates whether the function call was successful.
|
339 |
*/
|
340 |
static inline apalExitStatus_t apalQEIGetDirection(apalQEIDriver_t* qei, apalQEIDirection_t* const direction) |
341 |
{ |
342 |
apalDbgAssert(qei != NULL);
|
343 |
apalDbgAssert(direction != NULL);
|
344 |
|
345 |
*direction = (qei_lld_get_direction(qei)) ? APAL_QEI_DIRECTION_DOWN : APAL_QEI_DIRECTION_UP; |
346 |
|
347 |
return APAL_STATUS_OK;
|
348 |
} |
349 |
|
350 |
/**
|
351 |
* @brief Gets the current position of the ecnoder.
|
352 |
*
|
353 |
* @param[in] qei The QEI driver to use.
|
354 |
* @param[out] position The current position of the encoder.
|
355 |
*
|
356 |
* @return The status indicates whether the function call was successful.
|
357 |
*/
|
358 |
static inline apalExitStatus_t apalQEIGetPosition(apalQEIDriver_t* qei, apalQEICount_t* const position) |
359 |
{ |
360 |
apalDbgAssert(qei != NULL);
|
361 |
apalDbgAssert(position != NULL);
|
362 |
|
363 |
*position = qei_lld_get_position(qei); |
364 |
|
365 |
return APAL_STATUS_OK;
|
366 |
} |
367 |
|
368 |
/**
|
369 |
* @brief Gets the value range of the encoder.
|
370 |
*
|
371 |
* @param[in] qei The QEI driver to use.
|
372 |
* @param[out] range The value range of the encoder.
|
373 |
*
|
374 |
* @return The status indicates whether the function call was successful.
|
375 |
*/
|
376 |
static inline apalExitStatus_t apalQEIGetRange(apalQEIDriver_t* qei, apalQEICount_t* const range) |
377 |
{ |
378 |
apalDbgAssert(qei != NULL);
|
379 |
apalDbgAssert(range != NULL);
|
380 |
|
381 |
*range = qei_lld_get_range(qei); |
382 |
|
383 |
return APAL_STATUS_OK;
|
384 |
} |
385 |
|
386 |
#endif /* (HAL_USE_QEI == TRUE) */ |
387 |
|
388 |
/*============================================================================*/
|
389 |
/* I2C */
|
390 |
/*============================================================================*/
|
391 |
|
392 |
#if (HAL_USE_I2C == TRUE) || defined(__DOXYGEN__)
|
393 |
|
394 |
/**
|
395 |
* @brief I2C driver type.
|
396 |
*/
|
397 |
typedef I2CDriver apalI2CDriver_t;
|
398 |
|
399 |
/**
|
400 |
* @brief Transmit data and receive a response.
|
401 |
*
|
402 |
* @param[in] i2cd The I2C driver to use.
|
403 |
* @param[in] addr Address to write to.
|
404 |
* @param[in] txbuf Buffer containing data to send.
|
405 |
* @param[in] txbytes Number of bytes to send.
|
406 |
* @param[out] rxbuf Buffer to store a response to.
|
407 |
* @param[in] rxbytes Number of bytes to receive.
|
408 |
* @param[in] timeout Timeout for the function to return (in microseconds).
|
409 |
*
|
410 |
* @return The status indicates whether the function call was succesful or a timeout occurred.
|
411 |
*/
|
412 |
static inline apalExitStatus_t apalI2CMasterTransmit(apalI2CDriver_t* i2cd, const apalI2Caddr_t addr, const uint8_t* const txbuf, const size_t txbytes, uint8_t* const rxbuf, const size_t rxbytes, const apalTime_t timeout) |
413 |
{ |
414 |
apalDbgAssert(i2cd != NULL);
|
415 |
|
416 |
#if (I2C_USE_MUTUAL_EXCLUSION == TRUE)
|
417 |
// check whether the I2C driver was locked externally
|
418 |
const bool i2cd_locked_external = i2cd->mutex.owner == currp; |
419 |
if (!i2cd_locked_external) {
|
420 |
i2cAcquireBus(i2cd); |
421 |
} |
422 |
#endif /* (I2C_USE_MUTUAL_EXCLUSION == TRUE) */ |
423 |
|
424 |
#pragma GCC diagnostic push
|
425 |
#pragma GCC diagnostic ignored "-Wtype-limits" |
426 |
#if defined(STM32F1XX_I2C)
|
427 |
// Due to a hardware limitation, for STM32F1 platform the minimum number of bytes that can be received is two.
|
428 |
msg_t status = MSG_OK; |
429 |
if (rxbytes == 1) { |
430 |
uint8_t buffer[2];
|
431 |
status = i2cMasterTransmitTimeout(i2cd, addr, txbuf, txbytes, buffer, 2, ((timeout >= TIME_INFINITE) ? TIME_INFINITE : TIME_US2I(timeout)) );
|
432 |
rxbuf[0] = buffer[0]; |
433 |
} else {
|
434 |
status = i2cMasterTransmitTimeout(i2cd, addr, txbuf, txbytes, rxbuf, rxbytes, ((timeout >= TIME_INFINITE) ? TIME_INFINITE : TIME_US2I(timeout)) ); |
435 |
} |
436 |
#else /* defined(STM32F1XX_I2C) */ |
437 |
const msg_t status = i2cMasterTransmitTimeout(i2cd, addr, txbuf, txbytes, rxbuf, rxbytes, ((timeout >= TIME_INFINITE) ? TIME_INFINITE : TIME_US2I(timeout)) );
|
438 |
#endif /* defined(STM32F1XX_I2C) */ |
439 |
#pragma GCC diagnostic pop
|
440 |
|
441 |
#if (I2C_USE_MUTUAL_EXCLUSION == TRUE)
|
442 |
if (!i2cd_locked_external) {
|
443 |
i2cReleaseBus(i2cd); |
444 |
} |
445 |
#endif /* (I2C_USE_MUTUAL_EXCLUSION == TRUE) */ |
446 |
|
447 |
switch (status)
|
448 |
{ |
449 |
case MSG_OK:
|
450 |
#if defined(STM32F1XX_I2C)
|
451 |
return (rxbytes != 1) ? APAL_STATUS_OK : APAL_STATUS_WARNING; |
452 |
#else /* defined(STM32F1XX_I2C) */ |
453 |
return APAL_STATUS_OK;
|
454 |
#endif /* defined(STM32F1XX_I2C) */ |
455 |
case MSG_TIMEOUT:
|
456 |
return APAL_STATUS_TIMEOUT;
|
457 |
case MSG_RESET:
|
458 |
default:
|
459 |
return APAL_STATUS_ERROR;
|
460 |
} |
461 |
} |
462 |
|
463 |
/**
|
464 |
* @brief Read data from a specific address.
|
465 |
*
|
466 |
* @param[in] i2cd The I2C driver to use.
|
467 |
* @param[in] addr Address to read.
|
468 |
* @param[out] rxbuf Buffer to store the response to.
|
469 |
* @param[in] rxbytes Number of bytes to receive.
|
470 |
* @param[in] timeout Timeout for the function to return (in microseconds).
|
471 |
*
|
472 |
* @return The status indicates whether the function call was succesful or a timeout occurred.
|
473 |
*/
|
474 |
static inline apalExitStatus_t apalI2CMasterReceive(apalI2CDriver_t* i2cd, const apalI2Caddr_t addr, uint8_t* const rxbuf, const size_t rxbytes, const apalTime_t timeout) |
475 |
{ |
476 |
apalDbgAssert(i2cd != NULL);
|
477 |
|
478 |
#if (I2C_USE_MUTUAL_EXCLUSION == TRUE)
|
479 |
// check whether the I2C driver was locked externally
|
480 |
const bool i2cd_locked_external = i2cd->mutex.owner == currp; |
481 |
if (!i2cd_locked_external) {
|
482 |
i2cAcquireBus(i2cd); |
483 |
} |
484 |
#endif /* (I2C_USE_MUTUAL_EXCLUSION == TRUE) */ |
485 |
|
486 |
#pragma GCC diagnostic push
|
487 |
#pragma GCC diagnostic ignored "-Wtype-limits" |
488 |
#if defined(STM32F1XX_I2C)
|
489 |
// Due to a hardware limitation, for STM32F1 platform the minimum number of bytes that can be received is two.
|
490 |
msg_t status = MSG_OK; |
491 |
if (rxbytes == 1) { |
492 |
uint8_t buffer[2];
|
493 |
status = i2cMasterReceiveTimeout(i2cd, addr, buffer, 2, ((timeout >= TIME_INFINITE) ? TIME_INFINITE : TIME_US2I(timeout)) );
|
494 |
rxbuf[0] = buffer[0]; |
495 |
} else {
|
496 |
status = i2cMasterReceiveTimeout(i2cd, addr, rxbuf, rxbytes, ((timeout >= TIME_INFINITE) ? TIME_INFINITE : TIME_US2I(timeout)) ); |
497 |
} |
498 |
#else /* defined(STM32F1XX_I2C) */ |
499 |
const msg_t status = i2cMasterReceiveTimeout(i2cd, addr, rxbuf, rxbytes, ((timeout >= TIME_INFINITE) ? TIME_INFINITE : TIME_US2I(timeout)) );
|
500 |
#endif /* defined(STM32F1XX_I2C) */ |
501 |
#pragma GCC diagnostic pop
|
502 |
|
503 |
#if (I2C_USE_MUTUAL_EXCLUSION == TRUE)
|
504 |
if (!i2cd_locked_external) {
|
505 |
i2cReleaseBus(i2cd); |
506 |
} |
507 |
#endif /* (I2C_USE_MUTUAL_EXCLUSION == TRUE) */ |
508 |
|
509 |
switch (status)
|
510 |
{ |
511 |
case MSG_OK:
|
512 |
#if defined(STM32F1XX_I2C)
|
513 |
return (rxbytes != 1) ? APAL_STATUS_OK : APAL_STATUS_WARNING; |
514 |
#else /* defined(STM32F1XX_I2C) */ |
515 |
return APAL_STATUS_OK;
|
516 |
#endif /* defined(STM32F1XX_I2C) */ |
517 |
case MSG_TIMEOUT:
|
518 |
return APAL_STATUS_TIMEOUT;
|
519 |
case MSG_RESET:
|
520 |
default:
|
521 |
return APAL_STATUS_ERROR;
|
522 |
} |
523 |
} |
524 |
|
525 |
#endif /* (HAL_USE_I2C == TRUE) */ |
526 |
|
527 |
/*============================================================================*/
|
528 |
/* SPI */
|
529 |
/*============================================================================*/
|
530 |
|
531 |
#if (HAL_USE_SPI == TRUE) || defined(__DOXYGEN__)
|
532 |
|
533 |
/**
|
534 |
* @brief SPI driver type.
|
535 |
*/
|
536 |
typedef SPIDriver apalSPIDriver_t;
|
537 |
|
538 |
/**
|
539 |
* @brief SPI confguration type.
|
540 |
*/
|
541 |
typedef SPIConfig apalSPIConfig_t;
|
542 |
|
543 |
/**
|
544 |
* @brief Reconfigure an SPI driver.
|
545 |
*
|
546 |
* @param[in] spid The SPI driver to be reconfigured.
|
547 |
* @param[in] config Configuration to apply.
|
548 |
*
|
549 |
* @return The status indicates whether the function call was succesful.
|
550 |
*/
|
551 |
static inline apalExitStatus_t apalSPIReconfigure(apalSPIDriver_t* spid, const apalSPIConfig_t* config) |
552 |
{ |
553 |
apalDbgAssert(spid != NULL);
|
554 |
apalDbgAssert(config != NULL);
|
555 |
|
556 |
#if (SPI_USE_MUTUAL_EXCLUSION == TRUE)
|
557 |
// check whether the SPI driver was locked externally
|
558 |
const bool spid_locked_external = spid->mutex.owner == currp; |
559 |
if (!spid_locked_external) {
|
560 |
spiAcquireBus(spid); |
561 |
} |
562 |
#endif /* (SPI_USE_MUTUAL_EXCLUSION == TRUE) */ |
563 |
|
564 |
spiStop(spid); |
565 |
spiStart(spid, config); |
566 |
|
567 |
#if (SPI_USE_MUTUAL_EXCLUSION == TRUE)
|
568 |
if (!spid_locked_external) {
|
569 |
spiReleaseBus(spid); |
570 |
} |
571 |
#endif /* (SPI_USE_MUTUAL_EXCLUSION == TRUE) */ |
572 |
|
573 |
return APAL_STATUS_OK;
|
574 |
} |
575 |
|
576 |
/**
|
577 |
* @brief Transmit and receive data from SPI
|
578 |
*
|
579 |
* @param[in] spid The SPI driver to use.
|
580 |
* @param[in] txData Buffer containing data to send.
|
581 |
* @param[out] rxData Buffer to store.
|
582 |
* @param[in] length Number of bytes to send.
|
583 |
*
|
584 |
* @return The status indicates whether the function call was succesful.
|
585 |
*/
|
586 |
static inline apalExitStatus_t apalSPIExchange(apalSPIDriver_t* spid, const uint8_t* const txData , uint8_t* const rxData, const size_t length) |
587 |
{ |
588 |
apalDbgAssert(spid != NULL);
|
589 |
|
590 |
#if (SPI_USE_MUTUAL_EXCLUSION == TRUE)
|
591 |
// check whether the SPI driver was locked externally
|
592 |
const bool spid_locked_external = spid->mutex.owner == currp; |
593 |
if (!spid_locked_external) {
|
594 |
spiAcquireBus(spid); |
595 |
} |
596 |
#endif /* (SPI_USE_MUTUAL_EXCLUSION == TRUE) */ |
597 |
|
598 |
spiSelect(spid); |
599 |
spiExchange(spid, length, txData, rxData); |
600 |
spiUnselect(spid); |
601 |
|
602 |
#if (SPI_USE_MUTUAL_EXCLUSION == TRUE)
|
603 |
if (!spid_locked_external) {
|
604 |
spiReleaseBus(spid); |
605 |
} |
606 |
#endif /* (SPI_USE_MUTUAL_EXCLUSION == TRUE) */ |
607 |
|
608 |
return APAL_STATUS_OK;
|
609 |
} |
610 |
|
611 |
/**
|
612 |
* @brief Receive data from SPI
|
613 |
*
|
614 |
* @param[in] spid The SPI driver to use.
|
615 |
* @param[out] data Buffer to store.
|
616 |
* @param[in] length Number of bytes to send.
|
617 |
*
|
618 |
* @return The status indicates whether the function call was succesful.
|
619 |
*/
|
620 |
static inline apalExitStatus_t apalSPIReceive(apalSPIDriver_t* spid, uint8_t* const data, const size_t length) |
621 |
{ |
622 |
apalDbgAssert(spid != NULL);
|
623 |
|
624 |
#if (SPI_USE_MUTUAL_EXCLUSION == TRUE)
|
625 |
// check whether the SPI driver was locked externally
|
626 |
const bool spid_locked_external = spid->mutex.owner == currp; |
627 |
if (!spid_locked_external) {
|
628 |
spiAcquireBus(spid); |
629 |
} |
630 |
#endif /* (SPI_USE_MUTUAL_EXCLUSION == TRUE) */ |
631 |
|
632 |
spiSelect(spid); |
633 |
spiReceive(spid, length, data); |
634 |
spiUnselect(spid); |
635 |
|
636 |
#if (SPI_USE_MUTUAL_EXCLUSION == TRUE)
|
637 |
if (!spid_locked_external) {
|
638 |
spiReleaseBus(spid); |
639 |
} |
640 |
#endif /* (SPI_USE_MUTUAL_EXCLUSION == TRUE) */ |
641 |
|
642 |
return APAL_STATUS_OK;
|
643 |
} |
644 |
|
645 |
/**
|
646 |
* @brief Transmit data to SPI
|
647 |
*
|
648 |
* @param[in] spid The SPI driver to use.
|
649 |
* @param[in] data Buffer containing data to send.
|
650 |
* @param[in] length Number of bytes to send.
|
651 |
*
|
652 |
* @return The status indicates whether the function call was succesful.
|
653 |
*/
|
654 |
static inline apalExitStatus_t apalSPITransmit(apalSPIDriver_t* spid, const uint8_t* const data, const size_t length) |
655 |
{ |
656 |
apalDbgAssert(spid != NULL);
|
657 |
|
658 |
#if (SPI_USE_MUTUAL_EXCLUSION == TRUE)
|
659 |
// check whether the SPI driver was locked externally
|
660 |
const bool spid_locked_external = spid->mutex.owner == currp; |
661 |
if (!spid_locked_external) {
|
662 |
spiAcquireBus(spid); |
663 |
} |
664 |
#endif /* (SPI_USE_MUTUAL_EXCLUSION == TRUE) */ |
665 |
|
666 |
spiSelect(spid); |
667 |
spiSend(spid, length, data); |
668 |
spiUnselect(spid); |
669 |
|
670 |
#if (SPI_USE_MUTUAL_EXCLUSION == TRUE)
|
671 |
if (!spid_locked_external) {
|
672 |
spiReleaseBus(spid); |
673 |
} |
674 |
#endif /* (SPI_USE_MUTUAL_EXCLUSION == TRUE) */ |
675 |
|
676 |
return APAL_STATUS_OK;
|
677 |
} |
678 |
|
679 |
/**
|
680 |
* @brief Transmit data to SPI and receive data afterwards without releasing the bus in between.
|
681 |
*
|
682 |
* @param spid The SPI driver to use.
|
683 |
* @param txData Transmit data buffer.
|
684 |
* @param rxData Receive data buffer.
|
685 |
* @param txLength Number of bytes to send.
|
686 |
* @param rxLength Number of bytes to receive.
|
687 |
*
|
688 |
* @return The status indicates whether the function call was succesful.
|
689 |
*/
|
690 |
static inline apalExitStatus_t apalSPITransmitAndReceive(apalSPIDriver_t* spid, const uint8_t* const txData , uint8_t* const rxData, const size_t txLength, const size_t rxLength) |
691 |
{ |
692 |
apalDbgAssert(spid != NULL);
|
693 |
|
694 |
#if (SPI_USE_MUTUAL_EXCLUSION == TRUE)
|
695 |
// check whether the SPI driver was locked externally
|
696 |
const bool spid_locked_external = spid->mutex.owner == currp; |
697 |
if (!spid_locked_external) {
|
698 |
spiAcquireBus(spid); |
699 |
} |
700 |
#endif /* (SPI_USE_MUTUAL_EXCLUSION == TRUE) */ |
701 |
|
702 |
spiSelect(spid); |
703 |
spiSend(spid, txLength, txData); |
704 |
spiReceive(spid, rxLength, rxData); |
705 |
spiUnselect(spid); |
706 |
|
707 |
#if (SPI_USE_MUTUAL_EXCLUSION == TRUE)
|
708 |
if (!spid_locked_external) {
|
709 |
spiReleaseBus(spid); |
710 |
} |
711 |
#endif /* (SPI_USE_MUTUAL_EXCLUSION == TRUE) */ |
712 |
|
713 |
return APAL_STATUS_OK;
|
714 |
} |
715 |
|
716 |
#endif /* (HAL_USE_SPI == TRUE) */ |
717 |
|
718 |
#endif /* AMIROOS_PERIPHAL_H */ |