Quadrature Decoder (QDEC)

Low-level QDEC peripheral driver.

This file was inspired by pwm.h written by : Hauke Petersen hauke.petersen@fu-berlin.de

QDEC interface enables access to CPU peripherals acquiring quadrature signals. On most platforms, this interface will be implemented based on hardware timers.

A quadrature encoder outputs two wave forms that are 90 degrees out of phase.

According to these cases, three modes are available :

  • X1 mode : signal A, rising edges (cases 1 and 2)
  • X2 mode : signal A, rising and falling edges (cases 1, 2, 3 and 4)
  • X4 mode : signals A and B, rising and falling edges (all cases)

The mapping/configuration of QDEC devices (timers) and the used pins has to be done in the board configuration (the board’s `periph_conf.h).

When using the QDEC interface, first thing you have to do is initialize the QDEC device with the targeted mode. Once the device is initialized, it will start counting quadrature signals on all configured pins immediately.

enum qdec_mode_t
QDEC_X1
QDEC_X2
QDEC_X4
unsigned int qdec_t

Default QDEC type definition.

void(* qdec_cb_t()

Signature of event callback functions triggered from interrupts.

Parameters

arg:optional context for the callback

int32_t qdec_init(qdec.h::qdec_t dev, qdec.h::qdec_mode_t mode, qdec.h::qdec_cb_t cb, void * arg)

Initialize a QDEC device.

The QDEC module is based on virtual QDEC devices. The QDEC devices can be configured to run in three modes :

  • X1
  • X2
  • X4 See description above for more details about modes.

On QDEC counter overflow, an interrupt is triggered. The interruption calls the callback defined.

Parameters

dev:QDEC device to initialize
mode:QDEC mode : X1, X2 or X4
cb:Callback on QDEC timer overflow
arg:Callback arguments

Return values

  • error code on error
  • 0 on success
int32_t qdec_read(qdec.h::qdec_t dev)

Read the current value of the given qdec device.

Parameters

dev:the qdec to read the current value from

Return values

  • the qdecs current value
int32_t qdec_read_and_reset(qdec.h::qdec_t dev)

Read the current value of the given qdec device and reset it.

Parameters

dev:the qdec to read the current value from

Return values

  • the qdecs current value
void qdec_start(qdec.h::qdec_t qdec)

Start the given qdec timer.

This function is only needed if the qdec timer was stopped manually before.

Parameters

qdec:the qdec device to start

void qdec_stop(qdec.h::qdec_t qdec)

Stop the given qdec timer.

This will effect all of the timer’s channels.

Parameters

qdec:the qdec device to stop

QDEC_DEV( x)

Default QDEC access macro.

1
(x)
QDEC_UNDEF

Default QDEC undefined value.

1
(UINT_MAX)
struct qdec_isr_ctx_t

Default interrupt context entry holding callback and argument.

qdec.h::qdec_cb_t cb

callback executed from qdec interrupt

void * arg

optional argument given to that callback