Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
src/serial.cpp@30:d8721a46ee03, 2019-03-06 (annotated)
- Committer:
- Slord2142
- Date:
- Wed Mar 06 22:32:31 2019 +0000
- Revision:
- 30:d8721a46ee03
- Parent:
- 16:5791665200cb
Removed extraneous comments from all files
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mfwic | 0:44a3005d4f20 | 1 | #include "mbed.h" |
mfwic | 0:44a3005d4f20 | 2 | #include "serial.h" |
mfwic | 0:44a3005d4f20 | 3 | #include "stdio.h" |
mfwic | 0:44a3005d4f20 | 4 | #include "string.h" |
mfwic | 0:44a3005d4f20 | 5 | #include "globals.h" |
mfwic | 1:9f8583ba2431 | 6 | #include "all_io.h" |
mfwic | 0:44a3005d4f20 | 7 | |
mfwic | 0:44a3005d4f20 | 8 | /************* GLOBAL VARIABLES *****************************/ |
mfwic | 0:44a3005d4f20 | 9 | struct serialStatusBits serialStatus; |
mfwic | 16:5791665200cb | 10 | char strbuf[150] = {0}; |
mfwic | 0:44a3005d4f20 | 11 | char rxbuf[MAXRXBUF+1] = {0}; |
mfwic | 0:44a3005d4f20 | 12 | volatile unsigned int bufloc = 0; |
mfwic | 0:44a3005d4f20 | 13 | |
mfwic | 0:44a3005d4f20 | 14 | /************* FORWARD DECLARATIONS *************************/ |
mfwic | 0:44a3005d4f20 | 15 | void Rx_IRQHandler(void); |
mfwic | 0:44a3005d4f20 | 16 | |
mfwic | 0:44a3005d4f20 | 17 | |
mfwic | 0:44a3005d4f20 | 18 | // set up serial port for 115200 baud |
mfwic | 7:860b3a8275cb | 19 | Serial dcm1(USBTX, USBRX, 921600); |
mfwic | 0:44a3005d4f20 | 20 | |
mfwic | 0:44a3005d4f20 | 21 | |
mfwic | 0:44a3005d4f20 | 22 | // initialize the serial port variables |
mfwic | 0:44a3005d4f20 | 23 | void initSerial(void) |
mfwic | 0:44a3005d4f20 | 24 | { |
mfwic | 0:44a3005d4f20 | 25 | // create the RxIRQ callback |
mfwic | 2:46faae84b8b0 | 26 | dcm1.attach(&Rx_IRQHandler, Serial::RxIrq); |
mfwic | 0:44a3005d4f20 | 27 | |
mfwic | 0:44a3005d4f20 | 28 | } |
mfwic | 0:44a3005d4f20 | 29 | |
mfwic | 0:44a3005d4f20 | 30 | |
mfwic | 0:44a3005d4f20 | 31 | // this is the interrupt request handler (IRQ) for ALL Recoev interrupts |
mfwic | 0:44a3005d4f20 | 32 | void Rx_IRQHandler(void) |
mfwic | 0:44a3005d4f20 | 33 | { |
mfwic | 0:44a3005d4f20 | 34 | // reinit the repeat flag |
mfwic | 0:44a3005d4f20 | 35 | serialStatus.repeat = FALSE; |
mfwic | 0:44a3005d4f20 | 36 | |
mfwic | 0:44a3005d4f20 | 37 | // check if the USART1 receive interrupt flag was set |
mfwic | 2:46faae84b8b0 | 38 | while (dcm1.readable()) |
mfwic | 0:44a3005d4f20 | 39 | { |
mfwic | 2:46faae84b8b0 | 40 | rxbuf[bufloc] = dcm1.getc(); |
mfwic | 0:44a3005d4f20 | 41 | |
mfwic | 0:44a3005d4f20 | 42 | if (rxbuf[bufloc] == CR) // check for end of command |
mfwic | 0:44a3005d4f20 | 43 | { |
mfwic | 0:44a3005d4f20 | 44 | serialStatus.command = TRUE; |
mfwic | 0:44a3005d4f20 | 45 | rxbuf[bufloc] = 0; |
mfwic | 0:44a3005d4f20 | 46 | bufloc = 0; |
mfwic | 0:44a3005d4f20 | 47 | return; |
mfwic | 0:44a3005d4f20 | 48 | } |
mfwic | 0:44a3005d4f20 | 49 | else if (rxbuf[bufloc] == '$') // check for computer command |
mfwic | 0:44a3005d4f20 | 50 | { |
mfwic | 0:44a3005d4f20 | 51 | serialStatus.computer = TRUE; |
mfwic | 0:44a3005d4f20 | 52 | bufloc = 0; // reset the buffer if computer command |
mfwic | 0:44a3005d4f20 | 53 | } |
mfwic | 0:44a3005d4f20 | 54 | else if (rxbuf[bufloc] == ')') // check for repeat command |
mfwic | 0:44a3005d4f20 | 55 | { |
mfwic | 0:44a3005d4f20 | 56 | serialStatus.repeat = TRUE; |
mfwic | 0:44a3005d4f20 | 57 | rxbuf[bufloc] = 0; |
mfwic | 0:44a3005d4f20 | 58 | } |
mfwic | 0:44a3005d4f20 | 59 | |
mfwic | 0:44a3005d4f20 | 60 | // Echo character back out the serial port |
mfwic | 0:44a3005d4f20 | 61 | if (!serialStatus.computer) |
mfwic | 0:44a3005d4f20 | 62 | { |
mfwic | 2:46faae84b8b0 | 63 | dcm1.putc((unsigned char)rxbuf[bufloc]); |
mfwic | 0:44a3005d4f20 | 64 | } |
mfwic | 0:44a3005d4f20 | 65 | |
mfwic | 0:44a3005d4f20 | 66 | // handle backspace |
mfwic | 0:44a3005d4f20 | 67 | if (rxbuf[bufloc] != 0x08) |
mfwic | 0:44a3005d4f20 | 68 | { |
mfwic | 0:44a3005d4f20 | 69 | bufloc++; // increment the location |
mfwic | 0:44a3005d4f20 | 70 | } |
mfwic | 0:44a3005d4f20 | 71 | else |
mfwic | 0:44a3005d4f20 | 72 | { |
mfwic | 0:44a3005d4f20 | 73 | if (bufloc > 0) |
mfwic | 0:44a3005d4f20 | 74 | { |
mfwic | 0:44a3005d4f20 | 75 | rxbuf[--bufloc] = 0; // set to null |
mfwic | 0:44a3005d4f20 | 76 | } |
mfwic | 0:44a3005d4f20 | 77 | else |
mfwic | 0:44a3005d4f20 | 78 | { |
mfwic | 0:44a3005d4f20 | 79 | rxbuf[bufloc] = 0; // set to null |
mfwic | 0:44a3005d4f20 | 80 | } |
mfwic | 0:44a3005d4f20 | 81 | } |
mfwic | 0:44a3005d4f20 | 82 | |
mfwic | 0:44a3005d4f20 | 83 | // check for overflow |
mfwic | 0:44a3005d4f20 | 84 | if (bufloc >= MAXRXBUF) |
mfwic | 0:44a3005d4f20 | 85 | { |
mfwic | 0:44a3005d4f20 | 86 | bufloc = 0; |
mfwic | 0:44a3005d4f20 | 87 | rxbuf[0] = 0; |
mfwic | 0:44a3005d4f20 | 88 | } |
mfwic | 0:44a3005d4f20 | 89 | |
Slord2142 | 30:d8721a46ee03 | 90 | |
mfwic | 0:44a3005d4f20 | 91 | } |
mfwic | 0:44a3005d4f20 | 92 | } |
mfwic | 0:44a3005d4f20 | 93 | |
mfwic | 0:44a3005d4f20 | 94 | |
mfwic | 0:44a3005d4f20 | 95 | |
mfwic | 1:9f8583ba2431 | 96 | // send buffer to the serial port |
mfwic | 0:44a3005d4f20 | 97 | void sendSerial(char *buf) |
mfwic | 0:44a3005d4f20 | 98 | { |
mfwic | 2:46faae84b8b0 | 99 | dcm1.printf(buf); |
mfwic | 0:44a3005d4f20 | 100 | } |
mfwic | 0:44a3005d4f20 | 101 | |
mfwic | 1:9f8583ba2431 | 102 | /************************************************************ |
mfwic | 1:9f8583ba2431 | 103 | * Routine: showRangeError |
mfwic | 1:9f8583ba2431 | 104 | * Input: error -- 0 = in range, 1 = out of range |
mfwic | 1:9f8583ba2431 | 105 | * Output: none |
mfwic | 1:9f8583ba2431 | 106 | * Description: |
mfwic | 1:9f8583ba2431 | 107 | * Sends a floating point number (value) over the serial port |
mfwic | 1:9f8583ba2431 | 108 | * if it is being retrieved (GET) |
mfwic | 1:9f8583ba2431 | 109 | * |
mfwic | 1:9f8583ba2431 | 110 | **************************************************************/ |
mfwic | 1:9f8583ba2431 | 111 | void showRangeError(int intValue, int iBadValue, float fBadValue) |
mfwic | 1:9f8583ba2431 | 112 | { |
mfwic | 12:fd1fd1857628 | 113 | if (intValue){ |
mfwic | 12:fd1fd1857628 | 114 | sprintf(strbuf, " Out of Range: %d", iBadValue); |
mfwic | 12:fd1fd1857628 | 115 | }else{ |
mfwic | 12:fd1fd1857628 | 116 | sprintf(strbuf, " Out of Range: %0.3f", fBadValue); |
mfwic | 12:fd1fd1857628 | 117 | } |
mfwic | 1:9f8583ba2431 | 118 | |
mfwic | 1:9f8583ba2431 | 119 | sendSerial(strbuf); |
mfwic | 1:9f8583ba2431 | 120 | } |
mfwic | 0:44a3005d4f20 | 121 |