High level interface for interacting with the Quadrature Decoder hardware resource.
The quadrature decoder block is commonly used to measure the time of occurrence of an event, to measure the time difference between two events or perform an action after a specified period of time.
The Quadrature Decoder block provides the ability to count transitions on a pair of digital signals. The signals are typically provided by a speed/position feedback system mounted on a motor or trackball. The driver allows the user to invoke a callback function when a particular event occurs. The signals, typically called A and B, are positioned 90° out-of-phase, which results in a Gray code output (a sequence where only one bit changes on each count). It also allows detection of direction and relative position. A third optional signal, named index, is used as a reference to establish an absolute position once per rotation.
The Quadrature Decoder operates in one of three resolution modes. (see cyhal_quaddec_resolution_t) The mode dictates the number of events that are counted.
An index event causes the counter to be set to the midpoint. For example, if the hardware has 16-bit resolution, the midpoint would be 0x8000. For 32-bit resolution: 0x80000000.
For more details about this functionality, see the "Quadrature Decoder Mode" section of the Technical Reference Manual.
Some use case scenarios of the Quadrature Decoder:
cyhal_quaddec_init can be used for quadrature decoder initialization by providing the quaddec object - cyhal_quaddec_t, input pins, and shared clock source - clk (optional).
See Snippet 1: Initialization and direction detection.
The following snippet initializes a quadrature decoder and measures the counter to determine direction. The clk may be left NULL, in which case a clock resource is automatically assigned.
The following snippet initializes a quadrature decoder and triggers an event as changes happen. The clk need not be provided (NULL), in which case a clock resource is assigned.
API Reference | |
Quadrature Decoder HAL Results | |
Quadrature Decoder specific return codes. | |
Typedefs | |
typedef void(* | cyhal_quaddec_event_callback_t) (void *callback_arg, cyhal_quaddec_event_t event) |
Handler for quadrature decoder events. | |
Enumerations | |
enum | cyhal_quaddec_resolution_t { CYHAL_QUADDEC_RESOLUTION_1X , CYHAL_QUADDEC_RESOLUTION_2X , CYHAL_QUADDEC_RESOLUTION_4X } |
Operating resolutions for the quadrature decoder. More... | |
enum | cyhal_quaddec_input_t { CYHAL_QUADDEC_INPUT_PHI_A , CYHAL_QUADDEC_INPUT_PHI_B , CYHAL_QUADDEC_INPUT_STOP , CYHAL_QUADDEC_INPUT_INDEX } |
Quadrature decoder input signal. More... | |
enum | cyhal_quaddec_output_t { CYHAL_QUADDEC_OUTPUT_COMPARE_MATCH } |
Quadrature decoder output signal. More... | |
enum | cyhal_quaddec_event_t { CYHAL_QUADDEC_IRQ_NONE = 0 , CYHAL_QUADDEC_IRQ_TERMINAL_COUNT = (1 << 0) , CYHAL_QUADDEC_IRQ_CAPTURE = (1 << 1) , CYHAL_QUADDEC_IRQ_ALL = (1 << 2) - 1 } |
Interrupt triggers for the quadrature decoder. More... | |
Functions | |
cy_rslt_t | cyhal_quaddec_init (cyhal_quaddec_t *obj, cyhal_gpio_t phi_a, cyhal_gpio_t phi_b, cyhal_gpio_t index, cyhal_quaddec_resolution_t resolution, const cyhal_clock_t *clk, uint32_t frequency) |
Initialize the quadrature decoder peripheral and configure the pin. More... | |
cy_rslt_t | cyhal_quaddec_init_cfg (cyhal_quaddec_t *obj, const cyhal_quaddec_configurator_t *cfg) |
Initialize the quadrature decoder peripheral using a configurator generated configuration struct. More... | |
void | cyhal_quaddec_free (cyhal_quaddec_t *obj) |
Deinitialize the quadrature decoder object. More... | |
cy_rslt_t | cyhal_quaddec_start (cyhal_quaddec_t *obj) |
Starts the quadrature decoder. More... | |
cy_rslt_t | cyhal_quaddec_stop (cyhal_quaddec_t *obj) |
Stops the quadrature decoder. More... | |
int32_t | cyhal_quaddec_get_delta (cyhal_quaddec_t *obj) |
Gets the change in the quadrature decoder counter, either positive or negative, since the last time that this function was called. More... | |
uint32_t | cyhal_quaddec_read_counter (const cyhal_quaddec_t *obj) |
Reads the current value from the quadrature decoder The read operation works even if the counter is stopped. More... | |
uint32_t | cyhal_quaddec_read_capture (const cyhal_quaddec_t *obj) |
Reads the value from the quadrature decoder's capture register This function does not clear the counter value. More... | |
void | cyhal_quaddec_register_callback (cyhal_quaddec_t *obj, cyhal_quaddec_event_callback_t callback, void *callback_arg) |
Register a quadrature decoder callback handler This function does not clear the counter value. More... | |
void | cyhal_quaddec_enable_event (cyhal_quaddec_t *obj, cyhal_quaddec_event_t event, uint8_t intr_priority, bool enable) |
Configure quadrature decoder event enable More... | |
cy_rslt_t | cyhal_quaddec_connect_digital (cyhal_quaddec_t *obj, cyhal_source_t source, cyhal_quaddec_input_t signal) |
Connects a source signal and configures and enables a quadrature decoder event to be triggered from that signal. More... | |
cy_rslt_t | cyhal_quaddec_connect_digital2 (cyhal_quaddec_t *obj, cyhal_source_t source, cyhal_quaddec_input_t signal, cyhal_edge_type_t edge_type) |
Connects a source signal and configures and enables a quadrature decoder event to be triggered from that signal with a configurable edge type. More... | |
cy_rslt_t | cyhal_quaddec_disconnect_digital (cyhal_quaddec_t *obj, cyhal_source_t source, cyhal_quaddec_input_t signal) |
Disconnects a source signal and disables the quadrature decoder event. More... | |
cy_rslt_t | cyhal_quaddec_enable_output (cyhal_quaddec_t *obj, cyhal_quaddec_output_t signal, cyhal_source_t *source) |
Enables the specified output signal from a quadrature decoder that will be triggered when the corresponding event occurs. More... | |
cy_rslt_t | cyhal_quaddec_disable_output (cyhal_quaddec_t *obj, cyhal_quaddec_output_t signal) |
Disables the specified output signal from a quadrature decoder. More... | |
Quadrature decoder input signal.
cy_rslt_t cyhal_quaddec_init | ( | cyhal_quaddec_t * | obj, |
cyhal_gpio_t | phi_a, | ||
cyhal_gpio_t | phi_b, | ||
cyhal_gpio_t | index, | ||
cyhal_quaddec_resolution_t | resolution, | ||
const cyhal_clock_t * | clk, | ||
uint32_t | frequency | ||
) |
Initialize the quadrature decoder peripheral and configure the pin.
See Snippet 1: Initialization and direction detection.
[out] | obj | Pointer to a quadrature decoder object. The caller must allocate the memory for this object but the init function will initialize its contents. |
[in] | phi_a | - The "A" input of the quadrature decoder. |
[in] | phi_b | - The "B" input of the quadrature decoder. |
[in] | index | - Optional, resets the counter when active to act as a reference position for the quadrature decoder. |
[in] | resolution | - The resolution that the quadrature decoder runs at |
[in] | clk | - Optional, the shared clock to use, if not provided a new clock will be allocated and the quadrature decoder frequency will be set to the value passed in with the frequency parameter. |
[in] | frequency | - This is the frequency, in hertz, to use with the clock allocated by this function. This parameter is only used if the clk parameter is set to NULL. When the clk parameter is not NULL, this must be set to zero. When the clk paramether is NULL, this must be set to something other than zero. |
cy_rslt_t cyhal_quaddec_init_cfg | ( | cyhal_quaddec_t * | obj, |
const cyhal_quaddec_configurator_t * | cfg | ||
) |
Initialize the quadrature decoder peripheral using a configurator generated configuration struct.
[out] | obj | Pointer to a quadrature decoder object. The caller must allocate the memory for this object but the init function will initialize its contents. |
[in] | cfg | Configuration structure generated by a configurator. |
void cyhal_quaddec_free | ( | cyhal_quaddec_t * | obj | ) |
Deinitialize the quadrature decoder object.
[in,out] | obj | The quadrature decoder object |
cy_rslt_t cyhal_quaddec_start | ( | cyhal_quaddec_t * | obj | ) |
Starts the quadrature decoder.
This function also acts as a reset, in that it will trigger reload/index the QuadDec. When this function is called, the count value gets stored in the capture register and the count value is returned to the mid point. For example, if the hardware has 16-bit resolution, the midpoint would be 0x8000. For 32-bit resolution: 0x80000000. See Snippet 1: Initialization and direction detection.
[in] | obj | The quadrature decoder object |
cy_rslt_t cyhal_quaddec_stop | ( | cyhal_quaddec_t * | obj | ) |
Stops the quadrature decoder.
Does not reset counter value.
[in] | obj | The quadrature decoder object |
int32_t cyhal_quaddec_get_delta | ( | cyhal_quaddec_t * | obj | ) |
Gets the change in the quadrature decoder counter, either positive or negative, since the last time that this function was called.
[in] | obj | The quadrature decoder object |
uint32_t cyhal_quaddec_read_counter | ( | const cyhal_quaddec_t * | obj | ) |
Reads the current value from the quadrature decoder
The read operation works even if the counter is stopped.
See Snippet 1: Initialization and direction detection.
[in] | obj | The quadrature decoder object |
uint32_t cyhal_quaddec_read_capture | ( | const cyhal_quaddec_t * | obj | ) |
Reads the value from the quadrature decoder's capture register
This function does not clear the counter value.
The capture register is updated whenever there is an index event.
[in] | obj | The quadrature decoder object |
void cyhal_quaddec_register_callback | ( | cyhal_quaddec_t * | obj, |
cyhal_quaddec_event_callback_t | callback, | ||
void * | callback_arg | ||
) |
Register a quadrature decoder callback handler
This function does not clear the counter value.
This function will be called when one of the events enabled by cyhal_quaddec_enable_event occurs.
See Snippet 2: Handling an event in a callback function.
[in] | obj | The quadrature decoder object |
[in] | callback | The callback handler which will be invoked when the event occurs |
[in] | callback_arg | Generic argument that will be provided to the callback when called |
void cyhal_quaddec_enable_event | ( | cyhal_quaddec_t * | obj, |
cyhal_quaddec_event_t | event, | ||
uint8_t | intr_priority, | ||
bool | enable | ||
) |
Configure quadrature decoder event enable
When an enabled event occurs, the function specified by cyhal_quaddec_register_callback will be called.
See Snippet 2: Handling an event in a callback function.
[in] | obj | The quadrature decoder object |
[in] | event | The quadrature decoder event type |
[in] | intr_priority | The priority for NVIC interrupt events |
[in] | enable | True to turn on interrupts, False to turn off |
cy_rslt_t cyhal_quaddec_connect_digital | ( | cyhal_quaddec_t * | obj, |
cyhal_source_t | source, | ||
cyhal_quaddec_input_t | signal | ||
) |
Connects a source signal and configures and enables a quadrature decoder event to be triggered from that signal.
These quadrature decoder events can be configured independently and connect to the same or different source signals.
[in] | obj | Quadrature decoder obj |
[in] | source | Source signal obtained from another driver's cyhal_<PERIPH>_enable_output |
[in] | signal | The quadrature decoder input signal |
cy_rslt_t cyhal_quaddec_connect_digital2 | ( | cyhal_quaddec_t * | obj, |
cyhal_source_t | source, | ||
cyhal_quaddec_input_t | signal, | ||
cyhal_edge_type_t | edge_type | ||
) |
Connects a source signal and configures and enables a quadrature decoder event to be triggered from that signal with a configurable edge type.
These quadrature decoder events can be configured independently and connect to the same or different source signals.
[in] | obj | Quadrature decoder obj |
[in] | source | Source signal obtained from another driver's cyhal_<PERIPH>_enable_output |
[in] | signal | The quadrature decoder input signal |
[in] | edge_type | The edge type that should trigger the event. This must be consistent with the edge type of source . If source produces a "level" signal, the only valid value is CYHAL_EDGE_TYPE_LEVEL. If source produces an "edge" signal, then CYHAL_EDGE_TYPE_LEVEL is not a valid value. |
cy_rslt_t cyhal_quaddec_disconnect_digital | ( | cyhal_quaddec_t * | obj, |
cyhal_source_t | source, | ||
cyhal_quaddec_input_t | signal | ||
) |
Disconnects a source signal and disables the quadrature decoder event.
[in] | obj | Quadrature decoder obj |
[in] | source | Source signal from cyhal_<PERIPH>_enable_output to disable |
[in] | signal | The quadrature decoder input signal |
cy_rslt_t cyhal_quaddec_enable_output | ( | cyhal_quaddec_t * | obj, |
cyhal_quaddec_output_t | signal, | ||
cyhal_source_t * | source | ||
) |
Enables the specified output signal from a quadrature decoder that will be triggered when the corresponding event occurs.
Multiple output signals can be configured simultaneously.
[in] | obj | Quadrature decoder obj |
[in] | signal | The quadrature decoder output signal |
[out] | source | Pointer to user-allocated source signal object which will be initialized by enable_output. source should be passed to (dis)connect_digital functions to (dis)connect the associated endpoints. |
cy_rslt_t cyhal_quaddec_disable_output | ( | cyhal_quaddec_t * | obj, |
cyhal_quaddec_output_t | signal | ||
) |
Disables the specified output signal from a quadrature decoder.
[in] | obj | Quadrature decoder obj |
[in] | signal | The quadrature decoder output signal |