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@1:bc3509459a27, 2022-12-06 (annotated)
- Committer:
- Slord2142
- Date:
- Tue Dec 06 18:47:01 2022 +0000
- Revision:
- 1:bc3509459a27
- Parent:
- 0:b3410a1e9843
Helix commit 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Slord2142 | 0:b3410a1e9843 | 1 | #include "mbed.h" |
Slord2142 | 0:b3410a1e9843 | 2 | #include "serial.h" |
Slord2142 | 0:b3410a1e9843 | 3 | #include "stdio.h" |
Slord2142 | 0:b3410a1e9843 | 4 | #include "string.h" |
Slord2142 | 0:b3410a1e9843 | 5 | #include "globals.h" |
Slord2142 | 0:b3410a1e9843 | 6 | #include "all_io.h" |
Slord2142 | 0:b3410a1e9843 | 7 | |
Slord2142 | 0:b3410a1e9843 | 8 | /************* GLOBAL VARIABLES *****************************/ |
Slord2142 | 0:b3410a1e9843 | 9 | struct serialStatusBits serialStatus; |
Slord2142 | 0:b3410a1e9843 | 10 | char strbuf[150] = {0}; |
Slord2142 | 0:b3410a1e9843 | 11 | char rxbuf[MAXRXBUF+1] = {0}; |
Slord2142 | 0:b3410a1e9843 | 12 | volatile unsigned int bufloc = 0; |
Slord2142 | 0:b3410a1e9843 | 13 | |
Slord2142 | 0:b3410a1e9843 | 14 | /************* FORWARD DECLARATIONS *************************/ |
Slord2142 | 0:b3410a1e9843 | 15 | void Rx_IRQHandler(void); |
Slord2142 | 0:b3410a1e9843 | 16 | |
Slord2142 | 0:b3410a1e9843 | 17 | |
Slord2142 | 0:b3410a1e9843 | 18 | // set up serial port for 115200 baud |
Slord2142 | 1:bc3509459a27 | 19 | Serial dcm1(USBTX, USBRX, 115200); |
Slord2142 | 0:b3410a1e9843 | 20 | //Serial dcm1(USBTX, USBRX, 921600); |
Slord2142 | 1:bc3509459a27 | 21 | //Serial dcm1(PA_2, PA_3, 921600); |
Slord2142 | 0:b3410a1e9843 | 22 | |
Slord2142 | 0:b3410a1e9843 | 23 | |
Slord2142 | 0:b3410a1e9843 | 24 | // initialize the serial port variables |
Slord2142 | 0:b3410a1e9843 | 25 | void initSerial(void) |
Slord2142 | 0:b3410a1e9843 | 26 | { |
Slord2142 | 0:b3410a1e9843 | 27 | // create the RxIRQ callback |
Slord2142 | 0:b3410a1e9843 | 28 | dcm1.attach(&Rx_IRQHandler, Serial::RxIrq); |
Slord2142 | 0:b3410a1e9843 | 29 | |
Slord2142 | 0:b3410a1e9843 | 30 | } |
Slord2142 | 0:b3410a1e9843 | 31 | |
Slord2142 | 0:b3410a1e9843 | 32 | |
Slord2142 | 0:b3410a1e9843 | 33 | // this is the interrupt request handler (IRQ) for ALL Recoev interrupts |
Slord2142 | 0:b3410a1e9843 | 34 | void Rx_IRQHandler(void) |
Slord2142 | 0:b3410a1e9843 | 35 | { |
Slord2142 | 0:b3410a1e9843 | 36 | // reinit the repeat flag |
Slord2142 | 0:b3410a1e9843 | 37 | serialStatus.repeat = FALSE; |
Slord2142 | 0:b3410a1e9843 | 38 | |
Slord2142 | 0:b3410a1e9843 | 39 | // check if the USART1 receive interrupt flag was set |
Slord2142 | 0:b3410a1e9843 | 40 | while (dcm1.readable()) |
Slord2142 | 0:b3410a1e9843 | 41 | { |
Slord2142 | 0:b3410a1e9843 | 42 | //led_usb = 0; |
Slord2142 | 0:b3410a1e9843 | 43 | rxbuf[bufloc] = dcm1.getc(); |
Slord2142 | 0:b3410a1e9843 | 44 | |
Slord2142 | 0:b3410a1e9843 | 45 | if (rxbuf[bufloc] == CR) // check for end of command |
Slord2142 | 0:b3410a1e9843 | 46 | { |
Slord2142 | 0:b3410a1e9843 | 47 | serialStatus.command = TRUE; |
Slord2142 | 0:b3410a1e9843 | 48 | rxbuf[bufloc] = 0; |
Slord2142 | 0:b3410a1e9843 | 49 | bufloc = 0; |
Slord2142 | 0:b3410a1e9843 | 50 | return; |
Slord2142 | 0:b3410a1e9843 | 51 | } |
Slord2142 | 0:b3410a1e9843 | 52 | else if (rxbuf[bufloc] == '$') // check for computer command |
Slord2142 | 0:b3410a1e9843 | 53 | { |
Slord2142 | 0:b3410a1e9843 | 54 | serialStatus.computer = TRUE; |
Slord2142 | 0:b3410a1e9843 | 55 | bufloc = 0; // reset the buffer if computer command |
Slord2142 | 0:b3410a1e9843 | 56 | } |
Slord2142 | 0:b3410a1e9843 | 57 | else if (rxbuf[bufloc] == ')') // check for repeat command |
Slord2142 | 0:b3410a1e9843 | 58 | { |
Slord2142 | 0:b3410a1e9843 | 59 | serialStatus.repeat = TRUE; |
Slord2142 | 0:b3410a1e9843 | 60 | rxbuf[bufloc] = 0; |
Slord2142 | 0:b3410a1e9843 | 61 | } |
Slord2142 | 0:b3410a1e9843 | 62 | |
Slord2142 | 0:b3410a1e9843 | 63 | // Echo character back out the serial port |
Slord2142 | 0:b3410a1e9843 | 64 | if (!serialStatus.computer) |
Slord2142 | 0:b3410a1e9843 | 65 | { |
Slord2142 | 0:b3410a1e9843 | 66 | dcm1.putc((unsigned char)rxbuf[bufloc]); |
Slord2142 | 0:b3410a1e9843 | 67 | } |
Slord2142 | 0:b3410a1e9843 | 68 | |
Slord2142 | 0:b3410a1e9843 | 69 | // handle backspace |
Slord2142 | 0:b3410a1e9843 | 70 | if (rxbuf[bufloc] != 0x08) |
Slord2142 | 0:b3410a1e9843 | 71 | { |
Slord2142 | 0:b3410a1e9843 | 72 | bufloc++; // increment the location |
Slord2142 | 0:b3410a1e9843 | 73 | } |
Slord2142 | 0:b3410a1e9843 | 74 | else |
Slord2142 | 0:b3410a1e9843 | 75 | { |
Slord2142 | 0:b3410a1e9843 | 76 | if (bufloc > 0) |
Slord2142 | 0:b3410a1e9843 | 77 | { |
Slord2142 | 0:b3410a1e9843 | 78 | rxbuf[--bufloc] = 0; // set to null |
Slord2142 | 0:b3410a1e9843 | 79 | } |
Slord2142 | 0:b3410a1e9843 | 80 | else |
Slord2142 | 0:b3410a1e9843 | 81 | { |
Slord2142 | 0:b3410a1e9843 | 82 | rxbuf[bufloc] = 0; // set to null |
Slord2142 | 0:b3410a1e9843 | 83 | } |
Slord2142 | 0:b3410a1e9843 | 84 | } |
Slord2142 | 0:b3410a1e9843 | 85 | |
Slord2142 | 0:b3410a1e9843 | 86 | // check for overflow |
Slord2142 | 0:b3410a1e9843 | 87 | if (bufloc >= MAXRXBUF) |
Slord2142 | 0:b3410a1e9843 | 88 | { |
Slord2142 | 0:b3410a1e9843 | 89 | bufloc = 0; |
Slord2142 | 0:b3410a1e9843 | 90 | rxbuf[0] = 0; |
Slord2142 | 0:b3410a1e9843 | 91 | } |
Slord2142 | 0:b3410a1e9843 | 92 | |
Slord2142 | 0:b3410a1e9843 | 93 | //led_usb = 1; |
Slord2142 | 0:b3410a1e9843 | 94 | } |
Slord2142 | 0:b3410a1e9843 | 95 | } |
Slord2142 | 0:b3410a1e9843 | 96 | |
Slord2142 | 0:b3410a1e9843 | 97 | |
Slord2142 | 0:b3410a1e9843 | 98 | // sends carriage return and linefeed and prompt character |
Slord2142 | 0:b3410a1e9843 | 99 | /* |
Slord2142 | 0:b3410a1e9843 | 100 | void sendCRLF(void) |
Slord2142 | 0:b3410a1e9843 | 101 | { |
Slord2142 | 0:b3410a1e9843 | 102 | char strMenu[30] ={0}; |
Slord2142 | 0:b3410a1e9843 | 103 | |
Slord2142 | 0:b3410a1e9843 | 104 | menu_type = MENU_DCM1; |
Slord2142 | 0:b3410a1e9843 | 105 | |
Slord2142 | 0:b3410a1e9843 | 106 | switch(menuType) |
Slord2142 | 0:b3410a1e9843 | 107 | { |
Slord2142 | 0:b3410a1e9843 | 108 | case MENU_DCM1: strcpy(strMenu,"DCM1"); break; |
Slord2142 | 0:b3410a1e9843 | 109 | case MENU_DIFFERENTIAL: strcpy(strMenu,"DIFF"); break; |
Slord2142 | 0:b3410a1e9843 | 110 | case MENU_SINGLE: strcpy(strMenu,"SINGLE"); break; |
Slord2142 | 0:b3410a1e9843 | 111 | case MENU_DUAL: strcpy(strMenu,"DUAL"); break; |
Slord2142 | 0:b3410a1e9843 | 112 | case MENU_INVERT: strcpy(strMenu,"INVERT"); break; |
Slord2142 | 0:b3410a1e9843 | 113 | case MENU_SETTINGS: strcpy(strMenu,"SET"); break; |
Slord2142 | 0:b3410a1e9843 | 114 | case MENU_SETTINGS_CHAN1: strcpy(strMenu,"SET CHAN1"); break; |
Slord2142 | 0:b3410a1e9843 | 115 | case MENU_SETTINGS_CHAN2: strcpy(strMenu,"SET CHAN2"); break; |
Slord2142 | 0:b3410a1e9843 | 116 | case MENU_SETTINGS_CLOCK: strcpy(strMenu,"SET CLOCK"); break; |
Slord2142 | 0:b3410a1e9843 | 117 | case MENU_CALIBRATE: strcpy(strMenu,"CAL"); break; |
Slord2142 | 0:b3410a1e9843 | 118 | case MENU_TEST: strcpy(strMenu,"TEST"); break; |
Slord2142 | 0:b3410a1e9843 | 119 | case MENU_MAIN: strcpy(strMenu,"MAIN"); break; |
Slord2142 | 0:b3410a1e9843 | 120 | default: strcpy(strMenu,"UNDF"); break; |
Slord2142 | 0:b3410a1e9843 | 121 | } |
Slord2142 | 0:b3410a1e9843 | 122 | |
Slord2142 | 0:b3410a1e9843 | 123 | // append the CR,LF and the ready character |
Slord2142 | 0:b3410a1e9843 | 124 | if(serialStatus.computer) |
Slord2142 | 0:b3410a1e9843 | 125 | { |
Slord2142 | 0:b3410a1e9843 | 126 | sendSerial("\r"); |
Slord2142 | 0:b3410a1e9843 | 127 | } |
Slord2142 | 0:b3410a1e9843 | 128 | else |
Slord2142 | 0:b3410a1e9843 | 129 | { |
Slord2142 | 0:b3410a1e9843 | 130 | // adds a carrot so you know you are talking in terminal |
Slord2142 | 0:b3410a1e9843 | 131 | sprintf(strbuf,"\n\r%s>",strMenu); |
Slord2142 | 0:b3410a1e9843 | 132 | sendSerial(strbuf); |
Slord2142 | 0:b3410a1e9843 | 133 | // sendSerial("\n\r>"); |
Slord2142 | 0:b3410a1e9843 | 134 | } |
Slord2142 | 0:b3410a1e9843 | 135 | }*/ |
Slord2142 | 0:b3410a1e9843 | 136 | |
Slord2142 | 0:b3410a1e9843 | 137 | // send buffer to the serial port |
Slord2142 | 0:b3410a1e9843 | 138 | void sendSerial(char *buf) |
Slord2142 | 0:b3410a1e9843 | 139 | { |
Slord2142 | 0:b3410a1e9843 | 140 | //led_usb = 0; |
Slord2142 | 0:b3410a1e9843 | 141 | dcm1.printf(buf); |
Slord2142 | 0:b3410a1e9843 | 142 | //led_usb = 1; |
Slord2142 | 0:b3410a1e9843 | 143 | } |
Slord2142 | 0:b3410a1e9843 | 144 | |
Slord2142 | 0:b3410a1e9843 | 145 | /************************************************************ |
Slord2142 | 0:b3410a1e9843 | 146 | * Routine: showRangeError |
Slord2142 | 0:b3410a1e9843 | 147 | * Input: error -- 0 = in range, 1 = out of range |
Slord2142 | 0:b3410a1e9843 | 148 | * Output: none |
Slord2142 | 0:b3410a1e9843 | 149 | * Description: |
Slord2142 | 0:b3410a1e9843 | 150 | * Sends a floating point number (value) over the serial port |
Slord2142 | 0:b3410a1e9843 | 151 | * if it is being retrieved (GET) |
Slord2142 | 0:b3410a1e9843 | 152 | * |
Slord2142 | 0:b3410a1e9843 | 153 | **************************************************************/ |
Slord2142 | 0:b3410a1e9843 | 154 | void showRangeError(int intValue, int iBadValue, float fBadValue) |
Slord2142 | 0:b3410a1e9843 | 155 | { |
Slord2142 | 0:b3410a1e9843 | 156 | if (intValue){ |
Slord2142 | 0:b3410a1e9843 | 157 | sprintf(strbuf, " Out of Range: %d", iBadValue); |
Slord2142 | 0:b3410a1e9843 | 158 | }else{ |
Slord2142 | 0:b3410a1e9843 | 159 | sprintf(strbuf, " Out of Range: %0.3f", fBadValue); |
Slord2142 | 0:b3410a1e9843 | 160 | } |
Slord2142 | 0:b3410a1e9843 | 161 | |
Slord2142 | 0:b3410a1e9843 | 162 | sendSerial(strbuf); |
Slord2142 | 0:b3410a1e9843 | 163 | } |
Slord2142 | 0:b3410a1e9843 | 164 |