fal_flash_stm32f2_port.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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-01-26 armink the first version
  9. */
  10. #include <fal.h>
  11. #include <stm32f2xx.h>
  12. /* base address of the flash sectors */
  13. #define ADDR_FLASH_SECTOR_0 ((uint32_t)0x08000000) /* Base address of Sector 0, 16 K bytes */
  14. #define ADDR_FLASH_SECTOR_1 ((uint32_t)0x08004000) /* Base address of Sector 1, 16 K bytes */
  15. #define ADDR_FLASH_SECTOR_2 ((uint32_t)0x08008000) /* Base address of Sector 2, 16 K bytes */
  16. #define ADDR_FLASH_SECTOR_3 ((uint32_t)0x0800C000) /* Base address of Sector 3, 16 K bytes */
  17. #define ADDR_FLASH_SECTOR_4 ((uint32_t)0x08010000) /* Base address of Sector 4, 64 K bytes */
  18. #define ADDR_FLASH_SECTOR_5 ((uint32_t)0x08020000) /* Base address of Sector 5, 128 K bytes */
  19. #define ADDR_FLASH_SECTOR_6 ((uint32_t)0x08040000) /* Base address of Sector 6, 128 K bytes */
  20. #define ADDR_FLASH_SECTOR_7 ((uint32_t)0x08060000) /* Base address of Sector 7, 128 K bytes */
  21. #define ADDR_FLASH_SECTOR_8 ((uint32_t)0x08080000) /* Base address of Sector 8, 128 K bytes */
  22. #define ADDR_FLASH_SECTOR_9 ((uint32_t)0x080A0000) /* Base address of Sector 9, 128 K bytes */
  23. #define ADDR_FLASH_SECTOR_10 ((uint32_t)0x080C0000) /* Base address of Sector 10, 128 K bytes */
  24. #define ADDR_FLASH_SECTOR_11 ((uint32_t)0x080E0000) /* Base address of Sector 11, 128 K bytes */
  25. /**
  26. * Get the sector of a given address
  27. *
  28. * @param address flash address
  29. *
  30. * @return The sector of a given address
  31. */
  32. static uint32_t stm32_get_sector(uint32_t address)
  33. {
  34. uint32_t sector = 0;
  35. if ((address < ADDR_FLASH_SECTOR_1) && (address >= ADDR_FLASH_SECTOR_0))
  36. {
  37. sector = FLASH_Sector_0;
  38. }
  39. else if ((address < ADDR_FLASH_SECTOR_2) && (address >= ADDR_FLASH_SECTOR_1))
  40. {
  41. sector = FLASH_Sector_1;
  42. }
  43. else if ((address < ADDR_FLASH_SECTOR_3) && (address >= ADDR_FLASH_SECTOR_2))
  44. {
  45. sector = FLASH_Sector_2;
  46. }
  47. else if ((address < ADDR_FLASH_SECTOR_4) && (address >= ADDR_FLASH_SECTOR_3))
  48. {
  49. sector = FLASH_Sector_3;
  50. }
  51. else if ((address < ADDR_FLASH_SECTOR_5) && (address >= ADDR_FLASH_SECTOR_4))
  52. {
  53. sector = FLASH_Sector_4;
  54. }
  55. else if ((address < ADDR_FLASH_SECTOR_6) && (address >= ADDR_FLASH_SECTOR_5))
  56. {
  57. sector = FLASH_Sector_5;
  58. }
  59. else if ((address < ADDR_FLASH_SECTOR_7) && (address >= ADDR_FLASH_SECTOR_6))
  60. {
  61. sector = FLASH_Sector_6;
  62. }
  63. else if ((address < ADDR_FLASH_SECTOR_8) && (address >= ADDR_FLASH_SECTOR_7))
  64. {
  65. sector = FLASH_Sector_7;
  66. }
  67. else if ((address < ADDR_FLASH_SECTOR_9) && (address >= ADDR_FLASH_SECTOR_8))
  68. {
  69. sector = FLASH_Sector_8;
  70. }
  71. else if ((address < ADDR_FLASH_SECTOR_10) && (address >= ADDR_FLASH_SECTOR_9))
  72. {
  73. sector = FLASH_Sector_9;
  74. }
  75. else if ((address < ADDR_FLASH_SECTOR_11) && (address >= ADDR_FLASH_SECTOR_10))
  76. {
  77. sector = FLASH_Sector_10;
  78. }
  79. else
  80. {
  81. sector = FLASH_Sector_11;
  82. }
  83. return sector;
  84. }
  85. /**
  86. * Get the sector size
  87. *
  88. * @param sector sector
  89. *
  90. * @return sector size
  91. */
  92. static uint32_t stm32_get_sector_size(uint32_t sector) {
  93. assert(IS_FLASH_SECTOR(sector));
  94. switch (sector) {
  95. case FLASH_Sector_0: return 16 * 1024;
  96. case FLASH_Sector_1: return 16 * 1024;
  97. case FLASH_Sector_2: return 16 * 1024;
  98. case FLASH_Sector_3: return 16 * 1024;
  99. case FLASH_Sector_4: return 64 * 1024;
  100. case FLASH_Sector_5: return 128 * 1024;
  101. case FLASH_Sector_6: return 128 * 1024;
  102. case FLASH_Sector_7: return 128 * 1024;
  103. case FLASH_Sector_8: return 128 * 1024;
  104. case FLASH_Sector_9: return 128 * 1024;
  105. case FLASH_Sector_10: return 128 * 1024;
  106. case FLASH_Sector_11: return 128 * 1024;
  107. default : return 128 * 1024;
  108. }
  109. }
  110. static int init(void)
  111. {
  112. /* do nothing now */
  113. }
  114. static int read(long offset, uint8_t *buf, size_t size)
  115. {
  116. size_t i;
  117. uint32_t addr = stm32f2_onchip_flash.addr + offset;
  118. for (i = 0; i < size; i++, addr++, buf++)
  119. {
  120. *buf = *(uint8_t *) addr;
  121. }
  122. return size;
  123. }
  124. static int write(long offset, const uint8_t *buf, size_t size)
  125. {
  126. size_t i;
  127. uint32_t read_data;
  128. uint32_t addr = stm32f2_onchip_flash.addr + offset;
  129. FLASH_Unlock();
  130. FLASH_ClearFlag(
  131. FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR
  132. | FLASH_FLAG_PGSERR);
  133. for (i = 0; i < size; i++, buf++, addr++)
  134. {
  135. /* write data */
  136. FLASH_ProgramByte(addr, *buf);
  137. read_data = *(uint8_t *) addr;
  138. /* check data */
  139. if (read_data != *buf)
  140. {
  141. return -1;
  142. }
  143. }
  144. FLASH_Lock();
  145. return size;
  146. }
  147. static int erase(long offset, size_t size)
  148. {
  149. FLASH_Status flash_status;
  150. size_t erased_size = 0;
  151. uint32_t cur_erase_sector;
  152. uint32_t addr = stm32f2_onchip_flash.addr + offset;
  153. /* start erase */
  154. FLASH_Unlock();
  155. FLASH_ClearFlag(
  156. FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR
  157. | FLASH_FLAG_PGSERR);
  158. /* it will stop when erased size is greater than setting size */
  159. while (erased_size < size)
  160. {
  161. cur_erase_sector = stm32_get_sector(addr + erased_size);
  162. flash_status = FLASH_EraseSector(cur_erase_sector, VoltageRange_3);
  163. if (flash_status != FLASH_COMPLETE)
  164. {
  165. return -1;
  166. }
  167. erased_size += stm32_get_sector_size(cur_erase_sector);
  168. }
  169. FLASH_Lock();
  170. return size;
  171. }
  172. const struct fal_flash_dev stm32f2_onchip_flash =
  173. {
  174. .name = "stm32_onchip",
  175. .addr = 0x08000000,
  176. .len = 1024*1024,
  177. .blk_size = 128*1024,
  178. .ops = {init, read, write, erase},
  179. .write_gran = 8
  180. };