unsigned char trans[6] = {0xAA,0xC3,0x00,0xA2,0x00,0x24}; //所要发送的信息 void spi_xmit(Uint16 a); //发送一个数据 void Ready_Config(); //从机芯片ready标志位,此位读到1表明从机已经准备好,才可以与从机进行通信 void SPI_RXbuff(unsigned char *buff,unsigned int len); void SPI_TXbuff(unsigned char *buff,unsigned int len); unsigned char cmd_result[2] = {0x00}; //所要接收的返回信息存放数组,目前为0 void main(void) { Ready_Config(); //TI官方spi loopback例程,给出的初始化 // Step 1. Initialize System Control: // PLL, WatchDog, enable Peripheral Clocks // This example function is found in the F2837xS_SysCtrl.c file. InitSysCtrl(); // Step 2. Initialize GPIO: // This example function is found in the F2837xS_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 // This function is found in F2837xS_Spi.c InitSpiaGpio(); // Step 3. Clear all __interrupts and initialize PIE vector table: // Disable CPU __interrupts DINT; // Initialize PIE control registers to their default state. // The default state is all PIE __interrupts disabled and flags // are cleared. // This function is found in the F2837xS_PieCtrl.c file. InitPieCtrl(); // Disable CPU __interrupts and clear all CPU __interrupt flags: IER = 0x0000; IFR = 0x0000; // 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 F2837xS_DefaultIsr.c. // This function is found in F2837xS_PieVect.c. InitPieVectTable(); // Step 4. Initialize the Device Peripherals: InitSpi(); // init SPI //发送接收 SPI_TXbuff(trans,6); DELAY_US(10); SPI_RXbuff(cmd_result,2); } //各函数定义 void spi_xmit(Uint16 a) { //while(SpiaRegs.SPISTS.bit.BUFFULL_FLAG); SpiaRegs.SPITXBUF=a; // SPI Serial Output Buffer Register //while(SpiaRegs.SPISTS.bit.INT_FLAG != 1); } //Ready信号接收引脚设置为GPIO10 void Ready_Config() { EALLOW; GpioCtrlRegs.GPAMUX1.bit.GPIO10 = 0; //i/o // GpioCtrlRegs.GPAPUD.bit.GPIO10 = 0; //内部上拉 GpioCtrlRegs.GPADIR.bit.GPIO10 = 0; //input EDIS; } //接收缓冲区数据函数 void SPI_RXbuff(unsigned char *buff,unsigned int len) //uint8_t uint32_t { unsigned int i = 0; if(len==0) return; while(GpioDataRegs.GPADAT.bit.GPIO10 == 0);//等待从机就绪 GpioDataRegs.GPACLEAR.bit.GPIO11 = 1; //拉低片选信号 DELAY_US(10); for(i=0;i<len;i++) { buff[i] = (SpiaRegs.SPIRXBUF>>8); DELAY_US(10); } GpioDataRegs.GPASET.bit.GPIO11 = 1;//拉高片选信号 } //发送缓冲区数据函数 void SPI_TXbuff(unsigned char *buff,unsigned int len) //uint8_t uint32_t { unsigned int i = 0; if(len==0) return; while(GpioDataRegs.GPADAT.bit.GPIO10 == 0);//等待从机就绪 GpioDataRegs.GPACLEAR.bit.GPIO11 = 1; //拉低片选信号 DELAY_US(10); for(i=0;i<len;i++) { spi_xmit(buff[i]); //左移8位,适应MSB的要求 DELAY_US(10); } GpioDataRegs.GPASET.bit.GPIO11 = 1;//拉高片选信号 } //spi.c文件里spi配置 #include "F2837xD_device.h" #include "F2837xD_Examples.h" // // Calculate BRR: 7-bit baud rate register value // SPI CLK freq = 115200 Hz // LSPCLK freq = CPU freq / 4 (by default) // BRR = (LSPCLK freq / SPI CLK freq) - 1 // #if CPU_FRQ_200MHZ #define SPI_BRR ((200E6 / 4) / 115200) - 1 #endif #if CPU_FRQ_150MHZ #define SPI_BRR ((150E6 / 4) / 115200) - 1 #endif #if CPU_FRQ_120MHZ #define SPI_BRR ((120E6 / 4) / 115200) - 1 #endif // // InitSPI - This function initializes the SPI to a known state // void InitSpi(void) { // Initialize SPI-A // Set reset low before configuration changes // Clock polarity (0 == rising, 1 == falling) // 8-bit character // disable loop-back SpiaRegs.SPICCR.bit.SPISWRESET = 0; SpiaRegs.SPICCR.bit.CLKPOLARITY = 0; SpiaRegs.SPICCR.bit.SPICHAR = (8-1); SpiaRegs.SPICCR.bit.SPILBK = 0; // Enable master (0 == slave, 1 == master) // Enable transmission (Talk) // Clock phase (0 == normal, 1 == delayed) // SPI interrupts are disabled SpiaRegs.SPICTL.bit.MASTER_SLAVE = 1; SpiaRegs.SPICTL.bit.TALK = 1; SpiaRegs.SPICTL.bit.CLK_PHASE = 0; SpiaRegs.SPICTL.bit.SPIINTENA = 0; // Set the baud rate SpiaRegs.SPIBRR.bit.SPI_BIT_RATE = SPI_BRR; // Set FREE bit // Halting on a breakpoint will not halt the SPI SpiaRegs.SPIPRI.bit.FREE = 1; // Release the SPI from reset SpiaRegs.SPICCR.bit.SPISWRESET = 1; } // // InitSpiGpio - This function initializes GPIO pins to function as SPI pins. // Each GPIO pin can be configured as a GPIO pin or up to 3 // different peripheral functional pins. By default all pins come // up as GPIO inputs after reset. // // Caution: // For each SPI peripheral // Only one GPIO pin should be enabled for SPISOMO operation. // Only one GPIO pin should be enabled for SPISOMI operation. // Only one GPIO pin should be enabled for SPICLK operation. // Only one GPIO pin should be enabled for SPISTE operation. // Comment out other unwanted lines. // void InitSpiGpio() { InitSpiaGpio(); } // // InitSpiaGpio - Initialize SPIA GPIOs // 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.GPIO5 = 0; // Enable pull-up on GPIO5 (SPISIMOA) GpioCtrlRegs.GPAPUD.bit.GPIO17 = 0; // Enable pull-up on GPIO17 (SPISOMIA) // GpioCtrlRegs.GPAPUD.bit.GPIO3 = 0; // Enable pull-up on GPIO3 (SPISOMIA) GpioCtrlRegs.GPAPUD.bit.GPIO18 = 0; // Enable pull-up on GPIO18 (SPICLKA) GpioCtrlRegs.GPAPUD.bit.GPIO19 = 0; // Enable pull-up on GPIO19 (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.GPIO16 = 3; // Asynch input GPIO16 (SPISIMOA) // GpioCtrlRegs.GPAQSEL1.bit.GPIO5 = 3; // Asynch input GPIO5 (SPISIMOA) GpioCtrlRegs.GPAQSEL2.bit.GPIO17 = 3; // Asynch input GPIO17 (SPISOMIA) // GpioCtrlRegs.GPAQSEL1.bit.GPIO3 = 3; // Asynch input GPIO3 (SPISOMIA) GpioCtrlRegs.GPAQSEL2.bit.GPIO18 = 3; // Asynch input GPIO18 (SPICLKA) GpioCtrlRegs.GPAQSEL2.bit.GPIO19 = 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.GPAMUX1.bit.GPIO5 = 2; // Configure GPIO5 as SPISIMOA GpioCtrlRegs.GPAMUX2.bit.GPIO17 = 1; // Configure GPIO17 as SPISOMIA // GpioCtrlRegs.GPAMUX1.bit.GPIO3 = 2; // Configure GPIO3 as SPISOMIA GpioCtrlRegs.GPAMUX2.bit.GPIO18 = 1; // Configure GPIO18 as SPICLKA GpioCtrlRegs.GPAMUX2.bit.GPIO19 = 1; // Configure GPIO19 as SPISTEA //使能软件片选信号 GPIO11 GpioCtrlRegs.GPAMUX1.bit.GPIO11 = 0; // 设置为普通的GPIO模式 GpioCtrlRegs.GPADIR.bit.GPIO11 = 1; //设置为输出i/o EDIS; } // // End of file //
debug结果为:
可以看出RXBUF的值就是TXBUF的值左移8位(用16进制可以很明显观察到,比如TXBUF是0x24,RXBUF就是0x2400)。loopback模式已经禁用,可是还是感觉数据传输处于loopback模式里,没有真正接收到从机返回值。
以上问题无论是否启用中断模式都是一样存在。