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  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 /**
19  * @addtogroup hal_sleep
20  * @{
21  * @defgroup hal_sleep_test_util Tests
22  * Tests of the sleep HAL.
23  * @{
24  */
25 
26 #ifndef MBED_SLEEP_TEST_UTILS_H
27 #define MBED_SLEEP_TEST_UTILS_H
28 
29 #include "hal/ticker_api.h"
30 #include "hal/us_ticker_api.h"
31 #include "hal/lp_ticker_api.h"
32 
33 /* To prevent a loss of Greentea data, the serial buffers have to be flushed
34  * before the UART peripheral shutdown. The UART shutdown happens when the
35  * device is entering the deepsleep mode or performing a reset.
36  *
37  * With the current API, it is not possible to check if the hardware buffers
38  * are empty. However, it is possible to determine the time required for the
39  * buffers to flush.
40  *
41  * Assuming the biggest Tx FIFO of 128 bytes (as for CY8CPROTO_062_4343W)
42  * and a default UART config (9600, 8N1), flushing the Tx FIFO wold take:
43  * (1 start_bit + 8 data_bits + 1 stop_bit) * 128 * 1000 / 9600 = 133.3 ms.
44  * To be on the safe side, set the wait time to 150 ms.
45  */
46 #define SERIAL_FLUSH_TIME_MS 150
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:144
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.