一.基本参数
1.采用串行Nor flash外扩存储芯片 2.支持SPI接口 3.工作电压:2.7~3.6V 4.容量: 32Mbit(W25Q32) 64Mbit(W25Q64) 128Mbit(W25Q128)
二.通信协议
1、时序:
2、代码:
此处使用硬件SPI
void SPI_FLASH_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
FLASH_SPI_APBxClock_FUN ( FLASH_SPI_CLK, ENABLE );
FLASH_SPI_CS_APBxClock_FUN ( FLASH_SPI_CS_CLK|FLASH_SPI_SCK_CLK|
FLASH_SPI_MISO_PIN|FLASH_SPI_MOSI_PIN, ENABLE );
GPIO_InitStructure.GPIO_Pin = FLASH_SPI_CS_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(FLASH_SPI_CS_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = FLASH_SPI_SCK_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(FLASH_SPI_SCK_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = FLASH_SPI_MISO_PIN;
GPIO_Init(FLASH_SPI_MISO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = FLASH_SPI_MOSI_PIN;
GPIO_Init(FLASH_SPI_MOSI_PORT, &GPIO_InitStructure);
SPI_FLASH_CS_HIGH();
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(FLASH_SPIx , &SPI_InitStructure);
SPI_Cmd(FLASH_SPIx , ENABLE);
}
三.引脚定义与接线
1、引脚定义
引脚 | 功能 |
---|
CS(NSS) | 片选,低电平有效 | SCK | 时钟信号引脚 | MISO/DO | 模块数据输出引脚 | MOSI/DI | 模块数据输入引脚 |
2、对应接线
W25Q64模块 | STM32F103C8T6 |
---|
VCC | 3.3V | SPI_CS | GPIOA_4 | SPI_SCK | GPIOA_5 | SPI_MISO/DO | GPIOA_6 | SPI_MOSI/DI | GPIOA_7 |
四.主要代码
int main(void)
{
uint16_t i;
LED_GPIO_Config();
LED_BLUE;
USART_Config();
printf("\r\n 这是一个64Mbit串行flash(W25Q64)实验 \r\n");
SPI_FLASH_Init();
DeviceID = SPI_FLASH_ReadDeviceID();
Delay( 200 );
FlashID = SPI_FLASH_ReadID();
printf("\r\n FlashID is 0x%X,\
Manufacturer Device ID is 0x%X\r\n", FlashID, DeviceID);
if (FlashID == sFLASH_ID)
{
printf("\r\n 检测到串行flash W25Q64 !\r\n");
SPI_FLASH_SectorErase(FLASH_SectorToErase);
for(i=0;i<100;i++)
{
Tx_Buffer[i] = i;
}
SPI_FLASH_BufferWrite(Tx_Buffer, FLASH_WriteAddress, BufferSize);
printf("\r\n写入的数据为:\r\n");
for(i=0;i<BufferSize;i++)
{
if((i%10)==0)
{
printf("\r\n");
}
printf("%x ", Tx_Buffer[i]);
}
SPI_FLASH_BufferRead(Rx_Buffer, FLASH_ReadAddress, BufferSize);
printf("\r\n读出的数据为:\r\n");
for(i=0;i<BufferSize;i++)
{
if((i%10)==0)
{
printf("\r\n");
}
printf("%x ", Rx_Buffer[i]);
}
TransferStatus1 = Buffercmp(Tx_Buffer, Rx_Buffer, BufferSize);
if( PASSED == TransferStatus1 )
{
LED_GREEN;
printf("\r\n 64Mbit串行flash(W25Q64)测试成功!\n\r");
}
else
{
LED_RED;
printf("\r\n 64Mbit串行flash(W25Q64)测试失败!\n\r");
}
}
else
{
LED_RED;
printf("\r\n 获取不到 W25Q64 ID!\n\r");
}
while(1);
}
五.操作步骤与现象
下载程序后打开串口助手,选择对应端口,波特率选115200 打开串口 最后按下开发板复位按键就会有如下输出:
六.总结
1、供电切勿超过3.6V,建议3.3V供电 2、注意写入读出的数据量大小一致,否者读出可能乱码 需要完整工程的可下方留言邮箱获取
|