LwPKT

group LWPKT

Lightweight packet protocol.

Defines

lwpkt_get_from_addr(pkt)

Get address from where packet was sent.

Parameters
  • pkt[in] LwPKT instance

Returns

Address

lwpkt_get_to_addr(pkt)

Get address to where packet was sent.

Parameters
  • pkt[in] LwPKT instance

Returns

Address

lwpkt_get_data_len(pkt)

Get length of packet.

Parameters
  • pkt[in] LwPKT instance

Returns

Number of data bytes in packet

lwpkt_get_data(pkt)

Get pointer to packet data.

Parameters
  • pkt[in] LwPKT instance

Returns

Pointer to data

lwpkt_get_cmd(pkt)

Get packet command data field.

Parameters
  • pkt[in] LwPKT instance

Returns

Command data field

lwpkt_is_for_me(pkt)

Check if packet to field address matches device address.

Parameters
  • pkt[in] LwPKT instance

Returns

1 on success, 0 otherwise

lwpkt_is_broadcast(pkt)

Check if packet was sent to all devices on network.

Parameters
  • pkt[in] LwPKT instance

Returns

1 if broadcast, 0 otherwise

Typedefs

typedef uint32_t lwpkt_addr_t

Device address data type.

typedef void (*lwpkt_evt_fn)(lwpkt_t *pkt, lwpkt_evt_type_t type)

LwPKT event function.

Parameters
  • pkt[in] LwPKT instance with valid packet

  • type[in] Event type

Enums

enum lwpkt_state_t

Packet state enumeration.

Values:

enumerator LWPKT_STATE_START

Packet waits for start byte

enumerator LWPKT_STATE_FROM

Packet waits for “packet from” byte

enumerator LWPKT_STATE_TO

Packet waits for “packet to” byte

enumerator LWPKT_STATE_CMD

Packet waits for “packet cmd” byte

enumerator LWPKT_STATE_LEN

Packet waits for (multiple) data length bytes

enumerator LWPKT_STATE_DATA

Packet waits for actual data bytes

enumerator LWPKT_STATE_CRC

Packet waits for CRC data

enumerator LWPKT_STATE_STOP

Packet waits for stop byte

enum lwpktr_t

Packet result enumeration.

Values:

enumerator lwpktOK

Function returns successfully

enumerator lwpktERR

General error for function status

enumerator lwpktINPROG

Receive is in progress

enumerator lwpktVALID

packet valid and ready to be read as CRC is valid and STOP received

enumerator lwpktERRCRC

CRC integrity error for the packet. Will not wait STOP byte if received

enumerator lwpktERRSTOP

Packet error with STOP byte, wrong character received for STOP

enumerator lwpktWAITDATA

Packet state is in start mode, waiting start byte to start receiving

enum lwpkt_evt_type_t

List of event types.

Values:

enumerator LWPKT_EVT_PKT

Valid packet ready to read

enumerator LWPKT_EVT_TIMEOUT

Timeout on packat, reset event

Functions

lwpktr_t lwpkt_init (lwpkt_t *pkt, LWRB_VOLATILE lwrb_t *tx_rb, LWRB_VOLATILE lwrb_t *rx_rb)

Initialize packet instance and set device address.

Parameters
  • pkt[in] Packet instance

  • tx_rb[in] TX LwRB instance for data write

  • rx_rb[in] RX LwRB instance for data read

Returns

lwpktOK on success, member of lwpktr_t otherwise

lwpktr_t lwpkt_set_addr(lwpkt_t *pkt, lwpkt_addr_t addr)

Set device address for packet instance.

Parameters
  • pkt[in] Packet instance

  • addr[in] New device address

Returns

lwpktOK on success, member of lwpktr_t otherwise

lwpktr_t lwpkt_read(lwpkt_t *pkt)

Read raw data from RX buffer and prepare packet.

Parameters

pkt[in] Packet instance

Returns

lwpktVALID when packet valid, member of lwpktr_t otherwise

lwpktr_t lwpkt_write(lwpkt_t *pkt, lwpkt_addr_t to, uint8_t cmd, const void *data, size_t len)

Write packet data to TX ringbuffer.

Parameters
  • pkt[in] Packet instance

  • to[in] End device address

  • cmd[in] Packet command

  • data[in] Pointer to input data. Set to NULL if not used

  • len[in] Length of input data. Must be set to 0 if data == NULL

Returns

lwpktOK on success, member of lwpktr_t otherwise

lwpktr_t lwpkt_reset(lwpkt_t *pkt)

Reset packet state.

Parameters

pkt[in] Packet instance

Returns

lwpktOK on success, member of lwpktr_t otherwise

lwpktr_t lwpkt_process(lwpkt_t *pkt, uint32_t time, lwpkt_evt_fn evt_fn)

Process packet instance and read new data.

Parameters
  • pkt[in] Packet instance

  • time[in] Current time in units of milliseconds

  • evt_fn[in] Event function to be called on events

Returns

lwpktOK if processing OK, member of lwpktr_t otherwise

struct lwpkt_crc_t
#include <lwpkt.h>

CRC structure for packet.

Public Members

uint8_t crc

Current CRC value

struct lwpkt_t
#include <lwpkt.h>

Packet structure.

Public Members

lwpkt_addr_t addr

Current device address

uint8_t data[LWPKT_CFG_MAX_DATA_LEN]

Memory to write received data

LWRB_VOLATILE lwrb_t * tx_rb

TX ringbuffer

LWRB_VOLATILE lwrb_t * rx_rb

RX ringbuffer

uint32_t last_rx_time

Last RX time in units of milliseconds

lwpkt_state_t state

Actual packet state machine

lwpkt_crc_t crc

Packet CRC byte

lwpkt_addr_t from

Device address packet is coming from

lwpkt_addr_t to

Device address packet is intended for

uint8_t cmd

Command packet

size_t len

Number of bytes to receive

size_t index

General index variable for multi-byte parts of packet

struct lwpkt_t::[anonymous] m

Module that is periodically reset for next packet