本例程没有使用DMA功能,直接调用SPI接口函数,实现25q64的数据读写。理论上使用DMA会提升代码的执行效率,当然程序也会复杂一些。
1 初始化SPI接口
void SPIM_Init(SPI_TypeDef* SPIx, unsigned short spi_baud_div)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_3); //SPI_NSS
GPIO_PinAFConfig(GPIOA, GPIO_PinSource13, GPIO_AF_4); //SPI_MISO
GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_4); //SPI_MOSI
GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_3); //SPI_SCK
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //SPI_NSS
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //Push to avoid multiplexing output
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14; //SPI_MOSI | SPI_SCK
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; //SPI_MISO
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //Pull-up input
GPIO_Init(GPIOA, &GPIO_InitStructure);
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_DataWidth = SPI_DataWidth_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = spi_baud_div;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_Init(SPIx, &SPI_InitStructure);
SPI_Cmd(SPIx, ENABLE);
SPIM_TXEn(SPIx);
SPIM_RXEn(SPIx);
}
注意MM32F003由于接口数量较少,SPI接口与SWD接口复用。所以将GPIO设置成SPI后,无法使用SWD调试,同时会影响程序的下载,最好在配置GPIO之前,做个1到2秒的延迟,防止配置SPI后芯片无法烧写程序。
2 25Q64读写接口
void SPI_FLASH_Read(SPI_TypeDef* SPIx, u32 Addr, u32 NumBytes, void *data)
{
u8 *Buffer = (u8 *) data;
SPIM_CSLow(SPIx);
SPI_FLASH_WriteRead(SPIx, ReadData); //Send Read Data instruction
SPI_FLASH_WriteRead(SPIx, Addr >> 16); //Send destination address 16-23 bits
SPI_FLASH_WriteRead(SPIx, Addr >> 8); //Send destination address 8-15 bits
SPI_FLASH_WriteRead(SPIx, Addr); //Send destination address 0-7 bits
while (NumBytes--) {
*Buffer = SPI_FLASH_WriteRead(SPIx, Dummy_Byte); //Save the data read from flash into the specified array
Buffer++;
}
SPIM_CSHigh(SPIx);
}
void SPI_FLASH_Write(SPI_TypeDef* SPIx, unsigned long address, unsigned char* data, u32 PageSize)
{
int i;
unsigned char addr0, addr1, addr2;
u8 *Buffer = data;
u32 count = ((address / FlashSize) + 1) * FlashSize;
while((FlashSize - address) < PageSize) { //Judge flash this layer is enough space left
SPI_SectorErase(SPIx, count);
count = count + FlashSize;
address = count;
}
while(1) {
if(SPI_FLASH_WaitForWrite(SPIx) == W25X_NotBUSY) break; //Waiting for the flash is not busy state
}
address = address & 0xffffff00;
addr0 = (unsigned char)(address >> 16);
addr1 = (unsigned char)(address >> 8);
addr2 = (unsigned char)address;
SPIM_WriteEnable(SPIx);
SPIM_CSLow(SPIx);
SPI_FLASH_WriteRead(SPIx, PP); //Send Page Program instruction
SPI_FLASH_WriteRead(SPIx, addr0); //Send destination address 16-23 bits
SPI_FLASH_WriteRead(SPIx, addr1); //Send destination address 8-15 bits
SPI_FLASH_WriteRead(SPIx, addr2); //Send destination address 0-7 bits
for(i = 0; i < PageSize; i++) {
SPI_FLASH_WriteRead(SPIx, *Buffer);
Buffer++;
}
SPIM_CSHigh(SPIx);
SPIM_checkStatus(SPIx);
}
测试流程
int main(void)
{
u32 i ;
InitSystick();
delay(1000);
RCC_ConfigInit();
GPIO_ConfigInit();
Uart_ConfigInit(9600);
UartSendGroup((u8*)printBuf, sprintf(printBuf, "\r\nsprintf ok\r\n"));
SPIM_Init(SPI, 0x30);
for(i = 0; i < 256; i++) {
tmpdata[i] = i/* * 2*/;
}
SPIM_ReadID(SPI, rxtmpdata);
UartSendGroup((u8*)printBuf, sprintf(printBuf, "\r\nSPIM_ReadID:\r\n"));
for(i = 0; i < 4; i++) {
UartSendGroup((u8*)printBuf, sprintf(printBuf, "%2x ", rxtmpdata[i]));
}
SPI_SectorErase(SPI, 0);
SPI_FLASH_Write(SPI, 0, tmpdata, 256);
for(i = 0; i < 256; i++) {
rxtmpdata[i] = 0x0;
}
SPI_FLASH_Read(SPI, 0, 256, rxtmpdata);
UartSendGroup((u8*)printBuf, sprintf(printBuf, "\r\nSPI_FLASH_Read:\r\n"));
for(i = 0; i < 256; i++) {
UartSendGroup((u8*)printBuf, sprintf(printBuf, "%2x ", rxtmpdata[i]));
}
while(1) {
}
}
串口数据输出
SPIM_ReadID:
ef 40 17 0
SPI_FLASH_Read:
0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f 90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff