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

Dependencies:   mbed

.

Committer:
jont
Date:
Fri Feb 06 09:46:44 2015 +0000
Revision:
4:107d2d3294da
Parent:
3:6a35fb789679
Tweaked for GU Projects

Who changed what in which revision?

UserRevisionLine numberNew 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 4:107d2d3294da 21 //(c)jont@ninelocks.com 1992-2015
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 3:6a35fb789679 65 theChar = pc.getc();
jont 4:107d2d3294da 66 pc.putc(theChar);
jont 4:107d2d3294da 67
jont 0:87e65dabdb95 68 /* if an Xoff was received from
jont 0:87e65dabdb95 69 remote term telling us to stop...
jont 0:87e65dabdb95 70 in which case if anything other than an xon is receievd
jont 0:87e65dabdb95 71 we'll get caught as cant send due to xoff and cant get an irq
jont 0:87e65dabdb95 72 as were already in one */
jont 0:87e65dabdb95 73
jont 0:87e65dabdb95 74 switch(theChar)
jont 0:87e65dabdb95 75 {
jont 0:87e65dabdb95 76 case (XOFF):
jont 0:87e65dabdb95 77 JPORTOK0 = 0xff; ;break; /* busy */
jont 0:87e65dabdb95 78 case (XON):
jont 0:87e65dabdb95 79 JPORTOK0 = 0x00; ;break; /* not busy */
jont 0:87e65dabdb95 80 case ('?'):
jont 0:87e65dabdb95 81 comBuffIndex = 0; comBuff[0] ='\0';
jont 0:87e65dabdb95 82 // printf("\n\r>?"); break; /* start of enq com */
jont 0:87e65dabdb95 83 default: break;
jont 0:87e65dabdb95 84 }
jont 0:87e65dabdb95 85
jont 0:87e65dabdb95 86 if (theChar != XON && theChar != XOFF && theChar != '?')
jont 0:87e65dabdb95 87 {
jont 0:87e65dabdb95 88 if (comBuffIndex >= combuffsize - 1)
jont 0:87e65dabdb95 89 {
jont 0:87e65dabdb95 90 comBuffIndex = combuffsize - 1;
jont 0:87e65dabdb95 91 }
jont 0:87e65dabdb95 92
jont 0:87e65dabdb95 93 /* jt need to force upper case ?? also add set functions */
jont 0:87e65dabdb95 94 comBuff[comBuffIndex] = toupper(theChar);
jont 0:87e65dabdb95 95 if (theChar == 0x0d)
jont 0:87e65dabdb95 96 {
jont 0:87e65dabdb95 97 comBuff[comBuffIndex] = '\0'; /* null terminate it */
jont 0:87e65dabdb95 98 }
jont 0:87e65dabdb95 99 comBuffIndex++; /* inc index for next time */
jont 0:87e65dabdb95 100
jont 0:87e65dabdb95 101 /* we use to check for 2 chhars as well */
jont 0:87e65dabdb95 102 if (theChar == 0x0d )
jont 0:87e65dabdb95 103 {
jont 0:87e65dabdb95 104 commandWaiting = true; /* set flag */
jont 0:87e65dabdb95 105 comBuffIndex = 0;
jont 0:87e65dabdb95 106 }
jont 0:87e65dabdb95 107
jont 0:87e65dabdb95 108 }
jont 0:87e65dabdb95 109
jont 0:87e65dabdb95 110 }
jont 0:87e65dabdb95 111
jont 0:87e65dabdb95 112 ///should normally be called by an irq that notices incoming characters
jont 0:87e65dabdb95 113 /*======================================================================*/
jont 0:87e65dabdb95 114 /* sioRXHandler */
jont 0:87e65dabdb95 115 /*======================================================================*/
jont 0:87e65dabdb95 116 //used to work great for one character recived, but not with mbed, so lets see if its
jont 0:87e65dabdb95 117 //got multi characyters before it call the irq
jont 0:87e65dabdb95 118 void sioRXHandler(void)
jont 0:87e65dabdb95 119 {
jont 0:87e65dabdb95 120
jont 0:87e65dabdb95 121 //see http://mbed.org/forum/mbed/topic/3414/?page=1#comment-17231
jont 4:107d2d3294da 122 while (pc.readable()){
jont 4:107d2d3294da 123
jont 0:87e65dabdb95 124 sioGetSingle();
jont 4:107d2d3294da 125
jont 0:87e65dabdb95 126
jont 0:87e65dabdb95 127 }
jont 0:87e65dabdb95 128 //http://mbed.org/forum/mbed/topic/408/?page=1#comment-2221
jont 0:87e65dabdb95 129 // pc.putc(0x00);
jont 0:87e65dabdb95 130
jont 4:107d2d3294da 131 // uint32_t UART_0_RBR = LPC_UART0->RBR;
jont 0:87e65dabdb95 132
jont 0:87e65dabdb95 133 }