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@2:8cb6a0a77e56, 2018-10-17 (annotated)
- Committer:
- hudakz
- Date:
- Wed Oct 17 16:16:34 2018 +0000
- Revision:
- 2:8cb6a0a77e56
- Parent:
- 1:b869674fe56e
Number of start patterns in preamble can be set.
Who changed what in which revision?
User | Revision | Line number | New 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>© 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 | 1:b869674fe56e | 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 | 1:b869674fe56e | 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 | 1:b869674fe56e | 34 | * |
hudakz | 0:e076052bcffd | 35 | * The library is based on the article published by Adrian Mills: |
hudakz | 1:b869674fe56e | 36 | * http://www.quickbuilder.co.uk/qb/articles/Manchester_encoding_using_RS232.pdf |
hudakz | 0:e076052bcffd | 37 | */ |
hudakz | 0:e076052bcffd | 38 | #ifndef MANCHESTERUART_H |
hudakz | 0:e076052bcffd | 39 | #define MANCHESTERUART_H |
hudakz | 0:e076052bcffd | 40 | |
hudakz | 0:e076052bcffd | 41 | #include "mbed.h" |
hudakz | 0:e076052bcffd | 42 | #include "ManchesterMsg.h" |
hudakz | 0:e076052bcffd | 43 | |
hudakz | 1:b869674fe56e | 44 | #define START 0xF0 // start pattern |
hudakz | 0:e076052bcffd | 45 | |
hudakz | 1:b869674fe56e | 46 | #define STOP 0x0F // stop pattern |
hudakz | 0:e076052bcffd | 47 | class ManchesterUART |
hudakz | 0:e076052bcffd | 48 | { |
hudakz | 0:e076052bcffd | 49 | enum Error |
hudakz | 0:e076052bcffd | 50 | { |
hudakz | 0:e076052bcffd | 51 | NO_ERROR, |
hudakz | 0:e076052bcffd | 52 | ILLEGAL_CODE, |
hudakz | 0:e076052bcffd | 53 | RX_TIMEOUT, |
hudakz | 0:e076052bcffd | 54 | BUF_OVERRUN |
hudakz | 0:e076052bcffd | 55 | }; |
hudakz | 0:e076052bcffd | 56 | public: |
hudakz | 0:e076052bcffd | 57 | /** |
hudakz | 0:e076052bcffd | 58 | * @brief Creates a ManchesterUART object |
hudakz | 0:e076052bcffd | 59 | * @note |
hudakz | 0:e076052bcffd | 60 | * @param txPin Pin name of transmitter line |
hudakz | 1:b869674fe56e | 61 | * @param rxPin Pin name of receiver line |
hudakz | 1:b869674fe56e | 62 | * @param preamble Number of start patterns at the begin of each transmission |
hudakz | 1:b869674fe56e | 63 | * @param baudrate Communication bit rate in bits per second. Defaults to 115200bps |
hudakz | 1:b869674fe56e | 64 | * @param timeout Receive timeout in seconds. Defaults to 5 seconds. |
hudakz | 0:e076052bcffd | 65 | * @retval |
hudakz | 0:e076052bcffd | 66 | */ |
hudakz | 1:b869674fe56e | 67 | ManchesterUART |
hudakz | 1:b869674fe56e | 68 | ( |
hudakz | 1:b869674fe56e | 69 | PinName txPin, |
hudakz | 1:b869674fe56e | 70 | PinName rxPin, |
hudakz | 1:b869674fe56e | 71 | int baudrate = 9600, /* bits/sec */ |
hudakz | 1:b869674fe56e | 72 | uint8_t preamble = 8, /* number of start patterns */ |
hudakz | 1:b869674fe56e | 73 | float rxTimeout = 5 /* seconds */ |
hudakz | 1:b869674fe56e | 74 | ) : |
hudakz | 1:b869674fe56e | 75 | _serial(txPin, rxPin, baudrate), |
hudakz | 1:b869674fe56e | 76 | _rxTimeout(rxTimeout), |
hudakz | 1:b869674fe56e | 77 | _preamble(preamble), |
hudakz | 1:b869674fe56e | 78 | _error(NO_ERROR) |
hudakz | 1:b869674fe56e | 79 | { } |
hudakz | 0:e076052bcffd | 80 | |
hudakz | 1:b869674fe56e | 81 | ~ ManchesterUART(void) { } |
hudakz | 0:e076052bcffd | 82 | void transmit(ManchesterMsg& msg); |
hudakz | 1:b869674fe56e | 83 | void attach(Callback<void ()> func, SerialBase::IrqType type = SerialBase::RxIrq) { _serial.attach(func, type); } |
hudakz | 0:e076052bcffd | 84 | bool receive(ManchesterMsg& msg); |
hudakz | 1:b869674fe56e | 85 | bool readable() { return _serial.readable(); } |
hudakz | 1:b869674fe56e | 86 | void baud(int baudrate) { _serial.baud(baudrate); } |
hudakz | 1:b869674fe56e | 87 | void setPreamble(uint8_t length) { _preamble = length; } |
hudakz | 1:b869674fe56e | 88 | void setRxTimeout(int seconds) { _rxTimeout = seconds; } |
hudakz | 1:b869674fe56e | 89 | Error lastError() { return _error; } |
hudakz | 0:e076052bcffd | 90 | private: |
hudakz | 0:e076052bcffd | 91 | Serial _serial; |
hudakz | 1:b869674fe56e | 92 | char* _data; // data array |
hudakz | 1:b869674fe56e | 93 | size_t _len; // data length in bytes |
hudakz | 1:b869674fe56e | 94 | size_t _maxLen; // data maximum length |
hudakz | 1:b869674fe56e | 95 | Timeout _timeout; // timeout |
hudakz | 1:b869674fe56e | 96 | float _rxTimeout; // timeout time in seconds |
hudakz | 1:b869674fe56e | 97 | uint8_t _preamble; // number of start patterns |
hudakz | 1:b869674fe56e | 98 | Error _error; // error flag |
hudakz | 0:e076052bcffd | 99 | void rxTimeout(void); |
hudakz | 0:e076052bcffd | 100 | void transmitByte(uint8_t data); |
hudakz | 0:e076052bcffd | 101 | uint8_t receiveByte(void); |
hudakz | 0:e076052bcffd | 102 | uint8_t getNibble(uint8_t nibble); |
hudakz | 0:e076052bcffd | 103 | }; |
hudakz | 0:e076052bcffd | 104 | #endif // MANCHESTERUART_H |