Mistake on this page?
Report an issue in GitHub or email us
sleep_test_utils.h
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
19  * @{
20  * @defgroup hal_sleep_test_util Tests
21  * Tests of the sleep HAL.
22  * @{
23  */
24 
25 #ifndef MBED_SLEEP_TEST_UTILS_H
26 #define MBED_SLEEP_TEST_UTILS_H
27 
28 #include "hal/ticker_api.h"
29 #include "hal/us_ticker_api.h"
30 #include "hal/lp_ticker_api.h"
31 
32 /* Flush serial buffer before deep sleep
33  *
34  * Since deepsleep() may shut down the UART peripheral, we wait for some time
35  * to allow for hardware serial buffers to completely flush.
36  *
37  * Take NUMAKER_PFM_NUC472 as an example:
38  * Its UART peripheral has 16-byte Tx FIFO. With baud rate set to 9600, flush
39  * Tx FIFO would take: 16 * 8 * 1000 / 9600 = 13.3 (ms). So set wait time to
40  * 20ms here for safe.
41  *
42  * This should be replaced with a better function that checks if the
43  * hardware buffers are empty. However, such an API does not exist now,
44  * so we'll use the busy_wait_ms() function for now.
45  */
46 #define SERIAL_FLUSH_TIME_MS 20
47 
48 #define US_PER_S 1000000
49 
50 unsigned int ticks_to_us(unsigned int ticks, unsigned int freq)
51 {
52  return (unsigned int)((unsigned long long) ticks * US_PER_S / freq);
53 }
54 
55 unsigned int us_to_ticks(unsigned int us, unsigned int freq)
56 {
57  return (unsigned int)((unsigned long long) us * freq / US_PER_S);
58 }
59 
60 unsigned int overflow_protect(unsigned int timestamp, unsigned int ticker_width)
61 {
62  unsigned int counter_mask = ((1 << ticker_width) - 1);
63 
64  return (timestamp & counter_mask);
65 }
66 
67 bool compare_timestamps(unsigned int delta_ticks, unsigned int ticker_width, unsigned int expected, unsigned int actual)
68 {
69  const unsigned int counter_mask = ((1 << ticker_width) - 1);
70 
71  const unsigned int lower_bound = ((expected - delta_ticks) & counter_mask);
72  const unsigned int upper_bound = ((expected + delta_ticks) & counter_mask);
73 
74  if (lower_bound < upper_bound) {
75  if (actual >= lower_bound && actual <= upper_bound) {
76  return true;
77  } else {
78  return false;
79  }
80  } else {
81  if ((actual >= lower_bound && actual <= counter_mask) || (actual >= 0 && actual <= upper_bound)) {
82  return true;
83  } else {
84  return false;
85  }
86  }
87 }
88 
89 void busy_wait_ms(int ms)
90 {
92  uint32_t mask = (1 << info->bits) - 1;
93  int delay = (int)((uint64_t) ms * info->frequency / 1000);
94 
95  uint32_t prev = us_ticker_read();
96  while (delay > 0) {
97  uint32_t next = us_ticker_read();
98  delay -= (next - prev) & mask;
99  prev = next;
100  }
101 }
102 
103 void us_ticker_isr(const ticker_data_t *const ticker_data)
104 {
106 }
107 
108 #if DEVICE_LPTICKER
109 void lp_ticker_isr(const ticker_data_t *const ticker_data)
110 {
112 }
113 #endif
114 
115 #endif
116 
117 /** @}*/
118 /** @}*/
Information about the ticker implementation.
Definition: ticker_api.h:53
The key size.
Ticker&#39;s data structure.
Definition: ticker_api.h:93
uint32_t frequency
Frequency in Hz this ticker runs at.
Definition: ticker_api.h:54
uint32_t() us_ticker_read(void)
Read the current counter.
uint32_t bits
Number of bits this ticker supports.
Definition: ticker_api.h:55
const ticker_info_t * us_ticker_get_info(void)
Get frequency and counter bits of this ticker.
void lp_ticker_clear_interrupt(void)
Clear the low power ticker interrupt.
void us_ticker_clear_interrupt(void)
Clear us ticker interrupt.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.