diff --git a/os/xhal/codegen/hal_spi.xml b/os/xhal/codegen/hal_spi.xml index d482fd43e..ad38f3878 100644 --- a/os/xhal/codegen/hal_spi.xml +++ b/os/xhal/codegen/hal_spi.xml @@ -9,19 +9,19 @@ - + @p spiSelect() and @p spiUnselect() do nothing. - + Selection by PAL port and pad number. - + Selection by port and port mask. - + Selection by PAL line identifier. - + Selection by LLD-defined mode. @@ -31,10 +31,10 @@ Support for thread synchronization API. - + Handling method for SPI CS line. - + @@ -44,8 +44,7 @@ Type of structure representing a SPI configuration - (legacy). - + (legacy). @@ -137,74 +136,58 @@ SPI_DRIVER_EXT_INIT_HOOK(self); - - - - This class implements a buffered channel interface on top of SPI. - - - Pointer to the associated @p hal_spi_driver_c instance. - - - - - Pointer to - the @p hal_spi_driver_c object. - Pointer to the input buffer. - Size of the input buffer. - Pointer to the output buffer. - Size of the output buffer. - spip = spip;]]> - - - - - - - + + + Asserts the slave select signal and prepares for + transfers. + + state = HAL_DRV_STATE_STARTING; -osalSysUnlock(); +spi_lld_select(self);]]> + + + Deasserts the slave select signal. + + spip); -if (msg == HAL_RET_SUCCESS) { - spiSetCallbackX(self->spip, &__bspi_default_cb); - spiWriteEnableFlagsX(self->spip, SPI_EV_ALL_EVENTS); -} +spi_lld_unselect(self);]]> + + + + - - - config->ssline);]]> + + + state = HAL_DRV_STATE_STOPPING; -osalSysUnlock(); +palSetLine(self->config->ssline);]]> + + + + spip); +palClearPort(self->config->ssport, (spip)->config->ssmask);]]> + + + - - - spip, config);]]> - - +palSetPort(self->config->ssport, (spip)->config->ssmask);]]> + + + + config->ssport, (spip)->config->sspad);]]> + + + config->ssport, (spip)->config->sspad);]]> + + + diff --git a/os/xhal/include/hal_spi.h b/os/xhal/include/hal_spi.h index 28b262e0f..5864fd9c8 100644 --- a/os/xhal/include/hal_spi.h +++ b/os/xhal/include/hal_spi.h @@ -268,95 +268,6 @@ struct hal_spi_driver { }; /** @} */ -/** - * @class hal_buffered_spi_c - * @extends base_object_c, hal_base_driver_c, hal_buffered_serial_c. - * - * @brief This class implements a buffered channel interface on top of - * SPI. - * - * @name Class @p hal_buffered_spi_c structures - * @{ - */ - -/** - * @brief Type of a buffered SPI wrapper class. - */ -typedef struct hal_buffered_spi hal_buffered_spi_c; - -/** - * @brief Class @p hal_buffered_spi_c virtual methods table. - */ -struct hal_buffered_spi_vmt { - /* From base_object_c.*/ - void (*dispose)(void *ip); - /* From hal_base_driver_c.*/ - msg_t (*start)(void *ip); - void (*stop)(void *ip); - msg_t (*configure)(void *ip, const void *config); - /* From hal_buffered_serial_c.*/ - /* From hal_buffered_spi_c.*/ -}; - -/** - * @brief Structure representing a buffered SPI wrapper class. - */ -struct hal_buffered_spi { - /** - * @brief Virtual Methods Table. - */ - const struct hal_buffered_spi_vmt *vmt; - /** - * @brief Driver state. - */ - driver_state_t state; - /** - * @brief Driver argument. - */ - void *arg; -#if (HAL_USE_MUTUAL_EXCLUSION == TRUE) || defined (__DOXYGEN__) - /** - * @brief Driver mutex. - */ - mutex_t mutex; -#endif /* HAL_USE_MUTUAL_EXCLUSION == TRUE */ -#if (HAL_USE_REGISTRY == TRUE) || defined (__DOXYGEN__) - /** - * @brief Driver identifier. - */ - unsigned int id; - /** - * @brief Driver name. - */ - const char *name; - /** - * @brief Registry link structure. - */ - hal_regent_t regent; -#endif /* HAL_USE_REGISTRY == TRUE */ - /** - * @brief Implemented interface @p asynchronous_channel_i. - */ - asynchronous_channel_i chn; - /** - * @brief Input queue. - */ - input_queue_t iqueue; - /** - * @brief Output queue. - */ - output_queue_t oqueue; - /** - * @brief I/O condition event source. - */ - event_source_t event; - /** - * @brief Pointer to the associated @p hal_spi_driver_c instance. - */ - hal_spi_driver_c *spip; -}; -/** @} */ - /*===========================================================================*/ /* External declarations. */ /*===========================================================================*/ @@ -367,14 +278,6 @@ extern "C" { /* Methods of hal_spi_driver_c.*/ void *__spi_objinit_impl(void *ip, const void *vmt); void __spi_dispose_impl(void *ip); - /* Methods of hal_buffered_spi_c.*/ - void *__bspi_objinit_impl(void *ip, const void *vmt, hal_spi_driver_c *spip, - uint8_t *ib, size_t ibsize, uint8_t *ob, - size_t obsize); - void __bspi_dispose_impl(void *ip); - msg_t __bspi_start_impl(void *ip); - void __bspi_stop_impl(void *ip); - msg_t __bspi_configure_impl(void *ip, const void *config); /* Regular functions.*/ void spiInit(void); #ifdef __cplusplus @@ -409,35 +312,89 @@ static inline hal_spi_driver_c *spiObjectInit(hal_spi_driver_c *self) { /** @} */ /** - * @name Default constructor of hal_buffered_spi_c + * @name Inline methods of hal_spi_driver_c * @{ */ +#if (SPI_SELECT_MODE == SPI_SELECT_MODE_LLD) || defined (__DOXYGEN__) /** - * @memberof hal_buffered_spi_c + * @memberof hal_spi_driver_c + * @public * - * @brief Default initialization function of @p hal_buffered_spi_c. + * @brief Asserts the slave select signal and prepares for transfers. * - * @param[out] self Pointer to a @p hal_buffered_spi_c instance to - * be initialized. - * @param[in] spip Pointer to the @p hal_spi_driver_c object. - * @param[in] ib Pointer to the input buffer. - * @param[in] ibsize Size of the input buffer. - * @param[in] ob Pointer to the output buffer. - * @param[in] obsize Size of the output buffer. - * @return Pointer to the initialized object. + * @param[in,out] ip Pointer to a @p hal_spi_driver_c instance. * - * @objinit + * @xclass */ CC_FORCE_INLINE -static inline hal_buffered_spi_c *bspiObjectInit(hal_buffered_spi_c *self, - hal_spi_driver_c *spip, - uint8_t *ib, size_t ibsize, - uint8_t *ob, size_t obsize) { - extern const struct hal_buffered_spi_vmt __hal_buffered_spi_vmt; +static inline void spiSelectX(void *ip) { + hal_spi_driver_c *self = (hal_spi_driver_c *)ip; - return __bspi_objinit_impl(self, &__hal_buffered_spi_vmt, spip, ib, ibsize, - ob, obsize); + spi_lld_select(self); } + +/** + * @memberof hal_spi_driver_c + * @public + * + * @brief Deasserts the slave select signal. + * + * @param[in,out] ip Pointer to a @p hal_spi_driver_c instance. + * + * @xclass + */ +CC_FORCE_INLINE +static inline void spiUnselectX(void *ip) { + hal_spi_driver_c *self = (hal_spi_driver_c *)ip; + + spi_lld_unselect(self); +} + +#elif SPI_SELECT_MODE == SPI_SELECT_MODE_LINE +CC_FORCE_INLINE +static inline void spiSelectX(void *ip) { + hal_spi_driver_c *self = (hal_spi_driver_c *)ip; + + palClearLine(self->config->ssline); +} + +CC_FORCE_INLINE +static inline void spiUnselectX(void *ip) { + hal_spi_driver_c *self = (hal_spi_driver_c *)ip; + + palSetLine(self->config->ssline); +} + +#elif SPI_SELECT_MODE == SPI_SELECT_MODE_PORT +CC_FORCE_INLINE +static inline void spiSelectX(void *ip) { + hal_spi_driver_c *self = (hal_spi_driver_c *)ip; + + palClearPort(self->config->ssport, (spip)->config->ssmask); +} + +CC_FORCE_INLINE +static inline void spiUnselectX(void *ip) { + hal_spi_driver_c *self = (hal_spi_driver_c *)ip; + + palSetPort(self->config->ssport, (spip)->config->ssmask); +} + +#elif SPI_SELECT_MODE == SPI_SELECT_MODE_PAD +CC_FORCE_INLINE +static inline void spiSelectX(void *ip) { + hal_spi_driver_c *self = (hal_spi_driver_c *)ip; + + palClearPad(self->config->ssport, (spip)->config->sspad); +} + +CC_FORCE_INLINE +static inline void spiUnselectX(void *ip) { + hal_spi_driver_c *self = (hal_spi_driver_c *)ip; + + palSetPad(self->config->ssport, (spip)->config->sspad); +} +#endif /* SPI_SELECT_MODE == SPI_SELECT_MODE_LLD */ /** @} */ #endif /* HAL_USE_SPI == TRUE */ diff --git a/os/xhal/src/hal_spi.c b/os/xhal/src/hal_spi.c index 819137436..85e122d21 100644 --- a/os/xhal/src/hal_spi.c +++ b/os/xhal/src/hal_spi.c @@ -135,144 +135,6 @@ const struct hal_spi_driver_vmt __hal_spi_driver_vmt = { .configure = NULL /* Method not found.*/ }; -/*===========================================================================*/ -/* Module class "hal_buffered_spi_c" methods. */ -/*===========================================================================*/ - -/** - * @name Methods implementations of hal_buffered_spi_c - * @{ - */ -/** - * @memberof hal_buffered_spi_c - * @protected - * - * @brief Implementation of object creation. - * @note This function is meant to be used by derived classes. - * - * @param[out] ip Pointer to a @p hal_buffered_spi_c instance to - * be initialized. - * @param[in] vmt VMT pointer for the new object. - * @param[in] spip Pointer to the @p hal_spi_driver_c object. - * @param[in] ib Pointer to the input buffer. - * @param[in] ibsize Size of the input buffer. - * @param[in] ob Pointer to the output buffer. - * @param[in] obsize Size of the output buffer. - * @return A new reference to the object. - */ -void *__bspi_objinit_impl(void *ip, const void *vmt, hal_spi_driver_c *spip, - uint8_t *ib, size_t ibsize, uint8_t *ob, - size_t obsize) { - hal_buffered_spi_c *self = (hal_buffered_spi_c *)ip; - - /* Initialization code.*/ - __bs_objinit_impl((void *)self, vmt, - ib, ibsize, NULL, NULL, - ob, obsize, __bspi_onotify, (void *)self); - drvSetArgumentX(spip, self); - self->spip = spip; - - return self; -} - -/** - * @memberof hal_buffered_spi_c - * @protected - * - * @brief Implementation of object finalization. - * @note This function is meant to be used by derived classes. - * - * @param[in,out] ip Pointer to a @p hal_buffered_spi_c instance to - * be disposed. - */ -void __bspi_dispose_impl(void *ip) { - hal_buffered_spi_c *self = (hal_buffered_spi_c *)ip; - - /* No finalization code.*/ - (void)self; - - /* Finalization of the ancestors-defined parts.*/ - __bs_dispose_impl(self); -} - -/** - * @memberof hal_buffered_spi_c - * @protected - * - * @brief Override of method @p __drv_start(). - * - * @param[in,out] ip Pointer to a @p hal_buffered_spi_c instance. - * @return The operation status. - */ -msg_t __bspi_start_impl(void *ip) { - hal_buffered_spi_c *self = (hal_buffered_spi_c *)ip; - msg_t msg; - - /* Start is a slow operation in this driver, we need to switch to the - HAL_DRV_STATE_STARTING state.*/ - self->state = HAL_DRV_STATE_STARTING; - osalSysUnlock(); - - /* Starting the undelying SPI driver.*/ - msg = drvStart(self->spip); - if (msg == HAL_RET_SUCCESS) { - spiSetCallbackX(self->spip, &__bspi_default_cb); - spiWriteEnableFlagsX(self->spip, SPI_EV_ALL_EVENTS); - } - - /* Back into the critical section and return.*/ - osalSysLock(); - return msg; -} - -/** - * @memberof hal_buffered_spi_c - * @protected - * - * @brief Override of method @p __drv_stop(). - * - * @param[in,out] ip Pointer to a @p hal_buffered_spi_c instance. - */ -void __bspi_stop_impl(void *ip) { - hal_buffered_spi_c *self = (hal_buffered_spi_c *)ip; - - /* Start is a slow operation in this driver, we need to switch to the - HAL_DRV_STATE_STOPPING state.*/ - self->state = HAL_DRV_STATE_STOPPING; - osalSysUnlock(); - - drvStop(self->spip); - - /* Back into the critical section and return.*/ - osalSysLock(); -} - -/** - * @memberof hal_buffered_spi_c - * @protected - * - * @brief Override of method @p drvConfigureX(). - * - * @param[in,out] ip Pointer to a @p hal_buffered_spi_c instance. - * @param[in] config New driver configuration. - */ -msg_t __bspi_configure_impl(void *ip, const void *config) { - hal_buffered_spi_c *self = (hal_buffered_spi_c *)ip; - return drvConfigureX(self->spip, config); -} -/** @} */ - -/** - * @brief VMT structure of buffered SPI wrapper class. - * @note It is public because accessed by the inlined constructor. - */ -const struct hal_buffered_spi_vmt __hal_buffered_spi_vmt = { - .dispose = __bspi_dispose_impl, - .start = __bspi_start_impl, - .stop = __bspi_stop_impl, - .configure = __bspi_configure_impl -}; - #endif /* HAL_USE_SPI == TRUE */ /** @} */