gd32f10x_bkp.c 7.8 KB

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