123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392 |
- /*
- ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- /**
- * @file hal_wspi.c
- * @brief WSPI Driver code.
- *
- * @addtogroup WSPI
- * @{
- */
- #include "hal.h"
- #if (HAL_USE_WSPI == TRUE) || defined(__DOXYGEN__)
- /*===========================================================================*/
- /* Driver local definitions. */
- /*===========================================================================*/
- /*===========================================================================*/
- /* Driver exported variables. */
- /*===========================================================================*/
- /*===========================================================================*/
- /* Driver local variables and types. */
- /*===========================================================================*/
- /*===========================================================================*/
- /* Driver local functions. */
- /*===========================================================================*/
- /*===========================================================================*/
- /* Driver exported functions. */
- /*===========================================================================*/
- /**
- * @brief WSPI Driver initialization.
- * @note This function is implicitly invoked by @p halInit(), there is
- * no need to explicitly initialize the driver.
- *
- * @init
- */
- void wspiInit(void) {
- wspi_lld_init();
- }
- /**
- * @brief Initializes the standard part of a @p WSPIDriver structure.
- *
- * @param[out] wspip pointer to the @p WSPIDriver object
- *
- * @init
- */
- void wspiObjectInit(WSPIDriver *wspip) {
- wspip->state = WSPI_STOP;
- wspip->config = NULL;
- #if WSPI_USE_WAIT == TRUE
- wspip->thread = NULL;
- #endif
- #if WSPI_USE_MUTUAL_EXCLUSION == TRUE
- osalMutexObjectInit(&wspip->mutex);
- #endif
- #if defined(WSPI_DRIVER_EXT_INIT_HOOK)
- WSPI_DRIVER_EXT_INIT_HOOK(wspip);
- #endif
- }
- /**
- * @brief Configures and activates the WSPI peripheral.
- *
- * @param[in] wspip pointer to the @p WSPIDriver object
- * @param[in] config pointer to the @p WSPIConfig object
- *
- * @api
- */
- void wspiStart(WSPIDriver *wspip, const WSPIConfig *config) {
- osalDbgCheck((wspip != NULL) && (config != NULL));
- osalSysLock();
- osalDbgAssert((wspip->state == WSPI_STOP) || (wspip->state == WSPI_READY),
- "invalid state");
- wspip->config = config;
- wspi_lld_start(wspip);
- wspip->state = WSPI_READY;
- osalSysUnlock();
- }
- /**
- * @brief Deactivates the WSPI peripheral.
- * @note Deactivating the peripheral also enforces a release of the slave
- * select line.
- *
- * @param[in] wspip pointer to the @p WSPIDriver object
- *
- * @api
- */
- void wspiStop(WSPIDriver *wspip) {
- osalDbgCheck(wspip != NULL);
- osalSysLock();
- osalDbgAssert((wspip->state == WSPI_STOP) || (wspip->state == WSPI_READY),
- "invalid state");
- wspi_lld_stop(wspip);
- wspip->config = NULL;
- wspip->state = WSPI_STOP;
- osalSysUnlock();
- }
- /**
- * @brief Sends a command without data phase.
- * @post At the end of the operation the configured callback is invoked.
- *
- * @param[in] wspip pointer to the @p WSPIDriver object
- * @param[in] cmdp pointer to the command descriptor
- *
- * @api
- */
- void wspiStartCommand(WSPIDriver *wspip, const wspi_command_t *cmdp) {
- osalDbgCheck((wspip != NULL) && (cmdp != NULL));
- osalSysLock();
- osalDbgAssert(wspip->state == WSPI_READY, "not ready");
- wspiStartCommandI(wspip, cmdp);
- osalSysUnlock();
- }
- /**
- * @brief Sends a command with data over the WSPI bus.
- * @post At the end of the operation the configured callback is invoked.
- *
- * @param[in] wspip pointer to the @p WSPIDriver object
- * @param[in] cmdp pointer to the command descriptor
- * @param[in] n number of bytes to send
- * @param[in] txbuf the pointer to the transmit buffer
- *
- * @api
- */
- void wspiStartSend(WSPIDriver *wspip, const wspi_command_t *cmdp,
- size_t n, const uint8_t *txbuf) {
- osalDbgCheck((wspip != NULL) && (cmdp != NULL));
- osalDbgCheck((n > 0U) && (txbuf != NULL));
- osalSysLock();
- osalDbgAssert(wspip->state == WSPI_READY, "not ready");
- wspiStartSendI(wspip, cmdp, n, txbuf);
- osalSysUnlock();
- }
- /**
- * @brief Sends a command then receives data over the WSPI bus.
- * @post At the end of the operation the configured callback is invoked.
- *
- * @param[in] wspip pointer to the @p WSPIDriver object
- * @param[in] cmdp pointer to the command descriptor
- * @param[in] n number of bytes to send
- * @param[out] rxbuf the pointer to the receive buffer
- *
- * @api
- */
- void wspiStartReceive(WSPIDriver *wspip, const wspi_command_t *cmdp,
- size_t n, uint8_t *rxbuf) {
- osalDbgCheck((wspip != NULL) && (cmdp != NULL));
- osalDbgCheck((n > 0U) && (rxbuf != NULL));
- osalSysLock();
- osalDbgAssert(wspip->state == WSPI_READY, "not ready");
- wspiStartReceiveI(wspip, cmdp, n, rxbuf);
- osalSysUnlock();
- }
- #if (WSPI_USE_WAIT == TRUE) || defined(__DOXYGEN__)
- /**
- * @brief Sends a command without data phase.
- * @pre In order to use this function the option @p WSPI_USE_WAIT must be
- * enabled.
- * @pre In order to use this function the driver must have been configured
- * without callbacks (@p end_cb = @p NULL).
- *
- * @param[in] wspip pointer to the @p WSPIDriver object
- * @param[in] cmdp pointer to the command descriptor
- *
- * @api
- */
- void wspiCommand(WSPIDriver *wspip, const wspi_command_t *cmdp) {
- osalDbgCheck((wspip != NULL) && (cmdp != NULL));
- osalDbgCheck((cmdp->cfg & WSPI_CFG_DATA_MODE_MASK) == WSPI_CFG_DATA_MODE_NONE);
- osalSysLock();
- osalDbgAssert(wspip->state == WSPI_READY, "not ready");
- osalDbgAssert(wspip->config->end_cb == NULL, "has callback");
- wspiStartCommandI(wspip, cmdp);
- (void) osalThreadSuspendS(&wspip->thread);
- osalSysUnlock();
- }
- /**
- * @brief Sends a command with data over the WSPI bus.
- * @pre In order to use this function the option @p WSPI_USE_WAIT must be
- * enabled.
- * @pre In order to use this function the driver must have been configured
- * without callbacks (@p end_cb = @p NULL).
- *
- * @param[in] wspip pointer to the @p WSPIDriver object
- * @param[in] cmdp pointer to the command descriptor
- * @param[in] n number of bytes to send
- * @param[in] txbuf the pointer to the transmit buffer
- *
- * @api
- */
- void wspiSend(WSPIDriver *wspip, const wspi_command_t *cmdp,
- size_t n, const uint8_t *txbuf) {
- osalDbgCheck((wspip != NULL) && (cmdp != NULL));
- osalDbgCheck((n > 0U) && (txbuf != NULL));
- osalDbgCheck((cmdp->cfg & WSPI_CFG_DATA_MODE_MASK) != WSPI_CFG_DATA_MODE_NONE);
- osalSysLock();
- osalDbgAssert(wspip->state == WSPI_READY, "not ready");
- osalDbgAssert(wspip->config->end_cb == NULL, "has callback");
- wspiStartSendI(wspip, cmdp, n, txbuf);
- (void) osalThreadSuspendS(&wspip->thread);
- osalSysUnlock();
- }
- /**
- * @brief Sends a command then receives data over the WSPI bus.
- * @pre In order to use this function the option @p WSPI_USE_WAIT must be
- * enabled.
- * @pre In order to use this function the driver must have been configured
- * without callbacks (@p end_cb = @p NULL).
- *
- * @param[in] wspip pointer to the @p WSPIDriver object
- * @param[in] cmdp pointer to the command descriptor
- * @param[in] n number of bytes to send
- * @param[out] rxbuf the pointer to the receive buffer
- *
- * @api
- */
- void wspiReceive(WSPIDriver *wspip, const wspi_command_t *cmdp,
- size_t n, uint8_t *rxbuf) {
- osalDbgCheck((wspip != NULL) && (cmdp != NULL));
- osalDbgCheck((n > 0U) && (rxbuf != NULL));
- osalDbgCheck((cmdp->cfg & WSPI_CFG_DATA_MODE_MASK) != WSPI_CFG_DATA_MODE_NONE);
- osalSysLock();
- osalDbgAssert(wspip->state == WSPI_READY, "not ready");
- osalDbgAssert(wspip->config->end_cb == NULL, "has callback");
- wspiStartReceiveI(wspip, cmdp, n, rxbuf);
- (void) osalThreadSuspendS(&wspip->thread);
- osalSysUnlock();
- }
- #endif /* WSPI_USE_WAIT == TRUE */
- #if (WSPI_SUPPORTS_MEMMAP == TRUE) || defined(__DOXYGEN__)
- /**
- * @brief Maps in memory space a WSPI flash device.
- * @pre The memory flash device must be initialized appropriately
- * before mapping it in memory space.
- *
- * @param[in] wspip pointer to the @p WSPIDriver object
- * @param[in] cmdp pointer to the command descriptor
- * @param[out] addrp pointer to the memory start address of the mapped
- * flash or @p NULL
- *
- * @api
- */
- void wspiMapFlash(WSPIDriver *wspip,
- const wspi_command_t *cmdp,
- uint8_t **addrp) {
- osalDbgCheck((wspip != NULL) && (cmdp != NULL));
- osalDbgCheck((cmdp->cfg & WSPI_CFG_DATA_MODE_MASK) != WSPI_CFG_DATA_MODE_NONE);
- osalSysLock();
- osalDbgAssert(wspip->state == WSPI_READY, "not ready");
- wspiMapFlashI(wspip, cmdp, addrp);
- wspip->state = WSPI_MEMMAP;
- osalSysUnlock();
- }
- /**
- * @brief Unmaps from memory space a WSPI flash device.
- * @post The memory flash device must be re-initialized for normal
- * commands exchange.
- *
- * @param[in] wspip pointer to the @p WSPIDriver object
- *
- * @api
- */
- void wspiUnmapFlash(WSPIDriver *wspip) {
- osalDbgCheck(wspip != NULL);
- osalSysLock();
- osalDbgAssert(wspip->state == WSPI_MEMMAP, "not ready");
- wspiUnmapFlashI(wspip);
- wspip->state = WSPI_READY;
- osalSysUnlock();
- }
- #endif /* WSPI_SUPPORTS_MEMMAP == TRUE */
- #if (WSPI_USE_MUTUAL_EXCLUSION == TRUE) || defined(__DOXYGEN__)
- /**
- * @brief Gains exclusive access to the WSPI bus.
- * @details This function tries to gain ownership to the WSPI bus, if the bus
- * is already being used then the invoking thread is queued.
- * @pre In order to use this function the option @p WSPI_USE_MUTUAL_EXCLUSION
- * must be enabled.
- *
- * @param[in] wspip pointer to the @p WSPIDriver object
- *
- * @api
- */
- void wspiAcquireBus(WSPIDriver *wspip) {
- osalDbgCheck(wspip != NULL);
- osalMutexLock(&wspip->mutex);
- }
- /**
- * @brief Releases exclusive access to the WSPI bus.
- * @pre In order to use this function the option @p WSPI_USE_MUTUAL_EXCLUSION
- * must be enabled.
- *
- * @param[in] wspip pointer to the @p WSPIDriver object
- *
- * @api
- */
- void wspiReleaseBus(WSPIDriver *wspip) {
- osalDbgCheck(wspip != NULL);
- osalMutexUnlock(&wspip->mutex);
- }
- #endif /* WSPI_USE_MUTUAL_EXCLUSION == TRUE */
- #endif /* HAL_USE_WSPI == TRUE */
- /** @} */
|