drv_flash.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-04-08 wcx1024979076 first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "drv_flash.h"
  13. #ifdef BSP_USING_ON_CHIP_FLASH
  14. #define DBG_LEVEL DBG_LOG
  15. #define LOG_TAG "DRV.FLASH"
  16. #include <rtdbg.h>
  17. #define BFLB_FLASH_START_ADRESS 0x0000000
  18. #define BFLB_FLASH_END_ADDRESS 0x1000000
  19. #define BFLB_FLASH_SIZE 16 * 1024 * 1024
  20. #define BFLB_FLASH_PAGE_SIZE 4 * 1024
  21. int _flash_read(rt_uint32_t addr, rt_uint8_t *buf, size_t size)
  22. {
  23. rt_err_t result = RT_EOK;
  24. if ((addr + size) > BFLB_FLASH_END_ADDRESS)
  25. {
  26. LOG_E("read outrange flash size! addr is (0x%p)", (void *)(addr + size));
  27. return -RT_EINVAL;
  28. }
  29. result = bflb_flash_read(addr - BFLB_FLASH_START_ADRESS, buf, size);
  30. if(result != RT_EOK)
  31. {
  32. LOG_E("READ ERROR result = %d, addr = %d, addr1 = %d, len = %d", result, addr, addr - BFLB_FLASH_START_ADRESS, size);
  33. return -RT_ERROR;
  34. }
  35. else
  36. {
  37. return size;
  38. }
  39. }
  40. int _flash_write(rt_uint32_t addr, const rt_uint8_t *buf, size_t size)
  41. {
  42. rt_err_t result = RT_EOK;
  43. rt_uint32_t end_addr = addr + size;
  44. if (addr % 4 != 0)
  45. {
  46. LOG_E("write addr must be 4-byte alignment");
  47. return -RT_EINVAL;
  48. }
  49. if ((end_addr) > BFLB_FLASH_END_ADDRESS)
  50. {
  51. LOG_E("write outrange flash size! addr is (0x%p)", (void *)(addr + size));
  52. return -RT_EINVAL;
  53. }
  54. result = bflb_flash_write(addr - BFLB_FLASH_START_ADRESS, buf, size);
  55. if(result != RT_EOK)
  56. {
  57. LOG_E("WRITE ERROR result = %d, addr = %d, addr1 = %d, len = %d", result, addr, addr - BFLB_FLASH_START_ADRESS, size);
  58. return -RT_ERROR;
  59. }
  60. else
  61. {
  62. return size;
  63. }
  64. }
  65. int _flash_erase(rt_uint32_t addr, size_t size)
  66. {
  67. rt_err_t result = RT_EOK;
  68. rt_uint32_t end_addr = addr + size;
  69. rt_uint32_t page_addr = 0;
  70. if ((end_addr) > BFLB_FLASH_END_ADDRESS)
  71. {
  72. LOG_E("erase outrange flash size! addr is (0x%p)", (void *)(addr + size));
  73. return -RT_EINVAL;
  74. }
  75. result = bflb_flash_erase(addr - BFLB_FLASH_START_ADRESS, size);
  76. if (result != RT_EOK)
  77. {
  78. LOG_E("ERASE ERROR result = %d, addr = %d, addr1 = %d, size = %d", result, addr, addr - BFLB_FLASH_START_ADRESS, size);
  79. return -RT_ERROR;
  80. }
  81. else
  82. {
  83. return size;
  84. }
  85. }
  86. #ifdef RT_USING_FAL
  87. #include "fal.h"
  88. static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size);
  89. static int fal_flash_write(long offset, const rt_uint8_t *buf, size_t size);
  90. static int fal_flash_erase(long offset, size_t size);
  91. const struct fal_flash_dev _onchip_flash =
  92. {
  93. "bflb_onchip",
  94. BFLB_FLASH_START_ADRESS,
  95. BFLB_FLASH_SIZE,
  96. BFLB_FLASH_PAGE_SIZE,
  97. {
  98. NULL,
  99. fal_flash_read,
  100. fal_flash_write,
  101. fal_flash_erase
  102. }
  103. };
  104. static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size)
  105. {
  106. return _flash_read(_onchip_flash.addr + offset, buf, size);
  107. }
  108. static int fal_flash_write(long offset, const rt_uint8_t *buf, size_t size)
  109. {
  110. return _flash_write(_onchip_flash.addr + offset, buf, size);
  111. }
  112. static int fal_flash_erase(long offset, size_t size)
  113. {
  114. return _flash_erase(_onchip_flash.addr + offset, size);
  115. }
  116. static int rt_hw_on_chip_flash_init(void)
  117. {
  118. fal_init();
  119. return RT_EOK;
  120. }
  121. INIT_COMPONENT_EXPORT(rt_hw_on_chip_flash_init);
  122. #endif /* RT_USING_FAL */
  123. #endif /* BSP_USING_ON_CHIP_FLASH */