Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
mbed-os/targets/TARGET_NUVOTON/nu_timer.h@0:fbdae7e6d805, 2018-05-14 (annotated)
- Committer:
- borlanic
- Date:
- Mon May 14 11:29:06 2018 +0000
- Revision:
- 0:fbdae7e6d805
BBR
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| borlanic | 0:fbdae7e6d805 | 1 | /* mbed Microcontroller Library |
| borlanic | 0:fbdae7e6d805 | 2 | * Copyright (c) 2015-2016 Nuvoton |
| borlanic | 0:fbdae7e6d805 | 3 | * |
| borlanic | 0:fbdae7e6d805 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| borlanic | 0:fbdae7e6d805 | 5 | * you may not use this file except in compliance with the License. |
| borlanic | 0:fbdae7e6d805 | 6 | * You may obtain a copy of the License at |
| borlanic | 0:fbdae7e6d805 | 7 | * |
| borlanic | 0:fbdae7e6d805 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| borlanic | 0:fbdae7e6d805 | 9 | * |
| borlanic | 0:fbdae7e6d805 | 10 | * Unless required by applicable law or agreed to in writing, software |
| borlanic | 0:fbdae7e6d805 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| borlanic | 0:fbdae7e6d805 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| borlanic | 0:fbdae7e6d805 | 13 | * See the License for the specific language governing permissions and |
| borlanic | 0:fbdae7e6d805 | 14 | * limitations under the License. |
| borlanic | 0:fbdae7e6d805 | 15 | */ |
| borlanic | 0:fbdae7e6d805 | 16 | |
| borlanic | 0:fbdae7e6d805 | 17 | #ifndef NU_TIMER_H |
| borlanic | 0:fbdae7e6d805 | 18 | #define NU_TIMER_H |
| borlanic | 0:fbdae7e6d805 | 19 | |
| borlanic | 0:fbdae7e6d805 | 20 | #include <stdint.h> |
| borlanic | 0:fbdae7e6d805 | 21 | #include <stdbool.h> |
| borlanic | 0:fbdae7e6d805 | 22 | #include "cmsis.h" |
| borlanic | 0:fbdae7e6d805 | 23 | #include "mbed_power_mgmt.h" |
| borlanic | 0:fbdae7e6d805 | 24 | #include "mbed_critical.h" |
| borlanic | 0:fbdae7e6d805 | 25 | #include "ticker_api.h" |
| borlanic | 0:fbdae7e6d805 | 26 | #include "us_ticker_api.h" |
| borlanic | 0:fbdae7e6d805 | 27 | |
| borlanic | 0:fbdae7e6d805 | 28 | #ifdef __cplusplus |
| borlanic | 0:fbdae7e6d805 | 29 | extern "C" { |
| borlanic | 0:fbdae7e6d805 | 30 | #endif |
| borlanic | 0:fbdae7e6d805 | 31 | |
| borlanic | 0:fbdae7e6d805 | 32 | /* A simple count-down timer used for Nuvoton ported drivers |
| borlanic | 0:fbdae7e6d805 | 33 | * |
| borlanic | 0:fbdae7e6d805 | 34 | * NOTE: nu_countdown_init must be paired with nu_countdown_free. |
| borlanic | 0:fbdae7e6d805 | 35 | * |
| borlanic | 0:fbdae7e6d805 | 36 | * Example: |
| borlanic | 0:fbdae7e6d805 | 37 | * nu_countdown_ctx_s ctx; |
| borlanic | 0:fbdae7e6d805 | 38 | * |
| borlanic | 0:fbdae7e6d805 | 39 | * // Set up 2 ms timeout |
| borlanic | 0:fbdae7e6d805 | 40 | * nu_countdown_init(&ctx, 2000); |
| borlanic | 0:fbdae7e6d805 | 41 | * |
| borlanic | 0:fbdae7e6d805 | 42 | * // Timed-wait for a task |
| borlanic | 0:fbdae7e6d805 | 43 | * while (true) { |
| borlanic | 0:fbdae7e6d805 | 44 | * // Poll the task |
| borlanic | 0:fbdae7e6d805 | 45 | * |
| borlanic | 0:fbdae7e6d805 | 46 | * if (nu_countdown_expired(&ctx)) { |
| borlanic | 0:fbdae7e6d805 | 47 | * // Timeout |
| borlanic | 0:fbdae7e6d805 | 48 | * } |
| borlanic | 0:fbdae7e6d805 | 49 | * } |
| borlanic | 0:fbdae7e6d805 | 50 | * |
| borlanic | 0:fbdae7e6d805 | 51 | * // Must pair nu_countdown_init with nu_countdown_free in the end |
| borlanic | 0:fbdae7e6d805 | 52 | * nu_countdown_free(&ctx); |
| borlanic | 0:fbdae7e6d805 | 53 | */ |
| borlanic | 0:fbdae7e6d805 | 54 | |
| borlanic | 0:fbdae7e6d805 | 55 | struct nu_countdown_ctx_s { |
| borlanic | 0:fbdae7e6d805 | 56 | const ticker_data_t * _ticker_data; // Hold ticker_data_t |
| borlanic | 0:fbdae7e6d805 | 57 | us_timestamp_t _interval_end_us; // End of interval in us |
| borlanic | 0:fbdae7e6d805 | 58 | bool _expired; // Expired or not |
| borlanic | 0:fbdae7e6d805 | 59 | }; |
| borlanic | 0:fbdae7e6d805 | 60 | |
| borlanic | 0:fbdae7e6d805 | 61 | __STATIC_INLINE void nu_countdown_init(struct nu_countdown_ctx_s *ctx, us_timestamp_t interval_us) |
| borlanic | 0:fbdae7e6d805 | 62 | { |
| borlanic | 0:fbdae7e6d805 | 63 | core_util_critical_section_enter(); |
| borlanic | 0:fbdae7e6d805 | 64 | sleep_manager_lock_deep_sleep(); |
| borlanic | 0:fbdae7e6d805 | 65 | ctx->_ticker_data = get_us_ticker_data(); |
| borlanic | 0:fbdae7e6d805 | 66 | ctx->_interval_end_us = ticker_read_us(ctx->_ticker_data) + interval_us; |
| borlanic | 0:fbdae7e6d805 | 67 | ctx->_expired = false; |
| borlanic | 0:fbdae7e6d805 | 68 | core_util_critical_section_exit(); |
| borlanic | 0:fbdae7e6d805 | 69 | } |
| borlanic | 0:fbdae7e6d805 | 70 | |
| borlanic | 0:fbdae7e6d805 | 71 | __STATIC_INLINE bool nu_countdown_expired(struct nu_countdown_ctx_s *ctx) |
| borlanic | 0:fbdae7e6d805 | 72 | { |
| borlanic | 0:fbdae7e6d805 | 73 | core_util_critical_section_enter(); |
| borlanic | 0:fbdae7e6d805 | 74 | if (! ctx->_expired) { |
| borlanic | 0:fbdae7e6d805 | 75 | ctx->_expired = ticker_read_us(ctx->_ticker_data) >= ctx->_interval_end_us; |
| borlanic | 0:fbdae7e6d805 | 76 | } |
| borlanic | 0:fbdae7e6d805 | 77 | core_util_critical_section_exit(); |
| borlanic | 0:fbdae7e6d805 | 78 | |
| borlanic | 0:fbdae7e6d805 | 79 | return ctx->_expired; |
| borlanic | 0:fbdae7e6d805 | 80 | } |
| borlanic | 0:fbdae7e6d805 | 81 | |
| borlanic | 0:fbdae7e6d805 | 82 | __STATIC_INLINE void nu_countdown_free(struct nu_countdown_ctx_s *ctx) |
| borlanic | 0:fbdae7e6d805 | 83 | { |
| borlanic | 0:fbdae7e6d805 | 84 | core_util_critical_section_enter(); |
| borlanic | 0:fbdae7e6d805 | 85 | sleep_manager_unlock_deep_sleep(); |
| borlanic | 0:fbdae7e6d805 | 86 | core_util_critical_section_exit(); |
| borlanic | 0:fbdae7e6d805 | 87 | } |
| borlanic | 0:fbdae7e6d805 | 88 | |
| borlanic | 0:fbdae7e6d805 | 89 | #ifdef __cplusplus |
| borlanic | 0:fbdae7e6d805 | 90 | } |
| borlanic | 0:fbdae7e6d805 | 91 | #endif |
| borlanic | 0:fbdae7e6d805 | 92 | |
| borlanic | 0:fbdae7e6d805 | 93 | #endif |