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

Merge pull request #1 from RT-Thread-packages/master

sync
HubretXie 7 лет назад
Родитель
Сommit
515443dacd
6 измененных файлов с 160 добавлено и 4 удалено
  1. 10 0
      README.md
  2. 12 0
      inc/fal.h
  3. 1 1
      inc/fal_def.h
  4. 6 2
      src/fal.c
  5. 1 1
      src/fal_partition.c
  6. 130 0
      src/fal_rtt.c

+ 10 - 0
README.md

@@ -155,6 +155,16 @@ FAL 初始化时会自动装载默认分区表。使用该设置将临时修改
 | parition_name | 分区名称 |
 | return        | 创建成功,则返回对应的块设备,失败返回空   |
 
+### 3.1.11 根据分区名称,创建对应的 MTD Nor Flash 设备
+
+该函数可以根据指定的分区名称,创建对应的 MTD Nor Flash 设备,以便于在指定的分区上挂载文件系统
+
+`struct rt_device *fal_mtd_nor_device_create(const char *parition_name)`
+
+| 参数          | 描述                                                  |
+| :------------ | :---------------------------------------------------- |
+| parition_name | 分区名称                                              |
+| return        | 创建成功,则返回对应的 MTD Nor Flash 设备,失败返回空 |
 
 ## 3.2 Finsh/MSH 测试命令
 

+ 12 - 0
inc/fal.h

@@ -143,4 +143,16 @@ void fal_show_part_table(void);
 struct rt_device *fal_blk_device_create(const char *parition_name);
 #endif /* defined(RT_USING_DFS) */
 
+#if defined(RT_USING_MTD_NOR)
+/**
+ * create RT-Thread MTD NOR device by specified partition
+ *
+ * @param parition_name partition name
+ *
+ * @return != NULL: created MTD NOR device
+ *            NULL: created failed
+ */
+struct rt_device *fal_mtd_nor_device_create(const char *parition_name);
+#endif /* defined(RT_USING_MTD_NOR) */
+
 #endif /* _FAL_H_ */

+ 1 - 1
inc/fal_def.h

@@ -92,7 +92,7 @@ if (!(EXPR))                                                                   \
 #ifdef  log_i
 #undef  log_i
 #endif
-#define log_i(...)                     printf("\033[36;22m[I/FAL] ");                                printf(__VA_ARGS__);printf("\033[0m\n")
+#define log_i(...)                     printf("\033[32;22m[I/FAL] ");                                printf(__VA_ARGS__);printf("\033[0m\n")
 
 /* FAL flash and partition device name max length */
 #ifndef FAL_DEV_NAME_MAX

+ 6 - 2
src/fal.c

@@ -36,6 +36,8 @@ int fal_init(void)
     extern int fal_partition_init(void);
 
     int result;
+    static uint8_t init_ok = 0;
+
     /* initialize all flash device on FAL flash table */
     result = fal_flash_init();
 
@@ -48,12 +50,14 @@ int fal_init(void)
 
 __exit:
 
-    if (result > 0)
+    if ((result > 0) && (!init_ok))
     {
+        init_ok = 1;
         log_i("RT-Thread Flash Abstraction Layer (V%s) initialize success.", FAL_SW_VERSION);
     }
-    else
+    else if(result <= 0)
     {
+        init_ok = 0;
         log_e("RT-Thread Flash Abstraction Layer (V%s) initialize failed.", FAL_SW_VERSION);
     }
 

+ 1 - 1
src/fal_partition.c

@@ -90,7 +90,7 @@ void fal_show_part_table(void)
             part = &partition_table[i];
             if (strlen(part->name) > part_name_max)
             {
-                part_name_max = strlen(part->flash_name);
+                part_name_max = strlen(part->name);
             }
             if (strlen(part->flash_name) > flash_dev_name_max)
             {

+ 130 - 0
src/fal_rtt.c

@@ -199,6 +199,136 @@ struct rt_device *fal_blk_device_create(const char *parition_name)
 
 #endif /* defined(RT_USING_DFS) */
 
+#if defined(RT_USING_MTD_NOR)
+
+struct fal_mtd_nor_device
+{
+    struct rt_mtd_nor_device       parent;
+    const struct fal_partition     *fal_part;
+};
+
+static rt_size_t mtd_nor_dev_read(struct rt_mtd_nor_nor_device* device, rt_off_t offset, rt_uint8_t* data, rt_uint32_t length)
+{
+    int ret = 0;
+    struct fal_mtd_nor_device *part = (struct fal_mtd_nor_device*) device;
+
+    assert(part != RT_NULL);
+
+    ret = fal_partition_read(part->fal_part, offset, data, length);
+
+    if (ret != (int)length)
+    {
+        ret = 0;
+    }
+    else
+    {
+        ret = length;
+    }
+
+    return ret;
+}
+
+static rt_size_t mtd_nor_dev_write(struct rt_mtd_nor_nor_device* device, rt_off_t offset, const rt_uint8_t* data, rt_uint32_t length)
+{
+    int ret = 0;
+    struct fal_mtd_nor_device *part;
+
+    part = (struct fal_mtd_nor_device*) device;
+    assert(part != RT_NULL);
+
+    ret = fal_partition_write(part->fal_part, offset, data, length);
+
+    if (ret != (int) length)
+    {
+        ret = 0;
+    }
+    else
+    {
+        ret = length;
+    }
+
+    return ret;
+}
+
+static rt_err_t mtd_nor_dev_erase(struct rt_mtd_nor_nor_device* device, rt_off_t offset, rt_uint32_t length)
+{
+    int ret = 0;
+    struct fal_mtd_nor_device *part;
+
+    part = (struct fal_mtd_nor_device*) device;
+    assert(part != RT_NULL);
+
+    ret = fal_partition_erase(part->fal_part, offset, length);
+
+    if (ret != length)
+    {
+        return -RT_ERROR;
+    }
+    else
+    {
+        return RT_EOK;
+    }
+}
+
+static const struct rt_mtd_nor_nor_driver_ops _ops = 
+{
+    RT_NULL,
+    mtd_nor_dev_read,
+    mtd_nor_dev_write,
+    mtd_nor_dev_erase,
+};
+
+/**
+ * create RT-Thread MTD NOR device by specified partition
+ *
+ * @param parition_name partition name
+ *
+ * @return != NULL: created MTD NOR device
+ *            NULL: created failed
+ */
+struct rt_device *fal_mtd_nor_device_create(const char *parition_name)
+{
+    struct fal_mtd_nor_device *mtd_nor_dev;
+    const struct fal_partition *fal_part = fal_partition_find(parition_name);
+    const struct fal_flash_dev *fal_flash = NULL;
+
+    if (!fal_part)
+    {
+        log_e("Error: the partition name (%s) is not found.", parition_name);
+        return NULL;
+    }
+
+    if ((fal_flash = fal_flash_device_find(fal_part->flash_name)) == NULL)
+    {
+        log_e("Error: the flash device name (%s) is not found.", fal_part->flash_name);
+        return NULL;
+    }
+
+    mtd_nor_dev = (struct fal_mtd_nor_device*) rt_malloc(sizeof(struct fal_mtd_nor_device));
+    if (mtd_nor_dev)
+    {
+        mtd_nor_dev->fal_part = fal_part;
+
+        mtd_nor_dev->parent.block_start = 0;
+        mtd_nor_dev->parent.block_end = fal_part->len / fal_flash->blk_size;
+        mtd_nor_dev->parent.block_size = fal_flash->blk_size;
+
+        /* set ops */
+        mtd_nor_dev->parent.ops = &_ops;
+
+        log_i("The FAL MTD NOR device (%s) created successfully", fal_part->name);
+        rt_mtd_nor_register_device(fal_part->name, &mtd_nor_dev->parent);
+    }
+    else
+    {
+        log_e("Error: no memory for create FAL MTD NOR device");
+    }
+
+    return RT_DEVICE(&mtd_nor_dev->parent);
+}
+
+#endif /* defined(RT_USING_MTD_NOR) */
+
 #if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH)
 
 #include <finsh.h>