mbed library sources. Supersedes mbed-src.

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

Committer:
AnnaBridge
Date:
Fri Feb 16 16:09:33 2018 +0000
Revision:
181:57724642e740
Parent:
180:96ed750bd169
Child:
184:08ed48f1de7f
mbed-dev library. Release version 159.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Anna Bridge 180:96ed750bd169 1 /* mbed Microcontroller Library
Anna Bridge 180:96ed750bd169 2 * Copyright (c) 2015-2016 Nuvoton
Anna Bridge 180:96ed750bd169 3 *
Anna Bridge 180:96ed750bd169 4 * Licensed under the Apache License, Version 2.0 (the "License");
Anna Bridge 180:96ed750bd169 5 * you may not use this file except in compliance with the License.
Anna Bridge 180:96ed750bd169 6 * You may obtain a copy of the License at
Anna Bridge 180:96ed750bd169 7 *
Anna Bridge 180:96ed750bd169 8 * http://www.apache.org/licenses/LICENSE-2.0
Anna Bridge 180:96ed750bd169 9 *
Anna Bridge 180:96ed750bd169 10 * Unless required by applicable law or agreed to in writing, software
Anna Bridge 180:96ed750bd169 11 * distributed under the License is distributed on an "AS IS" BASIS,
Anna Bridge 180:96ed750bd169 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Anna Bridge 180:96ed750bd169 13 * See the License for the specific language governing permissions and
Anna Bridge 180:96ed750bd169 14 * limitations under the License.
Anna Bridge 180:96ed750bd169 15 */
Anna Bridge 180:96ed750bd169 16
Anna Bridge 180:96ed750bd169 17 #ifndef NU_TIMER_H
Anna Bridge 180:96ed750bd169 18 #define NU_TIMER_H
Anna Bridge 180:96ed750bd169 19
Anna Bridge 180:96ed750bd169 20 #include <stdint.h>
Anna Bridge 180:96ed750bd169 21 #include <stdbool.h>
Anna Bridge 180:96ed750bd169 22 #include "cmsis.h"
Anna Bridge 180:96ed750bd169 23 #include "mbed_sleep.h"
Anna Bridge 180:96ed750bd169 24 #include "mbed_critical.h"
Anna Bridge 180:96ed750bd169 25 #include "ticker_api.h"
Anna Bridge 180:96ed750bd169 26 #include "us_ticker_api.h"
Anna Bridge 180:96ed750bd169 27
Anna Bridge 180:96ed750bd169 28 #ifdef __cplusplus
Anna Bridge 180:96ed750bd169 29 extern "C" {
Anna Bridge 180:96ed750bd169 30 #endif
Anna Bridge 180:96ed750bd169 31
Anna Bridge 180:96ed750bd169 32 /* A simple count-down timer used for Nuvoton ported drivers
Anna Bridge 180:96ed750bd169 33 *
Anna Bridge 180:96ed750bd169 34 * NOTE: nu_countdown_init must be paired with nu_countdown_free.
Anna Bridge 180:96ed750bd169 35 *
Anna Bridge 180:96ed750bd169 36 * Example:
Anna Bridge 180:96ed750bd169 37 * nu_countdown_ctx_s ctx;
Anna Bridge 180:96ed750bd169 38 *
Anna Bridge 180:96ed750bd169 39 * // Set up 2 ms timeout
Anna Bridge 180:96ed750bd169 40 * nu_countdown_init(&ctx, 2000);
Anna Bridge 180:96ed750bd169 41 *
Anna Bridge 180:96ed750bd169 42 * // Timed-wait for a task
Anna Bridge 180:96ed750bd169 43 * while (true) {
Anna Bridge 180:96ed750bd169 44 * // Poll the task
Anna Bridge 180:96ed750bd169 45 *
Anna Bridge 180:96ed750bd169 46 * if (nu_countdown_expired(&ctx)) {
Anna Bridge 180:96ed750bd169 47 * // Timeout
Anna Bridge 180:96ed750bd169 48 * }
Anna Bridge 180:96ed750bd169 49 * }
Anna Bridge 180:96ed750bd169 50 *
Anna Bridge 180:96ed750bd169 51 * // Must pair nu_countdown_init with nu_countdown_free in the end
Anna Bridge 180:96ed750bd169 52 * nu_countdown_free(&ctx);
Anna Bridge 180:96ed750bd169 53 */
Anna Bridge 180:96ed750bd169 54
Anna Bridge 180:96ed750bd169 55 struct nu_countdown_ctx_s {
Anna Bridge 180:96ed750bd169 56 const ticker_data_t * _ticker_data; // Hold ticker_data_t
Anna Bridge 180:96ed750bd169 57 us_timestamp_t _interval_end_us; // End of interval in us
Anna Bridge 180:96ed750bd169 58 bool _expired; // Expired or not
Anna Bridge 180:96ed750bd169 59 };
Anna Bridge 180:96ed750bd169 60
Anna Bridge 180:96ed750bd169 61 __STATIC_INLINE void nu_countdown_init(struct nu_countdown_ctx_s *ctx, us_timestamp_t interval_us)
Anna Bridge 180:96ed750bd169 62 {
Anna Bridge 180:96ed750bd169 63 core_util_critical_section_enter();
Anna Bridge 180:96ed750bd169 64 sleep_manager_lock_deep_sleep();
Anna Bridge 180:96ed750bd169 65 ctx->_ticker_data = get_us_ticker_data();
Anna Bridge 180:96ed750bd169 66 ctx->_interval_end_us = ticker_read_us(ctx->_ticker_data) + interval_us;
Anna Bridge 180:96ed750bd169 67 ctx->_expired = false;
Anna Bridge 180:96ed750bd169 68 core_util_critical_section_exit();
Anna Bridge 180:96ed750bd169 69 }
Anna Bridge 180:96ed750bd169 70
Anna Bridge 180:96ed750bd169 71 __STATIC_INLINE bool nu_countdown_expired(struct nu_countdown_ctx_s *ctx)
Anna Bridge 180:96ed750bd169 72 {
Anna Bridge 180:96ed750bd169 73 core_util_critical_section_enter();
Anna Bridge 180:96ed750bd169 74 if (! ctx->_expired) {
Anna Bridge 180:96ed750bd169 75 ctx->_expired = ticker_read_us(ctx->_ticker_data) >= ctx->_interval_end_us;
Anna Bridge 180:96ed750bd169 76 }
Anna Bridge 180:96ed750bd169 77 core_util_critical_section_exit();
Anna Bridge 180:96ed750bd169 78
Anna Bridge 180:96ed750bd169 79 return ctx->_expired;
Anna Bridge 180:96ed750bd169 80 }
Anna Bridge 180:96ed750bd169 81
Anna Bridge 180:96ed750bd169 82 __STATIC_INLINE void nu_countdown_free(struct nu_countdown_ctx_s *ctx)
Anna Bridge 180:96ed750bd169 83 {
Anna Bridge 180:96ed750bd169 84 core_util_critical_section_enter();
Anna Bridge 180:96ed750bd169 85 sleep_manager_unlock_deep_sleep();
Anna Bridge 180:96ed750bd169 86 core_util_critical_section_exit();
Anna Bridge 180:96ed750bd169 87 }
Anna Bridge 180:96ed750bd169 88
Anna Bridge 180:96ed750bd169 89 #ifdef __cplusplus
Anna Bridge 180:96ed750bd169 90 }
Anna Bridge 180:96ed750bd169 91 #endif
Anna Bridge 180:96ed750bd169 92
Anna Bridge 180:96ed750bd169 93 #endif