|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2017 ARM Limited |
| 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 | +/** |
| 18 | + * @addtogroup hal_sleep_test_utils |
| 19 | + * @{ |
| 20 | + */ |
| 21 | + |
| 22 | +#ifndef MBED_SLEEP_TEST_UTILS_H |
| 23 | +#define MBED_SLEEP_TEST_UTILS_H |
| 24 | + |
| 25 | +#include "hal/ticker_api.h" |
| 26 | +#include "hal/us_ticker_api.h" |
| 27 | +#include "hal/lp_ticker_api.h" |
| 28 | + |
| 29 | +/* Flush serial buffer before deep sleep |
| 30 | + * |
| 31 | + * Since deepsleep() may shut down the UART peripheral, we wait for some time |
| 32 | + * to allow for hardware serial buffers to completely flush. |
| 33 | + * |
| 34 | + * Take NUMAKER_PFM_NUC472 as an example: |
| 35 | + * Its UART peripheral has 16-byte Tx FIFO. With baud rate set to 9600, flush |
| 36 | + * Tx FIFO would take: 16 * 8 * 1000 / 9600 = 13.3 (ms). So set wait time to |
| 37 | + * 20ms here for safe. |
| 38 | + * |
| 39 | + * This should be replaced with a better function that checks if the |
| 40 | + * hardware buffers are empty. However, such an API does not exist now, |
| 41 | + * so we'll use the busy_wait_ms() function for now. |
| 42 | + */ |
| 43 | +#define SERIAL_FLUSH_TIME_MS 20 |
| 44 | + |
| 45 | +#define US_PER_S 1000000 |
| 46 | + |
| 47 | +unsigned int ticks_to_us(unsigned int ticks, unsigned int freq) |
| 48 | +{ |
| 49 | + return (unsigned int) ((unsigned long long) ticks * US_PER_S / freq); |
| 50 | +} |
| 51 | + |
| 52 | +unsigned int us_to_ticks(unsigned int us, unsigned int freq) |
| 53 | +{ |
| 54 | + return (unsigned int) ((unsigned long long) us * freq / US_PER_S); |
| 55 | +} |
| 56 | + |
| 57 | +unsigned int overflow_protect(unsigned int timestamp, unsigned int ticker_width) |
| 58 | +{ |
| 59 | + unsigned int counter_mask = ((1 << ticker_width) - 1); |
| 60 | + |
| 61 | + return (timestamp & counter_mask); |
| 62 | +} |
| 63 | + |
| 64 | +bool compare_timestamps(unsigned int delta_ticks, unsigned int ticker_width, unsigned int expected, unsigned int actual) |
| 65 | +{ |
| 66 | + const unsigned int counter_mask = ((1 << ticker_width) - 1); |
| 67 | + |
| 68 | + const unsigned int lower_bound = ((expected - delta_ticks) & counter_mask); |
| 69 | + const unsigned int upper_bound = ((expected + delta_ticks) & counter_mask); |
| 70 | + |
| 71 | + if (lower_bound < upper_bound) { |
| 72 | + if (actual >= lower_bound && actual <= upper_bound) { |
| 73 | + return true; |
| 74 | + } else { |
| 75 | + return false; |
| 76 | + } |
| 77 | + } else { |
| 78 | + if ((actual >= lower_bound && actual <= counter_mask) || (actual >= 0 && actual <= upper_bound)) { |
| 79 | + return true; |
| 80 | + } else { |
| 81 | + return false; |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +void busy_wait_ms(int ms) |
| 87 | +{ |
| 88 | + const ticker_info_t *info = us_ticker_get_info(); |
| 89 | + uint32_t mask = (1 << info->bits) - 1; |
| 90 | + int delay = (int) ((uint64_t) ms * info->frequency / 1000); |
| 91 | + |
| 92 | + uint32_t prev = us_ticker_read(); |
| 93 | + while (delay > 0) { |
| 94 | + uint32_t next = us_ticker_read(); |
| 95 | + delay -= (next - prev) & mask; |
| 96 | + prev = next; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +void us_ticker_isr(const ticker_data_t * const ticker_data) |
| 101 | +{ |
| 102 | + us_ticker_clear_interrupt(); |
| 103 | +} |
| 104 | + |
| 105 | +#ifdef DEVICE_LPTICKER |
| 106 | +void lp_ticker_isr(const ticker_data_t * const ticker_data) |
| 107 | +{ |
| 108 | + lp_ticker_clear_interrupt(); |
| 109 | +} |
| 110 | +#endif |
| 111 | + |
| 112 | +#endif |
| 113 | + |
| 114 | +/** @}*/ |
0 commit comments