Nico Bollen / LIN

Dependents:   MBED_LIN_RGB_Master_Example

LinMaster.h

Committer:
bollenn
Date:
2014-05-11
Revision:
2:6d4c7f841a5d
Parent:
1:58b5d1e8fae3
Child:
3:3656b0de0e43

File content as of revision 2:6d4c7f841a5d:

/*
 * A 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 "mbed.h"

#ifndef MBED_LIN_MASTER_H
#define MBED_LIN_MASTER_H

#include "mbed.h"

/**  A master device LIN communication library for mbed
 *
 * @code
 * #include "mbed.h"
 * #include "LinMaster.h"
 * 
 * LinMaster lin(p30);
 * 
 * int main() {
 *     (void)lin.init();
 *     (void)lin.sendframe(M2S, 0x3C, (uint8_t*)u8Data, 8);
 * }
 * @endcode
 */
class LinMaster
{
public:
    /** LIN master constructor
     *
     * @param Pin The pinname to be used for LIN communication
     */
    LinMaster(PinName Pin);

    /** LIN master destructor */
    ~LinMaster();

    /** Initialise the LIN module
     * - configure IO
     * - configure Timer
     *
     * @return
     *   true on succes,
     *   false on fail
     */
    bool Init(void);

    /** Set the LIN baudrate
     *
     * @param uBaud baudrate value in kbps (1..20000)
     * @return
     *   true on succes,
     *   false on fail
     */
    bool Baudrate(uint16_t uBaud);

    /** Get the LIN baudrate
     *
     * @return
     *   The current configured LIN baudrate
     */
    uint16_t Baudrate(void);
    
    /** Bus status */
    enum DriverStatus {
        INIT,       /**< initializing */
        IDLE,       /**< idle */
        WAKEUP,     /**< busy sending a wake up pulse */
        TRANSMIT,   /**< busy sending a frame */
        RECEIVE     /**< busy receiving data */
    };

    /** Get the current LIN driver status
     *
     * @return
     *   The current LIN driver status
     */
    DriverStatus GetDriverStatus(void) {return ( DriverStat );};

    /** Frame status */
    enum FrameStatus {
        READY,      /**< the frame is ready */
        BREAK,      /**< break is being sent */
        DELIMITER,  /**< delimiter is being sent */
        SYNC,       /**< sync field is being sent */
        ID,         /**< Frame ID byte is being sent */
        DATA        /**< Data bytes are being sent */
    };
    
    /** Get the current frame status
     *
     * @return
     *   The current frame status
     */
    FrameStatus GetFrameStatus(void) {return ( FrameStat );};
    
    /** Send frame direction */
    enum FrameDir {
        M2S,     /**< Master to Slave */
        S2M      /**< Slave to Master */
    };
    
    /** Send a frame on the LIN bus
     *
     * @param Dir direction of the frame
     * @param u8ID LIN Frame ID
     * @param ptrData pointer to the data to be transmitted/received
     * @param u8Len lenght of the data to be transmitted/received
     * @return
     *   true on succes,
     *   false on fail
     */
    bool SendFrame(FrameDir Dir, uint8_t u8ID, uint8_t* ptrData, uint8_t u8Len);
    
    void TickEventHndl(void);
    
private:
    DriverStatus DriverStat;
    FrameStatus FrameStat;
    uint16_t u16BitPeriod;
    uint8_t u8BreakLen;
    uint8_t u8DelimLen;
    uint8_t u8TickCnt;
    uint8_t u8NextBusLvl;
    uint8_t u8FrameID;
    FrameDir Direction;
    uint8_t u8FrameData[8];
    uint8_t u8FrameLen;
    uint8_t u8Byte;
    uint8_t u8ByteOnLin;
    PinName MyPin;
    Ticker MyTicker;
};

#endif /* MBED_LIN_MASTER_H */