mbed-os
Fork of mbed-os by
targets/TARGET_NUVOTON/TARGET_NUC472/sleep.c@0:f269e3021894, 2016-10-23 (annotated)
- Committer:
- elessair
- Date:
- Sun Oct 23 15:10:02 2016 +0000
- Revision:
- 0:f269e3021894
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | /* mbed Microcontroller Library |
elessair | 0:f269e3021894 | 2 | * Copyright (c) 2015-2016 Nuvoton |
elessair | 0:f269e3021894 | 3 | * |
elessair | 0:f269e3021894 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
elessair | 0:f269e3021894 | 5 | * you may not use this file except in compliance with the License. |
elessair | 0:f269e3021894 | 6 | * You may obtain a copy of the License at |
elessair | 0:f269e3021894 | 7 | * |
elessair | 0:f269e3021894 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
elessair | 0:f269e3021894 | 9 | * |
elessair | 0:f269e3021894 | 10 | * Unless required by applicable law or agreed to in writing, software |
elessair | 0:f269e3021894 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
elessair | 0:f269e3021894 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
elessair | 0:f269e3021894 | 13 | * See the License for the specific language governing permissions and |
elessair | 0:f269e3021894 | 14 | * limitations under the License. |
elessair | 0:f269e3021894 | 15 | */ |
elessair | 0:f269e3021894 | 16 | |
elessair | 0:f269e3021894 | 17 | #include "sleep_api.h" |
elessair | 0:f269e3021894 | 18 | #include "serial_api.h" |
elessair | 0:f269e3021894 | 19 | #include "lp_ticker_api.h" |
elessair | 0:f269e3021894 | 20 | |
elessair | 0:f269e3021894 | 21 | #if DEVICE_SLEEP |
elessair | 0:f269e3021894 | 22 | |
elessair | 0:f269e3021894 | 23 | #include "cmsis.h" |
elessair | 0:f269e3021894 | 24 | #include "device.h" |
elessair | 0:f269e3021894 | 25 | #include "objects.h" |
elessair | 0:f269e3021894 | 26 | #include "PeripheralPins.h" |
elessair | 0:f269e3021894 | 27 | |
elessair | 0:f269e3021894 | 28 | void us_ticker_prepare_sleep(struct sleep_s *obj); |
elessair | 0:f269e3021894 | 29 | void us_ticker_wakeup_from_sleep(struct sleep_s *obj); |
elessair | 0:f269e3021894 | 30 | static void mbed_enter_sleep(struct sleep_s *obj); |
elessair | 0:f269e3021894 | 31 | static void mbed_exit_sleep(struct sleep_s *obj); |
elessair | 0:f269e3021894 | 32 | |
elessair | 0:f269e3021894 | 33 | int serial_allow_powerdown(void); |
elessair | 0:f269e3021894 | 34 | int spi_allow_powerdown(void); |
elessair | 0:f269e3021894 | 35 | int i2c_allow_powerdown(void); |
elessair | 0:f269e3021894 | 36 | int pwmout_allow_powerdown(void); |
elessair | 0:f269e3021894 | 37 | |
elessair | 0:f269e3021894 | 38 | /** |
elessair | 0:f269e3021894 | 39 | * Enter Idle mode. |
elessair | 0:f269e3021894 | 40 | */ |
elessair | 0:f269e3021894 | 41 | void sleep(void) |
elessair | 0:f269e3021894 | 42 | { |
elessair | 0:f269e3021894 | 43 | struct sleep_s sleep_obj; |
elessair | 0:f269e3021894 | 44 | sleep_obj.powerdown = 0; |
elessair | 0:f269e3021894 | 45 | mbed_enter_sleep(&sleep_obj); |
elessair | 0:f269e3021894 | 46 | mbed_exit_sleep(&sleep_obj); |
elessair | 0:f269e3021894 | 47 | } |
elessair | 0:f269e3021894 | 48 | |
elessair | 0:f269e3021894 | 49 | /** |
elessair | 0:f269e3021894 | 50 | * Enter Power-down mode while no peripheral is active; otherwise, enter Idle mode. |
elessair | 0:f269e3021894 | 51 | */ |
elessair | 0:f269e3021894 | 52 | void deepsleep(void) |
elessair | 0:f269e3021894 | 53 | { |
elessair | 0:f269e3021894 | 54 | struct sleep_s sleep_obj; |
elessair | 0:f269e3021894 | 55 | sleep_obj.powerdown = 1; |
elessair | 0:f269e3021894 | 56 | mbed_enter_sleep(&sleep_obj); |
elessair | 0:f269e3021894 | 57 | mbed_exit_sleep(&sleep_obj); |
elessair | 0:f269e3021894 | 58 | } |
elessair | 0:f269e3021894 | 59 | |
elessair | 0:f269e3021894 | 60 | static void mbed_enter_sleep(struct sleep_s *obj) |
elessair | 0:f269e3021894 | 61 | { |
elessair | 0:f269e3021894 | 62 | // Check if serial allows entering power-down mode |
elessair | 0:f269e3021894 | 63 | if (obj->powerdown) { |
elessair | 0:f269e3021894 | 64 | obj->powerdown = serial_allow_powerdown(); |
elessair | 0:f269e3021894 | 65 | } |
elessair | 0:f269e3021894 | 66 | // Check if spi allows entering power-down mode |
elessair | 0:f269e3021894 | 67 | if (obj->powerdown) { |
elessair | 0:f269e3021894 | 68 | obj->powerdown = spi_allow_powerdown(); |
elessair | 0:f269e3021894 | 69 | } |
elessair | 0:f269e3021894 | 70 | // Check if i2c allows entering power-down mode |
elessair | 0:f269e3021894 | 71 | if (obj->powerdown) { |
elessair | 0:f269e3021894 | 72 | obj->powerdown = i2c_allow_powerdown(); |
elessair | 0:f269e3021894 | 73 | } |
elessair | 0:f269e3021894 | 74 | // Check if pwmout allows entering power-down mode |
elessair | 0:f269e3021894 | 75 | if (obj->powerdown) { |
elessair | 0:f269e3021894 | 76 | obj->powerdown = pwmout_allow_powerdown(); |
elessair | 0:f269e3021894 | 77 | } |
elessair | 0:f269e3021894 | 78 | // TODO: Check if other peripherals allow entering power-down mode |
elessair | 0:f269e3021894 | 79 | |
elessair | 0:f269e3021894 | 80 | obj->start_us = lp_ticker_read(); |
elessair | 0:f269e3021894 | 81 | // Let us_ticker prepare for power-down or reject it. |
elessair | 0:f269e3021894 | 82 | us_ticker_prepare_sleep(obj); |
elessair | 0:f269e3021894 | 83 | |
elessair | 0:f269e3021894 | 84 | // NOTE(STALE): To pass mbed-drivers test, timer requires to be fine-grained, so its implementation needs HIRC rather than LIRC/LXT as its clock source. |
elessair | 0:f269e3021894 | 85 | // But as CLK_PowerDown()/CLK_Idle() is called, HIRC will be disabled and timer cannot keep counting and alarm. To overcome the dilemma, |
elessair | 0:f269e3021894 | 86 | // just make CPU halt and compromise power saving. |
elessair | 0:f269e3021894 | 87 | // NOTE: As CLK_PowerDown()/CLK_Idle() is called, HIRC/HXT will be disabled in normal mode, but not in ICE mode. This may cause confusion in development. |
elessair | 0:f269e3021894 | 88 | |
elessair | 0:f269e3021894 | 89 | if (obj->powerdown) { // Power-down mode (HIRC/HXT disabled, LIRC/LXT enabled) |
elessair | 0:f269e3021894 | 90 | SYS_UnlockReg(); |
elessair | 0:f269e3021894 | 91 | CLK_PowerDown(); |
elessair | 0:f269e3021894 | 92 | SYS_LockReg(); |
elessair | 0:f269e3021894 | 93 | } |
elessair | 0:f269e3021894 | 94 | else { // CPU halt mode (HIRC/HXT enabled, LIRC/LXT enabled) |
elessair | 0:f269e3021894 | 95 | // NOTE: NUC472's CLK_Idle() will also disable HIRC/HXT. |
elessair | 0:f269e3021894 | 96 | SYS_UnlockReg(); |
elessair | 0:f269e3021894 | 97 | SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk; |
elessair | 0:f269e3021894 | 98 | CLK->PWRCTL &= ~CLK_PWRCTL_PDEN_Msk; |
elessair | 0:f269e3021894 | 99 | __WFI(); |
elessair | 0:f269e3021894 | 100 | SYS_LockReg(); |
elessair | 0:f269e3021894 | 101 | } |
elessair | 0:f269e3021894 | 102 | __NOP(); |
elessair | 0:f269e3021894 | 103 | __NOP(); |
elessair | 0:f269e3021894 | 104 | __NOP(); |
elessair | 0:f269e3021894 | 105 | __NOP(); |
elessair | 0:f269e3021894 | 106 | |
elessair | 0:f269e3021894 | 107 | obj->end_us = lp_ticker_read(); |
elessair | 0:f269e3021894 | 108 | obj->period_us = (obj->end_us > obj->start_us) ? (obj->end_us - obj->start_us) : (uint32_t) ((uint64_t) obj->end_us + 0xFFFFFFFFu - obj->start_us); |
elessair | 0:f269e3021894 | 109 | // Let us_ticker recover from power-down. |
elessair | 0:f269e3021894 | 110 | us_ticker_wakeup_from_sleep(obj); |
elessair | 0:f269e3021894 | 111 | } |
elessair | 0:f269e3021894 | 112 | |
elessair | 0:f269e3021894 | 113 | static void mbed_exit_sleep(struct sleep_s *obj) |
elessair | 0:f269e3021894 | 114 | { |
elessair | 0:f269e3021894 | 115 | // TODO: TO BE CONTINUED |
elessair | 0:f269e3021894 | 116 | |
elessair | 0:f269e3021894 | 117 | (void)obj; |
elessair | 0:f269e3021894 | 118 | } |
elessair | 0:f269e3021894 | 119 | |
elessair | 0:f269e3021894 | 120 | #endif |