wiegand: Wiegand protocol receiver

Typedefs

typedef struct wiegand_reader wiegand_reader_t
typedef void (*wiegand_callback_t)(wiegand_reader_t *reader)

Data reception callback.

Called from a task, not from an interrupt, when a frame is complete, that is when no pulse arrived for 50 ms. The frame is in wiegand_reader_t::buf, its length in bits in wiegand_reader_t::bits.

Data received while the callback runs is lost, reception is disabled for the duration of the call. The callback may call wiegand_reader_disable() to stop reception, but it must not call wiegand_reader_done().

Param reader:

Reader descriptor

Enums

enum wiegand_order_t

Bit and byte order of data.

Values:

enumerator WIEGAND_MSB_FIRST
enumerator WIEGAND_LSB_FIRST

Functions

esp_err_t wiegand_reader_init(wiegand_reader_t *reader, gpio_num_t gpio_d0, gpio_num_t gpio_d1, bool internal_pullups, size_t buf_size, wiegand_callback_t callback, wiegand_order_t bit_order, wiegand_order_t byte_order)

Create and initialize reader instance.

Both lines must be usable and not used by another reader of this driver; the driver keeps track of D0/D1 it has taken and returns ESP_ERR_INVALID_STATE if the lines are busy. Lines occupied by other drivers are not detected.

A frame ends when no pulse arrives for 50 ms, then wiegand_callback_t is called with the frame in wiegand_reader_t::buf and its length in bits in wiegand_reader_t::bits.

Buffer layout:

  • WIEGAND_MSB_FIRST byte order fills the buffer from its end, so the frame is right-aligned: buf[buf_size - 1] is the last byte of the frame;

  • WIEGAND_LSB_FIRST byte order fills the buffer from its beginning: buf[0] is the first byte of the frame;

  • WIEGAND_MSB_FIRST bit order packs the first received bit into the most significant bit of a byte, WIEGAND_LSB_FIRST into the least significant.

A frame longer than buf_size * 8 bits is truncated: the extra pulses are ignored, so a truncated frame is indistinguishable from a complete one except by its length in bits.

Parameters:
  • reader – Reader descriptor

  • gpio_d0 – GPIO pin for D0

  • gpio_d1 – GPIO pin for D1, must differ from gpio_d0

  • internal_pullups – Enable internal pull-up resistors for D0 and D1 GPIO

  • buf_size – Reader buffer size in bytes, must be large enough to contain entire Wiegand key

  • callback – Callback function for processing received codes

  • bit_order – Bit order of data

  • byte_order – Byte order of data

Returns:

  • ESP_OK on success

  • ESP_ERR_INVALID_ARG if a pointer is NULL, a pin is not usable or both pins are the same

  • ESP_ERR_INVALID_STATE if D0 or D1 is already used by another reader

  • ESP_ERR_NO_MEM if the frame buffer could not be allocated

  • error code from the GPIO or esp_timer driver otherwise

esp_err_t wiegand_reader_disable(wiegand_reader_t *reader)

Disable reader.

While reader is disabled, it will not receive new data. A frame that is being received is dropped.

May be called from the callback.

Parameters:

reader – Reader descriptor

Returns:

  • ESP_OK on success

  • ESP_ERR_INVALID_ARG if reader is NULL

  • ESP_ERR_INVALID_STATE if the reader is not initialized

esp_err_t wiegand_reader_enable(wiegand_reader_t *reader)

Enable reader.

Clears the frame buffer and the bit counter, so the frame received before is lost.

Parameters:

reader – Reader descriptor

Returns:

  • ESP_OK on success

  • ESP_ERR_INVALID_ARG if reader is NULL

  • ESP_ERR_INVALID_STATE if the reader is not initialized

esp_err_t wiegand_reader_done(wiegand_reader_t *reader)

Delete reader instance.

Releases the GPIO lines, so they can be used again. The descriptor is left in the uninitialized state: any call except wiegand_reader_init() returns ESP_ERR_INVALID_STATE.

Must not be called from the callback.

The frame timer is deleted by the esp_timer task, so the descriptor stays valid until the timer is gone. If that task does not respond, the reader stays initialized and the call can be repeated.

Parameters:

reader – Reader descriptor

Returns:

  • ESP_OK on success

  • ESP_ERR_INVALID_ARG if reader is NULL

  • ESP_ERR_INVALID_STATE if the reader is not initialized or the frame timer could not be deleted

  • error code from the GPIO driver otherwise

struct wiegand_reader
#include <wiegand.h>

Wiegand reader descriptor.

Fields marked as internal are maintained by the driver. The descriptor must stay alive until wiegand_reader_done() is called.

Public Members

gpio_num_t gpio_d0

D0 line, set by wiegand_reader_init()

gpio_num_t gpio_d1

D1 line, set by wiegand_reader_init()

wiegand_callback_t callback

Frame callback, set by wiegand_reader_init()

wiegand_order_t bit_order

Bit order, set by wiegand_reader_init()

wiegand_order_t byte_order

Byte order, set by wiegand_reader_init()

uint8_t *buf

Frame buffer, allocated by wiegand_reader_init()

size_t size

Frame buffer size in bytes, set by wiegand_reader_init()

volatile size_t bits

Bits in the current frame, internal.

esp_timer_handle_t timer

Frame timeout timer, internal.

volatile bool enabled

Reception state, internal.

SemaphoreHandle_t released

Release handshake, internal.

volatile bool releasing

Release state, internal.