使用w25qxx flash

1.CubeMx

  1. new project

change drivers/board.h

1
2
/*#define BSP_USING_ON_CHIP_FLASH*/
#define BSP_USING_ON_CHIP_FLASH
  1. RT-Thread Setting

add packages

image-20220304203615948

  1. onchip flash Tisp

stm32f4_onchip_flash_map

  1. generated by rt-rhread drivers/drv_flash_f4.c

image-20220304205249004

  1. new source file applications/fal_cfg.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#ifndef _FAL_CFG_H_
#define _FAL_CFG_H_

#include <rtconfig.h>
#include <board.h>

//#define NOR_FLASH_DEV_NAME FAL_USING_NOR_FLASH_DEV_NAME

//STM32F4VET6 512K FLASH
#define FLASH_SIZE_GRANULARITY_16K (4*16*1024)
#define FLASH_SIZE_GRANULARITY_64K (1*64*1024)
#define FLASH_SIZE_GRANULARITY_128K (3*128*1024)
/*STM32F4xxx 1024K FLASH
#define FLASH_SIZE_GRANULARITY_16K (4*16*1024)
#define FLASH_SIZE_GRANULARITY_64K (1*64*1024)
#define FLASH_SIZE_GRANULARITY_128K (7*128*1024)
*/

#define STM32_FLASH_START_ADRESS_16K (STM32_FLASH_START_ADRESS)
#define STM32_FLASH_START_ADRESS_64K (STM32_FLASH_START_ADRESS_16K+FLASH_SIZE_GRANULARITY_16K)
#define STM32_FLASH_START_ADRESS_128K (STM32_FLASH_START_ADRESS_64K+FLASH_SIZE_GRANULARITY_64K)

/* ===================== Flash device Configuration ========================= */
extern const struct fal_flash_dev stm32_onchip_flash_16k;
extern const struct fal_flash_dev stm32_onchip_flash_64k;
extern const struct fal_flash_dev stm32_onchip_flash_128k;
extern struct fal_flash_dev nor_flash0;

/* flash device table */
#define FAL_FLASH_DEV_TABLE \
{ \
&stm32_onchip_flash_16k, \
&stm32_onchip_flash_64k, \
&stm32_onchip_flash_128k, \
/*&nor_flash0, */ \
}
/* ====================== Partition Configuration ========================== */
#ifdef FAL_PART_HAS_TABLE_CFG
//boot = (onchip_flash_16k + onchip_flash_64k) = 128k
//dev name ===>> stm32_onchip_flash_16k
/* partition table */
#define FAL_PART_TABLE \
{ \
{FAL_PART_MAGIC_WORD, "bl", "onchip_flash_16k", 0, FLASH_SIZE_GRANULARITY_16K, 0}, \
{FAL_PART_MAGIC_WORD, "bl64", "onchip_flash_64k", 0, FLASH_SIZE_GRANULARITY_64K, 0}, \
{FAL_PART_MAGIC_WORD, "app", "onchip_flash_128k", 0, FLASH_SIZE_GRANULARITY_128K, 0}, \
/* {FAL_PART_MAGIC_WORD, "easyflash", NOR_FLASH_DEV_NAME, 0, 512*1024, 0},*/ \
/* {FAL_PART_MAGIC_WORD, "download", NOR_FLASH_DEV_NAME, 512*1024, 512*1024, 0},*/ \
/* {FAL_PART_MAGIC_WORD, "file", NOR_FLASH_DEV_NAME, 1*1024*1024, 7*1024*1024, 0},*/ \
}
#endif /* FAL_PART_HAS_TABLE_CFG */

#endif /* _FAL_CFG_H_ */
  1. new source file applications/usr_flash.c
1
2
3
4
5
6
7
8
9
10
#include <rtthread.h>
#include "fal.h"

static int usr_flash_init(void)
{
fal_init();
return RT_EOK;
}

INIT_ENV_EXPORT(usr_flash_init);
  1. Build + Download

PuTTY

image-20220304213533067

  1. debug

image-20220304214034449

2.W25Q64


  1. change drivers/board.h
1
2
3
/*#define BSP_USING_SPI1*/
#define BSP_USING_SPI2
/*#define BSP_USING_SPI3*/
  1. RT-Thread Setting

image-20220304214418575

image-20220304223001519

  1. CubeMX Setting

enable spi bus

  1. change applications/usr_flash.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <rtthread.h>
#include "drv_spi.h"
#include "spi_flash_sfud.h"
#include "fal.h"

static int usr_flash_init(void)
{
rt_hw_spi_device_attach("spi2", "spi20", GPIOE, GPIO_PIN_3);
if (RT_NULL == rt_sfud_flash_probe("norflash0", "spi20")) {
return RT_ERROR;
}

fal_init();

return RT_EOK;
}

INIT_ENV_EXPORT(usr_flash_init);
  1. copy file from packages/fal-v0.5.0/samples/porting/fal_flash_sfud_port.c to applications/fal_flash_sfud_port.c

  2. change fal_cfg.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#ifndef _FAL_CFG_H_
#define _FAL_CFG_H_

#include <rtconfig.h>
#include <board.h>

#define NOR_FLASH_DEV_NAME FAL_USING_NOR_FLASH_DEV_NAME

//STM32F4VET6 512K FLASH
#define FLASH_SIZE_GRANULARITY_16K (4*16*1024)
#define FLASH_SIZE_GRANULARITY_64K (1*64*1024)
#define FLASH_SIZE_GRANULARITY_128K (3*128*1024)
/*STM32F4xxx 1024K FLASH
#define FLASH_SIZE_GRANULARITY_16K (4*16*1024)
#define FLASH_SIZE_GRANULARITY_64K (1*64*1024)
#define FLASH_SIZE_GRANULARITY_128K (7*128*1024)
*/

#define STM32_FLASH_START_ADRESS_16K (STM32_FLASH_START_ADRESS)
#define STM32_FLASH_START_ADRESS_64K (STM32_FLASH_START_ADRESS_16K+FLASH_SIZE_GRANULARITY_16K)
#define STM32_FLASH_START_ADRESS_128K (STM32_FLASH_START_ADRESS_64K+FLASH_SIZE_GRANULARITY_64K)

/* ===================== Flash device Configuration ========================= */
extern const struct fal_flash_dev stm32_onchip_flash_16k;
extern const struct fal_flash_dev stm32_onchip_flash_64k;
extern const struct fal_flash_dev stm32_onchip_flash_128k;
extern struct fal_flash_dev nor_flash0;

/* flash device table */
#define FAL_FLASH_DEV_TABLE \
{ \
&stm32_onchip_flash_16k, \
&stm32_onchip_flash_64k, \
&stm32_onchip_flash_128k, \
&nor_flash0, \
}
/* ====================== Partition Configuration ========================== */
#ifdef FAL_PART_HAS_TABLE_CFG
//boot = (onchip_flash_16k + onchip_flash_64k) = 128k
//dev name ==>> stm32_onchip_flash_16k
/* partition table */
#define FAL_PART_TABLE \
{ \
{FAL_PART_MAGIC_WORD, "bl", "onchip_flash_16k", 0, FLASH_SIZE_GRANULARITY_16K, 0}, \
{FAL_PART_MAGIC_WORD, "bl64", "onchip_flash_64k", 0, FLASH_SIZE_GRANULARITY_64K, 0}, \
{FAL_PART_MAGIC_WORD, "app", "onchip_flash_128k", 0, FLASH_SIZE_GRANULARITY_128K, 0}, \
{FAL_PART_MAGIC_WORD, "easyflash", NOR_FLASH_DEV_NAME, 0, 512*1024, 0}, \
{FAL_PART_MAGIC_WORD, "download", NOR_FLASH_DEV_NAME, 512*1024, 512*1024, 0}, \
{FAL_PART_MAGIC_WORD, "file", NOR_FLASH_DEV_NAME, 1*1024*1024, 7*1024*1024, 0}, \
}
#endif /* FAL_PART_HAS_TABLE_CFG */

#endif /* _FAL_CFG_H_ */
  1. Build + Download

  2. debug

3.fal test

image-20220304225042638