test
targets/TARGET_NUVOTON/TARGET_M451/sleep.c@1:8a094db1347f, 2020-11-09 (annotated)
- Committer:
- elijahsj
- Date:
- Mon Nov 09 00:02:47 2020 -0500
- Revision:
- 1:8a094db1347f
test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elijahsj | 1:8a094db1347f | 1 | /* mbed Microcontroller Library |
elijahsj | 1:8a094db1347f | 2 | * Copyright (c) 2015-2016 Nuvoton |
elijahsj | 1:8a094db1347f | 3 | * |
elijahsj | 1:8a094db1347f | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
elijahsj | 1:8a094db1347f | 5 | * you may not use this file except in compliance with the License. |
elijahsj | 1:8a094db1347f | 6 | * You may obtain a copy of the License at |
elijahsj | 1:8a094db1347f | 7 | * |
elijahsj | 1:8a094db1347f | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
elijahsj | 1:8a094db1347f | 9 | * |
elijahsj | 1:8a094db1347f | 10 | * Unless required by applicable law or agreed to in writing, software |
elijahsj | 1:8a094db1347f | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
elijahsj | 1:8a094db1347f | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
elijahsj | 1:8a094db1347f | 13 | * See the License for the specific language governing permissions and |
elijahsj | 1:8a094db1347f | 14 | * limitations under the License. |
elijahsj | 1:8a094db1347f | 15 | */ |
elijahsj | 1:8a094db1347f | 16 | |
elijahsj | 1:8a094db1347f | 17 | #include "sleep_api.h" |
elijahsj | 1:8a094db1347f | 18 | #include "serial_api.h" |
elijahsj | 1:8a094db1347f | 19 | #include "lp_ticker_api.h" |
elijahsj | 1:8a094db1347f | 20 | |
elijahsj | 1:8a094db1347f | 21 | #if DEVICE_SLEEP |
elijahsj | 1:8a094db1347f | 22 | |
elijahsj | 1:8a094db1347f | 23 | #include "cmsis.h" |
elijahsj | 1:8a094db1347f | 24 | #include "device.h" |
elijahsj | 1:8a094db1347f | 25 | #include "objects.h" |
elijahsj | 1:8a094db1347f | 26 | #include "PeripheralPins.h" |
elijahsj | 1:8a094db1347f | 27 | |
elijahsj | 1:8a094db1347f | 28 | static void mbed_enter_sleep(struct sleep_s *obj); |
elijahsj | 1:8a094db1347f | 29 | static void mbed_exit_sleep(struct sleep_s *obj); |
elijahsj | 1:8a094db1347f | 30 | |
elijahsj | 1:8a094db1347f | 31 | int serial_allow_powerdown(void); |
elijahsj | 1:8a094db1347f | 32 | int spi_allow_powerdown(void); |
elijahsj | 1:8a094db1347f | 33 | int i2c_allow_powerdown(void); |
elijahsj | 1:8a094db1347f | 34 | int pwmout_allow_powerdown(void); |
elijahsj | 1:8a094db1347f | 35 | |
elijahsj | 1:8a094db1347f | 36 | /** |
elijahsj | 1:8a094db1347f | 37 | * Enter Idle mode. |
elijahsj | 1:8a094db1347f | 38 | */ |
elijahsj | 1:8a094db1347f | 39 | void hal_sleep(void) |
elijahsj | 1:8a094db1347f | 40 | { |
elijahsj | 1:8a094db1347f | 41 | struct sleep_s sleep_obj; |
elijahsj | 1:8a094db1347f | 42 | sleep_obj.powerdown = 0; |
elijahsj | 1:8a094db1347f | 43 | mbed_enter_sleep(&sleep_obj); |
elijahsj | 1:8a094db1347f | 44 | mbed_exit_sleep(&sleep_obj); |
elijahsj | 1:8a094db1347f | 45 | } |
elijahsj | 1:8a094db1347f | 46 | |
elijahsj | 1:8a094db1347f | 47 | /** |
elijahsj | 1:8a094db1347f | 48 | * Enter Power-down mode while no peripheral is active; otherwise, enter Idle mode. |
elijahsj | 1:8a094db1347f | 49 | */ |
elijahsj | 1:8a094db1347f | 50 | void hal_deepsleep(void) |
elijahsj | 1:8a094db1347f | 51 | { |
elijahsj | 1:8a094db1347f | 52 | struct sleep_s sleep_obj; |
elijahsj | 1:8a094db1347f | 53 | sleep_obj.powerdown = 1; |
elijahsj | 1:8a094db1347f | 54 | mbed_enter_sleep(&sleep_obj); |
elijahsj | 1:8a094db1347f | 55 | mbed_exit_sleep(&sleep_obj); |
elijahsj | 1:8a094db1347f | 56 | } |
elijahsj | 1:8a094db1347f | 57 | |
elijahsj | 1:8a094db1347f | 58 | static void mbed_enter_sleep(struct sleep_s *obj) |
elijahsj | 1:8a094db1347f | 59 | { |
elijahsj | 1:8a094db1347f | 60 | // Check if serial allows entering power-down mode |
elijahsj | 1:8a094db1347f | 61 | if (obj->powerdown) { |
elijahsj | 1:8a094db1347f | 62 | obj->powerdown = serial_allow_powerdown(); |
elijahsj | 1:8a094db1347f | 63 | } |
elijahsj | 1:8a094db1347f | 64 | // Check if spi allows entering power-down mode |
elijahsj | 1:8a094db1347f | 65 | if (obj->powerdown) { |
elijahsj | 1:8a094db1347f | 66 | obj->powerdown = spi_allow_powerdown(); |
elijahsj | 1:8a094db1347f | 67 | } |
elijahsj | 1:8a094db1347f | 68 | // Check if i2c allows entering power-down mode |
elijahsj | 1:8a094db1347f | 69 | if (obj->powerdown) { |
elijahsj | 1:8a094db1347f | 70 | obj->powerdown = i2c_allow_powerdown(); |
elijahsj | 1:8a094db1347f | 71 | } |
elijahsj | 1:8a094db1347f | 72 | // Check if pwmout allows entering power-down mode |
elijahsj | 1:8a094db1347f | 73 | if (obj->powerdown) { |
elijahsj | 1:8a094db1347f | 74 | obj->powerdown = pwmout_allow_powerdown(); |
elijahsj | 1:8a094db1347f | 75 | } |
elijahsj | 1:8a094db1347f | 76 | // TODO: Check if other peripherals allow entering power-down mode |
elijahsj | 1:8a094db1347f | 77 | |
elijahsj | 1:8a094db1347f | 78 | if (obj->powerdown) { // Power-down mode (HIRC/HXT disabled, LIRC/LXT enabled) |
elijahsj | 1:8a094db1347f | 79 | SYS_UnlockReg(); |
elijahsj | 1:8a094db1347f | 80 | CLK_PowerDown(); |
elijahsj | 1:8a094db1347f | 81 | SYS_LockReg(); |
elijahsj | 1:8a094db1347f | 82 | } |
elijahsj | 1:8a094db1347f | 83 | else { // CPU halt mode (HIRC/HXT enabled, LIRC/LXT enabled) |
elijahsj | 1:8a094db1347f | 84 | SYS_UnlockReg(); |
elijahsj | 1:8a094db1347f | 85 | CLK_Idle(); |
elijahsj | 1:8a094db1347f | 86 | SYS_LockReg(); |
elijahsj | 1:8a094db1347f | 87 | } |
elijahsj | 1:8a094db1347f | 88 | __NOP(); |
elijahsj | 1:8a094db1347f | 89 | __NOP(); |
elijahsj | 1:8a094db1347f | 90 | __NOP(); |
elijahsj | 1:8a094db1347f | 91 | __NOP(); |
elijahsj | 1:8a094db1347f | 92 | } |
elijahsj | 1:8a094db1347f | 93 | |
elijahsj | 1:8a094db1347f | 94 | static void mbed_exit_sleep(struct sleep_s *obj) |
elijahsj | 1:8a094db1347f | 95 | { |
elijahsj | 1:8a094db1347f | 96 | // TODO: TO BE CONTINUED |
elijahsj | 1:8a094db1347f | 97 | |
elijahsj | 1:8a094db1347f | 98 | (void)obj; |
elijahsj | 1:8a094db1347f | 99 | } |
elijahsj | 1:8a094db1347f | 100 | |
elijahsj | 1:8a094db1347f | 101 | #endif |