mbed-os
Dependents: cobaLCDJoyMotor_Thread odometry_omni_3roda_v3 odometry_omni_3roda_v1 odometry_omni_3roda_v2 ... more
hal/mbed_sleep_manager.c@0:b74591d5ab33, 2017-12-11 (annotated)
- Committer:
- be_bryan
- Date:
- Mon Dec 11 17:54:04 2017 +0000
- Revision:
- 0:b74591d5ab33
motor ++
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
be_bryan | 0:b74591d5ab33 | 1 | /* mbed Microcontroller Library |
be_bryan | 0:b74591d5ab33 | 2 | * Copyright (c) 2017 ARM Limited |
be_bryan | 0:b74591d5ab33 | 3 | * |
be_bryan | 0:b74591d5ab33 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
be_bryan | 0:b74591d5ab33 | 5 | * you may not use this file except in compliance with the License. |
be_bryan | 0:b74591d5ab33 | 6 | * You may obtain a copy of the License at |
be_bryan | 0:b74591d5ab33 | 7 | * |
be_bryan | 0:b74591d5ab33 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
be_bryan | 0:b74591d5ab33 | 9 | * |
be_bryan | 0:b74591d5ab33 | 10 | * Unless required by applicable law or agreed to in writing, software |
be_bryan | 0:b74591d5ab33 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
be_bryan | 0:b74591d5ab33 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
be_bryan | 0:b74591d5ab33 | 13 | * See the License for the specific language governing permissions and |
be_bryan | 0:b74591d5ab33 | 14 | * limitations under the License. |
be_bryan | 0:b74591d5ab33 | 15 | */ |
be_bryan | 0:b74591d5ab33 | 16 | |
be_bryan | 0:b74591d5ab33 | 17 | #include "mbed_sleep.h" |
be_bryan | 0:b74591d5ab33 | 18 | #include "mbed_critical.h" |
be_bryan | 0:b74591d5ab33 | 19 | #include "sleep_api.h" |
be_bryan | 0:b74591d5ab33 | 20 | #include "mbed_error.h" |
be_bryan | 0:b74591d5ab33 | 21 | #include <limits.h> |
be_bryan | 0:b74591d5ab33 | 22 | |
be_bryan | 0:b74591d5ab33 | 23 | #if DEVICE_SLEEP |
be_bryan | 0:b74591d5ab33 | 24 | |
be_bryan | 0:b74591d5ab33 | 25 | // deep sleep locking counter. A target is allowed to deep sleep if counter == 0 |
be_bryan | 0:b74591d5ab33 | 26 | static uint16_t deep_sleep_lock = 0U; |
be_bryan | 0:b74591d5ab33 | 27 | |
be_bryan | 0:b74591d5ab33 | 28 | void sleep_manager_lock_deep_sleep(void) |
be_bryan | 0:b74591d5ab33 | 29 | { |
be_bryan | 0:b74591d5ab33 | 30 | core_util_critical_section_enter(); |
be_bryan | 0:b74591d5ab33 | 31 | if (deep_sleep_lock == USHRT_MAX) { |
be_bryan | 0:b74591d5ab33 | 32 | core_util_critical_section_exit(); |
be_bryan | 0:b74591d5ab33 | 33 | error("Deep sleep lock would overflow (> USHRT_MAX)"); |
be_bryan | 0:b74591d5ab33 | 34 | } |
be_bryan | 0:b74591d5ab33 | 35 | core_util_atomic_incr_u16(&deep_sleep_lock, 1); |
be_bryan | 0:b74591d5ab33 | 36 | core_util_critical_section_exit(); |
be_bryan | 0:b74591d5ab33 | 37 | } |
be_bryan | 0:b74591d5ab33 | 38 | |
be_bryan | 0:b74591d5ab33 | 39 | void sleep_manager_unlock_deep_sleep(void) |
be_bryan | 0:b74591d5ab33 | 40 | { |
be_bryan | 0:b74591d5ab33 | 41 | core_util_critical_section_enter(); |
be_bryan | 0:b74591d5ab33 | 42 | if (deep_sleep_lock == 0) { |
be_bryan | 0:b74591d5ab33 | 43 | core_util_critical_section_exit(); |
be_bryan | 0:b74591d5ab33 | 44 | error("Deep sleep lock would underflow (< 0)"); |
be_bryan | 0:b74591d5ab33 | 45 | } |
be_bryan | 0:b74591d5ab33 | 46 | core_util_atomic_decr_u16(&deep_sleep_lock, 1); |
be_bryan | 0:b74591d5ab33 | 47 | core_util_critical_section_exit(); |
be_bryan | 0:b74591d5ab33 | 48 | } |
be_bryan | 0:b74591d5ab33 | 49 | |
be_bryan | 0:b74591d5ab33 | 50 | bool sleep_manager_can_deep_sleep(void) |
be_bryan | 0:b74591d5ab33 | 51 | { |
be_bryan | 0:b74591d5ab33 | 52 | return deep_sleep_lock == 0 ? true : false; |
be_bryan | 0:b74591d5ab33 | 53 | } |
be_bryan | 0:b74591d5ab33 | 54 | |
be_bryan | 0:b74591d5ab33 | 55 | void sleep_manager_sleep_auto(void) |
be_bryan | 0:b74591d5ab33 | 56 | { |
be_bryan | 0:b74591d5ab33 | 57 | core_util_critical_section_enter(); |
be_bryan | 0:b74591d5ab33 | 58 | // debug profile should keep debuggers attached, no deep sleep allowed |
be_bryan | 0:b74591d5ab33 | 59 | #ifdef MBED_DEBUG |
be_bryan | 0:b74591d5ab33 | 60 | hal_sleep(); |
be_bryan | 0:b74591d5ab33 | 61 | #else |
be_bryan | 0:b74591d5ab33 | 62 | if (sleep_manager_can_deep_sleep()) { |
be_bryan | 0:b74591d5ab33 | 63 | hal_deepsleep(); |
be_bryan | 0:b74591d5ab33 | 64 | } else { |
be_bryan | 0:b74591d5ab33 | 65 | hal_sleep(); |
be_bryan | 0:b74591d5ab33 | 66 | } |
be_bryan | 0:b74591d5ab33 | 67 | #endif |
be_bryan | 0:b74591d5ab33 | 68 | core_util_critical_section_exit(); |
be_bryan | 0:b74591d5ab33 | 69 | } |
be_bryan | 0:b74591d5ab33 | 70 | |
be_bryan | 0:b74591d5ab33 | 71 | #else |
be_bryan | 0:b74591d5ab33 | 72 | |
be_bryan | 0:b74591d5ab33 | 73 | // locking is valid only if DEVICE_SLEEP is defined |
be_bryan | 0:b74591d5ab33 | 74 | // we provide empty implementation |
be_bryan | 0:b74591d5ab33 | 75 | |
be_bryan | 0:b74591d5ab33 | 76 | void sleep_manager_lock_deep_sleep(void) |
be_bryan | 0:b74591d5ab33 | 77 | { |
be_bryan | 0:b74591d5ab33 | 78 | |
be_bryan | 0:b74591d5ab33 | 79 | } |
be_bryan | 0:b74591d5ab33 | 80 | |
be_bryan | 0:b74591d5ab33 | 81 | void sleep_manager_unlock_deep_sleep(void) |
be_bryan | 0:b74591d5ab33 | 82 | { |
be_bryan | 0:b74591d5ab33 | 83 | |
be_bryan | 0:b74591d5ab33 | 84 | } |
be_bryan | 0:b74591d5ab33 | 85 | |
be_bryan | 0:b74591d5ab33 | 86 | bool sleep_manager_can_deep_sleep(void) |
be_bryan | 0:b74591d5ab33 | 87 | { |
be_bryan | 0:b74591d5ab33 | 88 | // no sleep implemented |
be_bryan | 0:b74591d5ab33 | 89 | return false; |
be_bryan | 0:b74591d5ab33 | 90 | } |
be_bryan | 0:b74591d5ab33 | 91 | |
be_bryan | 0:b74591d5ab33 | 92 | #endif |