UART Driver to receive asynchronous Serial Comms from a Raspberry Pi and parse the results.
SerialComms.cpp@5:d497795761a5, 2016-02-20 (annotated)
- Committer:
- sk398
- Date:
- Sat Feb 20 13:33:32 2016 +0000
- Revision:
- 5:d497795761a5
- Parent:
- 4:85353bd571f2
zxzxzc
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sk398 | 0:8ac1280934b4 | 1 | #include "mbed.h" |
sk398 | 0:8ac1280934b4 | 2 | #include "SerialComms.h" |
sk398 | 0:8ac1280934b4 | 3 | |
sk398 | 1:bf3fb80028d8 | 4 | SerialComms::SerialComms(PinName tx,PinName rx) |
sk398 | 0:8ac1280934b4 | 5 | { |
sk398 | 3:b608ee5b9b5d | 6 | _HLC_Conn = new RawSerial(tx,rx); |
sk398 | 0:8ac1280934b4 | 7 | _HLC_Conn-> baud(SERIAL_BAUD_RATE); |
sk398 | 0:8ac1280934b4 | 8 | _HLC_Conn -> format(8,SerialBase::None,1); |
sk398 | 0:8ac1280934b4 | 9 | |
sk398 | 3:b608ee5b9b5d | 10 | _HLC_Conn -> attach(this,&SerialComms::incomingDataISR,RawSerial::RxIrq); |
sk398 | 0:8ac1280934b4 | 11 | } |
sk398 | 0:8ac1280934b4 | 12 | |
sk398 | 0:8ac1280934b4 | 13 | |
sk398 | 2:cb74b330b285 | 14 | void SerialComms::incomingDataISR() |
sk398 | 0:8ac1280934b4 | 15 | { |
sk398 | 0:8ac1280934b4 | 16 | int a = 0; |
sk398 | 2:cb74b330b285 | 17 | dataCheck = 0; |
sk398 | 0:8ac1280934b4 | 18 | for(uint8_t charCount=0; charCount<(2*NUM_BYTES_RECEIVING); charCount++) |
sk398 | 0:8ac1280934b4 | 19 | { |
sk398 | 0:8ac1280934b4 | 20 | if((charCount%2) == 1) |
sk398 | 0:8ac1280934b4 | 21 | { |
sk398 | 0:8ac1280934b4 | 22 | receiverBuffer[a] = _HLC_Conn -> getc(); |
sk398 | 0:8ac1280934b4 | 23 | a++; |
sk398 | 0:8ac1280934b4 | 24 | } |
sk398 | 0:8ac1280934b4 | 25 | else |
sk398 | 0:8ac1280934b4 | 26 | { |
sk398 | 2:cb74b330b285 | 27 | dataCheck += _HLC_Conn -> getc(); |
sk398 | 0:8ac1280934b4 | 28 | } |
sk398 | 0:8ac1280934b4 | 29 | } |
sk398 | 2:cb74b330b285 | 30 | incomingDataUpdate = TRUE; |
sk398 | 0:8ac1280934b4 | 31 | } |
sk398 | 0:8ac1280934b4 | 32 | |
sk398 | 4:85353bd571f2 | 33 | int *SerialComms::returnCommData() |
sk398 | 0:8ac1280934b4 | 34 | { |
sk398 | 4:85353bd571f2 | 35 | if(SerialComms::incomingDataUpdate == TRUE && dataCheck == 15) |
sk398 | 0:8ac1280934b4 | 36 | { |
sk398 | 2:cb74b330b285 | 37 | SerialComms::incomingDataUpdate = FALSE; |
sk398 | 0:8ac1280934b4 | 38 | return receiverBuffer; |
sk398 | 0:8ac1280934b4 | 39 | } |
sk398 | 0:8ac1280934b4 | 40 | else |
sk398 | 0:8ac1280934b4 | 41 | { |
sk398 | 0:8ac1280934b4 | 42 | int noNewData = -1; |
sk398 | 0:8ac1280934b4 | 43 | int *noNewDataPtr = &noNewData; |
sk398 | 0:8ac1280934b4 | 44 | return noNewDataPtr; |
sk398 | 0:8ac1280934b4 | 45 | } |
sk398 | 0:8ac1280934b4 | 46 | } |