PSoC6 ModusToolbox cyhal_adc library

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

cross mob
pime_2449811
Level 1
Level 1
First question asked First reply posted

I am working on a project to read a voltage from GPIO pin (e.g. P10_0) and send the ADC data with BLE link to a cellphone.  This project is based on:

PSoC 6

ModusToolbox

cyhal_adc library

I started with “BLE Battery Level FreeRTOS” example code.  

In main.c, I added the following code to start adc

...

/*Initialize GPIO Pin*/

cyhal_gpio_init((cyhal_gpio_t)P10_0, CYHAL_GPIO_DIR_INPUT,

                               CYHAL_GPIO_DRIVE_PULLDOWN, 1U);

/* Turn on the ADC for the specified pin */

cyhal_adc_t adc;

cyhal_adc_channel_t channel;

cy_rslt_t m_result =cyhal_adc_init( &adc, (cyhal_gpio_t)P10_0, (cyhal_clock_divider_t*)NULL );

cyhal_adc_channel_init( &channel, &adc, (cyhal_gpio_t)P10_0 );

if (CY_RSLT_SUCCESS == m_result){

cyhal_gpio_write((cyhal_gpio_t)CYBSP_USER_LED2, CYBSP_LED_STATE_ON);

}

......

m_result is to check if adc initialization is successful.  It is NOT.  I am wondering if I missed something before these codes?  Any help would be greatly appreciated.

Ping

0 Likes
1 Solution
RodolfoGL
Employee
Employee
250 solutions authored 250 sign-ins 5 comments on KBA

You don't need to call gpio_init() on P10_0. That's handle internally by the adc_init() function.

View solution in original post

0 Likes
4 Replies
RodolfoGL
Employee
Employee
250 solutions authored 250 sign-ins 5 comments on KBA

You don't need to call gpio_init() on P10_0. That's handle internally by the adc_init() function.

0 Likes

Thanks for the advice.  I did some debugging into “cyhal_adc.c”.  It seems that ADC channel enabling failed at:

    cyhal_resource_inst_t pinRsc = cyhal_utils_get_gpio_resource(pin);

    if (CY_RSLT_SUCCESS != (result = cyhal_hwmgr_reserve(&pinRsc))){

                return result;

    }

I do not know where to set “pinRsc” within ModusToolbox.  I think this may be the cause of the ADC reading failed. 

0 Likes

That's because you have called the gpio_Init on P10_0. It allocates this pin, so you can't use for other things, like the an ADC input.

The CYHAL uses a hardware manager for allocating peripherals, pins and clocks.

0 Likes

Many Thanks! 

You are right:  I deleted gpio_Init on P10_0 and also cleaned settings in “Device Configuration”.  It is working now 🙂

0 Likes