lol

Dependencies:   MMA8451Q

Fork of Application by Mateusz Kowalik

Committer:
danix
Date:
Sun Jan 21 22:28:30 2018 +0000
Revision:
12:3a30cdffa27c
Parent:
10:41552d038a69
Working acelerometer and mouse

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
Zaitsev 10:41552d038a69 61 void mbed_enter_sleep(struct sleep_s *obj)
Zaitsev 10:41552d038a69 62 {
Zaitsev 10:41552d038a69 63 // Check if serial allows entering power-down mode
Zaitsev 10:41552d038a69 64 if (obj->powerdown) {
Zaitsev 10:41552d038a69 65 obj->powerdown = serial_allow_powerdown();
Zaitsev 10:41552d038a69 66 }
Zaitsev 10:41552d038a69 67 // Check if spi allows entering power-down mode
Zaitsev 10:41552d038a69 68 if (obj->powerdown) {
Zaitsev 10:41552d038a69 69 obj->powerdown = spi_allow_powerdown();
Zaitsev 10:41552d038a69 70 }
Zaitsev 10:41552d038a69 71 // Check if i2c allows entering power-down mode
Zaitsev 10:41552d038a69 72 if (obj->powerdown) {
Zaitsev 10:41552d038a69 73 obj->powerdown = i2c_allow_powerdown();
Zaitsev 10:41552d038a69 74 }
Zaitsev 10:41552d038a69 75 // Check if pwmout allows entering power-down mode
Zaitsev 10:41552d038a69 76 if (obj->powerdown) {
Zaitsev 10:41552d038a69 77 obj->powerdown = pwmout_allow_powerdown();
Zaitsev 10:41552d038a69 78 }
Zaitsev 10:41552d038a69 79 // TODO: Check if other peripherals allow entering power-down mode
Zaitsev 10:41552d038a69 80
Zaitsev 10:41552d038a69 81 obj->start_us = lp_ticker_read();
Zaitsev 10:41552d038a69 82 // Let us_ticker prepare for power-down or reject it.
Zaitsev 10:41552d038a69 83 us_ticker_prepare_sleep(obj);
Zaitsev 10:41552d038a69 84
Zaitsev 10:41552d038a69 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.
Zaitsev 10:41552d038a69 86 // 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 87 // just make CPU halt and compromise power saving.
Zaitsev 10:41552d038a69 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.
Zaitsev 10:41552d038a69 89
Zaitsev 10:41552d038a69 90 if (obj->powerdown) { // Power-down mode (HIRC/HXT disabled, LIRC/LXT enabled)
Zaitsev 10:41552d038a69 91 SYS_UnlockReg();
Zaitsev 10:41552d038a69 92 CLK_PowerDown();
Zaitsev 10:41552d038a69 93 SYS_LockReg();
Zaitsev 10:41552d038a69 94 }
Zaitsev 10:41552d038a69 95 else { // CPU halt mode (HIRC/HXT enabled, LIRC/LXT enabled)
Zaitsev 10:41552d038a69 96 SYS_UnlockReg();
Zaitsev 10:41552d038a69 97 CLK_Idle();
Zaitsev 10:41552d038a69 98 SYS_LockReg();
Zaitsev 10:41552d038a69 99 }
Zaitsev 10:41552d038a69 100 __NOP();
Zaitsev 10:41552d038a69 101 __NOP();
Zaitsev 10:41552d038a69 102 __NOP();
Zaitsev 10:41552d038a69 103 __NOP();
Zaitsev 10:41552d038a69 104
Zaitsev 10:41552d038a69 105 obj->end_us = lp_ticker_read();
Zaitsev 10:41552d038a69 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);
Zaitsev 10:41552d038a69 107 // Let us_ticker recover from power-down.
Zaitsev 10:41552d038a69 108 us_ticker_wakeup_from_sleep(obj);
Zaitsev 10:41552d038a69 109 }
Zaitsev 10:41552d038a69 110
Zaitsev 10:41552d038a69 111 void mbed_exit_sleep(struct sleep_s *obj)
Zaitsev 10:41552d038a69 112 {
Zaitsev 10:41552d038a69 113 // TODO: TO BE CONTINUED
Zaitsev 10:41552d038a69 114
Zaitsev 10:41552d038a69 115 (void)obj;
Zaitsev 10:41552d038a69 116 }
Zaitsev 10:41552d038a69 117
Zaitsev 10:41552d038a69 118 #endif