.    SPI Falsh 采用了GD自家的芯片:GD25Q16,参数如下:

存储器构架(格式)FLASH
存储器接口类型SPI
存储器容量16Mb (2M x 8)
工作电压2.7V ~ 3.6V
存储器类型Non-Volatile

2MB的容量,存个字库已经戳戳有余了呢!

接下来就是利用历程的代码驱动它,这里做了稍许修改,写了一个测试小函数。

void SPI_Flash_Test(void)
  • {
  •         uint8_t  tx_buffer[BUFFER_SIZE];
  •         uint8_t  rx_buffer[BUFFER_SIZE];
  •        
  •         printf("\r\nWrite to tx_buffer:\r\n");
  •        
  •         for(int i = 0; i < BUFFER_SIZE; i ++)
  •         {
  •                   tx_buffer = i;
  •                    printf("0x%02X ",tx_buffer);
  •             if(31 == i%32)
  •                 printf("\n\r");
  •         }
  •        
  •   /* erases the specified flash sector */
  •         spi_flash_sector_erase(FLASH_WRITE_ADDRESS);
  •         /* write tx_buffer data to the flash */
  •         spi_flash_buffer_write(tx_buffer,FLASH_WRITE_ADDRESS,BUFFER_SIZE);
  •          printf("\n\r\n\rRead from rx_buffer:\n\r\n\r");
  •         /* read a block of data from the flash to rx_buffer */
  •         spi_flash_buffer_read(rx_buffer,FLASH_READ_ADDRESS,BUFFER_SIZE);
  •         /* printf rx_buffer value */
  •         for(int i = 0; i < BUFFER_SIZE; i ++)
  •         {
  •                 printf("0x%02X ", rx_buffer);
  •                 if(31 == i%32)
  •                         printf("\n\r");
  •         }
  • }
  • 复制代码

    且看头文件:


    • #ifndef GD25QXX_H
    • #define GD25QXX_H

    • #include "gd32e50x.h"
    • #include <string.h>
    • #include <stdio.h>

    • #define BUFFER_SIZE                   SPI_FLASH_PAGE_SIZE // 256


    • #define  VERSION_ID                    "1.0.0"
    • #define  SFLASH_ID                     0xC84015
    • #define  FLASH_WRITE_ADDRESS           0x000000
    • #define  FLASH_READ_ADDRESS            FLASH_WRITE_ADDRESS


    • #define WRITE            0x02     /* write to memory instruction */
    • #define WRSR             0x01     /* write status register instruction */
    • #define WREN             0x06     /* write enable instruction */

    • #define READ             0x03     /* read from memory instruction */
    • #define RDSR             0x05     /* read status register instruction  */
    • #define RDID             0x9F     /* read identification */
    • #define SE               0x20     /* sector erase instruction */
    • #define BE               0xC7     /* bulk erase instruction */

    • #define WIP_FLAG         0x01     /* write in progress(wip)flag */
    • #define DUMMY_BYTE       0xA5

    • #define  SPI_FLASH_PAGE_SIZE           0x100
    • #define  SPI_FLASH_CS_LOW()            gpio_bit_reset(GPIOE, GPIO_PIN_3)
    • #define  SPI_FLASH_CS_HIGH()           gpio_bit_set(GPIOE, GPIO_PIN_3)

    • /* initialize SPI0 GPIO and parameter */
    • void spi_flash_init(void);
    • /* erase the specified flash sector */
    • void spi_flash_sector_erase(uint32_t sector_addr);
    • /* erase the entire flash */
    • void spi_flash_bulk_erase(void);
    • /* write more than one byte to the flash */
    • void spi_flash_page_write(uint8_t* pbuffer,uint32_t write_addr,uint16_t num_byte_to_write);
    • /* write block of data to the flash */
    • void spi_flash_buffer_write(uint8_t* pbuffer,uint32_t write_addr,uint16_t num_byte_to_write);
    • /* read a block of data from the flash */
    • void spi_flash_buffer_read(uint8_t* pbuffer,uint32_t read_addr,uint16_t num_byte_to_read);
    • /* read flash identification */
    • uint32_t spi_flash_read_id(void);
    • /* initiate a read data byte (read) sequence from the flash */
    • void spi_flash_start_read_sequence(uint32_t read_addr);
    • /* read a byte from the SPI flash */
    • uint8_t spi_flash_read_byte(void);
    • /* send a byte through the SPI interface and return the byte received from the SPI bus */
    • uint8_t spi_flash_send_byte(uint8_t byte);
    • /* send a half word through the SPI interface and return the half word received from the SPI bus */
    • uint16_t spi_flash_send_halfword(uint16_t half_word);
    • /* enable the write access to the flash */
    • void spi_flash_write_enable(void);
    • /* poll the status of the write in progress (wip) flag in the flash's status register */
    • void spi_flash_wait_for_write_end(void);

    • void SPI_Flash_Test(void);

    • #endif /* GD25QXX_H */

    最后看测试效果吧,嘿嘿:

    153135a7abo0cqc00j6xxt.png.thumb.jpg