Shane Lobo / Mbed OS nRF52_Pulse_Timeouts
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2018 ARM Limited
00003  * SPDX-License-Identifier: Apache-2.0
00004  */
00005 
00006 #include <mbed.h>
00007 
00008 #define CH_INIT             0
00009 #define CH_BOOST_START      1
00010 #define CH_BOOSTING         2
00011 #define CH_POST_BOOST       3
00012 #define CH_WAIT_200MS       4
00013 #define CH_WAIT_800MS       5
00014 
00015 Serial pc(p6, p8);
00016 
00017 DigitalOut led1(LED1);  // Used to indicate boost state to user
00018 
00019 DigitalOut ch1(LED4);
00020 
00021 Timeout chStateTimeout;
00022 
00023 Timer t;
00024 
00025 volatile bool ch_progress = false;
00026 volatile bool ch_boost_done = false;
00027 
00028 // -----------------------------------------------------------------------------
00029 void timeoutHandler() {
00030     ch_progress = true;
00031 }
00032 
00033 // -----------------------------------------------------------------------------
00034 void chBoostTimeout() {
00035     ch1 = 0;
00036     t.stop();
00037     ch_boost_done = true;
00038 }
00039 
00040 // -----------------------------------------------------------------------------
00041 int main()
00042 {
00043     uint8_t ch_state = CH_INIT;
00044     unsigned int dummy_count = 0, dummy_count_post;
00045     
00046     ch1 = 0;
00047     
00048     while(1) {
00049         switch (ch_state) {
00050             case CH_INIT:
00051                 ch1 = 0;
00052                 ch_progress = false;
00053                 ch_boost_done = false;
00054                 dummy_count = 0;
00055                 dummy_count_post = 0;
00056                 ch_state = CH_BOOST_START;
00057                 break;
00058             case CH_BOOST_START:          
00059                 ch_state = CH_BOOSTING;
00060                 led1 = 1;   // Give visual indication at start of boost stage
00061                 t.start();
00062                 ch1 = 1;    // Boost for 100us
00063                 chStateTimeout.attach_us(&chBoostTimeout, 100);
00064             case CH_BOOSTING:
00065                 dummy_count++;  // measure response during 100us boost stage
00066                 
00067                 if (ch_boost_done) {
00068                     ch_state = CH_POST_BOOST;
00069                     chStateTimeout.attach_us(&timeoutHandler, 1000);
00070                 }
00071                 else break;
00072             case CH_POST_BOOST:
00073                 dummy_count_post++;  // measuring response after boost stage for 1ms
00074                 
00075                 if (ch_progress) {
00076                     ch_progress = false;
00077                     ch_state = CH_WAIT_200MS;
00078                     chStateTimeout.attach(&timeoutHandler, 0.2f);
00079                 }
00080                 break;
00081             case CH_WAIT_200MS:
00082                 if (ch_progress) {
00083                     ch_progress = false;
00084                     led1 = 0;   // Turn off boost indication
00085                     ch_state = CH_WAIT_800MS;
00086                     chStateTimeout.attach(&timeoutHandler, 0.8f);
00087                 }
00088                 break;
00089             case CH_WAIT_800MS:
00090                 if (ch_progress) {
00091                     ch_progress = false;
00092                     ch_state = CH_INIT;
00093                     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;
00094                     t.reset();
00095                 }
00096                 break;            
00097         }
00098         
00099     } 
00100 }