When debugging code it can be handy to examine/alter variables and to check the state of input lines. So your main program can run and you have access to alter variables and run functions from a terminal. In this sample the main program is just a loop that flashes an LED. In that loop a periodic call is made to ShellTC which handles any commands from the serial terminal. The code is a bit quirky(it was originally written for a very resource limited device) but it works well enough to be useful, hence its published. More details in the main.cpp
.
sio.cpp@1:329a8125838a, 2012-10-26 (annotated)
- Committer:
- jont
- Date:
- Fri Oct 26 04:51:30 2012 +0000
- Revision:
- 1:329a8125838a
- Parent:
- 0:87e65dabdb95
- Child:
- 3:6a35fb789679
Increased command buffer size to allow room for unix size.;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jont | 0:87e65dabdb95 | 1 | /* Copyright (c) <year> <copyright holders>, MIT License |
jont | 0:87e65dabdb95 | 2 | * |
jont | 0:87e65dabdb95 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
jont | 0:87e65dabdb95 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
jont | 0:87e65dabdb95 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
jont | 0:87e65dabdb95 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
jont | 0:87e65dabdb95 | 7 | * furnished to do so, subject to the following conditions: |
jont | 0:87e65dabdb95 | 8 | * |
jont | 0:87e65dabdb95 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
jont | 0:87e65dabdb95 | 10 | * substantial portions of the Software. |
jont | 0:87e65dabdb95 | 11 | * |
jont | 0:87e65dabdb95 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
jont | 0:87e65dabdb95 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
jont | 0:87e65dabdb95 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
jont | 0:87e65dabdb95 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
jont | 0:87e65dabdb95 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
jont | 0:87e65dabdb95 | 17 | */ |
jont | 0:87e65dabdb95 | 18 | /* |
jont | 0:87e65dabdb95 | 19 | sio handler august 96 |
jont | 0:87e65dabdb95 | 20 | */ |
jont | 0:87e65dabdb95 | 21 | //(c)jont@ninelocks.com 1992-2012 |
jont | 0:87e65dabdb95 | 22 | #include "string.h" |
jont | 0:87e65dabdb95 | 23 | #include <stdio.h> |
jont | 0:87e65dabdb95 | 24 | #include <ctype.h> |
jont | 0:87e65dabdb95 | 25 | |
jont | 0:87e65dabdb95 | 26 | #include "mbed.h" //fiddle |
jont | 0:87e65dabdb95 | 27 | |
jont | 0:87e65dabdb95 | 28 | #define XON 0x11 |
jont | 0:87e65dabdb95 | 29 | #define XOFF 0x13 |
jont | 0:87e65dabdb95 | 30 | |
jont | 0:87e65dabdb95 | 31 | |
jont | 0:87e65dabdb95 | 32 | //why do we end up defineing true and false |
jont | 0:87e65dabdb95 | 33 | |
jont | 1:329a8125838a | 34 | #define combuffsize 32 |
jont | 0:87e65dabdb95 | 35 | void sioRXHandler(void); |
jont | 0:87e65dabdb95 | 36 | //horrible as this is platform depdndent but for now |
jont | 0:87e65dabdb95 | 37 | Serial pc(USBTX, USBRX); // tx, rx |
jont | 0:87e65dabdb95 | 38 | |
jont | 0:87e65dabdb95 | 39 | |
jont | 0:87e65dabdb95 | 40 | int commandWaiting; /* flag to indicate if data in buffer */ |
jont | 0:87e65dabdb95 | 41 | char comBuff[combuffsize]; /* space for commands yeh its small make bigger if you need to */ |
jont | 0:87e65dabdb95 | 42 | int comBuffIndex; /* buffer index */ |
jont | 0:87e65dabdb95 | 43 | unsigned char JPORTOK0; |
jont | 0:87e65dabdb95 | 44 | |
jont | 0:87e65dabdb95 | 45 | void serialInit(void) |
jont | 0:87e65dabdb95 | 46 | { |
jont | 0:87e65dabdb95 | 47 | JPORTOK0 = 0; |
jont | 0:87e65dabdb95 | 48 | pc.attach(&sioRXHandler); |
jont | 0:87e65dabdb95 | 49 | // pc.printf("Hello World!"); |
jont | 0:87e65dabdb95 | 50 | |
jont | 0:87e65dabdb95 | 51 | } |
jont | 0:87e65dabdb95 | 52 | |
jont | 0:87e65dabdb95 | 53 | /*======================================================================*/ |
jont | 0:87e65dabdb95 | 54 | /* Get a character into our buffer */ |
jont | 0:87e65dabdb95 | 55 | /*======================================================================*/ |
jont | 0:87e65dabdb95 | 56 | /* read single character into command buffer */ |
jont | 0:87e65dabdb95 | 57 | void sioGetSingle(void) |
jont | 0:87e65dabdb95 | 58 | { |
jont | 0:87e65dabdb95 | 59 | |
jont | 0:87e65dabdb95 | 60 | |
jont | 0:87e65dabdb95 | 61 | |
jont | 0:87e65dabdb95 | 62 | //char theChar; |
jont | 0:87e65dabdb95 | 63 | char theChar = 0x00; //used to be a char... |
jont | 0:87e65dabdb95 | 64 | //h8 theChar = SIO_RDR; /* get received character */ |
jont | 0:87e65dabdb95 | 65 | //theChar = pc.getc(); |
jont | 0:87e65dabdb95 | 66 | theChar = LPC_UART0->RBR; //oct2012 expereiment |
jont | 0:87e65dabdb95 | 67 | // |
jont | 0:87e65dabdb95 | 68 | //see this for reg names http://mbed.org/users/4180_1/notebook/cc-io-register-names/ |
jont | 0:87e65dabdb95 | 69 | do { |
jont | 0:87e65dabdb95 | 70 | } while ((LPC_UART0->LSR & 0x20)==0) ; |
jont | 0:87e65dabdb95 | 71 | //write character to UART |
jont | 0:87e65dabdb95 | 72 | LPC_UART0->THR = theChar ; |
jont | 0:87e65dabdb95 | 73 | |
jont | 0:87e65dabdb95 | 74 | //theChar = RCREG; //PIC |
jont | 0:87e65dabdb95 | 75 | // theChar = 0x42; //TODO2k12 this needs to be whats read from sio RXREG |
jont | 0:87e65dabdb95 | 76 | /* if an Xoff was received from |
jont | 0:87e65dabdb95 | 77 | remote term telling us to stop... |
jont | 0:87e65dabdb95 | 78 | in which case if anything other than an xon is receievd |
jont | 0:87e65dabdb95 | 79 | we'll get caught as cant send due to xoff and cant get an irq |
jont | 0:87e65dabdb95 | 80 | as were already in one */ |
jont | 0:87e65dabdb95 | 81 | |
jont | 0:87e65dabdb95 | 82 | switch(theChar) |
jont | 0:87e65dabdb95 | 83 | { |
jont | 0:87e65dabdb95 | 84 | case (XOFF): |
jont | 0:87e65dabdb95 | 85 | JPORTOK0 = 0xff; ;break; /* busy */ |
jont | 0:87e65dabdb95 | 86 | case (XON): |
jont | 0:87e65dabdb95 | 87 | JPORTOK0 = 0x00; ;break; /* not busy */ |
jont | 0:87e65dabdb95 | 88 | case ('?'): |
jont | 0:87e65dabdb95 | 89 | comBuffIndex = 0; comBuff[0] ='\0'; |
jont | 0:87e65dabdb95 | 90 | // printf("\n\r>?"); break; /* start of enq com */ |
jont | 0:87e65dabdb95 | 91 | default: break; |
jont | 0:87e65dabdb95 | 92 | } |
jont | 0:87e65dabdb95 | 93 | |
jont | 0:87e65dabdb95 | 94 | if (theChar != XON && theChar != XOFF && theChar != '?') |
jont | 0:87e65dabdb95 | 95 | { |
jont | 0:87e65dabdb95 | 96 | if (comBuffIndex >= combuffsize - 1) |
jont | 0:87e65dabdb95 | 97 | { |
jont | 0:87e65dabdb95 | 98 | comBuffIndex = combuffsize - 1; |
jont | 0:87e65dabdb95 | 99 | } |
jont | 0:87e65dabdb95 | 100 | |
jont | 0:87e65dabdb95 | 101 | /* jt need to force upper case ?? also add set functions */ |
jont | 0:87e65dabdb95 | 102 | comBuff[comBuffIndex] = toupper(theChar); |
jont | 0:87e65dabdb95 | 103 | if (theChar == 0x0d) |
jont | 0:87e65dabdb95 | 104 | { |
jont | 0:87e65dabdb95 | 105 | comBuff[comBuffIndex] = '\0'; /* null terminate it */ |
jont | 0:87e65dabdb95 | 106 | } |
jont | 0:87e65dabdb95 | 107 | comBuffIndex++; /* inc index for next time */ |
jont | 0:87e65dabdb95 | 108 | |
jont | 0:87e65dabdb95 | 109 | /* we use to check for 2 chhars as well */ |
jont | 0:87e65dabdb95 | 110 | if (theChar == 0x0d ) |
jont | 0:87e65dabdb95 | 111 | { |
jont | 0:87e65dabdb95 | 112 | commandWaiting = true; /* set flag */ |
jont | 0:87e65dabdb95 | 113 | comBuffIndex = 0; |
jont | 0:87e65dabdb95 | 114 | } |
jont | 0:87e65dabdb95 | 115 | |
jont | 0:87e65dabdb95 | 116 | } |
jont | 0:87e65dabdb95 | 117 | |
jont | 0:87e65dabdb95 | 118 | } |
jont | 0:87e65dabdb95 | 119 | |
jont | 0:87e65dabdb95 | 120 | ///should normally be called by an irq that notices incoming characters |
jont | 0:87e65dabdb95 | 121 | /*======================================================================*/ |
jont | 0:87e65dabdb95 | 122 | /* sioRXHandler */ |
jont | 0:87e65dabdb95 | 123 | /*======================================================================*/ |
jont | 0:87e65dabdb95 | 124 | //used to work great for one character recived, but not with mbed, so lets see if its |
jont | 0:87e65dabdb95 | 125 | //got multi characyters before it call the irq |
jont | 0:87e65dabdb95 | 126 | void sioRXHandler(void) |
jont | 0:87e65dabdb95 | 127 | { |
jont | 0:87e65dabdb95 | 128 | |
jont | 0:87e65dabdb95 | 129 | //see http://mbed.org/forum/mbed/topic/3414/?page=1#comment-17231 |
jont | 0:87e65dabdb95 | 130 | while (pc.readable() == 1){ |
jont | 0:87e65dabdb95 | 131 | // jtdebug(); |
jont | 0:87e65dabdb95 | 132 | sioGetSingle(); |
jont | 0:87e65dabdb95 | 133 | uint32_t UART_0_RBR = LPC_UART0->RBR; |
jont | 0:87e65dabdb95 | 134 | |
jont | 0:87e65dabdb95 | 135 | } |
jont | 0:87e65dabdb95 | 136 | //http://mbed.org/forum/mbed/topic/408/?page=1#comment-2221 |
jont | 0:87e65dabdb95 | 137 | // pc.putc(0x00); |
jont | 0:87e65dabdb95 | 138 | |
jont | 0:87e65dabdb95 | 139 | uint32_t UART_0_RBR = LPC_UART0->RBR; |
jont | 0:87e65dabdb95 | 140 | |
jont | 0:87e65dabdb95 | 141 | } |