Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

208 Zeilen
5.4 KiB

  1. /** @defgroup flash_file FLASH peripheral API
  2. *
  3. * @ingroup peripheral_apis
  4. *
  5. * @brief <b>libopencm3 STM32L4xx FLASH</b>
  6. *
  7. * @version 1.0.0
  8. *
  9. * Benjamin Levine <benjamin@jesco.karoo.co.uk>
  10. *
  11. * @date 12 February 2016
  12. *
  13. * This library supports the FLASH memory controller in the STM32L4
  14. * series of ARM Cortex Microcontrollers by ST Microelectronics.
  15. *
  16. * For the STM32L4xx, accessing FLASH memory is described briefly in
  17. * section 3 of the STM32L4x6 Reference Manual.
  18. *
  19. * LGPL License Terms @ref lgpl_license
  20. */
  21. /*
  22. * This file is part of the libopencm3 project.
  23. *
  24. * Copyright (C) 2016 Benjamin Levine <benjamin@jesco.karoo.co.uk>
  25. *
  26. * This library is free software: you can redistribute it and/or modify
  27. * it under the terms of the GNU Lesser General Public License as published by
  28. * the Free Software Foundation, either version 3 of the License, or
  29. * (at your option) any later version.
  30. *
  31. * This library is distributed in the hope that it will be useful,
  32. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34. * GNU Lesser General Public License for more details.
  35. *
  36. * You should have received a copy of the GNU Lesser General Public License
  37. * along with this library. If not, see <http://www.gnu.org/licenses/>.
  38. */
  39. /**@{*/
  40. #include <libopencm3/stm32/flash.h>
  41. /** @brief Wait until Last Operation has Ended
  42. * This loops indefinitely until an operation (write or erase) has completed
  43. * by testing the busy flag.
  44. */
  45. void flash_wait_for_last_operation(void)
  46. {
  47. while ((FLASH_SR & FLASH_SR_BSY) == FLASH_SR_BSY);
  48. }
  49. /** @brief Clear the Programming Sequence Error Flag
  50. * This flag is set when incorrect programming configuration has been made.
  51. */
  52. void flash_clear_pgserr_flag(void)
  53. {
  54. FLASH_SR |= FLASH_SR_PGSERR;
  55. }
  56. /** Clear programming size error flag */
  57. void flash_clear_size_flag(void)
  58. {
  59. FLASH_SR |= FLASH_SR_SIZERR;
  60. }
  61. /** @brief Clear the Programming Alignment Error Flag
  62. */
  63. void flash_clear_pgaerr_flag(void)
  64. {
  65. FLASH_SR |= FLASH_SR_PGAERR;
  66. }
  67. /** @brief Clear the Write Protect Error Flag
  68. */
  69. void flash_clear_wrperr_flag(void)
  70. {
  71. FLASH_SR |= FLASH_SR_WRPERR;
  72. }
  73. /** @brief Clear the Programming Error Status Flag
  74. */
  75. void flash_clear_progerr_flag(void)
  76. {
  77. FLASH_SR |= FLASH_SR_PROGERR;
  78. }
  79. /** @brief Clear All Status Flags
  80. * Program error, end of operation, write protect error, busy.
  81. */
  82. void flash_clear_status_flags(void)
  83. {
  84. flash_clear_pgserr_flag();
  85. flash_clear_size_flag();
  86. flash_clear_pgaerr_flag();
  87. flash_clear_wrperr_flag();
  88. flash_clear_progerr_flag();
  89. flash_clear_eop_flag();
  90. }
  91. /** @brief Lock the Option Byte Access
  92. * This disables write access to the option bytes. It is locked by default on
  93. * reset.
  94. */
  95. void flash_lock_option_bytes(void)
  96. {
  97. FLASH_CR |= FLASH_CR_OPTLOCK;
  98. }
  99. /** @brief Program a 64 bit word to FLASH
  100. *
  101. * This performs all operations necessary to program a 64 bit word to FLASH memory.
  102. * The program error flag should be checked separately for the event that memory
  103. * was not properly erased.
  104. *
  105. * @param[in] address Starting address in Flash.
  106. * @param[in] data Double word to write
  107. */
  108. void flash_program_double_word(uint32_t address, uint64_t data)
  109. {
  110. /* Ensure that all flash operations are complete. */
  111. flash_wait_for_last_operation();
  112. /* Enable writes to flash. */
  113. FLASH_CR |= FLASH_CR_PG;
  114. /* Program the each word separately. */
  115. MMIO32(address) = (uint32_t)data;
  116. MMIO32(address+4) = (uint32_t)(data >> 32);
  117. /* Wait for the write to complete. */
  118. flash_wait_for_last_operation();
  119. /* Disable writes to flash. */
  120. FLASH_CR &= ~FLASH_CR_PG;
  121. }
  122. /** @brief Program a Data Block to FLASH
  123. * This programs an arbitrary length data block to FLASH memory.
  124. * The program error flag should be checked separately for the event that
  125. * memory was not properly erased.
  126. * @param[in] address Starting address in Flash.
  127. * @param[in] data Pointer to start of data block.
  128. * @param[in] len Length of data block in bytes (multiple of 8).
  129. */
  130. void flash_program(uint32_t address, uint8_t *data, uint32_t len)
  131. {
  132. for (uint32_t i = 0; i < len; i += 8) {
  133. flash_program_double_word(address+i, *(uint64_t*)(data + i));
  134. }
  135. }
  136. /** @brief Erase a page of FLASH
  137. * @param[in] page (0 - 255 for bank 1, 256-511 for bank 2)
  138. */
  139. void flash_erase_page(uint32_t page)
  140. {
  141. flash_wait_for_last_operation();
  142. /* page and bank are contiguous bits */
  143. FLASH_CR &= ~((FLASH_CR_PNB_MASK << FLASH_CR_PNB_SHIFT) | FLASH_CR_BKER);
  144. if (page > 255) {
  145. FLASH_CR |= FLASH_CR_BKER;
  146. }
  147. FLASH_CR |= page << FLASH_CR_PNB_SHIFT;
  148. FLASH_CR |= FLASH_CR_PER;
  149. FLASH_CR |= FLASH_CR_START;
  150. flash_wait_for_last_operation();
  151. FLASH_CR &= ~FLASH_CR_PER;
  152. }
  153. /** @brief Erase All FLASH
  154. * This performs all operations necessary to erase all sectors in the FLASH
  155. * memory.
  156. */
  157. void flash_erase_all_pages(void)
  158. {
  159. flash_wait_for_last_operation();
  160. FLASH_CR |= FLASH_CR_MER1 | FLASH_CR_MER2;
  161. FLASH_CR |= FLASH_CR_START;
  162. flash_wait_for_last_operation();
  163. FLASH_CR &= ~FLASH_CR_MER1 & ~FLASH_CR_MER2;
  164. }
  165. /** @brief Program the Option Bytes
  166. * This performs all operations necessary to program the option bytes.
  167. * The option bytes do not need to be erased first.
  168. * @param[in] data value to be programmed.
  169. */
  170. void flash_program_option_bytes(uint32_t data)
  171. {
  172. flash_wait_for_last_operation();
  173. if (FLASH_CR & FLASH_CR_OPTLOCK) {
  174. flash_unlock_option_bytes();
  175. }
  176. FLASH_OPTR = data;
  177. FLASH_CR |= FLASH_CR_OPTSTRT;
  178. flash_wait_for_last_operation();
  179. }
  180. /**@}*/