Revision 1f94ac64 periphery-lld/periphAL.h
| periphery-lld/periphAL.h | ||
|---|---|---|
| 41 | 41 |
/* DEPENDENCIES */ |
| 42 | 42 |
/*============================================================================*/ |
| 43 | 43 |
|
| 44 |
#include <aosconf.h> |
|
| 44 | 45 |
#include <hal.h> |
| 45 | 46 |
|
| 46 | 47 |
/*============================================================================*/ |
| 48 |
/* DEBUG */ |
|
| 49 |
/*============================================================================*/ |
|
| 50 |
|
|
| 51 |
#if (AMIROOS_CFG_DBG == true) || defined(__DOXYGEN__) |
|
| 52 |
|
|
| 53 |
#ifdef __cplusplus |
|
| 54 |
extern "C" {
|
|
| 55 |
#endif |
|
| 56 |
void _apalDbgAssertMsg(const bool c, const char* fmt, ...); |
|
| 57 |
void apalDbgPrintf(const char* fmt, ...); |
|
| 58 |
#ifdef __cplusplus |
|
| 59 |
} |
|
| 60 |
#endif |
|
| 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) \ |
|
| 68 |
_apalDbgAssertMsg(c, "%s(%u): apalDbgAssert failed", __FILE__, __LINE__); |
|
| 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 */ |
|
| 77 |
|
|
| 78 |
/*============================================================================*/ |
|
| 47 | 79 |
/* GENERAL */ |
| 48 | 80 |
/*============================================================================*/ |
| 49 | 81 |
|
| ... | ... | |
| 55 | 87 |
static inline void usleep(apalTime_t us) |
| 56 | 88 |
{
|
| 57 | 89 |
// check if the specified time can be represented by the system |
| 58 |
chDbgCheck(us <= chTimeI2US(TIME_INFINITE));
|
|
| 90 |
apalDbgAssert(us <= chTimeI2US(TIME_INFINITE));
|
|
| 59 | 91 |
|
| 60 | 92 |
const sysinterval_t interval = chTimeUS2I(us); |
| 61 | 93 |
// TIME_IMMEDIATE makes no sense and would even cause system halt |
| ... | ... | |
| 89 | 121 |
*/ |
| 90 | 122 |
static inline apalExitStatus_t apalGpioRead(apalGpio_t* gpio, apalGpioState_t* const val) |
| 91 | 123 |
{
|
| 92 |
chDbgCheck(gpio != NULL);
|
|
| 93 |
chDbgCheck(val != NULL);
|
|
| 124 |
apalDbgAssert(gpio != NULL);
|
|
| 125 |
apalDbgAssert(val != NULL);
|
|
| 94 | 126 |
|
| 95 | 127 |
*val = (palReadPad(gpio->port, gpio->pad) == PAL_HIGH) ? APAL_GPIO_HIGH : APAL_GPIO_LOW; |
| 96 | 128 |
return APAL_STATUS_OK; |
| ... | ... | |
| 106 | 138 |
*/ |
| 107 | 139 |
static inline apalExitStatus_t apalGpioWrite(apalGpio_t* gpio, const apalGpioState_t val) |
| 108 | 140 |
{
|
| 109 |
chDbgCheck(gpio != NULL);
|
|
| 141 |
apalDbgAssert(gpio != NULL);
|
|
| 110 | 142 |
|
| 111 | 143 |
// palWritePad() is not guaranteed to be atomic, thus the scheduler is locked. |
| 112 | 144 |
syssts_t sysstatus = chSysGetStatusAndLockX(); |
| ... | ... | |
| 124 | 156 |
*/ |
| 125 | 157 |
static inline apalExitStatus_t apalGpioToggle(apalGpio_t* gpio) |
| 126 | 158 |
{
|
| 127 |
chDbgCheck(gpio != NULL);
|
|
| 159 |
apalDbgAssert(gpio != NULL);
|
|
| 128 | 160 |
|
| 129 | 161 |
// palWritePad() is not guaranteed to be atomic, thus the scheduler is locked. |
| 130 | 162 |
syssts_t sysstatus = chSysGetStatusAndLockX(); |
| ... | ... | |
| 143 | 175 |
*/ |
| 144 | 176 |
static inline apalExitStatus_t apalControlGpioGet(const apalControlGpio_t* const cgpio, apalControlGpioState_t* const val) |
| 145 | 177 |
{
|
| 146 |
chDbgCheck(cgpio != NULL);
|
|
| 147 |
chDbgCheck(cgpio->gpio != NULL);
|
|
| 148 |
chDbgCheck(val != NULL);
|
|
| 178 |
apalDbgAssert(cgpio != NULL);
|
|
| 179 |
apalDbgAssert(cgpio->gpio != NULL);
|
|
| 180 |
apalDbgAssert(val != NULL);
|
|
| 149 | 181 |
|
| 150 | 182 |
*val = ((palReadPad(cgpio->gpio->port, cgpio->gpio->pad) == PAL_HIGH) ^ (cgpio->meta.active == APAL_GPIO_ACTIVE_HIGH)) ? APAL_GPIO_OFF : APAL_GPIO_ON; |
| 151 | 183 |
return APAL_STATUS_OK; |
| ... | ... | |
| 161 | 193 |
*/ |
| 162 | 194 |
static inline apalExitStatus_t apalControlGpioSet(const apalControlGpio_t* const cgpio, const apalControlGpioState_t val) |
| 163 | 195 |
{
|
| 164 |
chDbgCheck(cgpio != NULL);
|
|
| 165 |
chDbgCheck(cgpio->gpio != NULL);
|
|
| 166 |
chDbgCheck(cgpio->meta.direction == APAL_GPIO_DIRECTION_OUTPUT || cgpio->meta.direction == APAL_GPIO_DIRECTION_BIDIRECTIONAL);
|
|
| 196 |
apalDbgAssert(cgpio != NULL);
|
|
| 197 |
apalDbgAssert(cgpio->gpio != NULL);
|
|
| 198 |
apalDbgAssert(cgpio->meta.direction == APAL_GPIO_DIRECTION_OUTPUT || cgpio->meta.direction == APAL_GPIO_DIRECTION_BIDIRECTIONAL);
|
|
| 167 | 199 |
|
| 168 | 200 |
// palWritePad() is not guaranteed to be atomic, thus the scheduler is locked. |
| 169 | 201 |
syssts_t sysstatus = chSysGetStatusAndLockX(); |
| ... | ... | |
| 180 | 212 |
(edge == APAL_GPIO_EDGE_FALLING) ? PAL_EVENT_MODE_FALLING_EDGE : \ |
| 181 | 213 |
(edge == APAL_GPIO_EDGE_BOTH) ? PAL_EVENT_MODE_BOTH_EDGES : 0) |
| 182 | 214 |
|
| 183 |
#endif |
|
| 215 |
#endif /* HAL_USE_PAL */
|
|
| 184 | 216 |
|
| 185 | 217 |
/*============================================================================*/ |
| 186 | 218 |
/* PWM */ |
| ... | ... | |
| 204 | 236 |
*/ |
| 205 | 237 |
static inline apalExitStatus_t apalPWMSet(apalPWMDriver_t* pwm, const apalPWMchannel_t channel, const apalPWMwidth_t width) |
| 206 | 238 |
{
|
| 207 |
chDbgCheck(pwm != NULL);
|
|
| 239 |
apalDbgAssert(pwm != NULL);
|
|
| 208 | 240 |
|
| 209 | 241 |
pwmEnableChannel(pwm, (pwmchannel_t)channel, pwm->period * ((float)width / (float)APAL_PWM_WIDTH_MAX) + 0.5f); |
| 210 | 242 |
return APAL_STATUS_OK; |
| ... | ... | |
| 220 | 252 |
*/ |
| 221 | 253 |
static inline apalExitStatus_t apalPWMGetFrequency(apalPWMDriver_t* pwm, apalPWMfrequency_t* const frequency) |
| 222 | 254 |
{
|
| 223 |
chDbgCheck(pwm != NULL);
|
|
| 224 |
chDbgCheck(frequency != NULL);
|
|
| 255 |
apalDbgAssert(pwm != NULL);
|
|
| 256 |
apalDbgAssert(frequency != NULL);
|
|
| 225 | 257 |
|
| 226 | 258 |
*frequency = pwm->config->frequency; |
| 227 | 259 |
return APAL_STATUS_OK; |
| ... | ... | |
| 237 | 269 |
*/ |
| 238 | 270 |
static inline apalExitStatus_t apalPWMGetPeriod(apalPWMDriver_t* pwm, apalPWMperiod_t* const period) |
| 239 | 271 |
{
|
| 240 |
chDbgCheck(pwm != NULL);
|
|
| 241 |
chDbgCheck(period != NULL);
|
|
| 272 |
apalDbgAssert(pwm != NULL);
|
|
| 273 |
apalDbgAssert(period != NULL);
|
|
| 242 | 274 |
|
| 243 | 275 |
*period = pwm->period; |
| 244 | 276 |
return APAL_STATUS_OK; |
| 245 | 277 |
} |
| 246 | 278 |
|
| 247 |
#endif |
|
| 279 |
#endif /* HAL_USE_PWM */
|
|
| 248 | 280 |
|
| 249 | 281 |
/*============================================================================*/ |
| 250 | 282 |
/* QEI */ |
| ... | ... | |
| 267 | 299 |
*/ |
| 268 | 300 |
static inline apalExitStatus_t apalQEIGetDirection(apalQEIDriver_t* qei, apalQEIDirection_t* const direction) |
| 269 | 301 |
{
|
| 270 |
chDbgCheck(qei != NULL);
|
|
| 271 |
chDbgCheck(direction != NULL);
|
|
| 302 |
apalDbgAssert(qei != NULL);
|
|
| 303 |
apalDbgAssert(direction != NULL);
|
|
| 272 | 304 |
|
| 273 | 305 |
*direction = (qei_lld_get_direction(qei)) ? APAL_QEI_DIRECTION_DOWN : APAL_QEI_DIRECTION_UP; |
| 274 | 306 |
|
| ... | ... | |
| 285 | 317 |
*/ |
| 286 | 318 |
static inline apalExitStatus_t apalQEIGetPosition(apalQEIDriver_t* qei, apalQEICount_t* const position) |
| 287 | 319 |
{
|
| 288 |
chDbgCheck(qei != NULL);
|
|
| 289 |
chDbgCheck(position != NULL);
|
|
| 320 |
apalDbgAssert(qei != NULL);
|
|
| 321 |
apalDbgAssert(position != NULL);
|
|
| 290 | 322 |
|
| 291 | 323 |
*position = qei_lld_get_position(qei); |
| 292 | 324 |
|
| ... | ... | |
| 303 | 335 |
*/ |
| 304 | 336 |
static inline apalExitStatus_t apalQEIGetRange(apalQEIDriver_t* qei, apalQEICount_t* const range) |
| 305 | 337 |
{
|
| 306 |
chDbgCheck(qei != NULL);
|
|
| 307 |
chDbgCheck(range != NULL);
|
|
| 338 |
apalDbgAssert(qei != NULL);
|
|
| 339 |
apalDbgAssert(range != NULL);
|
|
| 308 | 340 |
|
| 309 | 341 |
*range = qei_lld_get_range(qei); |
| 310 | 342 |
|
| 311 | 343 |
return APAL_STATUS_OK; |
| 312 | 344 |
} |
| 313 | 345 |
|
| 314 |
#endif |
|
| 346 |
#endif /* HAL_USE_QEI */
|
|
| 315 | 347 |
|
| 316 | 348 |
/*============================================================================*/ |
| 317 | 349 |
/* I2C */ |
| ... | ... | |
| 339 | 371 |
*/ |
| 340 | 372 |
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) |
| 341 | 373 |
{
|
| 342 |
chDbgCheck(i2cd != NULL);
|
|
| 374 |
apalDbgAssert(i2cd != NULL);
|
|
| 343 | 375 |
|
| 344 | 376 |
#if (I2C_USE_MUTUAL_EXCLUSION == TRUE) |
| 345 | 377 |
// check whether the I2C driver was locked externally |
| ... | ... | |
| 401 | 433 |
*/ |
| 402 | 434 |
static inline apalExitStatus_t apalI2CMasterReceive(apalI2CDriver_t* i2cd, const apalI2Caddr_t addr, uint8_t* const rxbuf, const size_t rxbytes, const apalTime_t timeout) |
| 403 | 435 |
{
|
| 404 |
chDbgCheck(i2cd != NULL);
|
|
| 436 |
apalDbgAssert(i2cd != NULL);
|
|
| 405 | 437 |
|
| 406 | 438 |
#if (I2C_USE_MUTUAL_EXCLUSION == TRUE) |
| 407 | 439 |
// check whether the I2C driver was locked externally |
| ... | ... | |
| 450 | 482 |
} |
| 451 | 483 |
} |
| 452 | 484 |
|
| 453 |
#endif |
|
| 485 |
#endif /* HAL_USE_I2C */
|
|
| 454 | 486 |
|
| 455 | 487 |
/*============================================================================*/ |
| 456 | 488 |
/* SPI */ |
| ... | ... | |
| 475 | 507 |
*/ |
| 476 | 508 |
static inline apalExitStatus_t apalSPIExchange(apalSPIDriver_t* spid, const uint8_t* const txData , uint8_t* const rxData, const size_t length) |
| 477 | 509 |
{
|
| 478 |
chDbgCheck(spid != NULL);
|
|
| 510 |
apalDbgAssert(spid != NULL);
|
|
| 479 | 511 |
|
| 480 | 512 |
#if (SPI_USE_MUTUAL_EXCLUSION) |
| 481 | 513 |
// check whether the SPI driver was locked externally |
| ... | ... | |
| 509 | 541 |
*/ |
| 510 | 542 |
static inline apalExitStatus_t apalSPIReceive(apalSPIDriver_t* spid, uint8_t* const data, const size_t length) |
| 511 | 543 |
{
|
| 512 |
chDbgCheck(spid != NULL);
|
|
| 544 |
apalDbgAssert(spid != NULL);
|
|
| 513 | 545 |
|
| 514 | 546 |
#if (SPI_USE_MUTUAL_EXCLUSION) |
| 515 | 547 |
// check whether the SPI driver was locked externally |
| ... | ... | |
| 543 | 575 |
*/ |
| 544 | 576 |
static inline apalExitStatus_t apalSPITransmit(apalSPIDriver_t* spid, const uint8_t* const data, const size_t length) |
| 545 | 577 |
{
|
| 546 |
chDbgCheck(spid != NULL);
|
|
| 578 |
apalDbgAssert(spid != NULL);
|
|
| 547 | 579 |
|
| 548 | 580 |
#if (SPI_USE_MUTUAL_EXCLUSION) |
| 549 | 581 |
// check whether the SPI driver was locked externally |
| ... | ... | |
| 579 | 611 |
*/ |
| 580 | 612 |
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) |
| 581 | 613 |
{
|
| 582 |
chDbgCheck(spid != NULL);
|
|
| 614 |
apalDbgAssert(spid != NULL);
|
|
| 583 | 615 |
|
| 584 | 616 |
#if (SPI_USE_MUTUAL_EXCLUSION) |
| 585 | 617 |
// check whether the SPI driver was locked externally |
| ... | ... | |
| 603 | 635 |
return APAL_STATUS_OK; |
| 604 | 636 |
} |
| 605 | 637 |
|
| 606 |
#endif |
|
| 607 |
|
|
| 608 |
/*============================================================================*/ |
|
| 609 |
/* DEBUG */ |
|
| 610 |
/*============================================================================*/ |
|
| 611 |
|
|
| 612 |
/** |
|
| 613 |
* @brief Assert function to check a given condition. |
|
| 614 |
* |
|
| 615 |
* @param[in] c The condition to check. |
|
| 616 |
*/ |
|
| 617 |
#define apalDbgAssert(c) chDbgAssert(c, "") |
|
| 618 |
|
|
| 619 |
|
|
| 620 |
/** |
|
| 621 |
* @brief Printf function for messages printed only in debug builds. |
|
| 622 |
* |
|
| 623 |
* @param[in] fmt Formatted string to print. |
|
| 624 |
*/ |
|
| 625 |
#if (AMIROOS_CFG_DBG == true) || defined(__DOXYGEN__) |
|
| 626 |
#define apalDbgPrintf(fmt, ...) chprintf((BaseSequentialStream*)&aos.iostream, fmt, ##__VA_ARGS__) |
|
| 627 |
#else |
|
| 628 |
#define apalDbgPrintf(fmt, ...) { \
|
|
| 629 |
(void)(fmt); \ |
|
| 630 |
} |
|
| 631 |
#endif |
|
| 638 |
#endif /* HAL_USE_SPI */ |
|
| 632 | 639 |
|
| 633 | 640 |
#endif /* AMIROOS_PERIPHAL_H */ |
Also available in: Unified diff