文章目录
【相关文章导航】
[FlashDB]基于GD32纯代码裸机移植FlashDB数据库——一、介绍
[FlashDB]基于GD32纯代码裸机移植FlashDB数据库——二、移植前准备
[FlashDB]基于GD32纯代码裸机移植FlashDB数据库——三、移植SFUD
[FlashDB]基于GD32纯代码裸机移植FlashDB数据库——四、移植Fal
[FlashDB]基于GD32纯代码裸机移植FlashDB数据库——五、移植FlashDB
四、移植前准备
4.1 下载源码
1、 下载官方源码;地址
2、 下载我自己整理过的源码;地址
4.2 官方源码目录结构介绍
说明:SFUD源码位于 /demos\stm32f405rg_spi_flash\sfud/ 文件夹下。
4.3 工程文件夹下创建源码文件夹
在工程文件夹下创建drv-flashDB文件夹,用来存放移植过来的源码。
在drv-flashDB文件夹下分别创建四个文件夹,
fal:存放Fal硬件抽象层源码。
flashDB:存访FlashDB源码。
flashDB_samples:(非必须)存放FlashDB测试代码。
sfud:存放SFUD万能SPI Flash驱动库源码。
4.4 在Keil中创建文件组
4.5 在Keil中包含头文件路径
4.6 实现SPI初始化及Flash读写函数
-
SPI初始化
/*引脚使用说明: SPI:SPI0 SPI_CS:PD2 SPI_CLK:PB3 SPI_MISO:PB4 SPI_MOSI:PB5 */ void bsp_spi_flash_init(void) { rcu_periph_clock_enable(RCU_GPIOD); rcu_periph_clock_enable(RCU_GPIOB); /* SPI0_CS GPIO pin configuration */ gpio_mode_set(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_2); gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2); gpio_bit_set(GPIOD, GPIO_PIN_2); /* SPI0_CLK GPIO pin configuration */ gpio_af_set(GPIOB, GPIO_AF_5, GPIO_PIN_3); gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_3); gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_3); /* SPI0_MISO GPIO pin configuration */ gpio_af_set(GPIOB, GPIO_AF_5, GPIO_PIN_4); gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_4); gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_4); /* SPI0_MOSI GPIO pin configuration */ gpio_af_set(GPIOB, GPIO_AF_5, GPIO_PIN_5); gpio_mode_set(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_5); gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_5); SPI_FLASH_CS_HIGH(); //CS引脚拉高 spi_parameter_struct spi_init_struct; rcu_periph_clock_enable(RCU_SPI0); /* SPI0参数配置*/ spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX; spi_init_struct.device_mode = SPI_MASTER; spi_init_struct.frame_size = SPI_FRAMESIZE_8BIT; spi_init_struct.clock_polarity_phase = SPI_CK_PL_LOW_PH_1EDGE; spi_init_struct.nss = SPI_NSS_SOFT; //使用软件控制CS spi_init_struct.prescale = SPI_PSC_32; spi_init_struct.endian = SPI_ENDIAN_MSB; spi_init(SPI0, &spi_init_struct); /* 配置SPI0字节访问FIFO */ spi_fifo_access_size_config(SPI0, SPI_BYTE_ACCESS); /* 配置CRC多项式 */ spi_crc_polynomial_set(SPI0, 7); /* 使能SPI0 */ spi_enable(SPI0); }
-
Flash写函数
/* 通过SPI接口发送一个字节,并返回从SPI总线接收的字节 */ uint8_t bsp_spi_flash_send_byte(uint8_t byte) { /* loop while data register in not emplty */ while(RESET == spi_i2s_flag_get(SPI0, SPI_FLAG_TBE)); /* send byte through the FLASH_SPIx peripheral */ spi_i2s_data_transmit(SPI0, byte); /* wait to receive a byte */ while(RESET == spi_i2s_flag_get(SPI0, SPI_FLAG_RBNE)); /* return the byte read from the SPI bus */ return(spi_i2s_data_receive(SPI0)); } /* 向闪存写入多个字节 */ void user_spi_flash_page_write(const uint8_t* pbuffer, uint16_t num_byte_to_write) { /* select the flash: chip select low */ SPI_FLASH_CS_LOW(); /* while there is data to be written on the flash */ while(num_byte_to_write--) { /* send the current byte */ bsp_spi_flash_send_byte(*pbuffer); /* point on the next byte to be written */ pbuffer++; } }
-
Flash读函数
/* 读取SPI闪存中的一个字节 */ uint8_t bsp_spi_flash_read_byte(void) { return(bsp_spi_flash_send_byte(DUMMY_BYTE)); } /* 从闪存读取一个数据块 */ void user_spi_flash_buffer_read(uint8_t *pbuffer, uint16_t num_byte_to_read) { /* select the flash: chip slect low */ SPI_FLASH_CS_LOW(); /* while there is data to be read */ while(num_byte_to_read--) { /* read a byte from the flash */ *pbuffer = bsp_spi_flash_send_byte(DUMMY_BYTE); /* point to the next location where the byte read will be saved */ pbuffer++; } }
4.7 实现printf函数通过串口打印
至此前期准备都已完成,接下来进行移植。。。