Shane Lobo
/
nRF52_Pulse_WaitUS
pulse consistency test using wait_us
main.cpp
- Committer:
- punkisnail
- Date:
- 2019-05-10
- Revision:
- 1:0f0c1a9a174a
- Parent:
- 0:18409537564c
File content as of revision 1:0f0c1a9a174a:
/* mbed Microcontroller Library * Copyright (c) 2018 ARM Limited * SPDX-License-Identifier: Apache-2.0 */ #include <mbed.h> #define CH_INIT 0 #define CH_BOOST_START 1 #define CH_BOOSTING 2 #define CH_POST_BOOST 3 #define CH_WAIT_200MS 4 #define CH_WAIT_800MS 5 Serial pc(p6, p8); DigitalOut led1(LED1); // Used to indicate boost state to user DigitalOut ch1(LED4); Timeout chStateTimeout; Timer t; volatile bool ch_progress = false; volatile bool ch_boost_done = false; // ----------------------------------------------------------------------------- void timeoutHandler() { ch_progress = true; } // ----------------------------------------------------------------------------- int main() { uint8_t ch_state = CH_INIT; unsigned int dummy_count = 0, dummy_count_post; ch1 = 0; while(1) { switch (ch_state) { case CH_INIT: ch1 = 0; ch_progress = false; ch_boost_done = false; dummy_count = 0; dummy_count_post = 0; ch_state = CH_BOOST_START; break; case CH_BOOST_START: ch_state = CH_BOOSTING; led1 = 1; // Give visual indication at start of boost stage case CH_BOOSTING: t.start(); //core_util_critical_section_enter(); // critical sections affect the timer t? ch1 = 1; // Boost for 100us wait_us(100); // This blocks the main thread, so cannot measure during 100us boost stage ch1 = 0; //core_util_critical_section_enter(); t.stop(); dummy_count++; // measure response during 100us boost stage .. will read as 1 due to blocking wait_us ch_state = CH_POST_BOOST; chStateTimeout.attach_us(&timeoutHandler, 1000); case CH_POST_BOOST: dummy_count_post++; // measuring response after boost stage for 1ms if (ch_progress) { ch_progress = false; ch_state = CH_WAIT_200MS; chStateTimeout.attach(&timeoutHandler, 0.2f); } break; case CH_WAIT_200MS: if (ch_progress) { ch_progress = false; led1 = 0; // Turn off boost indication ch_state = CH_WAIT_800MS; chStateTimeout.attach(&timeoutHandler, 0.8f); } break; case CH_WAIT_800MS: if (ch_progress) { ch_progress = false; ch_state = CH_INIT; 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; t.reset(); } break; } } }