It's not complete or tested but I post it here to document the progress and comments.
It started here viewtopic.php?f=9&t=1686&start=30 but is now more firmware.
Code: Select all
typedef struct
{
uint16_t CS_Port_Bit; // Some sort of Port pin mapping for the Chip Select. Copy how Fred has mapped this.
uint8_t SPI_Bus; // Which of the 2 SPI buses do we want to use.
uint16_t Bus_Speed;
uint16_t Bus_Mode; // There may be other settings and these will be defined when I get into the code.
} SPI_Settings_t;
/** @brief Transmit and Receive bytes over the SPI Bus
*
* This will Send and receive a number of bytes over the SPI Bus
* It will use the data in pSettings to configure the bus.
* This function will use interrupts to complete the transfer
* and will return immediately. Use SPI_Complete() to check
* if the transfer is complete.
*
*
* @author
*
* @warning Do not call this function if a transfer is in progress.
* Use SPI_Complete() to check if the transfer is complete.
*
*
* @param pSettings pointer to setup information for the SPI Bus.
* @param pTxBuffer pointer to a buffer holding the the bytes to transmit.
* @param pRxBuffer pointer to a buffer were the received bytes will be stored.
* @param TxCount The number of bytes to send from the TxBuffer
* @param RxCount The number of bytes to receive and store in RxBuffer
*
*
* @return An error code. Zero means success, anything else is a failure.
*/
uint8_t SPI_Read_Write(SPI_Settings_t *pSettings,uint8_t *pTxBuffer, uint8_t *pRxBuffer, uint16_t TxCount, uint16_t RxCount);
/** @brief Check on the status of the SPI transfer
*
* Calling this function will return the status of the
* interrupt transfer.
*
*
* @author
*
* @warning
*
* @param SPI_Bus Which SPI Bus to check.
*
* @return Zero means Busy. One means complete no error. anything else is a failure.
*/
uint8_t SPI_Complete(uint8_t SPI_Bus);
/** @brief Transmit and Receive bytes over the SPI Bus and wait till complete
*
* This will Send and receive a number of bytes over the SPI Bus
* It will use the data in pSettings to configure the bus.
*
*
* @author
*
* @warning
*
*
* @param pSettings pointer to setup information for the SPI Bus.
* @param pTxBuffer pointer to a buffer holding the the bytes to transmit.
* @param pRxBuffer pointer to a buffer were the received bytes will be stored.
* @param TxCount The number of bytes to send from the TxBuffer
* @param RxCount The number of bytes to receive and store in RxBuffer
*
*
* @return An error code. Zero means success, anything else is a failure.
*/
uint8_t SPI_Read_Write_Blocking(SPI_Settings_t * pSettings,uint8_t *pTxBuffe, uint8_t *pRxBuffer, uint16_t BytesToSend, uint16_t BytesToGet)
{
uint8_t Result;
Result = SPI_Read_Write(pSettings,*pTxBuffer,pRxBuffer,TxCount, RxCount);
if (Result)
{
return (Result);
}
while (SPI_Complete(pSettings->SPI_Bus) == 0); // Wait for transfer to complete
return (SPI_Complete(pSettings->SPI_Bus));
}