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
LinMaster.cpp
- Committer:
- bollenn
- Date:
- 2014-05-11
- Revision:
- 2:6d4c7f841a5d
- Parent:
- 1:58b5d1e8fae3
- Child:
- 3:3656b0de0e43
File content as of revision 2:6d4c7f841a5d:
/* * Master device LIN communication library for mbed * * Copyright (C) 2014 TASS Belgium NV * * Released under GPL v2 * * Other licensing models might apply at the sole discretion of the copyright holders. * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software * and associated documentation files (the "Software"), to deal in the Software without restriction, * including without limitation the rights to use, copy, modify, merge, publish, distribute, * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all copies or * substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "LinMaster.h" LinMaster::LinMaster(PinName Pin) { DriverStat = INIT; FrameStat = READY; MyPin = Pin; u8BreakLen = 20; u8DelimLen = 4; (void)Baudrate(9600); } LinMaster::~LinMaster() { } bool LinMaster::Init(void) { DigitalInOut LinPin(MyPin); LinPin.output(); LinPin.write(1); DriverStat = IDLE; return ( true ); } bool LinMaster::Baudrate(uint16_t uBaud) { bool blReturn = false; if ((uBaud > 0) && (uBaud <= 20000)) { u16BitPeriod = 1000000/uBaud; blReturn = true; } return ( blReturn ); } uint16_t LinMaster::Baudrate(void) { return ( 1000000/u16BitPeriod ); } bool LinMaster::SendFrame(FrameDir Dir, uint8_t u8ID, uint8_t* ptrData, uint8_t u8Len) { 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++; 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; } 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 */