1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author            Notes
8  * 2023-10-19     ChuShicheng       first version
9  */
10 #include "drv_flash.h"
11 #include "board.h"
12 #include "hardware/flash.h"
13 
14 #ifdef BSP_USING_ON_CHIP_FLASH
15 
16 #define DBG_LEVEL DBG_LOG
17 #define LOG_TAG  "DRV.FLASH"
18 #include <rtdbg.h>
19 
20 #define FLASH_SIZE           2 * 1024 * 1024
21 #define FLASH_START_ADRESS   0x0000000
22 
_flash_read(rt_uint32_t addr,rt_uint8_t * buf,size_t size)23 int _flash_read(rt_uint32_t addr, rt_uint8_t *buf, size_t size)
24 {
25     rt_memcpy(buf, (const void *)(XIP_BASE + addr), size);
26 
27     return size;
28 }
29 
_flash_write(rt_uint32_t addr,const rt_uint8_t * buf,size_t size)30 int _flash_write(rt_uint32_t addr, const rt_uint8_t *buf, size_t size)
31 {
32     if (addr % FLASH_PAGE_SIZE != 0)
33     {
34         LOG_E("write addr must be %d-byte alignment", FLASH_PAGE_SIZE);
35         return -RT_EINVAL;
36     }
37 
38     if (size % FLASH_PAGE_SIZE != 0)
39     {
40         LOG_E("write size must be %d-byte alignment", FLASH_PAGE_SIZE);
41         return -RT_EINVAL;
42     }
43 
44     flash_range_program(addr, buf, size);
45 
46     return size;
47 }
48 
_flash_erase(rt_uint32_t addr,size_t size)49 int _flash_erase(rt_uint32_t addr, size_t size)
50 {
51     if(size % FLASH_SECTOR_SIZE)
52     {
53         LOG_E("erase size must be %d-byte alignment", FLASH_SECTOR_SIZE);
54     }
55 
56     flash_range_erase(addr, size);
57 
58     return size;
59 }
60 
61 #ifdef RT_USING_FAL
62 
63 #include "fal.h"
64 
65 static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size);
66 static int fal_flash_write(long offset, const rt_uint8_t *buf, size_t size);
67 static int fal_flash_erase(long offset, size_t size);
68 
69 const struct fal_flash_dev _onchip_flash =
70 {
71     "onchip_flash",
72     FLASH_START_ADRESS,
73     FLASH_SIZE,
74     FLASH_PAGE_SIZE,
75     {
76         NULL,
77         fal_flash_read,
78         fal_flash_write,
79         fal_flash_erase
80     }
81 };
82 
fal_flash_read(long offset,rt_uint8_t * buf,size_t size)83 static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size)
84 {
85     return _flash_read(_onchip_flash.addr + offset, buf, size);
86 }
87 
fal_flash_write(long offset,const rt_uint8_t * buf,size_t size)88 static int fal_flash_write(long offset, const rt_uint8_t *buf, size_t size)
89 {
90     return _flash_write(_onchip_flash.addr + offset, buf, size);
91 }
92 
fal_flash_erase(long offset,size_t size)93 static int fal_flash_erase(long offset, size_t size)
94 {
95     return _flash_erase(_onchip_flash.addr + offset, size);
96 }
97 
rt_hw_on_chip_flash_init(void)98 static int rt_hw_on_chip_flash_init(void)
99 {
100     fal_init();
101     return RT_EOK;
102 }
103 INIT_COMPONENT_EXPORT(rt_hw_on_chip_flash_init);
104 
105 #endif /* RT_USING_FAL */
106 
107 #endif /* BSP_USING_ON_CHIP_FLASH */
108