You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

154 line
4.3 KiB

  1. /** @defgroup flash_file FLASH peripheral API
  2. *
  3. * @ingroup peripheral_apis
  4. *
  5. * @brief <b>libopencm3 GD32F1x0 FLASH</b>
  6. *
  7. * @version 1.0.0
  8. *
  9. * @author @htmlonly &copy; @endhtmlonly 2013
  10. * Frantisek Burian <BuFran@seznam.cz>
  11. *
  12. * @date 14 January 2014
  13. *
  14. * FLASH memory may be used for data storage as well as code, and may be
  15. * programmatically modified. Note that for firmware upload the GD32F1x0
  16. * provides a built-in bootloader in system memory that can be entered from a
  17. * running program.
  18. *
  19. * FLASH must first be unlocked before programming. In this module a write to
  20. * FLASH is a blocking operation until the end-of-operation flag is asserted.
  21. *
  22. * @note: don't forget to lock it again when all operations are complete.
  23. *
  24. * LGPL License Terms @ref lgpl_license
  25. */
  26. /*
  27. * This file is part of the libopencm3 project.
  28. *
  29. * Copyright (C) 2013 Frantisek Burian <BuFran@seznam.cz>
  30. *
  31. * This library is free software: you can redistribute it and/or modify
  32. * it under the terms of the GNU Lesser General Public License as published by
  33. * the Free Software Foundation, either version 3 of the License, or
  34. * (at your option) any later version.
  35. *
  36. * This library is distributed in the hope that it will be useful,
  37. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  38. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  39. * GNU Lesser General Public License for more details.
  40. *
  41. * You should have received a copy of the GNU Lesser General Public License
  42. * along with this library. If not, see <http://www.gnu.org/licenses/>.
  43. */
  44. /**@{*/
  45. #include <libopencm3/gd32/flash.h>
  46. /*---------------------------------------------------------------------------*/
  47. /** @brief Clear All Status Flags
  48. Program error, end of operation, write protect error, busy.
  49. */
  50. void flash_clear_status_flags(void)
  51. {
  52. flash_clear_pgerr_flag();
  53. flash_clear_eop_flag();
  54. flash_clear_wrprterr_flag();
  55. }
  56. /*---------------------------------------------------------------------------*/
  57. /** @brief Read All Status Flags
  58. The programming error, end of operation, write protect error and busy flags
  59. are returned in the order of appearance in the status register.
  60. @returns uint32_t. bit 0: busy, bit 2: programming error, bit 4: write protect
  61. error, bit 5: end of operation.
  62. */
  63. uint32_t flash_get_status_flags(void)
  64. {
  65. return FLASH_SR & (FLASH_SR_PGERR |
  66. FLASH_SR_EOP |
  67. FLASH_SR_WRPRTERR |
  68. FLASH_SR_BSY);
  69. }
  70. /*---------------------------------------------------------------------------*/
  71. /** @brief Program a Half Word to FLASH
  72. This performs all operations necessary to program a 16 bit word to FLASH memory.
  73. The program error flag should be checked separately for the event that memory
  74. was not properly erased.
  75. Status bit polling is used to detect end of operation.
  76. @param[in] address Full address of flash half word to be programmed.
  77. @param[in] data half word to write
  78. */
  79. void flash_program_half_word(uint32_t address, uint16_t data)
  80. {
  81. flash_wait_for_last_operation();
  82. FLASH_CR |= FLASH_CR_PG;
  83. MMIO16(address) = data;
  84. flash_wait_for_last_operation();
  85. FLASH_CR &= ~FLASH_CR_PG;
  86. }
  87. /*---------------------------------------------------------------------------*/
  88. /** @brief Erase a Page of FLASH
  89. This performs all operations necessary to erase a page in FLASH memory.
  90. The page should be checked to ensure that it was properly erased. A page must
  91. first be fully erased before attempting to program it.
  92. Note that the page sizes differ between devices. See the reference manual or
  93. the FLASH programming manual for details.
  94. @param[in] page_address Full address of flash page to be erased.
  95. */
  96. void flash_erase_page(uint32_t page_address)
  97. {
  98. flash_wait_for_last_operation();
  99. FLASH_CR |= FLASH_CR_PER;
  100. FLASH_AR = page_address;
  101. FLASH_CR |= FLASH_CR_STRT;
  102. flash_wait_for_last_operation();
  103. FLASH_CR &= ~FLASH_CR_PER;
  104. }
  105. /*---------------------------------------------------------------------------*/
  106. /** @brief Erase All FLASH
  107. This performs all operations necessary to erase all user pages in the FLASH
  108. memory. The information block is unaffected.
  109. */
  110. void flash_erase_all_pages(void)
  111. {
  112. flash_wait_for_last_operation();
  113. FLASH_CR |= FLASH_CR_MER; /* Enable mass erase. */
  114. FLASH_CR |= FLASH_CR_STRT; /* Trigger the erase. */
  115. flash_wait_for_last_operation();
  116. FLASH_CR &= ~FLASH_CR_MER; /* Disable mass erase. */
  117. }
  118. /**@}*/