gd32f30x_bkp.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*!
  2. \file gd32f30x_bkp.c
  3. \brief BKP driver
  4. \version 2023-12-30, V2.2.0, firmware for GD32F30x
  5. */
  6. /*
  7. Copyright (c) 2020, 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 "gd32f30x_bkp.h"
  30. #define TAMPER_FLAG_SHIFT ((uint8_t)8U)
  31. /*!
  32. \brief reset BKP registers
  33. \param[in] none
  34. \param[out] none
  35. \retval none
  36. */
  37. void bkp_deinit(void)
  38. {
  39. /* reset BKP domain register*/
  40. rcu_bkp_reset_enable();
  41. rcu_bkp_reset_disable();
  42. }
  43. /*!
  44. \brief write BKP data register
  45. \param[in] register_number: refer to bkp_data_register_enum, only one parameter can be selected
  46. \arg BKP_DATA_x(x = 0..41): bkp data register number x
  47. \param[in] data: the data to be write in BKP data register
  48. \param[out] none
  49. \retval none
  50. */
  51. void bkp_write_data(bkp_data_register_enum register_number, uint16_t data)
  52. {
  53. if((register_number >= BKP_DATA_10) && (register_number <= BKP_DATA_41)){
  54. BKP_DATA10_41(register_number-1U) = data;
  55. }else if((register_number >= BKP_DATA_0) && (register_number <= BKP_DATA_9)){
  56. BKP_DATA0_9(register_number-1U) = data;
  57. }else{
  58. /* illegal parameters */
  59. }
  60. }
  61. /*!
  62. \brief read BKP data register
  63. \param[in] register_number: refer to bkp_data_register_enum, only one parameter can be selected
  64. \arg BKP_DATA_x(x = 0..41): bkp data register number x
  65. \param[out] none
  66. \retval data of BKP data register
  67. */
  68. uint16_t bkp_read_data(bkp_data_register_enum register_number)
  69. {
  70. uint16_t data = 0U;
  71. /* get the data from the BKP data register */
  72. if((register_number >= BKP_DATA_10) && (register_number <= BKP_DATA_41)){
  73. data = BKP_DATA10_41(register_number-1U);
  74. }else if((register_number >= BKP_DATA_0) && (register_number <= BKP_DATA_9)){
  75. data = BKP_DATA0_9(register_number-1U);
  76. }else{
  77. /* illegal parameters */
  78. }
  79. return data;
  80. }
  81. /*!
  82. \brief enable RTC clock calibration output
  83. \param[in] none
  84. \param[out] none
  85. \retval none
  86. */
  87. void bkp_rtc_calibration_output_enable(void)
  88. {
  89. BKP_OCTL |= (uint16_t)BKP_OCTL_COEN;
  90. }
  91. /*!
  92. \brief disable RTC clock calibration output
  93. \param[in] none
  94. \param[out] none
  95. \retval none
  96. */
  97. void bkp_rtc_calibration_output_disable(void)
  98. {
  99. BKP_OCTL &= (uint16_t)~BKP_OCTL_COEN;
  100. }
  101. /*!
  102. \brief enable RTC alarm or second signal output
  103. \param[in] none
  104. \param[out] none
  105. \retval none
  106. */
  107. void bkp_rtc_signal_output_enable(void)
  108. {
  109. BKP_OCTL |= (uint16_t)BKP_OCTL_ASOEN;
  110. }
  111. /*!
  112. \brief disable RTC alarm or second signal output
  113. \param[in] none
  114. \param[out] none
  115. \retval none
  116. */
  117. void bkp_rtc_signal_output_disable(void)
  118. {
  119. BKP_OCTL &= (uint16_t)~BKP_OCTL_ASOEN;
  120. }
  121. /*!
  122. \brief select RTC output
  123. \param[in] outputsel: RTC output selection
  124. \arg RTC_OUTPUT_ALARM_PULSE: RTC alarm pulse is selected as the RTC output
  125. \arg RTC_OUTPUT_SECOND_PULSE: RTC second pulse is selected as the RTC output
  126. \param[out] none
  127. \retval none
  128. */
  129. void bkp_rtc_output_select(uint16_t outputsel)
  130. {
  131. uint16_t ctl = 0U;
  132. ctl = BKP_OCTL;
  133. ctl &= (uint16_t)~BKP_OCTL_ROSEL;
  134. ctl |= outputsel;
  135. BKP_OCTL = ctl;
  136. }
  137. /*!
  138. \brief select RTC clock output
  139. \param[in] clocksel: RTC clock output selection
  140. \arg RTC_CLOCK_DIV_64: RTC clock div 64
  141. \arg RTC_CLOCK_DIV_1: RTC clock
  142. \param[out] none
  143. \retval none
  144. */
  145. void bkp_rtc_clock_output_select(uint16_t clocksel)
  146. {
  147. uint16_t ctl = 0U;
  148. ctl = BKP_OCTL;
  149. ctl &= (uint16_t)~BKP_OCTL_CCOSEL;
  150. ctl |= clocksel;
  151. BKP_OCTL = ctl;
  152. }
  153. /*!
  154. \brief RTC clock calibration direction
  155. \param[in] direction: RTC clock calibration direction
  156. \arg RTC_CLOCK_SLOWED_DOWN: RTC clock slow down
  157. \arg RTC_CLOCK_SPEED_UP: RTC clock speed up
  158. \param[out] none
  159. \retval none
  160. */
  161. void bkp_rtc_clock_calibration_direction(uint16_t direction)
  162. {
  163. uint16_t ctl = 0U;
  164. ctl = BKP_OCTL;
  165. ctl &= (uint16_t)~BKP_OCTL_CALDIR;
  166. ctl |= direction;
  167. BKP_OCTL = ctl;
  168. }
  169. /*!
  170. \brief set RTC clock calibration value
  171. \param[in] value: RTC clock calibration value
  172. \arg 0x00 - 0x7F
  173. \param[out] none
  174. \retval none
  175. */
  176. void bkp_rtc_calibration_value_set(uint8_t value)
  177. {
  178. uint16_t ctl;
  179. ctl = BKP_OCTL;
  180. ctl &= ~(uint16_t)BKP_OCTL_RCCV;
  181. ctl |= (uint16_t)OCTL_RCCV(value);
  182. BKP_OCTL = ctl;
  183. }
  184. /*!
  185. \brief enable tamper detection
  186. \param[in] none
  187. \param[out] none
  188. \retval none
  189. */
  190. void bkp_tamper_detection_enable(void)
  191. {
  192. BKP_TPCTL |= (uint16_t)BKP_TPCTL_TPEN;
  193. }
  194. /*!
  195. \brief disable tamper detection
  196. \param[in] none
  197. \param[out] none
  198. \retval none
  199. */
  200. void bkp_tamper_detection_disable(void)
  201. {
  202. BKP_TPCTL &= (uint16_t)~BKP_TPCTL_TPEN;
  203. }
  204. /*!
  205. \brief set tamper pin active level
  206. \param[in] level: tamper active level
  207. \arg TAMPER_PIN_ACTIVE_HIGH: the tamper pin is active high
  208. \arg TAMPER_PIN_ACTIVE_LOW: the tamper pin is active low
  209. \param[out] none
  210. \retval none
  211. */
  212. void bkp_tamper_active_level_set(uint16_t level)
  213. {
  214. uint16_t ctl = 0U;
  215. ctl = BKP_TPCTL;
  216. ctl &= (uint16_t)~BKP_TPCTL_TPAL;
  217. ctl |= level;
  218. BKP_TPCTL = ctl;
  219. }
  220. /*!
  221. \brief enable tamper interrupt
  222. \param[in] none
  223. \param[out] none
  224. \retval none
  225. */
  226. void bkp_tamper_interrupt_enable(void)
  227. {
  228. BKP_TPCS |= (uint16_t)BKP_TPCS_TPIE;
  229. }
  230. /*!
  231. \brief disable tamper interrupt
  232. \param[in] none
  233. \param[out] none
  234. \retval none
  235. */
  236. void bkp_tamper_interrupt_disable(void)
  237. {
  238. BKP_TPCS &= (uint16_t)~BKP_TPCS_TPIE;
  239. }
  240. /*!
  241. \brief get bkp flag state
  242. \param[in] flag
  243. \arg BKP_FLAG_TAMPER: tamper event flag
  244. \param[out] none
  245. \retval FlagStatus: SET or RESET
  246. */
  247. FlagStatus bkp_flag_get(uint16_t flag)
  248. {
  249. if(RESET != (BKP_TPCS & flag)){
  250. return SET;
  251. }else{
  252. return RESET;
  253. }
  254. }
  255. /*!
  256. \brief clear bkp flag state
  257. \param[in] flag
  258. \arg BKP_FLAG_TAMPER: tamper event flag
  259. \param[out] none
  260. \retval none
  261. */
  262. void bkp_flag_clear(uint16_t flag)
  263. {
  264. BKP_TPCS |= (uint16_t)(flag >> TAMPER_FLAG_SHIFT);
  265. }
  266. /*!
  267. \brief get bkp interrupt flag state
  268. \param[in] flag
  269. \arg BKP_INT_FLAG_TAMPER: tamper interrupt flag
  270. \param[out] none
  271. \retval FlagStatus: SET or RESET
  272. */
  273. FlagStatus bkp_interrupt_flag_get(uint16_t flag)
  274. {
  275. if(RESET != (BKP_TPCS & flag)){
  276. return SET;
  277. }else{
  278. return RESET;
  279. }
  280. }
  281. /*!
  282. \brief clear bkp interrupt flag state
  283. \param[in] flag
  284. \arg BKP_INT_FLAG_TAMPER: tamper interrupt flag
  285. \param[out] none
  286. \retval none
  287. */
  288. void bkp_interrupt_flag_clear(uint16_t flag)
  289. {
  290. BKP_TPCS |= (uint16_t)(flag >> TAMPER_FLAG_SHIFT);
  291. }