n32g45x_iwdg.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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_iwdg.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_iwdg.h"
  35. /** @addtogroup N32G45X_StdPeriph_Driver
  36. * @{
  37. */
  38. /** @addtogroup IWDG
  39. * @brief IWDG driver modules
  40. * @{
  41. */
  42. /** @addtogroup IWDG_Private_TypesDefinitions
  43. * @{
  44. */
  45. /**
  46. * @}
  47. */
  48. /** @addtogroup IWDG_Private_Defines
  49. * @{
  50. */
  51. /* ---------------------- IWDG registers bit mask ----------------------------*/
  52. /* KEY register bit mask */
  53. #define KEY_ReloadKey ((uint16_t)0xAAAA)
  54. #define KEY_EnableKey ((uint16_t)0xCCCC)
  55. /**
  56. * @}
  57. */
  58. /** @addtogroup IWDG_Private_Macros
  59. * @{
  60. */
  61. /**
  62. * @}
  63. */
  64. /** @addtogroup IWDG_Private_Variables
  65. * @{
  66. */
  67. /**
  68. * @}
  69. */
  70. /** @addtogroup IWDG_Private_FunctionPrototypes
  71. * @{
  72. */
  73. /**
  74. * @}
  75. */
  76. /** @addtogroup IWDG_Private_Functions
  77. * @{
  78. */
  79. /**
  80. * @brief Enables or disables write access to IWDG_PR and IWDG_RLR registers.
  81. * @param IWDG_WriteAccess new state of write access to IWDG_PR and IWDG_RLR registers.
  82. * This parameter can be one of the following values:
  83. * @arg IWDG_WRITE_ENABLE Enable write access to IWDG_PR and IWDG_RLR registers
  84. * @arg IWDG_WRITE_DISABLE Disable write access to IWDG_PR and IWDG_RLR registers
  85. */
  86. void IWDG_WriteConfig(uint16_t IWDG_WriteAccess)
  87. {
  88. /* Check the parameters */
  89. assert_param(IS_IWDG_WRITE(IWDG_WriteAccess));
  90. IWDG->KEY = IWDG_WriteAccess;
  91. }
  92. /**
  93. * @brief Sets IWDG Prescaler value.
  94. * @param IWDG_Prescaler specifies the IWDG Prescaler value.
  95. * This parameter can be one of the following values:
  96. * @arg IWDG_PRESCALER_DIV4 IWDG prescaler set to 4
  97. * @arg IWDG_PRESCALER_DIV8 IWDG prescaler set to 8
  98. * @arg IWDG_PRESCALER_DIV16 IWDG prescaler set to 16
  99. * @arg IWDG_PRESCALER_DIV32 IWDG prescaler set to 32
  100. * @arg IWDG_PRESCALER_DIV64 IWDG prescaler set to 64
  101. * @arg IWDG_PRESCALER_DIV128 IWDG prescaler set to 128
  102. * @arg IWDG_PRESCALER_DIV256 IWDG prescaler set to 256
  103. */
  104. void IWDG_SetPrescalerDiv(uint8_t IWDG_Prescaler)
  105. {
  106. /* Check the parameters */
  107. assert_param(IS_IWDG_PRESCALER_DIV(IWDG_Prescaler));
  108. IWDG->PREDIV = IWDG_Prescaler;
  109. }
  110. /**
  111. * @brief Sets IWDG Reload value.
  112. * @param Reload specifies the IWDG Reload value.
  113. * This parameter must be a number between 0 and 0x0FFF.
  114. */
  115. void IWDG_CntReload(uint16_t Reload)
  116. {
  117. /* Check the parameters */
  118. assert_param(IS_IWDG_RELOAD(Reload));
  119. IWDG->RELV = Reload;
  120. }
  121. /**
  122. * @brief Reloads IWDG counter with value defined in the reload register
  123. * (write access to IWDG_PR and IWDG_RLR registers disabled).
  124. */
  125. void IWDG_ReloadKey(void)
  126. {
  127. IWDG->KEY = KEY_ReloadKey;
  128. }
  129. /**
  130. * @brief Enables IWDG (write access to IWDG_PR and IWDG_RLR registers disabled).
  131. */
  132. void IWDG_Enable(void)
  133. {
  134. IWDG->KEY = KEY_EnableKey;
  135. }
  136. /**
  137. * @brief Checks whether the specified IWDG flag is set or not.
  138. * @param IWDG_FLAG specifies the flag to check.
  139. * This parameter can be one of the following values:
  140. * @arg IWDG_PVU_FLAG Prescaler Value Update on going
  141. * @arg IWDG_CRVU_FLAG Reload Value Update on going
  142. * @return The new state of IWDG_FLAG (SET or RESET).
  143. */
  144. FlagStatus IWDG_GetStatus(uint16_t IWDG_FLAG)
  145. {
  146. FlagStatus bitstatus = RESET;
  147. /* Check the parameters */
  148. assert_param(IS_IWDG_FLAG(IWDG_FLAG));
  149. if ((IWDG->STS & IWDG_FLAG) != (uint32_t)RESET)
  150. {
  151. bitstatus = SET;
  152. }
  153. else
  154. {
  155. bitstatus = RESET;
  156. }
  157. /* Return the flag status */
  158. return bitstatus;
  159. }
  160. /**
  161. * @}
  162. */
  163. /**
  164. * @}
  165. */
  166. /**
  167. * @}
  168. */