main.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*!
  2. \file main.c
  3. \brief tamper demo
  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 nvic_config(void);
  37. void write_backup_register(uint16_t data);
  38. uint32_t check_backup_register(uint16_t data);
  39. /*!
  40. \brief main function
  41. \param[in] none
  42. \param[out] none
  43. \retval none
  44. */
  45. int main(void)
  46. {
  47. /* led configuration and turn on all led */
  48. led_config();
  49. /* NVIC configuration */
  50. nvic_config();
  51. /* PMU lock enable */
  52. rcu_periph_clock_enable(RCU_PMU);
  53. /* BKP clock enable */
  54. rcu_periph_clock_enable(RCU_BKPI);
  55. /* enable write access to the registers in backup domain */
  56. pmu_backup_write_enable();
  57. /* tamper pin active level set */
  58. bkp_tamper_active_level_set(TAMPER_PIN_ACTIVE_LOW);
  59. /* tamper detection disable */
  60. bkp_tamper_detection_disable();
  61. /* disable the tamper interrupt */
  62. bkp_tamper_interrupt_disable();
  63. /* clear the bit flag of tamper event */
  64. bkp_flag_clear(BKP_FLAG_TAMPER);
  65. /* configure the tamper pin active on low level, and enable the tamper pin */
  66. bkp_tamper_interrupt_enable();
  67. /* tamper detection enable */
  68. bkp_tamper_detection_enable();
  69. /* write data to backup DATAx registers */
  70. write_backup_register(0x1226);
  71. /* check if the written data are correct */
  72. if(0x00 == check_backup_register(0x1226)){
  73. /* turn on LED2 */
  74. gd_eval_led_on(LED2);
  75. }else{
  76. /* turn on LED3 */
  77. gd_eval_led_on(LED3);
  78. }
  79. while(1){
  80. }
  81. }
  82. /*!
  83. \brief write data to backup DATAx registers
  84. \param[in] data: the data to be written to backup data registers
  85. \arg 0x0000-0xFFFF
  86. \param[out] none
  87. \retval none
  88. */
  89. void write_backup_register(uint16_t data)
  90. {
  91. uint32_t temp = 0;
  92. /* write data to backup data registers */
  93. for (temp = 0; temp < BKP_DATA_REG_NUM; temp++){
  94. if(temp < 10){
  95. BKP_DATA0_9(temp) = data + (temp * 0x50);
  96. }else{
  97. BKP_DATA10_41(temp) = data + (temp * 0x50);
  98. }
  99. }
  100. }
  101. /*!
  102. \brief check if the backup DATAx registers values are correct or not
  103. \param[in] data: the data to be written to backup data registers
  104. \arg 0x0000-0xFFFF
  105. \param[out] none
  106. \retval the number of data register
  107. */
  108. uint32_t check_backup_register(uint16_t data)
  109. {
  110. uint32_t temp = 0;
  111. /* check the data of backup data registers */
  112. for(temp = 0; temp < BKP_DATA_REG_NUM; temp++){
  113. if(temp < 10){
  114. /* get data from data register 0-9 */
  115. if(data + (temp * 0x50) != BKP_DATA_GET(BKP_DATA0_9(temp))){
  116. return temp+1;
  117. }
  118. }else{
  119. /* get data from data register 10-41 */
  120. if(data + (temp * 0x50) != BKP_DATA_GET(BKP_DATA10_41(temp))){
  121. return temp+1;
  122. }
  123. }
  124. }
  125. return 0;
  126. }
  127. /*!
  128. \brief configure led
  129. \param[in] none
  130. \param[out] none
  131. \retval none
  132. */
  133. void led_config(void)
  134. {
  135. gd_eval_led_init(LED2);
  136. gd_eval_led_init(LED3);
  137. gd_eval_led_init(LED4);
  138. gd_eval_led_init(LED5);
  139. }
  140. /*!
  141. \brief check if the backup data registers are clear or not
  142. \param[in] none
  143. \param[out] none
  144. \retval the number of data register
  145. */
  146. uint32_t is_backup_register_clear(void)
  147. {
  148. uint32_t temp = 0;
  149. for(temp = 0; temp < BKP_DATA_REG_NUM; temp++){
  150. if(temp < 10){
  151. /* check if the data of data register 0-9 is 0x0000 */
  152. if(0x0000 != BKP_DATA_GET(BKP_DATA0_9(temp))){
  153. return temp+1;
  154. }
  155. }else{
  156. /* check if the data of data register 10-41 is 0x0000 */
  157. if(0x0000 != BKP_DATA_GET(BKP_DATA10_41(temp))){
  158. return temp+1;
  159. }
  160. }
  161. }
  162. return 0;
  163. }
  164. /*!
  165. \brief configure the nested vectored interrupt controller
  166. \param[in] none
  167. \param[out] none
  168. \retval none
  169. */
  170. void nvic_config(void)
  171. {
  172. nvic_irq_enable(TAMPER_IRQn,0,0);
  173. }