|
试验目的:芯片采用F0芯片,通过配置SPI1完成W25X16的读写。片选CS为PA1,SCK为PB3,MISO为PB4,MOSI为PB5 具体配置:
由于没有使用中断和DMA,所以中断配置不选。
程序分析: 首先要读取W25X16的ID号,需要发送0x90,加2个伪数据+0x00。同时由于ID号为2个8位数据构成,并且ST的F0芯片的SPI是要读取时需要先写,所以定义 uint8_t Data1[4] = {0x90,0x00,0x00,0x00} uint8_t Data2[2]= {0x00,0x00} uint8_t Rxdata[2]; ; ?& j' D% f' U& B$ R, _; K6 p 然后主程序中使能SPI端口:HAL_SPI_MspInit(&hspi1); 最后就是读取ID命令了,如下图
此时Rxdata=0xEF14,这个就是W25X16的ID号。 读取ID正确了,剩下的就是对Flash的操作了。 + T c% J( v1 D/ _ |
微信公众号
手机版
void SPI_FLASH_WriteEnable(void)
{' E( b: {& d9 ~" x! v
SPI_FLASH_CS_LOW();! a# r9 I" k* `. W% J( J, f
HAL_SPI_Transmit(&hspi1, &W25X_WriteEnable, 1, 100);2 b: I4 y; ]2 N4 F) \& Z
SPI_FLASH_CS_HIGH();
}
uint8_t SPI_FLASH_SendByte(uint8_t byte)5 w) X9 [4 |6 `; h: N2 X
{
uint8_t temp;
uint8_t temp1[1];
HAL_SPI_TransmitReceive(&hspi1, &byte, &temp1[0], 1,100);+ M0 j9 ? f. W+ B
temp=temp1[0];
return temp;
}* N. ]+ V; Z2 a, F
void SPI_FLASH_WaitForWriteEnd(void)
{ : c2 O0 T$ h$ I
uint8_t FLASH_Status=0;! ~6 n# E! c' _ G7 J
uint8_t temp1[1];
SPI_FLASH_CS_LOW();! ^6 ?& B$ H: l& a5 T4 ^$ c
HAL_SPI_Transmit(&hspi1, &W25X_ReadStatusReg, 1, 100); ) X+ C5 B' y% O! a8 Y! O
do
{/ r: e9 [5 I( K. R+ ]1 _
HAL_SPI_TransmitReceive(&hspi1, &Dummy_Byte, &temp1[0], 1,100);
FLASH_Status = temp1[0];# S1 r' E& j1 a1 G+ |
}
while ((FLASH_Status & WIP_Flag) == SET);
SPI_FLASH_CS_HIGH();; q' ^/ ~1 S5 A" X+ B) J
}
这是用调用到的函数
SPI的引脚配置可以这样设置吗? /**SPI1 GPIO Configuration ) n2 r' R! o# g: B
PB3 ------> SPI1_SCK7 U% [. c9 {( e" L
PB4 ------> SPI1_MISO
PB5 ------> SPI1_MOSI ! c( l/ c% h. {# o
*/
GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;' m, ^! R }1 _. X! x9 ?
GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
void SPI_FLASH_SectorErase(uint32_t SectorAddr)$ z9 n7 t9 S, X# m
{3 t3 w& k& }( ]' q1 P
SPI_FLASH_WriteEnable();
SPI_FLASH_CS_LOW();
SPI_FLASH_SendByte(W25X_SectorErase);
SPI_FLASH_SendByte((SectorAddr & 0xFF0000) >> 16);
SPI_FLASH_SendByte((SectorAddr & 0xFF00) >> 8);* u3 e9 B: Z# j, p# d% {
SPI_FLASH_SendByte(SectorAddr & 0xFF);* a( E5 D" i' ^" n7 W' Y
SPI_FLASH_CS_HIGH();
SPI_FLASH_WaitForWriteEnd();
}
äºååèæ°ï½å¤§éª
一般不应该是0,如果读不到,应该是FF