// ----------------------------------------------------------------------------- /// @file spi.h /// @brief File description // ----------------------------------------------------------------------------- // Micro-Key bv // Industrieweg 28, 9804 TG Noordhorn // Postbus 92, 9800 AB Zuidhorn // The Netherlands // Tel: +31 594 503020 // Fax: +31 594 505825 // Email: support@microkey.nl // Web: www.microkey.nl // ----------------------------------------------------------------------------- /// $Revision$ /// $Author$ /// $Date$ // (c) 2017 Micro-Key bv // ----------------------------------------------------------------------------- /// @defgroup {group_name} {group_description} /// Description /// @file spi.h /// @ingroup {group_name} #ifndef MISC_INC_SPI_H_ #define MISC_INC_SPI_H_ // ----------------------------------------------------------------------------- // Include files // ----------------------------------------------------------------------------- #include #include "FreeRTOS.h" #include "semphr.h" #include "platform.h" // ----------------------------------------------------------------------------- // Constant and macro definitions // ----------------------------------------------------------------------------- #define SPI_DEF_Direction (SPI_Direction_2Lines_FullDuplex) #define SPI_DEF_Mode (SPI_Mode_Master) #define SPI_DEF_DataSize (SPI_DataSize_8b) #define SPI_DEF_CPOL (SPI_CPOL_Low) #define SPI_DEF_CPHA (SPI_CPHA_1Edge) #define SPI_DEF_NSS (SPI_NSS_Hard) #define SPI_DEF_BaudRatePrescaler (SPI_BaudRatePrescaler_2) #define SPI_DEF_FirstBit (SPI_FirstBit_MSB) #define SPI_DEF_CRCPolynomial (7) #define SPI_DEF_RX_QUEUE (16) #define SPI_DEF_TX_QUEUE (16) // ----------------------------------------------------------------------------- // Type definitions. // ----------------------------------------------------------------------------- struct spiQueueItem { char byte; }; struct Spi { SPI_TypeDef* SPI_TypeDef; SPI_InitTypeDef SPI_InitStruct; T_PL_GPIO SPI_CLK; T_PL_GPIO* SPI_CE; T_PL_GPIO SPI_MOSI; T_PL_GPIO SPI_MISO; SemaphoreHandle_t spiClaimed; //! Semaphore to protect SPI bus //! against multiple use SemaphoreHandle_t txSemaphore; //! Semaphore for transmit handler //! to allow wait state while //! transmission is active xQueueHandle txQueue; //! SPI Transfer queue identifier xQueueHandle rxQueue; //! SPI Receive queue identifier bool initialized; }; struct SpiParameters { uint16_t SPI_Direction; uint16_t SPI_Mode; uint16_t SPI_DataSize; uint16_t SPI_CPOL; uint16_t SPI_CPHA; uint16_t SPI_NSS; uint16_t SPI_BaudRatePrescaler; uint16_t SPI_FirstBit; uint16_t SPI_CRCPolynomial; UBaseType_t txQueueSize; UBaseType_t rxQueueSize; }; struct SpiDevice { struct Spi* spi; struct SpiParameters* spiParameters; T_PL_GPIO SPI_CE; }; // ----------------------------------------------------------------------------- // Function declarations // ----------------------------------------------------------------------------- /** ---------------------------------------------------------------------------- * Spi_construct * Description of function * * @param self The SPi object to initialize * @param parameters The SPI parameters to use * * @return ErrorStatus SUCCESS if writing message was successful * ERROR otherwise * * @todo * ----------------------------------------------------------------------------- */ extern ErrorStatus SPI_construct(struct Spi* self, struct SpiParameters* parameters); /** ---------------------------------------------------------------------------- * SPI_destruct * Destructor for SPI interface in argument "self" * * @param self SPI to destruct * * @return ErrorStatus SUCCESS if destruct was successful * ERROR otherwise * * @todo * ----------------------------------------------------------------------------- */ extern ErrorStatus SPI_destruct(struct Spi* self); /** ---------------------------------------------------------------------------- * Spi_getDefaultParameters * Function that assigns default parameters to the spi struct * * @param parameters * * @return ErrorStatus SUCCESS if destruct was successful * ERROR otherwise * * @todo * ----------------------------------------------------------------------------- */ extern ErrorStatus SPI_getDefaultParameters(struct SpiParameters* parameters); /** ---------------------------------------------------------------------------- * Spi_Write * Write the data in buffer to the SPI in argument self * * @param self The UART class object * @param buffer Message string to send * @parm length Message length * * @return ErrorStatus SUCCESS if writing message was successful * ERROR otherwise * * @todo * ----------------------------------------------------------------------------- */ extern ErrorStatus SPI_write (struct SpiDevice* self, const uint8_t* buffer, int length); #endif /* MISC_INC_SPI_H_ */