Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Sun May 14 23:18:57 2017 +0000
Revision:
18:6a4db94011d3
Publishing again

Who changed what in which revision?

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