n32g45x_pwr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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_pwr.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_pwr.h"
  35. /** @addtogroup N32G45X_StdPeriph_Driver
  36. * @{
  37. */
  38. /** @addtogroup PWR
  39. * @brief PWR driver modules
  40. * @{
  41. */
  42. /** @addtogroup PWR_Private_TypesDefinitions
  43. * @{
  44. */
  45. /**
  46. * @}
  47. */
  48. /** @addtogroup PWR_Private_Defines
  49. * @{
  50. */
  51. /* --------- PWR registers bit address in the alias region ---------- */
  52. #define PWR_OFFSET (PWR_BASE - PERIPH_BASE)
  53. /* --- CTRL Register ---*/
  54. /* Alias word address of DBKP bit */
  55. #define CTRL_OFFSET (PWR_OFFSET + 0x00)
  56. #define DBKP_BITN 0x08
  57. #define CTRL_DBKP_BB (PERIPH_BB_BASE + (CTRL_OFFSET * 32) + (DBKP_BITN * 4))
  58. /* Alias word address of PVDEN bit */
  59. #define PVDEN_BITN 0x04
  60. #define CTRL_PVDEN_BB (PERIPH_BB_BASE + (CTRL_OFFSET * 32) + (PVDEN_BITN * 4))
  61. /* --- CTRLSTS Register ---*/
  62. /* Alias word address of WKUPEN bit */
  63. #define CTRLSTS_OFFSET (PWR_OFFSET + 0x04)
  64. #define WKUPEN_BITN 0x08
  65. #define CTRLSTS_WKUPEN_BB (PERIPH_BB_BASE + (CTRLSTS_OFFSET * 32) + (WKUPEN_BITN * 4))
  66. /* ------------------ PWR registers bit mask ------------------------ */
  67. /* CTRL register bit mask */
  68. #define CTRL_DS_MASK ((uint32_t)0xFFFFFFFC)
  69. #define CTRL_PRS_MASK ((uint32_t)0xFFFFFD1F)
  70. /**
  71. * @}
  72. */
  73. /** @addtogroup PWR_Private_Macros
  74. * @{
  75. */
  76. /**
  77. * @}
  78. */
  79. /** @addtogroup PWR_Private_Variables
  80. * @{
  81. */
  82. /**
  83. * @}
  84. */
  85. /** @addtogroup PWR_Private_FunctionPrototypes
  86. * @{
  87. */
  88. /**
  89. * @}
  90. */
  91. /** @addtogroup PWR_Private_Functions
  92. * @{
  93. */
  94. /**
  95. * @brief Deinitializes the PWR peripheral registers to their default reset values.
  96. */
  97. void PWR_DeInit(void)
  98. {
  99. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_PWR, ENABLE);
  100. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_PWR, DISABLE);
  101. }
  102. /**
  103. * @brief Enables or disables access to the RTC and backup registers.
  104. * @param Cmd new state of the access to the RTC and backup registers.
  105. * This parameter can be: ENABLE or DISABLE.
  106. */
  107. void PWR_BackupAccessEnable(FunctionalState Cmd)
  108. {
  109. /* Check the parameters */
  110. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  111. *(__IO uint32_t*)CTRL_DBKP_BB = (uint32_t)Cmd;
  112. }
  113. /**
  114. * @brief Enables or disables the Power Voltage Detector(PVD).
  115. * @param Cmd new state of the PVD.
  116. * This parameter can be: ENABLE or DISABLE.
  117. */
  118. void PWR_PvdEnable(FunctionalState Cmd)
  119. {
  120. /* Check the parameters */
  121. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  122. *(__IO uint32_t*)CTRL_PVDEN_BB = (uint32_t)Cmd;
  123. }
  124. /**
  125. * @brief Configures the voltage threshold detected by the Power Voltage Detector(PVD).
  126. * @param PWR_PVDLevel specifies the PVD detection level
  127. * This parameter can be one of the following values:
  128. * @arg PWR_PVDRANGRE_2V2 PVD detection level set to 2.2V
  129. * @arg PWR_PVDRANGRE_2V3 PVD detection level set to 2.3V
  130. * @arg PWR_PVDRANGRE_2V4 PVD detection level set to 2.4V
  131. * @arg PWR_PVDRANGRE_2V5 PVD detection level set to 2.5V
  132. * @arg PWR_PVDRANGRE_2V6 PVD detection level set to 2.6V
  133. * @arg PWR_PVDRANGRE_2V7 PVD detection level set to 2.7V
  134. * @arg PWR_PVDRANGRE_2V8 PVD detection level set to 2.8V
  135. * @arg PWR_PVDRANGRE_2V9 PVD detection level set to 2.9V
  136. */
  137. void PWR_PvdRangeConfig(uint32_t PWR_PVDLevel)
  138. {
  139. uint32_t tmpregister = 0;
  140. /* Check the parameters */
  141. assert_param(IS_PWR_PVD_LEVEL(PWR_PVDLevel));
  142. tmpregister = PWR->CTRL;
  143. /* Clear PRS[7:5] bits */
  144. tmpregister &= CTRL_PRS_MASK;
  145. /* Set PRS[7:5] bits according to PWR_PVDLevel value */
  146. tmpregister |= PWR_PVDLevel;
  147. /* Store the new value */
  148. PWR->CTRL = tmpregister;
  149. }
  150. /**
  151. * @brief Enables or disables the WakeUp Pin functionality.
  152. * @param Cmd new state of the WakeUp Pin functionality.
  153. * This parameter can be: ENABLE or DISABLE.
  154. */
  155. void PWR_WakeUpPinEnable(FunctionalState Cmd)
  156. {
  157. /* Check the parameters */
  158. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  159. *(__IO uint32_t*)CTRLSTS_WKUPEN_BB = (uint32_t)Cmd;
  160. }
  161. /**
  162. * @brief Enters SLEEP mode.
  163. * @param SLEEPONEXIT specifies the SLEEPONEXIT state in SLEEP mode.
  164. * This parameter can be one of the following values:
  165. * @arg 0 SLEEP mode with SLEEPONEXIT disable
  166. * @arg 1 SLEEP mode with SLEEPONEXIT enable
  167. * @param PWR_STOPEntry specifies if SLEEP mode in entered with WFI or WFE instruction.
  168. * This parameter can be one of the following values:
  169. * @arg PWR_STOPENTRY_WFI enter SLEEP mode with WFI instruction
  170. * @arg PWR_STOPENTRY_WFE enter SLEEP mode with WFE instruction
  171. */
  172. void PWR_EnterSLEEPMode(uint8_t SLEEPONEXIT, uint8_t PWR_STOPEntry)
  173. {
  174. // uint32_t tmpregister = 0;
  175. /* Check the parameters */
  176. assert_param(IS_PWR_STOP_ENTRY(PWR_STOPEntry));
  177. /* CLEAR SLEEPDEEP bit of Cortex System Control Register */
  178. SCB->SCR &= (uint32_t) ~((uint32_t)SCB_SCR_SLEEPDEEP);
  179. /* Select SLEEPONEXIT mode entry --------------------------------------------------*/
  180. if (SLEEPONEXIT == 1)
  181. {
  182. /* the MCU enters Sleep mode as soon as it exits the lowest priority INTSTS */
  183. SCB->SCR |= SCB_SCR_SLEEPONEXIT;
  184. }
  185. else if (SLEEPONEXIT == 0)
  186. {
  187. /* Sleep-now */
  188. SCB->SCR &= (uint32_t) ~((uint32_t)SCB_SCR_SLEEPONEXIT);
  189. }
  190. /* Select SLEEP mode entry --------------------------------------------------*/
  191. if (PWR_STOPEntry == PWR_STOPENTRY_WFI)
  192. {
  193. /* Request Wait For Interrupt */
  194. __WFI();
  195. }
  196. else
  197. {
  198. /* Request Wait For Event */
  199. __SEV();
  200. __WFE();
  201. __WFE();
  202. }
  203. }
  204. /**
  205. * @brief Enters STOP mode.
  206. * @param PWR_Regulator specifies the regulator state in STOP mode.
  207. * This parameter can be one of the following values:
  208. * @arg PWR_REGULATOR_ON STOP mode with regulator ON
  209. * @arg PWR_REGULATOR_LOWPOWER STOP mode with regulator in low power mode
  210. * @param PWR_STOPEntry specifies if STOP mode in entered with WFI or WFE instruction.
  211. * This parameter can be one of the following values:
  212. * @arg PWR_STOPENTRY_WFI enter STOP mode with WFI instruction
  213. * @arg PWR_STOPENTRY_WFE enter STOP mode with WFE instruction
  214. */
  215. void PWR_EnterStopState(uint32_t PWR_Regulator, uint8_t PWR_STOPEntry)
  216. {
  217. uint32_t tmpregister = 0;
  218. /* Check the parameters */
  219. assert_param(IS_PWR_REGULATOR(PWR_Regulator));
  220. assert_param(IS_PWR_STOP_ENTRY(PWR_STOPEntry));
  221. /* Select the regulator state in STOP mode ---------------------------------*/
  222. tmpregister = PWR->CTRL;
  223. /* Clear PDS and LPS bits */
  224. tmpregister &= CTRL_DS_MASK;
  225. /* Set LPS bit according to PWR_Regulator value */
  226. tmpregister |= PWR_Regulator;
  227. /* Store the new value */
  228. PWR->CTRL = tmpregister;
  229. /* Set SLEEPDEEP bit of Cortex System Control Register */
  230. SCB->SCR |= SCB_SCR_SLEEPDEEP;
  231. /* Select STOP mode entry --------------------------------------------------*/
  232. if (PWR_STOPEntry == PWR_STOPENTRY_WFI)
  233. {
  234. /* Request Wait For Interrupt */
  235. __WFI();
  236. }
  237. else
  238. {
  239. /* Request Wait For Event */
  240. __SEV();
  241. __WFE();
  242. __WFE();
  243. }
  244. /* Reset SLEEPDEEP bit of Cortex System Control Register */
  245. SCB->SCR &= (uint32_t) ~((uint32_t)SCB_SCR_SLEEPDEEP);
  246. }
  247. /**
  248. * @brief Enters STOP2 mode.
  249. * @param PWR_STOPEntry specifies if STOP2 mode in entered with WFI or WFE instruction.
  250. * This parameter can be one of the following values:
  251. * @arg PWR_STOPENTRY_WFI enter STOP2 mode with WFI instruction
  252. * @arg PWR_STOPENTRY_WFE enter STOP2 mode with WFE instruction
  253. */
  254. void PWR_EnterSTOP2Mode(uint8_t PWR_STOPEntry)
  255. {
  256. uint32_t tmpregister = 0;
  257. /* Check the parameters */
  258. assert_param(IS_PWR_STOP_ENTRY(PWR_STOPEntry));
  259. /* Select the regulator state in STOP2 mode ---------------------------------*/
  260. tmpregister = PWR->CTRL;
  261. /* Clear PDS and LPS bits */
  262. tmpregister &= CTRL_DS_MASK;
  263. /* Store the new value */
  264. PWR->CTRL = tmpregister;
  265. /*STOP2 sleep mode control-stop2s*/
  266. PWR->CTRL2 |= PWR_CTRL2_STOP2S;
  267. /* Set SLEEPDEEP bit of Cortex System Control Register */
  268. SCB->SCR |= SCB_SCR_SLEEPDEEP;
  269. // PWR_CTRL2.BIT0 STOP2S need?
  270. /* Select STOP mode entry --------------------------------------------------*/
  271. if (PWR_STOPEntry == PWR_STOPENTRY_WFI)
  272. {
  273. /* Request Wait For Interrupt */
  274. __WFI();
  275. }
  276. else
  277. {
  278. /* Request Wait For Event */
  279. __SEV();
  280. __WFE();
  281. __WFE();
  282. }
  283. /* Reset SLEEPDEEP bit of Cortex System Control Register */
  284. SCB->SCR &= (uint32_t) ~((uint32_t)SCB_SCR_SLEEPDEEP);
  285. }
  286. /**
  287. * @brief Enters STANDBY mode.
  288. */
  289. void PWR_EnterStandbyState(void)
  290. {
  291. /* Clear Wake-up flag */
  292. PWR->CTRL |= PWR_CTRL_CWKUP;
  293. /* Clear PDS and LPS bits */
  294. PWR->CTRL &= CTRL_DS_MASK;
  295. /* Select STANDBY mode */
  296. PWR->CTRL |= PWR_CTRL_PDS;
  297. /* Set SLEEPDEEP bit of Cortex System Control Register */
  298. SCB->SCR |= SCB_SCR_SLEEPDEEP;
  299. /* This option is used to ensure that store operations are completed */
  300. #if defined(__CC_ARM)
  301. __force_stores();
  302. #endif
  303. /* Request Wait For Interrupt */
  304. __WFI();
  305. }
  306. /**
  307. * @brief Checks whether the specified PWR flag is set or not.
  308. * @param PWR_FLAG specifies the flag to check.
  309. * This parameter can be one of the following values:
  310. * @arg PWR_WU_FLAG Wake Up flag
  311. * @arg PWR_SB_FLAG StandBy flag
  312. * @arg PWR_PVDO_FLAG PVD Output
  313. * @arg PWR_VBATF_FLAG VBAT flag
  314. * @return The new state of PWR_FLAG (SET or RESET).
  315. */
  316. FlagStatus PWR_GetFlagStatus(uint32_t PWR_FLAG)
  317. {
  318. FlagStatus bitstatus = RESET;
  319. /* Check the parameters */
  320. assert_param(IS_PWR_GET_FLAG(PWR_FLAG));
  321. if ((PWR->CTRLSTS & PWR_FLAG) != (uint32_t)RESET)
  322. {
  323. bitstatus = SET;
  324. }
  325. else
  326. {
  327. bitstatus = RESET;
  328. }
  329. /* Return the flag status */
  330. return bitstatus;
  331. }
  332. /**
  333. * @brief Clears the PWR's pending flags.
  334. * @param PWR_FLAG specifies the flag to clear.
  335. * This parameter can be one of the following values:
  336. * @arg PWR_WU_FLAG Wake Up flag
  337. * @arg PWR_SB_FLAG StandBy and VBAT flag
  338. */
  339. void PWR_ClearFlag(uint32_t PWR_FLAG)
  340. {
  341. /* Check the parameters */
  342. assert_param(IS_PWR_CLEAR_FLAG(PWR_FLAG));
  343. PWR->CTRL |= PWR_FLAG << 2;
  344. }
  345. /**
  346. * @}
  347. */
  348. /**
  349. * @}
  350. */
  351. /**
  352. * @}
  353. */