WIFI
Dependencies: mbed
Fork of SerialPassthroughcjsESP8266 by
main.cpp@7:d78ed22a787d, 2016-06-07 (annotated)
- Committer:
- cstevens
- Date:
- Tue Jun 07 19:37:59 2016 +0000
- Revision:
- 7:d78ed22a787d
- Parent:
- 6:dc4c165f6b53
- Child:
- 8:34cc66421054
added comments and changed name to reflect newer specialist role of the code on the CWM for testing the ESP units.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cstevens | 7:d78ed22a787d | 1 | /* Simpler prog based on the serial passthrough code to enable a command line driven test of esp8266 |
cstevens | 7:d78ed22a787d | 2 | * wifi modules. |
cstevens | 7:d78ed22a787d | 3 | * NB this uses the mbed sleep() command to form a low power system but on some MCUs this is a problem |
cstevens | 7:d78ed22a787d | 4 | * this works fine on an lpc1768 but not as yet on the KL25Z |
cstevens | 7:d78ed22a787d | 5 | */ |
mbedAustin | 0:59bec1fd956e | 6 | #include "mbed.h" |
mbedAustin | 2:a8dcb07a1d00 | 7 | |
cstevens | 7:d78ed22a787d | 8 | RawSerial pc(USBTX, USBRX); // serial terminal for the pc connection |
cstevens | 7:d78ed22a787d | 9 | RawSerial dev(p28,p27); // serial uart for connecting to the esp8266 tx,rx NB mbed tx must connect ot esp rx and vice versa |
cstevens | 7:d78ed22a787d | 10 | DigitalOut led1(LED1); // twp leds |
cstevens | 7:d78ed22a787d | 11 | DigitalOut led4(LED4); // to allow visual check of bidirectional comms |
cstevens | 6:dc4c165f6b53 | 12 | DigitalOut rst(p26); |
mbedAustin | 2:a8dcb07a1d00 | 13 | |
cstevens | 7:d78ed22a787d | 14 | // subroutine to run anytime a serial interrupt arrives from the device |
cstevens | 7:d78ed22a787d | 15 | // this basically passes everything thatthe device produces on to the pc terminal screen |
sam_grove | 5:96cb82af9996 | 16 | void dev_recv() |
mbedAustin | 2:a8dcb07a1d00 | 17 | { |
sam_grove | 5:96cb82af9996 | 18 | led1 = !led1; |
sam_grove | 5:96cb82af9996 | 19 | while(dev.readable()) { |
sam_grove | 5:96cb82af9996 | 20 | pc.putc(dev.getc()); |
sam_grove | 5:96cb82af9996 | 21 | } |
sam_grove | 5:96cb82af9996 | 22 | } |
cstevens | 7:d78ed22a787d | 23 | // subroutine to service the serial interrupt on the pc connection |
cstevens | 7:d78ed22a787d | 24 | // this is a bit more complex - it takes what the use sends on the pc and copies it on to the device |
cstevens | 7:d78ed22a787d | 25 | // the esp should echo these straight back to the the pc if all is well |
cstevens | 7:d78ed22a787d | 26 | // this also detects the end of command character which is ascii 13 (0x0d) adn adds a linefeed after it =asscii 10 (0x0a) |
sam_grove | 5:96cb82af9996 | 27 | void pc_recv() |
sam_grove | 5:96cb82af9996 | 28 | { |
cstevens | 6:dc4c165f6b53 | 29 | char c; |
sam_grove | 5:96cb82af9996 | 30 | led4 = !led4; |
sam_grove | 5:96cb82af9996 | 31 | while(pc.readable()) { |
cstevens | 6:dc4c165f6b53 | 32 | c=pc.getc(); |
cstevens | 6:dc4c165f6b53 | 33 | dev.putc(c); |
cstevens | 6:dc4c165f6b53 | 34 | //pc.putc(c); // echo back |
cstevens | 7:d78ed22a787d | 35 | if(c==13) {dev.putc(10); // send the linefeed to complement the carriage return generated by return key on the pc |
cstevens | 6:dc4c165f6b53 | 36 | pc.putc(10); |
cstevens | 6:dc4c165f6b53 | 37 | } |
mbedAustin | 0:59bec1fd956e | 38 | } |
mbedAustin | 0:59bec1fd956e | 39 | } |
mbedAustin | 4:ba9100d52e48 | 40 | |
mbedAustin | 4:ba9100d52e48 | 41 | int main() |
mbedAustin | 4:ba9100d52e48 | 42 | { |
cstevens | 6:dc4c165f6b53 | 43 | rst=0; |
cstevens | 6:dc4c165f6b53 | 44 | wait(1); |
cstevens | 6:dc4c165f6b53 | 45 | rst=1; |
cstevens | 6:dc4c165f6b53 | 46 | wait(1); |
cstevens | 6:dc4c165f6b53 | 47 | pc.printf("go\n\r"); |
cstevens | 6:dc4c165f6b53 | 48 | pc.baud(115200); |
cstevens | 6:dc4c165f6b53 | 49 | dev.baud(115200); |
mbedAustin | 4:ba9100d52e48 | 50 | |
sam_grove | 5:96cb82af9996 | 51 | pc.attach(&pc_recv, Serial::RxIrq); |
sam_grove | 5:96cb82af9996 | 52 | dev.attach(&dev_recv, Serial::RxIrq); |
cstevens | 6:dc4c165f6b53 | 53 | |
cstevens | 6:dc4c165f6b53 | 54 | |
sam_grove | 5:96cb82af9996 | 55 | |
mbedAustin | 4:ba9100d52e48 | 56 | while(1) { |
sam_grove | 5:96cb82af9996 | 57 | sleep(); |
mbedAustin | 4:ba9100d52e48 | 58 | } |
mbedAustin | 4:ba9100d52e48 | 59 | } |