drv_flash.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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-08-05 mazhiyuan first version
  9. */
  10. #include <rtthread.h>
  11. #include "hal_flash.h"
  12. #include "drv_flash.h"
  13. #define OCFLASH_BLK_SIZE 1024
  14. #define OCFLASH_LEN 1024*512
  15. #define OCFLASH_ADDR 0x08000000
  16. #ifdef OCFLASH_USE_FAL
  17. #include <fal.h>
  18. #endif
  19. #ifdef OCFLASH_USE_LFS
  20. #include <dfs_fs.h>
  21. #define FS_PARTITION_NAME "filesystem"
  22. #endif
  23. static int init(void)
  24. {
  25. /* do nothing now */
  26. return 0;
  27. }
  28. static int read(long offset, uint8_t *buf, size_t size)
  29. {
  30. size_t i;
  31. uint32_t addr = OCFLASH_ADDR + offset;
  32. for (i = 0; i < size; i++)
  33. {
  34. *buf = *(__IO uint8_t *)addr;
  35. buf++;
  36. addr++;
  37. }
  38. return size;
  39. }
  40. static int write(long offset, const uint8_t *buf, size_t size)
  41. {
  42. size_t i;
  43. uint32_t addr = OCFLASH_ADDR + offset;
  44. FLASH->KEYR = 0x45670123;
  45. FLASH->KEYR = 0xCDEF89AB;
  46. FLASH->SR = 0x00000001 | 0x00000004 | 0x00000010;
  47. FLASH->CR |= 0x1;
  48. i = 0;
  49. while (i < size)
  50. {
  51. *(__IO uint16_t *)addr = *buf | *(buf + 1) << 8;
  52. addr = addr + 2;
  53. buf += 2;
  54. i += 2;
  55. }
  56. //Lock flash
  57. FLASH->CR |= 0x00000080;
  58. return size;
  59. }
  60. static int erase(long offset, size_t size)
  61. {
  62. int len;
  63. RT_ASSERT(offset > 0 && offset < OCFLASH_LEN);
  64. int page_addr = (offset >> 10) << 10;
  65. len = size + (offset - page_addr);
  66. while (len > 0)
  67. {
  68. FLASH_Unlock();
  69. FLASH_ErasePage(page_addr);
  70. FLASH_Lock();
  71. len -= OCFLASH_BLK_SIZE;
  72. page_addr += OCFLASH_BLK_SIZE;
  73. }
  74. return size;
  75. }
  76. #ifdef OCFLASH_USE_FAL
  77. const struct fal_flash_dev mm32_onchip_flash =
  78. {
  79. .name = "mm32_onchip",
  80. .addr = 0x08000000,
  81. .len = 1024 * 512,
  82. .blk_size = 1024,
  83. .ops = {init, read, write, erase},
  84. .write_gran = 2
  85. };
  86. #endif
  87. int flash_init(void)
  88. {
  89. #ifdef OCFLASH_USE_FAL
  90. fal_init();
  91. #endif
  92. #ifdef OCFLASH_USE_LFS
  93. struct rt_device *flash_dev = fal_mtd_nor_device_create(FS_PARTITION_NAME);
  94. if (flash_dev == NULL)
  95. {
  96. rt_kprintf("Can't create a mtd device on '%s' partition.\n", FS_PARTITION_NAME);
  97. }
  98. else
  99. {
  100. rt_kprintf("Create a mtd device on the %s partition of flash successful.\n", FS_PARTITION_NAME);
  101. }
  102. if (rt_device_find(FS_PARTITION_NAME) != RT_NULL)
  103. {
  104. if (dfs_mount(FS_PARTITION_NAME, "/", "lfs", 0, 0) == RT_EOK)
  105. {
  106. rt_kprintf("onchip lfs filesystem mount to '/'\n");
  107. }
  108. else
  109. {
  110. dfs_mkfs("lfs", FS_PARTITION_NAME);
  111. if (dfs_mount(FS_PARTITION_NAME, "/", "lfs", 0, 0) == RT_EOK)
  112. {
  113. rt_kprintf("onchip lfs filesystem mount to '/' with mkfs\n");
  114. }
  115. else
  116. {
  117. rt_kprintf("onchip lfs filesystem mount to '/' failed!\n");
  118. }
  119. }
  120. }
  121. else
  122. {
  123. rt_kprintf("find filesystem portion failed\r\n");
  124. }
  125. #endif
  126. return 0;
  127. }
  128. INIT_APP_EXPORT(flash_init);