Driver API for SPI Peripheral.
The functions and other declarations used in this part of the driver are in cy_scb_spi.h. You can also include cy_pdl.h to get access to all functions and declarations in the PDL.
The SPI protocol is a synchronous serial interface protocol. Devices operate in either master or slave mode. The master initiates the data transfer. The SCB supports single-master-multiple-slaves topology for SPI. Multiple slaves are supported with individual slave select lines.
Features:
The SPI driver configuration can be divided to number of sequential steps listed below:
To set up the SPI driver, provide the configuration parameters in the cy_stc_scb_spi_config_t structure. For example: provide spiMode, subMode, sclkMode, oversample, rxDataWidth, and txDataWidth. The other parameters are optional for operation. To initialize the driver, call Cy_SCB_SPI_Init function providing a pointer to the populated cy_stc_scb_spi_config_t structure and the allocated cy_stc_scb_spi_context_t structure.
Only dedicated SCB pins can be used for SPI operation. The HSIOM register must be configured to connect dedicated SCB SPI pins to the SCB block. Also, the SPI output pins must be configured in Strong Drive Input Off mode and SPI input pins in Digital High-Z.
A clock source must be connected to the SCB block to oversample input and output signals, in this document this clock will be referred as clk_scb. You must use one of the 8-bit or 16-bit dividers. Use the SysClk (System Clock) driver API to do this.
To get the SPI slave to operate with the desired data rate, the clk_scb must be fast enough to provide sufficient oversampling. Use the SysClk (System Clock) driver API to do that.
To get the SPI master to operate with the desired data rate, multiply the oversample factor by the desired data rate to determine the required frequency for clk_scb. Use the SysClk (System Clock) driver API to configure clk_scb frequency. Set the oversample parameter in configuration structure to define number of SCB clocks in one SCLK period. When this value is even, the first and second phases of the SCLK period are the same. Otherwise, the first phase is one SCB clock cycle longer than the second phase. The level of the first phase of the clock period depends on CPOL settings: 0 - low level and 1 - high level.
Refer to the technical reference manual (TRM) section SPI sub-section Oversampling and Bit Rate to get information about how to configure SPI to run with desired data rate.
The interrupt is optional for the SPI operation. To configure the interrupt, the Cy_SCB_SPI_Interrupt function must be called in the interrupt handler for the selected SCB instance. Also, this interrupt must be enabled in the NVIC. The interrupt must be configured when High-Level API will be used.
Finally, enable the SPI operation by calling Cy_SCB_SPI_Enable. For the slave, this means that SPI device starts responding to the transfers. For the master, it is ready to execute transfers.
The SPI API is the same for the master and slave mode operation and is divided into two categories: Low-Level and High-Level.
Do not mix High-Level and Low-Level API because a Low-Level API can adversely affect the operation of a High-Level API.
The Low-Level functions allow interacting directly with the hardware and do not use Cy_SCB_SPI_Interrupt. These functions do not require context for operation. Thus, NULL can be passed for context parameter in Cy_SCB_SPI_Init and Cy_SCB_SPI_Disable instead of a pointer to the context structure.
The High-Level API use Cy_SCB_SPI_Interrupt to execute the transfer. Call Cy_SCB_SPI_Transfer to start communication: for master mode calling this function starts a transaction with the slave. For slave mode the read and write buffers are prepared for the communication with the master. After a transfer is started, the Cy_SCB_SPI_Interrupt handles the transfer until its completion. Therefore, the Cy_SCB_SPI_Interrupt function must be called inside the user interrupt handler to make the High-Level API work. To monitor the status of the transfer operation, use Cy_SCB_SPI_GetTransferStatus. Alternatively, use Cy_SCB_SPI_RegisterCallback to register a callback function to be notified about SPI Callback Events.
The SCB provides TX and RX output trigger signals that can be routed to the DMA controller inputs. These signals are assigned based on the data availability in the TX and RX FIFOs appropriately.
To route SCB TX or RX trigger signals to the DMA controller, use TrigMux (Trigger Multiplexer) driver API.
The SPI driver provides callback functions to handle power mode transitions. The callback Cy_SCB_SPI_DeepSleepCallback must be called during execution of Cy_SysPm_CpuEnterDeepSleep; Cy_SCB_SPI_HibernateCallback must be called during execution of Cy_SysPm_SystemEnterHibernate. To trigger the callback execution, the callback must be registered before calling the power mode transition function. Refer to SysPm (System Power Management) driver for more information about power mode transitions and callback registration.
The SPI master is disabled during Deep Sleep and Hibernate and stops driving the output pins. The state of the SPI master output pins SCLK, SS, and MOSI is High-Z, which can cause unexpected behavior of the SPI Slave due to possible glitches on these lines. These pins must keep the inactive level (the same state when SPI master is enabled and does not transfer data) before entering Deep Sleep or Hibernate mode. To do that, write the GPIO data register of each pin to the inactive level for each output pin. Then configure High-Speed Input Output Multiplexer (HSIOM) of each pin to be controlled by the GPIO (use GPIO (General Purpose Input Output) driver API). After after exiting Deep Sleep mode the SPI master must be enabled and the pins configuration restored to return the SPI master control of the pins (after exiting Hibernate mode, the system initialization code does the same). Copy either or both Cy_SCB_SPI_DeepSleepCallback and Cy_SCB_SPI_HibernateCallback as appropriate, and make the changes described above inside the function. Alternately, external pull-up or pull-down resistors can be connected to the appropriate SPI lines to keep them inactive during Deep-Sleep or Hibernate.
API Reference | |
Macros | |
Functions | |
Data Structures | |
Enumerated Types | |