Reception of multiple characters over UART without interrupting the program. Uses 2 input buffers.
Dependencies: mbed
Fork of Nucleo-getc by
main.cpp@3:e316c5e314e1, 2015-05-12 (annotated)
- Committer:
- Foxnec
- Date:
- Tue May 12 09:31:21 2015 +0000
- Revision:
- 3:e316c5e314e1
- Parent:
- 2:416bbbc23e82
- Child:
- 4:472805b710a0
Changes to comments
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dousape2 | 1:4486f14a88ad | 1 | /********************************************************************************** |
dousape2 | 1:4486f14a88ad | 2 | * @file main.cpp |
dousape2 | 1:4486f14a88ad | 3 | * @author Petr Dousa |
dousape2 | 1:4486f14a88ad | 4 | * @version V1.00 |
dousape2 | 1:4486f14a88ad | 5 | * @date 22-March-2015 |
Foxnec | 3:e316c5e314e1 | 6 | * @brief Every single second push to serial message and read from serial |
dousape2 | 1:4486f14a88ad | 7 | * Serial speed is set to 115200. |
dousape2 | 1:4486f14a88ad | 8 | ***********************************************************************************/ |
dousape2 | 1:4486f14a88ad | 9 | |
dousape2 | 1:4486f14a88ad | 10 | /* Includes ----------------------------------------------------------------------*/ |
dousape2 | 0:2a0d1131965d | 11 | #include "mbed.h" |
dousape2 | 0:2a0d1131965d | 12 | |
dousape2 | 1:4486f14a88ad | 13 | /* Defines -----------------------------------------------------------------------*/ |
dousape2 | 1:4486f14a88ad | 14 | #define buffer_size 256 // incoming buffer size |
dousape2 | 1:4486f14a88ad | 15 | #define buffer_fill buffer_size+1 // number, when buffer is ready |
dousape2 | 1:4486f14a88ad | 16 | |
dousape2 | 1:4486f14a88ad | 17 | /* Function prototypes -----------------------------------------------------------*/ |
dousape2 | 1:4486f14a88ad | 18 | void serialRx(); |
dousape2 | 2:416bbbc23e82 | 19 | int serialGetBuffer(char * & data); |
dousape2 | 0:2a0d1131965d | 20 | |
dousape2 | 1:4486f14a88ad | 21 | /* Variables ---------------------------------------------------------------------*/ |
Foxnec | 3:e316c5e314e1 | 22 | // additional variables for incoming data |
dousape2 | 1:4486f14a88ad | 23 | char serial_buffer[buffer_size]; // buffer to save incoming data |
dousape2 | 1:4486f14a88ad | 24 | char serial_buffer2[buffer_size]; // second buffer to save incoming data |
dousape2 | 1:4486f14a88ad | 25 | int serial_buffer_where=0; // index array for buffer |
dousape2 | 1:4486f14a88ad | 26 | int serial_buffer2_where=0; // index array for second buffer |
dousape2 | 1:4486f14a88ad | 27 | bool serial_end_line = false; // searching for end line |
dousape2 | 1:4486f14a88ad | 28 | |
dousape2 | 1:4486f14a88ad | 29 | //mbed - initialization of peripherals |
dousape2 | 0:2a0d1131965d | 30 | Serial pc(SERIAL_TX, SERIAL_RX); |
dousape2 | 0:2a0d1131965d | 31 | DigitalOut myled(LED1); |
dousape2 | 0:2a0d1131965d | 32 | |
dousape2 | 1:4486f14a88ad | 33 | /* Functions----------------------------------------------------------------------*/ |
dousape2 | 0:2a0d1131965d | 34 | |
dousape2 | 1:4486f14a88ad | 35 | /******************************************************************************* |
dousape2 | 1:4486f14a88ad | 36 | * Function Name : serialRx. |
dousape2 | 2:416bbbc23e82 | 37 | * Description : Incoming interruption fom serial. Fill two buffers with |
Foxnec | 3:e316c5e314e1 | 38 | * incoming data and prepare it to serialGetBuffer. |
dousape2 | 1:4486f14a88ad | 39 | * Input : None. |
dousape2 | 1:4486f14a88ad | 40 | * Output : None. |
dousape2 | 1:4486f14a88ad | 41 | * Return : None. |
dousape2 | 1:4486f14a88ad | 42 | *******************************************************************************/ |
dousape2 | 0:2a0d1131965d | 43 | void serialRx() |
dousape2 | 0:2a0d1131965d | 44 | { |
dousape2 | 1:4486f14a88ad | 45 | while(pc.readable()) { // read all data from serial |
dousape2 | 1:4486f14a88ad | 46 | char character=pc.getc(); // get a char form serial |
dousape2 | 1:4486f14a88ad | 47 | if(((int)character==10 || (int)character==13) && serial_end_line) { // search for end line /r or /n |
Foxnec | 3:e316c5e314e1 | 48 | serial_end_line=true; // end was find in the previous charascter, skip |
dousape2 | 0:2a0d1131965d | 49 | continue; |
dousape2 | 1:4486f14a88ad | 50 | } else { |
dousape2 | 1:4486f14a88ad | 51 | serial_end_line=false; // clear serial_end_line flag |
dousape2 | 1:4486f14a88ad | 52 | } |
Foxnec | 3:e316c5e314e1 | 53 | if(serial_buffer_where!=buffer_fill && !(serial_buffer2_where != 0 && serial_buffer2_where!=buffer_fill)) { |
dousape2 | 0:2a0d1131965d | 54 | serial_buffer[serial_buffer_where++]=character; |
Foxnec | 3:e316c5e314e1 | 55 | if(serial_buffer_where==buffer_size) { // check if incoming data are smaller than buffer size |
dousape2 | 1:4486f14a88ad | 56 | serial_buffer[buffer_size-1]='\0'; // posibility to lost data, if the incoming data are too big |
dousape2 | 1:4486f14a88ad | 57 | serial_buffer_where=buffer_fill; // set index array to indicate buffer fill |
dousape2 | 2:416bbbc23e82 | 58 | continue; |
dousape2 | 0:2a0d1131965d | 59 | } |
Foxnec | 3:e316c5e314e1 | 60 | if(character==13 || character==10) { // if end of line (\r \n) is indicated, prepare the buffer to serialGetBuffer |
dousape2 | 1:4486f14a88ad | 61 | serial_buffer[serial_buffer_where-1]='\0'; // set end of buffer with 0 |
dousape2 | 1:4486f14a88ad | 62 | serial_buffer_where=buffer_fill; // set index array to indicate buffer fill |
dousape2 | 1:4486f14a88ad | 63 | serial_end_line=true; // end of line was find, set serial_end_line flag |
dousape2 | 0:2a0d1131965d | 64 | } |
dousape2 | 1:4486f14a88ad | 65 | } else if(serial_buffer2_where!=buffer_fill) { // same for second buffer |
dousape2 | 0:2a0d1131965d | 66 | serial_buffer2[serial_buffer2_where++]=character; |
dousape2 | 0:2a0d1131965d | 67 | if(serial_buffer2_where==buffer_size) { |
dousape2 | 0:2a0d1131965d | 68 | serial_buffer2[buffer_size-1]='\0'; |
dousape2 | 0:2a0d1131965d | 69 | serial_buffer2_where=buffer_fill; |
dousape2 | 2:416bbbc23e82 | 70 | continue; |
dousape2 | 0:2a0d1131965d | 71 | } |
dousape2 | 0:2a0d1131965d | 72 | if(character==13 || character==10) { |
dousape2 | 0:2a0d1131965d | 73 | serial_buffer2[serial_buffer2_where-1]='\0'; |
dousape2 | 0:2a0d1131965d | 74 | serial_buffer2_where=buffer_fill; |
dousape2 | 0:2a0d1131965d | 75 | serial_end_line=true; |
dousape2 | 0:2a0d1131965d | 76 | } |
Foxnec | 3:e316c5e314e1 | 77 | } /*else { // the buffers are full and thedata are lost |
dousape2 | 0:2a0d1131965d | 78 | while(!pc.writeable()); |
dousape2 | 0:2a0d1131965d | 79 | pc.printf("Code is too slow!\n"); |
dousape2 | 1:4486f14a88ad | 80 | //pc.printf("I get: \"%c\", DEC: %d\n",character,(int)character); |
dousape2 | 0:2a0d1131965d | 81 | }*/ |
dousape2 | 0:2a0d1131965d | 82 | } |
dousape2 | 0:2a0d1131965d | 83 | } |
dousape2 | 0:2a0d1131965d | 84 | |
dousape2 | 1:4486f14a88ad | 85 | /******************************************************************************* |
dousape2 | 1:4486f14a88ad | 86 | * Function Name : serialGetBuffer. |
dousape2 | 1:4486f14a88ad | 87 | * Description : Timer overflow handle. Blinks the LED. |
dousape2 | 1:4486f14a88ad | 88 | * Input : Char array with size buffer_size. |
dousape2 | 1:4486f14a88ad | 89 | * Output : Char array with incoming data. |
dousape2 | 1:4486f14a88ad | 90 | * Return : 0 - on no data, 1 - one buffer is fill, 2 -two buffers are fill (posibility to lost data) |
dousape2 | 1:4486f14a88ad | 91 | *******************************************************************************/ |
dousape2 | 2:416bbbc23e82 | 92 | int serialGetBuffer(char * & data) |
dousape2 | 0:2a0d1131965d | 93 | { |
dousape2 | 0:2a0d1131965d | 94 | if(serial_buffer_where==buffer_fill && serial_buffer2_where==buffer_fill) { |
dousape2 | 2:416bbbc23e82 | 95 | data=serial_buffer; |
dousape2 | 2:416bbbc23e82 | 96 | //memcpy(data, serial_buffer, strlen(serial_buffer)+1); |
dousape2 | 0:2a0d1131965d | 97 | serial_buffer_where=0; |
dousape2 | 0:2a0d1131965d | 98 | return 2; |
dousape2 | 0:2a0d1131965d | 99 | } else if(serial_buffer2_where==buffer_fill) { |
dousape2 | 2:416bbbc23e82 | 100 | data=serial_buffer2; |
dousape2 | 2:416bbbc23e82 | 101 | //memcpy(data, serial_buffer2, strlen(serial_buffer2)+1); |
dousape2 | 0:2a0d1131965d | 102 | serial_buffer2_where=0; |
dousape2 | 0:2a0d1131965d | 103 | return 1; |
dousape2 | 0:2a0d1131965d | 104 | } else if(serial_buffer_where==buffer_fill) { |
dousape2 | 2:416bbbc23e82 | 105 | data=serial_buffer; |
dousape2 | 2:416bbbc23e82 | 106 | //memcpy(data, serial_buffer, strlen(serial_buffer)+1); |
dousape2 | 0:2a0d1131965d | 107 | serial_buffer_where=0; |
dousape2 | 0:2a0d1131965d | 108 | return 1; |
dousape2 | 0:2a0d1131965d | 109 | } else { |
dousape2 | 0:2a0d1131965d | 110 | return 0; |
dousape2 | 0:2a0d1131965d | 111 | } |
dousape2 | 0:2a0d1131965d | 112 | } |
dousape2 | 0:2a0d1131965d | 113 | |
dousape2 | 1:4486f14a88ad | 114 | /*********************************************************************************** |
dousape2 | 1:4486f14a88ad | 115 | * Function Name : main. |
dousape2 | 1:4486f14a88ad | 116 | * Description : Main routine. |
dousape2 | 1:4486f14a88ad | 117 | * Input : None. |
dousape2 | 1:4486f14a88ad | 118 | * Output : None. |
dousape2 | 1:4486f14a88ad | 119 | * Return : None. |
dousape2 | 1:4486f14a88ad | 120 | ***********************************************************************************/ |
dousape2 | 1:4486f14a88ad | 121 | int main() |
dousape2 | 1:4486f14a88ad | 122 | { |
dousape2 | 1:4486f14a88ad | 123 | int i = 1; |
dousape2 | 1:4486f14a88ad | 124 | int j = 0; |
dousape2 | 2:416bbbc23e82 | 125 | int k = 0; |
dousape2 | 1:4486f14a88ad | 126 | |
Foxnec | 3:e316c5e314e1 | 127 | //array for data that are coming to user |
dousape2 | 2:416bbbc23e82 | 128 | char * prijata_data; |
dousape2 | 1:4486f14a88ad | 129 | |
Foxnec | 3:e316c5e314e1 | 130 | pc.baud(115200); // set serial speed to 115200 |
Foxnec | 3:e316c5e314e1 | 131 | pc.attach(&serialRx,Serial::RxIrq); // attach serialRx to reception interrupt |
dousape2 | 1:4486f14a88ad | 132 | pc.printf("Hello World !\n"); |
dousape2 | 1:4486f14a88ad | 133 | |
dousape2 | 1:4486f14a88ad | 134 | while(1) { |
Foxnec | 3:e316c5e314e1 | 135 | wait_ms(10); // wait 1 second |
dousape2 | 1:4486f14a88ad | 136 | if(serialGetBuffer(prijata_data)) { // get char array from serial |
dousape2 | 1:4486f14a88ad | 137 | sscanf (prijata_data,"%d",&j); // sscanf for searching integer |
dousape2 | 1:4486f14a88ad | 138 | pc.printf("From PC:%d.\n" ,j); // write to serial incoming integer |
dousape2 | 1:4486f14a88ad | 139 | } |
dousape2 | 2:416bbbc23e82 | 140 | |
dousape2 | 2:416bbbc23e82 | 141 | k++; |
dousape2 | 2:416bbbc23e82 | 142 | |
dousape2 | 2:416bbbc23e82 | 143 | if (k>=100) { |
Foxnec | 3:e316c5e314e1 | 144 | pc.printf("This program runs since %d seconds.\n", i++); // write some string to serial |
dousape2 | 2:416bbbc23e82 | 145 | |
dousape2 | 2:416bbbc23e82 | 146 | myled = !myled; // blink LED |
dousape2 | 2:416bbbc23e82 | 147 | |
dousape2 | 2:416bbbc23e82 | 148 | k=0; |
dousape2 | 2:416bbbc23e82 | 149 | } |
dousape2 | 1:4486f14a88ad | 150 | } |
dousape2 | 1:4486f14a88ad | 151 | } |
dousape2 | 1:4486f14a88ad | 152 |