mbed library sources. Supersedes mbed-src.

Fork of mbed-dev by mbed official

Committer:
sivasuren
Date:
Fri Nov 25 07:57:40 2016 +0000
Revision:
150:da61ba4e9755
Parent:
149:156823d33999
surendar changes

Who changed what in which revision?

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