mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

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-2013 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.
00020 #ifdef MBED_CONF_RTOS_PRESENT
00021 
00022 #include "platform/mbed_wait_api.h"
00023 #include "hal/us_ticker_api.h"
00024 #include "rtos/ThisThread.h"
00025 #include "platform/mbed_critical.h"
00026 #include "platform/mbed_power_mgmt.h"
00027 #include "platform/mbed_error.h"
00028 #include "rtos/ThisThread.h"
00029 
00030 void wait(float s)
00031 {
00032     if ((s >= 0.01f)  && core_util_are_interrupts_enabled()) {
00033         wait_ms(s * 1000.0f);
00034         return;
00035     }
00036 
00037     uint32_t us = (s * 1000000.0f);
00038     const ticker_data_t *const ticker = get_us_ticker_data();
00039     uint32_t start = ticker_read(ticker);
00040     if ((us >= 1000) && core_util_are_interrupts_enabled()) {
00041         // Use the RTOS to wait for millisecond delays if possible
00042         sleep_manager_lock_deep_sleep();
00043         rtos::ThisThread::sleep_for((uint32_t)us / 1000);
00044         sleep_manager_unlock_deep_sleep();
00045     }
00046     // Use busy waiting for sub-millisecond delays, or for the whole
00047     // interval if interrupts are not enabled
00048     while ((ticker_read(ticker) - start) < (uint32_t)us);
00049 }
00050 
00051 /*  The actual time delay may be up to one timer tick less - 1 msec */
00052 void wait_ms(int ms)
00053 {
00054     if (core_util_is_isr_active() || !core_util_are_interrupts_enabled()) {
00055 #if defined(MBED_TRAP_ERRORS_ENABLED) && MBED_TRAP_ERRORS_ENABLED
00056         MBED_ERROR(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_INVALID_OPERATION),
00057                    "Deprecated behavior: milli-sec delay should not be used in interrupt.\n");
00058 #else
00059         wait_us(ms * 1000);
00060 #endif
00061     } else {
00062         rtos::ThisThread::sleep_for(ms);
00063     }
00064 }
00065 
00066 /*  The actual time delay may be 1 less usec */
00067 void wait_us(int us)
00068 {
00069     if (us > 10000) {
00070         MBED_WARNING(MBED_MAKE_ERROR(MBED_MODULE_PLATFORM, MBED_ERROR_UNKNOWN),
00071                      "wait_us blocks deep sleep, wait_ms recommended for long delays\n");
00072     }
00073     const ticker_data_t *const ticker = get_us_ticker_data();
00074     uint32_t start = ticker_read(ticker);
00075     while ((ticker_read(ticker) - start) < (uint32_t)us);
00076 }
00077 
00078 #endif // #if MBED_CONF_RTOS_PRESENT
00079