![](/media/cache/profiles/d3b154a6afe99c7d4f2b3fa23962a134.jpg.50x50_q85.png)
testing functionalities of serial over USB
main.cpp@1:a00097bc510b, 2022-03-10 (annotated)
- Committer:
- ffxx68
- Date:
- Thu Mar 10 14:57:26 2022 +0000
- Revision:
- 1:a00097bc510b
- Parent:
- 0:8765af830f0e
tested as working, receiving a file over serial
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ffxx68 | 0:8765af830f0e | 1 | #include "mbed.h" |
ffxx68 | 1:a00097bc510b | 2 | #define BUF_SIZE 1024 |
ffxx68 | 1:a00097bc510b | 3 | |
ffxx68 | 1:a00097bc510b | 4 | // attributes - peripherals |
ffxx68 | 1:a00097bc510b | 5 | Serial pc(SERIAL_TX, SERIAL_RX); |
ffxx68 | 0:8765af830f0e | 6 | DigitalOut myled(LED1); |
ffxx68 | 1:a00097bc510b | 7 | Timer timeout; |
ffxx68 | 1:a00097bc510b | 8 | Ticker blinker; |
ffxx68 | 0:8765af830f0e | 9 | |
ffxx68 | 1:a00097bc510b | 10 | // attributes - program variables |
ffxx68 | 1:a00097bc510b | 11 | char fileOverSerialBuffer[BUF_SIZE]; // buffer to store received string over pc |
ffxx68 | 1:a00097bc510b | 12 | |
ffxx68 | 1:a00097bc510b | 13 | // local methods |
ffxx68 | 1:a00097bc510b | 14 | void ledBlink ( void ) { |
ffxx68 | 1:a00097bc510b | 15 | myled != myled; |
ffxx68 | 1:a00097bc510b | 16 | } |
ffxx68 | 1:a00097bc510b | 17 | |
ffxx68 | 1:a00097bc510b | 18 | // main |
ffxx68 | 1:a00097bc510b | 19 | int main() { |
ffxx68 | 1:a00097bc510b | 20 | |
ffxx68 | 1:a00097bc510b | 21 | int i; |
ffxx68 | 1:a00097bc510b | 22 | int bytesReceived = 0; |
ffxx68 | 1:a00097bc510b | 23 | |
ffxx68 | 1:a00097bc510b | 24 | pc.baud ( 600 ); // slow communication! |
ffxx68 | 1:a00097bc510b | 25 | //pc.attach(&serialDataCallback); // attach pc ISR |
ffxx68 | 1:a00097bc510b | 26 | |
ffxx68 | 1:a00097bc510b | 27 | while ( true ) { |
ffxx68 | 1:a00097bc510b | 28 | |
ffxx68 | 1:a00097bc510b | 29 | // ready for a new file to be received |
ffxx68 | 1:a00097bc510b | 30 | bytesReceived = 0; |
ffxx68 | 1:a00097bc510b | 31 | timeout.stop(); |
ffxx68 | 1:a00097bc510b | 32 | timeout.reset(); |
ffxx68 | 1:a00097bc510b | 33 | blinker.attach( &ledBlink, 1.0 ); |
ffxx68 | 0:8765af830f0e | 34 | |
ffxx68 | 1:a00097bc510b | 35 | // wait for input |
ffxx68 | 1:a00097bc510b | 36 | while ( pc.readable() && timeout.read() < 1 ) { |
ffxx68 | 1:a00097bc510b | 37 | if ( bytesReceived == 0 ) { |
ffxx68 | 1:a00097bc510b | 38 | timeout.start(); // watchdog |
ffxx68 | 1:a00097bc510b | 39 | timeout.reset(); |
ffxx68 | 1:a00097bc510b | 40 | } |
ffxx68 | 1:a00097bc510b | 41 | if ( bytesReceived < BUF_SIZE-1 ) { // avoid buffer overflow |
ffxx68 | 1:a00097bc510b | 42 | fileOverSerialBuffer[bytesReceived++] = pc.getc(); |
ffxx68 | 1:a00097bc510b | 43 | myled = fileOverSerialBuffer[bytesReceived] & 0x01; // show activity |
ffxx68 | 1:a00097bc510b | 44 | timeout.reset(); // got byte: reset watchdog |
ffxx68 | 1:a00097bc510b | 45 | } else |
ffxx68 | 1:a00097bc510b | 46 | break; // buffer full - stop receiving |
ffxx68 | 0:8765af830f0e | 47 | } |
ffxx68 | 1:a00097bc510b | 48 | |
ffxx68 | 1:a00097bc510b | 49 | // file completed - process |
ffxx68 | 1:a00097bc510b | 50 | fileOverSerialBuffer[bytesReceived+1] = '\x00'; // terminate a string |
ffxx68 | 1:a00097bc510b | 51 | blinker.detach(); // stop flashing |
ffxx68 | 1:a00097bc510b | 52 | myled = 1; // processing |
ffxx68 | 1:a00097bc510b | 53 | pc.printf("Got %d bytes\n\r", bytesReceived); |
ffxx68 | 1:a00097bc510b | 54 | for ( i=0; i < bytesReceived; i++) { |
ffxx68 | 1:a00097bc510b | 55 | pc.printf("%.2x ", fileOverSerialBuffer[i]); |
ffxx68 | 1:a00097bc510b | 56 | if ( !(i%16) && i!=0 ) pc.printf("\n\r"); |
ffxx68 | 1:a00097bc510b | 57 | } |
ffxx68 | 1:a00097bc510b | 58 | pc.printf("\n\r"); |
ffxx68 | 1:a00097bc510b | 59 | myled = 0; // done |
ffxx68 | 0:8765af830f0e | 60 | |
ffxx68 | 1:a00097bc510b | 61 | |
ffxx68 | 1:a00097bc510b | 62 | } |
ffxx68 | 0:8765af830f0e | 63 | } |