37 #ifndef SPI_H_INCLUDED 38 #define SPI_H_INCLUDED 51 #define SPI_TIMEOUT 15000 81 #define spi_get_pcs(chip_sel_id) ((~(1u<<(chip_sel_id)))&0xF) 333 spi_status_t
spi_read(
Spi *p_spi, uint16_t *us_data, uint8_t *p_pcs);
334 spi_status_t
spi_write(
Spi *p_spi, uint16_t us_data, uint8_t uc_pcs,
490 uint32_t ul_polarity);
493 uint32_t ul_cs_behavior);
497 uint8_t uc_baudrate_divider);
501 #if (SAM3S || SAM3N || SAM4S || SAM4E || SAM4N || SAM4C || SAMG || SAM4CP || SAM4CM) 509 static inline Pdc *spi_get_pdc_base(
Spi *p_spi)
511 return (Pdc *)&(p_spi->SPI_RPR);
515 #if (SAM3U || SAM3XA || SAMV71 || SAMV70 || SAME70 || SAMS70) 523 static inline void *spi_get_tx_access(
Spi *p_spi)
525 return (
void *)&(p_spi->
SPI_TDR);
535 static inline void *spi_get_rx_access(
Spi *p_spi)
537 return (
void *)&(p_spi->
SPI_RDR);
__I uint32_t SPI_SR
(Spi Offset: 0x10) Status Register
static void spi_set_master_mode(Spi *p_spi)
Set SPI to Master mode.
#define SPI_MR_WDRBT
(SPI_MR) Wait Data Read Before Transfer
void spi_set_clock_phase(Spi *p_spi, uint32_t ul_pcs_ch, uint32_t ul_phase)
Set Data Capture Phase.
static uint32_t spi_is_enabled(Spi *p_spi)
Test if the SPI is enabled.
void spi_configure_cs_behavior(Spi *p_spi, uint32_t ul_pcs_ch, uint32_t ul_cs_behavior)
Configure CS behavior for SPI transfer (spi_cs_behavior_t).
#define SPI_MR_MODFDIS
(SPI_MR) Mode Fault Detection
static void spi_enable(Spi *p_spi)
Enable SPI.
static uint32_t spi_get_mode_fault_detect_setting(Spi *p_spi)
Check if mode fault detection is enabled.
#define SPI_CR_SPIEN
(SPI_CR) SPI Enable
__I uint32_t SPI_IMR
(Spi Offset: 0x1C) Interrupt Mask Register
__I uint32_t SPI_RDR
(Spi Offset: 0x08) Receive Data Register
#define SPI_CR_SPIDIS
(SPI_CR) SPI Disable
#define SPI_MR_MSTR
(SPI_MR) Master/Slave Mode
static void spi_enable_loopback(Spi *p_spi)
Enable loopback mode.
spi_status_t spi_read(Spi *p_spi, uint16_t *us_data, uint8_t *p_pcs)
Read the received data and it's peripheral chip select value. While SPI works in fixed peripheral sel...
int16_t spi_set_baudrate_div(Spi *p_spi, uint32_t ul_pcs_ch, uint8_t uc_baudrate_divider)
Set Serial Clock Baud Rate divider value (SCBR).
#define SPI_MR_PCSDEC
(SPI_MR) Chip Select Decode
static uint32_t spi_get_tx_on_rx_empty_setting(Spi *p_spi)
Check if SPI waits RX_EMPTY before transfer starts.
static void spi_enable_interrupt(Spi *p_spi, uint32_t ul_sources)
Enable SPI interrupts.
enum spi_cs_behavior spi_cs_behavior_t
static uint32_t spi_is_rx_full(Spi *p_spi)
Check if the SPI contains a received character.
static uint32_t spi_get_mode(Spi *p_spi)
Get SPI work mode.
void spi_set_bits_per_transfer(Spi *p_spi, uint32_t ul_pcs_ch, uint32_t ul_bits)
Set number of bits per transfer.
#define SPI_CSR_CSNAAT
(SPI_CSR[4]) Chip Select Not Active After Transfer (Ignored if CSAAT = 1)
static uint32_t spi_get_peripheral_select_decode_setting(Spi *p_spi)
Get Peripheral Select Decode mode.
void spi_set_clock_polarity(Spi *p_spi, uint32_t ul_pcs_ch, uint32_t ul_polarity)
Set clock default state.
static void spi_disable_interrupt(Spi *p_spi, uint32_t ul_sources)
Disable SPI interrupts.
#define SPI_CR_SWRST
(SPI_CR) SPI Software Reset
#define SPI_TDR_TD(value)
__O uint32_t SPI_IER
(Spi Offset: 0x14) Interrupt Enable Register
static uint32_t spi_read_status(Spi *p_spi)
Read status register.
#define SPI_MR_LLB
(SPI_MR) Local Loopback Enable
Commonly used includes, types and macros.
#define SPI_MR_PS
(SPI_MR) Peripheral Select
static void spi_reset(Spi *p_spi)
Reset SPI and set it to Slave mode.
static uint32_t spi_is_tx_ready(Spi *p_spi)
Check if all transmissions are ready.
static void spi_disable_loopback(Spi *p_spi)
Disable loopback mode.
#define SPI_SR_SPIENS
(SPI_SR) SPI Enable Status
void spi_enable_clock(Spi *p_spi)
Enable SPI clock.
static void spi_disable_tx_on_rx_empty(Spi *p_spi)
Disable waiting RX_EMPTY before transfer starts.
__O uint32_t SPI_TDR
(Spi Offset: 0x0C) Transmit Data Register
spi_status_t spi_write(Spi *p_spi, uint16_t us_data, uint8_t uc_pcs, uint8_t uc_last)
Write the transmitted data with specified peripheral chip select value.
#define SPI_SR_TDRE
(SPI_SR) Transmit Data Register Empty (cleared by writing SPI_TDR)
void spi_disable_clock(Spi *p_spi)
Disable SPI clock.
static void spi_disable_peripheral_select_decode(Spi *p_spi)
Disable Peripheral Select Decode.
static void spi_disable_mode_fault_detect(Spi *p_spi)
Disable Mode Fault Detection.
void spi_set_peripheral_chip_select_value(Spi *p_spi, uint32_t ul_value)
Set Peripheral Chip Select (PCS) value.
USBInterfaceDescriptor data
__O uint32_t SPI_IDR
(Spi Offset: 0x18) Interrupt Disable Register
static uint32_t spi_read_interrupt_mask(Spi *p_spi)
Read SPI interrupt mask.
static void spi_put(Spi *p_spi, uint16_t data)
Put one data to a SPI peripheral.
int16_t spi_calc_baudrate_div(const uint32_t baudrate, uint32_t mck)
Calculate the baudrate divider.
static uint32_t spi_is_rx_ready(Spi *p_spi)
Check if all receptions are ready.
static uint32_t spi_is_tx_empty(Spi *p_spi)
Check if all transmissions are complete.
static void spi_set_fixed_peripheral_select(Spi *p_spi)
Set Fixed Peripheral Select. Peripheral Chip Select is controlled by SPI_MR.
void spi_set_transfer_delay(Spi *p_spi, uint32_t ul_pcs_ch, uint8_t uc_dlybs, uint8_t uc_dlybct)
Configure timing for SPI transfer.
static void spi_enable_mode_fault_detect(Spi *p_spi)
Enable Mode Fault Detection.
static void spi_enable_peripheral_select_decode(Spi *p_spi)
Enable Peripheral Select Decode.
void spi_set_delay_between_chip_select(Spi *p_spi, uint32_t ul_delay)
Set delay between chip selects (in number of MCK clocks). If DLYBCS <= 6, 6 MCK clocks will be insert...
#define SPI_CR_LASTXFER
(SPI_CR) Last Transfer
void spi_set_writeprotect(Spi *p_spi, uint32_t ul_enable)
Enable or disable write protection of SPI registers.
#define SPI_SR_RDRF
(SPI_SR) Receive Data Register Full (cleared by reading SPI_RDR)
static uint16_t spi_get(Spi *p_spi)
Get one data to a SPI peripheral.
static void spi_set_variable_peripheral_select(Spi *p_spi)
Set Variable Peripheral Select. Peripheral Chip Select can be controlled by SPI_TDR.
static void spi_disable(Spi *p_spi)
Disable SPI.
static uint32_t spi_get_peripheral_select_mode(Spi *p_spi)
Get Peripheral Select mode.
#define SPI_RDR_RD_Msk
(SPI_RDR) Receive Data
static void spi_enable_tx_on_rx_empty(Spi *p_spi)
Enable waiting RX_EMPTY before transfer starts.
__O uint32_t SPI_CR
(Spi Offset: 0x00) Control Register
#define SPI_SR_TXEMPTY
(SPI_SR) Transmission Registers Empty (cleared by writing SPI_TDR)
__IO uint32_t SPI_MR
(Spi Offset: 0x04) Mode Register
static void spi_set_slave_mode(Spi *p_spi)
Set SPI to Slave mode.
uint32_t spi_get_writeprotect_status(Spi *p_spi)
Indicate write protect status.
#define SPI_CSR_CSAAT
(SPI_CSR[4]) Chip Select Active After Transfer
static void spi_set_lastxfer(Spi *p_spi)
Issue a LASTXFER command. The next transfer is the last transfer and after that CS is de-asserted...