gd32f30x_bkp.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*!
  2. \file gd32f30x_bkp.c
  3. \brief BKP driver
  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_bkp.h"
  33. #define TAMPER_FLAG_SHIFT ((uint8_t)8U)
  34. /*!
  35. \brief reset BKP registers
  36. \param[in] none
  37. \param[out] none
  38. \retval none
  39. */
  40. void bkp_deinit(void)
  41. {
  42. /* reset BKP domain register*/
  43. rcu_bkp_reset_enable();
  44. rcu_bkp_reset_disable();
  45. }
  46. /*!
  47. \brief write BKP data register
  48. \param[in] register_number: refer to bkp_data_register_enum, only one parameter can be selected
  49. \arg BKP_DATA_x(x = 0..41): bkp data register number x
  50. \param[in] data: the data to be write in BKP data register
  51. \param[out] none
  52. \retval none
  53. */
  54. void bkp_write_data(bkp_data_register_enum register_number, uint16_t data)
  55. {
  56. if((register_number >= BKP_DATA_10) && (register_number <= BKP_DATA_41)){
  57. BKP_DATA10_41(register_number-1U) = data;
  58. }else if((register_number >= BKP_DATA_0) && (register_number <= BKP_DATA_9)){
  59. BKP_DATA0_9(register_number-1U) = data;
  60. }else{
  61. /* illegal parameters */
  62. }
  63. }
  64. /*!
  65. \brief read BKP data register
  66. \param[in] register_number: refer to bkp_data_register_enum, only one parameter can be selected
  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_read_data(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. \arg RTC_OUTPUT_ALARM_PULSE: RTC alarm pulse is selected as the RTC output
  128. \arg RTC_OUTPUT_SECOND_PULSE: RTC second pulse is selected as the RTC output
  129. \param[out] none
  130. \retval none
  131. */
  132. void bkp_rtc_output_select(uint16_t outputsel)
  133. {
  134. uint16_t ctl = 0U;
  135. ctl = BKP_OCTL;
  136. ctl &= (uint16_t)~BKP_OCTL_ROSEL;
  137. ctl |= outputsel;
  138. BKP_OCTL = ctl;
  139. }
  140. /*!
  141. \brief select RTC clock output
  142. \param[in] clocksel: RTC clock output selection
  143. \arg RTC_CLOCK_DIV_64: RTC clock div 64
  144. \arg RTC_CLOCK_DIV_1: RTC clock
  145. \param[out] none
  146. \retval none
  147. */
  148. void bkp_rtc_clock_output_select(uint16_t clocksel)
  149. {
  150. uint16_t ctl = 0U;
  151. ctl = BKP_OCTL;
  152. ctl &= (uint16_t)~BKP_OCTL_CCOSEL;
  153. ctl |= clocksel;
  154. BKP_OCTL = ctl;
  155. }
  156. /*!
  157. \brief RTC clock calibration direction
  158. \param[in] direction: RTC clock calibration direction
  159. \arg RTC_CLOCK_SLOWED_DOWN: RTC clock slow down
  160. \arg RTC_CLOCK_SPEED_UP: RTC clock speed up
  161. \param[out] none
  162. \retval none
  163. */
  164. void bkp_rtc_clock_calibration_direction(uint16_t direction)
  165. {
  166. uint16_t ctl = 0U;
  167. ctl = BKP_OCTL;
  168. ctl &= (uint16_t)~BKP_OCTL_CALDIR;
  169. ctl |= direction;
  170. BKP_OCTL = ctl;
  171. }
  172. /*!
  173. \brief set RTC clock calibration value
  174. \param[in] value: RTC clock calibration value
  175. \arg 0x00 - 0x7F
  176. \param[out] none
  177. \retval none
  178. */
  179. void bkp_rtc_calibration_value_set(uint8_t value)
  180. {
  181. uint16_t ctl;
  182. ctl = BKP_OCTL;
  183. ctl &= ~(uint16_t)BKP_OCTL_RCCV;
  184. ctl |= (uint16_t)OCTL_RCCV(value);
  185. BKP_OCTL = ctl;
  186. }
  187. /*!
  188. \brief enable tamper detection
  189. \param[in] none
  190. \param[out] none
  191. \retval none
  192. */
  193. void bkp_tamper_detection_enable(void)
  194. {
  195. BKP_TPCTL |= (uint16_t)BKP_TPCTL_TPEN;
  196. }
  197. /*!
  198. \brief disable tamper detection
  199. \param[in] none
  200. \param[out] none
  201. \retval none
  202. */
  203. void bkp_tamper_detection_disable(void)
  204. {
  205. BKP_TPCTL &= (uint16_t)~BKP_TPCTL_TPEN;
  206. }
  207. /*!
  208. \brief set tamper pin active level
  209. \param[in] level: tamper active level
  210. \arg TAMPER_PIN_ACTIVE_HIGH: the tamper pin is active high
  211. \arg TAMPER_PIN_ACTIVE_LOW: the tamper pin is active low
  212. \param[out] none
  213. \retval none
  214. */
  215. void bkp_tamper_active_level_set(uint16_t level)
  216. {
  217. uint16_t ctl = 0U;
  218. ctl = BKP_TPCTL;
  219. ctl &= (uint16_t)~BKP_TPCTL_TPAL;
  220. ctl |= level;
  221. BKP_TPCTL = ctl;
  222. }
  223. /*!
  224. \brief enable tamper interrupt
  225. \param[in] none
  226. \param[out] none
  227. \retval none
  228. */
  229. void bkp_tamper_interrupt_enable(void)
  230. {
  231. BKP_TPCS |= (uint16_t)BKP_TPCS_TPIE;
  232. }
  233. /*!
  234. \brief disable tamper interrupt
  235. \param[in] none
  236. \param[out] none
  237. \retval none
  238. */
  239. void bkp_tamper_interrupt_disable(void)
  240. {
  241. BKP_TPCS &= (uint16_t)~BKP_TPCS_TPIE;
  242. }
  243. /*!
  244. \brief get bkp flag state
  245. \param[in] flag
  246. \arg BKP_FLAG_TAMPER: tamper event flag
  247. \param[out] none
  248. \retval FlagStatus: SET or RESET
  249. */
  250. FlagStatus bkp_flag_get(uint16_t flag)
  251. {
  252. if(RESET != (BKP_TPCS & flag)){
  253. return SET;
  254. }else{
  255. return RESET;
  256. }
  257. }
  258. /*!
  259. \brief clear bkp flag state
  260. \param[in] flag
  261. \arg BKP_FLAG_TAMPER: tamper event flag
  262. \param[out] none
  263. \retval none
  264. */
  265. void bkp_flag_clear(uint16_t flag)
  266. {
  267. BKP_TPCS |= (uint16_t)(flag >> TAMPER_FLAG_SHIFT);
  268. }
  269. /*!
  270. \brief get bkp interrupt flag state
  271. \param[in] flag
  272. \arg BKP_INT_FLAG_TAMPER: tamper interrupt flag
  273. \param[out] none
  274. \retval FlagStatus: SET or RESET
  275. */
  276. FlagStatus bkp_interrupt_flag_get(uint16_t flag)
  277. {
  278. if(RESET != (BKP_TPCS & flag)){
  279. return SET;
  280. }else{
  281. return RESET;
  282. }
  283. }
  284. /*!
  285. \brief clear bkp interrupt flag state
  286. \param[in] flag
  287. \arg BKP_INT_FLAG_TAMPER: tamper interrupt flag
  288. \param[out] none
  289. \retval none
  290. */
  291. void bkp_interrupt_flag_clear(uint16_t flag)
  292. {
  293. BKP_TPCS |= (uint16_t)(flag >> TAMPER_FLAG_SHIFT);
  294. }