本帖最后由 阳阳阳阳阳阳阳阳 于 2021-8-23 17:51 编辑
/****************************************************************** 编 译 器:Haawking IDE V0.5.0 D S P:dsc28027勇士板 电 脑 配 置:Windows10 专业版; 64 位操作系统 仿 真 器:HX100V2 使 用 库:无 作 用:DSC28027的SPI端口连接W25Q16芯片,实现对W25Q16的读写操作 版 本:V0.0.1 ******************************************************************/ 1.HaawkingIDE新建工程
2.SPI引脚配置 /****************************************************************** 函数名:void SPI_IOinit(void) 参 数:无 返回值:无 作 用:GPIO16、GPIO17、GPIO18为SPI功能,GPIO19为片选引脚
******************************************************************/
- void SPI_IOinit(void)
- {
- EALLOW;
- //GpioCtrlRegs.GPAMUX2.bit.GPIO19 = 0; //作为普通IO口使用
- //GpioCtrlRegs.GPADIR.bit.GPIO19 = 1; //管脚配置成输出
- GpioCtrlRegs.GPAPUD.bit.GPIO16 = 0; //Enable pull-up on GPIO16 (SPIAMOSI)
- GpioCtrlRegs.GPAPUD.bit.GPIO17 = 0; //Enable pull_up on GPIO17 (SPIAMISO)
- GpioCtrlRegs.GPAPUD.bit.GPIO18 = 0; // Enable pull-up on GPIO18 (SPICLKA)
- GpioCtrlRegs.GPAPUD.bit.GPIO19 = 0; // Enable pull-up on GPIO19 (SPISTEA)
- 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.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.GPAMUX2.bit.GPIO19 = 0; //作为普通IO口使用
- GpioCtrlRegs.GPADIR.bit.GPIO19 = 1; //管脚配置成输出
- GpioDataRegs.GPACLEAR.bit.GPIO19 = 1;
- EDIS;
- }
复制代码
SPI的FIFO寄存器初始化: /****************************************************************** 函数名:void spi_fifo_init(void) 参 数:无 返回值:无 作 用:初始化SPI的FIFO寄存器,
******************************************************************/
- void spi_fifo_init(void)
- {
- SpiaRegs.SPIFFTX.bit.SPIRST = 1; //写0复位SPI发送和接收通道,SPI FIFO寄存器配置位将被保留,SPI发送寄存器
- //写1,SPI FIFO能重新开始发送或接收,这不影响SPI的寄存器位
- SpiaRegs.SPIFFTX.bit.SPIFFENA = 1; //SPI FIFO增强使能
- SpiaRegs.SPIFFTX.bit.TXFFST = 1; //Re-enable Transmit FIFO operation,发送FIFO有一个字节
- SpiaRegs.SPIFFTX.bit.TXFFINTCLR = 1; //Write 1 to clear TXFFINT flag in bit 7.
- SpiaRegs.SPIFFTX.bit.TXFFIENA = 0; //不用中断
- SpiaRegs.SPIFFRX.bit.RXFFOVFCLR = 1; //清除RXFFOVF位
- SpiaRegs.SPIFFRX.bit.RXFFINTCLR =1; //写1清除RXFFINT标志位
- SpiaRegs.SPIFFRX.bit.RXFFST = 1; //接收FIFO有1个字
- SpiaRegs.SPIFFRX.bit.RXFFIENA = 0; //不用FIFO接收中断
- SpiaRegs.SPIFFRX.bit.RXFFIL = 4;
- SpiaRegs.SPIFFCT.all = 0x0; //无延迟
- }
复制代码
SPI控制寄存器初始化: /****************************************************************** 函数名:void spi_init(void) 参 数:无 返回值:无 作 用:初始化SPI的控制寄存器,
******************************************************************/
- void spi_init(void)
- {
- GpioDataRegs.GPASET.bit.GPIO19 = 1;
- SpiaRegs.SPICCR.bit.SPISWRESET = 0; //初始化SPI操作标志位到复位条件
- SpiaRegs.SPICCR.bit.SPICHAR = 7; //8位数据传输
- SpiaRegs.SPICCR.bit.CLKPOLARITY = 0; //when no SPI data is sent,SPICLK is at low
- SpiaRegs.SPICTL.bit.CLK_PHASE = 1; //当CPHA,数据线在SCK的偶数边沿采样
- SpiaRegs.SPICTL.bit.MASTER_SLAVE =1;
- SpiaRegs.SPIBRR =5; //3; //For SPIBRR = 3 to 127; SPI Baud Rate = LSPCLK/(SPIBRR+1)
- SpiaRegs.SPICCR.bit.SPISWRESET = 1; //SW Reset off
- SpiaRegs.SPIPRI.bit.FREE = 1; //Set so breakpoints don't disturb xmission
- SpiaRegs.SPIPRI.bit.CS_PRIORITY = 0;
- }
复制代码
读取状态寄存器1和状态寄存器2的值: /************************************************************************ 函数名:uint16 Read_Status_2Reg(void) 参 数:无 返回值:状态寄存器1和状态寄存器2中的值 作 用:读状态寄存器1和状态寄存器2的值 //P23 //指令 0x03 //地址 A23--A0
/************************************************************************/
- uint16 Read_Status_2Reg(void)
- {
- uint16 wbyte1,wbyte2;
- GpioDataRegs.GPACLEAR.bit.GPIO19 = 1;
- Send_Byte(READ_STATUS_REG_IST); //读状态寄存器1指令,返回Status Register1
- wbyte1 = Get_Byte();
- GpioDataRegs.GPASET.bit.GPIO19 = 1;
- GpioDataRegs.GPACLEAR.bit.GPIO19 = 1;
- Send_Byte(0x35); //READ_STATUS_REG_IST 读状态寄存器2指令,返回Status Register2
- wbyte2 = Get_Byte();
- GpioDataRegs.GPASET.bit.GPIO19 = 1;
- wbyte2<<=6;
- wbyte2 &= 0xff00;
- wbyte2 += wbyte1&0xff;
- GpioDataRegs.GPASET.bit.GPIO19 = 1;
- return wbyte2;
- }
复制代码
3.main函数
- int main(void)
- {
- // int temp, in;
- // volatile unsigned int i;
- InitSysCtrl();
- // Step 2. Initalize GPIO:
- // This example function is found in the DSP2803x_Gpio.c file and
- // illustrates how to set the GPIO to it's default state.
- // InitGpio(); // Skipped for this example
- // 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 DSP2803x_PieCtrl.c file.
- InitPieCtrl();
- // Disable CPU interrupts and clear all CPU interrupt flags:
- IER_DISABLE(0xffff);
- IFR_DISABLE(0xffff);
- // 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 DSP2803x_DefaultIsr.c.
- // This function is found in DSP2803x_PieVect.c.
- InitPieVectTable();
- EALLOW;
- SysCtrlRegs.LOSPCP.all = 0x0002; //低速外设时钟倍频
- SysCtrlRegs.XCLK.all = 0x0002; //XCLKOUT Divide Ratio:These two bits select the XCLKOUT ;XCLKOUT = SYSCLKOUT
- SysCtrlRegs.PCLKCR0.bit.SPIAENCLK = 1; //使能SPIA的时钟
- SysCtrlRegs.PCLKCR3.all = 0x2000; // GPIO Input Clock Enable
- SysCtrlRegs.PLLCR.all = 0x0002;
- SysCtrlRegs.PLLSTS.all = 0x0180;
- EDIS;
- // InitSysCtrl();
- SPI_IOinit(); //SPI IO端口配置
- spi_fifo_init();
- spi_init();
- SPIFlash.Jedec_ID = Jedec_ID_Read();
- SPIFlash.SReg = Read_Status_2Reg();
- WREN();
- WrSReg(0x0000);
- SPIFlash.SReg = Read_Status_2Reg();
- Wait_Busy();
- SPIFlash.SReg = Read_Status_2Reg();
- //ReadData(0,upper_128,128);
- WREN();
- Chip_Erase();
- Wait_Busy();
- ReadData(0,upper_128,128);
- WREN();
- PageProgram(0,upper_100,128);
- Wait_Busy();
- ReadData(0,upper_128,128);
- while(1){
- }
- return 0;
- }
复制代码
4.点击编译、编译完成后点击下载
5.运行结果:W25Q16的片选信号是GPIO19,Flash写入操作,只能从逻辑1写为逻辑1或逻辑0;两个数组的前8个数据相等: upper_128[]=upper_100[] |