Firmware SDK
twr_system.h
1 #ifndef _TWR_SYSTEM_H
2 #define _TWR_SYSTEM_H
3 
4 #include <stm32l0xx.h>
5 #include <twr_common.h>
6 
7 typedef enum
8 {
9  TWR_SYSTEM_CLOCK_MSI = 0,
10  TWR_SYSTEM_CLOCK_HSI = 1,
11  TWR_SYSTEM_CLOCK_PLL = 2
12 
13 } twr_system_clock_t;
14 
15 void twr_system_init(void);
16 
17 static inline void twr_system_sleep(void)
18 {
19  __WFI();
20 
21  // TODO: Is there a better way to determine whether the RTC RSF bit needs to
22  // be cleared?
23 
24  if (SCB->SCR & SCB_SCR_SLEEPDEEP_Msk) {
25  /* RTC shadow registers are not updated in deep sleep modes. When waking
26  * up from deep sleep, clear the RSF bit in RTC->ISR so that functions
27  * that read the RTC calendar know whether they need to wait 2 RTCCLK
28  * periods for the registers to re-initialize.
29  *
30  * Note: We intentially do NOT use twr_rtc_{enable,disable}_write here
31  * for performance reasons. This function is on the scheduler hot path
32  * and should be as quick as possible. A minor drawback is that if the
33  * application leaves the RTC open for writes when control returns to
34  * the main scheduler function, writes to RTC will be disabled
35  * regardless of the _twr_rtc_writable_semaphore values. Leaving the RTC
36  * open for writes is considered a bug anayway.
37  */
38  RTC->WPR = 0xca;
39  RTC->WPR = 0x53;
40  RTC->ISR &= ~RTC_ISR_RSF;
41  RTC->WPR = 0xff;
42  }
43 }
44 
45 twr_system_clock_t twr_system_clock_get(void);
46 
47 void twr_system_hsi16_enable(void);
48 
49 void twr_system_hsi16_disable(void);
50 
51 void twr_system_pll_enable(void);
52 
53 void twr_system_pll_disable(void);
54 
55 void twr_system_deep_sleep_disable(void);
56 
57 void twr_system_deep_sleep_enable(void);
58 
59 void twr_system_enter_standby_mode(void);
60 
61 uint32_t twr_system_get_clock(void);
62 
63 void twr_system_reset(void);
64 
65 bool twr_system_get_vbus_sense(void);
66 
67 #endif // _TWR_SYSTEM_H