Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed_wait_api_rtos.cpp Source File

mbed_wait_api_rtos.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2019 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 // This implementation of the wait functions will be compiled only
00019 // if the RTOS is present. Note that we still use these old
00020 // bare metal versions of wait and wait_ms rather than using
00021 // thread_sleep_for for backwards compatibility. People should
00022 // be prompted to shift via their deprecation.
00023 #ifdef MBED_CONF_RTOS_PRESENT
00024 
00025 #include "platform/mbed_wait_api.h"
00026 #include "hal/us_ticker_api.h"
00027 #include "rtos/ThisThread.h"
00028 #include "platform/mbed_critical.h"
00029 #include "platform/mbed_power_mgmt.h"
00030 #include "platform/mbed_error.h"
00031 
00032 void wait(float s)
00033 {
00034     if ((s >= 0.01f)  && core_util_are_interrupts_enabled()) {
00035         rtos::ThisThread::sleep_for(s * 1000.0f);
00036         return;
00037     }
00038 
00039     uint32_t us = (s * 1000000.0f);
00040     const ticker_data_t *const ticker = get_us_ticker_data();
00041     uint32_t start = ticker_read(ticker);
00042     if ((us >= 1000) && core_util_are_interrupts_enabled()) {
00043         // Use the RTOS to wait for millisecond delays if possible
00044         sleep_manager_lock_deep_sleep();
00045         rtos::ThisThread::sleep_for((uint32_t)us / 1000);
00046         sleep_manager_unlock_deep_sleep();
00047     }
00048     // Use busy waiting for sub-millisecond delays, or for the whole
00049     // interval if interrupts are not enabled
00050     while ((ticker_read(ticker) - start) < (uint32_t)us);
00051 }
00052 
00053 /*  The actual time delay may be up to one timer tick less - 1 msec */
00054 void wait_ms(int ms)
00055 {
00056     if (core_util_is_isr_active() || !core_util_are_interrupts_enabled()) {
00057 #if defined(MBED_TRAP_ERRORS_ENABLED) && MBED_TRAP_ERRORS_ENABLED
00058         MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_INVALID_OPERATION),
00059                    "Deprecated behavior: milli-sec delay should not be used in interrupt.\n");
00060 #else
00061         wait_us(ms * 1000);
00062 #endif
00063     } else {
00064         rtos::ThisThread::sleep_for(ms);
00065     }
00066 }
00067 
00068 #endif // #if MBED_CONF_RTOS_PRESENT