本帖最后由 tianxj01 于 2018-7-29 10:09 编辑
因为003芯片硬件SPI已经被单独占用,而外部W25X64使用频率不高,因此用IO模拟SPI进行驱动是一个很好的方法。硬件连接:由于W25X64 是3.3V,而我的003用的是5V,所以这里全部采用外接3.3K电阻上拉3.3V,003端口输入采用高阻外部上拉,输出采用开漏,上拉。
下面给出全部源代码:(SPI_Flash_cmd.h)
#ifndef __SPI_FLASH_CMD_H__
#define __SPI_FLASH_CMD_H__
#define W25X_BUSY 0
#define W25X_NotBUSY 1
#define Dummy_Byte1 0xFF
/*********************************************
- W25X的操作指令表,MCU通过向W25X
发送以下指令就可以对W25X进行以下的操作
*********************************************/
#define W25X_WriteEnable 0x06 //Write Enable
#define W25X_WriteEnableVSR 0x50 //Write Enable for Volatile Status Register
#define W25X_WriteDisable 0x04 //Write Disable
#define W25X_ReadStatusReg1 0x05 //读状态寄存器1:S7~S0
#define W25X_ReadStatusReg2 0x35 //读状态寄存器2:S15~S8
#define W25X_WriteStatusReg 0x01 //写读状态寄存器:BYTE1:S7~S0 BYTE2:S15~S8
#define W25X_PageProgram 0x02 //单页编程:BYTE1:A23~A16 BYTE2:A15~A8 BYTE3:A7~A0 BYTE4:D7~D0
#define W25X_SectorErase 0x20 //扇区擦除:4K BYTE1:A23~A16 BYTE2:A15~A8 BYTE3:A7~A0
#define W25X_BlockErase32K 0x52 //块擦除:32K BYTE1:A23~A16 BYTE2:A15~A8 BYTE3:A7~A0
#define W25X_BlockErase64K 0xD8 //块擦除:64K BYTE1:A23~A16 BYTE2:A15~A8 BYTE3:A7~A0
#define W25X_ChipErase 0xC7 //芯片擦除
#define W25X_EraseSuspend 0x75 //暂停擦除
#define W25X_EraseResume 0x7A //恢复擦除
#define W25X_PowerDown 0xB9 //掉电
#define W25X_ContinuousReadMode 0xFF //连续读模式
#define W25X_ReadData 0x03 //读数据:BYTE1:A23~A16 BYTE2:A15~A8 BYTE3:A7~A0 BYTE4:D7~D0
#define W25X_FastReadData 0x0B //快速读取:BYTE1:A23~A16 BYTE2:A15~A8 BYTE3:A7~A0 BYTE4:dummy BYTE5:D7~D0
#define W25X_FastReadDual 0x3B //快速双读取:BYTE1:A23~A16 BYTE2:A15~A8 BYTE3:A7~A0 BYTE4:dummy BYTE5:D7~D0
#define W25X_ReleasePowerDown 0xAB
#define W25X_DeviceID 0xAB
#define W25X_ManufactDeviceID 0x90
#define W25X_JedecDeviceID 0x9F
#endif
SPI_Flash_reg.h:
/**************************************
软SPI端口连接
P16 -- spi cs
P15 -- spi clock
P14 -- spi MOSI
P13 -- spi MISO
***************************************/
#define SPI_CS P16
#define SPI_CLK P15
#define SPI_MOSI P14
#define SPI_MISO P13
void SPI_Flash_Init_IO (void);
void SPI_Flash_WriteEnable (void);
void SPI_Flash_WriteDisable (void);
void SPI_Flash_Wait_Busy (void);
void SPI_Flash_Read ( u32 ReadAddr , u8* pBuffer , u16 NumByteToRead );
void SPI_Flash_Write_Page (u32 WriteAddr, u8* pBuffer , u16 NumByteToWrite);
void SPI_Flash_Erase_Block( u32 Erase_Addr ,u8 mode );
unsigned int SPI_Flash_ReadID(void);
unsigned char SPI_Flash_Read_Busy(void);
unsigned char SPI_Flash_Write_Byte(unsigned char Data);
|