Test program using serial in RTOS
Dependencies: EthernetInterface mbed-rtos mbed
Revision 0:30562abde493, committed 2015-09-24
- Comitter:
- Cypress
- Date:
- Thu Sep 24 22:24:28 2015 +0000
- Commit message:
- Initial commit, broken.
Changed in this revision
diff -r 000000000000 -r 30562abde493 EthernetInterface.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EthernetInterface.lib Thu Sep 24 22:24:28 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/EthernetInterface/#2fc406e2553f
diff -r 000000000000 -r 30562abde493 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Sep 24 22:24:28 2015 +0000 @@ -0,0 +1,100 @@ +#include "main.h" + +char rxBuffer[BUF_LEN]; +int rxChars; +bool rxDone; +long rxTime; +RtosTimer *rxTimer; + +char txBuffer[BUF_LEN]; +int txChars; + +Serial sp(SP_RX, SP_TX); +EthernetInterface eth; +TCPSocketServer serv; +TCPSocketConnection client; + +int main ( void ) +{ + // Initalize serial port + sp.baud(SP_BAUD); + sp.attach(&rxInt); + rxTimer = new RtosTimer(&timerTask, osTimerPeriodic, NULL); + rxTimer->start(SP_TIMER_PERIOD); + + // Initalize server + serv.bind(SERV_PORT); + serv.listen(); + while (true) + { + serv.accept(client); + client.set_blocking(false, 1500); + + while (client.is_connected()) + { + txChars = client.receive(txBuffer, BUF_LEN); + if (txChars <= 0) break; + + sendBuffer(txBuffer, txChars); + } + client.close(); + } +} + +void timerTask ( void const *arg ) +{ + if (!rxDone) + { + if (client.is_connected()) + { + client.send_all(rxBuffer, rxChars); + resetRX(); + } + } + else + { + if (rxChars > 0) + { + if (rxTime++ >= SP_TIMEOUT) rxDone = true; + } + } +} + +void rxInt ( void ) +{ + if (!rxDone) + { + if (rxChars < BUF_LEN) + { + rxBuffer[rxChars++] = SP_UART->D; + rxTime = 0; + } + else rxDone = true; + } +} + +void sendBuffer (char *buffer, int len) +{ + for (int i = 0; i < len; i++) + { + sp.putc(buffer[i]); + } +} + +void clearBuffer ( char *buffer, int len ) +{ + for (int i = 0; i < len; i++) buffer[i] = 0; +} + +void resetRX ( void ) +{ + clearBuffer(rxBuffer, BUF_LEN); + rxChars = 0; + rxDone = false; +} + +void resetTX ( void ) +{ + clearBuffer(txBuffer, BUF_LEN); + txChars = 0; +} \ No newline at end of file
diff -r 000000000000 -r 30562abde493 main.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.h Thu Sep 24 22:24:28 2015 +0000 @@ -0,0 +1,24 @@ +#include "mbed.h" +#include "TCPSocketServer.h" +#include "TCPSocketConnection.h" +#include "EthernetInterface.h" +#include "Serial.h" +#include "RtosTimer.h" + +#define SP_RX PTC17 +#define SP_TX PTC16 +#define SP_BAUD 9600 +#define SP_UART UART3 +#define SP_TIMER_PERIOD 10 +#define SP_TIMEOUT 10 + +#define BUF_LEN 1023 +#define SERV_PORT 1234 + +int main ( void ); +void timerTask ( void ); +void rxInt ( void ); +void sendBuffer (char *buffer, int len); +void clearBuffer ( char *buffer, int len ); +void resetRX ( void ); +void resetTX ( void );
diff -r 000000000000 -r 30562abde493 mbed-rtos.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Thu Sep 24 22:24:28 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#9d001ed5feec
diff -r 000000000000 -r 30562abde493 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Thu Sep 24 22:24:28 2015 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/4f6c30876dfa \ No newline at end of file