├── .gitignore ├── LICENSE ├── README.md ├── README_ZH.md ├── SConscript ├── docs ├── fal_api.md ├── fal_api_en.md └── figures │ ├── fal-api-en.png │ ├── fal-api.png │ ├── fal-port-en.png │ ├── fal-port.png │ ├── fal_framework-en.png │ └── fal_framework.png ├── inc ├── fal.h └── fal_def.h ├── samples ├── README.md └── porting │ ├── README.md │ ├── fal_cfg.h │ ├── fal_flash_sfud_port.c │ └── fal_flash_stm32f2_port.c └── src ├── fal.c ├── fal_flash.c ├── fal_partition.c └── fal_rtt.c /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Attention 2 | ## ⚠️ This repository is no longer maintained any more. Please move to: https://github.com/RT-Thread/rt-thread/tree/master/components/fal 3 | 4 | # FAL:Flash Abstraction Layer 5 | 6 | [Chinese](./README_ZH.md) | English 7 | 8 | ## 1. Introduction to FAL 9 | 10 | The Flash abstraction layer is an abstraction layer for the management and operation of Flash and Flash-based partitions. The upper layer unifies the Flash and partition operation API (the framework diagram is shown below), and has the following characteristics: 11 | 12 | - Supports static and configurable partition table and can associate multiple Flash devices; 13 | - The partition table supports **automatic loading**. Avoid the problem that the partition table is defined multiple times in multiple firmware projects; 14 | - The code is streamlined, **no dependency** on the operating system, and can run on bare metal platforms, such as Bootloader that has certain requirements for resources; 15 | - Unified operation interface. Ensure the file system, OTA, NVM (for example: [EasyFlash](https://github.com/armink-rtt-pkgs/EasyFlash)) and other components that have certain dependencies on Flash, and the reusability of the underlying Flash driver; 16 | - Comes with Finsh/MSH-based test commands, which can be operated in byte addressing mode (read, write, and erase) Flash or partition through Shell, which is convenient for developers to debug and test; 17 | 18 | ![FAL framework](docs/figures/fal_framework-en.png) 19 | 20 | ### 1.1,Open FAL 21 | 22 | To use fal package, you need to select it in the RT-Thread package manager. The specific path is as follows: 23 | 24 | ``` 25 | RT-Thread online packages 26 | system packages ---> 27 | --- fal: Flash Abstraction Layer implement. Manage flash device and partition. 28 | [*] Enable debug log output 29 | [*] FAL partition table config has defined on'fal_cfg.h' 30 | (onchip) The flash device which saving partition table 31 | (65536) The patition table end address relative to flash device offset. 32 | [ ] FAL uses SFUD drivers 33 | (norflash0) The name of the device used by FAL (NEW) 34 | version (latest) ---> 35 | ``` 36 | 37 | The configuration instructions for each function are as follows: 38 | 39 | - Enable debug log output (enabled by default); 40 | - Whether the partition table is defined in `fal_cfg.h` (enabled by default). If you turn off this option, fal will automatically go to the specified location of the designated Flash to retrieve and load the partition table. For the specific configuration, see the following two options; 41 | 42 | - Flash device storing the partition table; 43 | - The **end address** of the partition table is located at the offset on the Flash device. fal will retrieve the partition table from this address and read it directly to the top of the Flash. If you are not sure about the specific location of the partition table, you can also configure it as the end address of the Flash, fal will retrieve the entire Flash, and the retrieval time may increase. 44 | - Enable FAL migration files for SFUD (closed by default); 45 | - The name of the FLASH device passed in when calling the `rt_sfud_flash_probe` function should be entered (you can also check the name of the Block Device through the list_device command). This name corresponds to the Flash name in the partition table. Only when the device name is set correctly can the read and write operations on FLASH be completed. 46 | 47 | Then let the RT-Thread package manager automatically update, or use the `pkgs --update` command to update the package to the BSP. 48 | 49 | ### 1.2, FAL directory 50 | 51 | | Name | Description | 52 | | ------- | ---------- | 53 | | inc | Header file directory | 54 | | src | Source Code Directory | 55 | | samples | Sample catalog | 56 | 57 | ### 1.3, FAL API 58 | 59 | The FAL-related API is shown in the figure, [click here to view the detailed API parameters](docs/fal_api_en.md). 60 | 61 | ![FAL API](docs/figures/fal-api-en.png) 62 | 63 | ### 1.4, License 64 | 65 | The fal package complies with the Apache-2.0 license, see the `LICENSE` file for details. 66 | 67 | ### 1.5, Dependency 68 | 69 | It has no dependence on RT-Thread and can also be used on bare metal. 70 | 71 | > Test command function needs to rely on RT-Thread FinSH/MSH 72 | 73 | ## 2. Use FAL 74 | 75 | The basic steps for using FAL are as follows: 76 | 77 | 1. Open FAL: Open the fal software package from Env and download it to the project. 78 | 2. FAL migration: define flash device, define flash device table, define flash partition table. Step 2 is mainly explained below. 79 | 3. Call fal_init() to initialize the library: After the migration is completed, it can be called in the application layer, such as in the main function. 80 | 81 | ![fal port](docs/figures/fal-port-en.png) 82 | 83 | ### 2.1, Define flash device 84 | 85 | Before defining the Flash device table, you need to define the Flash device first. It can be on-chip flash or off-chip spi flash based on SFUD: 86 | 87 | - Refer to [`fal_flash_stm32f2_port.c`](https://github.com/RT-Thread-packages/fal/blob/master/samples/porting/fal_flash_stm32f2_port.c) to define the on-chip flash device. 88 | - Refer to [`fal_flash_sfud_port.c`](https://github.com/RT-Thread-packages/fal/blob/master/samples/porting/fal_flash_sfud_port.c) to define off-chip spi flash device. 89 | 90 | To define specific Flash device objects, users need to implement the operation functions of `init`, `read`, `write`, and `erase` according to their own Flash conditions: 91 | 92 | - `static int init(void)`: **Optional** initialization operation. 93 | - `static int read(long offset, uint8_t *buf, size_t size)`: read operation. 94 | 95 | | Parameters | Description | 96 | | ------ | ------------------------- | 97 | | offset | Flash offset address for reading data | 98 | | buf | Buffer to store the data to be read | 99 | | size | The size of the data to be read | 100 | | return | Return the actual read data size | 101 | 102 | - `static int write(long offset, const uint8_t *buf, size_t size)`: write operation. 103 | 104 | | Parameters | Description | 105 | | ------ | ------------------------- | 106 | | offset | Flash offset address for writing data | 107 | | buf | Buffer to store data to be written | 108 | | size | The size of the data to be written | 109 | | return | Return the actual written data size | 110 | 111 | - `static int erase(long offset, size_t size)`: erase operation. 112 | 113 | | Parameters | Description | 114 | | ------ | ------------------------- | 115 | | offset | Flash offset address of erase area | 116 | | size | The size of the erased area | 117 | | return | Return the actual erased area size | 118 | 119 | Users need to implement these operation functions according to their own Flash conditions. A specific Flash device object is defined at the bottom of the file. The following example defines stm32f2 on-chip flash: stm32f2_onchip_flash 120 | 121 | ```c 122 | const struct fal_flash_dev stm32f2_onchip_flash = 123 | { 124 | .name = "stm32_onchip", 125 | .addr = 0x08000000, 126 | .len = 1024*1024, 127 | .blk_size = 128*1024, 128 | .ops = {init, read, write, erase}, 129 | .write_gran = 8 130 | }; 131 | ``` 132 | 133 | - `"stm32_onchip"`: the name of the flash device. 134 | - `0x08000000`: Start address for flash operation. 135 | - `1024*1024`: Total size of Flash (1MB). 136 | - `128*1024`: Flash block/sector size (because the STM32F2 blocks have uneven sizes, the erase granularity is the largest block size: 128K). 137 | - `{init, read, write, erase}`: Flash operation functions. If there is no init initialization process, the first operation function position can be left blank. 138 | - `8`: Set the write granularity, the unit is bit, 0 means not effective (the default value is 0), this member is a new member whose fal version is greater than 0.4.0. Each flash write granularity is not the same, it can be set through this member, the following are several common Flash write granularities: 139 | - nor flash: 1 bit 140 | - stm32f2/f4: 8 bit 141 | - stm32f1: 32 bit 142 | - stm32l4: 64 bit 143 | 144 | ### 2.2, Define the flash device table 145 | 146 | The Flash device table is defined in the header file `fal_cfg.h`, you need to **create a new `fal_cfg.h` file** before defining the partition table. Please place this file in the port folder of the corresponding BSP or project directory, and Add the header file path to the project. fal_cfg.h can refer to [Sample file fal/samples/porting/fal_cfg.h](https://github.com/RT-Thread-packages/fal/blob/master/samples/porting/fal_cfg.h) to complete. 147 | 148 | Flash device table example: 149 | 150 | 151 | ```c 152 | /* ===================== Flash device Configuration ========================= */ 153 | extern const struct fal_flash_dev stm32f2_onchip_flash; 154 | extern struct fal_flash_dev nor_flash0; 155 | 156 | /* flash device table */ 157 | #define FAL_FLASH_DEV_TABLE \ 158 | { \ 159 | &stm32f2_onchip_flash, \ 160 | &nor_flash0, \ 161 | } 162 | ``` 163 | 164 | In the Flash device table, there are two Flash objects, one is the STM32F2 on-chip Flash, and the other is the off-chip Nor Flash. 165 | 166 | ### 2.3, Define flash partition table 167 | 168 | The partition table is also defined in the `fal_cfg.h` header file. Flash partitions are based on Flash devices. Each Flash device can have N partitions. The collection of these partitions is the partition table. Before configuring the partition table, make sure that the **Flash device** and **device table** have been defined. fal_cfg.h can refer to [Sample file fal/samples/porting/fal_cfg.h](https://github.com/RT-Thread-packages/fal/blob/master/samples/porting/fal_cfg.h) to complete. 169 | 170 | Example of partition table: 171 | 172 | ```c 173 | #define NOR_FLASH_DEV_NAME "norflash0" 174 | /* ====================== Partition Configuration ========================== */ 175 | #ifdef FAL_PART_HAS_TABLE_CFG 176 | /* partition table */ 177 | #define FAL_PART_TABLE \ 178 | { \ 179 | {FAL_PART_MAGIC_WORD, "bl", "stm32_onchip", 0, 64*1024, 0}, \ 180 | {FAL_PART_MAGIC_WORD, "app", "stm32_onchip", 64*1024, 704*1024, 0}, \ 181 | {FAL_PART_MAGIC_WORD, "easyflash", NOR_FLASH_DEV_NAME, 0, 1024*1024, 0}, \ 182 | {FAL_PART_MAGIC_WORD, "download", NOR_FLASH_DEV_NAME, 1024*1024, 1024*1024, 0}, \ 183 | } 184 | #endif /* FAL_PART_HAS_TABLE_CFG */ 185 | ``` 186 | 187 | The detailed description of the above partition table is as follows: 188 | 189 | | Partition name | Flash device name | Offset address | Size | Description | 190 | | ----------- | -------------- | --------- | ----- | ------------------ | 191 | | "bl" | "stm32_onchip" | 0 | 64KB | Bootloader | 192 | | "app" | "stm32_onchip" | 64*1024 | 704KB | Application | 193 | | "easyflash" | "norflash0" | 0 | 1MB | EasyFlash parameter storage | 194 | | "download" | "norflash0" | 1024*1024 | 1MB | OTA download area | 195 | 196 | The partition parameters that users need to modify include: partition name, associated Flash device name, offset address (relative to the internal Flash device), and size. Pay attention to the following points: 197 | 198 | - Partition name guarantee **cannot be repeated**; 199 | - The associated Flash device **must have been defined in the Flash device table**, and the **name is the same**, otherwise there will be an error that the Flash device cannot be found; 200 | - The starting address and size of the partition **cannot exceed the address range of the Flash device**, otherwise it will cause packet initialization errors; 201 | 202 | > Note: When defining each partition, in addition to filling in the parameter attributes described above, you need to add the attribute `FAL_PART_MAGIC_WORD` at the front and add `0` at the end (currently used for reserved functions) 203 | 204 | ## 3. FinSH/MSH test command 205 | 206 | FAL provides a wealth of test commands, and the project only needs to enable the Finsh/MSH function on RT-Thread. These commands will be very useful when doing some Flash-based application development and debugging. It can accurately write or read the original Flash data at the specified location, quickly verify the integrity of the Flash driver, and even perform performance tests on the Flash. 207 | 208 | The specific functions are as follows: enter `fal` to see the complete command list 209 | 210 | ``` 211 | msh />fal 212 | Usage: 213 | fal probe [dev_name|part_name] - probe flash device or partition by given name 214 | fal read addr size - read 'size' bytes starting at 'addr' 215 | fal write addr data1 ... dataN - write some bytes 'data' starting at 'addr' 216 | fal erase addr size - erase 'size' bytes starting at 'addr' 217 | fal bench - benchmark test with per block size 218 | 219 | msh /> 220 | ``` 221 | 222 | ### 3.1, Specify the Flash device or Flash partition to be operated 223 | 224 | When using the fal command for the first time, directly inputting `fal probe` will display the partition table information. You can specify the object to be operated as a partition in the partition table or a flash device. 225 | 226 | After the partition or Flash is successfully selected, some of its attributes will be displayed. The general effect is as follows: 227 | 228 | ``` 229 | msh />fal probe 230 | No flash device or partition was probed. 231 | Usage: fal probe [dev_name|part_name] - probe flash device or partition by given name. 232 | [I/FAL] ==================== FAL partition table ==================== 233 | [I/FAL] | name | flash_dev | offset | length | 234 | [I/FAL] ------------------------------------------------------------- 235 | [I/FAL] | bl | stm32_onchip | 0x00000000 | 0x00010000 | 236 | [I/FAL] | app | stm32_onchip | 0x00010000 | 0x000b0000 | 237 | [I/FAL] | ef | norflash0 | 0x00000000 | 0x00100000 | 238 | [I/FAL] | download | norflash0 | 0x00100000 | 0x00100000 | 239 | [I/FAL] ============================================================= 240 | msh /> 241 | msh />fal probe download 242 | Probed a flash partition | download | flash_dev: norflash0 | offset: 1048576 | len: 1048576 |. 243 | msh /> 244 | ``` 245 | 246 | ### 3.2, Erase data 247 | 248 | Enter `fal erase` first, followed by the starting address and length of the data to be erased. The following command is: erase 4096 bytes of data from address 0 (relative to Flash or partition) 249 | 250 | > Note: According to the Flash characteristics, the erase action will be processed according to sector alignment. Therefore, if the erase operation address or length is not aligned with the flash sector, the entire sector data associated with it will be erased. 251 | 252 | ``` 253 | msh />fal erase 0 4096 254 | Erase data success. Start from 0x00000000, size is 4096. 255 | msh /> 256 | ``` 257 | 258 | ### 3.3, Write data 259 | 260 | Enter `fal write` first, followed by N pieces of data to be written, separated by spaces. The following command is: Write 1, 2, 3, 4, 5, 5 bytes of data from address 8 261 | 262 | ``` 263 | msh />fal write 8 1 2 3 4 5 264 | Write data success. Start from 0x00000008, size is 5. 265 | Write data: 1 2 3 4 5. 266 | msh /> 267 | ``` 268 | 269 | ### 3.4, Read data 270 | 271 | Enter `fal read` first, followed by the starting address and length of the data to be read. The following command is: read 64 bytes of data from address 0 272 | 273 | ``` 274 | msh />fal read 0 64 275 | Read data success. Start from 0x00000000, size is 64. The data is: 276 | Offset (h) 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 277 | [00000000] FF FF FF FF FF FF FF FF 01 02 03 04 05 FF FF FF 278 | [00000010] FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 279 | [00000020] FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 280 | [00000030] FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 281 | 282 | msh /> 283 | ``` 284 | 285 | ### 3.5, Performance test 286 | 287 | The performance test will test the erasing, writing and reading speed of the Flash. At the same time, the accuracy of writing and reading data will be tested to ensure the consistency of the writing and reading data of the entire Flash or the entire partition. 288 | 289 | Enter `fal bench` first, followed by the sector size of the Flash to be tested (please check the corresponding Flash manual, SPI Nor Flash is generally 4096). Since the performance test will lose the data of the entire Flash or the entire partition, the command must be followed by `yes` at the end. 290 | 291 | ``` 292 | msh />fal bench 4096 yes 293 | Erasing 1048576 bytes data, waiting... 294 | Erase benchmark success, total time: 2.674S. 295 | Writing 1048576 bytes data, waiting... 296 | Write benchmark success, total time: 7.107S. 297 | Reading 1048576 bytes data, waiting... 298 | Read benchmark success, total time: 2.716S. 299 | msh /> 300 | ``` 301 | 302 | ## 4. Common applications 303 | 304 | - [Example of fatfs file system based on FAL partition](https://github.com/RT-Thread/IoT_Board/tree/master/examples/15_component_fs_flash) 305 | - [Application note of littlefs file system based on FAL partition](https://www.rt-thread.org/document/site/application-note/components/dfs/an0027-littlefs/) 306 | - [EasyFlash porting instructions based on FAL partition](https://github.com/armink-rtt-pkgs/EasyFlash/tree/master/ports) 307 | 308 | ## 5. Common problems 309 | 310 | **1. When using FAL, the header file `fal_cfg.h` cannot be found** 311 | 312 | `fal_cfg.h` is the configuration file of the fal software package, which needs to be created manually by the user and defines the relevant partition table information. Please place the file in the port folder of the BSP or the port folder of the project directory (if not, create a new port folder), and add the path of the header file to the project, see "`2.2. Define the Flash Device Table `" section. 313 | 314 | ## 6. Contact 315 | 316 | * Maintenance: [armink](https://github.com/armink) 317 | * Homepage: https://github.com/RT-Thread-packages/fal 318 | -------------------------------------------------------------------------------- /README_ZH.md: -------------------------------------------------------------------------------- 1 | # FAL:Flash 抽象层 2 | 3 | 中文页 | [英文页](./README.md) 4 | 5 | ### 从RT-Thread v4.1.0版本开始,FAL作为内部组件移到了系统内部,请移至:https://github.com/RT-Thread/rt-thread/tree/master/components/fal 6 | 7 | **此存储库不再维护。** 8 | 9 | ## 1、FAL介绍 10 | 11 | FAL (Flash Abstraction Layer) Flash 抽象层,是对 Flash 及基于 Flash 的分区进行管理、操作的抽象层,对上层统一了 Flash 及 分区操作的 API (框架图如下所示),并具有以下特性: 12 | 13 | - 支持静态可配置的分区表,并可关联多个 Flash 设备; 14 | - 分区表支持 **自动装载** 。避免在多固件项目,分区表被多次定义的问题; 15 | - 代码精简,对操作系统 **无依赖** ,可运行于裸机平台,比如对资源有一定要求的 Bootloader; 16 | - 统一的操作接口。保证了文件系统、OTA、NVM(例如:[EasyFlash](https://github.com/armink-rtt-pkgs/EasyFlash)) 等对 Flash 有一定依赖的组件,底层 Flash 驱动的可重用性; 17 | - 自带基于 Finsh/MSH 的测试命令,可以通过 Shell 按字节寻址的方式操作(读写擦) Flash 或分区,方便开发者进行调试、测试; 18 | 19 | ![FAL framework](docs/figures/fal_framework.png) 20 | 21 | ### 1.1、打开 FAL 22 | 23 | 使用 fal package 需要在 RT-Thread 的包管理器中选择它,具体路径如下: 24 | 25 | ``` 26 | RT-Thread online packages 27 | system packages ---> 28 | --- fal: Flash Abstraction Layer implement. Manage flash device and partition. 29 | [*] Enable debug log output 30 | [*] FAL partition table config has defined on 'fal_cfg.h' 31 | (onchip) The flash device which saving partition table 32 | (65536) The patition table end address relative to flash device offset. 33 | [ ] FAL uses SFUD drivers 34 | (norflash0) The name of the device used by FAL (NEW) 35 | version (latest) ---> 36 | ``` 37 | 38 | 每个功能的配置说明如下: 39 | 40 | - 开启调试日志输出(默认开启); 41 | - 分区表是否在 `fal_cfg.h` 中定义(默认开启)。如果关闭此选项,fal 将会自动去指定 Flash 的指定位置去检索并装载分区表,具体配置详见下面两个选项; 42 | - 存放分区表的 Flash 设备; 43 | - 分区表的 **结束地址** 位于 Flash 设备上的偏移。fal 将从此地址开始往回进行检索分区表,直接读取到 Flash 顶部。如果不确定分区表具体位置,这里也可以配置为 Flash 的结束地址,fal 将会检索整个 Flash,检索时间可能会增加。 44 | - 启用 FAL 针对 SFUD 的移植文件(默认关闭); 45 | - 应输入调用 `rt_sfud_flash_probe` 函数时传入的 FLASH 设备名称(也可以通过 list_device 命令查看 Block Device 的名字获取)。该名称与分区表中的 Flash 名称对应,只有正确设置设备名字,才能完成对 FLASH 的读写操作。 46 | 47 | 然后让 RT-Thread 的包管理器自动更新,或者使用 `pkgs --update` 命令更新包到 BSP 中。 48 | 49 | ### 1.2、FAL 目录 50 | 51 | | 名称 | 说明 | 52 | | ------- | ---------- | 53 | | inc | 头文件目录 | 54 | | src | 源代码目录 | 55 | | samples | 例程目录 | 56 | 57 | ### 1.3、FAL API 58 | 59 | FAL 相关的 API 如图所示,[点击此处查看 API 参数详解](docs/fal_api.md)。 60 | 61 | ![FAL API](docs/figures/fal-api.png) 62 | 63 | ### 1.4、许可证 64 | 65 | fal package 遵循 Apache-2.0 许可,详见 `LICENSE` 文件。 66 | 67 | ### 1.5、依赖 68 | 69 | 对 RT-Thread 无依赖,也可用于裸机。 70 | 71 | > 测试命令功能需要依赖 RT-Thread FinSH/MSH 72 | 73 | ## 2、使用 FAL 74 | 75 | 使用 FAL 的基本步骤如下所示: 76 | 77 | 1. 打开 FAL:从 Env 中打开 fal 软件包并下载到工程。 78 | 2. FAL 移植:定义 flash 设备、定义 flash 设备表、定义 flash 分区表。以下主要对步骤 2 展开讲解。 79 | 3. 调用 fal_init() 初始化该库:移植完成后,可在应用层调用,如在 main 函数中调用。 80 | 81 | ![fal 移植](docs/figures/fal-port.png) 82 | 83 | ### 2.1、定义 flash 设备 84 | 85 | 在定义 Flash 设备表前,需要先定义 Flash 设备。可以是片内 flash, 也可以是片外基于 SFUD 的 spi flash: 86 | 87 | - 定义片内 flash 设备可以参考 [`fal_flash_stm32f2_port.c`](https://github.com/RT-Thread-packages/fal/blob/master/samples/porting/fal_flash_stm32f2_port.c) 。 88 | - 定义片外 spi flash 设备可以参考 [`fal_flash_sfud_port.c`](https://github.com/RT-Thread-packages/fal/blob/master/samples/porting/fal_flash_sfud_port.c) 。 89 | 90 | 定义具体的 Flash 设备对象,用户需要根据自己的 Flash 情况分别实现 `init`、 `read`、 `write`、 `erase` 这些操作函数: 91 | 92 | - `static int init(void)`:**可选** 的初始化操作。 93 | - `static int read(long offset, uint8_t *buf, size_t size)`:读取操作。 94 | 95 | | 参数 | 描述 | 96 | | ------ | ------------------------- | 97 | | offset | 读取数据的 Flash 偏移地址 | 98 | | buf | 存放待读取数据的缓冲区 | 99 | | size | 待读取数据的大小 | 100 | | return | 返回实际读取的数据大小 | 101 | 102 | - `static int write(long offset, const uint8_t *buf, size_t size)` :写入操作。 103 | 104 | | 参数 | 描述 | 105 | | ------ | ------------------------- | 106 | | offset | 写入数据的 Flash 偏移地址 | 107 | | buf | 存放待写入数据的缓冲区 | 108 | | size | 待写入数据的大小 | 109 | | return | 返回实际写入的数据大小 | 110 | 111 | - `static int erase(long offset, size_t size)` :擦除操作。 112 | 113 | | 参数 | 描述 | 114 | | ------ | ------------------------- | 115 | | offset | 擦除区域的 Flash 偏移地址 | 116 | | size | 擦除区域的大小 | 117 | | return | 返回实际擦除的区域大小 | 118 | 119 | 用户需要根据自己的 Flash 情况分别实现这些操作函数。在文件最底部定义了具体的 Flash 设备对象 ,如下示例定义了 stm32f2 片上 flash:stm32f2_onchip_flash 120 | 121 | ```c 122 | const struct fal_flash_dev stm32f2_onchip_flash = 123 | { 124 | .name = "stm32_onchip", 125 | .addr = 0x08000000, 126 | .len = 1024*1024, 127 | .blk_size = 128*1024, 128 | .ops = {init, read, write, erase}, 129 | .write_gran = 8 130 | }; 131 | ``` 132 | 133 | - `"stm32_onchip"` : Flash 设备的名字。 134 | - `0x08000000`: 对 Flash 操作的起始地址。 135 | - `1024*1024`:Flash 的总大小(1MB)。 136 | - `128*1024`:Flash 块/扇区大小(因为 STM32F2 各块大小不均匀,所以擦除粒度为最大块的大小:128K)。 137 | - `{init, read, write, erase}` :Flash 的操作函数。 如果没有 init 初始化过程,第一个操作函数位置可以置空。 138 | - `8` : 设置写粒度,单位 bit, 0 表示未生效(默认值为 0 ),该成员是 fal 版本大于 0.4.0 的新增成员。各个 flash 写入粒度不尽相同,可通过该成员进行设置,以下列举几种常见 Flash 写粒度: 139 | - nor flash: 1 bit 140 | - stm32f2/f4: 8 bit 141 | - stm32f1: 32 bit 142 | - stm32l4: 64 bit 143 | 144 | ### 2.2、定义 flash 设备表 145 | 146 | Flash 设备表定义在 `fal_cfg.h` 头文件中,定义分区表前需 **新建 `fal_cfg.h` 文件** ,请将该文件统一放在对应 BSP 或工程目录的 port 文件夹下,并将该头文件路径加入到工程。fal_cfg.h 可以参考 [示例文件 fal/samples/porting/fal_cfg.h](https://github.com/RT-Thread-packages/fal/blob/master/samples/porting/fal_cfg.h) 完成。 147 | 148 | 设备表示例: 149 | 150 | ```c 151 | /* ===================== Flash device Configuration ========================= */ 152 | extern const struct fal_flash_dev stm32f2_onchip_flash; 153 | extern struct fal_flash_dev nor_flash0; 154 | 155 | /* flash device table */ 156 | #define FAL_FLASH_DEV_TABLE \ 157 | { \ 158 | &stm32f2_onchip_flash, \ 159 | &nor_flash0, \ 160 | } 161 | ``` 162 | 163 | Flash 设备表中,有两个 Flash 对象,一个为 STM32F2 的片内 Flash ,一个为片外的 Nor Flash。 164 | 165 | ### 2.3、定义 flash 分区表 166 | 167 | 分区表也定义在 `fal_cfg.h` 头文件中。Flash 分区基于 Flash 设备,每个 Flash 设备又可以有 N 个分区,这些分区的集合就是分区表。在配置分区表前,务必保证已定义好 **Flash 设备** 及 **设备表**。fal_cfg.h 可以参考 [示例文件 fal/samples/porting/fal_cfg.h](https://github.com/RT-Thread-packages/fal/blob/master/samples/porting/fal_cfg.h) 完成。 168 | 169 | 分区表示例: 170 | 171 | ```c 172 | #define NOR_FLASH_DEV_NAME "norflash0" 173 | /* ====================== Partition Configuration ========================== */ 174 | #ifdef FAL_PART_HAS_TABLE_CFG 175 | /* partition table */ 176 | #define FAL_PART_TABLE \ 177 | { \ 178 | {FAL_PART_MAGIC_WORD, "bl", "stm32_onchip", 0, 64*1024, 0}, \ 179 | {FAL_PART_MAGIC_WORD, "app", "stm32_onchip", 64*1024, 704*1024, 0}, \ 180 | {FAL_PART_MAGIC_WORD, "easyflash", NOR_FLASH_DEV_NAME, 0, 1024*1024, 0}, \ 181 | {FAL_PART_MAGIC_WORD, "download", NOR_FLASH_DEV_NAME, 1024*1024, 1024*1024, 0}, \ 182 | } 183 | #endif /* FAL_PART_HAS_TABLE_CFG */ 184 | ``` 185 | 186 | 上面这个分区表详细描述信息如下: 187 | 188 | | 分区名 | Flash 设备名 | 偏移地址 | 大小 | 说明 | 189 | | ----------- | -------------- | --------- | ----- | ------------------ | 190 | | "bl" | "stm32_onchip" | 0 | 64KB | 引导程序 | 191 | | "app" | "stm32_onchip" | 64*1024 | 704KB | 应用程序 | 192 | | "easyflash" | "norflash0" | 0 | 1MB | EasyFlash 参数存储 | 193 | | "download" | "norflash0" | 1024*1024 | 1MB | OTA 下载区 | 194 | 195 | 用户需要修改的分区参数包括:分区名称、关联的 Flash 设备名、偏移地址(相对 Flash 设备内部)、大小,需要注意以下几点: 196 | 197 | - 分区名保证 **不能重复**; 198 | - 关联的 Flash 设备 **务必已经在 Flash 设备表中定义好** ,并且 **名称一致** ,否则会出现无法找到 Flash 设备的错误; 199 | - 分区的起始地址和大小 **不能超过 Flash 设备的地址范围** ,否则会导致包初始化错误; 200 | 201 | > 注意:每个分区定义时,除了填写上面介绍的参数属性外,需在前面增加 `FAL_PART_MAGIC_WORD` 属性,末尾增加 `0` (目前用于保留功能) 202 | 203 | ## 3、Finsh/MSH 测试命令 204 | 205 | fal 提供了丰富的测试命令,项目只要在 RT-Thread 上开启 Finsh/MSH 功能即可。在做一些基于 Flash 的应用开发、调试时,这些命令会非常实用。它可以准确的写入或者读取指定位置的原始 Flash 数据,快速的验证 Flash 驱动的完整性,甚至可以对 Flash 进行性能测试。 206 | 207 | 具体功能如下:输入 fal 可以看到完整的命令列表 208 | 209 | ``` 210 | msh />fal 211 | Usage: 212 | fal probe [dev_name|part_name] - probe flash device or partition by given name 213 | fal read addr size - read 'size' bytes starting at 'addr' 214 | fal write addr data1 ... dataN - write some bytes 'data' starting at 'addr' 215 | fal erase addr size - erase 'size' bytes starting at 'addr' 216 | fal bench - benchmark test with per block size 217 | 218 | msh /> 219 | ``` 220 | 221 | ### 3.1、指定待操作的 Flash 设备或 Flash 分区 222 | 223 | 当第一次使用 fal 命令时,直接输入 `fal probe` 将会显示分区表信息。可以指定待操作的对象为分区表里的某个分区,或者某个 Flash 设备。 224 | 225 | 分区或者 Flash 被成功选中后,还将会显示它的一些属性情况。大致效果如下: 226 | 227 | ``` 228 | msh />fal probe 229 | No flash device or partition was probed. 230 | Usage: fal probe [dev_name|part_name] - probe flash device or partition by given name. 231 | [I/FAL] ==================== FAL partition table ==================== 232 | [I/FAL] | name | flash_dev | offset | length | 233 | [I/FAL] ------------------------------------------------------------- 234 | [I/FAL] | bl | stm32_onchip | 0x00000000 | 0x00010000 | 235 | [I/FAL] | app | stm32_onchip | 0x00010000 | 0x000b0000 | 236 | [I/FAL] | ef | norflash0 | 0x00000000 | 0x00100000 | 237 | [I/FAL] | download | norflash0 | 0x00100000 | 0x00100000 | 238 | [I/FAL] ============================================================= 239 | msh /> 240 | msh />fal probe download 241 | Probed a flash partition | download | flash_dev: norflash0 | offset: 1048576 | len: 1048576 |. 242 | msh /> 243 | ``` 244 | 245 | ### 3.2、擦除数据 246 | 247 | 先输入 `fal erase` ,后面跟着待擦除数据的起始地址以及长度。以下命令为:从 0 地址(相对 Flash 或分区)开始擦除 4096 字节数据 248 | 249 | > 注意:根据 Flash 特性,擦除动作将按扇区对齐进行处理。所以,如果擦除操作地址或长度未按照 Flash 的扇区对齐,将会擦除掉与其关联的整个扇区数据。 250 | 251 | ``` 252 | msh />fal erase 0 4096 253 | Erase data success. Start from 0x00000000, size is 4096. 254 | msh /> 255 | ``` 256 | 257 | ### 3.3、写入数据 258 | 259 | 先输入 `fal write` ,后面跟着 N 个待写入的数据,并以空格隔开。以下命令为:从地址 8 的位置依次开始写入 1、2、3、4 、 5 这 5 个字节数据 260 | 261 | ``` 262 | msh />fal write 8 1 2 3 4 5 263 | Write data success. Start from 0x00000008, size is 5. 264 | Write data: 1 2 3 4 5 . 265 | msh /> 266 | ``` 267 | 268 | ### 3.4、读取数据 269 | 270 | 先输入 `fal read` ,后面跟着待读取数据的起始地址以及长度。以下命令为:从 0 地址开始读取 64 字节数据 271 | 272 | ``` 273 | msh />fal read 0 64 274 | Read data success. Start from 0x00000000, size is 64. The data is: 275 | Offset (h) 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 276 | [00000000] FF FF FF FF FF FF FF FF 01 02 03 04 05 FF FF FF 277 | [00000010] FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 278 | [00000020] FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 279 | [00000030] FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 280 | 281 | msh /> 282 | ``` 283 | 284 | ### 3.5、性能测试 285 | 286 | 性能测试将会测试 Flash 的擦除、写入及读取速度,同时将会测试写入及读取数据的准确性,保证整个 Flash 或整个分区的 写入与读取 数据的一致性。 287 | 288 | 先输入 `fal bench` ,后面跟着待测试 Flash 的扇区大小(请查看对应的 Flash 手册,SPI Nor Flash 一般为 4096)。由于性能测试将会让整个 Flash 或者整个分区的数据丢失,所以命令最后必须跟 `yes` 。 289 | 290 | ``` 291 | msh />fal bench 4096 yes 292 | Erasing 1048576 bytes data, waiting... 293 | Erase benchmark success, total time: 2.674S. 294 | Writing 1048576 bytes data, waiting... 295 | Write benchmark success, total time: 7.107S. 296 | Reading 1048576 bytes data, waiting... 297 | Read benchmark success, total time: 2.716S. 298 | msh /> 299 | ``` 300 | 301 | ## 4、常见应用 302 | 303 | - [基于 FAL 分区的 fatfs 文件系统例程](https://github.com/RT-Thread/IoT_Board/tree/master/examples/15_component_fs_flash) 304 | - [基于 FAL 分区的 littlefs 文件系统应用笔记](https://www.rt-thread.org/document/site/application-note/components/dfs/an0027-littlefs/) 305 | - [基于 FAL 分区的 EasyFlash 移植说明](https://github.com/armink-rtt-pkgs/EasyFlash/tree/master/ports) 306 | 307 | ## 5、常见问题 308 | 309 | **1、使用 FAL 时,无法找到 `fal_cfg.h` 头文件** 310 | 311 | `fal_cfg.h` 为 fal 软件包的配置文件,需要用户手动新建,并定义相关的分区表信息。请将该文件统一放在 BSP 的 port 文件夹下或工程目录的 port 文件夹下(若没有则新建 port 文件夹),并将该头文件路径加入到工程,详见 "`2.2、定义 flash 设备表`" 小节。 312 | 313 | ## 6、联系方式 314 | 315 | * 维护:[armink](https://github.com/armink) 316 | * 主页:https://github.com/RT-Thread-packages/fal 317 | -------------------------------------------------------------------------------- /SConscript: -------------------------------------------------------------------------------- 1 | 2 | from building import * 3 | import rtconfig 4 | 5 | cwd = GetCurrentDir() 6 | src = Glob('src/*.c') 7 | CPPPATH = [cwd + '/inc'] 8 | LOCAL_CCFLAGS = '' 9 | 10 | if GetDepend(['FAL_USING_SFUD_PORT']): 11 | src += Glob('samples/porting/fal_flash_sfud_port.c') 12 | 13 | group = DefineGroup('fal', src, depend = ['PKG_USING_FAL'], CPPPATH = CPPPATH, LOCAL_CCFLAGS = LOCAL_CCFLAGS) 14 | 15 | Return('group') 16 | -------------------------------------------------------------------------------- /docs/fal_api.md: -------------------------------------------------------------------------------- 1 | # FAL API 2 | 3 | ## 查找 Flash 设备 4 | 5 | ```C 6 | const struct fal_flash_dev *fal_flash_device_find(const char *name) 7 | ``` 8 | 9 | | 参数 | 描述 | 10 | | :----- | :----------------------- | 11 | | name | Flash 设备名称 | 12 | | return | 如果查找成功,将返回 Flash 设备对象,查找失败返回 NULL | 13 | 14 | ## 查找 Flash 分区 15 | 16 | ```C 17 | const struct fal_partition *fal_partition_find(const char *name) 18 | ``` 19 | 20 | | 参数 | 描述 | 21 | | :----- | :----------------------- | 22 | | name | Flash 分区名称 | 23 | | return | 如果查找成功,将返回 Flash 分区对象,查找失败返回 NULL | 24 | 25 | ## 获取分区表 26 | 27 | ```C 28 | const struct fal_partition *fal_get_partition_table(size_t *len) 29 | ``` 30 | 31 | | 参数 | 描述 | 32 | | :----- | :----------------------- | 33 | | len | 分区表的长度 | 34 | | return | 分区表 | 35 | 36 | ## 临时设置分区表 37 | 38 | FAL 初始化时会自动装载默认分区表。使用该设置将临时修改分区表,重启后会 **丢失** 该设置 39 | 40 | ```C 41 | void fal_set_partition_table_temp(struct fal_partition *table, size_t len) 42 | ``` 43 | 44 | | 参数 | 描述 | 45 | | :----- | :----------------------- | 46 | | table | 分区表 | 47 | | len | 分区表的长度 | 48 | 49 | ## 从分区读取数据 50 | 51 | ```C 52 | int fal_partition_read(const struct fal_partition *part, uint32_t addr, uint8_t *buf, size_t size) 53 | ``` 54 | 55 | | 参数 | 描述 | 56 | | :----- | :----------------------- | 57 | | part | 分区对象 | 58 | | addr | 相对分区的偏移地址 | 59 | | buf | 存放待读取数据的缓冲区 | 60 | | size | 待读取数据的大小 | 61 | | return | 返回实际读取的数据大小 | 62 | 63 | ## 往分区写入数据 64 | 65 | ```C 66 | int fal_partition_write(const struct fal_partition *part, uint32_t addr, const uint8_t *buf, size_t size) 67 | ``` 68 | 69 | | 参数 | 描述 | 70 | | :----- | :----------------------- | 71 | | part | 分区对象 | 72 | | addr | 相对分区的偏移地址 | 73 | | buf | 存放待写入数据的缓冲区 | 74 | | size | 待写入数据的大小 | 75 | | return | 返回实际写入的数据大小 | 76 | 77 | ## 擦除分区数据 78 | 79 | ```C 80 | int fal_partition_erase(const struct fal_partition *part, uint32_t addr, size_t size) 81 | ``` 82 | 83 | | 参数 | 描述 | 84 | | :----- | :----------------------- | 85 | | part | 分区对象 | 86 | | addr | 相对分区的偏移地址 | 87 | | size | 擦除区域的大小 | 88 | | return | 返回实际擦除的区域大小 | 89 | 90 | ## 擦除整个分区数据 91 | 92 | ```C 93 | int fal_partition_erase_all(const struct fal_partition *part) 94 | ``` 95 | 96 | | 参数 | 描述 | 97 | | :----- | :----------------------- | 98 | | part | 分区对象 | 99 | | return | 返回实际擦除的区域大小 | 100 | 101 | ## 打印分区表 102 | 103 | ```c 104 | void fal_show_part_table(void) 105 | ``` 106 | 107 | ## 创建块设备 108 | 109 | 该函数可以根据指定的分区名称,创建对应的块设备,以便于在指定的分区上挂载文件系统 110 | 111 | ```C 112 | struct rt_device *fal_blk_device_create(const char *parition_name) 113 | ``` 114 | 115 | | 参数 | 描述 | 116 | | :----- | :----------------------- | 117 | | parition_name | 分区名称 | 118 | | return | 创建成功,则返回对应的块设备,失败返回空 | 119 | 120 | ## 创建 MTD Nor Flash 设备 121 | 122 | 该函数可以根据指定的分区名称,创建对应的 MTD Nor Flash 设备,以便于在指定的分区上挂载文件系统 123 | 124 | ```C 125 | struct rt_device *fal_mtd_nor_device_create(const char *parition_name) 126 | ``` 127 | 128 | | 参数 | 描述 | 129 | | :------------ | :---------------------------------------------------- | 130 | | parition_name | 分区名称 | 131 | | return | 创建成功,则返回对应的 MTD Nor Flash 设备,失败返回空 | 132 | 133 | ## 创建字符设备 134 | 135 | 该函数可以根据指定的分区名称,创建对应的字符设备,以便于通过 deivice 接口或 devfs 接口操作分区,开启了 POSIX 后,还可以通过 open/read/write 函数操作分区。 136 | 137 | ```C 138 | struct rt_device *fal_char_device_create(const char *parition_name) 139 | ``` 140 | 141 | | 参数 | 描述 | 142 | | :------------ | :----------------------------------------- | 143 | | parition_name | 分区名称 | 144 | | return | 创建成功,则返回对应的字符设备,失败返回空 | 145 | 146 | -------------------------------------------------------------------------------- /docs/fal_api_en.md: -------------------------------------------------------------------------------- 1 | # FAL API 2 | 3 | ## Find Flash device 4 | 5 | ```C 6 | const struct fal_flash_dev *fal_flash_device_find(const char *name) 7 | ``` 8 | 9 | | Parameters | Description | 10 | | :----- | :----------------------- | 11 | | name | Flash device name | 12 | | return | If the search is successful, the Flash device object will be returned, and if the search fails, it will return NULL | 13 | 14 | ## Find Flash Partition 15 | 16 | ```C 17 | const struct fal_partition *fal_partition_find(const char *name) 18 | ``` 19 | 20 | | Parameters | Description | 21 | | :----- | :----------------------- | 22 | | name | Flash partition name | 23 | | return | If the search is successful, the Flash partition object will be returned, and if the search fails, it will return NULL | 24 | 25 | ## Get the partition table 26 | 27 | ```C 28 | const struct fal_partition *fal_get_partition_table(size_t *len) 29 | ``` 30 | 31 | | Parameters | Description | 32 | | :----- | :----------------------- | 33 | | len | The length of the partition table | 34 | | return | Partition table | 35 | 36 | ## Temporarily set the partition table 37 | 38 | The default partition table will be automatically loaded when FAL is initialized. Using this setting will temporarily modify the partition table and will **lost** this setting after restarting 39 | 40 | ```C 41 | void fal_set_partition_table_temp(struct fal_partition *table, size_t len) 42 | ``` 43 | 44 | | Parameters | Description | 45 | | :----- | :----------------------- | 46 | | table | Partition table | 47 | | len | Length of the partition table | 48 | 49 | ## Read data from partition 50 | 51 | ```C 52 | int fal_partition_read(const struct fal_partition *part, uint32_t addr, uint8_t *buf, size_t size) 53 | ``` 54 | 55 | | Parameters | Description | 56 | | :----- | :----------------------- | 57 | | part | Partition object | 58 | | addr | Relative partition offset address | 59 | | buf | Buffer to store the data to be read | 60 | | size | The size of the data to be read | 61 | | return | Return the actual read data size | 62 | 63 | ## Write data to partition 64 | 65 | ```C 66 | int fal_partition_write(const struct fal_partition *part, uint32_t addr, const uint8_t *buf, size_t size) 67 | ``` 68 | 69 | | Parameters | Description | 70 | | :----- | :----------------------- | 71 | | part | Partition object | 72 | | addr | Relative partition offset address | 73 | | buf | Buffer to store data to be written | 74 | | size | The size of the data to be written | 75 | | return | Return the actual written data size | 76 | 77 | ## Erase partition data 78 | 79 | ```C 80 | int fal_partition_erase(const struct fal_partition *part, uint32_t addr, size_t size) 81 | ``` 82 | 83 | | Parameters | Description | 84 | | :----- | :----------------------- | 85 | | part | Partition object | 86 | | addr | Relative partition offset address | 87 | | size | The size of the erased area | 88 | | return | Return the actual erased area size | 89 | 90 | ## Erase the entire partition data 91 | 92 | ```C 93 | int fal_partition_erase_all(const struct fal_partition *part) 94 | ``` 95 | 96 | | Parameters | Description | 97 | | :----- | :----------------------- | 98 | | part | Partition object | 99 | | return | Return the actual erased area size | 100 | 101 | ## Print partition table 102 | 103 | ```c 104 | void fal_show_part_table(void) 105 | ``` 106 | 107 | ## Create block device 108 | 109 | This function can create the corresponding block device according to the specified partition name, so as to mount the file system on the specified partition 110 | 111 | ```C 112 | struct rt_device *fal_blk_device_create(const char *parition_name) 113 | ``` 114 | 115 | | Parameters | Description | 116 | | :----- | :----------------------- | 117 | | parition_name | partition name | 118 | | return | If the creation is successful, the corresponding block device will be returned, and if it fails, empty | 119 | 120 | ## Create MTD Nor Flash device 121 | 122 | This function can create the corresponding MTD Nor Flash device according to the specified partition name, so as to mount the file system on the specified partition 123 | 124 | ```C 125 | struct rt_device *fal_mtd_nor_device_create(const char *parition_name) 126 | ``` 127 | 128 | | Parameters | Description | 129 | | :------------ | :---------------------------------- ------------------ | 130 | | parition_name | Partition name | 131 | | return | If the creation is successful, the corresponding MTD Nor Flash device will be returned, otherwise empty | 132 | 133 | ## Create a character device 134 | 135 | This function can create the corresponding character device according to the specified partition name to facilitate the operation of the partition through the deivice interface or the devfs interface. After POSIX is turned on, the partition can also be operated through the open/read/write function. 136 | 137 | ```C 138 | struct rt_device *fal_char_device_create(const char *parition_name) 139 | ``` 140 | 141 | | Parameters | Description | 142 | | :------------ | :---------------------------------- ------- | 143 | | parition_name | partition name | 144 | | return | If the creation is successful, the corresponding character device will be returned, otherwise empty | -------------------------------------------------------------------------------- /docs/figures/fal-api-en.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/fal/aa0211218634a613d3638425a830854bd07a0b76/docs/figures/fal-api-en.png -------------------------------------------------------------------------------- /docs/figures/fal-api.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/fal/aa0211218634a613d3638425a830854bd07a0b76/docs/figures/fal-api.png -------------------------------------------------------------------------------- /docs/figures/fal-port-en.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/fal/aa0211218634a613d3638425a830854bd07a0b76/docs/figures/fal-port-en.png -------------------------------------------------------------------------------- /docs/figures/fal-port.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/fal/aa0211218634a613d3638425a830854bd07a0b76/docs/figures/fal-port.png -------------------------------------------------------------------------------- /docs/figures/fal_framework-en.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/fal/aa0211218634a613d3638425a830854bd07a0b76/docs/figures/fal_framework-en.png -------------------------------------------------------------------------------- /docs/figures/fal_framework.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RT-Thread-packages/fal/aa0211218634a613d3638425a830854bd07a0b76/docs/figures/fal_framework.png -------------------------------------------------------------------------------- /inc/fal.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2018, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-05-17 armink the first version 9 | */ 10 | 11 | #ifndef _FAL_H_ 12 | #define _FAL_H_ 13 | 14 | #include 15 | #include 16 | #include "fal_def.h" 17 | 18 | /** 19 | * FAL (Flash Abstraction Layer) initialization. 20 | * It will initialize all flash device and all flash partition. 21 | * 22 | * @return >= 0: partitions total number 23 | */ 24 | int fal_init(void); 25 | 26 | /* =============== flash device operator API =============== */ 27 | /** 28 | * find flash device by name 29 | * 30 | * @param name flash device name 31 | * 32 | * @return != NULL: flash device 33 | * NULL: not found 34 | */ 35 | const struct fal_flash_dev *fal_flash_device_find(const char *name); 36 | 37 | /* =============== partition operator API =============== */ 38 | /** 39 | * find the partition by name 40 | * 41 | * @param name partition name 42 | * 43 | * @return != NULL: partition 44 | * NULL: not found 45 | */ 46 | const struct fal_partition *fal_partition_find(const char *name); 47 | 48 | /** 49 | * get the partition table 50 | * 51 | * @param len return the partition table length 52 | * 53 | * @return partition table 54 | */ 55 | const struct fal_partition *fal_get_partition_table(size_t *len); 56 | 57 | /** 58 | * set partition table temporarily 59 | * This setting will modify the partition table temporarily, the setting will be lost after restart. 60 | * 61 | * @param table partition table 62 | * @param len partition table length 63 | */ 64 | void fal_set_partition_table_temp(struct fal_partition *table, size_t len); 65 | 66 | /** 67 | * read data from partition 68 | * 69 | * @param part partition 70 | * @param addr relative address for partition 71 | * @param buf read buffer 72 | * @param size read size 73 | * 74 | * @return >= 0: successful read data size 75 | * -1: error 76 | */ 77 | int fal_partition_read(const struct fal_partition *part, uint32_t addr, uint8_t *buf, size_t size); 78 | 79 | /** 80 | * write data to partition 81 | * 82 | * @param part partition 83 | * @param addr relative address for partition 84 | * @param buf write buffer 85 | * @param size write size 86 | * 87 | * @return >= 0: successful write data size 88 | * -1: error 89 | */ 90 | int fal_partition_write(const struct fal_partition *part, uint32_t addr, const uint8_t *buf, size_t size); 91 | 92 | /** 93 | * erase partition data 94 | * 95 | * @param part partition 96 | * @param addr relative address for partition 97 | * @param size erase size 98 | * 99 | * @return >= 0: successful erased data size 100 | * -1: error 101 | */ 102 | int fal_partition_erase(const struct fal_partition *part, uint32_t addr, size_t size); 103 | 104 | /** 105 | * erase partition all data 106 | * 107 | * @param part partition 108 | * 109 | * @return >= 0: successful erased data size 110 | * -1: error 111 | */ 112 | int fal_partition_erase_all(const struct fal_partition *part); 113 | 114 | /** 115 | * print the partition table 116 | */ 117 | void fal_show_part_table(void); 118 | 119 | /* =============== API provided to RT-Thread =============== */ 120 | /** 121 | * create RT-Thread block device by specified partition 122 | * 123 | * @param parition_name partition name 124 | * 125 | * @return != NULL: created block device 126 | * NULL: created failed 127 | */ 128 | struct rt_device *fal_blk_device_create(const char *parition_name); 129 | 130 | #if defined(RT_USING_MTD_NOR) 131 | /** 132 | * create RT-Thread MTD NOR device by specified partition 133 | * 134 | * @param parition_name partition name 135 | * 136 | * @return != NULL: created MTD NOR device 137 | * NULL: created failed 138 | */ 139 | struct rt_device *fal_mtd_nor_device_create(const char *parition_name); 140 | #endif /* defined(RT_USING_MTD_NOR) */ 141 | 142 | /** 143 | * create RT-Thread char device by specified partition 144 | * 145 | * @param parition_name partition name 146 | * 147 | * @return != NULL: created char device 148 | * NULL: created failed 149 | */ 150 | struct rt_device *fal_char_device_create(const char *parition_name); 151 | 152 | #endif /* _FAL_H_ */ 153 | -------------------------------------------------------------------------------- /inc/fal_def.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2018, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-05-17 armink the first version 9 | */ 10 | 11 | #ifndef _FAL_DEF_H_ 12 | #define _FAL_DEF_H_ 13 | 14 | #include 15 | #include 16 | 17 | #define FAL_SW_VERSION "1.0.0" 18 | 19 | #ifdef __RTTHREAD__ /* for RT-Thread platform */ 20 | #include 21 | #define FAL_PRINTF rt_kprintf 22 | #define FAL_MALLOC rt_malloc 23 | #define FAL_CALLOC rt_calloc 24 | #define FAL_REALLOC rt_realloc 25 | #define FAL_FREE rt_free 26 | #endif 27 | 28 | #ifndef FAL_MALLOC 29 | #define FAL_MALLOC malloc 30 | #endif 31 | 32 | #ifndef FAL_CALLOC 33 | #define FAL_CALLOC calloc 34 | #endif 35 | 36 | #ifndef FAL_REALLOC 37 | #define FAL_REALLOC realloc 38 | #endif 39 | 40 | #ifndef FAL_FREE 41 | #define FAL_FREE free 42 | #endif 43 | 44 | #ifndef FAL_PRINTF 45 | #define FAL_PRINTF printf 46 | #endif 47 | 48 | #ifndef FAL_DEBUG 49 | #define FAL_DEBUG 0 50 | #endif 51 | 52 | #if FAL_DEBUG 53 | #ifdef assert 54 | #undef assert 55 | #endif 56 | #define assert(EXPR) \ 57 | if (!(EXPR)) \ 58 | { \ 59 | FAL_PRINTF("(%s) has assert failed at %s.\n", #EXPR, __FUNCTION__); \ 60 | while (1); \ 61 | } 62 | 63 | /* debug level log */ 64 | #ifdef log_d 65 | #undef log_d 66 | #endif 67 | #define log_d(...) FAL_PRINTF("[D/FAL] (%s:%d) ", __FUNCTION__, __LINE__); FAL_PRINTF(__VA_ARGS__);FAL_PRINTF("\n") 68 | 69 | #else 70 | 71 | #ifdef assert 72 | #undef assert 73 | #endif 74 | #define assert(EXPR) ((void)0); 75 | 76 | /* debug level log */ 77 | #ifdef log_d 78 | #undef log_d 79 | #endif 80 | #define log_d(...) 81 | #endif /* FAL_DEBUG */ 82 | 83 | /* error level log */ 84 | #ifdef log_e 85 | #undef log_e 86 | #endif 87 | #define log_e(...) FAL_PRINTF("\033[31;22m[E/FAL] (%s:%d) ", __FUNCTION__, __LINE__);FAL_PRINTF(__VA_ARGS__);FAL_PRINTF("\033[0m\n") 88 | 89 | /* info level log */ 90 | #ifdef log_i 91 | #undef log_i 92 | #endif 93 | #define log_i(...) FAL_PRINTF("\033[32;22m[I/FAL] "); FAL_PRINTF(__VA_ARGS__);FAL_PRINTF("\033[0m\n") 94 | 95 | /* FAL flash and partition device name max length */ 96 | #ifndef FAL_DEV_NAME_MAX 97 | #define FAL_DEV_NAME_MAX 24 98 | #endif 99 | 100 | struct fal_flash_dev 101 | { 102 | char name[FAL_DEV_NAME_MAX]; 103 | 104 | /* flash device start address and len */ 105 | uint32_t addr; 106 | size_t len; 107 | /* the block size in the flash for erase minimum granularity */ 108 | size_t blk_size; 109 | 110 | struct 111 | { 112 | int (*init)(void); 113 | int (*read)(long offset, uint8_t *buf, size_t size); 114 | int (*write)(long offset, const uint8_t *buf, size_t size); 115 | int (*erase)(long offset, size_t size); 116 | } ops; 117 | 118 | /* write minimum granularity, unit: bit. 119 | 1(nor flash)/ 8(stm32f2/f4)/ 32(stm32f1)/ 64(stm32l4) 120 | 0 will not take effect. */ 121 | size_t write_gran; 122 | }; 123 | typedef struct fal_flash_dev *fal_flash_dev_t; 124 | 125 | /** 126 | * FAL partition 127 | */ 128 | struct fal_partition 129 | { 130 | uint32_t magic_word; 131 | 132 | /* partition name */ 133 | char name[FAL_DEV_NAME_MAX]; 134 | /* flash device name for partition */ 135 | char flash_name[FAL_DEV_NAME_MAX]; 136 | 137 | /* partition offset address on flash device */ 138 | long offset; 139 | size_t len; 140 | 141 | uint32_t reserved; 142 | }; 143 | typedef struct fal_partition *fal_partition_t; 144 | 145 | #endif /* _FAL_DEF_H_ */ 146 | -------------------------------------------------------------------------------- /samples/README.md: -------------------------------------------------------------------------------- 1 | | 文件夹 | 说明 | 2 | | :------ | :----------------------- | 3 | | porting | 移植相关的示例代码及文档 | 4 | 5 | -------------------------------------------------------------------------------- /samples/porting/README.md: -------------------------------------------------------------------------------- 1 | # Flash 设备及分区移植示例 2 | 3 | 本示例主要演示 Flash 设备及分区相关的移植。 4 | 5 | ## 1、Flash 设备 6 | 7 | 在定义 Flash 设备表前,需要先定义 Flash 设备,参考 [`fal_flash_sfud_port.c`](fal_flash_sfud_port.c) (基于 [SFUD](https://github.com/armink/SFUD) 万能 SPI Flash 驱动的 Flash 设备)与 [`fal_flash_stm32f2_port.c`](fal_flash_stm32f2_port.c) (STM32F2 片内 Flash)这两个文件。这里简介下 `fal_flash_stm32f2_port.c` 里的代码实现。 8 | 9 | ### 1.1 定义 Flash 设备 10 | 11 | 针对 Flash 的不同操作,这里定义了如下几个操作函数: 12 | 13 | - `static int init(void)`:**可选** 的初始化操作 14 | 15 | - `static int read(long offset, uint8_t *buf, size_t size)`:读取操作 16 | 17 | |参数 |描述| 18 | |:----- |:----| 19 | |offset |读取数据的 Flash 偏移地址| 20 | |buf |存放待读取数据的缓冲区| 21 | |size |待读取数据的大小| 22 | |return |返回实际读取的数据大小| 23 | 24 | - `static int write(long offset, const uint8_t *buf, size_t size)` :写入操作 25 | 26 | | 参数 | 描述 | 27 | | :----- | :------------------------ | 28 | | offset | 写入数据的 Flash 偏移地址 | 29 | | buf | 存放待写入数据的缓冲区 | 30 | | size | 待写入数据的大小 | 31 | | return | 返回实际写入的数据大小 | 32 | 33 | - `static int erase(long offset, size_t size)` :擦除操作 34 | 35 | | 参数 | 描述 | 36 | | :----- | :------------------------ | 37 | | offset | 擦除区域的 Flash 偏移地址 | 38 | | size | 擦除区域的大小 | 39 | | return | 返回实际擦除的区域大小 | 40 | 41 | 用户需要根据自己的 Flash 情况分别实现这些操作函数。在文件最底部定义了具体的 Flash 设备对象(stm32f2_onchip_flash): 42 | 43 | `const struct fal_flash_dev stm32f2_onchip_flash = { "stm32_onchip", 0x08000000, 1024*1024, 128*1024, {init, read, write, erase} };` 44 | 45 | - `"stm32_onchip"` : Flash 设备的名字 46 | - 0x08000000: 对 Flash 操作的起始地址 47 | - 1024*1024:Flash 的总大小(1MB) 48 | - 128*1024:Flash 块/扇区大小(因为 STM32F2 各块大小不均匀,所以擦除粒度为最大块的大小:128K) 49 | - {init, read, write, erase} }:Flash 的操作函数。 如果没有 init 初始化过程,第一个操作函数位置可以置空。 50 | 51 | ### 1.2 定义 Flash 设备表 52 | 53 | Flash 设备表定义在 `fal_cfg.h` 头文件中,定义分区表前需 **新建 `fal_cfg.h` 文件** 。 54 | 55 | 参考 [示例文件 samples/porting/fal_cfg.h](samples/porting/fal_cfg.h) 或如下代码: 56 | 57 | ```c 58 | /* ===================== Flash device Configuration ========================= */ 59 | extern const struct fal_flash_dev stm32f2_onchip_flash; 60 | extern struct fal_flash_dev nor_flash0; 61 | 62 | /* flash device table */ 63 | #define FAL_FLASH_DEV_TABLE \ 64 | { \ 65 | &stm32f2_onchip_flash, \ 66 | &nor_flash0, \ 67 | } 68 | ``` 69 | 70 | Flash 设备表中,有两个 Flash 对象,一个为 STM32F2 的片内 Flash ,一个为片外的 Nor Flash。 71 | 72 | ## 2、Flash 分区 73 | 74 | Flash 分区基于 Flash 设备,每个 Flash 设备又可以有 N 个分区,这些分区的集合就是分区表。在配置分区表前,务必保证已定义好 Flash 设备及设备表。 75 | 76 | 分区表也定义在 `fal_cfg.h` 头文件中。参考 [示例文件 samples/porting/fal_cfg.h](samples/porting/fal_cfg.h) 或如下代码: 77 | 78 | ```C 79 | #define NOR_FLASH_DEV_NAME "norflash0" 80 | /* ====================== Partition Configuration ========================== */ 81 | #ifdef FAL_PART_HAS_TABLE_CFG 82 | /* partition table */ 83 | #define FAL_PART_TABLE \ 84 | { \ 85 | {FAL_PART_MAGIC_WORD, "bl", "stm32_onchip", 0, 64*1024, 0}, \ 86 | {FAL_PART_MAGIC_WORD, "app", "stm32_onchip", 64*1024, 704*1024, 0}, \ 87 | {FAL_PART_MAGIC_WORD, "easyflash", NOR_FLASH_DEV_NAME, 0, 1024*1024, 0}, \ 88 | {FAL_PART_MAGIC_WORD, "download", NOR_FLASH_DEV_NAME, 1024*1024, 1024*1024, 0}, \ 89 | } 90 | #endif /* FAL_PART_HAS_TABLE_CFG */ 91 | ``` 92 | 93 | 上面这个分区表详细描述信息如下: 94 | 95 | | 分区名 | Flash 设备名 | 偏移地址 | 大小 | 说明 | 96 | | :---------- | :------------- | :-------- | :---- | :----------------- | 97 | | "bl" | "stm32_onchip" | 0 | 64KB | 引导程序 | 98 | | "app" | "stm32_onchip" | 64*1024 | 704KB | 应用程序 | 99 | | "easyflash" | "norflash0" | 0 | 1MB | EasyFlash 参数存储 | 100 | | "download" | "norflash0" | 1024*1024 | 1MB | OTA 下载区 | 101 | 102 | 用户需要修改的分区参数包括:分区名称、关联的 Flash 设备名、偏移地址(相对 Flash 设备内部)、大小,需要注意以下几点: 103 | 104 | - 分区名保证 **不能重复** 105 | - 关联的 Flash 设备 **务必已经在 Flash 设备表中定义好** ,并且 **名称一致** ,否则会出现无法找到 Flash 设备的错误 106 | - 分区的起始地址和大小 **不能超过 Flash 设备的地址范围** ,否则会导致包初始化错误 107 | 108 | > 注意:每个分区定义时,除了填写上面介绍的参数属性外,需在前面增加 `FAL_PART_MAGIC_WORD` 属性,末尾增加 `0` (目前用于保留功能) 109 | -------------------------------------------------------------------------------- /samples/porting/fal_cfg.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2018, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-05-17 armink the first version 9 | */ 10 | 11 | #ifndef _FAL_CFG_H_ 12 | #define _FAL_CFG_H_ 13 | 14 | #include 15 | #include 16 | 17 | #define NOR_FLASH_DEV_NAME "norflash0" 18 | 19 | /* ===================== Flash device Configuration ========================= */ 20 | extern const struct fal_flash_dev stm32f2_onchip_flash; 21 | extern struct fal_flash_dev nor_flash0; 22 | 23 | /* flash device table */ 24 | #define FAL_FLASH_DEV_TABLE \ 25 | { \ 26 | &stm32f2_onchip_flash, \ 27 | &nor_flash0, \ 28 | } 29 | /* ====================== Partition Configuration ========================== */ 30 | #ifdef FAL_PART_HAS_TABLE_CFG 31 | /* partition table */ 32 | #define FAL_PART_TABLE \ 33 | { \ 34 | {FAL_PART_MAGIC_WORD, "bl", "stm32_onchip", 0, 64*1024, 0}, \ 35 | {FAL_PART_MAGIC_WORD, "app", "stm32_onchip", 64*1024, 704*1024, 0}, \ 36 | {FAL_PART_MAGIC_WORD, "easyflash", NOR_FLASH_DEV_NAME, 0, 1024*1024, 0}, \ 37 | {FAL_PART_MAGIC_WORD, "download", NOR_FLASH_DEV_NAME, 1024*1024, 1024*1024, 0}, \ 38 | } 39 | #endif /* FAL_PART_HAS_TABLE_CFG */ 40 | 41 | #endif /* _FAL_CFG_H_ */ 42 | -------------------------------------------------------------------------------- /samples/porting/fal_flash_sfud_port.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2018, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-01-26 armink the first version 9 | */ 10 | 11 | #include 12 | #include 13 | 14 | #ifdef FAL_USING_SFUD_PORT 15 | #ifdef RT_USING_SFUD 16 | #include 17 | #endif 18 | 19 | #ifndef FAL_USING_NOR_FLASH_DEV_NAME 20 | #define FAL_USING_NOR_FLASH_DEV_NAME "norflash0" 21 | #endif 22 | 23 | static int init(void); 24 | static int read(long offset, uint8_t *buf, size_t size); 25 | static int write(long offset, const uint8_t *buf, size_t size); 26 | static int erase(long offset, size_t size); 27 | 28 | static sfud_flash_t sfud_dev = NULL; 29 | struct fal_flash_dev nor_flash0 = 30 | { 31 | .name = FAL_USING_NOR_FLASH_DEV_NAME, 32 | .addr = 0, 33 | .len = 8 * 1024 * 1024, 34 | .blk_size = 4096, 35 | .ops = {init, read, write, erase}, 36 | .write_gran = 1 37 | }; 38 | 39 | static int init(void) 40 | { 41 | 42 | #ifdef RT_USING_SFUD 43 | /* RT-Thread RTOS platform */ 44 | sfud_dev = rt_sfud_flash_find_by_dev_name(FAL_USING_NOR_FLASH_DEV_NAME); 45 | #else 46 | /* bare metal platform */ 47 | extern sfud_flash sfud_norflash0; 48 | sfud_dev = &sfud_norflash0; 49 | #endif 50 | 51 | if (NULL == sfud_dev) 52 | { 53 | return -1; 54 | } 55 | 56 | /* update the flash chip information */ 57 | nor_flash0.blk_size = sfud_dev->chip.erase_gran; 58 | nor_flash0.len = sfud_dev->chip.capacity; 59 | 60 | return 0; 61 | } 62 | 63 | static int read(long offset, uint8_t *buf, size_t size) 64 | { 65 | assert(sfud_dev); 66 | assert(sfud_dev->init_ok); 67 | sfud_read(sfud_dev, nor_flash0.addr + offset, size, buf); 68 | 69 | return size; 70 | } 71 | 72 | static int write(long offset, const uint8_t *buf, size_t size) 73 | { 74 | assert(sfud_dev); 75 | assert(sfud_dev->init_ok); 76 | if (sfud_write(sfud_dev, nor_flash0.addr + offset, size, buf) != SFUD_SUCCESS) 77 | { 78 | return -1; 79 | } 80 | 81 | return size; 82 | } 83 | 84 | static int erase(long offset, size_t size) 85 | { 86 | assert(sfud_dev); 87 | assert(sfud_dev->init_ok); 88 | if (sfud_erase(sfud_dev, nor_flash0.addr + offset, size) != SFUD_SUCCESS) 89 | { 90 | return -1; 91 | } 92 | 93 | return size; 94 | } 95 | #endif /* FAL_USING_SFUD_PORT */ 96 | 97 | -------------------------------------------------------------------------------- /samples/porting/fal_flash_stm32f2_port.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2018, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-01-26 armink the first version 9 | */ 10 | 11 | #include 12 | 13 | #include 14 | 15 | /* base address of the flash sectors */ 16 | #define ADDR_FLASH_SECTOR_0 ((uint32_t)0x08000000) /* Base address of Sector 0, 16 K bytes */ 17 | #define ADDR_FLASH_SECTOR_1 ((uint32_t)0x08004000) /* Base address of Sector 1, 16 K bytes */ 18 | #define ADDR_FLASH_SECTOR_2 ((uint32_t)0x08008000) /* Base address of Sector 2, 16 K bytes */ 19 | #define ADDR_FLASH_SECTOR_3 ((uint32_t)0x0800C000) /* Base address of Sector 3, 16 K bytes */ 20 | #define ADDR_FLASH_SECTOR_4 ((uint32_t)0x08010000) /* Base address of Sector 4, 64 K bytes */ 21 | #define ADDR_FLASH_SECTOR_5 ((uint32_t)0x08020000) /* Base address of Sector 5, 128 K bytes */ 22 | #define ADDR_FLASH_SECTOR_6 ((uint32_t)0x08040000) /* Base address of Sector 6, 128 K bytes */ 23 | #define ADDR_FLASH_SECTOR_7 ((uint32_t)0x08060000) /* Base address of Sector 7, 128 K bytes */ 24 | #define ADDR_FLASH_SECTOR_8 ((uint32_t)0x08080000) /* Base address of Sector 8, 128 K bytes */ 25 | #define ADDR_FLASH_SECTOR_9 ((uint32_t)0x080A0000) /* Base address of Sector 9, 128 K bytes */ 26 | #define ADDR_FLASH_SECTOR_10 ((uint32_t)0x080C0000) /* Base address of Sector 10, 128 K bytes */ 27 | #define ADDR_FLASH_SECTOR_11 ((uint32_t)0x080E0000) /* Base address of Sector 11, 128 K bytes */ 28 | 29 | /** 30 | * Get the sector of a given address 31 | * 32 | * @param address flash address 33 | * 34 | * @return The sector of a given address 35 | */ 36 | static uint32_t stm32_get_sector(uint32_t address) 37 | { 38 | uint32_t sector = 0; 39 | 40 | if ((address < ADDR_FLASH_SECTOR_1) && (address >= ADDR_FLASH_SECTOR_0)) 41 | { 42 | sector = FLASH_Sector_0; 43 | } 44 | else if ((address < ADDR_FLASH_SECTOR_2) && (address >= ADDR_FLASH_SECTOR_1)) 45 | { 46 | sector = FLASH_Sector_1; 47 | } 48 | else if ((address < ADDR_FLASH_SECTOR_3) && (address >= ADDR_FLASH_SECTOR_2)) 49 | { 50 | sector = FLASH_Sector_2; 51 | } 52 | else if ((address < ADDR_FLASH_SECTOR_4) && (address >= ADDR_FLASH_SECTOR_3)) 53 | { 54 | sector = FLASH_Sector_3; 55 | } 56 | else if ((address < ADDR_FLASH_SECTOR_5) && (address >= ADDR_FLASH_SECTOR_4)) 57 | { 58 | sector = FLASH_Sector_4; 59 | } 60 | else if ((address < ADDR_FLASH_SECTOR_6) && (address >= ADDR_FLASH_SECTOR_5)) 61 | { 62 | sector = FLASH_Sector_5; 63 | } 64 | else if ((address < ADDR_FLASH_SECTOR_7) && (address >= ADDR_FLASH_SECTOR_6)) 65 | { 66 | sector = FLASH_Sector_6; 67 | } 68 | else if ((address < ADDR_FLASH_SECTOR_8) && (address >= ADDR_FLASH_SECTOR_7)) 69 | { 70 | sector = FLASH_Sector_7; 71 | } 72 | else if ((address < ADDR_FLASH_SECTOR_9) && (address >= ADDR_FLASH_SECTOR_8)) 73 | { 74 | sector = FLASH_Sector_8; 75 | } 76 | else if ((address < ADDR_FLASH_SECTOR_10) && (address >= ADDR_FLASH_SECTOR_9)) 77 | { 78 | sector = FLASH_Sector_9; 79 | } 80 | else if ((address < ADDR_FLASH_SECTOR_11) && (address >= ADDR_FLASH_SECTOR_10)) 81 | { 82 | sector = FLASH_Sector_10; 83 | } 84 | else 85 | { 86 | sector = FLASH_Sector_11; 87 | } 88 | 89 | return sector; 90 | } 91 | 92 | /** 93 | * Get the sector size 94 | * 95 | * @param sector sector 96 | * 97 | * @return sector size 98 | */ 99 | static uint32_t stm32_get_sector_size(uint32_t sector) { 100 | assert(IS_FLASH_SECTOR(sector)); 101 | 102 | switch (sector) { 103 | case FLASH_Sector_0: return 16 * 1024; 104 | case FLASH_Sector_1: return 16 * 1024; 105 | case FLASH_Sector_2: return 16 * 1024; 106 | case FLASH_Sector_3: return 16 * 1024; 107 | case FLASH_Sector_4: return 64 * 1024; 108 | case FLASH_Sector_5: return 128 * 1024; 109 | case FLASH_Sector_6: return 128 * 1024; 110 | case FLASH_Sector_7: return 128 * 1024; 111 | case FLASH_Sector_8: return 128 * 1024; 112 | case FLASH_Sector_9: return 128 * 1024; 113 | case FLASH_Sector_10: return 128 * 1024; 114 | case FLASH_Sector_11: return 128 * 1024; 115 | default : return 128 * 1024; 116 | } 117 | } 118 | static int init(void) 119 | { 120 | /* do nothing now */ 121 | } 122 | 123 | static int read(long offset, uint8_t *buf, size_t size) 124 | { 125 | size_t i; 126 | uint32_t addr = stm32f2_onchip_flash.addr + offset; 127 | for (i = 0; i < size; i++, addr++, buf++) 128 | { 129 | *buf = *(uint8_t *) addr; 130 | } 131 | 132 | return size; 133 | } 134 | 135 | static int write(long offset, const uint8_t *buf, size_t size) 136 | { 137 | size_t i; 138 | uint32_t read_data; 139 | uint32_t addr = stm32f2_onchip_flash.addr + offset; 140 | 141 | FLASH_Unlock(); 142 | FLASH_ClearFlag( 143 | FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR 144 | | FLASH_FLAG_PGSERR); 145 | for (i = 0; i < size; i++, buf++, addr++) 146 | { 147 | /* write data */ 148 | FLASH_ProgramByte(addr, *buf); 149 | read_data = *(uint8_t *) addr; 150 | /* check data */ 151 | if (read_data != *buf) 152 | { 153 | return -1; 154 | } 155 | } 156 | FLASH_Lock(); 157 | 158 | return size; 159 | } 160 | 161 | static int erase(long offset, size_t size) 162 | { 163 | FLASH_Status flash_status; 164 | size_t erased_size = 0; 165 | uint32_t cur_erase_sector; 166 | uint32_t addr = stm32f2_onchip_flash.addr + offset; 167 | 168 | /* start erase */ 169 | FLASH_Unlock(); 170 | FLASH_ClearFlag( 171 | FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR 172 | | FLASH_FLAG_PGSERR); 173 | /* it will stop when erased size is greater than setting size */ 174 | while (erased_size < size) 175 | { 176 | cur_erase_sector = stm32_get_sector(addr + erased_size); 177 | flash_status = FLASH_EraseSector(cur_erase_sector, VoltageRange_3); 178 | if (flash_status != FLASH_COMPLETE) 179 | { 180 | return -1; 181 | } 182 | erased_size += stm32_get_sector_size(cur_erase_sector); 183 | } 184 | FLASH_Lock(); 185 | 186 | return size; 187 | } 188 | 189 | const struct fal_flash_dev stm32f2_onchip_flash = 190 | { 191 | .name = "stm32_onchip", 192 | .addr = 0x08000000, 193 | .len = 1024*1024, 194 | .blk_size = 128*1024, 195 | .ops = {init, read, write, erase}, 196 | .write_gran = 8 197 | }; 198 | 199 | -------------------------------------------------------------------------------- /src/fal.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2018, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-05-17 armink the first version 9 | */ 10 | 11 | #include 12 | 13 | static uint8_t init_ok = 0; 14 | 15 | /** 16 | * FAL (Flash Abstraction Layer) initialization. 17 | * It will initialize all flash device and all flash partition. 18 | * 19 | * @return >= 0: partitions total number 20 | */ 21 | int fal_init(void) 22 | { 23 | extern int fal_flash_init(void); 24 | extern int fal_partition_init(void); 25 | 26 | int result; 27 | 28 | /* initialize all flash device on FAL flash table */ 29 | result = fal_flash_init(); 30 | 31 | if (result < 0) { 32 | goto __exit; 33 | } 34 | 35 | /* initialize all flash partition on FAL partition table */ 36 | result = fal_partition_init(); 37 | 38 | __exit: 39 | 40 | if ((result > 0) && (!init_ok)) 41 | { 42 | init_ok = 1; 43 | log_i("RT-Thread Flash Abstraction Layer (V%s) initialize success.", FAL_SW_VERSION); 44 | } 45 | else if(result <= 0) 46 | { 47 | init_ok = 0; 48 | log_e("RT-Thread Flash Abstraction Layer (V%s) initialize failed.", FAL_SW_VERSION); 49 | } 50 | 51 | return result; 52 | } 53 | 54 | /** 55 | * Check if the FAL is initialized successfully 56 | * 57 | * @return 0: not init or init failed; 1: init success 58 | */ 59 | int fal_init_check(void) 60 | { 61 | return init_ok; 62 | } 63 | -------------------------------------------------------------------------------- /src/fal_flash.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2018, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-05-17 armink the first version 9 | */ 10 | 11 | #include 12 | #include 13 | 14 | /* flash device table, must defined by user */ 15 | #if !defined(FAL_FLASH_DEV_TABLE) 16 | #error "You must defined flash device table (FAL_FLASH_DEV_TABLE) on 'fal_cfg.h'" 17 | #endif 18 | 19 | static const struct fal_flash_dev * const device_table[] = FAL_FLASH_DEV_TABLE; 20 | static const size_t device_table_len = sizeof(device_table) / sizeof(device_table[0]); 21 | static uint8_t init_ok = 0; 22 | 23 | /** 24 | * Initialize all flash device on FAL flash table 25 | * 26 | * @return result 27 | */ 28 | int fal_flash_init(void) 29 | { 30 | size_t i; 31 | 32 | if (init_ok) 33 | { 34 | return 0; 35 | } 36 | 37 | for (i = 0; i < device_table_len; i++) 38 | { 39 | assert(device_table[i]->ops.read); 40 | assert(device_table[i]->ops.write); 41 | assert(device_table[i]->ops.erase); 42 | /* init flash device on flash table */ 43 | if (device_table[i]->ops.init) 44 | { 45 | device_table[i]->ops.init(); 46 | } 47 | log_d("Flash device | %*.*s | addr: 0x%08lx | len: 0x%08x | blk_size: 0x%08x |initialized finish.", 48 | FAL_DEV_NAME_MAX, FAL_DEV_NAME_MAX, device_table[i]->name, device_table[i]->addr, device_table[i]->len, 49 | device_table[i]->blk_size); 50 | } 51 | 52 | init_ok = 1; 53 | return 0; 54 | } 55 | 56 | /** 57 | * find flash device by name 58 | * 59 | * @param name flash device name 60 | * 61 | * @return != NULL: flash device 62 | * NULL: not found 63 | */ 64 | const struct fal_flash_dev *fal_flash_device_find(const char *name) 65 | { 66 | assert(init_ok); 67 | assert(name); 68 | 69 | size_t i; 70 | 71 | for (i = 0; i < device_table_len; i++) 72 | { 73 | if (!strncmp(name, device_table[i]->name, FAL_DEV_NAME_MAX)) { 74 | return device_table[i]; 75 | } 76 | } 77 | 78 | return NULL; 79 | } 80 | -------------------------------------------------------------------------------- /src/fal_partition.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2018, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-05-17 armink the first version 9 | */ 10 | 11 | #include 12 | #include 13 | #include 14 | 15 | /* partition magic word */ 16 | #define FAL_PART_MAGIC_WORD 0x45503130 17 | #define FAL_PART_MAGIC_WORD_H 0x4550L 18 | #define FAL_PART_MAGIC_WORD_L 0x3130L 19 | #define FAL_PART_MAGIC_WROD 0x45503130 20 | 21 | struct part_flash_info 22 | { 23 | const struct fal_flash_dev *flash_dev; 24 | }; 25 | 26 | /** 27 | * FAL partition table config has defined on 'fal_cfg.h'. 28 | * When this option is disable, it will auto find the partition table on a specified location in flash partition. 29 | */ 30 | #ifdef FAL_PART_HAS_TABLE_CFG 31 | 32 | /* check partition table definition */ 33 | #if !defined(FAL_PART_TABLE) 34 | #error "You must defined FAL_PART_TABLE on 'fal_cfg.h'" 35 | #endif 36 | 37 | #ifdef __CC_ARM /* ARM Compiler */ 38 | #define SECTION(x) __attribute__((section(x))) 39 | #define USED __attribute__((used)) 40 | #elif defined (__IAR_SYSTEMS_ICC__) /* for IAR Compiler */ 41 | #define SECTION(x) @ x 42 | #define USED __root 43 | #elif defined (__GNUC__) /* GNU GCC Compiler */ 44 | #define SECTION(x) __attribute__((section(x))) 45 | #define USED __attribute__((used)) 46 | #else 47 | #error not supported tool chain 48 | #endif /* __CC_ARM */ 49 | 50 | /* partition table definition */ 51 | USED static const struct fal_partition partition_table_def[] SECTION("FalPartTable") = FAL_PART_TABLE; 52 | static const struct fal_partition *partition_table = NULL; 53 | /* partition and flash object information cache table */ 54 | static struct part_flash_info part_flash_cache[sizeof(partition_table_def) / sizeof(partition_table_def[0])] = { 0 }; 55 | 56 | #else /* FAL_PART_HAS_TABLE_CFG */ 57 | 58 | #if !defined(FAL_PART_TABLE_FLASH_DEV_NAME) 59 | #error "You must defined FAL_PART_TABLE_FLASH_DEV_NAME on 'fal_cfg.h'" 60 | #endif 61 | 62 | /* check partition table end offset address definition */ 63 | #if !defined(FAL_PART_TABLE_END_OFFSET) 64 | #error "You must defined FAL_PART_TABLE_END_OFFSET on 'fal_cfg.h'" 65 | #endif 66 | 67 | static struct fal_partition *partition_table = NULL; 68 | static struct part_flash_info *part_flash_cache = NULL; 69 | #endif /* FAL_PART_HAS_TABLE_CFG */ 70 | 71 | static uint8_t init_ok = 0; 72 | static size_t partition_table_len = 0; 73 | 74 | /** 75 | * print the partition table 76 | */ 77 | void fal_show_part_table(void) 78 | { 79 | char *item1 = "name", *item2 = "flash_dev"; 80 | size_t i, part_name_max = strlen(item1), flash_dev_name_max = strlen(item2); 81 | const struct fal_partition *part; 82 | 83 | if (partition_table_len) 84 | { 85 | for (i = 0; i < partition_table_len; i++) 86 | { 87 | part = &partition_table[i]; 88 | if (strlen(part->name) > part_name_max) 89 | { 90 | part_name_max = strlen(part->name); 91 | } 92 | if (strlen(part->flash_name) > flash_dev_name_max) 93 | { 94 | flash_dev_name_max = strlen(part->flash_name); 95 | } 96 | } 97 | } 98 | log_i("==================== FAL partition table ===================="); 99 | log_i("| %-*.*s | %-*.*s | offset | length |", part_name_max, FAL_DEV_NAME_MAX, item1, flash_dev_name_max, 100 | FAL_DEV_NAME_MAX, item2); 101 | log_i("-------------------------------------------------------------"); 102 | for (i = 0; i < partition_table_len; i++) 103 | { 104 | 105 | #ifdef FAL_PART_HAS_TABLE_CFG 106 | part = &partition_table[i]; 107 | #else 108 | part = &partition_table[partition_table_len - i - 1]; 109 | #endif 110 | 111 | log_i("| %-*.*s | %-*.*s | 0x%08lx | 0x%08x |", part_name_max, FAL_DEV_NAME_MAX, part->name, flash_dev_name_max, 112 | FAL_DEV_NAME_MAX, part->flash_name, part->offset, part->len); 113 | } 114 | log_i("============================================================="); 115 | } 116 | 117 | static int check_and_update_part_cache(const struct fal_partition *table, size_t len) 118 | { 119 | const struct fal_flash_dev *flash_dev = NULL; 120 | size_t i; 121 | 122 | #ifndef FAL_PART_HAS_TABLE_CFG 123 | if (part_flash_cache) 124 | { 125 | FAL_FREE(part_flash_cache); 126 | } 127 | part_flash_cache = FAL_MALLOC(len * sizeof(struct part_flash_info)); 128 | if (part_flash_cache == NULL) 129 | { 130 | log_e("Initialize failed! No memory for partition table cache"); 131 | return -2; 132 | } 133 | #endif 134 | 135 | for (i = 0; i < len; i++) 136 | { 137 | flash_dev = fal_flash_device_find(table[i].flash_name); 138 | if (flash_dev == NULL) 139 | { 140 | log_d("Warning: Do NOT found the flash device(%s).", table[i].flash_name); 141 | continue; 142 | } 143 | 144 | if (table[i].offset >= (long)flash_dev->len) 145 | { 146 | log_e("Initialize failed! Partition(%s) offset address(%ld) out of flash bound(<%d).", 147 | table[i].name, table[i].offset, flash_dev->len); 148 | partition_table_len = 0; 149 | 150 | return -1; 151 | } 152 | 153 | part_flash_cache[i].flash_dev = flash_dev; 154 | } 155 | 156 | return 0; 157 | } 158 | 159 | /** 160 | * Initialize all flash partition on FAL partition table 161 | * 162 | * @return partitions total number 163 | */ 164 | int fal_partition_init(void) 165 | { 166 | 167 | if (init_ok) 168 | { 169 | return partition_table_len; 170 | } 171 | 172 | #ifdef FAL_PART_HAS_TABLE_CFG 173 | partition_table = &partition_table_def[0]; 174 | partition_table_len = sizeof(partition_table_def) / sizeof(partition_table_def[0]); 175 | #else 176 | /* load partition table from the end address FAL_PART_TABLE_END_OFFSET, error return 0 */ 177 | long part_table_offset = FAL_PART_TABLE_END_OFFSET; 178 | size_t table_num = 0, table_item_size = 0; 179 | uint8_t part_table_find_ok = 0; 180 | uint32_t read_magic_word; 181 | fal_partition_t new_part = NULL; 182 | size_t i; 183 | const struct fal_flash_dev *flash_dev = NULL; 184 | 185 | flash_dev = fal_flash_device_find(FAL_PART_TABLE_FLASH_DEV_NAME); 186 | if (flash_dev == NULL) 187 | { 188 | log_e("Initialize failed! Flash device (%s) NOT found.", FAL_PART_TABLE_FLASH_DEV_NAME); 189 | goto _exit; 190 | } 191 | 192 | /* check partition table offset address */ 193 | if (part_table_offset < 0 || part_table_offset >= (long) flash_dev->len) 194 | { 195 | log_e("Setting partition table end offset address(%ld) out of flash bound(<%d).", part_table_offset, flash_dev->len); 196 | goto _exit; 197 | } 198 | 199 | table_item_size = sizeof(struct fal_partition); 200 | new_part = (fal_partition_t)FAL_MALLOC(table_item_size); 201 | if (new_part == NULL) 202 | { 203 | log_e("Initialize failed! No memory for table buffer."); 204 | goto _exit; 205 | } 206 | 207 | /* find partition table location */ 208 | { 209 | uint8_t read_buf[64]; 210 | 211 | part_table_offset -= sizeof(read_buf); 212 | while (part_table_offset >= 0) 213 | { 214 | if (flash_dev->ops.read(part_table_offset, read_buf, sizeof(read_buf)) > 0) 215 | { 216 | /* find magic word in read buf */ 217 | for (i = 0; i < sizeof(read_buf) - sizeof(read_magic_word) + 1; i++) 218 | { 219 | read_magic_word = read_buf[0 + i] + (read_buf[1 + i] << 8) + (read_buf[2 + i] << 16) + (read_buf[3 + i] << 24); 220 | if (read_magic_word == ((FAL_PART_MAGIC_WORD_H << 16) + FAL_PART_MAGIC_WORD_L)) 221 | { 222 | part_table_find_ok = 1; 223 | part_table_offset += i; 224 | log_d("Find the partition table on '%s' offset @0x%08lx.", FAL_PART_TABLE_FLASH_DEV_NAME, 225 | part_table_offset); 226 | break; 227 | } 228 | } 229 | } 230 | else 231 | { 232 | /* read failed */ 233 | break; 234 | } 235 | 236 | if (part_table_find_ok) 237 | { 238 | break; 239 | } 240 | else 241 | { 242 | /* calculate next read buf position */ 243 | if (part_table_offset >= (long)sizeof(read_buf)) 244 | { 245 | part_table_offset -= sizeof(read_buf); 246 | part_table_offset += (sizeof(read_magic_word) - 1); 247 | } 248 | else if (part_table_offset != 0) 249 | { 250 | part_table_offset = 0; 251 | } 252 | else 253 | { 254 | /* find failed */ 255 | break; 256 | } 257 | } 258 | } 259 | } 260 | 261 | /* load partition table */ 262 | while (part_table_find_ok) 263 | { 264 | memset(new_part, 0x00, table_num); 265 | if (flash_dev->ops.read(part_table_offset - table_item_size * (table_num), (uint8_t *) new_part, 266 | table_item_size) < 0) 267 | { 268 | log_e("Initialize failed! Flash device (%s) read error!", flash_dev->name); 269 | table_num = 0; 270 | break; 271 | } 272 | 273 | if (new_part->magic_word != ((FAL_PART_MAGIC_WORD_H << 16) + FAL_PART_MAGIC_WORD_L)) 274 | { 275 | break; 276 | } 277 | 278 | partition_table = (fal_partition_t) FAL_REALLOC(partition_table, table_item_size * (table_num + 1)); 279 | if (partition_table == NULL) 280 | { 281 | log_e("Initialize failed! No memory for partition table"); 282 | table_num = 0; 283 | break; 284 | } 285 | 286 | memcpy(partition_table + table_num, new_part, table_item_size); 287 | 288 | table_num++; 289 | }; 290 | 291 | if (table_num == 0) 292 | { 293 | log_e("Partition table NOT found on flash: %s (len: %d) from offset: 0x%08x.", FAL_PART_TABLE_FLASH_DEV_NAME, 294 | FAL_DEV_NAME_MAX, FAL_PART_TABLE_END_OFFSET); 295 | goto _exit; 296 | } 297 | else 298 | { 299 | partition_table_len = table_num; 300 | } 301 | #endif /* FAL_PART_HAS_TABLE_CFG */ 302 | 303 | /* check the partition table device exists */ 304 | if (check_and_update_part_cache(partition_table, partition_table_len) != 0) 305 | { 306 | goto _exit; 307 | } 308 | 309 | init_ok = 1; 310 | 311 | _exit: 312 | 313 | #if FAL_DEBUG 314 | fal_show_part_table(); 315 | #endif 316 | 317 | #ifndef FAL_PART_HAS_TABLE_CFG 318 | if (new_part) 319 | { 320 | FAL_FREE(new_part); 321 | } 322 | #endif /* !FAL_PART_HAS_TABLE_CFG */ 323 | 324 | return partition_table_len; 325 | } 326 | 327 | /** 328 | * find the partition by name 329 | * 330 | * @param name partition name 331 | * 332 | * @return != NULL: partition 333 | * NULL: not found 334 | */ 335 | const struct fal_partition *fal_partition_find(const char *name) 336 | { 337 | assert(init_ok); 338 | 339 | size_t i; 340 | 341 | for (i = 0; i < partition_table_len; i++) 342 | { 343 | if (!strcmp(name, partition_table[i].name)) 344 | { 345 | return &partition_table[i]; 346 | } 347 | } 348 | 349 | return NULL; 350 | } 351 | 352 | static const struct fal_flash_dev *flash_device_find_by_part(const struct fal_partition *part) 353 | { 354 | assert(part >= partition_table); 355 | assert(part <= &partition_table[partition_table_len - 1]); 356 | 357 | return part_flash_cache[part - partition_table].flash_dev; 358 | } 359 | 360 | /** 361 | * get the partition table 362 | * 363 | * @param len return the partition table length 364 | * 365 | * @return partition table 366 | */ 367 | const struct fal_partition *fal_get_partition_table(size_t *len) 368 | { 369 | assert(init_ok); 370 | assert(len); 371 | 372 | *len = partition_table_len; 373 | 374 | return partition_table; 375 | } 376 | 377 | /** 378 | * set partition table temporarily 379 | * This setting will modify the partition table temporarily, the setting will be lost after restart. 380 | * 381 | * @param table partition table 382 | * @param len partition table length 383 | */ 384 | void fal_set_partition_table_temp(struct fal_partition *table, size_t len) 385 | { 386 | assert(init_ok); 387 | assert(table); 388 | 389 | check_and_update_part_cache(table, len); 390 | 391 | partition_table_len = len; 392 | partition_table = table; 393 | } 394 | 395 | /** 396 | * read data from partition 397 | * 398 | * @param part partition 399 | * @param addr relative address for partition 400 | * @param buf read buffer 401 | * @param size read size 402 | * 403 | * @return >= 0: successful read data size 404 | * -1: error 405 | */ 406 | int fal_partition_read(const struct fal_partition *part, uint32_t addr, uint8_t *buf, size_t size) 407 | { 408 | int ret = 0; 409 | const struct fal_flash_dev *flash_dev = NULL; 410 | 411 | assert(part); 412 | assert(buf); 413 | 414 | if (addr + size > part->len) 415 | { 416 | log_e("Partition read error! Partition address out of bound."); 417 | return -1; 418 | } 419 | 420 | flash_dev = flash_device_find_by_part(part); 421 | if (flash_dev == NULL) 422 | { 423 | log_e("Partition read error! Don't found flash device(%s) of the partition(%s).", part->flash_name, part->name); 424 | return -1; 425 | } 426 | 427 | ret = flash_dev->ops.read(part->offset + addr, buf, size); 428 | if (ret < 0) 429 | { 430 | log_e("Partition read error! Flash device(%s) read error!", part->flash_name); 431 | } 432 | 433 | return ret; 434 | } 435 | 436 | /** 437 | * write data to partition 438 | * 439 | * @param part partition 440 | * @param addr relative address for partition 441 | * @param buf write buffer 442 | * @param size write size 443 | * 444 | * @return >= 0: successful write data size 445 | * -1: error 446 | */ 447 | int fal_partition_write(const struct fal_partition *part, uint32_t addr, const uint8_t *buf, size_t size) 448 | { 449 | int ret = 0; 450 | const struct fal_flash_dev *flash_dev = NULL; 451 | 452 | assert(part); 453 | assert(buf); 454 | 455 | if (addr + size > part->len) 456 | { 457 | log_e("Partition write error! Partition address out of bound."); 458 | return -1; 459 | } 460 | 461 | flash_dev = flash_device_find_by_part(part); 462 | if (flash_dev == NULL) 463 | { 464 | log_e("Partition write error! Don't found flash device(%s) of the partition(%s).", part->flash_name, part->name); 465 | return -1; 466 | } 467 | 468 | ret = flash_dev->ops.write(part->offset + addr, buf, size); 469 | if (ret < 0) 470 | { 471 | log_e("Partition write error! Flash device(%s) write error!", part->flash_name); 472 | } 473 | 474 | return ret; 475 | } 476 | 477 | /** 478 | * erase partition data 479 | * 480 | * @param part partition 481 | * @param addr relative address for partition 482 | * @param size erase size 483 | * 484 | * @return >= 0: successful erased data size 485 | * -1: error 486 | */ 487 | int fal_partition_erase(const struct fal_partition *part, uint32_t addr, size_t size) 488 | { 489 | int ret = 0; 490 | const struct fal_flash_dev *flash_dev = NULL; 491 | 492 | assert(part); 493 | 494 | if (addr + size > part->len) 495 | { 496 | log_e("Partition erase error! Partition address out of bound."); 497 | return -1; 498 | } 499 | 500 | flash_dev = flash_device_find_by_part(part); 501 | if (flash_dev == NULL) 502 | { 503 | log_e("Partition erase error! Don't found flash device(%s) of the partition(%s).", part->flash_name, part->name); 504 | return -1; 505 | } 506 | 507 | ret = flash_dev->ops.erase(part->offset + addr, size); 508 | if (ret < 0) 509 | { 510 | log_e("Partition erase error! Flash device(%s) erase error!", part->flash_name); 511 | } 512 | 513 | return ret; 514 | } 515 | 516 | /** 517 | * erase partition all data 518 | * 519 | * @param part partition 520 | * 521 | * @return >= 0: successful erased data size 522 | * -1: error 523 | */ 524 | int fal_partition_erase_all(const struct fal_partition *part) 525 | { 526 | return fal_partition_erase(part, 0, part->len); 527 | } 528 | -------------------------------------------------------------------------------- /src/fal_rtt.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2006-2018, RT-Thread Development Team 3 | * 4 | * SPDX-License-Identifier: Apache-2.0 5 | * 6 | * Change Logs: 7 | * Date Author Notes 8 | * 2018-06-23 armink the first version 9 | * 2019-08-22 MurphyZhao adapt to none rt-thread case 10 | */ 11 | 12 | #include 13 | 14 | #ifdef RT_VER_NUM 15 | #include 16 | #include 17 | #include 18 | 19 | /* ========================== block device ======================== */ 20 | struct fal_blk_device 21 | { 22 | struct rt_device parent; 23 | struct rt_device_blk_geometry geometry; 24 | const struct fal_partition *fal_part; 25 | }; 26 | 27 | /* RT-Thread device interface */ 28 | #if RTTHREAD_VERSION >= 30000 29 | static rt_err_t blk_dev_control(rt_device_t dev, int cmd, void *args) 30 | #else 31 | static rt_err_t blk_dev_control(rt_device_t dev, rt_uint8_t cmd, void *args) 32 | #endif 33 | { 34 | struct fal_blk_device *part = (struct fal_blk_device*) dev; 35 | 36 | assert(part != RT_NULL); 37 | 38 | if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME) 39 | { 40 | struct rt_device_blk_geometry *geometry; 41 | 42 | geometry = (struct rt_device_blk_geometry *) args; 43 | if (geometry == RT_NULL) 44 | { 45 | return -RT_ERROR; 46 | } 47 | 48 | memcpy(geometry, &part->geometry, sizeof(struct rt_device_blk_geometry)); 49 | } 50 | else if (cmd == RT_DEVICE_CTRL_BLK_ERASE) 51 | { 52 | rt_uint32_t *addrs = (rt_uint32_t *) args, start_addr = addrs[0], end_addr = addrs[1], phy_start_addr; 53 | rt_size_t phy_size; 54 | 55 | if (addrs == RT_NULL || start_addr > end_addr) 56 | { 57 | return -RT_ERROR; 58 | } 59 | 60 | if (end_addr == start_addr) 61 | { 62 | end_addr++; 63 | } 64 | 65 | phy_start_addr = start_addr * part->geometry.bytes_per_sector; 66 | phy_size = (end_addr - start_addr) * part->geometry.bytes_per_sector; 67 | 68 | if (fal_partition_erase(part->fal_part, phy_start_addr, phy_size) < 0) 69 | { 70 | return -RT_ERROR; 71 | } 72 | } 73 | 74 | return RT_EOK; 75 | } 76 | 77 | static rt_size_t blk_dev_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size) 78 | { 79 | int ret = 0; 80 | struct fal_blk_device *part = (struct fal_blk_device*) dev; 81 | 82 | assert(part != RT_NULL); 83 | 84 | ret = fal_partition_read(part->fal_part, pos * part->geometry.block_size, buffer, size * part->geometry.block_size); 85 | 86 | if (ret != (int)(size * part->geometry.block_size)) 87 | { 88 | ret = 0; 89 | } 90 | else 91 | { 92 | ret = size; 93 | } 94 | 95 | return ret; 96 | } 97 | 98 | static rt_size_t blk_dev_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size) 99 | { 100 | int ret = 0; 101 | struct fal_blk_device *part; 102 | rt_off_t phy_pos; 103 | rt_size_t phy_size; 104 | 105 | part = (struct fal_blk_device*) dev; 106 | assert(part != RT_NULL); 107 | 108 | /* change the block device's logic address to physical address */ 109 | phy_pos = pos * part->geometry.bytes_per_sector; 110 | phy_size = size * part->geometry.bytes_per_sector; 111 | 112 | ret = fal_partition_erase(part->fal_part, phy_pos, phy_size); 113 | 114 | if (ret == (int) phy_size) 115 | { 116 | ret = fal_partition_write(part->fal_part, phy_pos, buffer, phy_size); 117 | } 118 | 119 | if (ret != (int) phy_size) 120 | { 121 | ret = 0; 122 | } 123 | else 124 | { 125 | ret = size; 126 | } 127 | 128 | return ret; 129 | } 130 | 131 | #ifdef RT_USING_DEVICE_OPS 132 | const static struct rt_device_ops blk_dev_ops = 133 | { 134 | RT_NULL, 135 | RT_NULL, 136 | RT_NULL, 137 | blk_dev_read, 138 | blk_dev_write, 139 | blk_dev_control 140 | }; 141 | #endif 142 | 143 | /** 144 | * create RT-Thread block device by specified partition 145 | * 146 | * @param parition_name partition name 147 | * 148 | * @return != NULL: created block device 149 | * NULL: created failed 150 | */ 151 | struct rt_device *fal_blk_device_create(const char *parition_name) 152 | { 153 | struct fal_blk_device *blk_dev; 154 | const struct fal_partition *fal_part = fal_partition_find(parition_name); 155 | const struct fal_flash_dev *fal_flash = NULL; 156 | 157 | if (!fal_part) 158 | { 159 | log_e("Error: the partition name (%s) is not found.", parition_name); 160 | return NULL; 161 | } 162 | 163 | if ((fal_flash = fal_flash_device_find(fal_part->flash_name)) == NULL) 164 | { 165 | log_e("Error: the flash device name (%s) is not found.", fal_part->flash_name); 166 | return NULL; 167 | } 168 | 169 | blk_dev = (struct fal_blk_device*) rt_malloc(sizeof(struct fal_blk_device)); 170 | if (blk_dev) 171 | { 172 | blk_dev->fal_part = fal_part; 173 | blk_dev->geometry.bytes_per_sector = fal_flash->blk_size; 174 | blk_dev->geometry.block_size = fal_flash->blk_size; 175 | blk_dev->geometry.sector_count = fal_part->len / fal_flash->blk_size; 176 | 177 | /* register device */ 178 | blk_dev->parent.type = RT_Device_Class_Block; 179 | 180 | #ifdef RT_USING_DEVICE_OPS 181 | blk_dev->parent.ops = &blk_dev_ops; 182 | #else 183 | blk_dev->parent.init = NULL; 184 | blk_dev->parent.open = NULL; 185 | blk_dev->parent.close = NULL; 186 | blk_dev->parent.read = blk_dev_read; 187 | blk_dev->parent.write = blk_dev_write; 188 | blk_dev->parent.control = blk_dev_control; 189 | #endif 190 | 191 | /* no private */ 192 | blk_dev->parent.user_data = RT_NULL; 193 | 194 | log_i("The FAL block device (%s) created successfully", fal_part->name); 195 | rt_device_register(RT_DEVICE(blk_dev), fal_part->name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE); 196 | } 197 | else 198 | { 199 | log_e("Error: no memory for create FAL block device"); 200 | } 201 | 202 | return RT_DEVICE(blk_dev); 203 | } 204 | 205 | /* ========================== MTD nor device ======================== */ 206 | #if defined(RT_USING_MTD_NOR) 207 | 208 | struct fal_mtd_nor_device 209 | { 210 | struct rt_mtd_nor_device parent; 211 | const struct fal_partition *fal_part; 212 | }; 213 | 214 | static rt_size_t mtd_nor_dev_read(struct rt_mtd_nor_device* device, rt_off_t offset, rt_uint8_t* data, rt_uint32_t length) 215 | { 216 | int ret = 0; 217 | struct fal_mtd_nor_device *part = (struct fal_mtd_nor_device*) device; 218 | 219 | assert(part != RT_NULL); 220 | 221 | ret = fal_partition_read(part->fal_part, offset, data, length); 222 | 223 | if (ret != (int)length) 224 | { 225 | ret = 0; 226 | } 227 | else 228 | { 229 | ret = length; 230 | } 231 | 232 | return ret; 233 | } 234 | 235 | static rt_size_t mtd_nor_dev_write(struct rt_mtd_nor_device* device, rt_off_t offset, const rt_uint8_t* data, rt_uint32_t length) 236 | { 237 | int ret = 0; 238 | struct fal_mtd_nor_device *part; 239 | 240 | part = (struct fal_mtd_nor_device*) device; 241 | assert(part != RT_NULL); 242 | 243 | ret = fal_partition_write(part->fal_part, offset, data, length); 244 | 245 | if (ret != (int) length) 246 | { 247 | ret = 0; 248 | } 249 | else 250 | { 251 | ret = length; 252 | } 253 | 254 | return ret; 255 | } 256 | 257 | static rt_err_t mtd_nor_dev_erase(struct rt_mtd_nor_device* device, rt_off_t offset, rt_uint32_t length) 258 | { 259 | int ret = 0; 260 | struct fal_mtd_nor_device *part; 261 | 262 | part = (struct fal_mtd_nor_device*) device; 263 | assert(part != RT_NULL); 264 | 265 | ret = fal_partition_erase(part->fal_part, offset, length); 266 | 267 | if (ret != length) 268 | { 269 | return -RT_ERROR; 270 | } 271 | else 272 | { 273 | return RT_EOK; 274 | } 275 | } 276 | 277 | static const struct rt_mtd_nor_driver_ops _ops = 278 | { 279 | RT_NULL, 280 | mtd_nor_dev_read, 281 | mtd_nor_dev_write, 282 | mtd_nor_dev_erase, 283 | }; 284 | 285 | /** 286 | * create RT-Thread MTD NOR device by specified partition 287 | * 288 | * @param parition_name partition name 289 | * 290 | * @return != NULL: created MTD NOR device 291 | * NULL: created failed 292 | */ 293 | struct rt_device *fal_mtd_nor_device_create(const char *parition_name) 294 | { 295 | struct fal_mtd_nor_device *mtd_nor_dev; 296 | const struct fal_partition *fal_part = fal_partition_find(parition_name); 297 | const struct fal_flash_dev *fal_flash = NULL; 298 | 299 | if (!fal_part) 300 | { 301 | log_e("Error: the partition name (%s) is not found.", parition_name); 302 | return NULL; 303 | } 304 | 305 | if ((fal_flash = fal_flash_device_find(fal_part->flash_name)) == NULL) 306 | { 307 | log_e("Error: the flash device name (%s) is not found.", fal_part->flash_name); 308 | return NULL; 309 | } 310 | 311 | mtd_nor_dev = (struct fal_mtd_nor_device*) rt_malloc(sizeof(struct fal_mtd_nor_device)); 312 | if (mtd_nor_dev) 313 | { 314 | mtd_nor_dev->fal_part = fal_part; 315 | 316 | mtd_nor_dev->parent.block_start = 0; 317 | mtd_nor_dev->parent.block_end = fal_part->len / fal_flash->blk_size; 318 | mtd_nor_dev->parent.block_size = fal_flash->blk_size; 319 | 320 | /* set ops */ 321 | mtd_nor_dev->parent.ops = &_ops; 322 | 323 | log_i("The FAL MTD NOR device (%s) created successfully", fal_part->name); 324 | rt_mtd_nor_register_device(fal_part->name, &mtd_nor_dev->parent); 325 | } 326 | else 327 | { 328 | log_e("Error: no memory for create FAL MTD NOR device"); 329 | } 330 | 331 | return RT_DEVICE(&mtd_nor_dev->parent); 332 | } 333 | 334 | #endif /* defined(RT_USING_MTD_NOR) */ 335 | 336 | 337 | /* ========================== char device ======================== */ 338 | struct fal_char_device 339 | { 340 | struct rt_device parent; 341 | const struct fal_partition *fal_part; 342 | }; 343 | 344 | /* RT-Thread device interface */ 345 | static rt_size_t char_dev_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size) 346 | { 347 | int ret = 0; 348 | struct fal_char_device *part = (struct fal_char_device *) dev; 349 | 350 | assert(part != RT_NULL); 351 | 352 | if (pos + size > part->fal_part->len) 353 | size = part->fal_part->len - pos; 354 | 355 | ret = fal_partition_read(part->fal_part, pos, buffer, size); 356 | 357 | if (ret != (int)(size)) 358 | ret = 0; 359 | 360 | return ret; 361 | } 362 | 363 | static rt_size_t char_dev_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size) 364 | { 365 | int ret = 0; 366 | struct fal_char_device *part; 367 | 368 | part = (struct fal_char_device *) dev; 369 | assert(part != RT_NULL); 370 | 371 | if (pos == 0) 372 | { 373 | fal_partition_erase_all(part->fal_part); 374 | } 375 | else if (pos + size > part->fal_part->len) 376 | { 377 | size = part->fal_part->len - pos; 378 | } 379 | 380 | ret = fal_partition_write(part->fal_part, pos, buffer, size); 381 | 382 | if (ret != (int) size) 383 | ret = 0; 384 | 385 | return ret; 386 | } 387 | 388 | #ifdef RT_USING_DEVICE_OPS 389 | const static struct rt_device_ops char_dev_ops = 390 | { 391 | RT_NULL, 392 | RT_NULL, 393 | RT_NULL, 394 | char_dev_read, 395 | char_dev_write, 396 | RT_NULL 397 | }; 398 | #endif 399 | 400 | #ifdef RT_USING_POSIX 401 | #include 402 | 403 | /* RT-Thread device filesystem interface */ 404 | static int char_dev_fopen(struct dfs_fd *fd) 405 | { 406 | struct fal_char_device *part = (struct fal_char_device *) fd->data; 407 | 408 | assert(part != RT_NULL); 409 | 410 | switch (fd->flags & O_ACCMODE) 411 | { 412 | case O_RDONLY: 413 | break; 414 | case O_WRONLY: 415 | case O_RDWR: 416 | /* erase partition when device file open */ 417 | fal_partition_erase_all(part->fal_part); 418 | break; 419 | default: 420 | break; 421 | } 422 | fd->pos = 0; 423 | 424 | return RT_EOK; 425 | } 426 | 427 | static int char_dev_fread(struct dfs_fd *fd, void *buf, size_t count) 428 | { 429 | int ret = 0; 430 | struct fal_char_device *part = (struct fal_char_device *) fd->data; 431 | 432 | assert(part != RT_NULL); 433 | 434 | if (fd->pos + count > part->fal_part->len) 435 | count = part->fal_part->len - fd->pos; 436 | 437 | ret = fal_partition_read(part->fal_part, fd->pos, buf, count); 438 | 439 | if (ret != (int)(count)) 440 | return 0; 441 | 442 | fd->pos += ret; 443 | 444 | return ret; 445 | } 446 | 447 | static int char_dev_fwrite(struct dfs_fd *fd, const void *buf, size_t count) 448 | { 449 | int ret = 0; 450 | struct fal_char_device *part = (struct fal_char_device *) fd->data; 451 | 452 | assert(part != RT_NULL); 453 | 454 | if (fd->pos + count > part->fal_part->len) 455 | count = part->fal_part->len - fd->pos; 456 | 457 | ret = fal_partition_write(part->fal_part, fd->pos, buf, count); 458 | 459 | if (ret != (int) count) 460 | return 0; 461 | 462 | fd->pos += ret; 463 | 464 | return ret; 465 | } 466 | 467 | static const struct dfs_file_ops char_dev_fops = 468 | { 469 | char_dev_fopen, 470 | RT_NULL, 471 | RT_NULL, 472 | char_dev_fread, 473 | char_dev_fwrite, 474 | RT_NULL, /* flush */ 475 | RT_NULL, /* lseek */ 476 | RT_NULL, /* getdents */ 477 | RT_NULL, 478 | }; 479 | #endif /* defined(RT_USING_POSIX) */ 480 | 481 | /** 482 | * create RT-Thread char device by specified partition 483 | * 484 | * @param parition_name partition name 485 | * 486 | * @return != NULL: created char device 487 | * NULL: created failed 488 | */ 489 | struct rt_device *fal_char_device_create(const char *parition_name) 490 | { 491 | struct fal_char_device *char_dev; 492 | const struct fal_partition *fal_part = fal_partition_find(parition_name); 493 | 494 | if (!fal_part) 495 | { 496 | log_e("Error: the partition name (%s) is not found.", parition_name); 497 | return NULL; 498 | } 499 | 500 | if ((fal_flash_device_find(fal_part->flash_name)) == NULL) 501 | { 502 | log_e("Error: the flash device name (%s) is not found.", fal_part->flash_name); 503 | return NULL; 504 | } 505 | 506 | char_dev = (struct fal_char_device *) rt_malloc(sizeof(struct fal_char_device)); 507 | if (char_dev) 508 | { 509 | char_dev->fal_part = fal_part; 510 | 511 | /* register device */ 512 | char_dev->parent.type = RT_Device_Class_Char; 513 | 514 | #ifdef RT_USING_DEVICE_OPS 515 | char_dev->parent.ops = &char_dev_ops; 516 | #else 517 | char_dev->parent.init = NULL; 518 | char_dev->parent.open = NULL; 519 | char_dev->parent.close = NULL; 520 | char_dev->parent.read = char_dev_read; 521 | char_dev->parent.write = char_dev_write; 522 | char_dev->parent.control = NULL; 523 | /* no private */ 524 | char_dev->parent.user_data = NULL; 525 | #endif 526 | 527 | rt_device_register(RT_DEVICE(char_dev), fal_part->name, RT_DEVICE_FLAG_RDWR); 528 | log_i("The FAL char device (%s) created successfully", fal_part->name); 529 | 530 | #ifdef RT_USING_POSIX 531 | /* set fops */ 532 | char_dev->parent.fops = &char_dev_fops; 533 | #endif 534 | 535 | } 536 | else 537 | { 538 | log_e("Error: no memory for create FAL char device"); 539 | } 540 | 541 | return RT_DEVICE(char_dev); 542 | } 543 | 544 | #if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) 545 | 546 | #include 547 | extern int fal_init_check(void); 548 | 549 | static void fal(uint8_t argc, char **argv) { 550 | 551 | #define __is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ') 552 | #define HEXDUMP_WIDTH 16 553 | #define CMD_PROBE_INDEX 0 554 | #define CMD_READ_INDEX 1 555 | #define CMD_WRITE_INDEX 2 556 | #define CMD_ERASE_INDEX 3 557 | #define CMD_BENCH_INDEX 4 558 | 559 | int result; 560 | static const struct fal_flash_dev *flash_dev = NULL; 561 | static const struct fal_partition *part_dev = NULL; 562 | size_t i = 0, j = 0; 563 | 564 | const char* help_info[] = 565 | { 566 | [CMD_PROBE_INDEX] = "fal probe [dev_name|part_name] - probe flash device or partition by given name", 567 | [CMD_READ_INDEX] = "fal read addr size - read 'size' bytes starting at 'addr'", 568 | [CMD_WRITE_INDEX] = "fal write addr data1 ... dataN - write some bytes 'data' starting at 'addr'", 569 | [CMD_ERASE_INDEX] = "fal erase addr size - erase 'size' bytes starting at 'addr'", 570 | [CMD_BENCH_INDEX] = "fal bench - benchmark test with per block size", 571 | }; 572 | 573 | if (fal_init_check() != 1) 574 | { 575 | rt_kprintf("\n[Warning] FAL is not initialized or failed to initialize!\n\n"); 576 | return; 577 | } 578 | 579 | if (argc < 2) 580 | { 581 | rt_kprintf("Usage:\n"); 582 | for (i = 0; i < sizeof(help_info) / sizeof(char*); i++) 583 | { 584 | rt_kprintf("%s\n", help_info[i]); 585 | } 586 | rt_kprintf("\n"); 587 | } 588 | else 589 | { 590 | const char *operator = argv[1]; 591 | uint32_t addr, size; 592 | 593 | if (!strcmp(operator, "probe")) 594 | { 595 | if (argc >= 3) 596 | { 597 | char *dev_name = argv[2]; 598 | if ((flash_dev = fal_flash_device_find(dev_name)) != NULL) 599 | { 600 | part_dev = NULL; 601 | } 602 | else if ((part_dev = fal_partition_find(dev_name)) != NULL) 603 | { 604 | flash_dev = NULL; 605 | } 606 | else 607 | { 608 | rt_kprintf("Device %s NOT found. Probe failed.\n", dev_name); 609 | flash_dev = NULL; 610 | part_dev = NULL; 611 | } 612 | } 613 | 614 | if (flash_dev) 615 | { 616 | rt_kprintf("Probed a flash device | %s | addr: %ld | len: %d |.\n", flash_dev->name, 617 | flash_dev->addr, flash_dev->len); 618 | } 619 | else if (part_dev) 620 | { 621 | rt_kprintf("Probed a flash partition | %s | flash_dev: %s | offset: %ld | len: %d |.\n", 622 | part_dev->name, part_dev->flash_name, part_dev->offset, part_dev->len); 623 | } 624 | else 625 | { 626 | rt_kprintf("No flash device or partition was probed.\n"); 627 | rt_kprintf("Usage: %s.\n", help_info[CMD_PROBE_INDEX]); 628 | fal_show_part_table(); 629 | } 630 | } 631 | else 632 | { 633 | if (!flash_dev && !part_dev) 634 | { 635 | rt_kprintf("No flash device or partition was probed. Please run 'fal probe'.\n"); 636 | return; 637 | } 638 | if (!rt_strcmp(operator, "read")) 639 | { 640 | if (argc < 4) 641 | { 642 | rt_kprintf("Usage: %s.\n", help_info[CMD_READ_INDEX]); 643 | return; 644 | } 645 | else 646 | { 647 | addr = strtol(argv[2], NULL, 0); 648 | size = strtol(argv[3], NULL, 0); 649 | uint8_t *data = rt_malloc(size); 650 | if (data) 651 | { 652 | if (flash_dev) 653 | { 654 | result = flash_dev->ops.read(addr, data, size); 655 | } 656 | else if (part_dev) 657 | { 658 | result = fal_partition_read(part_dev, addr, data, size); 659 | } 660 | if (result >= 0) 661 | { 662 | rt_kprintf("Read data success. Start from 0x%08X, size is %ld. The data is:\n", addr, 663 | size); 664 | rt_kprintf("Offset (h) 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n"); 665 | for (i = 0; i < size; i += HEXDUMP_WIDTH) 666 | { 667 | rt_kprintf("[%08X] ", addr + i); 668 | /* dump hex */ 669 | for (j = 0; j < HEXDUMP_WIDTH; j++) 670 | { 671 | if (i + j < size) 672 | { 673 | rt_kprintf("%02X ", data[i + j]); 674 | } 675 | else 676 | { 677 | rt_kprintf(" "); 678 | } 679 | } 680 | /* dump char for hex */ 681 | for (j = 0; j < HEXDUMP_WIDTH; j++) 682 | { 683 | if (i + j < size) 684 | { 685 | rt_kprintf("%c", __is_print(data[i + j]) ? data[i + j] : '.'); 686 | } 687 | } 688 | rt_kprintf("\n"); 689 | } 690 | rt_kprintf("\n"); 691 | } 692 | rt_free(data); 693 | } 694 | else 695 | { 696 | rt_kprintf("Low memory!\n"); 697 | } 698 | } 699 | } 700 | else if (!strcmp(operator, "write")) 701 | { 702 | if (argc < 4) 703 | { 704 | rt_kprintf("Usage: %s.\n", help_info[CMD_WRITE_INDEX]); 705 | return; 706 | } 707 | else 708 | { 709 | addr = strtol(argv[2], NULL, 0); 710 | size = argc - 3; 711 | uint8_t *data = rt_malloc(size); 712 | if (data) 713 | { 714 | for (i = 0; i < size; i++) 715 | { 716 | data[i] = strtol(argv[3 + i], NULL, 0); 717 | } 718 | if (flash_dev) 719 | { 720 | result = flash_dev->ops.write(addr, data, size); 721 | } 722 | else if (part_dev) 723 | { 724 | result = fal_partition_write(part_dev, addr, data, size); 725 | } 726 | if (result >= 0) 727 | { 728 | rt_kprintf("Write data success. Start from 0x%08X, size is %ld.\n", addr, size); 729 | rt_kprintf("Write data: "); 730 | for (i = 0; i < size; i++) 731 | { 732 | rt_kprintf("%d ", data[i]); 733 | } 734 | rt_kprintf(".\n"); 735 | } 736 | rt_free(data); 737 | } 738 | else 739 | { 740 | rt_kprintf("Low memory!\n"); 741 | } 742 | } 743 | } 744 | else if (!rt_strcmp(operator, "erase")) 745 | { 746 | if (argc < 4) 747 | { 748 | rt_kprintf("Usage: %s.\n", help_info[CMD_ERASE_INDEX]); 749 | return; 750 | } 751 | else 752 | { 753 | addr = strtol(argv[2], NULL, 0); 754 | size = strtol(argv[3], NULL, 0); 755 | if (flash_dev) 756 | { 757 | result = flash_dev->ops.erase(addr, size); 758 | } 759 | else if (part_dev) 760 | { 761 | result = fal_partition_erase(part_dev, addr, size); 762 | } 763 | if (result >= 0) 764 | { 765 | rt_kprintf("Erase data success. Start from 0x%08X, size is %ld.\n", addr, size); 766 | } 767 | } 768 | } 769 | else if (!strcmp(operator, "bench")) 770 | { 771 | if (argc < 3) 772 | { 773 | rt_kprintf("Usage: %s.\n", help_info[CMD_BENCH_INDEX]); 774 | return; 775 | } 776 | else if ((argc > 3 && strcmp(argv[3], "yes")) || argc < 4) 777 | { 778 | rt_kprintf("DANGER: It will erase full chip or partition! Please run 'fal bench %d yes'.\n", strtol(argv[2], NULL, 0)); 779 | return; 780 | } 781 | /* full chip benchmark test */ 782 | uint32_t start_time, time_cast; 783 | size_t write_size = strtol(argv[2], NULL, 0), read_size = strtol(argv[2], NULL, 0), cur_op_size; 784 | uint8_t *write_data = (uint8_t *)rt_malloc(write_size), *read_data = (uint8_t *)rt_malloc(read_size); 785 | 786 | if (write_data && read_data) 787 | { 788 | for (i = 0; i < write_size; i ++) { 789 | write_data[i] = i & 0xFF; 790 | } 791 | if (flash_dev) 792 | { 793 | size = flash_dev->len; 794 | } 795 | else if (part_dev) 796 | { 797 | size = part_dev->len; 798 | } 799 | /* benchmark testing */ 800 | rt_kprintf("Erasing %ld bytes data, waiting...\n", size); 801 | start_time = rt_tick_get(); 802 | if (flash_dev) 803 | { 804 | result = flash_dev->ops.erase(0, size); 805 | } 806 | else if (part_dev) 807 | { 808 | result = fal_partition_erase(part_dev, 0, size); 809 | } 810 | if (result >= 0) 811 | { 812 | time_cast = rt_tick_get() - start_time; 813 | rt_kprintf("Erase benchmark success, total time: %d.%03dS.\n", time_cast / RT_TICK_PER_SECOND, 814 | time_cast % RT_TICK_PER_SECOND / ((RT_TICK_PER_SECOND * 1 + 999) / 1000)); 815 | } 816 | else 817 | { 818 | rt_kprintf("Erase benchmark has an error. Error code: %d.\n", result); 819 | } 820 | /* write test */ 821 | rt_kprintf("Writing %ld bytes data, waiting...\n", size); 822 | start_time = rt_tick_get(); 823 | for (i = 0; i < size; i += write_size) 824 | { 825 | if (i + write_size <= size) 826 | { 827 | cur_op_size = write_size; 828 | } 829 | else 830 | { 831 | cur_op_size = size - i; 832 | } 833 | if (flash_dev) 834 | { 835 | result = flash_dev->ops.write(i, write_data, cur_op_size); 836 | } 837 | else if (part_dev) 838 | { 839 | result = fal_partition_write(part_dev, i, write_data, cur_op_size); 840 | } 841 | if (result < 0) 842 | { 843 | break; 844 | } 845 | } 846 | if (result >= 0) 847 | { 848 | time_cast = rt_tick_get() - start_time; 849 | rt_kprintf("Write benchmark success, total time: %d.%03dS.\n", time_cast / RT_TICK_PER_SECOND, 850 | time_cast % RT_TICK_PER_SECOND / ((RT_TICK_PER_SECOND * 1 + 999) / 1000)); 851 | } 852 | else 853 | { 854 | rt_kprintf("Write benchmark has an error. Error code: %d.\n", result); 855 | } 856 | /* read test */ 857 | rt_kprintf("Reading %ld bytes data, waiting...\n", size); 858 | start_time = rt_tick_get(); 859 | for (i = 0; i < size; i += read_size) 860 | { 861 | if (i + read_size <= size) 862 | { 863 | cur_op_size = read_size; 864 | } 865 | else 866 | { 867 | cur_op_size = size - i; 868 | } 869 | if (flash_dev) 870 | { 871 | result = flash_dev->ops.read(i, read_data, cur_op_size); 872 | } 873 | else if (part_dev) 874 | { 875 | result = fal_partition_read(part_dev, i, read_data, cur_op_size); 876 | } 877 | /* data check */ 878 | for (int index = 0; index < cur_op_size; index ++) 879 | { 880 | if (write_data[index] != read_data[index]) 881 | { 882 | rt_kprintf("%d %d %02x %02x.\n", i, index, write_data[index], read_data[index]); 883 | } 884 | } 885 | 886 | if (memcmp(write_data, read_data, cur_op_size)) 887 | { 888 | result = -RT_ERROR; 889 | rt_kprintf("Data check ERROR! Please check you flash by other command.\n"); 890 | } 891 | /* has an error */ 892 | if (result < 0) 893 | { 894 | break; 895 | } 896 | } 897 | if (result >= 0) 898 | { 899 | time_cast = rt_tick_get() - start_time; 900 | rt_kprintf("Read benchmark success, total time: %d.%03dS.\n", time_cast / RT_TICK_PER_SECOND, 901 | time_cast % RT_TICK_PER_SECOND / ((RT_TICK_PER_SECOND * 1 + 999) / 1000)); 902 | } 903 | else 904 | { 905 | rt_kprintf("Read benchmark has an error. Error code: %d.\n", result); 906 | } 907 | } 908 | else 909 | { 910 | rt_kprintf("Low memory!\n"); 911 | } 912 | rt_free(write_data); 913 | rt_free(read_data); 914 | } 915 | else 916 | { 917 | rt_kprintf("Usage:\n"); 918 | for (i = 0; i < sizeof(help_info) / sizeof(char*); i++) 919 | { 920 | rt_kprintf("%s\n", help_info[i]); 921 | } 922 | rt_kprintf("\n"); 923 | return; 924 | } 925 | if (result < 0) { 926 | rt_kprintf("This operate has an error. Error code: %d.\n", result); 927 | } 928 | } 929 | } 930 | } 931 | MSH_CMD_EXPORT(fal, FAL (Flash Abstraction Layer) operate.); 932 | 933 | #endif /* defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) */ 934 | #endif /* RT_VER_NUM */ 935 | --------------------------------------------------------------------------------