SPI IO初始化
void
InitSpiaGpio()
{
EALLOW;
//
// Enable internal pull-up for the selected pins
// Pull-ups can be enabled or disabled by the user.
// This will enable the pullups for the specified pins.
// Comment out other unwanted lines.
//
GpioCtrlRegs.GPAPUD.bit.GPIO16 = 0; //Enable pull-up on GPIO16 (SPISIMOA)
GpioCtrlRegs.GPAPUD.bit.GPIO17 = 0; //Enable pull-up on GPIO17 (SPISOMIA)
GpioCtrlRegs.GPAPUD.bit.GPIO18 = 0; //Enable pull-up on GPIO18 (SPICLKA)
GpioCtrlRegs.GPAPUD.bit.GPIO19 = 0; //Enable pull-up on GPIO19 (SPISTEA)
//GpioCtrlRegs.GPBPUD.bit.GPIO54 = 0; //Enable pull-up on GPIO54 (SPISIMOA)
//GpioCtrlRegs.GPBPUD.bit.GPIO55 = 0; //Enable pull-up on GPIO55 (SPISOMIA)
//GpioCtrlRegs.GPBPUD.bit.GPIO56 = 0; //Enable pull-up on GPIO56 (SPICLKA)
//GpioCtrlRegs.GPBPUD.bit.GPIO57 = 0; //Enable pull-up on GPIO57 (SPISTEA)
//
// Set qualification for selected pins to asynch only
// This will select asynch (no qualification) for the selected pins.
// Comment out other unwanted lines.
//
// GpioCtrlRegs.GPAQSEL2.bit.GPIO17 = 0; // Asynch input GPIO16 (SPISIMOA)
// GpioCtrlRegs.GPADIR.bit.GPIO17 = 0;
GpioCtrlRegs.GPAQSEL2.bit.GPIO16 = 3; // Asynch input GPIO16 (SPISIMOA)
GpioCtrlRegs.GPAQSEL2.bit.GPIO17 = 3; // Asynch input GPIO17 (SPISOMIA)
GpioCtrlRegs.GPAQSEL2.bit.GPIO18 = 3; // Asynch input GPIO18 (SPICLKA)
GpioCtrlRegs.GPAQSEL2.bit.GPIO19 = 3; // Asynch input GPIO19 (SPISTEA)
//GpioCtrlRegs.GPBQSEL2.bit.GPIO54 = 3; // Asynch input GPIO16 (SPISIMOA)
//GpioCtrlRegs.GPBQSEL2.bit.GPIO55 = 3; // Asynch input GPIO17 (SPISOMIA)
//GpioCtrlRegs.GPBQSEL2.bit.GPIO56 = 3; // Asynch input GPIO18 (SPICLKA)
//GpioCtrlRegs.GPBQSEL2.bit.GPIO57 = 3; // Asynch input GPIO19 (SPISTEA)
//
// Configure SPI-A pins using GPIO regs
// This specifies which of the possible GPIO pins will be SPI
// functional pins.
// Comment out other unwanted lines.
//
GpioCtrlRegs.GPAMUX2.bit.GPIO16 = 1; // Configure GPIO16 as SPISIMOA
GpioCtrlRegs.GPAMUX2.bit.GPIO17 = 1; // Configure GPIO17 as SPISOMIA
GpioCtrlRegs.GPAMUX2.bit.GPIO18 = 1; // Configure GPIO18 as SPICLKA
GpioCtrlRegs.GPAMUX2.bit.GPIO19 = 1; // Configure GPIO19 as SPISTEA
//GpioCtrlRegs.GPBMUX2.bit.GPIO54 = 1; // Configure GPIO54 as SPISIMOA
//GpioCtrlRegs.GPBMUX2.bit.GPIO55 = 1; // Configure GPIO55 as SPISOMIA
//GpioCtrlRegs.GPBMUX2.bit.GPIO56 = 1; // Configure GPIO56 as SPICLKA
//GpioCtrlRegs.GPBMUX2.bit.GPIO57 = 1; // Configure GPIO57 as SPISTEA
EDIS;
}
初始化SPIA相关寄存器
void InitSpia(void)
{
SpiaRegs.SPICCR.bit.SPISWRESET = 0; //SPI复位
//数据在时钟上升沿发送,在时钟下降沿所存输入的数据
SpiaRegs.SPICCR.bit.CLKPOLARITY = 0; //上升沿触发
SpiaRegs.SPICTL.bit.CLK_PHASE = 1; //延时时钟
SpiaRegs.SPICCR.bit.SPILBK = 0; //禁止内部回路
SpiaRegs.SPICCR.bit.SPICHAR = 0xF; //发送和接收的数据长度16
SpiaRegs.SPICTL.bit.OVERRUNINTENA = 0; //关闭溢出中断
SpiaRegs.SPICTL.bit.MASTER_SLAVE = 1; //主控制器模式
SpiaRegs.SPICTL.bit.TALK = 1; //使能发送功能
SpiaRegs.SPICTL.bit.SPIINTENA = 0; //禁止SPI中断
SpiaRegs.SPIBRR = 0x63; //波特率=150M/4/100=375K
SpiaRegs.SPIPRI.bit.FREE = 1; //仿真不影响发送数据
SpiaRegs.SPICCR.bit.SPISWRESET = 1; //SPI功能恢复
}
SPI 轮询发送与接受
void spi_xmit(Uint16 a)
{
SpiaRegs.SPITXBUF=a;//左对齐
while(SpiaRegs.SPISTS.bit.BUFFULL_FLAG){}
g_wData[0] = SpiaRegs.SPIRXBUF;
while(SpiaRegs.SPIFFRX.bit.RXFFST != 0);
SpiaRegs.SPITXBUF=0XFFFF;//左对齐
while(SpiaRegs.SPISTS.bit.BUFFULL_FLAG){}
g_wData[1] = SpiaRegs.SPIRXBUF;
while(SpiaRegs.SPIFFRX.bit.RXFFST != 0);
SpiaRegs.SPITXBUF=0XFFFF;//左对齐
while(SpiaRegs.SPISTS.bit.BUFFULL_FLAG){}
g_wData[2] = SpiaRegs.SPIRXBUF;
while(SpiaRegs.SPIFFRX.bit.RXFFST != 0);
SpiaRegs.SPITXBUF=0XFFFF;//左对齐
while(SpiaRegs.SPISTS.bit.BUFFULL_FLAG){}
g_wData[3] = SpiaRegs.SPIRXBUF;
while(SpiaRegs.SPIFFRX.bit.RXFFST != 0);
SpiaRegs.SPITXBUF=0XFFFF;//左对齐
while(SpiaRegs.SPISTS.bit.BUFFULL_FLAG){}
g_wData[4] = SpiaRegs.SPIRXBUF;
while(SpiaRegs.SPIFFRX.bit.RXFFST != 0);
SpiaRegs.SPITXBUF=0XFFFF;//左对齐
while(SpiaRegs.SPISTS.bit.BUFFULL_FLAG){}
g_wData[5] = SpiaRegs.SPIRXBUF;
while(SpiaRegs.SPIFFRX.bit.RXFFST != 0);
//
g_wData[6] = SpiaRegs.SPIRXBUF;
while(SpiaRegs.SPIFFRX.bit.RXFFST != 0);
}
波形(发送0x9f)查询JEDEC ID编码回复0xEF 0X3012
绿色:SPI_MOSI
黄色:SPI_CS
紫色:SPI_MISO
蓝色:SPI_CLK