Test program using serial in RTOS
Dependencies: EthernetInterface mbed-rtos mbed
main.cpp@0:30562abde493, 2015-09-24 (annotated)
- Committer:
- Cypress
- Date:
- Thu Sep 24 22:24:28 2015 +0000
- Revision:
- 0:30562abde493
Initial commit, broken.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Cypress | 0:30562abde493 | 1 | #include "main.h" |
Cypress | 0:30562abde493 | 2 | |
Cypress | 0:30562abde493 | 3 | char rxBuffer[BUF_LEN]; |
Cypress | 0:30562abde493 | 4 | int rxChars; |
Cypress | 0:30562abde493 | 5 | bool rxDone; |
Cypress | 0:30562abde493 | 6 | long rxTime; |
Cypress | 0:30562abde493 | 7 | RtosTimer *rxTimer; |
Cypress | 0:30562abde493 | 8 | |
Cypress | 0:30562abde493 | 9 | char txBuffer[BUF_LEN]; |
Cypress | 0:30562abde493 | 10 | int txChars; |
Cypress | 0:30562abde493 | 11 | |
Cypress | 0:30562abde493 | 12 | Serial sp(SP_RX, SP_TX); |
Cypress | 0:30562abde493 | 13 | EthernetInterface eth; |
Cypress | 0:30562abde493 | 14 | TCPSocketServer serv; |
Cypress | 0:30562abde493 | 15 | TCPSocketConnection client; |
Cypress | 0:30562abde493 | 16 | |
Cypress | 0:30562abde493 | 17 | int main ( void ) |
Cypress | 0:30562abde493 | 18 | { |
Cypress | 0:30562abde493 | 19 | // Initalize serial port |
Cypress | 0:30562abde493 | 20 | sp.baud(SP_BAUD); |
Cypress | 0:30562abde493 | 21 | sp.attach(&rxInt); |
Cypress | 0:30562abde493 | 22 | rxTimer = new RtosTimer(&timerTask, osTimerPeriodic, NULL); |
Cypress | 0:30562abde493 | 23 | rxTimer->start(SP_TIMER_PERIOD); |
Cypress | 0:30562abde493 | 24 | |
Cypress | 0:30562abde493 | 25 | // Initalize server |
Cypress | 0:30562abde493 | 26 | serv.bind(SERV_PORT); |
Cypress | 0:30562abde493 | 27 | serv.listen(); |
Cypress | 0:30562abde493 | 28 | while (true) |
Cypress | 0:30562abde493 | 29 | { |
Cypress | 0:30562abde493 | 30 | serv.accept(client); |
Cypress | 0:30562abde493 | 31 | client.set_blocking(false, 1500); |
Cypress | 0:30562abde493 | 32 | |
Cypress | 0:30562abde493 | 33 | while (client.is_connected()) |
Cypress | 0:30562abde493 | 34 | { |
Cypress | 0:30562abde493 | 35 | txChars = client.receive(txBuffer, BUF_LEN); |
Cypress | 0:30562abde493 | 36 | if (txChars <= 0) break; |
Cypress | 0:30562abde493 | 37 | |
Cypress | 0:30562abde493 | 38 | sendBuffer(txBuffer, txChars); |
Cypress | 0:30562abde493 | 39 | } |
Cypress | 0:30562abde493 | 40 | client.close(); |
Cypress | 0:30562abde493 | 41 | } |
Cypress | 0:30562abde493 | 42 | } |
Cypress | 0:30562abde493 | 43 | |
Cypress | 0:30562abde493 | 44 | void timerTask ( void const *arg ) |
Cypress | 0:30562abde493 | 45 | { |
Cypress | 0:30562abde493 | 46 | if (!rxDone) |
Cypress | 0:30562abde493 | 47 | { |
Cypress | 0:30562abde493 | 48 | if (client.is_connected()) |
Cypress | 0:30562abde493 | 49 | { |
Cypress | 0:30562abde493 | 50 | client.send_all(rxBuffer, rxChars); |
Cypress | 0:30562abde493 | 51 | resetRX(); |
Cypress | 0:30562abde493 | 52 | } |
Cypress | 0:30562abde493 | 53 | } |
Cypress | 0:30562abde493 | 54 | else |
Cypress | 0:30562abde493 | 55 | { |
Cypress | 0:30562abde493 | 56 | if (rxChars > 0) |
Cypress | 0:30562abde493 | 57 | { |
Cypress | 0:30562abde493 | 58 | if (rxTime++ >= SP_TIMEOUT) rxDone = true; |
Cypress | 0:30562abde493 | 59 | } |
Cypress | 0:30562abde493 | 60 | } |
Cypress | 0:30562abde493 | 61 | } |
Cypress | 0:30562abde493 | 62 | |
Cypress | 0:30562abde493 | 63 | void rxInt ( void ) |
Cypress | 0:30562abde493 | 64 | { |
Cypress | 0:30562abde493 | 65 | if (!rxDone) |
Cypress | 0:30562abde493 | 66 | { |
Cypress | 0:30562abde493 | 67 | if (rxChars < BUF_LEN) |
Cypress | 0:30562abde493 | 68 | { |
Cypress | 0:30562abde493 | 69 | rxBuffer[rxChars++] = SP_UART->D; |
Cypress | 0:30562abde493 | 70 | rxTime = 0; |
Cypress | 0:30562abde493 | 71 | } |
Cypress | 0:30562abde493 | 72 | else rxDone = true; |
Cypress | 0:30562abde493 | 73 | } |
Cypress | 0:30562abde493 | 74 | } |
Cypress | 0:30562abde493 | 75 | |
Cypress | 0:30562abde493 | 76 | void sendBuffer (char *buffer, int len) |
Cypress | 0:30562abde493 | 77 | { |
Cypress | 0:30562abde493 | 78 | for (int i = 0; i < len; i++) |
Cypress | 0:30562abde493 | 79 | { |
Cypress | 0:30562abde493 | 80 | sp.putc(buffer[i]); |
Cypress | 0:30562abde493 | 81 | } |
Cypress | 0:30562abde493 | 82 | } |
Cypress | 0:30562abde493 | 83 | |
Cypress | 0:30562abde493 | 84 | void clearBuffer ( char *buffer, int len ) |
Cypress | 0:30562abde493 | 85 | { |
Cypress | 0:30562abde493 | 86 | for (int i = 0; i < len; i++) buffer[i] = 0; |
Cypress | 0:30562abde493 | 87 | } |
Cypress | 0:30562abde493 | 88 | |
Cypress | 0:30562abde493 | 89 | void resetRX ( void ) |
Cypress | 0:30562abde493 | 90 | { |
Cypress | 0:30562abde493 | 91 | clearBuffer(rxBuffer, BUF_LEN); |
Cypress | 0:30562abde493 | 92 | rxChars = 0; |
Cypress | 0:30562abde493 | 93 | rxDone = false; |
Cypress | 0:30562abde493 | 94 | } |
Cypress | 0:30562abde493 | 95 | |
Cypress | 0:30562abde493 | 96 | void resetTX ( void ) |
Cypress | 0:30562abde493 | 97 | { |
Cypress | 0:30562abde493 | 98 | clearBuffer(txBuffer, BUF_LEN); |
Cypress | 0:30562abde493 | 99 | txChars = 0; |
Cypress | 0:30562abde493 | 100 | } |