SPI_MASTER: SPI_MASTER.c File Reference

SPI_MASTER

SPI_MASTER.c File Reference

Detailed Description

Date
2018-06-20

NOTE: This file is generated by DAVE. Any manual modification done to this file will be lost when the code is regenerated.

Definition in file SPI_MASTER.c.

#include "spi_master.h"

Functions

DAVE_APP_VERSION_t SPI_MASTER_GetAppVersion ()
 Get SPI_MASTER APP version. More...
 
SPI_MASTER_STATUS_t SPI_MASTER_Init (SPI_MASTER_t *const handle)
 Initialize the SPI channel as per the configuration made in GUI. More...
 
SPI_MASTER_STATUS_t SPI_MASTER_SetMode (SPI_MASTER_t *const handle, const XMC_SPI_CH_MODE_t mode)
 Set the communication mode along with required port configuration. More...
 
SPI_MASTER_STATUS_t SPI_MASTER_SetBaudRate (SPI_MASTER_t *const handle, const uint32_t baud_rate)
 Set the required baud rate during runtime. More...
 
SPI_MASTER_STATUS_t SPI_MASTER_Transmit (const SPI_MASTER_t *const handle, uint8_t *dataptr, uint32_t count)
 Transmits the specified number of data words and execute the callback defined in GUI, if enabled. More...
 
SPI_MASTER_STATUS_t SPI_MASTER_Receive (const SPI_MASTER_t *const handle, uint8_t *dataptr, uint32_t count)
 Receives the specified number of data words and execute the callback defined in GUI, if enabled. More...
 
SPI_MASTER_STATUS_t SPI_MASTER_Transfer (const SPI_MASTER_t *const handle, uint8_t *tx_dataptr, uint8_t *rx_dataptr, uint32_t count)
 Transmits and Receives the specified number of data words and execute the receive callback if it is enabled in GUI. More...
 
SPI_MASTER_STATUS_t SPI_MASTER_AbortReceive (const SPI_MASTER_t *const handle)
 Stops the active data reception request. More...
 
SPI_MASTER_STATUS_t SPI_MASTER_AbortTransmit (const SPI_MASTER_t *const handle)
 Aborts the ongoing data transmission. More...
 

Function Documentation

SPI_MASTER_STATUS_t SPI_MASTER_AbortReceive ( const SPI_MASTER_t *const  handle)

Stops the active data reception request.

Parameters
handlePointer to static and dynamic content of APP configuration.
Returns
None
Description:
If a reception is in progress, it will be stopped. When a reception request is active, user will not be able to place a new receive request till the active reception is complete. This API can stop the progressing reception to make a new receive request.

Example Usage:

#include <DAVE.h> //Declarations from DAVE Code Generation (includes SFR declaration)
//Description:
//Transmits the string "Infineon DAVE application" to the slave.
//Starts to receive data from slave, checks if the first byte is 0x55.
//If so, aborts the reception and retransmits 0x55 to slave.
int main(void)
{
DAVE_STATUS_t status;
uint8_t Send_Data[] = "Infineon DAVE application.";
uint8_t Rec_Data[64];
status = DAVE_Init(); // SPI_MASTER_Init() is called from DAVE_Init()
if(status == DAVE_STATUS_SUCCESS)
{
SPI_MASTER_Transmit(&SPI_MASTER_0, Send_Data, sizeof(Send_Data));
while(SPI_MASTER_0.runtime->tx_busy);
SPI_MASTER_Receive(&SPI_MASTER_0, Rec_Data, 15U);
if(SPI_MASTER_0.runtime->rx_data[0] == 0x55)
{
SPI_MASTER_AbortReceive(&SPI_MASTER_0);
SPI_MASTER_Transmit(&SPI_MASTER_0, Rec_Data, 1);
}
}
else
{
XMC_DEBUG("main: Application initialization failed");
while(1U)
{
}
}
return 1U;
}

Definition at line 813 of file SPI_MASTER.c.

References SPI_MASTER::channel, SPI_MASTER::config, SPI_MASTER_CONFIG::receive_mode, SPI_MASTER::runtime, SPI_MASTER_RUNTIME::rx_busy, SPI_MASTER_RUNTIME::rx_data, SPI_MASTER_CONFIG::rx_fifo_size, SPI_MASTER_AbortTransmit(), SPI_MASTER_STATUS_FAILURE, SPI_MASTER_STATUS_SUCCESS, SPI_MASTER_TRANSFER_MODE_DIRECT, SPI_MASTER_TRANSFER_MODE_DMA, and SPI_MASTER_RUNTIME::tx_data_dummy.

SPI_MASTER_STATUS_t SPI_MASTER_AbortTransmit ( const SPI_MASTER_t *const  handle)

Aborts the ongoing data transmission.

Parameters
handlePointer to static and dynamic content of APP configuration.
Returns
None
Description:
If there is a transmission in progress, it will be stopped. If transmit FIFO is used, the existing data will be flushed. After the transmission is stopped, user can start a new transmission without delay.

Example Usage:

#include <DAVE.h> //Declarations from DAVE Code Generation (includes SFR declaration)
//Description:
//Transmits test data from buffer Send_Data and aborts it immediately.
//Retransmits data from NewData.
int main(void)
{
DAVE_STATUS_t status;
uint8_t Send_Data[] = "Infineon DAVE application.";
uint8_t NewData[] = "New data message";
status = DAVE_Init(); // SPI_MASTER_Init() is called from DAVE_Init()
if(status == DAVE_STATUS_SUCCESS)
{
SPI_MASTER_Transmit(&SPI_MASTER_0, Send_Data, sizeof(Send_Data));
if(SPI_MASTER_0.runtime->tx_busy)
{
SPI_MASTER_AbortTransmit(&SPI_MASTER_0);
SPI_MASTER_Transmit(&SPI_MASTER_0, NewData, sizeof(NewData));
}
}
else
{
XMC_DEBUG("main: Application initialization failed");
while(1U)
{
}
}
return 1U;
}

Definition at line 871 of file SPI_MASTER.c.

References SPI_MASTER::channel, SPI_MASTER::config, SPI_MASTER::runtime, SPI_MASTER_STATUS_FAILURE, SPI_MASTER_STATUS_SUCCESS, SPI_MASTER_TRANSFER_MODE_DIRECT, SPI_MASTER_TRANSFER_MODE_DMA, SPI_MASTER_CONFIG::transmit_mode, SPI_MASTER_RUNTIME::tx_busy, SPI_MASTER_RUNTIME::tx_data, SPI_MASTER_RUNTIME::tx_data_dummy, and SPI_MASTER_CONFIG::tx_fifo_size.

Referenced by SPI_MASTER_AbortReceive().

SPI_MASTER_STATUS_t SPI_MASTER_Init ( SPI_MASTER_t *const  handle)

Initialize the SPI channel as per the configuration made in GUI.

Parameters
handlePointer to static and dynamic content of APP configuration.
Returns
SPI_MASTER_STATUS_t: Status of SPI_MASTER driver initialization.
SPI_MASTER_STATUS_SUCCESS - on successful initialization.
SPI_MASTER_STATUS_FAILURE - if initialization fails.
Description:
Initializes IO pins used for the SPI_MASTER communication and configures USIC registers based on the settings provided in the GUI. Calculates divider values PDIV and STEP for a precise baudrate. It also enables configured interrupt flags and service request values.

Example Usage:

#include <DAVE.h> //Declarations from DAVE Code Generation (includes SFR declaration)
int main(void)
{
DAVE_STATUS_t status;
status = DAVE_Init(); // SPI_MASTER_Init() is called from DAVE_Init()
if(status == DAVE_STATUS_SUCCESS)
{
while(1U)
{
}
}
else
{
XMC_DEBUG("main: Application initialization failed");
while(1U)
{
}
}
return 1U;
}

Definition at line 201 of file SPI_MASTER.c.

References SPI_MASTER::config, and SPI_MASTER_CONFIG::fptr_spi_master_config.

SPI_MASTER_STATUS_t SPI_MASTER_Receive ( const SPI_MASTER_t *const  handle,
uint8_t *  dataptr,
uint32_t  count 
)

Receives the specified number of data words and execute the callback defined in GUI, if enabled.

Parameters
handlePointer to static and dynamic content of APP configuration.
dataptrPointer to data in which value is written
countnumber of data words (word length configured) to be read
Returns
SPI_MASTER_STATUS_t SPI_MASTER_STATUS_SUCCESS : if read is successful
SPI_MASTER_STATUS_BUSY : if SPI channel is busy with other operation
Description:
Data will be received from the SPI slave synchronously. After the requested number of data bytes are received, optionally, the user configured callback function will be executed. Data reception is accomplished using the receive mode selected in the UI. Interrupt:
Based on the UI configuration, either standard receive buffer(RBUF) or receive FIFO(OUT) is used for data reception. An interrupt is configured for reading received data from the bus. This function only registers a request to receive a number of data bytes from a USIC channel. If FIFO is configured for reception, the FIFO limit is dynamically configured to optimally utilize the CPU load. Before starting data reception, the receive buffers are flushed. So only those data, received after calling the API, will be placed in the user buffer. When all the requested number of data bytes are received, the configured callback function will be executed. If a callback function is not configured, the user has to poll for the value of the variable, handle->runtime->rx_busy to be false. The value is updated to false when all the requested number of data bytes are received.
DMA:
DMA mode is available only in XMC4x family of microcontrollers. In this mode, a DMA channel is configured for receiving data from standard receive buffer(RBUF) to the user buffer. By calling this API, the DMA channel destination address is configured to the user buffer and the channel is enabled. Receive FIFO will not be used when the receive mode is DMA. Before starting data reception, the receive buffers are flushed. So only those data, received after calling the API, will be placed in the user buffer. When all the requested number of data bytes are received, the configured callback function will be executed. If a callback function is not configured, the user has to poll for the value of the variable, handle->runtime->rx_busy to be false. The value is updated to false when all the requested number of data bytes are received.
Direct
In Direct receive mode, neither interrupt nor DMA is used. The API polls the receive flag to read the received data and waits for all the requested number of bytes to be received. Based on FIFO configuration, either RBUF or OUT register is used for reading received data. Before starting data reception, the receive buffers are flushed. So only those data, received after calling the API, will be placed in the user buffer. Note: In Direct mode, the API blocks the CPU until the count of bytes requested is received.

Example Usage:

#include <DAVE.h>
//Description:
//Receives 10 bytes of data from slave.
int main(void)
{
DAVE_STATUS_t status;
uint8_t ReadData[10];
status = DAVE_Init(); // SPI_MASTER_Init() is called from DAVE_Init()
if(status == DAVE_STATUS_SUCCESS)
{
if(SPI_MASTER_Receive(&SPI_MASTER_0, ReadData, 10U))
{
while(SPI_MASTER_0.runtime->rx_busy)
{
}
}
}
else
{
XMC_DEBUG("main: Application initialization failed");
while(1U)
{
}
}
return 1U;
}


Definition at line 322 of file SPI_MASTER.c.

References SPI_MASTER::config, SPI_MASTER_CONFIG::receive_mode, SPI_MASTER_STATUS_FAILURE, SPI_MASTER_TRANSFER_MODE_DIRECT, SPI_MASTER_TRANSFER_MODE_DMA, and SPI_MASTER_TRANSFER_MODE_INTERRUPT.

SPI_MASTER_STATUS_t SPI_MASTER_SetBaudRate ( SPI_MASTER_t *const  handle,
const uint32_t  baud_rate 
)

Set the required baud rate during runtime.

Parameters
handlehandle Pointer to static and dynamic content of APP configuration.
baud_raterequired baud rate
Returns
SPI_MASTER_STATUS_t SPI_MASTER_STATUS_SUCCESS : if updation of baud rate is successful
SPI_MASTER_STATUS_FAILURE : if updation is failed
SPI_MASTER_STATUS_BUSY : if SPI channel is busy with other operation

Description:
While setting the baud rate to avoid noise of the port pins, all the pins are changed to input. After setting the required baud again ports are initialised with the configured settings.

Example Usage:

#include <DAVE.h>
//Description:
//The following code changes the SPI master baud rate to 9600 and starts sending the data stored in
//the buffer.
int main(void)
{
DAVE_STATUS_t status;
SPI_MASTER_STATUS_t spi_status;
uint8_t Send_Data[] = "Infineon DAVE application.";
uint32_t baud_rate;
status = DAVE_Init(); // SPI_MASTER_Init() is called from DAVE_Init()
if(status == DAVE_STATUS_SUCCESS)
{
baud_rate = 9600U;
spi_status = SPI_MASTER_SetBaudRate(&SPI_MASTER_0, baud_rate);
if(spi_status == SPI_MASTER_STATUS_SUCCESS)
{
SPI_MASTER_Transmit(&SPI_MASTER_0, Send_Data, sizeof(Send_Data));
}
}
else
{
XMC_DEBUG("main: Application initialization failed");
while(1U)
{
}
}
return 1U;
}


Definition at line 250 of file SPI_MASTER.c.

References SPI_MASTER::channel, SPI_MASTER::config, SPI_MASTER_CONFIG::leading_trailing_delay, SPI_MASTER::runtime, SPI_MASTER_RUNTIME::rx_busy, SPI_MASTER_CONFIG::shift_clk_passive_level, SPI_MASTER_STATUS_BUSY, SPI_MASTER_STATUS_SUCCESS, and SPI_MASTER_RUNTIME::tx_busy.

SPI_MASTER_STATUS_t SPI_MASTER_SetMode ( SPI_MASTER_t *const  handle,
const XMC_SPI_CH_MODE_t  mode 
)

Set the communication mode along with required port configuration.

Parameters
handlehandle Pointer to static and dynamic content of APP configuration.
modeSPI working mode
Returns
SPI_MASTER_STATUS_t SPI_MASTER_STATUS_SUCCESS : if updation of settings are successful
SPI_MASTER_STATUS_FAILURE : if mode is not supported by the selected pins
SPI_MASTER_STATUS_BUSY : if SPI channel is busy with transmit or receive operation

Description:
To change the mode of communication, it is advised to generate the code in Quad/Dual mode initially. Then changing the mode will be taken care by the APP.
  • If code is generated for Quad mode, it is possible to change to other modes like Dual, Half Duplex and Full Duplex
  • If code is generated for Dual mode, it is possible to change to other modes like Half Duplex and Full Duplex only
  • If code is generated for full-duplex mode, it is possible to change to Half Duplex only


Example Usage:

#include <DAVE.h>
//Precondition:
//Configure the SPI_MASTER APP operation mode as 'Quad SPI'.
//Description:
//The following code changes the SPI master device mode to Full duplex mode and starts sending the data stored in
//the buffer.
int main(void)
{
DAVE_STATUS_t status;
SPI_MASTER_STATUS_t spi_status;
uint8_t Send_Data[] = "Infineon DAVE application.";
status = DAVE_Init(); // SPI_MASTER_Init() is called from DAVE_Init()
if(status == DAVE_STATUS_SUCCESS)
{
spi_status = SPI_MASTER_SetMode(&SPI_MASTER_0, XMC_SPI_CH_MODE_STANDARD);
if(spi_status == SPI_MASTER_STATUS_SUCCESS)
{
SPI_MASTER_Transmit(&SPI_MASTER_0, Send_Data, sizeof(Send_Data));
}
}
else
{
XMC_DEBUG("main: Application initialization failed");
while(1U)
{
}
}
return 1U;
}


Definition at line 216 of file SPI_MASTER.c.

References SPI_MASTER::runtime, SPI_MASTER_RUNTIME::rx_busy, SPI_MASTER_RUNTIME::spi_master_mode, SPI_MASTER_STATUS_BUSY, SPI_MASTER_STATUS_SUCCESS, and SPI_MASTER_RUNTIME::tx_busy.

SPI_MASTER_STATUS_t SPI_MASTER_Transfer ( const SPI_MASTER_t *const  handle,
uint8_t *  tx_dataptr,
uint8_t *  rx_dataptr,
uint32_t  count 
)

Transmits and Receives the specified number of data words and execute the receive callback if it is enabled in GUI.

Parameters
handlePointer to static and dynamic content of APP configuration.
tx_dataptrPointer to data buffer which has to be send
rx_dataptrPointer to data buffer where the received data has to be stored.
countnumber of data words (word length configured) to be read and write
Returns
SPI_MASTER_STATUS_t SPI_MASTER_STATUS_SUCCESS : if transfer of data is successful
SPI_MASTER_STATUS_FAILURE : if transfer of data is failed (or) in other than standard full duplex mode SPI_MASTER_STATUS_BUFFER_INVALID : if passed buffers are NULL pointers (or) length of data transfer is zero.
Description:
Transmits and receives data simultaneously using the SPI channel as a master device. API is applicable only in Full duplex</> operation mode. Data transfer happens based on the individual modes configured for transmission and reception.
Two data pins MOSI and MISO will be used for receiving and transmitting data respectively. A callback function can be configured to execute after completing the transfer when 'Interrupt' or 'DMA' mode is used. The callback function should be configured for
End of receive/transfer callback in the 'Interrupt Settings' tab. The callback function will be executed when the last word of data is received.


Example Usage:

#include <DAVE.h>
//Precondition: Operation mode should be 'Full Duplex"
//Description:
//Transmits and Receives 10 bytes of data from slave in parallel.
int main(void)
{
DAVE_STATUS_t status;
uint8_t ReadData[10];
uint8_t SendData[10] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA};
status = DAVE_Init(); // SPI_MASTER_Init() is called from DAVE_Init()
if(status == DAVE_STATUS_SUCCESS)
{
SPI_MASTER_Transfer(&SPI_MASTER_0, SendData, ReadData, 10);
}
else
{
XMC_DEBUG("main: Application initialization failed");
while(1U)
{
}
}
return 1U;
}


Definition at line 747 of file SPI_MASTER.c.

References SPI_MASTER::config, SPI_MASTER_CONFIG::receive_mode, SPI_MASTER::runtime, SPI_MASTER_RUNTIME::rx_busy, SPI_MASTER_RUNTIME::rx_data, SPI_MASTER_RUNTIME::rx_data_dummy, SPI_MASTER_RUNTIME::spi_master_mode, SPI_MASTER_STATUS_BUFFER_INVALID, SPI_MASTER_STATUS_BUSY, SPI_MASTER_STATUS_FAILURE, SPI_MASTER_TRANSFER_MODE_DIRECT, SPI_MASTER_TRANSFER_MODE_DMA, SPI_MASTER_TRANSFER_MODE_INTERRUPT, SPI_MASTER_RUNTIME::tx_busy, SPI_MASTER_RUNTIME::tx_data, SPI_MASTER_RUNTIME::tx_data_count, and SPI_MASTER_RUNTIME::tx_data_dummy.

SPI_MASTER_STATUS_t SPI_MASTER_Transmit ( const SPI_MASTER_t *const  handle,
uint8_t *  dataptr,
uint32_t  count 
)

Transmits the specified number of data words and execute the callback defined in GUI, if enabled.

Parameters
handlePointer to static and dynamic content of APP configuration.
dataptrPointer to data
countnumber of data words (word length configured) to be transmitted
Returns
SPI_MASTER_STATUS_t SPI_MASTER_STATUS_SUCCESS : if transmit is successful
SPI_MASTER_STATUS_BUSY : if SPI channel is busy with other operation
Description:
Transmits data using the SPI channel as a master device. Transmission is accomplished using the transmit mode as configured in the UI.
Interrupt:
The data transmission is accomplished using transmit interrupt. User can configure a callback function in the APP UI. When the data is fully transmitted, the callback function will be executed. If transmit FIFO is enabled, the trigger limit is set to 1. So the transmit interrupt will be generated when all the data in FIFO is moved out of FIFO. The APP handle's runtime structure is used to store the data pointer, count, data index and status of transmission. This function only registers a data transmission request if there is no active transmission in progress. Actual data transmission happens in the transmit interrupt service routine. A trigger is generated for the transmit interrupt to start loading the data to the transmit buffer. If transmit FIFO is configured, the data is filled into the FIFO. Transmit interrupt will be generated subsequently when the transmit FIFO is empty. At this point of time, if there is some more data to be transmitted, it is loaded to the FIFO again. When FIFO is not enabled, data is transmitted one byte at a time. On transmission of each byte an interrupt is generated and the next byte is transmitted in the interrupt service routine. Callback function is executed when all the data bytes are transmitted. If a callback function is not configured, user has to poll for the value of tx_busy flag of the APP handle structure( handle->runtime->tx_busy ) to check for the completion of data transmission or use SPI_MASTER_IsTxBusy() API.
DMA:
DMA mode is available only in XMC4x family of microcontrollers. A DMA channel is configured to provide data to the SPI channel transmit buffer. This removes the load off the CPU. This API will only configure and enable the DMA channel by specifying the data buffer and count of bytes to transmit. Rest is taken care without the CPU's intervention. User can configure a callback function in the APP UI. When the transmission is complete, the callback function will be executed. FIFO will not be used in DMA mode. Receive start interrupt is configured for triggering the DMA channel. So each byte is transmitted in the background through the DMA channel. If the callback function is not configured, handle->runtime->tx_busy flag can be checked to verify if the transmission is complete. Direct:
Data will be transmitted using polling method. Status flags are used to check if data can be transmitted. Note: In Direct mode, the API blocks the CPU until the count of bytes requested is transmitted.

Example Usage:

#include <DAVE.h>
//Description:
//Transmits "Infineon" to the slave device.
int main(void)
{
DAVE_STATUS_t status;
uint8_t Send_Data[] = "Infineon";
status = DAVE_Init(); // SPI_MASTER_Init() is called from DAVE_Init()
if(status == DAVE_STATUS_SUCCESS)
{
if(SPI_MASTER_Transmit(&SPI_MASTER_0, Send_Data, sizeof(Send_Data)) == SPI_MASTER_STATUS_SUCCESS)
{
while(SPI_MASTER_0.runtime->tx_busy)
{
}
}
}
else
{
XMC_DEBUG("main: Application initialization failed");
while(1U)
{
}
}
return 1U;
}


Definition at line 292 of file SPI_MASTER.c.

References SPI_MASTER::config, SPI_MASTER_STATUS_FAILURE, SPI_MASTER_TRANSFER_MODE_DIRECT, SPI_MASTER_TRANSFER_MODE_DMA, SPI_MASTER_TRANSFER_MODE_INTERRUPT, and SPI_MASTER_CONFIG::transmit_mode.

Go to the source code of this file.