您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

1年前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /** @defgroup flash_file FLASH peripheral API
  2. *
  3. * @ingroup peripheral_apis
  4. *
  5. * @brief <b>libopencm3 STM32F1xx FLASH Memory</b>
  6. *
  7. * @version 1.0.0
  8. *
  9. * @author @htmlonly &copy; @endhtmlonly 2010
  10. * Thomas Otto <tommi@viadmin.org>
  11. * @author @htmlonly &copy; @endhtmlonly 2010
  12. * Mark Butler <mbutler@physics.otago.ac.nz>
  13. *
  14. * @date 14 January 2014
  15. *
  16. * For the STM32F1xx, accessing FLASH memory is described briefly in
  17. * section 3.3.3 of the STM32F10x Reference Manual.
  18. * For detailed programming information see:
  19. * PM0075 programming manual: STM32F10xxx Flash programming
  20. * August 2010, Doc ID 17863 Rev 1
  21. * https://github.com/libopencm3/libopencm3-archive/blob/master/st_micro/CD00283419.pdf
  22. *
  23. * FLASH memory may be used for data storage as well as code, and may be
  24. * programmatically modified. Note that for firmware upload the STM32F1xx
  25. * provides a built-in bootloader in system memory that can be entered from a
  26. * running program.
  27. *
  28. * FLASH must first be unlocked before programming. In this module a write to
  29. * FLASH is a blocking operation until the end-of-operation flag is asserted.
  30. *
  31. * @note: don't forget to lock it again when all operations are complete.
  32. *
  33. * For the large memory XL series, with two banks of FLASH, the upper bank is
  34. * accessed with a second set of registers. In principle both banks can be
  35. * written simultaneously, or one read while the other is written. This module
  36. * does not support the simultaneous write feature.
  37. *
  38. * LGPL License Terms @ref lgpl_license
  39. */
  40. /*
  41. * This file is part of the libopencm3 project.
  42. *
  43. * Copyright (C) 2010 Thomas Otto <tommi@viadmin.org>
  44. * Copyright (C) 2010 Mark Butler <mbutler@physics.otago.ac.nz>
  45. *
  46. * This library is free software: you can redistribute it and/or modify
  47. * it under the terms of the GNU Lesser General Public License as published by
  48. * the Free Software Foundation, either version 3 of the License, or
  49. * (at your option) any later version.
  50. *
  51. * This library is distributed in the hope that it will be useful,
  52. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  53. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  54. * GNU Lesser General Public License for more details.
  55. *
  56. * You should have received a copy of the GNU Lesser General Public License
  57. * along with this library. If not, see <http://www.gnu.org/licenses/>.
  58. */
  59. /**@{*/
  60. #include <libopencm3/stm32/desig.h>
  61. #include <libopencm3/stm32/flash.h>
  62. /*---------------------------------------------------------------------------*/
  63. /** @brief Enable the FLASH Half Cycle Mode
  64. This mode is used for power saving during read access. It is disabled by default
  65. on reset.
  66. Note carefully the clock restrictions under which the half cycle mode may be
  67. enabled or disabled. This mode may only be used while the clock is running at
  68. 8MHz. See the reference manual for details.
  69. */
  70. void flash_halfcycle_enable(void)
  71. {
  72. FLASH_ACR |= FLASH_ACR_HLFCYA;
  73. }
  74. /*---------------------------------------------------------------------------*/
  75. /** @brief Disable the FLASH Half Cycle Mode
  76. */
  77. void flash_halfcycle_disable(void)
  78. {
  79. FLASH_ACR &= ~FLASH_ACR_HLFCYA;
  80. }
  81. /*---------------------------------------------------------------------------*/
  82. /** @brief Unlock the Flash Program and Erase Controller, upper Bank
  83. This enables write access to the upper bank of the Flash memory in XL devices.
  84. It is locked by default on reset.
  85. */
  86. void flash_unlock_upper(void)
  87. {
  88. if (desig_get_flash_size() > 512) {
  89. /* Clear the unlock state. */
  90. FLASH_CR2 |= FLASH_CR_LOCK;
  91. /* Authorize the FPEC access. */
  92. FLASH_KEYR2 = FLASH_KEYR_KEY1;
  93. FLASH_KEYR2 = FLASH_KEYR_KEY2;
  94. }
  95. }
  96. /*---------------------------------------------------------------------------*/
  97. /** @brief Lock the Flash Program and Erase Controller, upper Bank
  98. Used to prevent spurious writes to FLASH.
  99. */
  100. void flash_lock_upper(void)
  101. {
  102. FLASH_CR2 |= FLASH_CR_LOCK;
  103. }
  104. /*---------------------------------------------------------------------------*/
  105. /** @brief Clear the Programming Error Status Flag, upper Bank
  106. */
  107. void flash_clear_pgerr_flag_upper(void)
  108. {
  109. if (desig_get_flash_size() > 512) {
  110. FLASH_SR2 |= FLASH_SR_PGERR;
  111. }
  112. }
  113. /*---------------------------------------------------------------------------*/
  114. /** @brief Clear the End of Operation Status Flag, upper Bank
  115. */
  116. void flash_clear_eop_flag_upper(void)
  117. {
  118. if (desig_get_flash_size() > 512) {
  119. FLASH_SR2 |= FLASH_SR_EOP;
  120. }
  121. }
  122. /*---------------------------------------------------------------------------*/
  123. /** @brief Clear the Write Protect Error Status Flag, upper Bank
  124. */
  125. void flash_clear_wrprterr_flag_upper(void)
  126. {
  127. if (desig_get_flash_size() > 512) {
  128. FLASH_SR2 |= FLASH_SR_WRPRTERR;
  129. }
  130. }
  131. /*---------------------------------------------------------------------------*/
  132. /** @brief Clear All Status Flags
  133. Program error, end of operation, write protect error, busy. Both banks cleared.
  134. */
  135. void flash_clear_status_flags(void)
  136. {
  137. flash_clear_pgerr_flag();
  138. flash_clear_eop_flag();
  139. flash_clear_wrprterr_flag();
  140. if (desig_get_flash_size() > 512) {
  141. flash_clear_pgerr_flag_upper();
  142. flash_clear_eop_flag_upper();
  143. flash_clear_wrprterr_flag_upper();
  144. }
  145. }
  146. /*---------------------------------------------------------------------------*/
  147. /** @brief Read All Status Flags
  148. The programming error, end of operation, write protect error and busy flags
  149. are returned in the order of appearance in the status register.
  150. Flags for the upper bank, where appropriate, are combined with those for
  151. the lower bank using bitwise OR, without distinction.
  152. @returns uint32_t. bit 0: busy, bit 2: programming error, bit 4: write protect
  153. error, bit 5: end of operation.
  154. */
  155. uint32_t flash_get_status_flags(void)
  156. {
  157. uint32_t flags = (FLASH_SR & (FLASH_SR_PGERR |
  158. FLASH_SR_EOP |
  159. FLASH_SR_WRPRTERR |
  160. FLASH_SR_BSY));
  161. if (desig_get_flash_size() > 512) {
  162. flags |= (FLASH_SR2 & (FLASH_SR_PGERR |
  163. FLASH_SR_EOP |
  164. FLASH_SR_WRPRTERR |
  165. FLASH_SR_BSY));
  166. }
  167. return flags;
  168. }
  169. /*---------------------------------------------------------------------------*/
  170. /** @brief Program a Half Word to FLASH
  171. This performs all operations necessary to program a 16 bit word to FLASH memory.
  172. The program error flag should be checked separately for the event that memory
  173. was not properly erased.
  174. Status bit polling is used to detect end of operation.
  175. @param[in] address Full address of flash half word to be programmed.
  176. @param[in] data half word to write
  177. */
  178. void flash_program_half_word(uint32_t address, uint16_t data)
  179. {
  180. flash_wait_for_last_operation();
  181. if ((desig_get_flash_size() > 512) && (address >= FLASH_BASE+0x00080000)) {
  182. FLASH_CR2 |= FLASH_CR_PG;
  183. } else {
  184. FLASH_CR |= FLASH_CR_PG;
  185. }
  186. MMIO16(address) = data;
  187. flash_wait_for_last_operation();
  188. if ((desig_get_flash_size() > 512) && (address >= FLASH_BASE+0x00080000)) {
  189. FLASH_CR2 &= ~FLASH_CR_PG;
  190. } else {
  191. FLASH_CR &= ~FLASH_CR_PG;
  192. }
  193. }
  194. /*---------------------------------------------------------------------------*/
  195. /** @brief Erase a Page of FLASH
  196. This performs all operations necessary to erase a page in FLASH memory.
  197. The page should be checked to ensure that it was properly erased. A page must
  198. first be fully erased before attempting to program it.
  199. Note that the page sizes differ between devices. See the reference manual or
  200. the FLASH programming manual for details.
  201. @param[in] page_address Full address of flash page to be erased.
  202. */
  203. void flash_erase_page(uint32_t page_address)
  204. {
  205. flash_wait_for_last_operation();
  206. if ((desig_get_flash_size() > 512)
  207. && (page_address >= FLASH_BASE+0x00080000)) {
  208. FLASH_CR2 |= FLASH_CR_PER;
  209. FLASH_AR2 = page_address;
  210. FLASH_CR2 |= FLASH_CR_STRT;
  211. } else {
  212. FLASH_CR |= FLASH_CR_PER;
  213. FLASH_AR = page_address;
  214. FLASH_CR |= FLASH_CR_STRT;
  215. }
  216. flash_wait_for_last_operation();
  217. if ((desig_get_flash_size() > 512)
  218. && (page_address >= FLASH_BASE+0x00080000)) {
  219. FLASH_CR2 &= ~FLASH_CR_PER;
  220. } else {
  221. FLASH_CR &= ~FLASH_CR_PER;
  222. }
  223. }
  224. /*---------------------------------------------------------------------------*/
  225. /** @brief Erase All FLASH
  226. This performs all operations necessary to erase all user pages in the FLASH
  227. memory. The information block is unaffected.
  228. */
  229. void flash_erase_all_pages(void)
  230. {
  231. flash_wait_for_last_operation();
  232. FLASH_CR |= FLASH_CR_MER; /* Enable mass erase. */
  233. FLASH_CR |= FLASH_CR_STRT; /* Trigger the erase. */
  234. flash_wait_for_last_operation();
  235. FLASH_CR &= ~FLASH_CR_MER; /* Disable mass erase. */
  236. /* Repeat for bank 2 */
  237. FLASH_CR2 |= FLASH_CR_MER;
  238. FLASH_CR2 |= FLASH_CR_STRT;
  239. flash_wait_for_last_operation();
  240. FLASH_CR2 &= ~FLASH_CR_MER;
  241. }
  242. /**@}*/