fal_rtt.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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. #include <fal.h>
  12. #ifdef RT_VER_NUM
  13. #include <rtthread.h>
  14. #include <rtdevice.h>
  15. #include <string.h>
  16. /* ========================== block device ======================== */
  17. struct fal_blk_device
  18. {
  19. struct rt_device parent;
  20. struct rt_device_blk_geometry geometry;
  21. const struct fal_partition *fal_part;
  22. };
  23. /* RT-Thread device interface */
  24. #if RTTHREAD_VERSION >= 30000
  25. static rt_err_t blk_dev_control(rt_device_t dev, int cmd, void *args)
  26. #else
  27. static rt_err_t blk_dev_control(rt_device_t dev, rt_uint8_t cmd, void *args)
  28. #endif
  29. {
  30. struct fal_blk_device *part = (struct fal_blk_device*) dev;
  31. assert(part != RT_NULL);
  32. if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME)
  33. {
  34. struct rt_device_blk_geometry *geometry;
  35. geometry = (struct rt_device_blk_geometry *) args;
  36. if (geometry == RT_NULL)
  37. {
  38. return -RT_ERROR;
  39. }
  40. memcpy(geometry, &part->geometry, sizeof(struct rt_device_blk_geometry));
  41. }
  42. else if (cmd == RT_DEVICE_CTRL_BLK_ERASE)
  43. {
  44. rt_uint32_t *addrs = (rt_uint32_t *) args, start_addr = addrs[0], end_addr = addrs[1], phy_start_addr;
  45. rt_size_t phy_size;
  46. if (addrs == RT_NULL || start_addr > end_addr)
  47. {
  48. return -RT_ERROR;
  49. }
  50. if (end_addr == start_addr)
  51. {
  52. end_addr++;
  53. }
  54. phy_start_addr = start_addr * part->geometry.bytes_per_sector;
  55. phy_size = (end_addr - start_addr) * part->geometry.bytes_per_sector;
  56. if (fal_partition_erase(part->fal_part, phy_start_addr, phy_size) < 0)
  57. {
  58. return -RT_ERROR;
  59. }
  60. }
  61. return RT_EOK;
  62. }
  63. static rt_size_t blk_dev_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
  64. {
  65. int ret = 0;
  66. struct fal_blk_device *part = (struct fal_blk_device*) dev;
  67. assert(part != RT_NULL);
  68. ret = fal_partition_read(part->fal_part, pos * part->geometry.block_size, buffer, size * part->geometry.block_size);
  69. if (ret != (int)(size * part->geometry.block_size))
  70. {
  71. ret = 0;
  72. }
  73. else
  74. {
  75. ret = size;
  76. }
  77. return ret;
  78. }
  79. static rt_size_t blk_dev_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
  80. {
  81. int ret = 0;
  82. struct fal_blk_device *part;
  83. rt_off_t phy_pos;
  84. rt_size_t phy_size;
  85. part = (struct fal_blk_device*) dev;
  86. assert(part != RT_NULL);
  87. /* change the block device's logic address to physical address */
  88. phy_pos = pos * part->geometry.bytes_per_sector;
  89. phy_size = size * part->geometry.bytes_per_sector;
  90. ret = fal_partition_erase(part->fal_part, phy_pos, phy_size);
  91. if (ret == (int) phy_size)
  92. {
  93. ret = fal_partition_write(part->fal_part, phy_pos, buffer, phy_size);
  94. }
  95. if (ret != (int) phy_size)
  96. {
  97. ret = 0;
  98. }
  99. else
  100. {
  101. ret = size;
  102. }
  103. return ret;
  104. }
  105. #ifdef RT_USING_DEVICE_OPS
  106. const static struct rt_device_ops blk_dev_ops =
  107. {
  108. RT_NULL,
  109. RT_NULL,
  110. RT_NULL,
  111. blk_dev_read,
  112. blk_dev_write,
  113. blk_dev_control
  114. };
  115. #endif
  116. /**
  117. * create RT-Thread block device by specified partition
  118. *
  119. * @param parition_name partition name
  120. *
  121. * @return != NULL: created block device
  122. * NULL: created failed
  123. */
  124. struct rt_device *fal_blk_device_create(const char *parition_name)
  125. {
  126. struct fal_blk_device *blk_dev;
  127. const struct fal_partition *fal_part = fal_partition_find(parition_name);
  128. const struct fal_flash_dev *fal_flash = NULL;
  129. if (!fal_part)
  130. {
  131. log_e("Error: the partition name (%s) is not found.", parition_name);
  132. return NULL;
  133. }
  134. if ((fal_flash = fal_flash_device_find(fal_part->flash_name)) == NULL)
  135. {
  136. log_e("Error: the flash device name (%s) is not found.", fal_part->flash_name);
  137. return NULL;
  138. }
  139. blk_dev = (struct fal_blk_device*) rt_malloc(sizeof(struct fal_blk_device));
  140. if (blk_dev)
  141. {
  142. blk_dev->fal_part = fal_part;
  143. blk_dev->geometry.bytes_per_sector = fal_flash->blk_size;
  144. blk_dev->geometry.block_size = fal_flash->blk_size;
  145. blk_dev->geometry.sector_count = fal_part->len / fal_flash->blk_size;
  146. /* register device */
  147. blk_dev->parent.type = RT_Device_Class_Block;
  148. #ifdef RT_USING_DEVICE_OPS
  149. blk_dev->parent.ops = &blk_dev_ops;
  150. #else
  151. blk_dev->parent.init = NULL;
  152. blk_dev->parent.open = NULL;
  153. blk_dev->parent.close = NULL;
  154. blk_dev->parent.read = blk_dev_read;
  155. blk_dev->parent.write = blk_dev_write;
  156. blk_dev->parent.control = blk_dev_control;
  157. #endif
  158. /* no private */
  159. blk_dev->parent.user_data = RT_NULL;
  160. log_i("The FAL block device (%s) created successfully", fal_part->name);
  161. rt_device_register(RT_DEVICE(blk_dev), fal_part->name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
  162. }
  163. else
  164. {
  165. log_e("Error: no memory for create FAL block device");
  166. }
  167. return RT_DEVICE(blk_dev);
  168. }
  169. /* ========================== MTD nor device ======================== */
  170. #if defined(RT_USING_MTD_NOR)
  171. struct fal_mtd_nor_device
  172. {
  173. struct rt_mtd_nor_device parent;
  174. const struct fal_partition *fal_part;
  175. };
  176. 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)
  177. {
  178. int ret = 0;
  179. struct fal_mtd_nor_device *part = (struct fal_mtd_nor_device*) device;
  180. assert(part != RT_NULL);
  181. ret = fal_partition_read(part->fal_part, offset, data, length);
  182. if (ret != (int)length)
  183. {
  184. ret = 0;
  185. }
  186. else
  187. {
  188. ret = length;
  189. }
  190. return ret;
  191. }
  192. 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)
  193. {
  194. int ret = 0;
  195. struct fal_mtd_nor_device *part;
  196. part = (struct fal_mtd_nor_device*) device;
  197. assert(part != RT_NULL);
  198. ret = fal_partition_write(part->fal_part, offset, data, length);
  199. if (ret != (int) length)
  200. {
  201. ret = 0;
  202. }
  203. else
  204. {
  205. ret = length;
  206. }
  207. return ret;
  208. }
  209. static rt_err_t mtd_nor_dev_erase(struct rt_mtd_nor_device* device, rt_off_t offset, rt_uint32_t length)
  210. {
  211. int ret = 0;
  212. struct fal_mtd_nor_device *part;
  213. part = (struct fal_mtd_nor_device*) device;
  214. assert(part != RT_NULL);
  215. ret = fal_partition_erase(part->fal_part, offset, length);
  216. if (ret != length)
  217. {
  218. return -RT_ERROR;
  219. }
  220. else
  221. {
  222. return RT_EOK;
  223. }
  224. }
  225. static const struct rt_mtd_nor_driver_ops _ops =
  226. {
  227. RT_NULL,
  228. mtd_nor_dev_read,
  229. mtd_nor_dev_write,
  230. mtd_nor_dev_erase,
  231. };
  232. /**
  233. * create RT-Thread MTD NOR device by specified partition
  234. *
  235. * @param parition_name partition name
  236. *
  237. * @return != NULL: created MTD NOR device
  238. * NULL: created failed
  239. */
  240. struct rt_device *fal_mtd_nor_device_create(const char *parition_name)
  241. {
  242. struct fal_mtd_nor_device *mtd_nor_dev;
  243. const struct fal_partition *fal_part = fal_partition_find(parition_name);
  244. const struct fal_flash_dev *fal_flash = NULL;
  245. if (!fal_part)
  246. {
  247. log_e("Error: the partition name (%s) is not found.", parition_name);
  248. return NULL;
  249. }
  250. if ((fal_flash = fal_flash_device_find(fal_part->flash_name)) == NULL)
  251. {
  252. log_e("Error: the flash device name (%s) is not found.", fal_part->flash_name);
  253. return NULL;
  254. }
  255. mtd_nor_dev = (struct fal_mtd_nor_device*) rt_malloc(sizeof(struct fal_mtd_nor_device));
  256. if (mtd_nor_dev)
  257. {
  258. mtd_nor_dev->fal_part = fal_part;
  259. mtd_nor_dev->parent.block_start = 0;
  260. mtd_nor_dev->parent.block_end = fal_part->len / fal_flash->blk_size;
  261. mtd_nor_dev->parent.block_size = fal_flash->blk_size;
  262. /* set ops */
  263. mtd_nor_dev->parent.ops = &_ops;
  264. log_i("The FAL MTD NOR device (%s) created successfully", fal_part->name);
  265. rt_mtd_nor_register_device(fal_part->name, &mtd_nor_dev->parent);
  266. }
  267. else
  268. {
  269. log_e("Error: no memory for create FAL MTD NOR device");
  270. }
  271. return RT_DEVICE(&mtd_nor_dev->parent);
  272. }
  273. #endif /* defined(RT_USING_MTD_NOR) */
  274. /* ========================== char device ======================== */
  275. struct fal_char_device
  276. {
  277. struct rt_device parent;
  278. const struct fal_partition *fal_part;
  279. };
  280. /* RT-Thread device interface */
  281. static rt_size_t char_dev_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  282. {
  283. int ret = 0;
  284. struct fal_char_device *part = (struct fal_char_device *) dev;
  285. assert(part != RT_NULL);
  286. if (pos + size > part->fal_part->len)
  287. size = part->fal_part->len - pos;
  288. ret = fal_partition_read(part->fal_part, pos, buffer, size);
  289. if (ret != (int)(size))
  290. ret = 0;
  291. return ret;
  292. }
  293. static rt_size_t char_dev_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  294. {
  295. int ret = 0;
  296. struct fal_char_device *part;
  297. part = (struct fal_char_device *) dev;
  298. assert(part != RT_NULL);
  299. if (pos == 0)
  300. {
  301. fal_partition_erase_all(part->fal_part);
  302. }
  303. else if (pos + size > part->fal_part->len)
  304. {
  305. size = part->fal_part->len - pos;
  306. }
  307. ret = fal_partition_write(part->fal_part, pos, buffer, size);
  308. if (ret != (int) size)
  309. ret = 0;
  310. return ret;
  311. }
  312. #ifdef RT_USING_DEVICE_OPS
  313. const static struct rt_device_ops char_dev_ops =
  314. {
  315. RT_NULL,
  316. RT_NULL,
  317. RT_NULL,
  318. char_dev_read,
  319. char_dev_write,
  320. RT_NULL
  321. };
  322. #endif
  323. #ifdef RT_USING_POSIX
  324. #include <dfs_posix.h>
  325. /* RT-Thread device filesystem interface */
  326. static int char_dev_fopen(struct dfs_fd *fd)
  327. {
  328. struct fal_char_device *part = (struct fal_char_device *) fd->data;
  329. assert(part != RT_NULL);
  330. switch (fd->flags & O_ACCMODE)
  331. {
  332. case O_RDONLY:
  333. break;
  334. case O_WRONLY:
  335. case O_RDWR:
  336. /* erase partition when device file open */
  337. fal_partition_erase_all(part->fal_part);
  338. break;
  339. default:
  340. break;
  341. }
  342. fd->pos = 0;
  343. return RT_EOK;
  344. }
  345. static int char_dev_fread(struct dfs_fd *fd, void *buf, size_t count)
  346. {
  347. int ret = 0;
  348. struct fal_char_device *part = (struct fal_char_device *) fd->data;
  349. assert(part != RT_NULL);
  350. if (fd->pos + count > part->fal_part->len)
  351. count = part->fal_part->len - fd->pos;
  352. ret = fal_partition_read(part->fal_part, fd->pos, buf, count);
  353. if (ret != (int)(count))
  354. return 0;
  355. fd->pos += ret;
  356. return ret;
  357. }
  358. static int char_dev_fwrite(struct dfs_fd *fd, const void *buf, size_t count)
  359. {
  360. int ret = 0;
  361. struct fal_char_device *part = (struct fal_char_device *) fd->data;
  362. assert(part != RT_NULL);
  363. if (fd->pos + count > part->fal_part->len)
  364. count = part->fal_part->len - fd->pos;
  365. ret = fal_partition_write(part->fal_part, fd->pos, buf, count);
  366. if (ret != (int) count)
  367. return 0;
  368. fd->pos += ret;
  369. return ret;
  370. }
  371. static const struct dfs_file_ops char_dev_fops =
  372. {
  373. char_dev_fopen,
  374. RT_NULL,
  375. RT_NULL,
  376. char_dev_fread,
  377. char_dev_fwrite,
  378. RT_NULL, /* flush */
  379. RT_NULL, /* lseek */
  380. RT_NULL, /* getdents */
  381. RT_NULL,
  382. };
  383. #endif /* defined(RT_USING_POSIX) */
  384. /**
  385. * create RT-Thread char device by specified partition
  386. *
  387. * @param parition_name partition name
  388. *
  389. * @return != NULL: created char device
  390. * NULL: created failed
  391. */
  392. struct rt_device *fal_char_device_create(const char *parition_name)
  393. {
  394. struct fal_char_device *char_dev;
  395. const struct fal_partition *fal_part = fal_partition_find(parition_name);
  396. if (!fal_part)
  397. {
  398. log_e("Error: the partition name (%s) is not found.", parition_name);
  399. return NULL;
  400. }
  401. if ((fal_flash_device_find(fal_part->flash_name)) == NULL)
  402. {
  403. log_e("Error: the flash device name (%s) is not found.", fal_part->flash_name);
  404. return NULL;
  405. }
  406. char_dev = (struct fal_char_device *) rt_malloc(sizeof(struct fal_char_device));
  407. if (char_dev)
  408. {
  409. char_dev->fal_part = fal_part;
  410. /* register device */
  411. char_dev->parent.type = RT_Device_Class_Char;
  412. #ifdef RT_USING_DEVICE_OPS
  413. char_dev->parent.ops = &char_dev_ops;
  414. #else
  415. char_dev->parent.init = NULL;
  416. char_dev->parent.open = NULL;
  417. char_dev->parent.close = NULL;
  418. char_dev->parent.read = char_dev_read;
  419. char_dev->parent.write = char_dev_write;
  420. char_dev->parent.control = NULL;
  421. /* no private */
  422. char_dev->parent.user_data = NULL;
  423. #endif
  424. rt_device_register(RT_DEVICE(char_dev), fal_part->name, RT_DEVICE_FLAG_RDWR);
  425. log_i("The FAL char device (%s) created successfully", fal_part->name);
  426. #ifdef RT_USING_POSIX
  427. /* set fops */
  428. char_dev->parent.fops = &char_dev_fops;
  429. #endif
  430. }
  431. else
  432. {
  433. log_e("Error: no memory for create FAL char device");
  434. }
  435. return RT_DEVICE(char_dev);
  436. }
  437. #if defined(RT_USING_FINSH) && defined(FINSH_USING_MSH)
  438. #include <finsh.h>
  439. extern int fal_init_check(void);
  440. static void fal(uint8_t argc, char **argv) {
  441. #define __is_print(ch) ((unsigned int)((ch) - ' ') < 127u - ' ')
  442. #define HEXDUMP_WIDTH 16
  443. #define CMD_PROBE_INDEX 0
  444. #define CMD_READ_INDEX 1
  445. #define CMD_WRITE_INDEX 2
  446. #define CMD_ERASE_INDEX 3
  447. #define CMD_BENCH_INDEX 4
  448. int result;
  449. static const struct fal_flash_dev *flash_dev = NULL;
  450. static const struct fal_partition *part_dev = NULL;
  451. size_t i = 0, j = 0;
  452. const char* help_info[] =
  453. {
  454. [CMD_PROBE_INDEX] = "fal probe [dev_name|part_name] - probe flash device or partition by given name",
  455. [CMD_READ_INDEX] = "fal read addr size - read 'size' bytes starting at 'addr'",
  456. [CMD_WRITE_INDEX] = "fal write addr data1 ... dataN - write some bytes 'data' starting at 'addr'",
  457. [CMD_ERASE_INDEX] = "fal erase addr size - erase 'size' bytes starting at 'addr'",
  458. [CMD_BENCH_INDEX] = "fal bench <blk_size> - benchmark test with per block size",
  459. };
  460. if (fal_init_check() != 1)
  461. {
  462. rt_kprintf("\n[Warning] FAL is not initialized or failed to initialize!\n\n");
  463. return;
  464. }
  465. if (argc < 2)
  466. {
  467. rt_kprintf("Usage:\n");
  468. for (i = 0; i < sizeof(help_info) / sizeof(char*); i++)
  469. {
  470. rt_kprintf("%s\n", help_info[i]);
  471. }
  472. rt_kprintf("\n");
  473. }
  474. else
  475. {
  476. const char *operator = argv[1];
  477. uint32_t addr, size;
  478. if (!strcmp(operator, "probe"))
  479. {
  480. if (argc >= 3)
  481. {
  482. char *dev_name = argv[2];
  483. if ((flash_dev = fal_flash_device_find(dev_name)) != NULL)
  484. {
  485. part_dev = NULL;
  486. }
  487. else if ((part_dev = fal_partition_find(dev_name)) != NULL)
  488. {
  489. flash_dev = NULL;
  490. }
  491. else
  492. {
  493. rt_kprintf("Device %s NOT found. Probe failed.\n", dev_name);
  494. flash_dev = NULL;
  495. part_dev = NULL;
  496. }
  497. }
  498. if (flash_dev)
  499. {
  500. rt_kprintf("Probed a flash device | %s | addr: %ld | len: %d |.\n", flash_dev->name,
  501. flash_dev->addr, flash_dev->len);
  502. }
  503. else if (part_dev)
  504. {
  505. rt_kprintf("Probed a flash partition | %s | flash_dev: %s | offset: %ld | len: %d |.\n",
  506. part_dev->name, part_dev->flash_name, part_dev->offset, part_dev->len);
  507. }
  508. else
  509. {
  510. rt_kprintf("No flash device or partition was probed.\n");
  511. rt_kprintf("Usage: %s.\n", help_info[CMD_PROBE_INDEX]);
  512. fal_show_part_table();
  513. }
  514. }
  515. else
  516. {
  517. if (!flash_dev && !part_dev)
  518. {
  519. rt_kprintf("No flash device or partition was probed. Please run 'fal probe'.\n");
  520. return;
  521. }
  522. if (!rt_strcmp(operator, "read"))
  523. {
  524. if (argc < 4)
  525. {
  526. rt_kprintf("Usage: %s.\n", help_info[CMD_READ_INDEX]);
  527. return;
  528. }
  529. else
  530. {
  531. addr = strtol(argv[2], NULL, 0);
  532. size = strtol(argv[3], NULL, 0);
  533. uint8_t *data = rt_malloc(size);
  534. if (data)
  535. {
  536. if (flash_dev)
  537. {
  538. result = flash_dev->ops.read(addr, data, size);
  539. }
  540. else if (part_dev)
  541. {
  542. result = fal_partition_read(part_dev, addr, data, size);
  543. }
  544. if (result >= 0)
  545. {
  546. rt_kprintf("Read data success. Start from 0x%08X, size is %ld. The data is:\n", addr,
  547. size);
  548. rt_kprintf("Offset (h) 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n");
  549. for (i = 0; i < size; i += HEXDUMP_WIDTH)
  550. {
  551. rt_kprintf("[%08X] ", addr + i);
  552. /* dump hex */
  553. for (j = 0; j < HEXDUMP_WIDTH; j++)
  554. {
  555. if (i + j < size)
  556. {
  557. rt_kprintf("%02X ", data[i + j]);
  558. }
  559. else
  560. {
  561. rt_kprintf(" ");
  562. }
  563. }
  564. /* dump char for hex */
  565. for (j = 0; j < HEXDUMP_WIDTH; j++)
  566. {
  567. if (i + j < size)
  568. {
  569. rt_kprintf("%c", __is_print(data[i + j]) ? data[i + j] : '.');
  570. }
  571. }
  572. rt_kprintf("\n");
  573. }
  574. rt_kprintf("\n");
  575. }
  576. rt_free(data);
  577. }
  578. else
  579. {
  580. rt_kprintf("Low memory!\n");
  581. }
  582. }
  583. }
  584. else if (!strcmp(operator, "write"))
  585. {
  586. if (argc < 4)
  587. {
  588. rt_kprintf("Usage: %s.\n", help_info[CMD_WRITE_INDEX]);
  589. return;
  590. }
  591. else
  592. {
  593. addr = strtol(argv[2], NULL, 0);
  594. size = argc - 3;
  595. uint8_t *data = rt_malloc(size);
  596. if (data)
  597. {
  598. for (i = 0; i < size; i++)
  599. {
  600. data[i] = strtol(argv[3 + i], NULL, 0);
  601. }
  602. if (flash_dev)
  603. {
  604. result = flash_dev->ops.write(addr, data, size);
  605. }
  606. else if (part_dev)
  607. {
  608. result = fal_partition_write(part_dev, addr, data, size);
  609. }
  610. if (result >= 0)
  611. {
  612. rt_kprintf("Write data success. Start from 0x%08X, size is %ld.\n", addr, size);
  613. rt_kprintf("Write data: ");
  614. for (i = 0; i < size; i++)
  615. {
  616. rt_kprintf("%d ", data[i]);
  617. }
  618. rt_kprintf(".\n");
  619. }
  620. rt_free(data);
  621. }
  622. else
  623. {
  624. rt_kprintf("Low memory!\n");
  625. }
  626. }
  627. }
  628. else if (!rt_strcmp(operator, "erase"))
  629. {
  630. if (argc < 4)
  631. {
  632. rt_kprintf("Usage: %s.\n", help_info[CMD_ERASE_INDEX]);
  633. return;
  634. }
  635. else
  636. {
  637. addr = strtol(argv[2], NULL, 0);
  638. size = strtol(argv[3], NULL, 0);
  639. if (flash_dev)
  640. {
  641. result = flash_dev->ops.erase(addr, size);
  642. }
  643. else if (part_dev)
  644. {
  645. result = fal_partition_erase(part_dev, addr, size);
  646. }
  647. if (result >= 0)
  648. {
  649. rt_kprintf("Erase data success. Start from 0x%08X, size is %ld.\n", addr, size);
  650. }
  651. }
  652. }
  653. else if (!strcmp(operator, "bench"))
  654. {
  655. if (argc < 3)
  656. {
  657. rt_kprintf("Usage: %s.\n", help_info[CMD_BENCH_INDEX]);
  658. return;
  659. }
  660. else if ((argc > 3 && strcmp(argv[3], "yes")) || argc < 4)
  661. {
  662. rt_kprintf("DANGER: It will erase full chip or partition! Please run 'fal bench %d yes'.\n", strtol(argv[2], NULL, 0));
  663. return;
  664. }
  665. /* full chip benchmark test */
  666. uint32_t start_time, time_cast;
  667. size_t write_size = strtol(argv[2], NULL, 0), read_size = strtol(argv[2], NULL, 0), cur_op_size;
  668. uint8_t *write_data = (uint8_t *)rt_malloc(write_size), *read_data = (uint8_t *)rt_malloc(read_size);
  669. if (write_data && read_data)
  670. {
  671. for (i = 0; i < write_size; i ++) {
  672. write_data[i] = i & 0xFF;
  673. }
  674. if (flash_dev)
  675. {
  676. size = flash_dev->len;
  677. }
  678. else if (part_dev)
  679. {
  680. size = part_dev->len;
  681. }
  682. /* benchmark testing */
  683. rt_kprintf("Erasing %ld bytes data, waiting...\n", size);
  684. start_time = rt_tick_get();
  685. if (flash_dev)
  686. {
  687. result = flash_dev->ops.erase(0, size);
  688. }
  689. else if (part_dev)
  690. {
  691. result = fal_partition_erase(part_dev, 0, size);
  692. }
  693. if (result >= 0)
  694. {
  695. time_cast = rt_tick_get() - start_time;
  696. rt_kprintf("Erase benchmark success, total time: %d.%03dS.\n", time_cast / RT_TICK_PER_SECOND,
  697. time_cast % RT_TICK_PER_SECOND / ((RT_TICK_PER_SECOND * 1 + 999) / 1000));
  698. }
  699. else
  700. {
  701. rt_kprintf("Erase benchmark has an error. Error code: %d.\n", result);
  702. }
  703. /* write test */
  704. rt_kprintf("Writing %ld bytes data, waiting...\n", size);
  705. start_time = rt_tick_get();
  706. for (i = 0; i < size; i += write_size)
  707. {
  708. if (i + write_size <= size)
  709. {
  710. cur_op_size = write_size;
  711. }
  712. else
  713. {
  714. cur_op_size = size - i;
  715. }
  716. if (flash_dev)
  717. {
  718. result = flash_dev->ops.write(i, write_data, cur_op_size);
  719. }
  720. else if (part_dev)
  721. {
  722. result = fal_partition_write(part_dev, i, write_data, cur_op_size);
  723. }
  724. if (result < 0)
  725. {
  726. break;
  727. }
  728. }
  729. if (result >= 0)
  730. {
  731. time_cast = rt_tick_get() - start_time;
  732. rt_kprintf("Write benchmark success, total time: %d.%03dS.\n", time_cast / RT_TICK_PER_SECOND,
  733. time_cast % RT_TICK_PER_SECOND / ((RT_TICK_PER_SECOND * 1 + 999) / 1000));
  734. }
  735. else
  736. {
  737. rt_kprintf("Write benchmark has an error. Error code: %d.\n", result);
  738. }
  739. /* read test */
  740. rt_kprintf("Reading %ld bytes data, waiting...\n", size);
  741. start_time = rt_tick_get();
  742. for (i = 0; i < size; i += read_size)
  743. {
  744. if (i + read_size <= size)
  745. {
  746. cur_op_size = read_size;
  747. }
  748. else
  749. {
  750. cur_op_size = size - i;
  751. }
  752. if (flash_dev)
  753. {
  754. result = flash_dev->ops.read(i, read_data, cur_op_size);
  755. }
  756. else if (part_dev)
  757. {
  758. result = fal_partition_read(part_dev, i, read_data, cur_op_size);
  759. }
  760. /* data check */
  761. for (int index = 0; index < cur_op_size; index ++)
  762. {
  763. if (write_data[index] != read_data[index])
  764. {
  765. rt_kprintf("%d %d %02x %02x.\n", i, index, write_data[index], read_data[index]);
  766. }
  767. }
  768. if (memcmp(write_data, read_data, cur_op_size))
  769. {
  770. result = -RT_ERROR;
  771. rt_kprintf("Data check ERROR! Please check you flash by other command.\n");
  772. }
  773. /* has an error */
  774. if (result < 0)
  775. {
  776. break;
  777. }
  778. }
  779. if (result >= 0)
  780. {
  781. time_cast = rt_tick_get() - start_time;
  782. rt_kprintf("Read benchmark success, total time: %d.%03dS.\n", time_cast / RT_TICK_PER_SECOND,
  783. time_cast % RT_TICK_PER_SECOND / ((RT_TICK_PER_SECOND * 1 + 999) / 1000));
  784. }
  785. else
  786. {
  787. rt_kprintf("Read benchmark has an error. Error code: %d.\n", result);
  788. }
  789. }
  790. else
  791. {
  792. rt_kprintf("Low memory!\n");
  793. }
  794. rt_free(write_data);
  795. rt_free(read_data);
  796. }
  797. else
  798. {
  799. rt_kprintf("Usage:\n");
  800. for (i = 0; i < sizeof(help_info) / sizeof(char*); i++)
  801. {
  802. rt_kprintf("%s\n", help_info[i]);
  803. }
  804. rt_kprintf("\n");
  805. return;
  806. }
  807. if (result < 0) {
  808. rt_kprintf("This operate has an error. Error code: %d.\n", result);
  809. }
  810. }
  811. }
  812. }
  813. MSH_CMD_EXPORT(fal, FAL (Flash Abstraction Layer) operate.);
  814. #endif /* defined(RT_USING_FINSH) && defined(FINSH_USING_MSH) */
  815. #endif /* RT_VER_NUM */