📄 svc_spi.c
字号:
/************************************************************************************
* File : svc_spi.c *
* Programmer: Konrad Wei (Taiwan) *
* November 2006 *
*************************************************************************************
- 2006 November. Add Slave Mode
*************************************************************************************/
#include "svc_spi.h"
/*
*********************************************************************************************************
* Defintion
*********************************************************************************************************
*/
#define SPI_INTERRUPT_LEVEL 1
/*
*********************************************************************************************************
* Global Variable
*********************************************************************************************************
*/
/* AT91SAN7S64 Component Offset */
AT91PS_SPI s_pSpi = AT91C_BASE_SPI;
AT91PS_PIO s_pPio = AT91C_BASE_PIOA;
AT91PS_PMC s_pPMC = AT91C_BASE_PMC;
AT91PS_PDC s_pPDC = AT91C_BASE_PDC_SPI;
static char buff_spi_rx0[100]; ///< SPI RX Buffer 1
static char buff_spi_rx1[100]; ///< SPI RX Buffer 2
static U8 gbSpiRxBufIndex =0; ///< Ping-Pong Buffer Index
/*
*********************************************************************************************************
* Local Function Prototype
*********************************************************************************************************
*/
void spi_c_interrupt_handler(void);
/*
*********************************************************************************************************
* Interrupt ISR
*********************************************************************************************************
*/
/**
\breif spi_interrupt_handler SPI interrupt handler.
\date 2006.11.20
*/
void spi_c_interrupt_handler(void)
{
unsigned int status;
status = s_pSpi->SPI_SR;
if ( status & AT91C_SPI_RXBUFF ) {
if (gbSpiRxBufIndex == 0) {
s_pSpi->SPI_RPR = (unsigned int) buff_spi_rx1;
s_pSpi->SPI_RCR = 100;
gbSpiRxBufIndex = 1;
//* TODO: Do some procedure for processing data.
} else{
s_pSpi->SPI_RPR = (unsigned int) buff_spi_rx0;
s_pSpi->SPI_RCR = 100;
gbSpiRxBufIndex = 0;
//* TODO: Do some procedure for processing data.
}
}
if( status & AT91C_SPI_NSSR ) {
//* TODO: Do some procedure for processing data.
}
}
/*
*********************************************************************************************************
* FUNCTION
*********************************************************************************************************
*/
// setup usart1 in spi mode
/**
\brief svc_spi_init Initiate SPI
\param[in] bMaster 0:Slave >1:Master
\param[in] bBaudRate SPI Baud Rate
\param[in] bClkMode ClockPhase & Polarity
\return ERROR CODE
*/
void spi_init (void)
{
//set functionalite to pins:
//port0.11 -> NPCS0
//port0.12 -> MISO
//port0.13 -> MOSI
//port0.14 -> SPCK
s_pPio->PIO_PDR = SPI_CS0 | SPI_MISO | SPI_MOSI | SPI_SPCK;
s_pPio->PIO_ASR = SPI_CS0 | SPI_MISO | SPI_MOSI | SPI_SPCK;
s_pPio->PIO_BSR = 0;
//s_pPMC->PMC_PCER = AT91C_ID_SPI; //enable the clock of SPI
s_pPMC->PMC_PCER = 1 << AT91C_ID_SPI;
/**** Fixed mode ****/
s_pSpi->SPI_CR = 0x81; //SPI Enable, Sowtware reset
s_pSpi->SPI_CR = 0x01; //SPI Enable
#ifdef SPI_MASTER
s_pSpi->SPI_MR = 0xE0011; //Master mode, fixed select, disable decoder, FDIV=0 (MCK), PCS=1110
#else
s_pSpi->SPI_MR = 0xE0010; //Slave mode, fixed select, disable decoder, FDIV=0 (MCK), PCS=1110
#endif
s_pSpi->SPI_CSR[0] = 0x4A02; //8bit, CPOL=0, ClockPhase=1, SCLK = 200kHz
//* PDC DMA enable
s_pPDC->PDC_PTCR = AT91C_PDC_TXTEN | AT91C_PDC_RXTEN;
s_pSpi->SPI_PTCR = AT91C_PDC_TXTEN | AT91C_PDC_RXTEN;
/* Konrad Test 2006.10.11 */
s_pSpi->SPI_CSR[0] |= AT91C_SPI_CSAAT;
/* AIC Setting */
//* open SPI Interrupt
AT91F_AIC_ConfigureIt (AT91C_BASE_AIC, AT91C_ID_SPI, SPI_INTERRUPT_LEVEL,
AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL, spi_c_interrupt_handler);
AT91F_AIC_EnableIt (AT91C_BASE_AIC, AT91C_ID_SPI);
//* Set the PDC
AT91F_PDC_Open(AT91C_BASE_PDC_SPI);
s_pSpi->SPI_RPR = (unsigned int) buff_spi_rx0;
s_pSpi->SPI_RCR = 100; ///< DMA Buffer length setting
gbSpiRxBufIndex = 0; ///< Buf Ping-Pong Index
AT91F_SPI_EnableIt(s_pSpi, AT91C_SPI_RXBUFF|AT91C_SPI_NSSR);
}
/**
\brief svc_spi_SendByte Send Byte and Read Byte is same function. SPI's Specific
\param[in] data byte data
\return Get the Receive Data
*/
__ramfunc U8 svc_spi_SendByte(const U8 sendData)
{
int i=0;
unsigned int spib;
while((s_pSpi->SPI_SR & AT91C_SPI_TDRE) == 0); // Wait for the transfer to complete
s_pSpi->SPI_TDR = (sendData & 0xFFFF); // Send the data
while((s_pSpi->SPI_SR & AT91C_SPI_RDRF) == 0); // Wait until the character can be sent
spib = ((s_pSpi->SPI_RDR) & 0xFFFF); // Get the data received
return (U8)spib;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -