gd32vf103_bkp.c 8.0 KB

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