Example of communication between a computer and a Nucleo board - RS232/USB - a terminal or an interface is required on the computer
main.cpp
00001 #include "mbed.h" 00002 00003 #define ETAT_STOP 0 00004 #define ETAT_WAIT_PARAM 1 00005 #define ETAT_WAIT_PARAM_N 11 00006 #define ETAT_WAIT_PARAM_F 12 00007 #define ETAT_WAIT_PARAM_S 13 00008 #define ETAT_WAIT_GO 2 00009 #define ETAT_ACQUIRING 3 00010 #define ETAT_SENDING_DATA 4 00011 00012 // inputs and outputs configuration 00013 DigitalOut debug_led(LED1); 00014 Serial rs232(USBTX, USBRX); 00015 AnalogIn can_input(A0); 00016 InterruptIn sync_input(D10); 00017 00018 Ticker tictoc; 00019 Ticker blinky; 00020 00021 // System functions 00022 void ISR_sync(void); 00023 void convert_signal(void); 00024 void ISR_get_data(void); 00025 void ISR_send_data(void); 00026 void TIK_blink_led(void); 00027 00028 // System variables 00029 int i, pdix, temp; 00030 char etat = 0; 00031 char data_received, i_received; 00032 char tab_received[10] = {0}; 00033 00034 // Acquisition parameters 00035 int number_points; 00036 int frequency; 00037 char sync = 0; 00038 00039 // Acquisition data 00040 int i_data; 00041 int tab_data[10000]; 00042 00043 // Main function 00044 int main() { 00045 number_points = 0; 00046 frequency = 0; 00047 i_received = 0; 00048 etat = ETAT_STOP; 00049 data_received = '0'; 00050 debug_led = 0; 00051 i_data = 0; 00052 rs232.baud(115200); 00053 rs232.attach(&ISR_get_data); 00054 sync_input.rise(&ISR_sync); 00055 while(1) { 00056 switch(etat){ 00057 00058 case ETAT_SENDING_DATA : 00059 rs232.putc('d'); 00060 blinky.detach(); 00061 tictoc.detach(); 00062 debug_led = 1; 00063 for(i = 0; i < number_points; i++){ 00064 rs232.printf("[%d;%d]", i, tab_data[i]); 00065 } 00066 etat = ETAT_STOP; 00067 debug_led = 0; 00068 break; 00069 default : 00070 temp = 0; 00071 } 00072 } 00073 } 00074 00075 void ISR_sync(void){ 00076 if((etat == ETAT_ACQUIRING) && (sync == 1)){ 00077 float period = 1.0/frequency; 00078 tictoc.attach(&convert_signal,period); 00079 } 00080 } 00081 00082 void convert_signal(void){ 00083 tab_data[i_data] = can_input.read_u16() >> 4; 00084 i_data++; 00085 if(i_data == number_points){ 00086 etat = ETAT_SENDING_DATA; 00087 tictoc.detach(); 00088 } 00089 } 00090 00091 void ISR_send_data(){ 00092 i++; 00093 rs232.printf("i=%d:\r\n", i); 00094 } 00095 00096 void ISR_get_data(){ 00097 data_received = rs232.getc(); 00098 if((data_received == 'U') && (etat != ETAT_STOP) && (etat != ETAT_ACQUIRING) && (etat != ETAT_SENDING_DATA)){ // UPDATE parameters 00099 rs232.putc('u'); 00100 etat = ETAT_WAIT_PARAM; 00101 blinky.attach(&TIK_blink_led, 0.5); 00102 } 00103 00104 switch(etat){ 00105 case ETAT_STOP : 00106 if(data_received == 'a'){ 00107 etat = ETAT_WAIT_PARAM; 00108 rs232.putc('o'); 00109 blinky.attach(&TIK_blink_led, 0.5); 00110 } 00111 break; 00112 case ETAT_WAIT_PARAM : 00113 if(data_received == 'n'){ 00114 rs232.putc('n'); 00115 etat = ETAT_WAIT_PARAM_N; 00116 i_received = 0; 00117 } 00118 break; 00119 case ETAT_WAIT_PARAM_N : 00120 if(data_received == 'f'){ 00121 rs232.putc('f'); 00122 // param number of points 00123 number_points = 0; 00124 pdix = 1; 00125 for(i = i_received-1; i >= 0; i--){ 00126 number_points += (tab_received[i] - '0') * pdix; 00127 pdix = pdix * 10; 00128 } 00129 // next state 00130 etat = ETAT_WAIT_PARAM_F; 00131 i_received = 0; 00132 } 00133 else{ 00134 tab_received[i_received] = data_received; 00135 i_received++; 00136 } 00137 break; 00138 case ETAT_WAIT_PARAM_F : 00139 if(data_received == 's'){ 00140 rs232.putc('s'); 00141 // param frequency 00142 frequency = 0; 00143 pdix = 1; 00144 for(i = i_received-1; i >= 0; i--){ 00145 frequency += (tab_received[i] - '0') * pdix; 00146 pdix = pdix * 10; 00147 } 00148 // next state 00149 etat = ETAT_WAIT_PARAM_S; 00150 i_received = 0; 00151 } 00152 else{ 00153 tab_received[i_received] = data_received; 00154 i_received++; 00155 } 00156 break; 00157 case ETAT_WAIT_PARAM_S : 00158 // param synchro 00159 sync = data_received - '0'; 00160 // next state 00161 etat = ETAT_WAIT_GO; 00162 i_received = 0; 00163 rs232.printf("N=%d;F=%d;S=%d;",number_points, frequency, sync); 00164 break; 00165 case ETAT_WAIT_GO : 00166 if(data_received == 'G'){ 00167 etat = ETAT_ACQUIRING; 00168 blinky.detach(); 00169 blinky.attach(&TIK_blink_led, 0.1); 00170 if(sync == 0){ 00171 float period = 1.0/frequency; 00172 tictoc.attach(&convert_signal,period); 00173 } 00174 } 00175 break; 00176 default : 00177 etat = ETAT_STOP; 00178 debug_led = 0; 00179 } 00180 } 00181 00182 void TIK_blink_led(void){ 00183 debug_led = !debug_led; 00184 }
Generated on Tue Jul 12 2022 21:35:48 by
1.7.2