gd32f30x_bkp.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*!
  2. \file gd32f30x_bkp.h
  3. \brief definitions for the BKP
  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. #ifndef GD32F30X_BKP_H
  33. #define GD32F30X_BKP_H
  34. #include "gd32f30x.h"
  35. /* BKP definitions */
  36. #define BKP BKP_BASE /*!< BKP base address */
  37. /* registers definitions */
  38. #define BKP_DATA0 REG16((BKP) + 0x04U) /*!< BKP data register 0 */
  39. #define BKP_DATA1 REG16((BKP) + 0x08U) /*!< BKP data register 1 */
  40. #define BKP_DATA2 REG16((BKP) + 0x0CU) /*!< BKP data register 2 */
  41. #define BKP_DATA3 REG16((BKP) + 0x10U) /*!< BKP data register 3 */
  42. #define BKP_DATA4 REG16((BKP) + 0x14U) /*!< BKP data register 4 */
  43. #define BKP_DATA5 REG16((BKP) + 0x18U) /*!< BKP data register 5 */
  44. #define BKP_DATA6 REG16((BKP) + 0x1CU) /*!< BKP data register 6 */
  45. #define BKP_DATA7 REG16((BKP) + 0x20U) /*!< BKP data register 7 */
  46. #define BKP_DATA8 REG16((BKP) + 0x24U) /*!< BKP data register 8 */
  47. #define BKP_DATA9 REG16((BKP) + 0x28U) /*!< BKP data register 9 */
  48. #define BKP_DATA10 REG16((BKP) + 0x40U) /*!< BKP data register 10 */
  49. #define BKP_DATA11 REG16((BKP) + 0x44U) /*!< BKP data register 11 */
  50. #define BKP_DATA12 REG16((BKP) + 0x48U) /*!< BKP data register 12 */
  51. #define BKP_DATA13 REG16((BKP) + 0x4CU) /*!< BKP data register 13 */
  52. #define BKP_DATA14 REG16((BKP) + 0x50U) /*!< BKP data register 14 */
  53. #define BKP_DATA15 REG16((BKP) + 0x54U) /*!< BKP data register 15 */
  54. #define BKP_DATA16 REG16((BKP) + 0x58U) /*!< BKP data register 16 */
  55. #define BKP_DATA17 REG16((BKP) + 0x5CU) /*!< BKP data register 17 */
  56. #define BKP_DATA18 REG16((BKP) + 0x60U) /*!< BKP data register 18 */
  57. #define BKP_DATA19 REG16((BKP) + 0x64U) /*!< BKP data register 19 */
  58. #define BKP_DATA20 REG16((BKP) + 0x68U) /*!< BKP data register 20 */
  59. #define BKP_DATA21 REG16((BKP) + 0x6CU) /*!< BKP data register 21 */
  60. #define BKP_DATA22 REG16((BKP) + 0x70U) /*!< BKP data register 22 */
  61. #define BKP_DATA23 REG16((BKP) + 0x74U) /*!< BKP data register 23 */
  62. #define BKP_DATA24 REG16((BKP) + 0x78U) /*!< BKP data register 24 */
  63. #define BKP_DATA25 REG16((BKP) + 0x7CU) /*!< BKP data register 25 */
  64. #define BKP_DATA26 REG16((BKP) + 0x80U) /*!< BKP data register 26 */
  65. #define BKP_DATA27 REG16((BKP) + 0x84U) /*!< BKP data register 27 */
  66. #define BKP_DATA28 REG16((BKP) + 0x88U) /*!< BKP data register 28 */
  67. #define BKP_DATA29 REG16((BKP) + 0x8CU) /*!< BKP data register 29 */
  68. #define BKP_DATA30 REG16((BKP) + 0x90U) /*!< BKP data register 30 */
  69. #define BKP_DATA31 REG16((BKP) + 0x94U) /*!< BKP data register 31 */
  70. #define BKP_DATA32 REG16((BKP) + 0x98U) /*!< BKP data register 32 */
  71. #define BKP_DATA33 REG16((BKP) + 0x9CU) /*!< BKP data register 33 */
  72. #define BKP_DATA34 REG16((BKP) + 0xA0U) /*!< BKP data register 34 */
  73. #define BKP_DATA35 REG16((BKP) + 0xA4U) /*!< BKP data register 35 */
  74. #define BKP_DATA36 REG16((BKP) + 0xA8U) /*!< BKP data register 36 */
  75. #define BKP_DATA37 REG16((BKP) + 0xACU) /*!< BKP data register 37 */
  76. #define BKP_DATA38 REG16((BKP) + 0xB0U) /*!< BKP data register 38 */
  77. #define BKP_DATA39 REG16((BKP) + 0xB4U) /*!< BKP data register 39 */
  78. #define BKP_DATA40 REG16((BKP) + 0xB8U) /*!< BKP data register 40 */
  79. #define BKP_DATA41 REG16((BKP) + 0xBCU) /*!< BKP data register 41 */
  80. #define BKP_OCTL REG16((BKP) + 0x2CU) /*!< RTC signal output control register */
  81. #define BKP_TPCTL REG16((BKP) + 0x30U) /*!< tamper pin control register */
  82. #define BKP_TPCS REG16((BKP) + 0x34U) /*!< tamper control and status register */
  83. /* bits definitions */
  84. /* BKP_DATA */
  85. #define BKP_DATA BITS(0,15) /*!< backup data */
  86. /* BKP_OCTL */
  87. #define BKP_OCTL_RCCV BITS(0,6) /*!< RTC clock calibration value */
  88. #define BKP_OCTL_COEN BIT(7) /*!< RTC clock calibration output enable */
  89. #define BKP_OCTL_ASOEN BIT(8) /*!< RTC alarm or second signal output enable */
  90. #define BKP_OCTL_ROSEL BIT(9) /*!< RTC output selection */
  91. #define BKP_OCTL_CCOSEL BIT(14) /*!< RTC clock output selection */
  92. #define BKP_OCTL_CALDIR BIT(15) /*!< RTC clock calibration direction */
  93. /* BKP_TPCTL */
  94. #define BKP_TPCTL_TPEN BIT(0) /*!< tamper detection enable */
  95. #define BKP_TPCTL_TPAL BIT(1) /*!< tamper pin active level */
  96. /* BKP_TPCS */
  97. #define BKP_TPCS_TER BIT(0) /*!< tamper event reset */
  98. #define BKP_TPCS_TIR BIT(1) /*!< tamper interrupt reset */
  99. #define BKP_TPCS_TPIE BIT(2) /*!< tamper interrupt enable */
  100. #define BKP_TPCS_TEF BIT(8) /*!< tamper event flag */
  101. #define BKP_TPCS_TIF BIT(9) /*!< tamper interrupt flag */
  102. /* constants definitions */
  103. /* BKP register */
  104. #define BKP_DATA0_9(number) REG16((BKP) + 0x04U + (number) * 0x04U)
  105. #define BKP_DATA10_41(number) REG16((BKP) + 0x40U + ((number)-10U) * 0x04U)
  106. /* get data of BKP data register */
  107. #define BKP_DATA_GET(regval) GET_BITS((uint32_t)(regval), 0, 15)
  108. /* RTC clock calibration value */
  109. #define OCTL_RCCV(regval) (BITS(0,6) & ((uint32_t)(regval) << 0))
  110. /* RTC output selection */
  111. #define RTC_OUTPUT_ALARM_PULSE ((uint16_t)0x0000U) /*!< RTC alarm pulse is selected as the RTC output */
  112. #define RTC_OUTPUT_SECOND_PULSE ((uint16_t)0x0200U) /*!< RTC second pulse is selected as the RTC output */
  113. /* RTC clock output selection */
  114. #define RTC_CLOCK_DIV_64 ((uint16_t)0x0000U) /*!< RTC clock div 64 */
  115. #define RTC_CLOCK_DIV_1 ((uint16_t)0x4000U) /*!< RTC clock div 1 */
  116. /* RTC clock calibration direction */
  117. #define RTC_CLOCK_SLOWED_DOWN ((uint16_t)0x0000U) /*!< RTC clock slow down */
  118. #define RTC_CLOCK_SPEED_UP ((uint16_t)0x8000U) /*!< RTC clock speed up */
  119. /* tamper pin active level */
  120. #define TAMPER_PIN_ACTIVE_HIGH ((uint16_t)0x0000U) /*!< the tamper pin is active high */
  121. #define TAMPER_PIN_ACTIVE_LOW ((uint16_t)0x0002U) /*!< the tamper pin is active low */
  122. /* tamper flag */
  123. #define BKP_FLAG_TAMPER BKP_TPCS_TEF /*!< tamper event flag */
  124. /* tamper interrupt flag */
  125. #define BKP_INT_FLAG_TAMPER BKP_TPCS_TIF /*!< tamper interrupt flag */
  126. /* BKP data register number */
  127. typedef enum
  128. {
  129. BKP_DATA_0 = 1, /*!< BKP data register 0 */
  130. BKP_DATA_1, /*!< BKP data register 1 */
  131. BKP_DATA_2, /*!< BKP data register 2 */
  132. BKP_DATA_3, /*!< BKP data register 3 */
  133. BKP_DATA_4, /*!< BKP data register 4 */
  134. BKP_DATA_5, /*!< BKP data register 5 */
  135. BKP_DATA_6, /*!< BKP data register 6 */
  136. BKP_DATA_7, /*!< BKP data register 7 */
  137. BKP_DATA_8, /*!< BKP data register 8 */
  138. BKP_DATA_9, /*!< BKP data register 9 */
  139. BKP_DATA_10, /*!< BKP data register 10 */
  140. BKP_DATA_11, /*!< BKP data register 11 */
  141. BKP_DATA_12, /*!< BKP data register 12 */
  142. BKP_DATA_13, /*!< BKP data register 13 */
  143. BKP_DATA_14, /*!< BKP data register 14 */
  144. BKP_DATA_15, /*!< BKP data register 15 */
  145. BKP_DATA_16, /*!< BKP data register 16 */
  146. BKP_DATA_17, /*!< BKP data register 17 */
  147. BKP_DATA_18, /*!< BKP data register 18 */
  148. BKP_DATA_19, /*!< BKP data register 19 */
  149. BKP_DATA_20, /*!< BKP data register 20 */
  150. BKP_DATA_21, /*!< BKP data register 21 */
  151. BKP_DATA_22, /*!< BKP data register 22 */
  152. BKP_DATA_23, /*!< BKP data register 23 */
  153. BKP_DATA_24, /*!< BKP data register 24 */
  154. BKP_DATA_25, /*!< BKP data register 25 */
  155. BKP_DATA_26, /*!< BKP data register 26 */
  156. BKP_DATA_27, /*!< BKP data register 27 */
  157. BKP_DATA_28, /*!< BKP data register 28 */
  158. BKP_DATA_29, /*!< BKP data register 29 */
  159. BKP_DATA_30, /*!< BKP data register 30 */
  160. BKP_DATA_31, /*!< BKP data register 31 */
  161. BKP_DATA_32, /*!< BKP data register 32 */
  162. BKP_DATA_33, /*!< BKP data register 33 */
  163. BKP_DATA_34, /*!< BKP data register 34 */
  164. BKP_DATA_35, /*!< BKP data register 35 */
  165. BKP_DATA_36, /*!< BKP data register 36 */
  166. BKP_DATA_37, /*!< BKP data register 37 */
  167. BKP_DATA_38, /*!< BKP data register 38 */
  168. BKP_DATA_39, /*!< BKP data register 39 */
  169. BKP_DATA_40, /*!< BKP data register 40 */
  170. BKP_DATA_41, /*!< BKP data register 41 */
  171. }bkp_data_register_enum;
  172. /* function declarations */
  173. /* reset BKP registers */
  174. void bkp_deinit(void);
  175. /* write BKP data register */
  176. void bkp_write_data(bkp_data_register_enum register_number, uint16_t data);
  177. /* read BKP data register */
  178. uint16_t bkp_read_data(bkp_data_register_enum register_number);
  179. /* RTC related functions */
  180. /* enable RTC clock calibration output */
  181. void bkp_rtc_calibration_output_enable(void);
  182. /* disable RTC clock calibration output */
  183. void bkp_rtc_calibration_output_disable(void);
  184. /* enable RTC alarm or second signal output */
  185. void bkp_rtc_signal_output_enable(void);
  186. /* disable RTC alarm or second signal output */
  187. void bkp_rtc_signal_output_disable(void);
  188. /* RTC output selection */
  189. void bkp_rtc_output_select(uint16_t outputsel);
  190. /* RTC clock output selection */
  191. void bkp_rtc_clock_output_select(uint16_t clocksel);
  192. /* RTC clock calibration direction */
  193. void bkp_rtc_clock_calibration_direction(uint16_t direction);
  194. /* set RTC clock calibration value */
  195. void bkp_rtc_calibration_value_set(uint8_t value);
  196. /* tamper pin related functions */
  197. /* enable tamper pin detection */
  198. void bkp_tamper_detection_enable(void);
  199. /* disable tamper pin detection */
  200. void bkp_tamper_detection_disable(void);
  201. /* set tamper pin active level */
  202. void bkp_tamper_active_level_set(uint16_t level);
  203. /* enable tamper pin interrupt */
  204. void bkp_tamper_interrupt_enable(void);
  205. /* disable tamper pin interrupt */
  206. void bkp_tamper_interrupt_disable(void);
  207. /* flag functions */
  208. /* get BKP flag state */
  209. FlagStatus bkp_flag_get(uint16_t flag);
  210. /* clear BKP flag state */
  211. void bkp_flag_clear(uint16_t flag);
  212. /* get BKP interrupt flag state */
  213. FlagStatus bkp_interrupt_flag_get(uint16_t flag);
  214. /* clear BKP interrupt flag state */
  215. void bkp_interrupt_flag_clear(uint16_t flag);
  216. #endif /* GD32F30X_BKP_H */