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_us_ticker_api.c Source File

mbed_us_ticker_api.c

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2015 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 #include <stddef.h>
00019 #include "platform/mbed_atomic.h"
00020 #include "hal/us_ticker_api.h"
00021 
00022 #if DEVICE_USTICKER
00023 
00024 static ticker_event_queue_t events = { 0 };
00025 
00026 static ticker_irq_handler_type irq_handler = ticker_irq_handler;
00027 
00028 #if MBED_CONF_TARGET_INIT_US_TICKER_AT_BOOT
00029 
00030 // If we are initializing at boot, we want the timer to be
00031 // always-on, so we block any attempt to free it. We do need
00032 // to pass through init(), as that needs to reset pending
00033 // interrupts.
00034 static void block_us_ticker_free()
00035 {
00036 }
00037 
00038 #else // MBED_CONF_TARGET_INIT_US_TICKER_AT_BOOT
00039 
00040 bool _us_ticker_initialized;
00041 
00042 // If we are not initializing at boot, we want to track
00043 // whether the timer has been initialized. This permits
00044 // a fast path for wait_us.
00045 static void note_us_ticker_init()
00046 {
00047     us_ticker_init();
00048     core_util_atomic_store_bool (&_us_ticker_initialized, true);
00049 }
00050 
00051 static void note_us_ticker_free()
00052 {
00053     core_util_atomic_store_bool (&_us_ticker_initialized, false);
00054     us_ticker_free();
00055 }
00056 
00057 #endif // MBED_CONF_TARGET_INIT_US_TICKER_AT_BOOT
00058 
00059 static const ticker_interface_t us_interface = {
00060 #if MBED_CONF_TARGET_INIT_US_TICKER_AT_BOOT
00061     .init = us_ticker_init,
00062 #else
00063     .init = note_us_ticker_init,
00064 #endif
00065     .read = us_ticker_read,
00066     .disable_interrupt = us_ticker_disable_interrupt,
00067     .clear_interrupt = us_ticker_clear_interrupt,
00068     .set_interrupt = us_ticker_set_interrupt,
00069     .fire_interrupt = us_ticker_fire_interrupt,
00070     .get_info = us_ticker_get_info,
00071 #if MBED_CONF_TARGET_INIT_US_TICKER_AT_BOOT
00072     .free = block_us_ticker_free,
00073 #else
00074     .free = note_us_ticker_free,
00075 #endif
00076     .runs_in_deep_sleep = false,
00077 };
00078 
00079 static const ticker_data_t us_data = {
00080     .interface = &us_interface,
00081     .queue = &events
00082 };
00083 
00084 const ticker_data_t *get_us_ticker_data(void)
00085 {
00086     return &us_data;
00087 }
00088 
00089 ticker_irq_handler_type set_us_ticker_irq_handler(ticker_irq_handler_type ticker_irq_handler)
00090 {
00091     ticker_irq_handler_type prev_irq_handler = irq_handler;
00092 
00093     irq_handler = ticker_irq_handler;
00094 
00095     return prev_irq_handler;
00096 }
00097 
00098 void us_ticker_irq_handler(void)
00099 {
00100     if (irq_handler) {
00101         irq_handler(&us_data);
00102     }
00103 }
00104 
00105 #else
00106 
00107 const ticker_data_t *get_us_ticker_data(void)
00108 {
00109     return NULL;
00110 }
00111 
00112 #endif  // DEVICE_USTICKER