Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: MBED_LIN_RGB_Master_Example
Diff: LinMaster.cpp
- Revision:
- 2:6d4c7f841a5d
- Parent:
- 1:58b5d1e8fae3
- Child:
- 3:3656b0de0e43
--- a/LinMaster.cpp Tue May 06 20:17:35 2014 +0000 +++ b/LinMaster.cpp Sun May 11 13:14:16 2014 +0000 @@ -27,6 +27,8 @@ LinMaster::LinMaster(PinName Pin) { + DriverStat = INIT; + FrameStat = READY; MyPin = Pin; u8BreakLen = 20; u8DelimLen = 4; @@ -44,6 +46,8 @@ LinPin.output(); LinPin.write(1); + DriverStat = IDLE; + return ( true ); } @@ -67,13 +71,164 @@ bool LinMaster::SendFrame(FrameDir Dir, uint8_t u8ID, uint8_t* ptrData, uint8_t u8Len) { - DigitalInOut LinPin(MyPin); - LinPin.output(); - LinPin.write(1); + bool blReturn = false; + uint8_t i; + + if (DriverStat == IDLE) + { + DriverStat = TRANSMIT; + FrameStat = BREAK; + u8NextBusLvl = 0; + u8TickCnt = 0; + Direction = Dir; + u8FrameID = u8ID; + u8FrameLen = u8Len; + + for (i=0; i<u8Len; i++) + { + u8FrameData[i] = *ptrData++; + } + + DigitalInOut LinPin(MyPin); + LinPin.output(); + LinPin.write(1); + + MyTicker.attach_us(this, &LinMaster::TickEventHndl, u16BitPeriod); + + blReturn = true; + } + + return ( blReturn ); +} + +void LinMaster::TickEventHndl(void) +{ + DigitalInOut LinPin(MyPin); + bool blByteSend = false; + + if (u8NextBusLvl != 0) + { + LinPin.output(); + LinPin.write(1); + } + else + { + LinPin.output(); + LinPin.write(0); + } + + u8TickCnt++; - wait_us(u16BitPeriod); + switch (FrameStat) + { + case BREAK: + u8NextBusLvl = 0; + if (u8TickCnt >= u8BreakLen) + { + u8TickCnt = 0; + FrameStat = DELIMITER; + u8NextBusLvl = 1; + } + break; + + case DELIMITER: + if (u8TickCnt >= u8DelimLen) + { + u8TickCnt = 0; + FrameStat = SYNC; + u8Byte = 0xAA; + } + break; + + case SYNC: + blByteSend = true; + if (u8TickCnt >= 10) + { + u8TickCnt = 0; + FrameStat = ID; + u8Byte = u8FrameID; + } + break; + + case ID: + blByteSend = true; + if (u8TickCnt >= 10) + { + u8TickCnt = 0; + FrameStat = DATA; + u8ByteOnLin = 0; + if (Direction == M2S) + { + u8Byte = u8FrameData[u8ByteOnLin]; + } + else + { + blByteSend = false; + } + } + break; + + case DATA: + if (Direction == M2S) + { + blByteSend = true; + } + else + { + blByteSend = false; + } + + if (u8TickCnt >= 10) + { + if (Direction == S2M) + { + u8FrameData[u8ByteOnLin] = u8Byte; + } + + u8ByteOnLin++; + u8TickCnt = 0; + + if (u8ByteOnLin >= u8FrameLen) + { + /* Sending/Receiving is ended */ + FrameStat = READY; + blByteSend = false; + u8NextBusLvl = 1; + } + else if (Direction == M2S) + { + u8Byte = u8FrameData[u8ByteOnLin]; + } + } + break; + + case READY: + default: + u8NextBusLvl = 1; + MyTicker.detach(); + FrameStat = READY; + DriverStat = IDLE; + break; + } - return ( true ); + if (blByteSend == true) + { + switch (u8TickCnt) + { + case 0: + /* Start Bit */ + u8NextBusLvl = 0; + break; + case 9: + /* Stop Bit */ + u8NextBusLvl = 1; + break; + default: + u8NextBusLvl = u8Byte & 0x1; + u8Byte >>= 1; + break; + } + } } /* EOF */ \ No newline at end of file