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