Library for Manchester encoding using UART's hardware.

Dependents:   ManchesterUART_Transmitter ManchesterUART_Receiver

Manchester encoding using UART's hardware

This library implements Manchester code using UART. Each data byte is encoded into two bytes representing two nibbles of the original data. UART's hardware is then used to generate the Manchester encoded bit stream. Prior to decoding, the receiver converts the received bit stream to bytes using its UART. Start and stop patterns are sent to identify the boundaries (begin and end) of a data frame.

ACKNOWLEDGEMENT: The library is based on an article published by Adrian Mills.


Import programManchesterUART_Transmitter

Transmitter demo for the Manchester encoding library using UART's hardware.


Import programManchesterUART_Receiver

Receiver demo for the Manchester encoding library using UART's hardware.

NOTE: To perform a simple test (without radio link) connect the txPin on transmitter board to the rxPin on the receiver board and make sure that grounds are also connected one another.

ManchesterUART.h

Committer:
hudakz
Date:
2017-11-22
Revision:
0:e076052bcffd
Child:
1:b869674fe56e

File content as of revision 0:e076052bcffd:

/*
 ******************************************************************************
 * @file    ManchesterUART.h
 * @author  Zoltan Hudak
 * @version
 * @date    2017-Nov-22
 * @brief   Manchester code over UART for mbed
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; COPYRIGHT(c) 2017 Zoltan Hudak <hudakz@outlook.com>
 *
 * All rights reserved.

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/*
 * This library implements Manchester code using UART serial connection. Each 
 * data byte is encoded into two bytes representing two nibbles of the original
 * data byte. These two bytes are then sent over UART serial link connection. 
 * The receiver reconstructs the original data byte from the two bytes received.
 * A start and stop pattern are sent to signify the begin and end of a message.
 *  
 * The library is based on the article published by Adrian Mills:
 * http://www.quickbuilder.co.uk/qb/articles/Manchester_encoding_using_RS232.pdf 
 */

#ifndef MANCHESTERUART_H
#define MANCHESTERUART_H

#include "mbed.h"
#include "ManchesterMsg.h"

#define START 0xF0          // start pattern
#define STOP  0x0F          // stop pattern

class   ManchesterUART
{
    enum Error
    {
        NO_ERROR,
        ILLEGAL_CODE,
        RX_TIMEOUT,
        BUF_OVERRUN
    };

public:
    /**
     * @brief   Creates a ManchesterUART object
     * @note
     * @param   txPin Pin name of transmitter line
     *          rxPin Pin name of receiver line
     *          speed Communication bit rate in bits per second. Defaults to 9600bps
     *          timeout Receive timeout in seconds. Defaults to 5 seconds.
     * @retval
     */
    ManchesterUART(PinName txPin, PinName rxPin, int speed = 9600, float timeout_sec = 5) :
        _serial(txPin, rxPin, speed), _timeout_sec(timeout_sec), _error(NO_ERROR) {}

    ~ManchesterUART(void) { }
    void    transmit(ManchesterMsg& msg);
    bool    receive(ManchesterMsg& msg);
    Error   lastError() { return _error; }

private:
    Serial  _serial;
    char*   _data;          // data array
    size_t  _len;           // data length in bytes
    size_t  _maxLen;        // maximum length
    Timeout _timeout;       // timeout
    float   _timeout_sec;   // timeout in seconds
    Error   _error;         // error flag

    void    rxTimeout(void);
    void    transmitByte(uint8_t data);
    uint8_t receiveByte(void);
    uint8_t getNibble(uint8_t nibble);
};
#endif // MANCHESTERUART_H