|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2015-2016 Nuvoton |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "nu_timer.h" |
| 18 | +#include "mbed_power_mgmt.h" |
| 19 | +#include "mbed_critical.h" |
| 20 | + |
| 21 | +void nu_countdown_init(struct nu_countdown_ctx_s *ctx, us_timestamp_t interval_us) |
| 22 | +{ |
| 23 | + core_util_critical_section_enter(); |
| 24 | + sleep_manager_lock_deep_sleep(); |
| 25 | + ctx->_ticker_data = get_us_ticker_data(); |
| 26 | + ctx->_interval_end_us = ticker_read_us(ctx->_ticker_data) + interval_us; |
| 27 | + ctx->_expired = false; |
| 28 | + core_util_critical_section_exit(); |
| 29 | +} |
| 30 | + |
| 31 | +bool nu_countdown_expired(struct nu_countdown_ctx_s *ctx) |
| 32 | +{ |
| 33 | + core_util_critical_section_enter(); |
| 34 | + if (! ctx->_expired) { |
| 35 | + ctx->_expired = ticker_read_us(ctx->_ticker_data) >= ctx->_interval_end_us; |
| 36 | + } |
| 37 | + core_util_critical_section_exit(); |
| 38 | + |
| 39 | + return ctx->_expired; |
| 40 | +} |
| 41 | + |
| 42 | +void nu_countdown_free(struct nu_countdown_ctx_s *ctx) |
| 43 | +{ |
| 44 | + core_util_critical_section_enter(); |
| 45 | + sleep_manager_unlock_deep_sleep(); |
| 46 | + core_util_critical_section_exit(); |
| 47 | +} |
0 commit comments