fal_flash_stm32f2_port.c 5.7 KB

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