main.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*!
  2. \file main.c
  3. \brief main flash program, write_protection
  4. \version 2017-02-10, V1.0.0, firmware for GD32F30x
  5. \version 2018-10-10, V1.1.0, firmware for GD32F30x
  6. \version 2018-12-25, V2.0.0, firmware for GD32F30x
  7. \version 2020-09-30, V2.1.0, firmware for GD32F30x
  8. */
  9. /*
  10. Copyright (c) 2020, GigaDevice Semiconductor Inc.
  11. Redistribution and use in source and binary forms, with or without modification,
  12. are permitted provided that the following conditions are met:
  13. 1. Redistributions of source code must retain the above copyright notice, this
  14. list of conditions and the following disclaimer.
  15. 2. Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or other materials provided with the distribution.
  18. 3. Neither the name of the copyright holder nor the names of its contributors
  19. may be used to endorse or promote products derived from this software without
  20. specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. OF SUCH DAMAGE.
  31. */
  32. #include "gd32f30x.h"
  33. #include "gd32f307c_eval.h"
  34. #include <stdio.h>
  35. #define FLASH_PAGE_PROGRAM
  36. //#define WRITE_PROTECTION_ENABLE
  37. #define WRITE_PROTECTION_DISABLE
  38. typedef enum {FAILED = 0, PASSED = !FAILED} test_state;
  39. #define FLASH_PAGE_SIZE ((uint16_t)0x800)
  40. #define FMC_PAGES_PROTECTED (OB_WP_6 | OB_WP_7)
  41. #define BANK0_WRITE_START_ADDR ((uint32_t)0x08006000)
  42. #define BANK0_WRITE_END_ADDR ((uint32_t)0x08008000)
  43. uint32_t erase_counter = 0x0, Address = 0x0;
  44. uint16_t data = 0x1753;
  45. uint32_t wp_value = 0xFFFFFFFF, protected_pages = 0x0;
  46. uint32_t page_num;
  47. __IO fmc_state_enum fmc_state = FMC_READY;
  48. __IO test_state program_state = PASSED;
  49. /*!
  50. \brief main function
  51. \param[in] none
  52. \param[out] none
  53. \retval none
  54. */
  55. int main(void)
  56. {
  57. /* initialize led on the board */
  58. gd_eval_led_init(LED2);
  59. gd_eval_led_init(LED3);
  60. /* unlock the flash program/erase controller */
  61. fmc_unlock();
  62. ob_unlock();
  63. fmc_flag_clear(FMC_FLAG_BANK0_END);
  64. fmc_flag_clear(FMC_FLAG_BANK0_WPERR);
  65. fmc_flag_clear(FMC_FLAG_BANK0_PGERR);
  66. /* Get pages write protection status */
  67. wp_value = ob_write_protection_get();
  68. #ifdef WRITE_PROTECTION_DISABLE
  69. /* Get pages already write protected */
  70. protected_pages = ~(wp_value | FMC_PAGES_PROTECTED);
  71. /* Check if desired pages are already write protected */
  72. if((wp_value | (~FMC_PAGES_PROTECTED)) != 0xFFFFFFFF ){
  73. /* Erase all the option Bytes */
  74. fmc_state = ob_erase();
  75. /* Check if there is write protected pages */
  76. if(protected_pages != 0x0){
  77. /* Restore write protected pages */
  78. fmc_state = ob_write_protection_enable(protected_pages);
  79. }
  80. /* Generate System Reset to load the new option byte values */
  81. NVIC_SystemReset();
  82. }
  83. #elif defined WRITE_PROTECTION_ENABLE
  84. /* Get current write protected pages and the new pages to be protected */
  85. protected_pages = (~wp_value) | FMC_PAGES_PROTECTED;
  86. /* Check if desired pages are not yet write protected */
  87. if(((~wp_value) & FMC_PAGES_PROTECTED )!= FMC_PAGES_PROTECTED){
  88. /* Erase all the option Bytes because if a program operation is
  89. performed on a protected page, the Flash memory returns a
  90. protection error */
  91. fmc_state = ob_erase();
  92. /* Enable the pages write protection */
  93. fmc_state = ob_write_protection_enable(protected_pages);
  94. /* Generate System Reset to load the new option byte values */
  95. NVIC_SystemReset();
  96. }
  97. #endif /* WRITE_PROTECTION_DISABLE */
  98. #ifdef FLASH_PAGE_PROGRAM
  99. /* Get the number of pages to be erased */
  100. page_num = (BANK0_WRITE_END_ADDR - BANK0_WRITE_START_ADDR) / FLASH_PAGE_SIZE;
  101. /* The selected pages are not write protected */
  102. if((wp_value & FMC_PAGES_PROTECTED) != 0x00){
  103. /* Clear All pending flags */
  104. fmc_flag_clear(FMC_FLAG_BANK0_END);
  105. fmc_flag_clear(FMC_FLAG_BANK0_WPERR);
  106. fmc_flag_clear(FMC_FLAG_BANK0_PGERR);
  107. /* erase the FLASH pages */
  108. for(erase_counter = 0; (erase_counter < page_num) && (fmc_state == FMC_READY); erase_counter++){
  109. fmc_state = fmc_page_erase(BANK0_WRITE_START_ADDR + (FLASH_PAGE_SIZE * erase_counter));
  110. }
  111. /* FLASH Half Word program of data 0x1753 at addresses defined by BANK1_WRITE_START_ADDR and BANK1_WRITE_END_ADDR */
  112. Address = BANK0_WRITE_START_ADDR;
  113. while((Address < BANK0_WRITE_END_ADDR) && (fmc_state == FMC_READY)){
  114. fmc_state = fmc_halfword_program(Address, data);
  115. Address = Address + 2;
  116. }
  117. /* Check the correctness of written data */
  118. Address = BANK0_WRITE_START_ADDR;
  119. while((Address < BANK0_WRITE_END_ADDR) && (program_state != FAILED)){
  120. if((*(__IO uint16_t*) Address) != data){
  121. program_state = FAILED;
  122. }
  123. Address += 2;
  124. }
  125. gd_eval_led_on(LED2);
  126. }
  127. else{
  128. /* Error to program the flash : The desired pages are write protected */
  129. program_state = FAILED;
  130. gd_eval_led_on(LED3);
  131. }
  132. #endif /* FLASH_PAGE_PROGRAM */
  133. while(1){
  134. }
  135. }