Estan Lujan / Mbed 2 deprecated baud_test

Dependencies:   mbed-rtos mbed

Fork of FRDM_serial_pc_board_pass_through by felipe manrique

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003  
00004 Serial pc(USBTX, USBRX);
00005 //TX RX pins, see https://mbed.org/handbook/mbed-FRDM-KL25Z
00006 Serial uart(p9, p10);
00007 //to test is you can use a hard bridge (piece of cable) between PTC4 and PTC3 open a terminal and write something :) 
00008 //you can integrate this to improve some interesting aplicattions like this:
00009 //http://www.instructables.com/id/Temperature-sensor--weatherstation/http://www.instructables.com/id/Temperature-sensor--weatherstation/ 
00010 DigitalOut pc_activity(LED1);
00011 DigitalOut uart_activity(LED2);
00012 DigitalIn receive(p10);
00013 
00014 Timer t;
00015 int state = 0;
00016 int period = 0;
00017 int baud_rate = 0;
00018  
00019 void baud_thread(void const *args) {
00020     while (true) {
00021         uart.putc(0x55);        
00022         Thread::wait(1000);
00023         if (period)
00024             pc.printf("baud rate %d ", baud_rate);
00025     }
00026 }
00027 
00028 void get_rate(int *baud_rate) {    
00029     if (*baud_rate > 90000 && *baud_rate < (int)(125200))
00030         *baud_rate = 115200;
00031     else if (*baud_rate > (int)0.9*9600 && *baud_rate < (int)1.1*9600)
00032         *baud_rate = 9600;
00033     else
00034         *baud_rate = 0;
00035 }
00036             
00037 
00038 int main() {
00039 
00040     int stop = 0;
00041     int counter = 0;
00042     uart.baud(115200);
00043     Thread thread(baud_thread);
00044     
00045     pc.printf("started \n");
00046     while(1) {
00047         if (state == 0)
00048             if(receive) {
00049                 state = 1;
00050             }
00051             
00052         if (state == 1)
00053             if(!receive) {
00054                 t.start();
00055                 state = 2;
00056             }
00057             
00058         if (state == 2)
00059             if(receive) {
00060                 t.stop();
00061                 period = t.read_us();
00062                 baud_rate = (int)(1.0/((float)t.read_us()/1000000.0));
00063                 get_rate(&baud_rate);
00064                 uart.putc(0xAA);
00065                 t.reset();
00066                 state = 0;
00067             }
00068         
00069         if(pc.readable()) {
00070             uart.putc(pc.getc());
00071             pc_activity = !pc_activity;
00072         }
00073         
00074         if(uart.readable()) {
00075             pc.putc(uart.getc());
00076             uart_activity = !uart_activity;
00077         }
00078     }
00079 }