lol

Dependencies:   MMA8451Q

Fork of Application by Mateusz Kowalik

Committer:
Zaitsev
Date:
Tue Jan 10 20:42:26 2017 +0000
Revision:
10:41552d038a69
USB Serial bi-directional bridge

Who changed what in which revision?

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