Skip to content
/ tinyproto Public
forked from lexus2k/tinyproto

Tiny Software Protocol for communication over UART, SPI, etc

License

LGPL-3.0 and 2 other licenses found

Licenses found

LGPL-3.0
LICENSE
GPL-3.0
COPYING
LGPL-3.0
COPYING.LESSER
Notifications You must be signed in to change notification settings

r2r0/tinyproto

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Tiny Protocol
Build Status Coverage Status Documentation

Introduction

If you want to get stable code, please, refer to stable branch. HD (half duplex) protocol is removed from this version. If you need it, please refer to stable branch.

Tiny Protocol is layer 2 protocol. It is intended to be used for the systems with low resources. It is also can be compiled for desktop Linux system, and it can be built it for Windows. Using this library you can easy implement data transfer between 2 microcontrollers or between microcontroller and pc via UART, SPI, I2C or any other communication channels. You don't need to think about data synchronization between points. The library use no dynamic allocation of memory. TinyProto is based on RFC 1662, it implements the following frames:

  • U-frames (SABM, UA)
  • S-frames (REJ, RR)
  • I-frames

Key Features

Main features:

  • Hot plug/unplug support
  • Connection autorecover for Full duplex and Light protocols (with enabled crc)
  • Platform independent hdlc framing implementation (hdlc low level API: hdlc_ll_xxxx)
  • High level hdlc implementation for backward compatibility with previous releases (hdlc_xxxx API)
  • Easy to use Light protcol (tiny_light_xxxx API, see examples)
  • Full-duplex protocol (tiny_fd_xxxx true RFC 1662 implementation, supports confirmation, frames retransmissions)
  • Error detection (low level, high level hdlc and full duplex (fd) protocols)
    • Simple 8-bit checksum (sum of bytes)
    • FCS16 (CCITT-16)
    • FCS32 (CCITT-32)
  • Frames of maximum 32K or 2G size (payload limit depends on platform).
  • Low SRAM consumption (starts at 50 bytes).
  • Low Flash consumption (starts at 1KiB, features can be disabled and enabled at compilation time)
  • No dynamic memory allocation
  • Special serial loopback tool for debug purposes and performance testing

Supported platforms

  • Any platform, where C/C++ compiler is available (C99, C++11)
Platform Examples
ESP32 IDF
Cortex M0 Zero
Linux Linux
Windows Win32
Other Any platform with implemented HAL

What if my platform is not yet supported?

That's not a problem. Just implement abstraction layer for your platform (timing and mutex functions). Refer to tiny_hal_init() function. To understand HAL implementation refer to Linux and ESP32 examples in HAL abstraction layer. Do not forget to add TINY_CUSTOM_PLATFORM define to your compilation flags. You may use template code platform_hal.c

Easy to use

Cpp

Usage of light Tiny Protocol in C++ can look like this:

#include "TinyProtocol.h"

tinyproto::Light  proto;
tinyproto::Packet<256> packet;

void setup() {
    ...
    proto.beginToSerial();
}

void loop() {
    if (Serial.available()) {
        int len = proto.read( packet );
        if (len > 0) {
            /* Send message back */
            proto.write( packet );
        }
    }
}

Example of using full duplex Tiny Protocol in C++ is a little bit bigger, but it is still simple:

#include "TinyProtocol.h"

tinyproto::Fd<FD_MIN_BUF_SIZE(64,4)>  proto;

void onReceive(void *udata, tinyproto::IPacket &pkt) {
    // Process message here, you can do with the message, what you need
    // Let's send it back to the sender ;)
    if ( proto.write(pkt) == TINY_ERR_TIMEOUT ) {
        // Do what you need to do if looping back failed on timeout.
        // But never use blocking operations inside callback
    }
}

void setup() {
    ...
    // Here we say FD protocol object, which callback to call once new msg is received
    proto.setReceiveCallback( onReceive );
    proto.begin();
}

void loop() {
    if (Serial.available()) {
        uint8_t byte = Serial.read();
        proto.run_rx( &byte, 1 ); // run FD protocol parser to process data received from the channel
    }
    uint8_t byte;
    if ( proto.run_tx( &byte, 1 ) == 1 ) // FD protocol fills buffer with data, we need to send to the channel
    {
        while ( Serial.write( byte ) == 0 ); // Just send the data
    }
}

Python

import tinyproto

p = tinyproto.Hdlc()
def on_read(a):
    print("Received bytes: " + ','.join( [ "{:#x}".format(x) for x in a ] ) )

# setup protocol
p.on_read = on_read
p.begin()

# provide rx bytes to the protocol, obtained from hardware rx channel
p.rx( bytearray([ 0x7E, 0xFF, 0x3F, 0xF3, 0x39, 0x7E  ]) )

How to build

Linux

make
# === OR ===
mkdir build
cd build
cmake -DEXAMPLES=ON ..
make

Windows

mkdir build
cd build
cmake -G "Visual Studio 16 2019" -DEXAMPLES=ON ..

ESP32

Just place the library to your project components folder.

Setting up

  • Arduino Option 1 (with docs and tools)

    • Download source from https://github.com/lexus2k/tinyproto
    • Put the downloaded library content to Arduino/libraries/tinyproto folder
    • Restart the Arduino IDE
    • You will find the examples in the Arduino IDE under File->Examples->tinyproto
  • Arduino Option 2 (only library without docs)

    • Go to Arduino Library manager
    • Find and install tinyproto library
    • Restart the Arduino IDE
    • You will find the examples in the Arduino IDE under File->Examples->tinyproto
  • ESP32 IDF

  • Linux

  • Plain AVR

  • Python

Using tiny_loopback tool

  • Connect your Arduino board to PC

  • Run your sketch or tinylight_loopback

  • Compile tiny_loopback tool

  • Run tiny_loopback tool: ./bld/tiny_loopback -p /dev/ttyUSB0 -t light -g -c 8 -a -r

  • Connect your Arduino board to PC

  • Run your sketch or tinyfd_loopback

  • Compile tiny_loopback tool

  • Run tiny_loopback tool: ./bld/tiny_loopback -p /dev/ttyUSB0 -t fd -c 8 -w 3 -g -a -r

For more information about this library, please, visit https://github.com/lexus2k/tinyproto. Doxygen documentation can be found at Codedocs xyz site. If you found any problem or have any idea, please, report to Issues section.

License

Copyright 2016-2021 (C) Alexey Dynda

This file is part of Tiny Protocol Library.

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

Protocol Library 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 Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with Protocol Library. If not, see http://www.gnu.org/licenses/.

About

Tiny Software Protocol for communication over UART, SPI, etc

Resources

License

LGPL-3.0 and 2 other licenses found

Licenses found

LGPL-3.0
LICENSE
GPL-3.0
COPYING
LGPL-3.0
COPYING.LESSER

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C 49.2%
  • C++ 47.7%
  • Shell 1.1%
  • Makefile 0.7%
  • CMake 0.5%
  • Python 0.5%
  • Batchfile 0.3%