![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Example of communication between a computer and a Nucleo board - RS232/USB - a terminal or an interface is required on the computer
main.cpp@0:32af38c93bc6, 2020-03-17 (annotated)
- Committer:
- villemejane
- Date:
- Tue Mar 17 07:07:00 2020 +0000
- Revision:
- 0:32af38c93bc6
Example of communication between a computer and a Nucleo board - RS232/USB - A terminal or interface is required
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
villemejane | 0:32af38c93bc6 | 1 | #include "mbed.h" |
villemejane | 0:32af38c93bc6 | 2 | |
villemejane | 0:32af38c93bc6 | 3 | #define ETAT_STOP 0 |
villemejane | 0:32af38c93bc6 | 4 | #define ETAT_WAIT_PARAM 1 |
villemejane | 0:32af38c93bc6 | 5 | #define ETAT_WAIT_PARAM_N 11 |
villemejane | 0:32af38c93bc6 | 6 | #define ETAT_WAIT_PARAM_F 12 |
villemejane | 0:32af38c93bc6 | 7 | #define ETAT_WAIT_PARAM_S 13 |
villemejane | 0:32af38c93bc6 | 8 | #define ETAT_WAIT_GO 2 |
villemejane | 0:32af38c93bc6 | 9 | #define ETAT_ACQUIRING 3 |
villemejane | 0:32af38c93bc6 | 10 | #define ETAT_SENDING_DATA 4 |
villemejane | 0:32af38c93bc6 | 11 | |
villemejane | 0:32af38c93bc6 | 12 | // inputs and outputs configuration |
villemejane | 0:32af38c93bc6 | 13 | DigitalOut debug_led(LED1); |
villemejane | 0:32af38c93bc6 | 14 | Serial rs232(USBTX, USBRX); |
villemejane | 0:32af38c93bc6 | 15 | AnalogIn can_input(A0); |
villemejane | 0:32af38c93bc6 | 16 | InterruptIn sync_input(D10); |
villemejane | 0:32af38c93bc6 | 17 | |
villemejane | 0:32af38c93bc6 | 18 | Ticker tictoc; |
villemejane | 0:32af38c93bc6 | 19 | Ticker blinky; |
villemejane | 0:32af38c93bc6 | 20 | |
villemejane | 0:32af38c93bc6 | 21 | // System functions |
villemejane | 0:32af38c93bc6 | 22 | void ISR_sync(void); |
villemejane | 0:32af38c93bc6 | 23 | void convert_signal(void); |
villemejane | 0:32af38c93bc6 | 24 | void ISR_get_data(void); |
villemejane | 0:32af38c93bc6 | 25 | void ISR_send_data(void); |
villemejane | 0:32af38c93bc6 | 26 | void TIK_blink_led(void); |
villemejane | 0:32af38c93bc6 | 27 | |
villemejane | 0:32af38c93bc6 | 28 | // System variables |
villemejane | 0:32af38c93bc6 | 29 | int i, pdix, temp; |
villemejane | 0:32af38c93bc6 | 30 | char etat = 0; |
villemejane | 0:32af38c93bc6 | 31 | char data_received, i_received; |
villemejane | 0:32af38c93bc6 | 32 | char tab_received[10] = {0}; |
villemejane | 0:32af38c93bc6 | 33 | |
villemejane | 0:32af38c93bc6 | 34 | // Acquisition parameters |
villemejane | 0:32af38c93bc6 | 35 | int number_points; |
villemejane | 0:32af38c93bc6 | 36 | int frequency; |
villemejane | 0:32af38c93bc6 | 37 | char sync = 0; |
villemejane | 0:32af38c93bc6 | 38 | |
villemejane | 0:32af38c93bc6 | 39 | // Acquisition data |
villemejane | 0:32af38c93bc6 | 40 | int i_data; |
villemejane | 0:32af38c93bc6 | 41 | int tab_data[10000]; |
villemejane | 0:32af38c93bc6 | 42 | |
villemejane | 0:32af38c93bc6 | 43 | // Main function |
villemejane | 0:32af38c93bc6 | 44 | int main() { |
villemejane | 0:32af38c93bc6 | 45 | number_points = 0; |
villemejane | 0:32af38c93bc6 | 46 | frequency = 0; |
villemejane | 0:32af38c93bc6 | 47 | i_received = 0; |
villemejane | 0:32af38c93bc6 | 48 | etat = ETAT_STOP; |
villemejane | 0:32af38c93bc6 | 49 | data_received = '0'; |
villemejane | 0:32af38c93bc6 | 50 | debug_led = 0; |
villemejane | 0:32af38c93bc6 | 51 | i_data = 0; |
villemejane | 0:32af38c93bc6 | 52 | rs232.baud(115200); |
villemejane | 0:32af38c93bc6 | 53 | rs232.attach(&ISR_get_data); |
villemejane | 0:32af38c93bc6 | 54 | sync_input.rise(&ISR_sync); |
villemejane | 0:32af38c93bc6 | 55 | while(1) { |
villemejane | 0:32af38c93bc6 | 56 | switch(etat){ |
villemejane | 0:32af38c93bc6 | 57 | |
villemejane | 0:32af38c93bc6 | 58 | case ETAT_SENDING_DATA : |
villemejane | 0:32af38c93bc6 | 59 | rs232.putc('d'); |
villemejane | 0:32af38c93bc6 | 60 | blinky.detach(); |
villemejane | 0:32af38c93bc6 | 61 | tictoc.detach(); |
villemejane | 0:32af38c93bc6 | 62 | debug_led = 1; |
villemejane | 0:32af38c93bc6 | 63 | for(i = 0; i < number_points; i++){ |
villemejane | 0:32af38c93bc6 | 64 | rs232.printf("[%d;%d]", i, tab_data[i]); |
villemejane | 0:32af38c93bc6 | 65 | } |
villemejane | 0:32af38c93bc6 | 66 | etat = ETAT_STOP; |
villemejane | 0:32af38c93bc6 | 67 | debug_led = 0; |
villemejane | 0:32af38c93bc6 | 68 | break; |
villemejane | 0:32af38c93bc6 | 69 | default : |
villemejane | 0:32af38c93bc6 | 70 | temp = 0; |
villemejane | 0:32af38c93bc6 | 71 | } |
villemejane | 0:32af38c93bc6 | 72 | } |
villemejane | 0:32af38c93bc6 | 73 | } |
villemejane | 0:32af38c93bc6 | 74 | |
villemejane | 0:32af38c93bc6 | 75 | void ISR_sync(void){ |
villemejane | 0:32af38c93bc6 | 76 | if((etat == ETAT_ACQUIRING) && (sync == 1)){ |
villemejane | 0:32af38c93bc6 | 77 | float period = 1.0/frequency; |
villemejane | 0:32af38c93bc6 | 78 | tictoc.attach(&convert_signal,period); |
villemejane | 0:32af38c93bc6 | 79 | } |
villemejane | 0:32af38c93bc6 | 80 | } |
villemejane | 0:32af38c93bc6 | 81 | |
villemejane | 0:32af38c93bc6 | 82 | void convert_signal(void){ |
villemejane | 0:32af38c93bc6 | 83 | tab_data[i_data] = can_input.read_u16() >> 4; |
villemejane | 0:32af38c93bc6 | 84 | i_data++; |
villemejane | 0:32af38c93bc6 | 85 | if(i_data == number_points){ |
villemejane | 0:32af38c93bc6 | 86 | etat = ETAT_SENDING_DATA; |
villemejane | 0:32af38c93bc6 | 87 | tictoc.detach(); |
villemejane | 0:32af38c93bc6 | 88 | } |
villemejane | 0:32af38c93bc6 | 89 | } |
villemejane | 0:32af38c93bc6 | 90 | |
villemejane | 0:32af38c93bc6 | 91 | void ISR_send_data(){ |
villemejane | 0:32af38c93bc6 | 92 | i++; |
villemejane | 0:32af38c93bc6 | 93 | rs232.printf("i=%d:\r\n", i); |
villemejane | 0:32af38c93bc6 | 94 | } |
villemejane | 0:32af38c93bc6 | 95 | |
villemejane | 0:32af38c93bc6 | 96 | void ISR_get_data(){ |
villemejane | 0:32af38c93bc6 | 97 | data_received = rs232.getc(); |
villemejane | 0:32af38c93bc6 | 98 | if((data_received == 'U') && (etat != ETAT_STOP) && (etat != ETAT_ACQUIRING) && (etat != ETAT_SENDING_DATA)){ // UPDATE parameters |
villemejane | 0:32af38c93bc6 | 99 | rs232.putc('u'); |
villemejane | 0:32af38c93bc6 | 100 | etat = ETAT_WAIT_PARAM; |
villemejane | 0:32af38c93bc6 | 101 | blinky.attach(&TIK_blink_led, 0.5); |
villemejane | 0:32af38c93bc6 | 102 | } |
villemejane | 0:32af38c93bc6 | 103 | |
villemejane | 0:32af38c93bc6 | 104 | switch(etat){ |
villemejane | 0:32af38c93bc6 | 105 | case ETAT_STOP : |
villemejane | 0:32af38c93bc6 | 106 | if(data_received == 'a'){ |
villemejane | 0:32af38c93bc6 | 107 | etat = ETAT_WAIT_PARAM; |
villemejane | 0:32af38c93bc6 | 108 | rs232.putc('o'); |
villemejane | 0:32af38c93bc6 | 109 | blinky.attach(&TIK_blink_led, 0.5); |
villemejane | 0:32af38c93bc6 | 110 | } |
villemejane | 0:32af38c93bc6 | 111 | break; |
villemejane | 0:32af38c93bc6 | 112 | case ETAT_WAIT_PARAM : |
villemejane | 0:32af38c93bc6 | 113 | if(data_received == 'n'){ |
villemejane | 0:32af38c93bc6 | 114 | rs232.putc('n'); |
villemejane | 0:32af38c93bc6 | 115 | etat = ETAT_WAIT_PARAM_N; |
villemejane | 0:32af38c93bc6 | 116 | i_received = 0; |
villemejane | 0:32af38c93bc6 | 117 | } |
villemejane | 0:32af38c93bc6 | 118 | break; |
villemejane | 0:32af38c93bc6 | 119 | case ETAT_WAIT_PARAM_N : |
villemejane | 0:32af38c93bc6 | 120 | if(data_received == 'f'){ |
villemejane | 0:32af38c93bc6 | 121 | rs232.putc('f'); |
villemejane | 0:32af38c93bc6 | 122 | // param number of points |
villemejane | 0:32af38c93bc6 | 123 | number_points = 0; |
villemejane | 0:32af38c93bc6 | 124 | pdix = 1; |
villemejane | 0:32af38c93bc6 | 125 | for(i = i_received-1; i >= 0; i--){ |
villemejane | 0:32af38c93bc6 | 126 | number_points += (tab_received[i] - '0') * pdix; |
villemejane | 0:32af38c93bc6 | 127 | pdix = pdix * 10; |
villemejane | 0:32af38c93bc6 | 128 | } |
villemejane | 0:32af38c93bc6 | 129 | // next state |
villemejane | 0:32af38c93bc6 | 130 | etat = ETAT_WAIT_PARAM_F; |
villemejane | 0:32af38c93bc6 | 131 | i_received = 0; |
villemejane | 0:32af38c93bc6 | 132 | } |
villemejane | 0:32af38c93bc6 | 133 | else{ |
villemejane | 0:32af38c93bc6 | 134 | tab_received[i_received] = data_received; |
villemejane | 0:32af38c93bc6 | 135 | i_received++; |
villemejane | 0:32af38c93bc6 | 136 | } |
villemejane | 0:32af38c93bc6 | 137 | break; |
villemejane | 0:32af38c93bc6 | 138 | case ETAT_WAIT_PARAM_F : |
villemejane | 0:32af38c93bc6 | 139 | if(data_received == 's'){ |
villemejane | 0:32af38c93bc6 | 140 | rs232.putc('s'); |
villemejane | 0:32af38c93bc6 | 141 | // param frequency |
villemejane | 0:32af38c93bc6 | 142 | frequency = 0; |
villemejane | 0:32af38c93bc6 | 143 | pdix = 1; |
villemejane | 0:32af38c93bc6 | 144 | for(i = i_received-1; i >= 0; i--){ |
villemejane | 0:32af38c93bc6 | 145 | frequency += (tab_received[i] - '0') * pdix; |
villemejane | 0:32af38c93bc6 | 146 | pdix = pdix * 10; |
villemejane | 0:32af38c93bc6 | 147 | } |
villemejane | 0:32af38c93bc6 | 148 | // next state |
villemejane | 0:32af38c93bc6 | 149 | etat = ETAT_WAIT_PARAM_S; |
villemejane | 0:32af38c93bc6 | 150 | i_received = 0; |
villemejane | 0:32af38c93bc6 | 151 | } |
villemejane | 0:32af38c93bc6 | 152 | else{ |
villemejane | 0:32af38c93bc6 | 153 | tab_received[i_received] = data_received; |
villemejane | 0:32af38c93bc6 | 154 | i_received++; |
villemejane | 0:32af38c93bc6 | 155 | } |
villemejane | 0:32af38c93bc6 | 156 | break; |
villemejane | 0:32af38c93bc6 | 157 | case ETAT_WAIT_PARAM_S : |
villemejane | 0:32af38c93bc6 | 158 | // param synchro |
villemejane | 0:32af38c93bc6 | 159 | sync = data_received - '0'; |
villemejane | 0:32af38c93bc6 | 160 | // next state |
villemejane | 0:32af38c93bc6 | 161 | etat = ETAT_WAIT_GO; |
villemejane | 0:32af38c93bc6 | 162 | i_received = 0; |
villemejane | 0:32af38c93bc6 | 163 | rs232.printf("N=%d;F=%d;S=%d;",number_points, frequency, sync); |
villemejane | 0:32af38c93bc6 | 164 | break; |
villemejane | 0:32af38c93bc6 | 165 | case ETAT_WAIT_GO : |
villemejane | 0:32af38c93bc6 | 166 | if(data_received == 'G'){ |
villemejane | 0:32af38c93bc6 | 167 | etat = ETAT_ACQUIRING; |
villemejane | 0:32af38c93bc6 | 168 | blinky.detach(); |
villemejane | 0:32af38c93bc6 | 169 | blinky.attach(&TIK_blink_led, 0.1); |
villemejane | 0:32af38c93bc6 | 170 | if(sync == 0){ |
villemejane | 0:32af38c93bc6 | 171 | float period = 1.0/frequency; |
villemejane | 0:32af38c93bc6 | 172 | tictoc.attach(&convert_signal,period); |
villemejane | 0:32af38c93bc6 | 173 | } |
villemejane | 0:32af38c93bc6 | 174 | } |
villemejane | 0:32af38c93bc6 | 175 | break; |
villemejane | 0:32af38c93bc6 | 176 | default : |
villemejane | 0:32af38c93bc6 | 177 | etat = ETAT_STOP; |
villemejane | 0:32af38c93bc6 | 178 | debug_led = 0; |
villemejane | 0:32af38c93bc6 | 179 | } |
villemejane | 0:32af38c93bc6 | 180 | } |
villemejane | 0:32af38c93bc6 | 181 | |
villemejane | 0:32af38c93bc6 | 182 | void TIK_blink_led(void){ |
villemejane | 0:32af38c93bc6 | 183 | debug_led = !debug_led; |
villemejane | 0:32af38c93bc6 | 184 | } |