Test of Freescale K64F serial communication. Input from bluetooth module is echoed to PC USB-serial connection.

Dependencies:   mbed

Committer:
Mr_What
Date:
Mon Aug 17 14:20:36 2015 +0000
Revision:
0:c571c0b21eb7
working tester for bluetooth input

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Mr_What 0:c571c0b21eb7 1 #include "mbed.h"
Mr_What 0:c571c0b21eb7 2
Mr_What 0:c571c0b21eb7 3 DigitalOut myled(LED_GREEN);
Mr_What 0:c571c0b21eb7 4 Serial PC(USBTX, USBRX);
Mr_What 0:c571c0b21eb7 5 Serial BT(PTC15, PTC14); // for Bluetooth module header
Mr_What 0:c571c0b21eb7 6
Mr_What 0:c571c0b21eb7 7 #define RINGBUF
Mr_What 0:c571c0b21eb7 8 #ifdef RINGBUF
Mr_What 0:c571c0b21eb7 9 #define CMDBUFLEN 128
Mr_What 0:c571c0b21eb7 10 int cbuf[CMDBUFLEN];
Mr_What 0:c571c0b21eb7 11 static int bufIn=0;
Mr_What 0:c571c0b21eb7 12 static int bufOut=0;
Mr_What 0:c571c0b21eb7 13 void gotChar() {
Mr_What 0:c571c0b21eb7 14 int c = BT.getc();
Mr_What 0:c571c0b21eb7 15 cbuf[bufIn % CMDBUFLEN]=c;
Mr_What 0:c571c0b21eb7 16 bufIn++;
Mr_What 0:c571c0b21eb7 17 // prevent byte count overflow
Mr_What 0:c571c0b21eb7 18 // WARNING: if there is a race and this thread resets counts...
Mr_What 0:c571c0b21eb7 19 // counts could look funny for a bit, but since bufIn
Mr_What 0:c571c0b21eb7 20 // decrements before bufOut, one hopes that the glitch will not be problematic.
Mr_What 0:c571c0b21eb7 21 // On race, bufIn < bufOut, they will think buffer is empty until next time
Mr_What 0:c571c0b21eb7 22 // they check, when count will be OK
Mr_What 0:c571c0b21eb7 23 if (bufIn > 202*CMDBUFLEN) {
Mr_What 0:c571c0b21eb7 24 bufIn -= 200*CMDBUFLEN;
Mr_What 0:c571c0b21eb7 25 bufOut -= 200*CMDBUFLEN;
Mr_What 0:c571c0b21eb7 26 }
Mr_What 0:c571c0b21eb7 27 }
Mr_What 0:c571c0b21eb7 28 #endif
Mr_What 0:c571c0b21eb7 29
Mr_What 0:c571c0b21eb7 30 void heartbeat() {
Mr_What 0:c571c0b21eb7 31 static int i=0;
Mr_What 0:c571c0b21eb7 32 PC.printf("%d \r", i++); // print the value of variable i
Mr_What 0:c571c0b21eb7 33 myled = !myled; // toggle a led
Mr_What 0:c571c0b21eb7 34 }
Mr_What 0:c571c0b21eb7 35
Mr_What 0:c571c0b21eb7 36 int main()
Mr_What 0:c571c0b21eb7 37 {
Mr_What 0:c571c0b21eb7 38 BT.baud(57600); // set to match your bluetooth-serial module
Mr_What 0:c571c0b21eb7 39 PC.baud(115200);
Mr_What 0:c571c0b21eb7 40 //int i = 0;
Mr_What 0:c571c0b21eb7 41 BT.printf("Hello World!\n");
Mr_What 0:c571c0b21eb7 42 PC.puts("Ready\r");
Mr_What 0:c571c0b21eb7 43 #ifdef RINGBUF
Mr_What 0:c571c0b21eb7 44 BT.attach(&gotChar);
Mr_What 0:c571c0b21eb7 45 #endif
Mr_What 0:c571c0b21eb7 46
Mr_What 0:c571c0b21eb7 47 Ticker HeartBeat;
Mr_What 0:c571c0b21eb7 48 HeartBeat.attach(heartbeat,0.8f);
Mr_What 0:c571c0b21eb7 49
Mr_What 0:c571c0b21eb7 50 while (true) {
Mr_What 0:c571c0b21eb7 51 wait(0.5f); // wait a small period of time
Mr_What 0:c571c0b21eb7 52
Mr_What 0:c571c0b21eb7 53 #ifdef RINGBUF
Mr_What 0:c571c0b21eb7 54 int n = bufIn - bufOut;
Mr_What 0:c571c0b21eb7 55 if (n>0) {
Mr_What 0:c571c0b21eb7 56 PC.putc('\n');
Mr_What 0:c571c0b21eb7 57 if ((n > CMDBUFLEN-3) && (n < CMDBUFLEN*180))
Mr_What 0:c571c0b21eb7 58 PC.printf("\r\n\tserial command input overflow warning!\r\n");
Mr_What 0:c571c0b21eb7 59
Mr_What 0:c571c0b21eb7 60 while (bufOut < bufIn) {
Mr_What 0:c571c0b21eb7 61 int c = cbuf[bufOut % CMDBUFLEN];
Mr_What 0:c571c0b21eb7 62 //PC.printf("\r\t%d got %d(%c)\r\n",bufOut++,c,c);
Mr_What 0:c571c0b21eb7 63 if ((c=='\r')||(c=='\n')||(c==0)) PC.putc('\n');
Mr_What 0:c571c0b21eb7 64 else PC.putc(c);
Mr_What 0:c571c0b21eb7 65 bufOut++;
Mr_What 0:c571c0b21eb7 66 }
Mr_What 0:c571c0b21eb7 67 PC.putc('\n');
Mr_What 0:c571c0b21eb7 68 }
Mr_What 0:c571c0b21eb7 69 #else
Mr_What 0:c571c0b21eb7 70 int n = PC.readable();
Mr_What 0:c571c0b21eb7 71 PC.printf("\n\n%d readable ",n);
Mr_What 0:c571c0b21eb7 72 while(n>0) {
Mr_What 0:c571c0b21eb7 73 int c = PC.getc();
Mr_What 0:c571c0b21eb7 74 PC.printf("\n got %d(%c)\n",c,c);
Mr_What 0:c571c0b21eb7 75 n = PC.readable();
Mr_What 0:c571c0b21eb7 76 }
Mr_What 0:c571c0b21eb7 77 #endif
Mr_What 0:c571c0b21eb7 78 }
Mr_What 0:c571c0b21eb7 79 }