A program for LPC11U68 UART1 testing

Dependencies:   mbed-src

Committer:
jurgis
Date:
Thu Jun 05 19:13:19 2014 +0000
Revision:
0:aa765ec85271
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jurgis 0:aa765ec85271 1 #include "mbed.h"
jurgis 0:aa765ec85271 2
jurgis 0:aa765ec85271 3 DigitalOut greenLed(LED_GREEN);
jurgis 0:aa765ec85271 4 DigitalOut redLed(LED_RED);
jurgis 0:aa765ec85271 5
jurgis 0:aa765ec85271 6 Serial pc(USBTX, USBRX);
jurgis 0:aa765ec85271 7 Serial uart1(P1_8, P1_2);
jurgis 0:aa765ec85271 8
jurgis 0:aa765ec85271 9 uint8_t buffer[256];
jurgis 0:aa765ec85271 10 uint16_t bufferIndex = 0;
jurgis 0:aa765ec85271 11 uint8_t data;
jurgis 0:aa765ec85271 12 uint32_t ledToggleTime;
jurgis 0:aa765ec85271 13
jurgis 0:aa765ec85271 14 int main()
jurgis 0:aa765ec85271 15 {
jurgis 0:aa765ec85271 16 pc.baud(115200);
jurgis 0:aa765ec85271 17 pc.printf("\r\n\r\nProgram started\r\nType some text and press ENTER ...\r\n");
jurgis 0:aa765ec85271 18
jurgis 0:aa765ec85271 19 memset(buffer, 0, sizeof(buffer));
jurgis 0:aa765ec85271 20 redLed = 1;
jurgis 0:aa765ec85271 21 ledToggleTime = 0;
jurgis 0:aa765ec85271 22
jurgis 0:aa765ec85271 23 uart1.baud(115200);
jurgis 0:aa765ec85271 24 pc.printf("Type text > "); // show the prompt
jurgis 0:aa765ec85271 25
jurgis 0:aa765ec85271 26 for(;;)
jurgis 0:aa765ec85271 27 {
jurgis 0:aa765ec85271 28 while (uart1.readable() > 0)
jurgis 0:aa765ec85271 29 {
jurgis 0:aa765ec85271 30 pc.printf("\r\nreadable(): %d\r\n", uart1.readable());
jurgis 0:aa765ec85271 31
jurgis 0:aa765ec85271 32 data = uart1.getc();
jurgis 0:aa765ec85271 33 pc.printf("\r\n<<< [%02X]\r\n", data);
jurgis 0:aa765ec85271 34 }
jurgis 0:aa765ec85271 35
jurgis 0:aa765ec85271 36 while (pc.readable() > 0)
jurgis 0:aa765ec85271 37 {
jurgis 0:aa765ec85271 38 if (bufferIndex < sizeof(buffer))
jurgis 0:aa765ec85271 39 {
jurgis 0:aa765ec85271 40 redLed = 1; // turn off the red led
jurgis 0:aa765ec85271 41 data = pc.getc();
jurgis 0:aa765ec85271 42
jurgis 0:aa765ec85271 43 // wait for ENTER
jurgis 0:aa765ec85271 44 if ((data == 0x0d) || (data == 0x0a))
jurgis 0:aa765ec85271 45 {
jurgis 0:aa765ec85271 46 if (bufferIndex > 0)
jurgis 0:aa765ec85271 47 {
jurgis 0:aa765ec85271 48 pc.printf("\r\nGot it: %s\r\n", buffer);
jurgis 0:aa765ec85271 49
jurgis 0:aa765ec85271 50 pc.printf("\r\n>>> %s\r\n", buffer);
jurgis 0:aa765ec85271 51 uart1.printf("%s", buffer);
jurgis 0:aa765ec85271 52
jurgis 0:aa765ec85271 53 memset(buffer, 0, sizeof(buffer));
jurgis 0:aa765ec85271 54 bufferIndex = 0;
jurgis 0:aa765ec85271 55 }
jurgis 0:aa765ec85271 56
jurgis 0:aa765ec85271 57 pc.printf("\r\nType text > "); // show the prompt
jurgis 0:aa765ec85271 58 }
jurgis 0:aa765ec85271 59 else
jurgis 0:aa765ec85271 60 {
jurgis 0:aa765ec85271 61 buffer[bufferIndex++] = data;
jurgis 0:aa765ec85271 62 pc.putc(data);
jurgis 0:aa765ec85271 63 }
jurgis 0:aa765ec85271 64 }
jurgis 0:aa765ec85271 65 else
jurgis 0:aa765ec85271 66 {
jurgis 0:aa765ec85271 67 redLed = 0; // turn on the red led
jurgis 0:aa765ec85271 68 }
jurgis 0:aa765ec85271 69 }
jurgis 0:aa765ec85271 70
jurgis 0:aa765ec85271 71 // toggle green led every 500 ms
jurgis 0:aa765ec85271 72 if ((us_ticker_read() - ledToggleTime)/1000 >= 500)
jurgis 0:aa765ec85271 73 {
jurgis 0:aa765ec85271 74 ledToggleTime = us_ticker_read();
jurgis 0:aa765ec85271 75 greenLed = !greenLed;
jurgis 0:aa765ec85271 76 }
jurgis 0:aa765ec85271 77 }
jurgis 0:aa765ec85271 78 }