n32g45x_wwdg.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file n32g45x_wwdg.c
  29. * @author Nations
  30. * @version v1.0.0
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #include "n32g45x_wwdg.h"
  35. #include "n32g45x_rcc.h"
  36. /** @addtogroup N32G45X_StdPeriph_Driver
  37. * @{
  38. */
  39. /** @addtogroup WWDG
  40. * @brief WWDG driver modules
  41. * @{
  42. */
  43. /** @addtogroup WWDG_Private_TypesDefinitions
  44. * @{
  45. */
  46. /**
  47. * @}
  48. */
  49. /** @addtogroup WWDG_Private_Defines
  50. * @{
  51. */
  52. /* ----------- WWDG registers bit address in the alias region ----------- */
  53. #define WWDG_OFFADDR (WWDG_BASE - PERIPH_BASE)
  54. /* Alias word address of EWI bit */
  55. #define CFG_OFFADDR (WWDG_OFFADDR + 0x04)
  56. #define EWINT_BIT 0x09
  57. #define CFG_EWINT_BB (PERIPH_BB_BASE + (CFG_OFFADDR * 32) + (EWINT_BIT * 4))
  58. /* --------------------- WWDG registers bit mask ------------------------ */
  59. /* CTRL register bit mask */
  60. #define CTRL_ACTB_SET ((uint32_t)0x00000080)
  61. /* CFG register bit mask */
  62. #define CFG_TIMERB_MASK ((uint32_t)0xFFFFFE7F)
  63. #define CFG_W_MASK ((uint32_t)0xFFFFFF80)
  64. #define BIT_MASK ((uint8_t)0x7F)
  65. /**
  66. * @}
  67. */
  68. /** @addtogroup WWDG_Private_Macros
  69. * @{
  70. */
  71. /**
  72. * @}
  73. */
  74. /** @addtogroup WWDG_Private_Variables
  75. * @{
  76. */
  77. /**
  78. * @}
  79. */
  80. /** @addtogroup WWDG_Private_FunctionPrototypes
  81. * @{
  82. */
  83. /**
  84. * @}
  85. */
  86. /** @addtogroup WWDG_Private_Functions
  87. * @{
  88. */
  89. /**
  90. * @brief Deinitializes the WWDG peripheral registers to their default reset values.
  91. */
  92. void WWDG_DeInit(void)
  93. {
  94. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_WWDG, ENABLE);
  95. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_WWDG, DISABLE);
  96. }
  97. /**
  98. * @brief Sets the WWDG Prescaler.
  99. * @param WWDG_Prescaler specifies the WWDG Prescaler.
  100. * This parameter can be one of the following values:
  101. * @arg WWDG_PRESCALER_DIV1 WWDG counter clock = (PCLK1/4096)/1
  102. * @arg WWDG_PRESCALER_DIV2 WWDG counter clock = (PCLK1/4096)/2
  103. * @arg WWDG_PRESCALER_DIV4 WWDG counter clock = (PCLK1/4096)/4
  104. * @arg WWDG_PRESCALER_DIV8 WWDG counter clock = (PCLK1/4096)/8
  105. */
  106. void WWDG_SetPrescalerDiv(uint32_t WWDG_Prescaler)
  107. {
  108. uint32_t tmpregister = 0;
  109. /* Check the parameters */
  110. assert_param(IS_WWDG_PRESCALER_DIV(WWDG_Prescaler));
  111. /* Clear WDGTB[1:0] bits */
  112. tmpregister = WWDG->CFG & CFG_TIMERB_MASK;
  113. /* Set WDGTB[1:0] bits according to WWDG_Prescaler value */
  114. tmpregister |= WWDG_Prescaler;
  115. /* Store the new value */
  116. WWDG->CFG = tmpregister;
  117. }
  118. /**
  119. * @brief Sets the WWDG window value.
  120. * @param WindowValue specifies the window value to be compared to the downcounter.
  121. * This parameter value must be lower than 0x80.
  122. */
  123. void WWDG_SetWValue(uint8_t WindowValue)
  124. {
  125. __IO uint32_t tmpregister = 0;
  126. /* Check the parameters */
  127. assert_param(IS_WWDG_WVALUE(WindowValue));
  128. /* Clear W[6:0] bits */
  129. tmpregister = WWDG->CFG & CFG_W_MASK;
  130. /* Set W[6:0] bits according to WindowValue value */
  131. tmpregister |= WindowValue & (uint32_t)BIT_MASK;
  132. /* Store the new value */
  133. WWDG->CFG = tmpregister;
  134. }
  135. /**
  136. * @brief Enables the WWDG Early Wakeup interrupt(EWI).
  137. */
  138. void WWDG_EnableInt(void)
  139. {
  140. *(__IO uint32_t*)CFG_EWINT_BB = (uint32_t)ENABLE;
  141. }
  142. /**
  143. * @brief Sets the WWDG counter value.
  144. * @param Counter specifies the watchdog counter value.
  145. * This parameter must be a number between 0x40 and 0x7F.
  146. */
  147. void WWDG_SetCnt(uint8_t Counter)
  148. {
  149. /* Check the parameters */
  150. assert_param(IS_WWDG_CNT(Counter));
  151. /* Write to T[6:0] bits to configure the counter value, no need to do
  152. a read-modify-write; writing a 0 to WDGA bit does nothing */
  153. WWDG->CTRL = Counter & BIT_MASK;
  154. }
  155. /**
  156. * @brief Enables WWDG and load the counter value.
  157. * @param Counter specifies the watchdog counter value.
  158. * This parameter must be a number between 0x40 and 0x7F.
  159. */
  160. void WWDG_Enable(uint8_t Counter)
  161. {
  162. /* Check the parameters */
  163. assert_param(IS_WWDG_CNT(Counter));
  164. WWDG->CTRL = CTRL_ACTB_SET | Counter;
  165. }
  166. /**
  167. * @brief Checks whether the Early Wakeup interrupt flag is set or not.
  168. * @return The new state of the Early Wakeup interrupt flag (SET or RESET)
  169. */
  170. FlagStatus WWDG_GetEWINTF(void)
  171. {
  172. return (FlagStatus)(WWDG->STS);
  173. }
  174. /**
  175. * @brief Clears Early Wakeup interrupt flag.
  176. */
  177. void WWDG_ClrEWINTF(void)
  178. {
  179. WWDG->STS = (uint32_t)RESET;
  180. }
  181. /**
  182. * @}
  183. */
  184. /**
  185. * @}
  186. */
  187. /**
  188. * @}
  189. */