sleep_and_exti.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. #include "stm32l1xx_hal.h"
  2. #include "stm32l1xx_hal_pwr.h"
  3. #include "stm32l1xx_hal_pwr_ex.h"
  4. #include "stm32l1xx_hal_pcd.h"
  5. #include "core/gpio.h"
  6. #include "core/main.h"
  7. #include "core/sleep_and_exti.h"
  8. #include "core/csect.h"
  9. #include <intrinsics.h>
  10. #include "core/config.h"
  11. #include "core/config_pins.h"
  12. // USB_VBUS_WKPIN
  13. // Specifies the special wake up pin (WKUP1..WKUP3) for waking up from USB_VBUS signal
  14. // Possible values: PWR_WAKEUP_PIN1, PWR_WAKEUP_PIN2, PWR_WAKEUP_PIN3, or undefined
  15. #define USB_VBUS_WKPIN (CONFIG_PIN__USB__VBUSWKUP)
  16. // USB_VBUS_LINE
  17. // Specifies the GPIO line number for USB_VBUS detecting using EXTI GPIO
  18. // Possible values: GPIO_PIN_0 .. GPIO_PIN_15
  19. #define USB_VBUS_LINE (CONFIG_PIN__USB__VBUSDETECT)
  20. // USB_VBUS_PORT
  21. // Specifies the GPIO port for USB_VBUS detecting using EXTI GPIO
  22. // Possible values: GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOH
  23. #define USB_VBUS_PORT (CONFIG_PORT__USB__VBUSDETECT)
  24. // USB_VBUS_PORT_CLKEN
  25. // Enables the corresponding port clocking (see USB_VBUS_PORT)
  26. #define USB_VBUS_PORT_CLKEN() __HAL_RCC_GPIOC_CLK_ENABLE()
  27. #define USB_VBUS_PORT_CLKDS() __HAL_RCC_GPIOC_CLK_DISABLE()
  28. // USB_VBUS_EXTI_IRQ
  29. // Specifies the IRQ-number for USB_VBUS detecting using EXTI GPIO
  30. // Possible values: EXTI15_10_IRQn, EXTI9_5_IRQn, EXTI4_IRQn, EXTI3_IRQn, EXTI2_IRQn, EXTI1_IRQn, EXTI0_IRQn
  31. #define USB_VBUS_EXTI_IRQ (EXTI15_10_IRQn)
  32. // USB_INTERNAL_WKUP_SUPPORT
  33. // Enables internal USB wakeup-event support
  34. #define USB_INTERNAL_WKUP_SUPPORT CONFIG_USB_INTERNAL_WKUP_SUPPORT
  35. // USB_DEBUG_SUPRESS_VBUS_WKUP
  36. // Debug: do not wakeup on VBUS line changing
  37. #define USB_DEBUG_SUPRESS_VBUS_WKUP CONFIG_DEBUG_USB_SUPRESS_VBUS_WKUP
  38. #if CONFIG_SLEEPMANAGER
  39. // This module uses WakeUp Pin #2 as a wakeup source
  40. // This WakeUp Pin (PC13, Pin #2) is connected to USB_VBUS
  41. // Rising edge means the USB cable plugging in
  42. // Falling edge means the USB cable unplugging
  43. static bool bInitialized = false;
  44. static bool bUSBActive = false; // shows if USB in suspend mode or not
  45. static bool bUSBPlugged = false; // shows if USB cable connected (USB VBUS Pin monitoring)
  46. static eSleepManager_WakeupReason_t xWakeupReason = eSleepManager_GenericWakeup;
  47. static bool SleepManager_Init();
  48. static void SleepManager_DeInit();
  49. static bool SleepManager_SetUSBWakeUp( bool );
  50. static bool SleepManager_EnterSleepTimeout( uint32_t );
  51. static bool SleepManager_EnterSleep();
  52. static bool SleepManager_GetUSBPlugged();
  53. static bool SleepManager_GetUSBActive();
  54. static eSleepManager_WakeupReason_t SleepManager_Sleep();
  55. static eSleepManager_WakeupReason_t SleepManager_SleepTimeout( uint32_t );
  56. static bool SleepManager_USBPlugStatusUpdate();
  57. const sSleepManager_Handle_t SleepManagerHandle = {
  58. .Init = SleepManager_Init,
  59. .SetUSBWakeup = SleepManager_SetUSBWakeUp,
  60. .Sleep = SleepManager_Sleep,
  61. .SleepTimeout = SleepManager_SleepTimeout,
  62. .GetUSBPlugged = SleepManager_GetUSBPlugged,
  63. .GetUSBActive = SleepManager_GetUSBActive,
  64. .DeInit = SleepManager_DeInit,
  65. };
  66. static void SleepManager_USBSuspendEvent();
  67. static void SleepManager_USBResumeEvent();
  68. const sSleepManager_Notify_t SleepManagerNotify = {
  69. .USBSuspendEvent = SleepManager_USBSuspendEvent,
  70. .USBResumeEvent = SleepManager_USBResumeEvent,
  71. };
  72. static bool SleepManager_Init()
  73. {
  74. __DI__
  75. HAL_PWR_DisableSleepOnExit();
  76. HAL_PWREx_EnableFastWakeUp();
  77. HAL_PWREx_DisableLowPowerRunMode();
  78. bInitialized = true;
  79. SleepManager_SetUSBWakeUp( false );
  80. SleepManager_USBPlugStatusUpdate();
  81. __EI__
  82. return true;
  83. }
  84. static void SleepManager_DeInit()
  85. {
  86. __DI__
  87. HAL_PWREx_DisableFastWakeUp();
  88. HAL_PWR_DisableSleepOnExit();
  89. HAL_PWREx_DisableLowPowerRunMode();
  90. SleepManager_SetUSBWakeUp( false );
  91. bInitialized = false;
  92. __EI__
  93. }
  94. static bool SleepManager_USBPlugStatusUpdate()
  95. {
  96. bool bValue = (GPIO_PIN_SET == HAL_GPIO_ReadPin( USB_VBUS_PORT, USB_VBUS_LINE ));
  97. __DI__
  98. bUSBPlugged = bValue;
  99. __EI__
  100. return bValue;
  101. }
  102. static bool SleepManager_SetUSBWakeUp( bool state )
  103. {
  104. bool ret = false;
  105. GPIO_InitTypeDef GPIO_InitStruct = {0};
  106. __DI__
  107. if( bInitialized )
  108. {
  109. if( state )
  110. {
  111. // Configure GPIO pins for VBUS
  112. #if USB_DEBUG_SUPRESS_VBUS_WKUP == 0
  113. // Enable port clocking
  114. USB_VBUS_PORT_CLKEN();
  115. // Configure EXTI line
  116. GPIO_InitStruct.Pin = USB_VBUS_LINE;
  117. GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING; // use rising and falling edges
  118. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  119. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  120. HAL_GPIO_Init( USB_VBUS_PORT, &GPIO_InitStruct );
  121. // Enable EXTI Interrupt
  122. HAL_NVIC_EnableIRQ( USB_VBUS_EXTI_IRQ );
  123. #ifdef USB_VBUS_WKPIN
  124. HAL_PWR_EnableWakeUpPin( USB_VBUS_WKPIN );
  125. #endif
  126. #endif
  127. #if USB_INTERNAL_WKUP_SUPPORT
  128. __HAL_USB_WAKEUP_EXTI_ENABLE_IT(); // Enable corresponding internal EXTI line (USB Wakeup)
  129. HAL_NVIC_EnableIRQ( USB_FS_WKUP_IRQn );
  130. #endif
  131. }
  132. else
  133. {
  134. #if USB_INTERNAL_WKUP_SUPPORT
  135. HAL_NVIC_DisableIRQ( USB_FS_WKUP_IRQn );
  136. __HAL_USB_WAKEUP_EXTI_DISABLE_IT(); // Disable corresponding internal EXTI line (USB Wakeup)
  137. #endif
  138. #if USB_DEBUG_SUPRESS_VBUS_WKUP == 0
  139. #ifdef USB_VBUS_WKPIN
  140. HAL_PWR_DisableWakeUpPin( PWR_WAKEUP_PIN2 );
  141. #endif
  142. // DeConfigure EXTI line, configure as Input
  143. GPIO_InitStruct.Pin = USB_VBUS_LINE;
  144. GPIO_InitStruct.Mode = GPIO_MODE_INPUT; // use port as input to poll signal
  145. GPIO_InitStruct.Pull = GPIO_NOPULL;
  146. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
  147. HAL_GPIO_Init( USB_VBUS_PORT, &GPIO_InitStruct );
  148. // Disable EXTI Interrupt
  149. HAL_NVIC_DisableIRQ( USB_VBUS_EXTI_IRQ );
  150. // DO NOT DISABLE PORT CLOCKING DUE TO IT CAN BE IN USE!!!
  151. /* __ USB_VBUS_PORT_CLKDS(); __ */
  152. #else
  153. // DeConfigure EXTI line, configure as Input
  154. GPIO_InitStruct.Pin = USB_VBUS_LINE;
  155. GPIO_InitStruct.Mode = GPIO_MODE_INPUT; // use port as input to poll signal
  156. GPIO_InitStruct.Pull = GPIO_NOPULL;
  157. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
  158. HAL_GPIO_Init( USB_VBUS_PORT, &GPIO_InitStruct );
  159. #endif
  160. }
  161. ret = true;
  162. }
  163. __EI__
  164. return ret;
  165. }
  166. // SleepManager_USBSuspendEvent callback
  167. // Is called by Low-Level PCD Module (USB HAL) to inform the high level
  168. // about the fact that USB enters in SUSPEND mode
  169. static void SleepManager_USBSuspendEvent()
  170. {
  171. __DI__
  172. if( bInitialized )
  173. {
  174. bUSBActive = false;
  175. }
  176. __EI__
  177. }
  178. // SleepManager_USBResumeEvent callback
  179. // Is called by Low-Level PCD Module (USB HAL) to inform the high level
  180. // about the fact that USB enters in RUN mode
  181. static void SleepManager_USBResumeEvent()
  182. {
  183. __DI__
  184. if( bInitialized )
  185. {
  186. bUSBActive = true;
  187. }
  188. __EI__
  189. }
  190. static bool SleepManager_GetUSBPlugged()
  191. {
  192. // Check if initialized:
  193. DI();
  194. if( !bInitialized )
  195. {
  196. EI();
  197. return false;
  198. }
  199. EI();
  200. return SleepManager_USBPlugStatusUpdate();
  201. }
  202. static bool SleepManager_GetUSBActive()
  203. {
  204. bool ret = false;
  205. __DI__ ret = bInitialized && bUSBActive; __EI__
  206. return ret;
  207. }
  208. static eSleepManager_WakeupReason_t SleepManager_GetWakeupReason()
  209. {
  210. eSleepManager_WakeupReason_t xValue;
  211. __DI__ xValue = xWakeupReason; __EI__
  212. return xValue;
  213. }
  214. static bool SleepManager_EnterSleepTimeout( uint32_t timeout )
  215. {
  216. xWakeupReason = eSleepManager_Error;
  217. // HAL_PWR_EnterSLEEPMode( PWR_LOWPOWERREGULATOR_ON,
  218. // PWR_SLEEPENTRY_WFE );
  219. //
  220. return false;
  221. }
  222. static bool SleepManager_EnterSleep( )
  223. {
  224. xWakeupReason = eSleepManager_GenericWakeup;
  225. // disable some clocks for the power consumption reducing?
  226. //RCC_AHBENR
  227. //RCC_AHB1ENR
  228. //RCC_AHB2ENR
  229. // HAL_PWR_EnableSleepOnExit();
  230. #if CONFIG_SLEEPMANAGER_KEEPDEBUGCLK
  231. HAL_DBGMCU_EnableDBGSleepMode();
  232. HAL_DBGMCU_EnableDBGStopMode();
  233. #endif
  234. #if CONFIG_SLEEPMANAGER_DEBUGPIN
  235. CONFIG_SLEEPMANAGER_DEBUGPIN_SETPIN
  236. #endif
  237. #if CONFIG_SLEEPMANAGER_DEBUG_NOSLEEP
  238. __NOP();
  239. #else
  240. HAL_PWR_EnterSLEEPMode( PWR_MAINREGULATOR_ON, // OK
  241. PWR_SLEEPENTRY_WFI ); // OK
  242. // HAL_PWR_EnterSLEEPMode( PWR_LOWPOWERREGULATOR_ON, //??
  243. // PWR_SLEEPENTRY_WFI ); //??
  244. //HAL_PWR_EnterSLEEPMode( PWR_MAINREGULATOR_ON,
  245. // PWR_SLEEPENTRY_WFE );
  246. //HAL_PWR_EnterSTOPMode( PWR_LOWPOWERREGULATOR_ON, //PWR_MAINREGULATOR_ON,
  247. // PWR_STOPENTRY_WFI );
  248. //HAL_PWR_EnterSTOPMode( PWR_LOWPOWERREGULATOR_ON, //PWR_MAINREGULATOR_ON,
  249. // PWR_STOPENTRY_WFE );
  250. #endif
  251. #if CONFIG_SLEEPMANAGER_DEBUGPIN
  252. CONFIG_SLEEPMANAGER_DEBUGPIN_CLRPIN
  253. #endif
  254. return true;
  255. }
  256. static eSleepManager_WakeupReason_t SleepManager_Sleep()
  257. {
  258. if( SleepManager_EnterSleep() )
  259. return SleepManager_GetWakeupReason();
  260. return eSleepManager_Error;
  261. }
  262. static eSleepManager_WakeupReason_t SleepManager_SleepTimeout( uint32_t timeout )
  263. {
  264. if( SleepManager_EnterSleepTimeout( timeout ) )
  265. return SleepManager_GetWakeupReason();
  266. return eSleepManager_Error;
  267. }
  268. #endif
  269. #if USB_INTERNAL_WKUP_SUPPORT
  270. //
  271. // USB-Wakeup Interrupt Event Routing
  272. //
  273. void USB_FS_WKUP_IRQHandler() // startup_stm32l151xb.s
  274. {
  275. xWakeupReason = eSleepManager_USBPlugWakeup;
  276. HAL_PWR_DisableSleepOnExit();
  277. }
  278. #endif
  279. // EXTI line detection callbacks.
  280. // Note: It is required to route the interrupt routine to the
  281. // HAL-function HAL_GPIO_EXTI_IRQHandler()
  282. // e.g. void EXTI4_IRQHandler { HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_4); }
  283. //
  284. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) // stm32l1xx_hal_gpio.c
  285. {
  286. // USB VBUS is connected to PC13.
  287. // This callback is called if any GPIO pin is triggered and
  288. // the corresponding EXTI-line is configured.
  289. // Check if the pin number is corresponds to PC13:
  290. if( USB_VBUS_LINE == GPIO_Pin )
  291. {
  292. #if CONFIG_SLEEPMANAGER
  293. SleepManager_USBPlugStatusUpdate();
  294. xWakeupReason = eSleepManager_USBPlugWakeup;
  295. #endif
  296. HAL_PWR_DisableSleepOnExit();
  297. }
  298. }