| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- /*! ----------------------------------------------------------------------------
- * @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; i<headerLength; i++)
- {
- BSP_SPIx->DR = headerBuffer[i];
- while (port_SPIx_no_data());
- BSP_SPIx->DR ;
- }
- for(i=0; i<bodyLength; i++)
- {
- BSP_SPIx->DR = 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; i<headerLength; i++)
- {
- BSP_SPIx->DR = headerBuffer[i];
- while(port_SPIx_no_data());
- readBuffer[0] = BSP_SPIx->DR ; // Dummy read as we write the header
- }
- for(i=0; i<readlength; i++)
- {
- BSP_SPIx->DR = 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()
|