drv_flash.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-11-30 flybreak first version
  9. */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include "board.h"
  14. #include "hal_data.h"
  15. #include "drv_common.h"
  16. #if defined(RT_USING_FAL)
  17. #include "fal.h"
  18. #endif
  19. //#define DRV_DEBUG
  20. #define LOG_TAG "drv.flash"
  21. #ifdef DRV_DEBUG
  22. #define DBG_LVL DBG_LOG
  23. #else
  24. #define DBG_LVL DBG_INFO
  25. #endif /* DRV_DEBUG */
  26. #include <rtdbg.h>
  27. #if BSP_FEATURE_FLASH_HP_VERSION
  28. /* FLASH API */
  29. #define R_FLASH_Open R_FLASH_HP_Open
  30. #define R_FLASH_Reset R_FLASH_HP_Reset
  31. #define R_FLASH_Write R_FLASH_HP_Write
  32. #define R_FLASH_Erase R_FLASH_HP_Erase
  33. #define R_FLASH_StartUpAreaSelect R_FLASH_HP_StartUpAreaSelect
  34. /* BSP_FEATURE_FLASH */
  35. #define FLASH_CF_WRITE_SIZE BSP_FEATURE_FLASH_HP_CF_WRITE_SIZE
  36. #define FLASH_DF_WRITE_SIZE BSP_FEATURE_FLASH_HP_DF_WRITE_SIZE
  37. #else /* FLASH LP */
  38. /* FLASH API */
  39. #define R_FLASH_Open R_FLASH_LP_Open
  40. #define R_FLASH_Reset R_FLASH_LP_Reset
  41. #define R_FLASH_Write R_FLASH_LP_Write
  42. #define R_FLASH_Erase R_FLASH_LP_Erase
  43. #define R_FLASH_StartUpAreaSelect R_FLASH_LP_StartUpAreaSelect
  44. /* BSP_FEATURE_FLASH */
  45. #define FLASH_CF_WRITE_SIZE BSP_FEATURE_FLASH_LP_CF_WRITE_SIZE
  46. #endif
  47. int _flash_init(void)
  48. {
  49. fsp_err_t err = FSP_SUCCESS;
  50. /* Open Flash_HP */
  51. err = R_FLASH_Open(&g_flash_ctrl, &g_flash_cfg);
  52. /* Handle Error */
  53. if (FSP_SUCCESS != err)
  54. {
  55. LOG_E("\r\n Flah_HP_Open API failed");
  56. }
  57. /* Setup Default Block 0 as Startup Setup Block */
  58. err = R_FLASH_StartUpAreaSelect(&g_flash_ctrl, FLASH_STARTUP_AREA_BLOCK0, true);
  59. if (err != FSP_SUCCESS)
  60. {
  61. LOG_E("\r\n Flah_HP_StartUpAreaSelect API failed");
  62. }
  63. return 0;
  64. }
  65. /**
  66. * Read data from flash.
  67. * @note This operation's units is word.
  68. *
  69. * @param addr flash address
  70. * @param buf buffer to store read data
  71. * @param size read bytes size
  72. *
  73. * @return result
  74. */
  75. int _flash_read(rt_uint32_t addr, rt_uint8_t *buf, size_t size)
  76. {
  77. size_t i;
  78. for (i = 0; i < size; i++, buf++, addr++)
  79. {
  80. *buf = *(rt_uint8_t *) addr;
  81. }
  82. return size;
  83. }
  84. /**
  85. * Write data to flash.
  86. * @note This operation's units is word.
  87. * @note This operation must after erase. @see flash_erase.
  88. *
  89. * @param addr flash address
  90. * @param buf the write data buffer
  91. * @param size write bytes size
  92. *
  93. * @return result
  94. */
  95. int _flash_write(rt_uint32_t addr, const rt_uint8_t *buf, size_t size)
  96. {
  97. rt_err_t result = RT_EOK;
  98. rt_base_t level;
  99. fsp_err_t err = FSP_SUCCESS;
  100. size_t written_size = 0;
  101. if (size % FLASH_CF_WRITE_SIZE)
  102. {
  103. LOG_E("Flash Write size must be an integer multiple of %d", FLASH_CF_WRITE_SIZE);
  104. return -RT_EINVAL;
  105. }
  106. while (written_size < size)
  107. {
  108. level = rt_hw_interrupt_disable();
  109. R_FLASH_Reset(&g_flash_ctrl);
  110. /* Write code flash data*/
  111. err = R_FLASH_Write(&g_flash_ctrl, (uint32_t)(buf + written_size), addr + written_size, FLASH_CF_WRITE_SIZE);
  112. rt_hw_interrupt_enable(level);
  113. /* Error Handle */
  114. if (FSP_SUCCESS != err)
  115. {
  116. LOG_E("Write API failed");
  117. return -RT_EIO;
  118. }
  119. written_size += FLASH_CF_WRITE_SIZE;
  120. }
  121. if (result != RT_EOK)
  122. {
  123. return result;
  124. }
  125. return size;
  126. }
  127. /**
  128. * Write data to flash.
  129. * @note This operation's units is word.
  130. * @note This operation must after erase. @see flash_erase.
  131. *
  132. * @param addr flash address
  133. * @param buf the write data buffer
  134. * @param size write bytes size
  135. *
  136. * @return result
  137. */
  138. int _flash_write_df(rt_uint32_t addr, const rt_uint8_t *buf, size_t size)
  139. {
  140. rt_err_t result = RT_EOK;
  141. rt_base_t level;
  142. fsp_err_t err = FSP_SUCCESS;
  143. size_t written_size = 0;
  144. if (size % FLASH_DF_WRITE_SIZE)
  145. {
  146. LOG_E("Flash Write size must be an integer multiple of %d", FLASH_DF_WRITE_SIZE);
  147. return -RT_EINVAL;
  148. }
  149. while (written_size < size)
  150. {
  151. level = rt_hw_interrupt_disable();
  152. R_FLASH_Reset(&g_flash_ctrl);
  153. /* Write code flash data*/
  154. err = R_FLASH_Write(&g_flash_ctrl, (uint32_t)(buf + written_size), addr + written_size, FLASH_DF_WRITE_SIZE);
  155. rt_hw_interrupt_enable(level);
  156. /* Error Handle */
  157. if (FSP_SUCCESS != err)
  158. {
  159. LOG_E("Write API failed");
  160. return -RT_EIO;
  161. }
  162. written_size += FLASH_DF_WRITE_SIZE;
  163. }
  164. if (result != RT_EOK)
  165. {
  166. return result;
  167. }
  168. return size;
  169. }
  170. /**
  171. * Erase data on flash.
  172. * @note This operation is irreversible.
  173. * @note This operation's units is different which on many chips.
  174. *
  175. * @param addr flash address
  176. * @param size erase bytes size
  177. *
  178. * @return result
  179. */
  180. #if BSP_FEATURE_FLASH_HP_VERSION
  181. int _flash_hp0_erase(rt_uint32_t addr, size_t size)
  182. #else
  183. int _flash_lp_erase(rt_uint32_t addr, size_t size)
  184. #endif
  185. {
  186. fsp_err_t err = FSP_SUCCESS;
  187. rt_base_t level;
  188. #if BSP_FEATURE_FLASH_HP_VERSION
  189. if ((addr + size) > BSP_FEATURE_FLASH_HP_CF_REGION0_SIZE)
  190. #else
  191. if ((addr + size) > BSP_ROM_SIZE_BYTES)
  192. #endif
  193. {
  194. LOG_E("ERROR: erase outrange flash size! addr is (0x%p)\n", (void *)(addr + size));
  195. return -RT_EINVAL;
  196. }
  197. if (size < 1)
  198. {
  199. return -RT_EINVAL;
  200. }
  201. level = rt_hw_interrupt_disable();
  202. R_FLASH_Reset(&g_flash_ctrl);
  203. /* Erase Block */
  204. #if BSP_FEATURE_FLASH_HP_VERSION
  205. err = R_FLASH_Erase(&g_flash_ctrl,
  206. RT_ALIGN_DOWN(addr, BSP_FEATURE_FLASH_HP_CF_REGION0_BLOCK_SIZE),
  207. ((size - 1) / BSP_FEATURE_FLASH_HP_CF_REGION0_BLOCK_SIZE + 1));
  208. #else
  209. err = R_FLASH_Erase(&g_flash_ctrl,
  210. RT_ALIGN_DOWN(addr, BSP_FEATURE_FLASH_LP_CF_BLOCK_SIZE),
  211. ((size - 1) / BSP_FEATURE_FLASH_LP_CF_BLOCK_SIZE + 1));
  212. #endif
  213. rt_hw_interrupt_enable(level);
  214. if (err != FSP_SUCCESS)
  215. {
  216. LOG_E("Erase failed:addr (0x%p), size %d", (void *)addr, size);
  217. return -RT_EIO;
  218. }
  219. LOG_D("erase done: addr (0x%p), size %d", (void *)addr, size);
  220. return size;
  221. }
  222. #if BSP_FEATURE_FLASH_HP_VERSION
  223. int _flash_hp1_erase(rt_uint32_t addr, size_t size)
  224. {
  225. fsp_err_t err = FSP_SUCCESS;
  226. rt_base_t level;
  227. if (size < 1)
  228. {
  229. return -RT_EINVAL;
  230. }
  231. level = rt_hw_interrupt_disable();
  232. R_FLASH_Reset(&g_flash_ctrl);
  233. /* Erase Block */
  234. err = R_FLASH_Erase(&g_flash_ctrl, RT_ALIGN_DOWN(addr, BSP_FEATURE_FLASH_HP_CF_REGION1_BLOCK_SIZE), (size - 1) / BSP_FEATURE_FLASH_HP_CF_REGION1_BLOCK_SIZE + 1);
  235. rt_hw_interrupt_enable(level);
  236. if (err != FSP_SUCCESS)
  237. {
  238. LOG_E("Erase API failed");
  239. return -RT_EIO;
  240. }
  241. LOG_D("erase done: addr (0x%p), size %d", (void *)addr, size);
  242. return size;
  243. }
  244. int _flash_hp3_erase(rt_uint32_t addr, size_t size)
  245. {
  246. fsp_err_t err = FSP_SUCCESS;
  247. rt_base_t level;
  248. if (size < 1)
  249. {
  250. return -RT_EINVAL;
  251. }
  252. level = rt_hw_interrupt_disable();
  253. R_FLASH_Reset(&g_flash_ctrl);
  254. /* Erase Block */
  255. err = R_FLASH_Erase(&g_flash_ctrl, RT_ALIGN_DOWN(addr, BSP_FEATURE_FLASH_HP_DF_BLOCK_SIZE), (size - 1) / BSP_FEATURE_FLASH_HP_DF_BLOCK_SIZE + 1);
  256. rt_hw_interrupt_enable(level);
  257. if (err != FSP_SUCCESS)
  258. {
  259. LOG_E("Erase API failed");
  260. return -RT_EIO;
  261. }
  262. LOG_D("erase done: addr (0x%p), size %d", (void *)addr, size);
  263. return size;
  264. }
  265. #endif
  266. #if defined(RT_USING_FAL)
  267. #define FLASH_START_ADDRESS 0x02000000
  268. #if BSP_FEATURE_FLASH_HP_VERSION
  269. static int fal_flash_hp0_read(long offset, rt_uint8_t *buf, size_t size);
  270. static int fal_flash_hp0_write(long offset, const rt_uint8_t *buf, size_t size);
  271. static int fal_flash_hp0_erase(long offset, size_t size);
  272. static int fal_flash_hp1_read(long offset, rt_uint8_t *buf, size_t size);
  273. static int fal_flash_hp1_write(long offset, const rt_uint8_t *buf, size_t size);
  274. static int fal_flash_hp1_erase(long offset, size_t size);
  275. static int fal_flash_hp3_read(long offset, rt_uint8_t *buf, size_t size);
  276. static int fal_flash_hp3_write(long offset, const rt_uint8_t *buf, size_t size);
  277. static int fal_flash_hp3_erase(long offset, size_t size);
  278. const struct fal_flash_dev _onchip_flash_hp0 =
  279. {
  280. "onchip_flash_hp0",
  281. FLASH_START_ADDRESS,
  282. BSP_FEATURE_FLASH_HP_CF_REGION0_SIZE,
  283. BSP_FEATURE_FLASH_HP_CF_REGION0_BLOCK_SIZE,
  284. {
  285. _flash_init,
  286. fal_flash_hp0_read,
  287. fal_flash_hp0_write,
  288. fal_flash_hp0_erase
  289. },
  290. (BSP_FEATURE_FLASH_HP_CF_WRITE_SIZE * 8)
  291. };
  292. const struct fal_flash_dev _onchip_flash_hp1 =
  293. {
  294. "onchip_flash_hp1",
  295. (FLASH_START_ADDRESS + BSP_FEATURE_FLASH_HP_CF_REGION0_SIZE),
  296. (BSP_ROM_SIZE_BYTES - BSP_FEATURE_FLASH_HP_CF_REGION0_SIZE),
  297. BSP_FEATURE_FLASH_HP_CF_REGION1_BLOCK_SIZE,
  298. {
  299. _flash_init,
  300. fal_flash_hp1_read,
  301. fal_flash_hp1_write,
  302. fal_flash_hp1_erase
  303. },
  304. (BSP_FEATURE_FLASH_HP_CF_WRITE_SIZE * 8)
  305. };
  306. const struct fal_flash_dev _onchip_flash_hp3 =
  307. {
  308. "onchip_flash_hp3",
  309. BSP_FEATURE_FLASH_DATA_FLASH_START,
  310. BSP_DATA_FLASH_SIZE_BYTES,
  311. BSP_FEATURE_FLASH_HP_DF_BLOCK_SIZE,
  312. {
  313. _flash_init,
  314. fal_flash_hp3_read,
  315. fal_flash_hp3_write,
  316. fal_flash_hp3_erase
  317. },
  318. (BSP_FEATURE_FLASH_HP_DF_WRITE_SIZE * 8)
  319. };
  320. /* code flash region0 */
  321. static int fal_flash_hp0_read(long offset, rt_uint8_t *buf, size_t size)
  322. {
  323. return _flash_read(_onchip_flash_hp0.addr + offset, buf, size);
  324. }
  325. static int fal_flash_hp0_write(long offset, const rt_uint8_t *buf, size_t size)
  326. {
  327. return _flash_write(_onchip_flash_hp0.addr + offset, buf, size);
  328. }
  329. static int fal_flash_hp0_erase(long offset, size_t size)
  330. {
  331. return _flash_hp0_erase(_onchip_flash_hp0.addr + offset, size);
  332. }
  333. /* code flash region1 */
  334. static int fal_flash_hp1_read(long offset, rt_uint8_t *buf, size_t size)
  335. {
  336. return _flash_read(_onchip_flash_hp1.addr + offset, buf, size);
  337. }
  338. static int fal_flash_hp1_write(long offset, const rt_uint8_t *buf, size_t size)
  339. {
  340. return _flash_write(_onchip_flash_hp1.addr + offset, buf, size);
  341. }
  342. static int fal_flash_hp1_erase(long offset, size_t size)
  343. {
  344. return _flash_hp1_erase(_onchip_flash_hp1.addr + offset, size);
  345. }
  346. /* data flash region1 */
  347. static int fal_flash_hp3_read(long offset, rt_uint8_t *buf, size_t size)
  348. {
  349. return _flash_read(_onchip_flash_hp3.addr + offset, buf, size);
  350. }
  351. static int fal_flash_hp3_write(long offset, const rt_uint8_t *buf, size_t size)
  352. {
  353. return _flash_write_df(_onchip_flash_hp3.addr + offset, buf, size);
  354. }
  355. static int fal_flash_hp3_erase(long offset, size_t size)
  356. {
  357. return _flash_hp3_erase(_onchip_flash_hp3.addr + offset, size);
  358. }
  359. #else /* flash lp code flash */
  360. static int fal_flash_lp_read(long offset, rt_uint8_t *buf, size_t size);
  361. static int fal_flash_lp_write(long offset, const rt_uint8_t *buf, size_t size);
  362. static int fal_flash_lp_erase(long offset, size_t size);
  363. const struct fal_flash_dev _onchip_flash_lp =
  364. {
  365. "onchip_flash_lp",
  366. FLASH_START_ADDRESS,
  367. BSP_ROM_SIZE_BYTES,
  368. BSP_FEATURE_FLASH_LP_CF_BLOCK_SIZE,
  369. {
  370. _flash_init,
  371. fal_flash_lp_read,
  372. fal_flash_lp_write,
  373. fal_flash_lp_erase
  374. },
  375. (BSP_FEATURE_FLASH_LP_CF_WRITE_SIZE * 8)
  376. };
  377. static int fal_flash_lp_read(long offset, rt_uint8_t *buf, size_t size)
  378. {
  379. return _flash_read(_onchip_flash_lp.addr + offset, buf, size);
  380. }
  381. static int fal_flash_lp_write(long offset, const rt_uint8_t *buf, size_t size)
  382. {
  383. return _flash_write(_onchip_flash_lp.addr + offset, buf, size);
  384. }
  385. static int fal_flash_lp_erase(long offset, size_t size)
  386. {
  387. return _flash_lp_erase(_onchip_flash_lp.addr + offset, size);
  388. }
  389. #endif
  390. int flash_test(void)
  391. {
  392. #if BSP_FEATURE_FLASH_HP_VERSION
  393. #define TEST_OFF (_onchip_flash_hp1.len - BSP_FEATURE_FLASH_HP_CF_REGION1_BLOCK_SIZE)
  394. #else
  395. #define TEST_OFF (_onchip_flash_lp.len - BSP_FEATURE_FLASH_LP_CF_BLOCK_SIZE)
  396. #endif
  397. const struct fal_partition *param;
  398. uint8_t write_buffer[FLASH_CF_WRITE_SIZE] = {0};
  399. uint8_t read_buffer[FLASH_CF_WRITE_SIZE] = {0};
  400. /* Set write buffer, clear read buffer */
  401. for (uint8_t index = 0; index < FLASH_CF_WRITE_SIZE; index++)
  402. {
  403. write_buffer[index] = index;
  404. read_buffer[index] = 0;
  405. }
  406. fal_init();
  407. #if BSP_FEATURE_FLASH_HP_VERSION
  408. param = fal_partition_find("param");
  409. #else
  410. param = fal_partition_find("app");
  411. #endif
  412. if (param == RT_NULL)
  413. {
  414. LOG_E("not find partition param!");
  415. return -1;
  416. }
  417. LOG_I("Erase Start...");
  418. #if BSP_FEATURE_FLASH_HP_VERSION
  419. fal_partition_erase(param, TEST_OFF, BSP_FEATURE_FLASH_HP_CF_REGION1_BLOCK_SIZE);
  420. #else
  421. fal_partition_erase(param, TEST_OFF, BSP_FEATURE_FLASH_LP_CF_BLOCK_SIZE);
  422. #endif
  423. LOG_I("Erase succeeded!");
  424. LOG_I("Write Start...");
  425. fal_partition_write(param, TEST_OFF, write_buffer, sizeof(write_buffer));
  426. LOG_I("Write succeeded!");
  427. LOG_I("Read Start...");
  428. fal_partition_read(param, TEST_OFF, read_buffer, FLASH_CF_WRITE_SIZE);
  429. LOG_I("Read succeeded!");
  430. for (int i = 0; i < FLASH_CF_WRITE_SIZE; i++)
  431. {
  432. if (read_buffer[i] != write_buffer[i])
  433. {
  434. LOG_E("Data verification failed!");
  435. return -1;
  436. }
  437. }
  438. LOG_I("Data verification succeeded!");
  439. return 0;
  440. }
  441. MSH_CMD_EXPORT(flash_test, "drv flash test.");
  442. #endif