Skip to content

Commit

Permalink
Tiny Protocol library fixes, and sketches for Arduino.
Browse files Browse the repository at this point in the history
  • Loading branch information
lexus2k committed Mar 19, 2016
1 parent c301d60 commit 1859fb4
Show file tree
Hide file tree
Showing 19 changed files with 585 additions and 127 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ OS ?= os/linux
DESTDIR ?=
BLD ?= bld

VERSION=0.1.0
VERSION=0.1.1

ifeq ($(TINYCONF), nano)
CONFIG_ENABLE_FCS32 ?= n
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* This example sends back every packet received over UART.
* To test this example, just compile it and upload to Arduino controller.
* Open Serial Monitor in Arduino IDE. send anything typing data around
* '~' chars (for example, ~Welcome~ ), and Arduino will send back the packet to you.
*
* TinyProtocol uses some special frame format, that means that it will ignore not valid
* chars received over UART. Each packet should be in the following format:
* ~[UID]DATA[FCS]~
* UID and FCS field are optional. In the example below, they are disabled. Nano versition
* of Tiny protocol supports only simple 1-byte Checksum field. For CRC-16/CRC-32 use Micro and
* Full versions of Tiny protocol.
*/
#include <TinyProto-Nano.h>

/* Creating protocol object is simple */
Tiny::Proto proto;

void setup() {
/* No timeout, since we want non-blocking UART operations. */
Serial.setTimeout(0);
/* Initialize serial protocol for test purposes */
Serial.begin(9600);
/* Redirect all protocol communication to Serial0 UART */
proto.beginToSerial();
/* We do not want to use crc */
proto.disableCrc();
}

/* Specify buffer for packets to send and receive */
char g_buf[16];

/* Create special class, which simplifies the work with buffer */
Tiny::Packet g_packet(g_buf, sizeof(g_buf));

void loop()
{
/* Check if some data are waiting for reading in UART */
/* If there is no delay in loop() cycle, there is no need to check for OUT_OF_SYNC error */
int len = proto.read(g_packet, TINY_FLAG_WAIT_FOREVER);
/* Check if we received something with no error */
if (len > 0)
{
/* Send received packet back to UART (echo) */
proto.write(g_packet, TINY_FLAG_WAIT_FOREVER);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* This example sends <Hello> packet every second.
* If some packet is received from remote side, it echos content back to the sender
*/
#include <TinyProto-Nano.h>

const int PIN_LED = 13;

/* Creating protocol object is simple */
Tiny::Proto proto;

void setup() {
pinMode(PIN_LED, OUTPUT);
/* No timeout, since we want non-blocking UART operations */
Serial.setTimeout(0);
/* Initialize serial protocol for test purposes */
Serial.begin(9600);
/* Redirect all protocol communication to Serial0 UART */
proto.beginToSerial();
/* We do not want to use crc */
proto.disableCrc();
}

/* Specify buffer for packets to send and receive */
char g_inBuf[16];
char g_outBuf[16];

/* Create special class, which simplifies the work with buffer */
Tiny::Packet outPacket(g_outBuf, sizeof(g_outBuf));
Tiny::Packet inPacket(g_inBuf, sizeof(g_inBuf));


void loop()
{
/* Create simple packet, containing only text */
outPacket.clear();
outPacket.put("\nHello\n");

/* Send packet over UART to other side */
proto.write(outPacket, TINY_FLAG_WAIT_FOREVER);

/* Check if some data are waiting for reading in UART */
/* If there is no delay in loop() cycle, there is no need to check for OUT_OF_SYNC error */
int len;
do {
len = proto.read(inPacket, TINY_FLAG_NO_WAIT);
} while (len == TINY_ERR_OUT_OF_SYNC);
if (len > 0)
{
/* Send received packet back to UART (echo) */
proto.write(inPacket, TINY_FLAG_WAIT_FOREVER);
}
delay(985);
digitalWrite(PIN_LED, HIGH);
delay(15);
digitalWrite(PIN_LED, LOW);
}
2 changes: 1 addition & 1 deletion releases/arduino/TinyProto-Full/library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=TinyProto-Full
version=0.1.0
version=0.1.1
author=Alexey Dynda
maintainer=Alexey Dynda <[email protected]>
sentence=Allows to communicate other boards/PC via any physical connection.
Expand Down
6 changes: 6 additions & 0 deletions releases/arduino/TinyProto-Full/src/TinyProto-Full.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,12 @@ class Packet
m_len += strlen(str);
m_buf[m_len++] = 0; }

/**
* Adds data from packet to the new packet being built.
* @param pkt - reference to the Packet to add.
*/
inline void put (const Packet &pkt){ memcpy(&m_buf[m_len], pkt.m_buf, pkt.m_len); m_len += pkt.m_len; }

/**
* Puts uid to the packet.
* @warning uid is sent only if this functionality is enabled in the Proto.
Expand Down
88 changes: 47 additions & 41 deletions releases/arduino/TinyProto-Full/src/proto/tiny_layer2.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
along with Protocol Library. If not, see <http://www.gnu.org/licenses/>.
*/

#include "tiny_layer2.h"
#include "crc.h"
#include "tiny_layer2.h"

#ifdef CONFIG_ENABLE_STATS
#define STATS(x) x
Expand Down Expand Up @@ -86,7 +86,7 @@ inline static int __check_fcs_field(uint8_t fcs_bits, fcs_t fcs)
#ifdef CONFIG_ENABLE_CHECKSUM
if ((fcs_bits == 8) && (fcs == GOODCHECKSUM)) return 1;
#endif
return 0;
return fcs_bits == 0;
}


Expand Down Expand Up @@ -236,27 +236,21 @@ int tiny_close(STinyData *handle)

int tiny_set_fcs_bits(STinyData *handle, uint8_t bits)
{
int result;
int result = TINY_NO_ERROR;
switch (bits)
{
#ifdef CONFIG_ENABLE_FCS16
case 16:
handle->fcs_bits = bits;
result = TINY_NO_ERROR;
break;
#endif
#ifdef CONFIG_ENABLE_FCS32
case 32:
handle->fcs_bits = bits;
result = TINY_NO_ERROR;
break;
#endif
#ifdef CONFIG_ENABLE_CHECKSUM
case 8:
#endif
case 0:
handle->fcs_bits = bits;
result = TINY_NO_ERROR;
break;
#endif
default:
result = TINY_ERR_FAILED;
break;
Expand Down Expand Up @@ -360,27 +354,37 @@ static int __send_frame_uid_state(STinyData *handle)
}


/**
* This is handler for SEND DATA state. It switches to
* TINY_TX_STATE_END only if all bytes are sent.
* If next byte is successfully sent, it returns 1.
* If switched to other state, it returns 1.
* If no byte is sent during the cycle, it returns 0.
* If error happened, it returns negative value.
* @param handle - pointer to Tiny Protocol structure
*/
static int __send_frame_data_state(STinyData *handle)
{
int result;
uint8_t byte;
if (handle->tx.sentbytes >= handle->tx.framelen)
{
__commit_fcs_field(handle->fcs_bits, &handle->tx.fcs);
#ifdef TINY_FCS_ENABLE
/* sending crc */
handle->tx.inprogress = handle->fcs_bits ? TINY_TX_STATE_SEND_CRC : TINY_TX_STATE_END;
handle->tx.bits = 0;
#else
handle->tx.inprogress = TINY_TX_STATE_END;
#endif
return 1;
}
byte = handle->tx.pframe[handle->tx.sentbytes];
result = __send_byte_state(handle, byte);
if (result > 0)
{
__update_fcs_field(handle->fcs_bits, &handle->tx.fcs, byte);
handle->tx.sentbytes++;
if (handle->tx.sentbytes == handle->tx.framelen)
{
__commit_fcs_field(handle->fcs_bits, &handle->tx.fcs);
#ifdef TINY_FCS_ENABLE
/* sending crc */
handle->tx.inprogress = handle->fcs_bits ? TINY_TX_STATE_SEND_CRC : TINY_TX_STATE_END;
handle->tx.bits = 0;
#else
handle->tx.inprogress = TINY_TX_STATE_END;
#endif
}
}
return result;
}
Expand Down Expand Up @@ -489,7 +493,10 @@ static int __wait_send_complete(STinyData *handle, uint8_t *pbuf, uint8_t flags)
return result;
}
#else
#define __wait_send_complete(handle, pbuf, flags) 0
#define __wait_send_complete(handle, pbuf, flags) \
0; \
handle->tx.pframe = pbuf; \
handle->tx.inprogress = TINY_TX_STATE_START;
#endif


Expand Down Expand Up @@ -526,7 +533,7 @@ int tiny_send(STinyData *handle, uint16_t *uid, uint8_t * pbuf, int len, uint8_t
do
{
result = __tiny_send_data(handle);
if (result != 0)
if ((result != 0) || (handle->tx.inprogress == TINY_TX_STATE_IDLE))
{
/* exit on error and on successful send */
break;
Expand Down Expand Up @@ -692,7 +699,7 @@ static int __tiny_read_data(STinyData *handle, uint16_t *uid, uint8_t *pbuf, int

int tiny_read(STinyData *handle, uint16_t *uid, uint8_t *pbuf, int len, uint8_t flags)
{
int result;
int result = TINY_NO_ERROR;
uint8_t byte;

if (!handle)
Expand All @@ -710,21 +717,8 @@ int tiny_read(STinyData *handle, uint16_t *uid, uint8_t *pbuf, int len, uint8_t
# else
result = handle->read_func(&byte, 1);
# endif
if (result<0)
{
result = TINY_ERR_FAILED;
break;
}
/* if no data on UART, just exit */
if (result == 0)
{
result = TINY_NO_ERROR;
if (!(flags & TINY_FLAG_WAIT_FOREVER))
{
break;
}
}
else /* Byte is received */
/* Byte is received */
if (result > 0)
{
/* New frame must be started with 0x7E char */
if (byte != FLAG_SEQUENCE)
Expand All @@ -739,11 +733,23 @@ int tiny_read(STinyData *handle, uint16_t *uid, uint8_t *pbuf, int len, uint8_t
handle->rx.prevbyte = byte;
__init_fcs_field(handle->fcs_bits, &handle->rx.fcs);
}
else if (result<0)
{
result = TINY_ERR_FAILED;
break;
}
}
/* Some frame is in process of receiving. Continue to parse data */
result = __tiny_read_data(handle, uid, pbuf, len);
if (handle->rx.inprogress != TINY_RX_STATE_IDLE)
{
result = __tiny_read_data(handle, uid, pbuf, len);
}
if (result == 0)
{
if (!(flags & TINY_FLAG_WAIT_FOREVER))
{
break;
}
TASK_YIELD();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* This example sends back every packet received over UART.
* To test this example, just compile it and upload to Arduino controller.
* Open Serial Monitor in Arduino IDE. send anything typing data around
* '~' chars (for example, ~Welcome~ ), and Arduino will send back the packet to you.
*
* TinyProtocol uses some special frame format, that means that it will ignore not valid
* chars received over UART. Each packet should be in the following format:
* ~[UID]DATA[FCS]~
* UID and FCS field are optional. In the example below, they are disabled. Nano versition
* of Tiny protocol supports only simple 1-byte Checksum field. For CRC-16/CRC-32 use Micro and
* Full versions of Tiny protocol.
*/
#include <TinyProto-Nano.h>

/* Creating protocol object is simple */
Tiny::Proto proto;

void setup() {
/* No timeout, since we want non-blocking UART operations. */
Serial.setTimeout(0);
/* Initialize serial protocol for test purposes */
Serial.begin(9600);
/* Redirect all protocol communication to Serial0 UART */
proto.beginToSerial();
/* We do not want to use crc */
proto.disableCrc();
}

/* Specify buffer for packets to send and receive */
char g_buf[16];

/* Create special class, which simplifies the work with buffer */
Tiny::Packet g_packet(g_buf, sizeof(g_buf));

void loop()
{
/* Check if some data are waiting for reading in UART */
/* If there is no delay in loop() cycle, there is no need to check for OUT_OF_SYNC error */
int len = proto.read(g_packet, TINY_FLAG_WAIT_FOREVER);
/* Check if we received something with no error */
if (len > 0)
{
/* Send received packet back to UART (echo) */
proto.write(g_packet, TINY_FLAG_WAIT_FOREVER);
}
}
Loading

0 comments on commit 1859fb4

Please sign in to comment.