drv_flash_f1.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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-12-5 SummerGift first version
  9. */
  10. #include "board.h"
  11. #ifdef BSP_USING_ON_CHIP_FLASH
  12. #include "drv_config.h"
  13. #include "drv_flash.h"
  14. #if defined(PKG_USING_FAL)
  15. #include "fal.h"
  16. #endif
  17. //#define DRV_DEBUG
  18. #define LOG_TAG "drv.flash"
  19. #include <drv_log.h>
  20. /**
  21. * @brief Gets the page of a given address
  22. * @param Addr: Address of the FLASH Memory
  23. * @retval The page of a given address
  24. */
  25. static uint32_t GetPage(uint32_t addr)
  26. {
  27. uint32_t page = 0;
  28. page = RT_ALIGN_DOWN(addr, FLASH_PAGE_SIZE);
  29. return page;
  30. }
  31. /**
  32. * Read data from flash.
  33. * @note This operation's units is word.
  34. *
  35. * @param addr flash address
  36. * @param buf buffer to store read data
  37. * @param size read bytes size
  38. *
  39. * @return result
  40. */
  41. int stm32_flash_read(long offset, rt_uint8_t *buf, size_t size)
  42. {
  43. size_t i;
  44. rt_uint32_t addr = STM32_FLASH_START_ADRESS + offset;
  45. if ((addr + size) > STM32_FLASH_END_ADDRESS)
  46. {
  47. LOG_E("read outrange flash size! addr is (0x%p)", (void *)(addr + size));
  48. return -RT_EINVAL;
  49. }
  50. for (i = 0; i < size; i++, buf++, addr++)
  51. {
  52. *buf = *(rt_uint8_t *) addr;
  53. }
  54. return size;
  55. }
  56. /**
  57. * Write data to flash.
  58. * @note This operation's units is word.
  59. * @note This operation must after erase. @see flash_erase.
  60. *
  61. * @param addr flash address
  62. * @param buf the write data buffer
  63. * @param size write bytes size
  64. *
  65. * @return result
  66. */
  67. int stm32_flash_write(long offset, const rt_uint8_t *buf, size_t size)
  68. {
  69. rt_err_t result = RT_EOK;
  70. rt_uint32_t addr = STM32_FLASH_START_ADRESS + offset;
  71. rt_uint32_t end_addr = addr + size;
  72. if (addr % 4 != 0)
  73. {
  74. LOG_E("write addr must be 4-byte alignment");
  75. return -RT_EINVAL;
  76. }
  77. if (size % 4 != 0)
  78. {
  79. LOG_E("write size must be 4-byte alignment");
  80. return -RT_EINVAL;
  81. }
  82. if ((end_addr) > STM32_FLASH_END_ADDRESS)
  83. {
  84. LOG_E("write outrange flash size! addr is (0x%p)", (void *)(addr + size));
  85. return -RT_EINVAL;
  86. }
  87. HAL_FLASH_Unlock();
  88. while (addr < end_addr)
  89. {
  90. if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, addr, *((rt_uint32_t *)buf)) == HAL_OK)
  91. {
  92. if (*(rt_uint32_t *)addr != *(rt_uint32_t *)buf)
  93. {
  94. result = -RT_ERROR;
  95. break;
  96. }
  97. addr += 4;
  98. buf += 4;
  99. }
  100. else
  101. {
  102. result = -RT_ERROR;
  103. break;
  104. }
  105. }
  106. HAL_FLASH_Lock();
  107. if (result != RT_EOK)
  108. {
  109. return result;
  110. }
  111. return size;
  112. }
  113. /**
  114. * Erase data on flash.
  115. * @note This operation is irreversible.
  116. * @note This operation's units is different which on many chips.
  117. *
  118. * @param addr flash address
  119. * @param size erase bytes size
  120. *
  121. * @return result
  122. */
  123. int stm32_flash_erase(long offset, size_t size)
  124. {
  125. rt_err_t result = RT_EOK;
  126. rt_uint32_t addr = STM32_FLASH_START_ADRESS + offset;
  127. uint32_t PAGEError = 0;
  128. /*Variable used for Erase procedure*/
  129. FLASH_EraseInitTypeDef EraseInitStruct;
  130. if ((addr + size) > STM32_FLASH_END_ADDRESS)
  131. {
  132. LOG_E("ERROR: erase outrange flash size! addr is (0x%p)\n", (void *)(addr + size));
  133. return -RT_EINVAL;
  134. }
  135. HAL_FLASH_Unlock();
  136. /* Fill EraseInit structure*/
  137. EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
  138. EraseInitStruct.PageAddress = GetPage(addr);
  139. EraseInitStruct.NbPages = (size + FLASH_PAGE_SIZE - 1) / FLASH_PAGE_SIZE;
  140. if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK)
  141. {
  142. result = -RT_ERROR;
  143. goto __exit;
  144. }
  145. __exit:
  146. HAL_FLASH_Lock();
  147. if (result != RT_EOK)
  148. {
  149. return result;
  150. }
  151. LOG_D("erase done: addr (0x%p), size %d", (void *)addr, size);
  152. return result;
  153. }
  154. #if defined(PKG_USING_FAL)
  155. const struct fal_flash_dev stm32_onchip_flash = { "onchip_flash", STM32_FLASH_START_ADRESS, STM32_FLASH_SIZE, FLASH_PAGE_SIZE, {NULL, stm32_flash_read, stm32_flash_write, stm32_flash_erase} };
  156. #endif
  157. #endif /* BSP_USING_ON_CHIP_FLASH */