gd32f10x_bkp.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*!
  2. \file gd32f10x_bkp.h
  3. \brief definitions for the BKP
  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. #ifndef GD32F10X_BKP_H
  33. #define GD32F10X_BKP_H
  34. #include "gd32f10x.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. /* BKP_TPCTL */
  92. #define BKP_TPCTL_TPEN BIT(0) /*!< tamper detection enable */
  93. #define BKP_TPCTL_TPAL BIT(1) /*!< tamper pin active level */
  94. /* BKP_TPCS */
  95. #define BKP_TPCS_TER BIT(0) /*!< tamper event reset */
  96. #define BKP_TPCS_TIR BIT(1) /*!< tamper interrupt reset */
  97. #define BKP_TPCS_TPIE BIT(2) /*!< tamper interrupt enable */
  98. #define BKP_TPCS_TEF BIT(8) /*!< tamper event flag */
  99. #define BKP_TPCS_TIF BIT(9) /*!< tamper interrupt flag */
  100. /* constants definitions */
  101. /* BKP data register number */
  102. typedef enum
  103. {
  104. BKP_DATA_0 = 1, /*!< BKP data register 0 */
  105. BKP_DATA_1, /*!< BKP data register 1 */
  106. BKP_DATA_2, /*!< BKP data register 2 */
  107. BKP_DATA_3, /*!< BKP data register 3 */
  108. BKP_DATA_4, /*!< BKP data register 4 */
  109. BKP_DATA_5, /*!< BKP data register 5 */
  110. BKP_DATA_6, /*!< BKP data register 6 */
  111. BKP_DATA_7, /*!< BKP data register 7 */
  112. BKP_DATA_8, /*!< BKP data register 8 */
  113. BKP_DATA_9, /*!< BKP data register 9 */
  114. BKP_DATA_10, /*!< BKP data register 10 */
  115. BKP_DATA_11, /*!< BKP data register 11 */
  116. BKP_DATA_12, /*!< BKP data register 12 */
  117. BKP_DATA_13, /*!< BKP data register 13 */
  118. BKP_DATA_14, /*!< BKP data register 14 */
  119. BKP_DATA_15, /*!< BKP data register 15 */
  120. BKP_DATA_16, /*!< BKP data register 16 */
  121. BKP_DATA_17, /*!< BKP data register 17 */
  122. BKP_DATA_18, /*!< BKP data register 18 */
  123. BKP_DATA_19, /*!< BKP data register 19 */
  124. BKP_DATA_20, /*!< BKP data register 20 */
  125. BKP_DATA_21, /*!< BKP data register 21 */
  126. BKP_DATA_22, /*!< BKP data register 22 */
  127. BKP_DATA_23, /*!< BKP data register 23 */
  128. BKP_DATA_24, /*!< BKP data register 24 */
  129. BKP_DATA_25, /*!< BKP data register 25 */
  130. BKP_DATA_26, /*!< BKP data register 26 */
  131. BKP_DATA_27, /*!< BKP data register 27 */
  132. BKP_DATA_28, /*!< BKP data register 28 */
  133. BKP_DATA_29, /*!< BKP data register 29 */
  134. BKP_DATA_30, /*!< BKP data register 30 */
  135. BKP_DATA_31, /*!< BKP data register 31 */
  136. BKP_DATA_32, /*!< BKP data register 32 */
  137. BKP_DATA_33, /*!< BKP data register 33 */
  138. BKP_DATA_34, /*!< BKP data register 34 */
  139. BKP_DATA_35, /*!< BKP data register 35 */
  140. BKP_DATA_36, /*!< BKP data register 36 */
  141. BKP_DATA_37, /*!< BKP data register 37 */
  142. BKP_DATA_38, /*!< BKP data register 38 */
  143. BKP_DATA_39, /*!< BKP data register 39 */
  144. BKP_DATA_40, /*!< BKP data register 40 */
  145. BKP_DATA_41, /*!< BKP data register 41 */
  146. }bkp_data_register_enum;
  147. /* BKP register */
  148. #define BKP_DATA0_9(number) REG16((BKP) + 0x04U + (number) * 0x04U)
  149. #define BKP_DATA10_41(number) REG16((BKP) + 0x40U + ((number)-10U) * 0x04U)
  150. /* get data of BKP data register */
  151. #define BKP_DATA_GET(regval) GET_BITS((uint32_t)(regval), 0, 15)
  152. /* RTC clock calibration value */
  153. #define OCTL_RCCV(regval) (BITS(0,6) & ((uint32_t)(regval) << 0))
  154. /* RTC output selection */
  155. #define RTC_OUTPUT_ALARM_PULSE ((uint16_t)0x0000U) /*!< RTC alarm pulse is selected as the RTC output */
  156. #define RTC_OUTPUT_SECOND_PULSE ((uint16_t)0x0200U) /*!< RTC second pulse is selected as the RTC output */
  157. /* tamper pin active level */
  158. #define TAMPER_PIN_ACTIVE_HIGH ((uint16_t)0x0000U) /*!< the tamper pin is active high */
  159. #define TAMPER_PIN_ACTIVE_LOW ((uint16_t)0x0002U) /*!< the tamper pin is active low */
  160. /* tamper flag */
  161. #define BKP_FLAG_TAMPER BKP_TPCS_TEF /*!< tamper event flag */
  162. /* tamper interrupt flag */
  163. #define BKP_INT_FLAG_TAMPER BKP_TPCS_TIF /*!< tamper interrupt flag */
  164. /* function declarations */
  165. /* reset BKP registers */
  166. void bkp_deinit(void);
  167. /* write BKP data register */
  168. void bkp_data_write(bkp_data_register_enum register_number, uint16_t data);
  169. /* read BKP data register */
  170. uint16_t bkp_data_read(bkp_data_register_enum register_number);
  171. /* RTC related functions */
  172. /* enable RTC clock calibration output */
  173. void bkp_rtc_calibration_output_enable(void);
  174. /* disable RTC clock calibration output */
  175. void bkp_rtc_calibration_output_disable(void);
  176. /* enable RTC alarm or second signal output */
  177. void bkp_rtc_signal_output_enable(void);
  178. /* disable RTC alarm or second signal output */
  179. void bkp_rtc_signal_output_disable(void);
  180. /* select RTC output */
  181. void bkp_rtc_output_select(uint16_t outputsel);
  182. /* set RTC clock calibration value */
  183. void bkp_rtc_calibration_value_set(uint8_t value);
  184. /* tamper pin related functions */
  185. /* enable tamper pin detection */
  186. void bkp_tamper_detection_enable(void);
  187. /* disable tamper pin detection */
  188. void bkp_tamper_detection_disable(void);
  189. /* set tamper pin active level */
  190. void bkp_tamper_active_level_set(uint16_t level);
  191. /* interrupt & flag functions */
  192. /* enable tamper interrupt */
  193. void bkp_interrupt_enable(void);
  194. /* disable tamper interrupt */
  195. void bkp_interrupt_disable(void);
  196. /* get tamper flag state */
  197. FlagStatus bkp_flag_get(void);
  198. /* clear tamper flag state */
  199. void bkp_flag_clear(void);
  200. /* get tamper interrupt flag state */
  201. FlagStatus bkp_interrupt_flag_get(void);
  202. /* clear tamper interrupt flag state */
  203. void bkp_interrupt_flag_clear(void);
  204. #endif /* GD32F10X_BKP_H */