BeRTOS
|
pocketBus protocol implementation. More...
#include "pocketbus.h"
#include "cfg/cfg_pocketbus.h"
#include <cfg/log.h>
#include <cfg/debug.h>
#include <cfg/macros.h>
#include <io/kfile.h>
#include <cpu/byteorder.h>
#include <string.h>
Go to the source code of this file.
Functions | |
void | pocketbus_putchar (struct PocketBusCtx *ctx, uint8_t c) |
Send a character over pocketBus channel stream, handling escape mode. | |
void | pocketbus_begin (struct PocketBusCtx *ctx, pocketbus_addr_t addr) |
Send pocketBus packet header. | |
void | pocketbus_write (struct PocketBusCtx *ctx, const void *_data, size_t len) |
Send buffer _data over bus, handling escape. | |
void | pocketbus_end (struct PocketBusCtx *ctx) |
Send pocketBus packet tail. | |
void | pocketbus_send (struct PocketBusCtx *ctx, pocketbus_addr_t addr, const void *data, size_t len) |
Send buffer of data to address addr with a pocketBus packet over channel stream. | |
bool | pocketbus_recv (struct PocketBusCtx *ctx, struct PocketMsg *msg) |
Try to read a packet from the pocketBus. | |
void | pocketbus_init (struct PocketBusCtx *ctx, struct KFile *fd) |
Initialize pocketBus protocol handler. |
pocketBus protocol implementation.
pocketBus protocol is a simple strictly master-slave protocol, usable in embedded systems. pocketBus frame is as follows:
+----------------------------------------+ | STX | VER | ADDR | PAYLOAD | CKS | ETX | +----------------------------------------+ | | | | | | | + 1B + 1B + 2B + N Byte + 2B + 1B +
Protocol parsing start on STX reception. When the receiving routine finds an STX char, it starts to read characters from the bus until an ETX is received. Once a packet is received, the parser checks packet correctness and checksum. If all is OK the payload is returned.
STX (0x02), ETX(0x03) and ESC(0x1B) are special characters and cannot be transmitted inside payload without escaping them. To escape a character you must precede it by the ESC char. E.G. STX -> ESC + STX ETX -> ESC + ETX ESC -> ESC + ESC
In the ADDR field is always specified the slave address. In the case of master trasmitting, ADDR contains the slave destination address. In case of slave replying, ADDR contains the slave address itself. Thus, the master device does not have an address. Packet must be routed to master by hardware bus design.
The checksum algorithm used is rotating hash algortihm, quite simple but more reliable than simple checksum. The checksum in computed on all fields excluding STX, ETX and CHK fields itself. Checksum is computed on the packet *before* escaping. Escape sequence counts for 1 character only (the escaped one).
Definition in file pocketbus.c.
bool pocketbus_recv | ( | struct PocketBusCtx * | ctx, |
struct PocketMsg * | msg | ||
) |
Try to read a packet from the pocketBus.
Definition at line 171 of file pocketbus.c.