|
15 | 15 | */
|
16 | 16 | #include "sleep_api.h"
|
17 | 17 |
|
| 18 | +// Number of warm-up cycle = warm-up time to set / input frequency cycle (s) |
| 19 | +// Number of 3*10^-6 (s) / (1/12 (MHz)) = 60000 = 0xea60 |
| 20 | +#define CG_WUODR_INT ((uint16_t)0xea60) |
| 21 | + |
| 22 | +static void external_losc_enable(void); |
| 23 | + |
18 | 24 | void hal_sleep(void)
|
19 | 25 | {
|
20 | 26 | // Set low power consumption mode IDLE
|
21 | 27 | CG_SetSTBYMode(CG_STBY_MODE_IDLE);
|
22 |
| - __DSB(); |
23 | 28 | // Enter idle mode
|
24 | 29 | __WFI();
|
25 | 30 | }
|
26 | 31 |
|
27 | 32 | void hal_deepsleep(void)
|
28 | 33 | {
|
29 |
| - // deepsleep = sleep because, TMPM46BF10FG does not support the low power |
30 |
| - // consumption mode configured with the SLEEPDEEP bit in the Cortex-M4 core. |
31 |
| - // Setting the bit of the system control register is prohibited. |
32 |
| - hal_sleep(); |
| 34 | + // Set low power consumption mode STOP1 |
| 35 | + CG_SetSTBYMode(CG_STBY_MODE_STOP1); |
| 36 | + // Setup warm up time |
| 37 | + CG_SetWarmUpTime(CG_WARM_UP_SRC_OSC_EXT_HIGH, CG_WUODR_INT); |
| 38 | + // Enter stop1 mode |
| 39 | + __WFI(); |
| 40 | + // Switch over from IHOSC to EHOSC |
| 41 | + external_losc_enable(); |
| 42 | +} |
| 43 | + |
| 44 | +static void external_losc_enable(void) |
| 45 | +{ |
| 46 | + // Enable high-speed oscillator |
| 47 | + CG_SetFosc(CG_FOSC_OSC_EXT, ENABLE); |
| 48 | + // Select internal(fIHOSC) as warm-up clock |
| 49 | + CG_SetWarmUpTime(CG_WARM_UP_SRC_OSC_INT_HIGH, CG_WUODR_INT); |
| 50 | + // Start warm-up |
| 51 | + CG_StartWarmUp(); |
| 52 | + // Wait until EHOSC become stable |
| 53 | + while (CG_GetWarmUpState() != DONE) { |
| 54 | + // Do nothing |
| 55 | + } |
| 56 | + |
| 57 | + // Set fosc source |
| 58 | + CG_SetFoscSrc(CG_FOSC_OSC_EXT); |
| 59 | + // Wait for <OSCSEL> to become "1" |
| 60 | + while (CG_GetFoscSrc() != CG_FOSC_OSC_EXT) { |
| 61 | + // Do nothing |
| 62 | + } |
| 63 | + |
| 64 | + // Stop IHOSC |
| 65 | + CG_SetFosc(CG_FOSC_OSC_INT, DISABLE); |
33 | 66 | }
|
0 commit comments