gd32vf103_bkp.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*!
  2. \file gd32vf103_bkp.c
  3. \brief BKP driver
  4. \version 2019-6-5, V1.0.0, firmware for GD32VF103
  5. */
  6. /*
  7. Copyright (c) 2019, GigaDevice Semiconductor Inc.
  8. Redistribution and use in source and binary forms, with or without modification,
  9. are permitted provided that the following conditions are met:
  10. 1. Redistributions of source code must retain the above copyright notice, this
  11. list of conditions and the following disclaimer.
  12. 2. Redistributions in binary form must reproduce the above copyright notice,
  13. this list of conditions and the following disclaimer in the documentation
  14. and/or other materials provided with the distribution.
  15. 3. Neither the name of the copyright holder nor the names of its contributors
  16. may be used to endorse or promote products derived from this software without
  17. specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  22. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  27. OF SUCH DAMAGE.
  28. */
  29. #include "gd32vf103_bkp.h"
  30. /* BKP register bits offset */
  31. #define BKP_TAMPER_BITS_OFFSET ((uint32_t)8U)
  32. /*!
  33. \brief reset BKP registers
  34. \param[in] none
  35. \param[out] none
  36. \retval none
  37. */
  38. void bkp_deinit(void)
  39. {
  40. /* reset BKP domain register*/
  41. rcu_bkp_reset_enable();
  42. rcu_bkp_reset_disable();
  43. }
  44. /*!
  45. \brief write BKP data register
  46. \param[in] register_number: refer to bkp_data_register_enum
  47. only one parameter can be selected which is shown as below:
  48. \arg BKP_DATA_x(x = 0..41): bkp data register number x
  49. \param[in] data: the data to be write in BKP data register
  50. \param[out] none
  51. \retval none
  52. */
  53. void bkp_data_write(bkp_data_register_enum register_number, uint16_t data)
  54. {
  55. if((register_number >= BKP_DATA_10) && (register_number <= BKP_DATA_41)){
  56. BKP_DATA10_41(register_number - 1U) = data;
  57. }else if((register_number >= BKP_DATA_0) && (register_number <= BKP_DATA_9)){
  58. BKP_DATA0_9(register_number - 1U) = data;
  59. }else{
  60. /* illegal parameters */
  61. }
  62. }
  63. /*!
  64. \brief read BKP data register
  65. \param[in] register_number: refer to bkp_data_register_enum
  66. only one parameter can be selected which is shown as below:
  67. \arg BKP_DATA_x(x = 0..41): bkp data register number x
  68. \param[out] none
  69. \retval data of BKP data register
  70. */
  71. uint16_t bkp_data_read(bkp_data_register_enum register_number)
  72. {
  73. uint16_t data = 0U;
  74. /* get the data from the BKP data register */
  75. if((register_number >= BKP_DATA_10) && (register_number <= BKP_DATA_41)){
  76. data = BKP_DATA10_41(register_number - 1U);
  77. }else if((register_number >= BKP_DATA_0) && (register_number <= BKP_DATA_9)){
  78. data = BKP_DATA0_9(register_number - 1U);
  79. }else{
  80. /* illegal parameters */
  81. }
  82. return data;
  83. }
  84. /*!
  85. \brief enable RTC clock calibration output
  86. \param[in] none
  87. \param[out] none
  88. \retval none
  89. */
  90. void bkp_rtc_calibration_output_enable(void)
  91. {
  92. BKP_OCTL |= (uint16_t)BKP_OCTL_COEN;
  93. }
  94. /*!
  95. \brief disable RTC clock calibration output
  96. \param[in] none
  97. \param[out] none
  98. \retval none
  99. */
  100. void bkp_rtc_calibration_output_disable(void)
  101. {
  102. BKP_OCTL &= (uint16_t)~BKP_OCTL_COEN;
  103. }
  104. /*!
  105. \brief enable RTC alarm or second signal output
  106. \param[in] none
  107. \param[out] none
  108. \retval none
  109. */
  110. void bkp_rtc_signal_output_enable(void)
  111. {
  112. BKP_OCTL |= (uint16_t)BKP_OCTL_ASOEN;
  113. }
  114. /*!
  115. \brief disable RTC alarm or second signal output
  116. \param[in] none
  117. \param[out] none
  118. \retval none
  119. */
  120. void bkp_rtc_signal_output_disable(void)
  121. {
  122. BKP_OCTL &= (uint16_t)~BKP_OCTL_ASOEN;
  123. }
  124. /*!
  125. \brief select RTC output
  126. \param[in] outputsel: RTC output selection
  127. only one parameter can be selected which is shown as below:
  128. \arg RTC_OUTPUT_ALARM_PULSE: RTC alarm pulse is selected as the RTC output
  129. \arg RTC_OUTPUT_SECOND_PULSE: RTC second pulse is selected as the RTC output
  130. \param[out] none
  131. \retval none
  132. */
  133. void bkp_rtc_output_select(uint16_t outputsel)
  134. {
  135. uint16_t ctl = 0U;
  136. /* configure BKP_OCTL_ROSEL with outputsel */
  137. ctl = BKP_OCTL;
  138. ctl &= (uint16_t)~BKP_OCTL_ROSEL;
  139. ctl |= outputsel;
  140. BKP_OCTL = ctl;
  141. }
  142. /*!
  143. \brief set RTC clock calibration value
  144. \param[in] value: RTC clock calibration value
  145. \arg 0x00 - 0x7F
  146. \param[out] none
  147. \retval none
  148. */
  149. void bkp_rtc_calibration_value_set(uint8_t value)
  150. {
  151. uint16_t ctl;
  152. /* configure BKP_OCTL_RCCV with value */
  153. ctl = BKP_OCTL;
  154. ctl &= (uint16_t)~BKP_OCTL_RCCV;
  155. ctl |= (uint16_t)OCTL_RCCV(value);
  156. BKP_OCTL = ctl;
  157. }
  158. /*!
  159. \brief enable tamper detection
  160. \param[in] none
  161. \param[out] none
  162. \retval none
  163. */
  164. void bkp_tamper_detection_enable(void)
  165. {
  166. BKP_TPCTL |= (uint16_t)BKP_TPCTL_TPEN;
  167. }
  168. /*!
  169. \brief disable tamper detection
  170. \param[in] none
  171. \param[out] none
  172. \retval none
  173. */
  174. void bkp_tamper_detection_disable(void)
  175. {
  176. BKP_TPCTL &= (uint16_t)~BKP_TPCTL_TPEN;
  177. }
  178. /*!
  179. \brief set tamper pin active level
  180. \param[in] level: tamper active level
  181. only one parameter can be selected which is shown as below:
  182. \arg TAMPER_PIN_ACTIVE_HIGH: the tamper pin is active high
  183. \arg TAMPER_PIN_ACTIVE_LOW: the tamper pin is active low
  184. \param[out] none
  185. \retval none
  186. */
  187. void bkp_tamper_active_level_set(uint16_t level)
  188. {
  189. uint16_t ctl = 0U;
  190. /* configure BKP_TPCTL_TPAL with level */
  191. ctl = BKP_TPCTL;
  192. ctl &= (uint16_t)~BKP_TPCTL_TPAL;
  193. ctl |= level;
  194. BKP_TPCTL = ctl;
  195. }
  196. /*!
  197. \brief enable tamper interrupt
  198. \param[in] none
  199. \param[out] none
  200. \retval none
  201. */
  202. void bkp_interrupt_enable(void)
  203. {
  204. BKP_TPCS |= (uint16_t)BKP_TPCS_TPIE;
  205. }
  206. /*!
  207. \brief disable tamper interrupt
  208. \param[in] none
  209. \param[out] none
  210. \retval none
  211. */
  212. void bkp_interrupt_disable(void)
  213. {
  214. BKP_TPCS &= (uint16_t)~BKP_TPCS_TPIE;
  215. }
  216. /*!
  217. \brief get tamper flag state
  218. \param[in] none
  219. \param[out] none
  220. \retval FlagStatus: SET or RESET
  221. */
  222. FlagStatus bkp_flag_get(void)
  223. {
  224. if(RESET != (BKP_TPCS & BKP_FLAG_TAMPER)){
  225. return SET;
  226. }else{
  227. return RESET;
  228. }
  229. }
  230. /*!
  231. \brief clear tamper flag state
  232. \param[in] none
  233. \param[out] none
  234. \retval none
  235. */
  236. void bkp_flag_clear(void)
  237. {
  238. BKP_TPCS |= (uint16_t)(BKP_FLAG_TAMPER >> BKP_TAMPER_BITS_OFFSET);
  239. }
  240. /*!
  241. \brief get tamper interrupt flag state
  242. \param[in] none
  243. \param[out] none
  244. \retval FlagStatus: SET or RESET
  245. */
  246. FlagStatus bkp_interrupt_flag_get(void)
  247. {
  248. if(RESET != (BKP_TPCS & BKP_INT_FLAG_TAMPER)){
  249. return SET;
  250. }else{
  251. return RESET;
  252. }
  253. }
  254. /*!
  255. \brief clear tamper interrupt flag state
  256. \param[in] none
  257. \param[out] none
  258. \retval none
  259. */
  260. void bkp_interrupt_flag_clear(void)
  261. {
  262. BKP_TPCS |= (uint16_t)(BKP_INT_FLAG_TAMPER >> BKP_TAMPER_BITS_OFFSET);
  263. }