main.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*!
  2. \file main.c
  3. \brief backup data register
  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. #define BKP_DATA_REG_NUM 42
  35. void led_config(void);
  36. void write_backup_register(uint16_t data);
  37. uint32_t check_backup_register(uint16_t data);
  38. /*!
  39. \brief main function
  40. \param[in] none
  41. \param[out] none
  42. \retval none
  43. */
  44. int main(void)
  45. {
  46. /* led configuration and turn on all led */
  47. led_config();
  48. /* PMU lock enable */
  49. rcu_periph_clock_enable(RCU_PMU);
  50. /* BKP clock enable */
  51. rcu_periph_clock_enable(RCU_BKPI);
  52. /* enable write access to the registers in backup domain */
  53. pmu_backup_write_enable();
  54. /* clear the bit flag of tamper event */
  55. bkp_flag_clear(BKP_FLAG_TAMPER);
  56. /* check if the POR/PDR reset flag is set */
  57. if(RESET != rcu_flag_get(RCU_FLAG_PORRST)){
  58. /* clear the RCU all reset flags */
  59. rcu_all_reset_flag_clear();
  60. /* turn on LED4 */
  61. gd_eval_led_on(LED4);
  62. /* check if backup data registers has been written */
  63. if(0x00 == check_backup_register(0x1226)){
  64. /* Backup data registers values are correct */
  65. /* turn on LED2 */
  66. gd_eval_led_on(LED2);
  67. }else{
  68. /* backup data registers values are not correct or they are not written*/
  69. /* write data to backup data registers */
  70. write_backup_register(0x1226);
  71. /* turn on LED3 */
  72. gd_eval_led_on(LED3);
  73. }
  74. }
  75. /* turn on LED5 */
  76. gd_eval_led_on(LED5);;
  77. while(1){
  78. }
  79. }
  80. /*!
  81. \brief write data to backup DATAx registers
  82. \param[in] data: the data to be written to backup data registers
  83. \arg 0x0000-0xFFFF
  84. \param[out] none
  85. \retval none
  86. */
  87. void write_backup_register(uint16_t data)
  88. {
  89. uint32_t temp = 0;
  90. /* write data to backup registers */
  91. for (temp = 0; temp < BKP_DATA_REG_NUM; temp++){
  92. if(temp < 10){
  93. BKP_DATA0_9(temp) = data + (temp * 0x50);
  94. }else{
  95. BKP_DATA10_41(temp) = data + (temp * 0x50);
  96. }
  97. }
  98. }
  99. /*!
  100. \brief check if the backup DATAx registers values are correct or not
  101. \param[in] data: the data to be written to backup data registers
  102. \arg 0x0000-0xFFFF
  103. \param[out] none
  104. \retval the number of data register
  105. */
  106. uint32_t check_backup_register(uint16_t data)
  107. {
  108. uint32_t temp = 0;
  109. /* check the data of backup registers */
  110. for(temp = 0; temp < BKP_DATA_REG_NUM; temp++){
  111. if(temp < 10){
  112. /* get data from data register 0-9 */
  113. if(data + (temp * 0x50) != BKP_DATA_GET(BKP_DATA0_9(temp))){
  114. return temp+1;
  115. }
  116. }else{
  117. /* get data from data register 10-41 */
  118. if(data + (temp * 0x50) != BKP_DATA_GET(BKP_DATA10_41(temp))){
  119. return temp+1;
  120. }
  121. }
  122. }
  123. return 0;
  124. }
  125. /*!
  126. \brief configure led
  127. \param[in] none
  128. \param[out] none
  129. \retval none
  130. */
  131. void led_config(void)
  132. {
  133. gd_eval_led_init(LED2);
  134. gd_eval_led_init(LED3);
  135. gd_eval_led_init(LED4);
  136. gd_eval_led_init(LED5);
  137. }