Shane Lobo
/
nRF52_Pulse_WaitUS
pulse consistency test using wait_us
main.cpp@1:0f0c1a9a174a, 2019-05-10 (annotated)
- Committer:
- punkisnail
- Date:
- Fri May 10 05:51:35 2019 +0000
- Revision:
- 1:0f0c1a9a174a
- Parent:
- 0:18409537564c
commit 1, pulse test using wait_us
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
punkisnail | 0:18409537564c | 1 | /* mbed Microcontroller Library |
punkisnail | 0:18409537564c | 2 | * Copyright (c) 2018 ARM Limited |
punkisnail | 0:18409537564c | 3 | * SPDX-License-Identifier: Apache-2.0 |
punkisnail | 0:18409537564c | 4 | */ |
punkisnail | 0:18409537564c | 5 | |
punkisnail | 0:18409537564c | 6 | #include <mbed.h> |
punkisnail | 0:18409537564c | 7 | |
punkisnail | 1:0f0c1a9a174a | 8 | #define CH_INIT 0 |
punkisnail | 1:0f0c1a9a174a | 9 | #define CH_BOOST_START 1 |
punkisnail | 1:0f0c1a9a174a | 10 | #define CH_BOOSTING 2 |
punkisnail | 1:0f0c1a9a174a | 11 | #define CH_POST_BOOST 3 |
punkisnail | 1:0f0c1a9a174a | 12 | #define CH_WAIT_200MS 4 |
punkisnail | 1:0f0c1a9a174a | 13 | #define CH_WAIT_800MS 5 |
punkisnail | 0:18409537564c | 14 | |
punkisnail | 1:0f0c1a9a174a | 15 | Serial pc(p6, p8); |
punkisnail | 0:18409537564c | 16 | |
punkisnail | 1:0f0c1a9a174a | 17 | DigitalOut led1(LED1); // Used to indicate boost state to user |
punkisnail | 0:18409537564c | 18 | |
punkisnail | 1:0f0c1a9a174a | 19 | DigitalOut ch1(LED4); |
punkisnail | 0:18409537564c | 20 | |
punkisnail | 1:0f0c1a9a174a | 21 | Timeout chStateTimeout; |
punkisnail | 1:0f0c1a9a174a | 22 | |
punkisnail | 1:0f0c1a9a174a | 23 | Timer t; |
punkisnail | 0:18409537564c | 24 | |
punkisnail | 1:0f0c1a9a174a | 25 | volatile bool ch_progress = false; |
punkisnail | 1:0f0c1a9a174a | 26 | volatile bool ch_boost_done = false; |
punkisnail | 0:18409537564c | 27 | |
punkisnail | 1:0f0c1a9a174a | 28 | // ----------------------------------------------------------------------------- |
punkisnail | 1:0f0c1a9a174a | 29 | void timeoutHandler() { |
punkisnail | 1:0f0c1a9a174a | 30 | ch_progress = true; |
punkisnail | 0:18409537564c | 31 | } |
punkisnail | 0:18409537564c | 32 | |
punkisnail | 1:0f0c1a9a174a | 33 | // ----------------------------------------------------------------------------- |
punkisnail | 0:18409537564c | 34 | int main() |
punkisnail | 0:18409537564c | 35 | { |
punkisnail | 1:0f0c1a9a174a | 36 | uint8_t ch_state = CH_INIT; |
punkisnail | 1:0f0c1a9a174a | 37 | unsigned int dummy_count = 0, dummy_count_post; |
punkisnail | 0:18409537564c | 38 | |
punkisnail | 1:0f0c1a9a174a | 39 | ch1 = 0; |
punkisnail | 0:18409537564c | 40 | |
punkisnail | 1:0f0c1a9a174a | 41 | while(1) { |
punkisnail | 1:0f0c1a9a174a | 42 | switch (ch_state) { |
punkisnail | 1:0f0c1a9a174a | 43 | case CH_INIT: |
punkisnail | 1:0f0c1a9a174a | 44 | ch1 = 0; |
punkisnail | 1:0f0c1a9a174a | 45 | ch_progress = false; |
punkisnail | 1:0f0c1a9a174a | 46 | ch_boost_done = false; |
punkisnail | 1:0f0c1a9a174a | 47 | dummy_count = 0; |
punkisnail | 1:0f0c1a9a174a | 48 | dummy_count_post = 0; |
punkisnail | 1:0f0c1a9a174a | 49 | ch_state = CH_BOOST_START; |
punkisnail | 1:0f0c1a9a174a | 50 | break; |
punkisnail | 1:0f0c1a9a174a | 51 | case CH_BOOST_START: |
punkisnail | 1:0f0c1a9a174a | 52 | ch_state = CH_BOOSTING; |
punkisnail | 1:0f0c1a9a174a | 53 | |
punkisnail | 1:0f0c1a9a174a | 54 | led1 = 1; // Give visual indication at start of boost stage |
punkisnail | 1:0f0c1a9a174a | 55 | |
punkisnail | 1:0f0c1a9a174a | 56 | case CH_BOOSTING: |
punkisnail | 1:0f0c1a9a174a | 57 | t.start(); |
punkisnail | 1:0f0c1a9a174a | 58 | //core_util_critical_section_enter(); // critical sections affect the timer t? |
punkisnail | 1:0f0c1a9a174a | 59 | ch1 = 1; // Boost for 100us |
punkisnail | 1:0f0c1a9a174a | 60 | wait_us(100); // This blocks the main thread, so cannot measure during 100us boost stage |
punkisnail | 1:0f0c1a9a174a | 61 | ch1 = 0; |
punkisnail | 1:0f0c1a9a174a | 62 | //core_util_critical_section_enter(); |
punkisnail | 1:0f0c1a9a174a | 63 | t.stop(); |
punkisnail | 1:0f0c1a9a174a | 64 | |
punkisnail | 1:0f0c1a9a174a | 65 | dummy_count++; // measure response during 100us boost stage .. will read as 1 due to blocking wait_us |
punkisnail | 1:0f0c1a9a174a | 66 | |
punkisnail | 1:0f0c1a9a174a | 67 | ch_state = CH_POST_BOOST; |
punkisnail | 1:0f0c1a9a174a | 68 | chStateTimeout.attach_us(&timeoutHandler, 1000); |
punkisnail | 1:0f0c1a9a174a | 69 | case CH_POST_BOOST: |
punkisnail | 1:0f0c1a9a174a | 70 | dummy_count_post++; // measuring response after boost stage for 1ms |
punkisnail | 1:0f0c1a9a174a | 71 | |
punkisnail | 1:0f0c1a9a174a | 72 | if (ch_progress) { |
punkisnail | 1:0f0c1a9a174a | 73 | ch_progress = false; |
punkisnail | 1:0f0c1a9a174a | 74 | ch_state = CH_WAIT_200MS; |
punkisnail | 1:0f0c1a9a174a | 75 | chStateTimeout.attach(&timeoutHandler, 0.2f); |
punkisnail | 1:0f0c1a9a174a | 76 | } |
punkisnail | 1:0f0c1a9a174a | 77 | break; |
punkisnail | 1:0f0c1a9a174a | 78 | case CH_WAIT_200MS: |
punkisnail | 1:0f0c1a9a174a | 79 | if (ch_progress) { |
punkisnail | 1:0f0c1a9a174a | 80 | ch_progress = false; |
punkisnail | 1:0f0c1a9a174a | 81 | led1 = 0; // Turn off boost indication |
punkisnail | 1:0f0c1a9a174a | 82 | ch_state = CH_WAIT_800MS; |
punkisnail | 1:0f0c1a9a174a | 83 | chStateTimeout.attach(&timeoutHandler, 0.8f); |
punkisnail | 1:0f0c1a9a174a | 84 | } |
punkisnail | 1:0f0c1a9a174a | 85 | break; |
punkisnail | 1:0f0c1a9a174a | 86 | case CH_WAIT_800MS: |
punkisnail | 1:0f0c1a9a174a | 87 | if (ch_progress) { |
punkisnail | 1:0f0c1a9a174a | 88 | ch_progress = false; |
punkisnail | 1:0f0c1a9a174a | 89 | ch_state = CH_INIT; |
punkisnail | 1:0f0c1a9a174a | 90 | pc.printf("CH_BOOSTING COUNT: %d; CH_POST_BOOST COUNT: %d; TIME TAKEN: %d\r\n", dummy_count, dummy_count_post, t.read_us()); // print measured response; |
punkisnail | 1:0f0c1a9a174a | 91 | t.reset(); |
punkisnail | 1:0f0c1a9a174a | 92 | } |
punkisnail | 1:0f0c1a9a174a | 93 | break; |
punkisnail | 1:0f0c1a9a174a | 94 | } |
punkisnail | 0:18409537564c | 95 | |
punkisnail | 1:0f0c1a9a174a | 96 | } |
punkisnail | 0:18409537564c | 97 | } |