cyhal_quaddec_init() fails

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lh
Level 1
Level 1
First question asked Welcome!

I'm having trouble initializing the quadrature decoder resource. I've followed the initialization snippet in the HAL docs, but the initialization fails.

P12_6 and P12_7 are GPIO pins, so it seems like this should work. But when debugging the failure, it looks like cyhal_gpio_enable_output fails when called by cyhal_quaddec_init. Is there anything I'm missing here?

cyhal_quaddec_t rotary_obj;

void rotary_init(void)
{
	cy_rslt_t rslt;
    // Initialize the quadrature decoder object. Does not use index ('pin' is NC) and does not use a
    // pre-configured clock source ('clk' is NULL).
    rslt = cyhal_quaddec_init(&rotary_obj, P12_6, P12_7, NC, CYHAL_QUADDEC_RESOLUTION_1X, NULL,
                              1000000);
    CY_ASSERT(CY_RSLT_SUCCESS == rslt); // init fails here
    // Start the quadrature decoder
    rslt = cyhal_quaddec_start(&rotary_obj);
    CY_ASSERT(CY_RSLT_SUCCESS == rslt);
}

 

0 Likes
1 Solution
PandaS
Moderator
Moderator
Moderator
250 replies posted 100 solutions authored 5 likes given

Hi @lh ,
The initialization snippet provided in the HAL documentation is correct. The init failure occurs due to the incorrect pin selection in application code.
P12_6 and P12_7 are GPIO pins, but not all pins are routed to Quadrature Decoder block of TCPWM. That's why cyhal_gpio_enable_output fails when called by cyhal_quaddec_init.

 

Identify the following properties for appropriate pin selection:

  1. Follow the Device Datasheet's Alternate Functionality Pin Mapping Section.
    Look for: peri.tr_io_input[u]: 0 where u is the signal number under ACT #12
  2. Also Check the Schematic file and verify that Pin is not connected to any other peripheral devices. Example: For cy8cproto-062-4343w kit P1_0 is connected to CapSense Button, hence not available for QuadDec Funtion.

(Only the pins satisfying both 1. and 2. are routed to Quadrature Decoder Block of TCPWM)

Example: For cy8cproto-062-4343w kit the Quadrature Decoder supported pins are P8_0, P9_0, P9_1, P13_0, P13_1 …

I have also attached a working example project, do take a look. Here the Pins used are P13_0 and P13_1, this also works with P9_0 and P9_1, also with P8_0 and P13_1.

Hope this answers your query.
Thankyou
Sobhit

#include "cy_pdl.h"
#include "cyhal.h"
#include "cybsp.h"

//for hello world
#include "cy_retarget_io.h"

/* Variable for storing character read from terminal */
uint8_t uart_read_value;

int main(void)
{
    cy_rslt_t result;

    /* Initialize the device and board peripherals */
    result = cybsp_init() ;
    if (result != CY_RSLT_SUCCESS)
    {
        CY_ASSERT(0);
    }

    __enable_irq();

    /* Initialize retarget-io to use the debug UART port */
    result = cy_retarget_io_init(CYBSP_DEBUG_UART_TX, CYBSP_DEBUG_UART_RX, CY_RETARGET_IO_BAUDRATE);

    /* retarget-io init failed. Stop program execution */
    if (result != CY_RSLT_SUCCESS)
    {
        CY_ASSERT(0);
    }

    /* \x1b[2J\x1b[;H - ANSI ESC sequence for clear screen */
	printf("\x1b[2J\x1b[;H");

	printf("****************** ""PSoC 6 MCU: Hello World! Example + Quad Decode O/P Check""****************** \r\n\n");

	printf("Lets Check If QuadDec Works!!!\r\n\n");



    cyhal_quaddec_t quaddec_obj;
    uint32_t        count = 0x8000, prev_count = 0x8000;

    // Initialize the quadrature decoder object. Does not use index ('pin' is NC) and does not use a
    // pre-configured clock source ('clk' is NULL).
    result = cyhal_quaddec_init(&quaddec_obj, P13_0, P13_1, NC, CYHAL_QUADDEC_RESOLUTION_1X, NULL, 1000000);
    CY_ASSERT(CY_RSLT_SUCCESS == result);

    // Start the quadrature decoder
    result = cyhal_quaddec_start(&quaddec_obj);
    CY_ASSERT(CY_RSLT_SUCCESS == result);

    while (1)
    {
        count = cyhal_quaddec_read_counter(&quaddec_obj);

        // Check for direction of change
        if (count > prev_count)
        {
            // Clockwise rotation
        	printf("ClockWise\r\n");
        }
        else if (count < prev_count)
        {
            // Counter Clockwise rotation
        	printf("Counter - ClockWise\r\n");
        }
        else
        {
            // No change
        	printf("No Change\r\n");
        }

        prev_count = count;
    }



}

 

View solution in original post

0 Likes
1 Reply
PandaS
Moderator
Moderator
Moderator
250 replies posted 100 solutions authored 5 likes given

Hi @lh ,
The initialization snippet provided in the HAL documentation is correct. The init failure occurs due to the incorrect pin selection in application code.
P12_6 and P12_7 are GPIO pins, but not all pins are routed to Quadrature Decoder block of TCPWM. That's why cyhal_gpio_enable_output fails when called by cyhal_quaddec_init.

 

Identify the following properties for appropriate pin selection:

  1. Follow the Device Datasheet's Alternate Functionality Pin Mapping Section.
    Look for: peri.tr_io_input[u]: 0 where u is the signal number under ACT #12
  2. Also Check the Schematic file and verify that Pin is not connected to any other peripheral devices. Example: For cy8cproto-062-4343w kit P1_0 is connected to CapSense Button, hence not available for QuadDec Funtion.

(Only the pins satisfying both 1. and 2. are routed to Quadrature Decoder Block of TCPWM)

Example: For cy8cproto-062-4343w kit the Quadrature Decoder supported pins are P8_0, P9_0, P9_1, P13_0, P13_1 …

I have also attached a working example project, do take a look. Here the Pins used are P13_0 and P13_1, this also works with P9_0 and P9_1, also with P8_0 and P13_1.

Hope this answers your query.
Thankyou
Sobhit

#include "cy_pdl.h"
#include "cyhal.h"
#include "cybsp.h"

//for hello world
#include "cy_retarget_io.h"

/* Variable for storing character read from terminal */
uint8_t uart_read_value;

int main(void)
{
    cy_rslt_t result;

    /* Initialize the device and board peripherals */
    result = cybsp_init() ;
    if (result != CY_RSLT_SUCCESS)
    {
        CY_ASSERT(0);
    }

    __enable_irq();

    /* Initialize retarget-io to use the debug UART port */
    result = cy_retarget_io_init(CYBSP_DEBUG_UART_TX, CYBSP_DEBUG_UART_RX, CY_RETARGET_IO_BAUDRATE);

    /* retarget-io init failed. Stop program execution */
    if (result != CY_RSLT_SUCCESS)
    {
        CY_ASSERT(0);
    }

    /* \x1b[2J\x1b[;H - ANSI ESC sequence for clear screen */
	printf("\x1b[2J\x1b[;H");

	printf("****************** ""PSoC 6 MCU: Hello World! Example + Quad Decode O/P Check""****************** \r\n\n");

	printf("Lets Check If QuadDec Works!!!\r\n\n");



    cyhal_quaddec_t quaddec_obj;
    uint32_t        count = 0x8000, prev_count = 0x8000;

    // Initialize the quadrature decoder object. Does not use index ('pin' is NC) and does not use a
    // pre-configured clock source ('clk' is NULL).
    result = cyhal_quaddec_init(&quaddec_obj, P13_0, P13_1, NC, CYHAL_QUADDEC_RESOLUTION_1X, NULL, 1000000);
    CY_ASSERT(CY_RSLT_SUCCESS == result);

    // Start the quadrature decoder
    result = cyhal_quaddec_start(&quaddec_obj);
    CY_ASSERT(CY_RSLT_SUCCESS == result);

    while (1)
    {
        count = cyhal_quaddec_read_counter(&quaddec_obj);

        // Check for direction of change
        if (count > prev_count)
        {
            // Clockwise rotation
        	printf("ClockWise\r\n");
        }
        else if (count < prev_count)
        {
            // Counter Clockwise rotation
        	printf("Counter - ClockWise\r\n");
        }
        else
        {
            // No change
        	printf("No Change\r\n");
        }

        prev_count = count;
    }



}

 

0 Likes