/** * @file * @brief Serial Peripheral Interface (SPI) communications driver. */ /****************************************************************************** * * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by * Analog Devices, Inc.), * Copyright (C) 2023-2024 Analog Devices, Inc. * * 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. * ******************************************************************************/ #ifndef LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32655_SPI_H_ #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32655_SPI_H_ /***** includes *******/ #include #include "spi_regs.h" #include "mxc_sys.h" #include "mxc_assert.h" #include "gpio.h" #include "mxc_pins.h" #include "mxc_lock.h" #ifdef __cplusplus extern "C" { #endif /** * @defgroup spi SPI * @ingroup periphlibs * @{ */ /***** Definitions *****/ /** * @brief The list of SPI Widths supported * * The SPI Width can be set on a per-transaction basis. * An example use case of SPI_WIDTH_STANDARD_HALFDUPLEX is * given. * * Using a MAX31865 RTD-to-SPI IC, read back the temperature * The IC requires a SPI Read to be executed as * 1. Assert SS * 2. Write an 8bit register address * 3. Read back the 8 bit register * 4. Deassert SS * This can be accomplished with the STANDARD_HALFDUPLEX width * 1. set txData to the address, txLen=1 * 2. set rxData to a buffer of 1 byte, rxLen=1 * 3. The driver will transmit the txData, and after completion of * txData begin to recieve data, padding MOSI with DefaultTXData * */ typedef enum { SPI_WIDTH_3WIRE, ///< 1 Data line, half duplex SPI_WIDTH_STANDARD, ///< MISO/MOSI, full duplex SPI_WIDTH_DUAL, ///< 2 Data lines, half duplex SPI_WIDTH_QUAD, ///< 4 Data lines, half duplex } mxc_spi_width_t; /** * @brief The list of SPI modes * * SPI supports four combinations of clock and phase polarity * * Clock polarity is controlled using the bit SPIn_CTRL2.cpol * and determines if the clock is active high or active low * * Clock phase determines when the data must be stable for sampling * */ typedef enum { SPI_MODE_0, ///< clock phase = 0, clock polarity = 0 SPI_MODE_1, ///< clock phase = 0, clock polarity = 1 SPI_MODE_2, ///< clock phase = 1, clock polarity = 0 SPI_MODE_3, ///< clock phase = 1, clock polarity = 1 } mxc_spi_mode_t; typedef struct _mxc_spi_pins_t mxc_spi_pins_t; /** * @brief Structure used to initialize SPI pins. * * @note All values must be initialized. * * @note True equals pin is set for the spi function false the pin is left to its latest state. */ struct _mxc_spi_pins_t { bool clock; /// 8 bits, use two bytes per character ///< and pad the MSB of the upper byte with zeros uint8_t *rxData; ///< Buffer to store received data For character sizes ///< < 8 bits, pad the MSB of each byte with zeros. For ///< character sizes > 8 bits, use two bytes per character ///< and pad the MSB of the upper byte with zeros uint32_t txLen; ///< Number of bytes to be sent from txData uint32_t rxLen; ///< Number of bytes to be stored in rxData uint32_t txCnt; ///< Number of bytes actually transmitted from txData uint32_t rxCnt; ///< Number of bytes stored in rxData spi_complete_cb_t completeCB; ///< Pointer to function called when transaction is complete }; /* ************************************************************************* */ /* Control/Configuration functions */ /* ************************************************************************* */ /** * @brief Initialize and enable SPI peripheral. * * This function initializes everything necessary to call a SPI transaction function. * Some parameters are set to defaults as follows: * SPI Mode - 0 * SPI Width - SPI_WIDTH_STANDARD (even if quadModeUsed is set) * * These parameters can be modified after initialization using low level functions * * @note On default this function enables SPI peripheral clock and spi gpio pins. * if you wish to manage clock and gpio related things in upper level instead of here. * Define MSDK_NO_GPIO_CLK_INIT flag in project.mk file. * By this flag this function will remove clock and gpio related codes from file. * * @param spi Pointer to SPI registers (selects the SPI block used.) * @param masterMode Whether to put the device in master or slave mode. Use * non-zero for master mode, and zero for slave mode. * @param quadModeUsed Whether to obtain control of the SDIO2/3 pins. Use * non-zero if the pins are needed (if Quad Mode will * be used), and zero if they are not needed (quad mode * will never be used). * @param numSlaves The number of slaves used, if in master mode. This * is used to obtain control of the necessary SS pins. * In slave mode this is ignored and SS1 is used. * @param ssPolarity This field sets the SS active polarity for each * slave, each bit position corresponds to each SS line. * @param hz The requested clock frequency. The actual clock frequency * will be returned by the function if successful. Used in * master mode only. * @param pins SPI pin structure. Pins selected as true will be initialized * for the requested SPI block. Has no effect incase of * MSDK_NO_GPIO_CLK_INIT has been defined. * * @return If successful, the actual clock frequency is returned. Otherwise, see * \ref MXC_Error_Codes for a list of return codes. */ int MXC_SPI_Init(mxc_spi_regs_t *spi, int masterMode, int quadModeUsed, int numSlaves, unsigned ssPolarity, unsigned int hz, mxc_spi_pins_t pins); /** * @brief Disable and shutdown SPI peripheral. * * @param spi Pointer to SPI registers (selects the SPI block used.) * * @return Success/Fail, see \ref MXC_Error_Codes for a list of return codes. */ int MXC_SPI_Shutdown(mxc_spi_regs_t *spi); /** * @brief Checks if the given SPI bus can be placed in sleep mode. * * This functions checks to see if there are any on-going SPI transactions in * progress. If there are transactions in progress, the application should * wait until the SPI bus is free before entering a low-power state. * * @param spi Pointer to SPI registers (selects the SPI block used.) * * @return #E_NO_ERROR if ready, and non-zero if busy or error. See \ref * MXC_Error_Codes for the list of error return codes. */ int MXC_SPI_ReadyForSleep(mxc_spi_regs_t *spi); /** * @brief Returns the frequency of the clock used as the bit rate generator for a given SPI instance. * * @param spi Pointer to SPI registers (selects the SPI block used.) * * @return Frequency of the clock used as the bit rate generator */ int MXC_SPI_GetPeripheralClock(mxc_spi_regs_t *spi); /** * @brief Set the frequency of the SPI interface. * * This function is applicable in Master mode only * * @param spi Pointer to SPI registers (selects the SPI block used.) * @param hz The desired frequency in Hertz. * * @return Negative if error, otherwise actual speed set. See \ref * MXC_Error_Codes for the list of error return codes. */ int MXC_SPI_SetFrequency(mxc_spi_regs_t *spi, unsigned int hz); /** * @brief Get the frequency of the SPI interface. * * This function is applicable in Master mode only * * @param spi Pointer to SPI registers (selects the SPI block used.) * * @return The SPI bus frequency in Hertz */ unsigned int MXC_SPI_GetFrequency(mxc_spi_regs_t *spi); /** * @brief Sets the number of bits per character * * @param spi Pointer to SPI registers (selects the SPI block used.) * @param dataSize The number of bits per character * * @return Success/Fail, see \ref MXC_Error_Codes for a list of return codes. */ int MXC_SPI_SetDataSize(mxc_spi_regs_t *spi, int dataSize); /** * @brief Gets the number of bits per character * * @param spi Pointer to SPI registers (selects the SPI block used.) * * @return Success/Fail, see \ref MXC_Error_Codes for a list of return codes. */ int MXC_SPI_GetDataSize(mxc_spi_regs_t *spi); /** * @brief Sets Multi-Transaction (MT) mode on or off * * This function allows Multi-Transaction Mode to be enabled or disabled. MT Mode * provides a performance improvement on back-to-back DMA operations. * * @param spi Pointer to SPI registers (selects the SPI block used.) * @param mtMode 1 to enable, 0 to disable (default) * * @return Success/Fail, see \ref MXC_Error_Codes for a list of return codes. */ int MXC_SPI_SetMTMode(mxc_spi_regs_t *spi, int mtMode); /** * @brief Gets the current setting for Multi-Transaction (MT) mode * * @param spi Pointer to SPI registers (selects the SPI block used.) * * @return The current value of MT mode, either 1 (enabled) or 0 (disabled) */ int MXC_SPI_GetMTMode(mxc_spi_regs_t *spi); /* ************************************************************************* */ /* Low-level functions */ /* ************************************************************************* */ /** * @brief Sets the slave select (SS) line used for transmissions * * This function is applicable in Master mode only * * @param spi Pointer to SPI registers (selects the SPI block used.) * @param ssIdx Slave select index * * @return Success/Fail, see \ref MXC_Error_Codes for a list of return codes. */ int MXC_SPI_SetSlave(mxc_spi_regs_t *spi, int ssIdx); /** * @brief Gets the slave select (SS) line used for transmissions * * This function is applicable in Master mode only * * @param spi Pointer to SPI registers (selects the SPI block used.) * * @return slave slect */ int MXC_SPI_GetSlave(mxc_spi_regs_t *spi); /** * @brief Sets the SPI width used for transmissions * * @param spi Pointer to SPI registers (selects the SPI block used.) * @param spiWidth SPI Width (3-Wire, Standard, Dual SPI, Quad SPI) * * @return Success/Fail, see \ref MXC_Error_Codes for a list of return codes. */ int MXC_SPI_SetWidth(mxc_spi_regs_t *spi, mxc_spi_width_t spiWidth); /** * @brief Gets the SPI width used for transmissions * * @param spi Pointer to SPI registers (selects the SPI block used.) * * @return Spi Width */ mxc_spi_width_t MXC_SPI_GetWidth(mxc_spi_regs_t *spi); /** * @brief Sets the spi mode using clock polarity and clock phase * * @param spi Pointer to SPI registers (selects the SPI block used.) * @param spiMode \ref mxc_spi_mode_t * * @return Success/Fail, see \ref MXC_Error_Codes for a list of return codes. */ int MXC_SPI_SetMode(mxc_spi_regs_t *spi, mxc_spi_mode_t spiMode); /** * @brief Gets the spi mode * * @param spi Pointer to SPI registers (selects the SPI block used.) * * @return mxc_spi_mode_t \ref mxc_spi_mode_t */ mxc_spi_mode_t MXC_SPI_GetMode(mxc_spi_regs_t *spi); /** * @brief Starts a SPI Transmission * * This function is applicable in Master mode only * * The user must ensure that there are no ongoing transmissions before * calling this function * * @param spi Pointer to SPI registers (selects the SPI block used.) * * @return Success/Fail, see \ref MXC_Error_Codes for a list of return codes. */ int MXC_SPI_StartTransmission(mxc_spi_regs_t *spi); /** * @brief Checks the SPI Peripheral for an ongoing transmission * * This function is applicable in Master mode only * * @param spi Pointer to SPI registers (selects the SPI block used.) * * @return Active/Inactive, see \ref MXC_Error_Codes for a list of return codes. */ int MXC_SPI_GetActive(mxc_spi_regs_t *spi); /** * @brief Aborts an ongoing SPI Transmission * * This function is applicable in Master mode only * * @param spi Pointer to SPI registers (selects the SPI block used.) * * @return Success/Fail, see \ref MXC_Error_Codes for a list of return codes. */ int MXC_SPI_AbortTransmission(mxc_spi_regs_t *spi); /** * @brief Unloads bytes from the receive FIFO. * * @param spi Pointer to SPI registers (selects the SPI block used.) * @param bytes The buffer to read the data into. * @param len The number of bytes to read. * * @return The number of bytes actually read. */ unsigned int MXC_SPI_ReadRXFIFO(mxc_spi_regs_t *spi, unsigned char *bytes, unsigned int len); /** * @brief Get the number of bytes currently available in the receive FIFO. * * @param spi Pointer to SPI registers (selects the SPI block used.) * * @return The number of bytes available. */ unsigned int MXC_SPI_GetRXFIFOAvailable(mxc_spi_regs_t *spi); /** * @brief Loads bytes into the transmit FIFO. * * @param spi Pointer to SPI registers (selects the SPI block used.) * @param bytes The buffer containing the bytes to write * @param len The number of bytes to write. * * @return The number of bytes actually written. */ unsigned int MXC_SPI_WriteTXFIFO(mxc_spi_regs_t *spi, unsigned char *bytes, unsigned int len); /** * @brief Get the amount of free space available in the transmit FIFO. * * @param spi Pointer to SPI registers (selects the SPI block used.) * * @return The number of bytes available. */ unsigned int MXC_SPI_GetTXFIFOAvailable(mxc_spi_regs_t *spi); /** * @brief Removes and discards all bytes currently in the receive FIFO. * * @param spi Pointer to SPI registers (selects the SPI block used.) */ void MXC_SPI_ClearRXFIFO(mxc_spi_regs_t *spi); /** * @brief Removes and discards all bytes currently in the transmit FIFO. * * @param spi Pointer to SPI registers (selects the SPI block used.) */ void MXC_SPI_ClearTXFIFO(mxc_spi_regs_t *spi); /** * @brief Set the receive threshold level. * * RX FIFO Receive threshold. Smaller values will cause * interrupts to occur more often, but reduce the possibility * of losing data because of a FIFO overflow. Larger values * will reduce the time required by the ISR, but increase the * possibility of data loss. Passing an invalid value will * cause the driver to use the value already set in the * appropriate register. * * @param spi Pointer to SPI registers (selects the SPI block used.) * @param numBytes The threshold level to set. This value must be * between 0 and 8 inclusive. * * @return Success/Fail, see \ref MXC_Error_Codes for a list of return codes. */ int MXC_SPI_SetRXThreshold(mxc_spi_regs_t *spi, unsigned int numBytes); /** * @brief Get the current receive threshold level. * * @param spi Pointer to SPI registers (selects the SPI block used.) * * @return The receive threshold value (in bytes). */ unsigned int MXC_SPI_GetRXThreshold(mxc_spi_regs_t *spi); /** * @brief Set the transmit threshold level. * * TX FIFO threshold. Smaller values will cause interrupts * to occur more often, but reduce the possibility of terminating * a transaction early in master mode, or transmitting invalid data * in slave mode. Larger values will reduce the time required by * the ISR, but increase the possibility errors occurring. Passing * an invalid value will cause the driver to use the value already * set in the appropriate register. * * @param spi Pointer to SPI registers (selects the SPI block used.) * @param numBytes The threshold level to set. This value must be * between 0 and 8 inclusive. * * @return Success/Fail, see \ref MXC_Error_Codes for a list of return codes. */ int MXC_SPI_SetTXThreshold(mxc_spi_regs_t *spi, unsigned int numBytes); /** * @brief Get the current transmit threshold level. * * @param spi Pointer to SPI registers (selects the SPI block used.) * * @return The transmit threshold value (in bytes). */ unsigned int MXC_SPI_GetTXThreshold(mxc_spi_regs_t *spi); /** * @brief Gets the interrupt flags that are currently set * * These functions should not be used while using non-blocking Transaction Level * functions (Async or DMA) * * @param spi Pointer to SPI registers (selects the SPI block used.) * * @return The interrupt flags */ unsigned int MXC_SPI_GetFlags(mxc_spi_regs_t *spi); /** * @brief Clears the interrupt flags that are currently set * * These functions should not be used while using non-blocking Transaction Level * functions (Async or DMA) * * @param spi Pointer to SPI registers (selects the SPI block used.) * */ void MXC_SPI_ClearFlags(mxc_spi_regs_t *spi); /** * @brief Enables specific interrupts * * These functions should not be used while using non-blocking Transaction Level * functions (Async or DMA) * * @param spi Pointer to SPI registers (selects the SPI block used.) * @param intEn The interrupts to be enabled */ void MXC_SPI_EnableInt(mxc_spi_regs_t *spi, unsigned int intEn); /** * @brief Disables specific interrupts * * These functions should not be used while using non-blocking Transaction Level * functions (Async or DMA) * * @param spi Pointer to SPI registers (selects the SPI block used.) * @param intDis The interrupts to be disabled */ void MXC_SPI_DisableInt(mxc_spi_regs_t *spi, unsigned int intDis); /* ************************************************************************* */ /* Transaction level functions */ /* ************************************************************************* */ /** * @brief Performs a blocking SPI transaction. * * Performs a blocking SPI transaction. * These actions will be performed in Master Mode: * 1. Assert the specified SS * 2. In Full Duplex Modes, send TX data while receiving RX Data * if rxLen > txLen, pad txData with DefaultTXData * if txLen > rxLen, discard rxData where rxCnt > rxLen * 3. In Half Duplex Modes, send TX Data, then receive RX Data * 4. Deassert the specified SS * * These actions will be performed in Slave Mode: * 1. Fill FIFO with txData * 2. Wait for SS Assert * 3. If needed, pad txData with DefaultTXData * 4. Unload RX FIFO as needed * 5. On SS Deassert, return * * @param req Pointer to details of the transaction * * @return See \ref MXC_Error_Codes for the list of error return codes. */ int MXC_SPI_MasterTransaction(mxc_spi_req_t *req); /** * @brief Setup an interrupt-driven SPI transaction * * The TX FIFO will be filled with txData, padded with DefaultTXData if necessary * Relevant interrupts will be enabled, and relevant registers set (SS, Width, etc) * * @param req Pointer to details of the transaction * * @return See \ref MXC_Error_Codes for the list of error return codes. */ int MXC_SPI_MasterTransactionAsync(mxc_spi_req_t *req); /** * @brief Setup a DMA driven SPI transaction * * The TX FIFO will be filled with txData, padded with DefaultTXData if necessary * Relevant interrupts will be enabled, and relevant registers set (SS, Width, etc) * * The lowest-indexed unused DMA channel will be acquired (using the DMA API) and * set up to load/unload the FIFOs with as few interrupt-based events as * possible. The channel will be reset and returned to the system at the end of * the transaction. * * @param req Pointer to details of the transaction * * @return See \ref MXC_Error_Codes for the list of error return codes. */ int MXC_SPI_MasterTransactionDMA(mxc_spi_req_t *req); /** * @brief Performs a blocking SPI transaction. * * Performs a blocking SPI transaction. * These actions will be performed in Slave Mode: * 1. Fill FIFO with txData * 2. Wait for SS Assert * 3. If needed, pad txData with DefaultTXData * 4. Unload RX FIFO as needed * 5. On SS Deassert, return * * @param req Pointer to details of the transaction * * @return See \ref MXC_Error_Codes for the list of error return codes. */ int MXC_SPI_SlaveTransaction(mxc_spi_req_t *req); /** * @brief Setup an interrupt-driven SPI transaction * * The TX FIFO will be filled with txData, padded with DefaultTXData if necessary * Relevant interrupts will be enabled, and relevant registers set (SS, Width, etc) * * @param req Pointer to details of the transactionz * * @return See \ref MXC_Error_Codes for the list of error return codes. */ int MXC_SPI_SlaveTransactionAsync(mxc_spi_req_t *req); /** * @brief Setup a DMA driven SPI transaction * * The TX FIFO will be filled with txData, padded with DefaultTXData if necessary * Relevant interrupts will be enabled, and relevant registers set (SS, Width, etc) * * The lowest-indexed unused DMA channel will be acquired (using the DMA API) and * set up to load/unload the FIFOs with as few interrupt-based events as * possible. The channel will be reset and returned to the system at the end of * the transaction. * * @param req Pointer to details of the transaction * * @return See \ref MXC_Error_Codes for the list of error return codes. */ int MXC_SPI_SlaveTransactionDMA(mxc_spi_req_t *req); /** * @brief Sets the TX data to transmit as a 'dummy' byte * * In single wire master mode, this data is transmitted on MOSI when performing * an RX (MISO) only transaction. This defaults to 0. * * @param spi Pointer to SPI registers (selects the SPI block used.) * @param defaultTXData Data to shift out in RX-only transactions * * @return Success/Fail, see \ref MXC_Error_Codes for a list of return codes. */ int MXC_SPI_SetDefaultTXData(mxc_spi_regs_t *spi, unsigned int defaultTXData); /** * @brief Abort any asynchronous requests in progress. * * Abort any asynchronous requests in progress. Any callbacks associated with * the active transaction will be executed to indicate when the transaction * has been terminated. * * @param spi Pointer to SPI registers (selects the SPI block used.) */ void MXC_SPI_AbortAsync(mxc_spi_regs_t *spi); /** * @brief The processing function for asynchronous transactions. * * When using the asynchronous functions, the application must call this * function periodically. This can be done from within the SPI interrupt * handler or periodically by the application if SPI interrupts are disabled. * * @param spi Pointer to SPI registers (selects the SPI block used.) */ void MXC_SPI_AsyncHandler(mxc_spi_regs_t *spi); /** * @brief Enable/Disable HW CS control feature. * * Depending on the application, the user might need to manually drive the slave select pin. * The SPI driver automatically drives the SS pin and this function enables/disables this * feature. * * @param spi Pointer to SPI registers (selects the SPI block used.) * @param state Non-zero values: enable HW SS mode. Zero: disable HW SS mode. */ void MXC_SPI_HWSSControl(mxc_spi_regs_t *spi, int state); /**@} end of group spi */ #ifdef __cplusplus } #endif #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32655_SPI_H_