/*! ---------------------------------------------------------------------------- * @file deca_spi.c * @brief SPI access functions * * @attention * * Copyright 2015 (c) DecaWave Ltd, Dublin, Ireland. * * All rights reserved. * * @author DecaWave */ #include "deca_spi.h" #include "deca_device_api.h" #include "port.h" #define DECAMAXDATALEN 1036 volatile uint8_t decaTxBuf[DECAMAXDATALEN] = {0}; int32_t writetospi_serial( uint16_t headerLength, const uint8_t *headerBuffer, uint32_t bodylength, const uint8_t *bodyBuffer ); int32_t readfromspi_serial( uint16_t headerLength, const uint8_t *headerBuffer, uint32_t readlength, uint8_t *readBuffer ); /*! ------------------------------------------------------------------------------------------------------------------ * Function: openspi() * * Low level abstract function to open and initialise access to the SPI device. * returns 0 for success, or -1 for error */ int openspi(/*SPI_TypeDef* SPIx*/) { // done by port.c, default SPI used is SPI1 return 0; } // end openspi() /*! ------------------------------------------------------------------------------------------------------------------ * Function: closespi() * * Low level abstract function to close the the SPI device. * returns 0 for success, or -1 for error */ int closespi(void) { while (port_SPIx_busy_sending()); //wait for tx buffer to empty port_SPIx_disable(); return 0; } // end closespi() /*! ------------------------------------------------------------------------------------------------------------------ * Function: writetospi() * * Low level abstract function to write to the SPI * Takes two separate byte buffers for write header and write data * returns 0 for success, or -1 for error */ #ifdef __GNUC__ #pragma GCC optimize ("O3") #elif defined(__ICCARM__) #pragma optimize=speed high #endif int32_t writetospi_serial ( uint16_t headerLength, const uint8_t *headerBuffer, uint32_t bodyLength, const uint8_t *bodyBuffer ) { decaIrqStatus_t stat ; stat = decamutexon() ; port_SPIx_CS_clear(); #if 0 int i=0; for(i=0; iDR = headerBuffer[i]; while (port_SPIx_no_data()); BSP_SPIx->DR ; } for(i=0; iDR = bodyBuffer[i]; while(port_SPIx_no_data()); BSP_SPIx->DR ; } #else /* HAL_SPI_Transmit(&DWM_SpiHandle, (uint8_t*)headerBuffer, headerLength, 1000); HAL_SPI_Transmit(&DWM_SpiHandle, (uint8_t*)bodyBuffer, bodyLength, 1000);*/ memcpy((void*)decaTxBuf, headerBuffer, headerLength); memcpy((void*)(decaTxBuf + headerLength), bodyBuffer, bodyLength); HAL_SPI_Transmit(&DWM_SpiHandle, (uint8_t*)decaTxBuf, headerLength+bodyLength, 1000); #endif port_SPIx_CS_set(); decamutexoff(stat) ; return 0; } // end writetospi() /*! ------------------------------------------------------------------------------------------------------------------ * Function: readfromspi() * * Low level abstract function to read from the SPI * Takes two separate byte buffers for write header and read data * returns the offset into read buffer where first byte of read data may be found, * or returns -1 if there was an error */ /*#ifdef __GNUC__ #pragma GCC optimize ("O3") #elif defined(__ICCARM__) #pragma optimize=speed high #endif*/ int32_t readfromspi_serial ( uint16_t headerLength, const uint8_t *headerBuffer, uint32_t readlength, uint8_t *readBuffer ) { decaIrqStatus_t stat ; stat = decamutexon() ; /* Wait for SPIx Tx buffer empty */ //while (port_SPIx_busy_sending()); port_SPIx_CS_clear(); #if 0 int i=0; for(i=0; iDR = headerBuffer[i]; while(port_SPIx_no_data()); readBuffer[0] = BSP_SPIx->DR ; // Dummy read as we write the header } for(i=0; iDR = 0; // Dummy write as we read the message body while(port_SPIx_no_data() ); readBuffer[i] = BSP_SPIx->DR ;//port_SPIx_receive_data(); //this clears RXNE bit } #else HAL_StatusTypeDef status; status = HAL_SPI_Transmit(&DWM_SpiHandle, (uint8_t*)headerBuffer, headerLength, 1000); status = HAL_SPI_Receive(&DWM_SpiHandle, readBuffer, readlength, 1000); #endif port_SPIx_CS_set(); decamutexoff(stat); return 0; } // end readfromspi()