Просмотр исходного кода

add:little_flash for suport nand flash

Dozingfiretruck 1 год назад
Родитель
Сommit
a583a150fa

+ 201 - 0
components/little_flash/LICENSE

@@ -0,0 +1,201 @@
+                                 Apache License
+                           Version 2.0, January 2004
+                        http://www.apache.org/licenses/
+
+   TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+   1. Definitions.
+
+      "License" shall mean the terms and conditions for use, reproduction,
+      and distribution as defined by Sections 1 through 9 of this document.
+
+      "Licensor" shall mean the copyright owner or entity authorized by
+      the copyright owner that is granting the License.
+
+      "Legal Entity" shall mean the union of the acting entity and all
+      other entities that control, are controlled by, or are under common
+      control with that entity. For the purposes of this definition,
+      "control" means (i) the power, direct or indirect, to cause the
+      direction or management of such entity, whether by contract or
+      otherwise, or (ii) ownership of fifty percent (50%) or more of the
+      outstanding shares, or (iii) beneficial ownership of such entity.
+
+      "You" (or "Your") shall mean an individual or Legal Entity
+      exercising permissions granted by this License.
+
+      "Source" form shall mean the preferred form for making modifications,
+      including but not limited to software source code, documentation
+      source, and configuration files.
+
+      "Object" form shall mean any form resulting from mechanical
+      transformation or translation of a Source form, including but
+      not limited to compiled object code, generated documentation,
+      and conversions to other media types.
+
+      "Work" shall mean the work of authorship, whether in Source or
+      Object form, made available under the License, as indicated by a
+      copyright notice that is included in or attached to the work
+      (an example is provided in the Appendix below).
+
+      "Derivative Works" shall mean any work, whether in Source or Object
+      form, that is based on (or derived from) the Work and for which the
+      editorial revisions, annotations, elaborations, or other modifications
+      represent, as a whole, an original work of authorship. For the purposes
+      of this License, Derivative Works shall not include works that remain
+      separable from, or merely link (or bind by name) to the interfaces of,
+      the Work and Derivative Works thereof.
+
+      "Contribution" shall mean any work of authorship, including
+      the original version of the Work and any modifications or additions
+      to that Work or Derivative Works thereof, that is intentionally
+      submitted to Licensor for inclusion in the Work by the copyright owner
+      or by an individual or Legal Entity authorized to submit on behalf of
+      the copyright owner. For the purposes of this definition, "submitted"
+      means any form of electronic, verbal, or written communication sent
+      to the Licensor or its representatives, including but not limited to
+      communication on electronic mailing lists, source code control systems,
+      and issue tracking systems that are managed by, or on behalf of, the
+      Licensor for the purpose of discussing and improving the Work, but
+      excluding communication that is conspicuously marked or otherwise
+      designated in writing by the copyright owner as "Not a Contribution."
+
+      "Contributor" shall mean Licensor and any individual or Legal Entity
+      on behalf of whom a Contribution has been received by Licensor and
+      subsequently incorporated within the Work.
+
+   2. Grant of Copyright License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      copyright license to reproduce, prepare Derivative Works of,
+      publicly display, publicly perform, sublicense, and distribute the
+      Work and such Derivative Works in Source or Object form.
+
+   3. Grant of Patent License. Subject to the terms and conditions of
+      this License, each Contributor hereby grants to You a perpetual,
+      worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+      (except as stated in this section) patent license to make, have made,
+      use, offer to sell, sell, import, and otherwise transfer the Work,
+      where such license applies only to those patent claims licensable
+      by such Contributor that are necessarily infringed by their
+      Contribution(s) alone or by combination of their Contribution(s)
+      with the Work to which such Contribution(s) was submitted. If You
+      institute patent litigation against any entity (including a
+      cross-claim or counterclaim in a lawsuit) alleging that the Work
+      or a Contribution incorporated within the Work constitutes direct
+      or contributory patent infringement, then any patent licenses
+      granted to You under this License for that Work shall terminate
+      as of the date such litigation is filed.
+
+   4. Redistribution. You may reproduce and distribute copies of the
+      Work or Derivative Works thereof in any medium, with or without
+      modifications, and in Source or Object form, provided that You
+      meet the following conditions:
+
+      (a) You must give any other recipients of the Work or
+          Derivative Works a copy of this License; and
+
+      (b) You must cause any modified files to carry prominent notices
+          stating that You changed the files; and
+
+      (c) You must retain, in the Source form of any Derivative Works
+          that You distribute, all copyright, patent, trademark, and
+          attribution notices from the Source form of the Work,
+          excluding those notices that do not pertain to any part of
+          the Derivative Works; and
+
+      (d) If the Work includes a "NOTICE" text file as part of its
+          distribution, then any Derivative Works that You distribute must
+          include a readable copy of the attribution notices contained
+          within such NOTICE file, excluding those notices that do not
+          pertain to any part of the Derivative Works, in at least one
+          of the following places: within a NOTICE text file distributed
+          as part of the Derivative Works; within the Source form or
+          documentation, if provided along with the Derivative Works; or,
+          within a display generated by the Derivative Works, if and
+          wherever such third-party notices normally appear. The contents
+          of the NOTICE file are for informational purposes only and
+          do not modify the License. You may add Your own attribution
+          notices within Derivative Works that You distribute, alongside
+          or as an addendum to the NOTICE text from the Work, provided
+          that such additional attribution notices cannot be construed
+          as modifying the License.
+
+      You may add Your own copyright statement to Your modifications and
+      may provide additional or different license terms and conditions
+      for use, reproduction, or distribution of Your modifications, or
+      for any such Derivative Works as a whole, provided Your use,
+      reproduction, and distribution of the Work otherwise complies with
+      the conditions stated in this License.
+
+   5. Submission of Contributions. Unless You explicitly state otherwise,
+      any Contribution intentionally submitted for inclusion in the Work
+      by You to the Licensor shall be under the terms and conditions of
+      this License, without any additional terms or conditions.
+      Notwithstanding the above, nothing herein shall supersede or modify
+      the terms of any separate license agreement you may have executed
+      with Licensor regarding such Contributions.
+
+   6. Trademarks. This License does not grant permission to use the trade
+      names, trademarks, service marks, or product names of the Licensor,
+      except as required for reasonable and customary use in describing the
+      origin of the Work and reproducing the content of the NOTICE file.
+
+   7. Disclaimer of Warranty. Unless required by applicable law or
+      agreed to in writing, Licensor provides the Work (and each
+      Contributor provides its Contributions) on an "AS IS" BASIS,
+      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+      implied, including, without limitation, any warranties or conditions
+      of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+      PARTICULAR PURPOSE. You are solely responsible for determining the
+      appropriateness of using or redistributing the Work and assume any
+      risks associated with Your exercise of permissions under this License.
+
+   8. Limitation of Liability. In no event and under no legal theory,
+      whether in tort (including negligence), contract, or otherwise,
+      unless required by applicable law (such as deliberate and grossly
+      negligent acts) or agreed to in writing, shall any Contributor be
+      liable to You for damages, including any direct, indirect, special,
+      incidental, or consequential damages of any character arising as a
+      result of this License or out of the use or inability to use the
+      Work (including but not limited to damages for loss of goodwill,
+      work stoppage, computer failure or malfunction, or any and all
+      other commercial damages or losses), even if such Contributor
+      has been advised of the possibility of such damages.
+
+   9. Accepting Warranty or Additional Liability. While redistributing
+      the Work or Derivative Works thereof, You may choose to offer,
+      and charge a fee for, acceptance of support, warranty, indemnity,
+      or other liability obligations and/or rights consistent with this
+      License. However, in accepting such obligations, You may act only
+      on Your own behalf and on Your sole responsibility, not on behalf
+      of any other Contributor, and only if You agree to indemnify,
+      defend, and hold each Contributor harmless for any liability
+      incurred by, or claims asserted against, such Contributor by reason
+      of your accepting any such warranty or additional liability.
+
+   END OF TERMS AND CONDITIONS
+
+   APPENDIX: How to apply the Apache License to your work.
+
+      To apply the Apache License to your work, attach the following
+      boilerplate notice, with the fields enclosed by brackets "[]"
+      replaced with your own identifying information. (Don't include
+      the brackets!)  The text should be enclosed in the appropriate
+      comment syntax for the file format. We also recommend that a
+      file or class name and description of purpose be included on the
+      same "printed page" as the copyright notice for easier
+      identification within third-party archives.
+
+   Copyright [yyyy] [name of copyright owner]
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.

+ 51 - 0
components/little_flash/inc/little_flash.h

@@ -0,0 +1,51 @@
+#ifndef _LITTLE_FLASH_H_
+#define _LITTLE_FLASH_H_
+
+#include "little_flash_define.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*
+    所有nand flash均使用BUF1模式驱动
+    nand flash无统一标准与命令,移植要注意状态寄存器和命令寄存器
+*/
+
+
+lf_err_t little_flash_init(void);
+
+lf_err_t little_flash_deinit(void);
+
+lf_err_t little_flash_device_init(little_flash_t *lf);
+
+lf_err_t little_flash_device_deinit(little_flash_t *lf);
+
+lf_err_t little_flash_erase(const little_flash_t *lf, uint32_t addr, uint32_t len);
+
+lf_err_t little_flash_chip_erase(const little_flash_t *lf);
+
+lf_err_t little_flash_write(const little_flash_t *lf, uint32_t addr, const uint8_t *data, uint32_t len);
+
+lf_err_t little_flash_read(const little_flash_t *lf, uint32_t addr, uint8_t *data, uint32_t len);
+
+lf_err_t little_flash_write_status(const little_flash_t *lf, uint8_t address, uint8_t status);
+
+lf_err_t little_flash_read_status(const little_flash_t *lf, uint8_t address, uint8_t *status);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _LITTLE_FLASH_H_ */
+
+
+
+
+
+
+
+
+
+

+ 185 - 0
components/little_flash/inc/little_flash_define.h

@@ -0,0 +1,185 @@
+#ifndef _LITTLE_FLASH_DEFINE_H_
+#define _LITTLE_FLASH_DEFINE_H_
+
+#include <stdint.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "little_flash_config.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define LF_SW_VERSION                             "0.0.1"
+
+struct little_flash;
+typedef struct little_flash little_flash_t;
+
+#ifndef LF_PRINTF
+#define LF_PRINTF printf
+#endif
+
+#ifndef LF_INFO
+#define LF_INFO(...)  LF_PRINTF(__VA_ARGS__)
+#endif
+
+#ifndef LF_ERROR
+#define LF_ERROR(...)  LF_PRINTF(__VA_ARGS__)
+#endif
+
+/* assert for developer. */
+#ifdef LF_DEBUG_MODE
+#ifndef LF_DEBUG
+#define LF_DEBUG(...)  LF_PRINTF(__VA_ARGS__)
+#endif
+#define LF_ASSERT(EXPR)                                                      \
+if (!(EXPR))                                                                   \
+{                                                                              \
+    LF_PRINTF("(%s) has assert failed at %s.", #EXPR, __FUNCTION__);          \
+    while (1);                                                                 \
+}
+#else
+#ifndef LF_DEBUG
+#define LF_DEBUG(...)
+#endif
+#define LF_ASSERT(EXPR)
+#endif
+
+#ifndef LF_NULL
+#define LF_NULL NULL
+#endif
+
+#define LF_ENABLE       1
+#define LF_DISABLE      0
+
+/**
+ * status register bits
+ */
+enum {
+    LF_STATUS_REGISTER_BUSY = (1 << 0),                  /**< busing */
+    LF_STATUS_REGISTER_WEL = (1 << 1),                   /**< write enable latch */
+};
+
+/**
+ * @brief flash type
+ * 
+ */
+#define LF_DRIVER_NOR_FLASH     (0)
+#define LF_DRIVER_NAND_FLASH    (1)
+
+typedef enum {
+    LF_ERR_OK = 0,
+    LF_ERR_NO_FLASH,
+    LF_ERR_TIMEOUT,
+    LF_ERR_TRANSFER,
+    LF_ERR_ERASE,
+    LF_ERR_WRITE,
+    LF_ERR_READ,
+    LF_ERR_BUSY,
+    LF_ERR_BAD_ADDRESS,
+    LF_ERR_NO_MEM,
+}lf_err_t;
+
+#ifndef LF_FLASH_NAME_LEN
+#define LF_FLASH_NAME_LEN 16
+#endif
+
+typedef struct {
+    char name[LF_FLASH_NAME_LEN];                /**< flash chip name */
+    uint8_t manufacturer_id;                     /**< MANUFACTURER ID */
+    uint16_t device_id;                          /**< DEVICE ID (1byte or 2bytes) */
+    union{                                       /**< driver type */
+        struct {
+            uint16_t type : 2;                   /**< flash type */
+            uint16_t :14;                        /**< reserved */
+        };
+        uint16_t driver_type;                          
+    };
+    uint32_t capacity;                           /**< flash capacity (bytes) */
+    uint8_t addr_bytes;                          /**< address bytes 2 3 4*/
+    uint8_t erase_cmd;                           /**< erase granularity size block command */
+    uint32_t erase_size;                         /**< erase granularity (bytes) */
+    /* 以下基本可以代码自动推断无需指定 */
+    uint32_t prog_size;                          /**< page size (bytes) */
+    uint32_t read_size;                          /**< read size (bytes) */
+    uint32_t retry_times;                        /**< retry times */
+} little_flash_chipinfo_t;
+
+typedef struct{
+    lf_err_t (*transfer)(little_flash_t *lf,uint8_t *tx_buf, uint32_t tx_len, uint8_t *rx_buf, uint32_t rx_len);
+    void* user_data;
+} little_flash_spi_t;
+
+struct little_flash{
+    little_flash_chipinfo_t chip_info;
+    little_flash_spi_t spi;
+    /* lock */
+    void (*lock)(little_flash_t *lf);
+    /* unlock */
+    void (*unlock)(little_flash_t *lf);
+    /* wait 10us */
+    void (*wait_10us)(void);
+    /* wait ms */
+    void (*wait_ms)(uint32_t ms);
+#ifdef LF_USE_HEAP
+    /* malloc */
+    void* (*malloc)(size_t size);
+    /* free */
+    void (*free)(void* ptr);
+#endif /* LF_USE_HEAP */
+    /* user data */
+    void* user_data;
+};
+
+#define LF_RETRY_TIMES                              (500000)
+
+#define LF_NANDFLASH_PAGE_ZISE                      (2048)     /**< NAND flash page size (bytes) */
+
+#define LF_CMD_WRITE_STATUS_REGISTER                (0x01)
+
+#define LF_CMD_WRITE_DISABLE                        (0x04)
+
+#define LF_CMD_READ_STATUS_REGISTER                 (0x05)
+
+#define LF_CMD_WRITE_ENABLE                         (0x06)
+
+#define LF_CMD_JEDEC_ID                             (0x9F)
+
+#define LF_CMD_ENABLE_RESET                         (0x66)
+
+#define LF_CMD_NORFLASH_RESET                       (0x99)
+
+#define LF_CMD_BLOCK_ERASE                          (0xD8)
+
+#define LF_CMD_NANDFLASH_RESET                      (0xFF)
+
+#define LF_CMD_PROG_DATA                            (0x02)
+#define LF_CMD_READ_DATA                            (0x03)
+
+#define LF_NANDFLASH_PAGE_DATA_READ                 (0x13)
+#define LF_NANDFLASH_PAGE_PROG_EXEC                 (0x10)
+
+
+/* NAND flash status registers */
+#define LF_NANDFLASH_STATUS_REGISTER1               (0xA0)
+#define LF_NANDFLASH_STATUS_REGISTER2               (0xB0)
+#define LF_NANDFLASH_STATUS_REGISTER3               (0xC0)
+#define LF_NANDFLASH_STATUS_REGISTER4               (0xD0)
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _LITTLE_FLASH_DEFINE_H_ */
+
+
+
+
+
+
+
+
+
+

+ 38 - 0
components/little_flash/inc/little_flash_table.h

@@ -0,0 +1,38 @@
+#ifndef _LITTLE_FLASH_TABLE_H_
+#define _LITTLE_FLASH_TABLE_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Flash MANUFACTURER ID */
+#define LF_MF_ID_WINBOND                             0xEF
+
+
+
+/* 
+    待考究是否需要 地址位数 页大小等参数
+    Flash table 
+    name | manufacturer ID |device ID | falsh type | capacity | addr bytes | erase cmd | erase size
+*/
+
+#define LITTLE_FLASH_CHIP_TABLE                                                                                 \
+{                                                                                                               \
+    {"W25N01GVZEIG", LF_MF_ID_WINBOND, 0xAA21, LF_DRIVER_NAND_FLASH, 128L*1024L*1024L, 2, 0xD8, 128L*1024L},    \
+}
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _LITTLE_FLASH_TABLE_H_ */
+
+
+
+
+
+
+
+
+
+

+ 47 - 0
components/little_flash/port/little_flash_config.h

@@ -0,0 +1,47 @@
+#ifndef _LITTLE_FLASH_CONFIG_H_
+#define _LITTLE_FLASH_CONFIG_H_
+
+#include "luat_debug.h"
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* define the printf function for little flash */
+#define LF_PRINTF LUAT_DEBUG_PRINT
+
+#define LF_DEBUG_MODE                   /* enable debug mode for little flash */
+
+#define LF_FLASH_NAME_LEN    16         /* the max length of flash name */
+
+#define LF_USE_HEAP                     /* enable malloc/free for little flash */
+
+#define LF_USE_SPI                      /* enable SPI for little flash */
+
+// #define LF_USE_QSPI                     /* enable QSPI for little flash */
+
+// #define LF_USE_SFDP                     /* enable SFDP driver for little flash */
+
+#define LF_USE_LOCAL_TABLE              /* enable local table driver for little flash */
+
+// #define LF_USE_NOR                      /* enable NOR for little flash */
+
+#define LF_USE_NAND                     /* enable NAND for little flash */
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _LITTLE_FLASH_CONFIG_H_ */
+
+
+
+
+
+
+
+
+
+

+ 83 - 0
components/little_flash/port/little_flash_port.c

@@ -0,0 +1,83 @@
+
+#include "little_flash.h"
+
+#include "luat_rtos.h"
+#include "luat_mem.h"
+#include "luat_spi.h"
+
+#ifdef LF_USE_SPI
+static lf_err_t little_flash_spi_transfer(const little_flash_t *lf,uint8_t *tx_buf, uint32_t tx_len, uint8_t *rx_buf, uint32_t rx_len){
+    lf_err_t result = LF_ERR_OK;
+    luat_spi_device_t *spi_dev = (luat_spi_device_t*)lf->spi.user_data;
+    if (tx_len && rx_len) {
+        if (luat_spi_device_transfer(spi_dev , (const char*)tx_buf, tx_len, (char*)rx_buf, rx_len) <= 0) {
+            result = LF_ERR_TRANSFER;
+        }
+    } else if (tx_buf) {
+        if (luat_spi_device_send(spi_dev ,  (const char*)tx_buf, tx_len) <= 0) {
+            result = LF_ERR_WRITE;
+        }
+    } else {
+        if (luat_spi_device_recv(spi_dev , (char*)rx_buf, rx_len) <= 0) {
+            result = LF_ERR_READ;
+        }
+    }
+    return result;
+}
+#endif
+
+static void little_flash_wait_10us(void){
+    uint32_t delay = 12;
+    while(delay--);
+}
+
+static void little_flash_wait_ms(uint32_t ms){
+    luat_rtos_task_sleep(ms);
+}
+
+#ifdef LF_USE_HEAP
+static void* little_flash_malloc(size_t size){
+    return luat_heap_opt_malloc(LUAT_HEAP_SRAM,size);
+}
+
+static void little_flash_free(void* ptr){
+    luat_heap_opt_free(LUAT_HEAP_AUTO,ptr);
+}
+#endif
+
+lf_err_t little_flash_port_init(little_flash_t *lf){
+#ifdef LF_USE_SPI
+    lf->spi.transfer = little_flash_spi_transfer;
+#endif
+    lf->wait_10us = little_flash_wait_10us;
+    lf->wait_ms = little_flash_wait_ms;
+#ifdef LF_USE_HEAP
+    lf->malloc = little_flash_malloc;
+    lf->free = little_flash_free;
+#endif
+    return LF_ERR_OK;
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+

+ 385 - 0
components/little_flash/src/little_flash.c

@@ -0,0 +1,385 @@
+
+#include "little_flash.h"
+#include "little_flash_table.h"
+
+extern lf_err_t little_flash_port_init(little_flash_t *lf);
+
+static const little_flash_chipinfo_t little_flash_table[] = LITTLE_FLASH_CHIP_TABLE;
+
+lf_err_t little_flash_write_status(const little_flash_t *lf, uint8_t address, uint8_t status){
+    lf_err_t result = LF_ERR_OK;
+    uint8_t cmd_data[3]={0};
+    cmd_data[0]=LF_CMD_WRITE_STATUS_REGISTER;
+    if (address){
+        cmd_data[1]=address;
+        cmd_data[2]=status;
+    }else{
+        cmd_data[1]=status;
+    }
+    result |= lf->spi.transfer(lf,cmd_data, address?3:2,LF_NULL,0);
+    return result;
+}
+
+lf_err_t little_flash_read_status(const little_flash_t *lf, uint8_t address, uint8_t *status){
+    lf_err_t result = LF_ERR_OK;
+    uint8_t cmd_data[2]={0};
+    cmd_data[0]=LF_CMD_READ_STATUS_REGISTER;
+    if (address){
+        cmd_data[1]=address;
+    }
+    cmd_data[1]=address;
+    result |= lf->spi.transfer(lf,cmd_data, address?2:1,status,1);
+    return result;
+}
+
+
+static lf_err_t little_flash_wait_busy(const little_flash_t *lf) {
+    lf_err_t result = LF_ERR_OK;
+    size_t retry_times = lf->chip_info.retry_times;
+    uint8_t status;
+    while (true) {
+        result |= little_flash_read_status(lf, LF_NANDFLASH_STATUS_REGISTER3, &status);
+        // LF_DEBUG("status 0x%02x",status);
+        if (result==LF_ERR_OK && (status&LF_STATUS_REGISTER_BUSY)==0){
+            return LF_ERR_OK;
+        }
+        retry_times--;
+        if (retry_times == 0) {
+            LF_ERROR("Error: Wait busy timeout.");
+            return LF_ERR_TIMEOUT;
+        }else{
+            lf->wait_10us();
+        }
+    }
+    return result;
+}
+
+/* 
+    reset device
+*/
+static lf_err_t little_flash_reset(const little_flash_t *lf){
+    lf_err_t result = LF_ERR_OK;
+    result |= little_flash_wait_busy(lf);
+    // nand flash
+    result |= lf->spi.transfer(lf,(uint8_t[]){LF_CMD_NANDFLASH_RESET}, 1,LF_NULL,0);
+    lf->wait_ms(1);
+    result |= little_flash_wait_busy(lf);
+    if (result) return result;
+    // 以下需要根据型号进行适配
+    result |= little_flash_write_status(lf,LF_NANDFLASH_STATUS_REGISTER1,0x00);
+    // ECC-E = 1, BUF = 1
+    result |= little_flash_write_status(lf,LF_NANDFLASH_STATUS_REGISTER2,(1 << 4) | (1 << 3));
+
+    return result;
+}
+
+static lf_err_t little_flash_write_enabled(const little_flash_t *lf, uint8_t enable){
+    lf_err_t result = LF_ERR_OK;
+    uint8_t status;
+    // nand flash
+    lf->spi.transfer(lf,enable?(uint8_t[]){LF_CMD_WRITE_ENABLE}:(uint8_t[]){LF_CMD_WRITE_DISABLE}, 1,LF_NULL,0);
+
+    result |= little_flash_wait_busy(lf);
+    if (result) return result;
+    // nand flash
+    result |= little_flash_read_status(lf, LF_NANDFLASH_STATUS_REGISTER3, &status);
+
+    if (enable && (status & LF_STATUS_REGISTER_WEL) == 0) {
+        LF_ERROR("Error: Can't enable write status.");
+        result = LF_ERR_WRITE;
+    } else if (!enable && (status & LF_STATUS_REGISTER_WEL) != 0) {
+        LF_ERROR("Error: Can't disable write status.");
+        result = LF_ERR_WRITE;
+    }
+
+    return result;
+}
+
+lf_err_t little_flash_init(void){
+    LF_INFO("Welcome to use little flash V%s .", LF_SW_VERSION);
+    LF_INFO("Github Repositories https://github.com/Dozingfiretruck/little_flash .");
+    LF_INFO("Gitee Repositories https://gitee.com/Dozingfiretruck/little_flash .");
+    return LF_ERR_OK;
+}
+
+lf_err_t little_flash_device_init(little_flash_t *lf){
+    lf_err_t result = LF_ERR_OK;
+    little_flash_port_init(lf);
+    LF_ASSERT(lf->wait_10us);
+    LF_ASSERT(lf->wait_ms);
+#ifdef LF_USE_SPI
+    LF_ASSERT(lf->spi.transfer);
+#endif
+#ifdef LF_USE_HEAP
+    LF_ASSERT(lf->malloc);
+    LF_ASSERT(lf->free);
+#endif
+    uint8_t cmd_data[2]={LF_CMD_JEDEC_ID}, recv_data[3]={0};
+    result |= lf->spi.transfer(lf,cmd_data, 2, recv_data, sizeof(recv_data));
+    if(result) return result;
+    for (size_t i = 0; i < sizeof(little_flash_table)/sizeof(little_flash_table[0]); i++){
+        if (recv_data[0]==little_flash_table[i].manufacturer_id && (recv_data[1]<<8|recv_data[2])==little_flash_table[i].device_id){
+            memcpy(&lf->chip_info.name,&little_flash_table[i],sizeof(little_flash_chipinfo_t));
+            LF_DEBUG("little flash fonud flash %s",lf->chip_info.name);
+            if(lf->chip_info.prog_size==0) lf->chip_info.prog_size = LF_NANDFLASH_PAGE_ZISE;
+            if(lf->chip_info.read_size==0) lf->chip_info.read_size = LF_NANDFLASH_PAGE_ZISE;
+            if(lf->chip_info.retry_times==0) lf->chip_info.retry_times = LF_RETRY_TIMES;
+            result |= little_flash_reset(lf);
+            return result;
+        }
+    }
+    LF_DEBUG("NOT fonud flash");
+    return LF_ERR_NO_FLASH;
+}
+
+lf_err_t little_flash_device_deinit(little_flash_t *lf){
+
+
+
+    return LF_ERR_OK;
+}
+
+lf_err_t little_flash_deinit(void){
+    return LF_ERR_OK;
+}
+
+// lf_err_t little_flash_get_info(const little_flash_t *lf, uint32_t *total_size, uint32_t *block_size, uint32_t *page_size, uint32_t *sector_size);
+
+static lf_err_t little_flash_cheak_erase(const little_flash_t *lf){
+    lf_err_t result = LF_ERR_OK;
+    uint8_t status;
+    result |= little_flash_wait_busy(lf);
+    result |= little_flash_read_status(lf, LF_NANDFLASH_STATUS_REGISTER3, &status);
+    if (result || (status&0x04)){
+        return LF_ERR_ERASE;
+    }
+    return result;
+}
+
+static lf_err_t little_flash_cheak_write(const little_flash_t *lf){
+    lf_err_t result = LF_ERR_OK;
+    uint8_t status;
+    result |= little_flash_wait_busy(lf);
+    result |= little_flash_read_status(lf, LF_NANDFLASH_STATUS_REGISTER3, &status);
+    if (result||(status&0x08)){
+        return LF_ERR_WRITE;
+    }
+    return result;
+}
+
+static lf_err_t little_flash_cheak_read(const little_flash_t *lf){
+    lf_err_t result = LF_ERR_OK;
+    uint8_t status;
+    result |= little_flash_wait_busy(lf);
+    result |= little_flash_read_status(lf, LF_NANDFLASH_STATUS_REGISTER3, &status);
+    // 以下也是要根据不同型号移植的
+    uint8_t ecc = (status & 0x30) >> 4;
+    if (result==0 && ecc<2){
+        return LF_ERR_OK;
+    }
+    return LF_ERR_READ;
+}
+
+lf_err_t little_flash_chip_erase(const little_flash_t *lf){
+    lf_err_t result = LF_ERR_OK;
+    uint32_t addr = 0;
+    uint8_t cmd_data[4];
+    result |= little_flash_write_enabled(lf, LF_ENABLE);
+    if(result) return result;
+    cmd_data[0] = lf->chip_info.erase_cmd;
+    while (true){
+        uint32_t page_addr = addr/lf->chip_info.prog_size;
+        cmd_data[1] = page_addr >> 16;
+        cmd_data[2] = page_addr >> 8;
+        cmd_data[3] = page_addr;
+        result |= lf->spi.transfer(lf,cmd_data, 4,LF_NULL,0);
+        if(result) return result;
+        lf->wait_ms(2);
+        result |= little_flash_cheak_erase(lf);
+        if(result) return result;
+        addr += lf->chip_info.addr_bytes;
+        if (addr>=lf->chip_info.capacity){
+            break;
+        }
+    }
+    result |= little_flash_write_enabled(lf, LF_DISABLE);
+    return result;
+}
+
+lf_err_t little_flash_erase(const little_flash_t *lf, uint32_t addr, uint32_t len){
+    lf_err_t result = LF_ERR_OK;
+    uint8_t cmd_data[4]={0};
+    if (addr + len > lf->chip_info.capacity) {
+        LF_ERROR("Error: Flash address is out of bound.");
+        return LF_ERR_BAD_ADDRESS;
+    }
+
+    if (addr == 0 && len == lf->chip_info.capacity) {
+        return little_flash_chip_erase(lf);
+    }
+
+    result |= little_flash_write_enabled(lf, LF_ENABLE);
+    if(result) return result;
+    cmd_data[0] = lf->chip_info.erase_cmd;
+    while (len){
+        uint32_t page_addr = addr/lf->chip_info.prog_size;
+        cmd_data[1] = page_addr >> 16;
+        cmd_data[2] = page_addr >> 8;
+        cmd_data[3] = page_addr;
+        result |= lf->spi.transfer(lf,cmd_data, 4,LF_NULL,0);
+        if(result) return result;
+        lf->wait_ms(2);
+        result |= little_flash_cheak_erase(lf);
+        if(result) return result;
+        addr += lf->chip_info.erase_size;
+        if (len<=lf->chip_info.erase_size){
+            break;
+        }else{
+            len -= lf->chip_info.erase_size;
+        }
+    }
+    result |= little_flash_write_enabled(lf, LF_DISABLE);
+    return result;
+}
+
+
+lf_err_t little_flash_write(const little_flash_t *lf, uint32_t addr, const uint8_t *data, uint32_t len){
+    lf_err_t result = LF_ERR_OK;
+#ifdef LF_USE_HEAP
+    uint8_t* cmd_data = (uint8_t*)lf->malloc(4+lf->chip_info.prog_size);
+    if (!cmd_data){
+        LF_ERROR("Error: malloc failed.");
+        return LF_ERR_NO_MEM;
+    }
+#else
+    uint8_t cmd_data[4+lf->chip_info.prog_size];
+#endif /* LF_USE_HEAP */
+    uint32_t base_addr = addr;
+    result |= little_flash_write_enabled(lf, LF_ENABLE);
+
+    while (len){
+        result |= little_flash_wait_busy(lf);
+        uint32_t page_addr = addr/lf->chip_info.prog_size;
+        uint16_t column_addr = addr%lf->chip_info.prog_size;
+        cmd_data[0] = LF_CMD_PROG_DATA;
+        cmd_data[1] = column_addr >> 8;
+        cmd_data[2] = column_addr;
+        if (column_addr){
+            if ((column_addr+len)<=lf->chip_info.prog_size){
+                memcpy(&cmd_data[3],&data[addr-base_addr],len);
+                lf->spi.transfer(lf,cmd_data, 3+len,LF_NULL,0);
+                little_flash_wait_busy(lf);
+                cmd_data[0] = LF_NANDFLASH_PAGE_PROG_EXEC;
+                cmd_data[1] = page_addr >> 16;
+                cmd_data[2] = page_addr >> 8;
+                cmd_data[3] = page_addr;
+                lf->spi.transfer(lf,cmd_data, 4,LF_NULL,0);
+                little_flash_cheak_write(lf);
+                break;
+            }else{
+                memcpy(&cmd_data[3],&data[addr-base_addr],lf->chip_info.prog_size-column_addr);
+                lf->spi.transfer(lf,cmd_data, 3+lf->chip_info.prog_size-column_addr,LF_NULL,0);
+                len -= (lf->chip_info.prog_size-column_addr);
+                addr += (lf->chip_info.prog_size-column_addr);
+            }
+        }else{
+            if (len<=lf->chip_info.prog_size){
+                memcpy(&cmd_data[3],&data[addr-base_addr],len);
+                lf->spi.transfer(lf,cmd_data, 3+len,LF_NULL,0);
+                little_flash_wait_busy(lf);
+                cmd_data[0] = LF_NANDFLASH_PAGE_PROG_EXEC;
+                cmd_data[1] = page_addr >> 16;
+                cmd_data[2] = page_addr >> 8;
+                cmd_data[3] = page_addr;
+                lf->spi.transfer(lf,cmd_data, 4,LF_NULL,0);
+                little_flash_cheak_write(lf);
+                break;
+            }else{
+                memcpy(&cmd_data[3],&data[addr-base_addr],lf->chip_info.prog_size);
+                lf->spi.transfer(lf,cmd_data, 3+lf->chip_info.prog_size,LF_NULL,0);
+                len -= lf->chip_info.prog_size;
+                addr += lf->chip_info.prog_size;
+            }
+        }
+
+        little_flash_wait_busy(lf);
+        cmd_data[0] = LF_NANDFLASH_PAGE_PROG_EXEC;
+        cmd_data[1] = page_addr >> 16;
+        cmd_data[2] = page_addr >> 8;
+        cmd_data[3] = page_addr;
+        lf->spi.transfer(lf,cmd_data, 4,LF_NULL,0);
+        little_flash_cheak_write(lf);
+
+    }
+    little_flash_write_enabled(lf, LF_DISABLE);
+#ifdef LF_USE_HEAP
+    lf->free(cmd_data);
+#endif /* LF_USE_HEAP */
+    return LF_ERR_OK;
+}
+
+
+
+lf_err_t little_flash_read(const little_flash_t *lf, uint32_t addr, uint8_t *data, uint32_t len){
+    uint8_t cmd_data[4];
+    uint32_t base_addr = addr;
+    while (len){
+        little_flash_wait_busy(lf);
+
+        uint32_t page_addr = addr/lf->chip_info.prog_size;
+        uint16_t column_addr = addr%lf->chip_info.prog_size;
+
+        cmd_data[0] = LF_NANDFLASH_PAGE_DATA_READ;
+        cmd_data[1] = page_addr >> 16;
+        cmd_data[2] = page_addr >> 8;
+        cmd_data[3] = page_addr;
+        lf->spi.transfer(lf,cmd_data, 4,LF_NULL,0);
+        if (little_flash_cheak_read(lf)){
+            return LF_ERR_READ;
+        }
+        cmd_data[0] = LF_CMD_READ_DATA;
+        cmd_data[1] = column_addr >> 8;
+        cmd_data[2] = column_addr;
+        cmd_data[3] = 0;
+        if (column_addr){
+            if ((column_addr+len)<=lf->chip_info.prog_size){
+                lf->spi.transfer(lf,cmd_data, 4,&data[addr-base_addr],len);
+                break;
+            }else{
+                lf->spi.transfer(lf,cmd_data, 4,&data[addr-base_addr],lf->chip_info.prog_size-column_addr);
+                len -= (lf->chip_info.prog_size-column_addr);
+                addr += (lf->chip_info.prog_size-column_addr);
+            }
+        }else{
+            if (len<=lf->chip_info.prog_size){
+                lf->spi.transfer(lf,cmd_data, 4,&data[addr-base_addr],len);
+                break;
+            }else{
+                lf->spi.transfer(lf,cmd_data, 4,&data[addr-base_addr],lf->chip_info.prog_size);
+                len -= lf->chip_info.prog_size;
+                addr += lf->chip_info.prog_size;
+            }
+        }
+    }
+    return LF_ERR_OK;
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+