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.

Committer:
hudakz
Date:
Wed Nov 22 16:37:13 2017 +0000
Revision:
0:e076052bcffd
Child:
1:b869674fe56e
Initial release.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 0:e076052bcffd 1 /*
hudakz 0:e076052bcffd 2 ******************************************************************************
hudakz 0:e076052bcffd 3 * @file ManchesterUART.h
hudakz 0:e076052bcffd 4 * @author Zoltan Hudak
hudakz 0:e076052bcffd 5 * @version
hudakz 0:e076052bcffd 6 * @date 2017-Nov-22
hudakz 0:e076052bcffd 7 * @brief Manchester code over UART for mbed
hudakz 0:e076052bcffd 8 ******************************************************************************
hudakz 0:e076052bcffd 9 * @attention
hudakz 0:e076052bcffd 10 *
hudakz 0:e076052bcffd 11 * <h2><center>&copy; COPYRIGHT(c) 2017 Zoltan Hudak <hudakz@outlook.com>
hudakz 0:e076052bcffd 12 *
hudakz 0:e076052bcffd 13 * All rights reserved.
hudakz 0:e076052bcffd 14
hudakz 0:e076052bcffd 15 This program is free software: you can redistribute it and/or modify
hudakz 0:e076052bcffd 16 it under the terms of the GNU General Public License as published by
hudakz 0:e076052bcffd 17 the Free Software Foundation, either version 3 of the License, or
hudakz 0:e076052bcffd 18 (at your option) any later version.
hudakz 0:e076052bcffd 19
hudakz 0:e076052bcffd 20 This program is distributed in the hope that it will be useful,
hudakz 0:e076052bcffd 21 but WITHOUT ANY WARRANTY; without even the implied warranty of
hudakz 0:e076052bcffd 22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
hudakz 0:e076052bcffd 23 GNU General Public License for more details.
hudakz 0:e076052bcffd 24
hudakz 0:e076052bcffd 25 You should have received a copy of the GNU General Public License
hudakz 0:e076052bcffd 26 along with this program. If not, see <http://www.gnu.org/licenses/>.
hudakz 0:e076052bcffd 27 */
hudakz 0:e076052bcffd 28 /*
hudakz 0:e076052bcffd 29 * This library implements Manchester code using UART serial connection. Each
hudakz 0:e076052bcffd 30 * data byte is encoded into two bytes representing two nibbles of the original
hudakz 0:e076052bcffd 31 * data byte. These two bytes are then sent over UART serial link connection.
hudakz 0:e076052bcffd 32 * The receiver reconstructs the original data byte from the two bytes received.
hudakz 0:e076052bcffd 33 * A start and stop pattern are sent to signify the begin and end of a message.
hudakz 0:e076052bcffd 34 *
hudakz 0:e076052bcffd 35 * The library is based on the article published by Adrian Mills:
hudakz 0:e076052bcffd 36 * http://www.quickbuilder.co.uk/qb/articles/Manchester_encoding_using_RS232.pdf
hudakz 0:e076052bcffd 37 */
hudakz 0:e076052bcffd 38
hudakz 0:e076052bcffd 39 #ifndef MANCHESTERUART_H
hudakz 0:e076052bcffd 40 #define MANCHESTERUART_H
hudakz 0:e076052bcffd 41
hudakz 0:e076052bcffd 42 #include "mbed.h"
hudakz 0:e076052bcffd 43 #include "ManchesterMsg.h"
hudakz 0:e076052bcffd 44
hudakz 0:e076052bcffd 45 #define START 0xF0 // start pattern
hudakz 0:e076052bcffd 46 #define STOP 0x0F // stop pattern
hudakz 0:e076052bcffd 47
hudakz 0:e076052bcffd 48 class ManchesterUART
hudakz 0:e076052bcffd 49 {
hudakz 0:e076052bcffd 50 enum Error
hudakz 0:e076052bcffd 51 {
hudakz 0:e076052bcffd 52 NO_ERROR,
hudakz 0:e076052bcffd 53 ILLEGAL_CODE,
hudakz 0:e076052bcffd 54 RX_TIMEOUT,
hudakz 0:e076052bcffd 55 BUF_OVERRUN
hudakz 0:e076052bcffd 56 };
hudakz 0:e076052bcffd 57
hudakz 0:e076052bcffd 58 public:
hudakz 0:e076052bcffd 59 /**
hudakz 0:e076052bcffd 60 * @brief Creates a ManchesterUART object
hudakz 0:e076052bcffd 61 * @note
hudakz 0:e076052bcffd 62 * @param txPin Pin name of transmitter line
hudakz 0:e076052bcffd 63 * rxPin Pin name of receiver line
hudakz 0:e076052bcffd 64 * speed Communication bit rate in bits per second. Defaults to 9600bps
hudakz 0:e076052bcffd 65 * timeout Receive timeout in seconds. Defaults to 5 seconds.
hudakz 0:e076052bcffd 66 * @retval
hudakz 0:e076052bcffd 67 */
hudakz 0:e076052bcffd 68 ManchesterUART(PinName txPin, PinName rxPin, int speed = 9600, float timeout_sec = 5) :
hudakz 0:e076052bcffd 69 _serial(txPin, rxPin, speed), _timeout_sec(timeout_sec), _error(NO_ERROR) {}
hudakz 0:e076052bcffd 70
hudakz 0:e076052bcffd 71 ~ManchesterUART(void) { }
hudakz 0:e076052bcffd 72 void transmit(ManchesterMsg& msg);
hudakz 0:e076052bcffd 73 bool receive(ManchesterMsg& msg);
hudakz 0:e076052bcffd 74 Error lastError() { return _error; }
hudakz 0:e076052bcffd 75
hudakz 0:e076052bcffd 76 private:
hudakz 0:e076052bcffd 77 Serial _serial;
hudakz 0:e076052bcffd 78 char* _data; // data array
hudakz 0:e076052bcffd 79 size_t _len; // data length in bytes
hudakz 0:e076052bcffd 80 size_t _maxLen; // maximum length
hudakz 0:e076052bcffd 81 Timeout _timeout; // timeout
hudakz 0:e076052bcffd 82 float _timeout_sec; // timeout in seconds
hudakz 0:e076052bcffd 83 Error _error; // error flag
hudakz 0:e076052bcffd 84
hudakz 0:e076052bcffd 85 void rxTimeout(void);
hudakz 0:e076052bcffd 86 void transmitByte(uint8_t data);
hudakz 0:e076052bcffd 87 uint8_t receiveByte(void);
hudakz 0:e076052bcffd 88 uint8_t getNibble(uint8_t nibble);
hudakz 0:e076052bcffd 89 };
hudakz 0:e076052bcffd 90 #endif // MANCHESTERUART_H