因为稍后要用到字库等,所以先把Flash弄了,使用SPI接口,神舟的2Mbyte,原子的6Mbyte,都是W25X系列。神舟真是小气的可以,搞个Flash和EEPROM一样大。
与SD的读写byte一样。
初始化之后读取Flash ID以此来判断是否初始化成功。
主要是一些读写,擦除函数。主要用的神舟代码这次,有部分借鉴原子的
初始化,读取ID,擦除,读写都OK了。
FLASHSPI_CONFIG_H
1 #ifndef __FLASHSPI_CONFIG_H 2 #define __FLASHSPI_CONFIG_H 3 //#include "sys.h" 4 5 /** 6 * @brief SPI_FLASH SPI Interface pins 7 */ 8 9 #define SPI_FLASH_SPI SPI1 10 #define SPI_FLASH_SPI_CLK RCC_APB2Periph_SPI1 11 #define SPI_FLASH_SPI_SCK_PIN GPIO_Pin_5 /* PA.05 */ 12 #define SPI_FLASH_SPI_SCK_GPIO_PORT GPIOA /* GPIOA */ 13 #define SPI_FLASH_SPI_SCK_GPIO_CLK RCC_APB2Periph_GPIOA 14 #define SPI_FLASH_SPI_MISO_PIN GPIO_Pin_6 /* PA.06 */ 15 #define SPI_FLASH_SPI_MISO_GPIO_PORT GPIOA /* GPIOA */ 16 #define SPI_FLASH_SPI_MISO_GPIO_CLK RCC_APB2Periph_GPIOA 17 #define SPI_FLASH_SPI_MOSI_PIN GPIO_Pin_7 /* PA.07 */ 18 #define SPI_FLASH_SPI_MOSI_GPIO_PORT GPIOA /* GPIOA */ 19 #define SPI_FLASH_SPI_MOSI_GPIO_CLK RCC_APB2Periph_GPIOA 20 #define SPI_FLASH_CS_PIN GPIO_Pin_9 /* PB.09 */ 21 #define SPI_FLASH_CS_GPIO_PORT GPIOB /* GPIOD */ 22 #define SPI_FLASH_CS_GPIO_CLK RCC_APB2Periph_GPIOB 23 24 #define SD_CS_PIN GPIO_Pin_11 /* PD.11 */ 25 #define SD_CS_GPIO_PORT GPIOD /* GPIOD */ 26 #define SD_CS_GPIO_CLK RCC_APB2Periph_GPIOD 27 28 /* Private define ------------------------------------------------------------*/ 29 //W25X系列/Q系列芯片列表 30 //W25Q80 ID 0XEF13 31 //W25Q16 ID 0XEF14 32 //W25Q32 ID 0XEF15 33 //W25Q32 ID 0XEF16 34 //指令 90h Manufacturer/Device ID (ID7-ID0) 35 #define W25Q80 0XEF13 36 #define W25Q16 0XEF14 37 #define W25Q32 0XEF15 38 #define W25Q64 0XEF16 39 //指令 9Fh JEDEC ID (ID15-ID0) 40 //#define W25Q80 0XEF3014 41 //#define W25Q16 0XEF3015 42 //#define W25Q32 0XEF3016 43 //#define W25Q64 0XEF3017 44 45 #define W25X_WriteEnable 0x06 46 #define W25X_WriteDisable 0x04 47 #define W25X_ReadStatusReg 0x05 48 #define W25X_WriteStatusReg 0x01 49 #define W25X_ReadData 0x03 50 #define W25X_FastReadData 0x0B 51 #define W25X_FastReadDual 0x3B 52 #define W25X_PageProgram 0x02 53 #define W25X_BlockErase 0xD8 54 #define W25X_SectorErase 0x20 55 #define W25X_ChipErase 0xC7 56 #define W25X_PowerDown 0xB9 57 #define W25X_ReleasePowerDown 0xAB 58 #define W25X_DeviceID 0xAB 59 #define W25X_ManufactDeviceID 0x90 60 #define W25X_JedecDeviceID 0x9F 61 62 #define WIP_Flag 0x01 /* Write In Progress (WIP) flag */ 63 64 #define Dummy_Byte 0xFF//0xA5 65 66 /* Private typedef -----------------------------------------------------------*/ 67 //#define SPI_FLASH_PageSize 4096 68 #define SPI_FLASH_PageSize 256 69 #define SPI_FLASH_PerWritePageSize 256 70 71 /* Exported macro ------------------------------------------------------------*/ 72 /* Select SPI FLASH: Chip Select pin low */ 73 #define SPI_FLASH_CS_LOW() GPIO_ResetBits(SPI_FLASH_CS_GPIO_PORT, SPI_FLASH_CS_PIN) 74 /* Deselect SPI FLASH: Chip Select pin high */ 75 #define SPI_FLASH_CS_HIGH() GPIO_SetBits(SPI_FLASH_CS_GPIO_PORT, SPI_FLASH_CS_PIN) 76 77 /*----- Low layer function -----*/ 78 u8 SPI_FLASH_ReadByte(void); 79 u8 SPI_FLASH_SendByte(u8 byte); 80 u16 SPI_FLASH_SendHalfWord(u16 HalfWord); 81 void SPI_FLASH_WriteEnable(void); 82 void SPI_FLASH_WriteDisable(void); 83 void SPI_FLASH_WaitForWriteEnd(void); 84 85 /* Exported functions ------------------------------------------------------- */ 86 /*----- High layer function -----*/ 87 void SPI_FLASH_Init(void); 88 void SPI_Flash_PowerDown(void); 89 void SPI_Flash_WAKEUP(void); 90 void SPI_FLASH_SectorErase(u32 SectorAddr); 91 void SPI_FLASH_ChipErase(void); 92 void SPI_FLASH_PageWrite(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite); 93 void SPI_FLASH_BufferWrite(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite); 94 void SPI_FLASH_BufferRead(u8* pBuffer, u32 ReadAddr, u16 NumByteToRead); 95 void SPI_FLASH_StartReadSequence(u32 ReadAddr); 96 u32 SPI_FLASH_ReadJedecID(void); 97 u32 SPI_FLASH_ReadDeviceID(void); 98 99 #endif
spi_config.c
1 #include "stm32f10x.h" 2 #include "stm32f10x_gpio.h" 3 #include "stm32f10x_rcc.h" 4 #include "stm32f10x_rtc.h" 5 #include "stm32f10x_spi.h" 6 #include "flashspi_config.h" 7 #include "spi_config.h" 8 9 //Flash pin map:FlashCS-PB9 MISO-PA6 MOSI-PA7 SCK-PA5 SDCS-PD11 10 void SPI_FLASH_Init(void) 11 { 12 SPI_InitTypeDef SPI_InitStructure; 13 GPIO_InitTypeDef GPIO_InitStructure; 14 15 16 /* Enable SPI1 and GPIO clocks */ 17 /*!< SPI_FLASH_SPI_CS_GPIO, SPI_FLASH_SPI_MOSI_GPIO, 18 SPI_FLASH_SPI_MISO_GPIO, SPI_FLASH_SPI_DETECT_GPIO 19 and SPI_FLASH_SPI_SCK_GPIO Periph clock enable */ 20 RCC_APB2PeriphClockCmd(SPI_FLASH_CS_GPIO_CLK | SPI_FLASH_SPI_MOSI_GPIO_CLK | 21 SPI_FLASH_SPI_MISO_GPIO_CLK | SPI_FLASH_SPI_SCK_GPIO_CLK | 22 RCC_APB2Periph_GPIOD, ENABLE); 23 24 /* Disable SD CS */ 25 GPIO_InitStructure.GPIO_Pin = SD_CS_PIN; 26 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 27 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 28 GPIO_Init(SD_CS_GPIO_PORT, &GPIO_InitStructure); 29 GPIO_SetBits(SD_CS_GPIO_PORT, SD_CS_PIN); 30 31 /*!< SPI_FLASH_SPI Periph clock enable */ 32 RCC_APB2PeriphClockCmd(SPI_FLASH_SPI_CLK, ENABLE); 33 /*!< AFIO Periph clock enable */ 34 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); 35 /*!< Remap SPI3 Pins */ 36 //GPIO_PinRemapConfig(GPIO_Remap_SPI3,ENABLE); 37 38 /*!< Configure SPI_FLASH_SPI pins: SCK */ 39 GPIO_InitStructure.GPIO_Pin = SPI_FLASH_SPI_SCK_PIN; 40 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 41 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 42 GPIO_Init(SPI_FLASH_SPI_SCK_GPIO_PORT, &GPIO_InitStructure); 43 44 /*!< Configure SPI_FLASH_SPI pins: MISO */ 45 GPIO_InitStructure.GPIO_Pin = SPI_FLASH_SPI_MISO_PIN; 46 GPIO_Init(SPI_FLASH_SPI_MISO_GPIO_PORT, &GPIO_InitStructure); 47 48 /*!< Configure SPI_FLASH_SPI pins: MOSI */ 49 GPIO_InitStructure.GPIO_Pin = SPI_FLASH_SPI_MOSI_PIN; 50 GPIO_Init(SPI_FLASH_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure); 51 52 /*!< Configure SPI_FLASH_SPI_CS_PIN pin: SPI_FLASH Card CS pin */ 53 GPIO_InitStructure.GPIO_Pin = SPI_FLASH_CS_PIN; 54 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 55 GPIO_Init(SPI_FLASH_CS_GPIO_PORT, &GPIO_InitStructure); 56 57 /* Deselect the FLASH: Chip Select high */ 58 SPI_FLASH_CS_HIGH(); 59 60 /* SPI1 configuration */ 61 // W25X16: data input on the DIO pin is sampled on the rising edge of the CLK. 62 // Data on the DO and DIO pins are clocked out on the falling edge of CLK. 63 SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; 64 SPI_InitStructure.SPI_Mode = SPI_Mode_Master; 65 SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; 66 SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; 67 SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; 68 SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; 69 SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; 70 SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; 71 SPI_InitStructure.SPI_CRCPolynomial = 7; 72 SPI_Init(SPI1, &SPI_InitStructure); 73 74 /* Enable SPI1 */ 75 SPI_Cmd(SPI1, ENABLE); 76 } 77 78 u8 SPI_FLASH_ReadByte(void) 79 { 80 return SPI1_ReadWriteByte(0xFF); 81 } 82 83 u8 SPI_FLASH_SendByte(u8 byte) 84 { 85 return SPI1_ReadWriteByte(byte); 86 } 87 88 /******************************************************************************* 89 * Function Name : SPI_FLASH_WriteEnable 90 * Description : Enables the write access to the FLASH. 91 * Input : None 92 * Output : None 93 * Return : None 94 *******************************************************************************/ 95 void SPI_FLASH_WriteEnable(void) 96 { 97 /* Select the FLASH: Chip Select low */ 98 SPI_FLASH_CS_LOW(); 99 100 /* Send "Write Enable" instruction */ 101 SPI_FLASH_SendByte(W25X_WriteEnable); 102 103 /* Deselect the FLASH: Chip Select high */ 104 SPI_FLASH_CS_HIGH(); 105 } 106 107 /******************************************************************************* 108 * Function Name : SPI_FLASH_WriteDisable 109 * Description : Disables the write access to the FLASH. 110 * Input : None 111 * Output : None 112 * Return : None 113 *******************************************************************************/ 114 void SPI_FLASH_WriteDisable(void) 115 { 116 /* Select the FLASH: Chip Select low */ 117 SPI_FLASH_CS_LOW(); 118 119 /* Send "Write Enable" instruction */ 120 SPI_FLASH_SendByte(W25X_WriteDisable); 121 122 /* Deselect the FLASH: Chip Select high */ 123 SPI_FLASH_CS_HIGH(); 124 } 125 126 /******************************************************************************* 127 * Function Name : SPI_FLASH_WaitForWriteEnd 128 * Description : Polls the status of the Write In Progress (WIP) flag in the 129 * FLASH's status register and loop until write opertaion 130 * has completed. 131 * Input : None 132 * Output : None 133 * Return : None 134 *******************************************************************************/ 135 void SPI_FLASH_WaitForWriteEnd(void) 136 { 137 u8 FLASH_Status = 0; 138 139 /* Select the FLASH: Chip Select low */ 140 SPI_FLASH_CS_LOW(); 141 142 /* Send "Read Status Register" instruction */ 143 SPI_FLASH_SendByte(W25X_ReadStatusReg); 144 145 /* Loop as long as the memory is busy with a write cycle */ 146 do 147 { 148 /* Send a dummy byte to generate the clock needed by the FLASH 149 and put the value of the status register in FLASH_Status variable */ 150 FLASH_Status = SPI_FLASH_SendByte(Dummy_Byte); 151 152 } 153 while ((FLASH_Status & WIP_Flag) == SET); /* Write in progress */ 154 155 /* Deselect the FLASH: Chip Select high */ 156 SPI_FLASH_CS_HIGH(); 157 } 158 159 //进入掉电模式 160 void SPI_Flash_PowerDown(void) 161 { 162 /* Select the FLASH: Chip Select low */ 163 SPI_FLASH_CS_LOW(); 164 165 /* Send "Power Down" instruction */ 166 SPI_FLASH_SendByte(W25X_PowerDown); 167 168 /* Deselect the FLASH: Chip Select high */ 169 SPI_FLASH_CS_HIGH(); 170 } 171 172 //唤醒 173 void SPI_Flash_WAKEUP(void) 174 { 175 /* Select the FLASH: Chip Select low */ 176 SPI_FLASH_CS_LOW(); 177 178 /* Send "Power Down" instruction */ 179 SPI_FLASH_SendByte(W25X_ReleasePowerDown); 180 181 /* Deselect the FLASH: Chip Select high */ 182 SPI_FLASH_CS_HIGH(); //等待TRES1 183 } 184 185 /******************************************************************************* 186 * Function Name : SPI_FLASH_SectorErase 187 * Description : Erases the specified FLASH sector. 188 * Input : SectorAddr: address of the sector to erase. 189 * Output : None 190 * Return : None 191 *******************************************************************************/ 192 void SPI_FLASH_SectorErase(u32 SectorAddr) 193 { 194 /* Send write enable instruction */ 195 SPI_FLASH_WriteEnable(); 196 197 /* Sector Erase */ 198 /* Select the FLASH: Chip Select low */ 199 SPI_FLASH_CS_LOW(); 200 /* Send Sector Erase instruction */ 201 SPI_FLASH_SendByte(W25X_SectorErase); 202 /* Send SectorAddr high nibble address byte */ 203 SPI_FLASH_SendByte((SectorAddr & 0xFF0000) >> 16); 204 /* Send SectorAddr medium nibble address byte */ 205 SPI_FLASH_SendByte((SectorAddr & 0xFF00) >> 8); 206 /* Send SectorAddr low nibble address byte */ 207 SPI_FLASH_SendByte(SectorAddr & 0xFF); 208 /* Deselect the FLASH: Chip Select high */ 209 SPI_FLASH_CS_HIGH(); 210 211 /* Wait the end of Flash writing */ 212 SPI_FLASH_WaitForWriteEnd(); 213 } 214 215 /******************************************************************************* 216 * Function Name : SPI_FLASH_BulkErase 217 * Description : Erases the entire FLASH. 218 * Input : None 219 * Output : None 220 * Return : None 221 *******************************************************************************/ 222 void SPI_FLASH_ChipErase(void) 223 { 224 /* Send write enable instruction */ 225 SPI_FLASH_WriteEnable(); 226 227 /* Bulk Erase */ 228 /* Select the FLASH: Chip Select low */ 229 SPI_FLASH_CS_LOW(); 230 /* Send Bulk Erase instruction */ 231 SPI_FLASH_SendByte(W25X_ChipErase); 232 /* Deselect the FLASH: Chip Select high */ 233 SPI_FLASH_CS_HIGH(); 234 235 /* Wait the end of Flash writing */ 236 SPI_FLASH_WaitForWriteEnd(); 237 } 238 239 /******************************************************************************* 240 * Function Name : SPI_FLASH_PageWrite 241 * Description : Writes more than one byte to the FLASH with a single WRITE 242 * cycle(Page WRITE sequence). The number of byte can't exceed 243 * the FLASH page size. 244 * Input : - pBuffer : pointer to the buffer containing the data to be 245 * written to the FLASH. 246 * - WriteAddr : FLASH's internal address to write to. 247 * - NumByteToWrite : number of bytes to write to the FLASH, 248 * must be equal or less than "SPI_FLASH_PageSize" value. 249 * Output : None 250 * Return : None 251 *******************************************************************************/ 252 void SPI_FLASH_PageWrite(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite) 253 { 254 /* Enable the write access to the FLASH */ 255 SPI_FLASH_WriteEnable(); 256 257 /* Select the FLASH: Chip Select low */ 258 SPI_FLASH_CS_LOW(); 259 /* Send "Write to Memory " instruction */ 260 SPI_FLASH_SendByte(W25X_PageProgram); 261 /* Send WriteAddr high nibble address byte to write to */ 262 SPI_FLASH_SendByte((WriteAddr & 0xFF0000) >> 16); 263 /* Send WriteAddr medium nibble address byte to write to */ 264 SPI_FLASH_SendByte((WriteAddr & 0xFF00) >> 8); 265 /* Send WriteAddr low nibble address byte to write to */ 266 SPI_FLASH_SendByte(WriteAddr & 0xFF); 267 268 if(NumByteToWrite > SPI_FLASH_PerWritePageSize) 269 { 270 NumByteToWrite = SPI_FLASH_PerWritePageSize; 271 printf("\n\r Err: SPI_FLASH_PageWrite too large!"); 272 } 273 274 /* while there is data to be written on the FLASH */ 275 while (NumByteToWrite--) 276 { 277 /* Send the current byte */ 278 SPI_FLASH_SendByte(*pBuffer); 279 /* Point on the next byte to be written */ 280 pBuffer++; 281 } 282 283 /* Deselect the FLASH: Chip Select high */ 284 SPI_FLASH_CS_HIGH(); 285 286 /* Wait the end of Flash writing */ 287 SPI_FLASH_WaitForWriteEnd(); 288 } 289 290 /******************************************************************************* 291 * Function Name : SPI_FLASH_BufferWrite 292 * Description : Writes block of data to the FLASH. In this function, the 293 * number of WRITE cycles are reduced, using Page WRITE sequence. 294 * Input : - pBuffer : pointer to the buffer containing the data to be 295 * written to the FLASH. 296 * - WriteAddr : FLASH's internal address to write to. 297 * - NumByteToWrite : number of bytes to write to the FLASH. 298 * Output : None 299 * Return : None 300 *******************************************************************************/ 301 void SPI_FLASH_BufferWrite(u8* pBuffer, u32 WriteAddr, u16 NumByteToWrite) 302 { 303 u8 NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0, temp = 0; 304 305 Addr = WriteAddr % SPI_FLASH_PageSize; 306 count = SPI_FLASH_PageSize - Addr; 307 NumOfPage = NumByteToWrite / SPI_FLASH_PageSize; 308 NumOfSingle = NumByteToWrite % SPI_FLASH_PageSize; 309 310 if (Addr == 0) /* WriteAddr is SPI_FLASH_PageSize aligned */ 311 { 312 if (NumOfPage == 0) /* NumByteToWrite < SPI_FLASH_PageSize */ 313 { 314 SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumByteToWrite); 315 } 316 else /* NumByteToWrite > SPI_FLASH_PageSize */ 317 { 318 while (NumOfPage--) 319 { 320 SPI_FLASH_PageWrite(pBuffer, WriteAddr, SPI_FLASH_PageSize); 321 WriteAddr += SPI_FLASH_PageSize; 322 pBuffer += SPI_FLASH_PageSize; 323 } 324 325 SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumOfSingle); 326 } 327 } 328 else /* WriteAddr is not SPI_FLASH_PageSize aligned */ 329 { 330 if (NumOfPage == 0) /* NumByteToWrite < SPI_FLASH_PageSize */ 331 { 332 if (NumOfSingle > count) /* (NumByteToWrite + WriteAddr) > SPI_FLASH_PageSize */ 333 { 334 temp = NumOfSingle - count; 335 336 SPI_FLASH_PageWrite(pBuffer, WriteAddr, count); 337 WriteAddr += count; 338 pBuffer += count; 339 340 SPI_FLASH_PageWrite(pBuffer, WriteAddr, temp); 341 } 342 else 343 { 344 SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumByteToWrite); 345 } 346 } 347 else /* NumByteToWrite > SPI_FLASH_PageSize */ 348 { 349 NumByteToWrite -= count; 350 NumOfPage = NumByteToWrite / SPI_FLASH_PageSize; 351 NumOfSingle = NumByteToWrite % SPI_FLASH_PageSize; 352 353 SPI_FLASH_PageWrite(pBuffer, WriteAddr, count); 354 WriteAddr += count; 355 pBuffer += count; 356 357 while (NumOfPage--) 358 { 359 SPI_FLASH_PageWrite(pBuffer, WriteAddr, SPI_FLASH_PageSize); 360 WriteAddr += SPI_FLASH_PageSize; 361 pBuffer += SPI_FLASH_PageSize; 362 } 363 364 if (NumOfSingle != 0) 365 { 366 SPI_FLASH_PageWrite(pBuffer, WriteAddr, NumOfSingle); 367 } 368 } 369 } 370 } 371 372 /******************************************************************************* 373 * Function Name : SPI_FLASH_BufferRead 374 * Description : Reads a block of data from the FLASH. 375 * Input : - pBuffer : pointer to the buffer that receives the data read 376 * from the FLASH. 377 * - ReadAddr : FLASH's internal address to read from. 378 * - NumByteToRead : number of bytes to read from the FLASH. 379 * Output : None 380 * Return : None 381 *******************************************************************************/ 382 void SPI_FLASH_BufferRead(u8* pBuffer, u32 ReadAddr, u16 NumByteToRead) 383 { 384 /* Select the FLASH: Chip Select low */ 385 SPI_FLASH_CS_LOW(); 386 387 /* Send "Read from Memory " instruction */ 388 SPI_FLASH_SendByte(W25X_ReadData); 389 390 /* Send ReadAddr high nibble address byte to read from */ 391 SPI_FLASH_SendByte((ReadAddr & 0xFF0000) >> 16); 392 /* Send ReadAddr medium nibble address byte to read from */ 393 SPI_FLASH_SendByte((ReadAddr& 0xFF00) >> 8); 394 /* Send ReadAddr low nibble address byte to read from */ 395 SPI_FLASH_SendByte(ReadAddr & 0xFF); 396 397 while (NumByteToRead--) /* while there is data to be read */ 398 { 399 /* Read a byte from the FLASH */ 400 *pBuffer = SPI_FLASH_SendByte(Dummy_Byte); 401 /* Point to the next location where the byte read will be saved */ 402 pBuffer++; 403 } 404 405 /* Deselect the FLASH: Chip Select high */ 406 SPI_FLASH_CS_HIGH(); 407 } 408 409 /******************************************************************************* 410 * Function Name : SPI_FLASH_StartReadSequence 411 * Description : Initiates a read data byte (READ) sequence from the Flash. 412 * This is done by driving the /CS line low to select the device, 413 * then the READ instruction is transmitted followed by 3 bytes 414 * address. This function exit and keep the /CS line low, so the 415 * Flash still being selected. With this technique the whole 416 * content of the Flash is read with a single READ instruction. 417 * Input : - ReadAddr : FLASH's internal address to read from. 418 * Output : None 419 * Return : None 420 *******************************************************************************/ 421 void SPI_FLASH_StartReadSequence(u32 ReadAddr) 422 { 423 /* Select the FLASH: Chip Select low */ 424 SPI_FLASH_CS_LOW(); 425 426 /* Send "Read from Memory " instruction */ 427 SPI_FLASH_SendByte(W25X_ReadData); 428 429 /* Send the 24-bit address of the address to read from -----------------------*/ 430 /* Send ReadAddr high nibble address byte */ 431 SPI_FLASH_SendByte((ReadAddr & 0xFF0000) >> 16); 432 /* Send ReadAddr medium nibble address byte */ 433 SPI_FLASH_SendByte((ReadAddr& 0xFF00) >> 8); 434 /* Send ReadAddr low nibble address byte */ 435 SPI_FLASH_SendByte(ReadAddr & 0xFF); 436 } 437 438 /******************************************************************************* 439 * Function Name : SPI_FLASH_ReadJedecID 440 * Description : Reads FLASH identification. 441 * Input : None 442 * Output : None 443 * Return : FLASH identification 444 *******************************************************************************/ 445 u32 SPI_FLASH_ReadJedecID(void) 446 { 447 u32 Temp = 0, Temp0 = 0, Temp1 = 0, Temp2 = 0; 448 449 /* Select the FLASH: Chip Select low */ 450 SPI_FLASH_CS_LOW(); 451 452 /* Send "RDID " instruction */ 453 SPI_FLASH_SendByte(W25X_JedecDeviceID); 454 455 /* Read a byte from the FLASH */ 456 Temp0 = SPI_FLASH_SendByte(Dummy_Byte); 457 458 /* Read a byte from the FLASH */ 459 Temp1 = SPI_FLASH_SendByte(Dummy_Byte); 460 461 /* Read a byte from the FLASH */ 462 Temp2 = SPI_FLASH_SendByte(Dummy_Byte); 463 464 /* Deselect the FLASH: Chip Select high */ 465 SPI_FLASH_CS_HIGH(); 466 467 Temp = (Temp0 << 16) | (Temp1 << 8) | Temp2; 468 469 return Temp; 470 } 471 /******************************************************************************* 472 * Function Name : SPI_FLASH_ReadID 473 * Description : Reads FLASH identification. 474 * Input : None 475 * Output : None 476 * Return : FLASH identification 477 *******************************************************************************/ 478 u32 SPI_FLASH_ReadDeviceID(void) 479 { 480 u32 Temp = 0; 481 482 /* Select the FLASH: Chip Select low */ 483 SPI_FLASH_CS_LOW(); 484 485 /* Send "RDID " instruction */ 486 SPI_FLASH_SendByte(W25X_DeviceID); 487 SPI_FLASH_SendByte(Dummy_Byte); 488 SPI_FLASH_SendByte(Dummy_Byte); 489 SPI_FLASH_SendByte(Dummy_Byte); 490 491 /* Read a byte from the FLASH */ 492 Temp = SPI_FLASH_SendByte(Dummy_Byte); 493 494 /* Deselect the FLASH: Chip Select high */ 495 SPI_FLASH_CS_HIGH(); 496 497 return Temp; 498 }
SPI_FLASH_Init();
printf("\r\n SPI_FLASH_ReadJedecID:%x \n\r",SPI_FLASH_ReadJedecID());
printf("\r\n SPI_FLASH_ReadDeviceID:%x \n\r",SPI_FLASH_ReadDeviceID());
SPI_FLASH_SectorErase(0);
for(sd_size=0;sd_size<512;sd_size++)buf[sd_size]=0x55;
SPI_FLASH_BufferWrite(buf,0,500);
SPI_FLASH_BufferRead(buf,0,512);
for(sd_size=0;sd_size<512;sd_size++)printf("%x ",buf[sd_size]);//打印0扇区数据