📄 spi_master.c
字号:
//###########################################################################
//
// Original Source by MARK
//
// $Sanch Release: 2AD-2DA-maste V1.00 $
// $Release Date: MARCH 31, 2009 $
//###########################################################################
//#include "DSP280x_Device.h" // DSP280x Headerfile Include File
//#include "DSP280x_Examples.h" // DSP280x Examples Include File
#include "DSP28x_Project.h" // Device Headerfile and Examples Include File
// Prototype statements for functions found within this file.
// interrupt void ISRTimer2(void);
interrupt void spiTxFifoIsr(void);
interrupt void spiRxFifoIsr(void);
void delay_loop(void);
void spi_fifo_init(void);
void error();
Uint16 sdata=0x0A001; // Send data buffer
Uint16 sdata_head=0x0A000;//
Uint16 sdata_address_l00=0x0A101;//
Uint16 sdata_address_h00=0x0A202;//
Uint16 sdata_address_l01=0x0A303;//
Uint16 sdata_address_h01=0x0A404;//
Uint16 sdata_over=0x0AFFF;//
Uint16 sdata_head_temp=0x0A000;//
Uint16 sdata_address_l00_temp=0x0A100;//
Uint16 sdata_address_h00_temp=0x0A200;//
Uint16 sdata_address_l01_temp=0x0A300;//
Uint16 sdata_address_h01_temp=0x0A400;//
Uint16 sdata_over_temp=0x0AFFF;//
Uint16 sdata_point=0; // Keep track of where we are
// in the data stream to check received data
Uint16 rdata; // Receive data buffer
Uint16 rdata_temp;//
Uint16 rdata_head=0x0A000;//
Uint16 rdata_address_l00=0x0A100;//
Uint16 rdata_address_h00=0x0A200;//
Uint16 rdata_address_l01=0x0A300;//
Uint16 rdata_address_h01=0x0A400;//
Uint16 rdata_over=0x0AFFF;//
void main(void)
{
// Uint16 i;
// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP280x_SysCtrl.c file.
InitSysCtrl();
// Step 2. Initalize GPIO:
// This example function is found in the DSP280x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
// InitGpio(); // Skipped for this example
// Setup only the GP I/O only for SPI-A functionality
InitSpiaGpio();
// Step 3. Initialize PIE vector table:
// Disable and clear all CPU interrupts
DINT;
IER = 0x0000;
IFR = 0x0000;
// Initialize PIE control registers to their default state:
// This function is found in the DSP280x_PieCtrl.c file.
InitPieCtrl();
// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example. This is useful for debug purposes.
// The shell ISR routines are found in DSP280x_DefaultIsr.c.
// This function is found in DSP280x_PieVect.c.
InitPieVectTable();
// Interrupts that are used in this example are re-mapped to
// ISR functions found within this file.
EALLOW; // This is needed to write to EALLOW protected registers
SysCtrlRegs.PCLKCR0.bit.SPIAENCLK = 1; // SPI-A
PieVectTable.SPIRXINTA = &spiRxFifoIsr;
PieVectTable.SPITXINTA = &spiTxFifoIsr;
EDIS; // This is needed to disable write to EALLOW protected registers
// Step 4. Initialize all the Device Peripherals:
// This function is found in DSP280x_InitPeripherals.c
// InitPeripherals(); // Not required for this example
spi_fifo_init(); // Initialize the SPI only
// Step 5. User specific code, enable interrupts:
// Enable interrupts required for this example
PieCtrlRegs.PIECTRL.bit.ENPIE = 1; // Enable the PIE block
PieCtrlRegs.PIEIER6.bit.INTx1=1; // Enable PIE Group 6, INT 1
PieCtrlRegs.PIEIER6.bit.INTx2=1; // Enable PIE Group 6, INT 2
IER=0x20; // Enable CPU INT6
EINT; // Enable Global Interrupts
// Step 6. IDLE loop. Just sit and loop forever (optional):
for(;;);
}
void spi_fifo_init()
{
// Initialize SPI FIFO registers
SpiaRegs.SPICCR.bit.SPISWRESET=0; // Reset SCI
SpiaRegs.SPICCR.all=0x000F; //1f 16-bit character, Loopback mode
// .7SPI复位 .6时钟极性0上升沿传送1下降
SpiaRegs.SPICTL.all=0x0017; //13 Interrupt enabled, Master/Slave XMIT enabled
//.3时钟相位 .4接受溢出中断使能
SpiaRegs.SPISTS.all=0x0000;
SpiaRegs.SPIBRR=0x07F; // Baud rate
SpiaRegs.SPIFFTX.all=0xC028; // Enable FIFO's, set TX FIFO level to 8
SpiaRegs.SPIFFRX.all=0x0028; // Set RX FIFO level to 31
SpiaRegs.SPIFFCT.all=0x0F; //发送延迟
SpiaRegs.SPIPRI.all=0x0001; //0位为1三线模式
SpiaRegs.SPICCR.bit.SPISWRESET=1; // Enable SPI
SpiaRegs.SPIFFTX.bit.TXFIFO=1;
SpiaRegs.SPIFFRX.bit.RXFIFORESET=1;
}
interrupt void spiTxFifoIsr(void)
{
if(sdata_point==5)//防止8位一组的数据被破坏了
{
sdata_head_temp=sdata_head;
sdata_address_l00_temp=sdata_address_l00;//
sdata_address_h00_temp=sdata_address_h00;//
sdata_address_l01_temp=sdata_address_l01;//
sdata_address_h01_temp=sdata_address_h01;//
sdata_over_temp=sdata_over;//
}
if(SpiaRegs.SPIFFTX.bit.TXFFST==0) //发送
{
if(sdata_point==0)
sdata=sdata_head_temp; // Send data
else if(sdata_point==1)
sdata=sdata_address_l00_temp; // Send data
else if(sdata_point==2)
sdata=sdata_address_h00_temp; // Send data
else if(sdata_point==3)
sdata=sdata_address_l01_temp; // Send data
else if(sdata_point==4)
sdata=sdata_address_h01_temp; // Send data
else if(sdata_point==5)
sdata=sdata_over_temp; // Send data
SpiaRegs.SPITXBUF=sdata;
}
sdata_point++;
if(sdata_point>=6)
sdata_point=0;
SpiaRegs.SPIFFTX.bit.TXFFINTCLR=1; // Clear Interrupt flag
PieCtrlRegs.PIEACK.all|=0x20; // Issue PIE ACK
}
interrupt void spiRxFifoIsr(void)
{
rdata=SpiaRegs.SPIRXBUF;
if((rdata&0x0f000)==0x0A000)
{
rdata_temp=rdata;
if( (rdata_temp&0x0ff00)==0x0A000 )
rdata_head=rdata_temp;
else if( (rdata_temp&0x0ff00)==0x0A100 )
rdata_address_l00=rdata_temp;
else if( (rdata_temp&0x0ff00)==0x0A200 )
rdata_address_h00=rdata_temp;
else if( (rdata_temp&0x0ff00)==0x0A300 )
rdata_address_l01=rdata_temp;
else if( (rdata_temp&0x0ff00)==0x0A400 )
rdata_address_h01=rdata_temp;
else if( (rdata_temp&0x0ff00)==0x0AF00 )
rdata_over=rdata_temp;
}
else
{
SpiaRegs.SPICCR.bit.SPISWRESET=0;
SpiaRegs.SPICCR.all=0x000F;
SpiaRegs.SPICTL.all=0x0017;
SpiaRegs.SPISTS.all=0x0000;
SpiaRegs.SPIBRR=0x07F;
SpiaRegs.SPIFFTX.all=0xC028;
SpiaRegs.SPIFFRX.all=0x0028;
SpiaRegs.SPIFFCT.all=0x00;
SpiaRegs.SPIPRI.all=0x0001;
SpiaRegs.SPICCR.bit.SPISWRESET=1;
SpiaRegs.SPIFFTX.bit.TXFIFO=1;
SpiaRegs.SPIFFRX.bit.RXFIFORESET=1;
}
SpiaRegs.SPIFFRX.bit.RXFFOVFCLR=1; // Clear Overflow flag
SpiaRegs.SPIFFRX.bit.RXFFINTCLR=1; // Clear Interrupt flag
PieCtrlRegs.PIEACK.all|=0x20; // Issue PIE ack
}
//===========================================================================
// No more.
//===========================================================================
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -