- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I'm working on CY8CKIT-062S2-43012.
* Create project from AnyCloud_BLE_Battery_Service.
* I'd like to change sending POT voltage as the battery level.
Add code to initialize and get ADC value.
However, execution stop at `cyhal_adc_read_u16();` It does not return value.
File `cyhal_adc.c` function `cyhal_adc_read()`, the following infinity loop does not break.
`while(!obj->adc->conversion_complete) { }`
How to use ADC?
```
int main()
{
cy_rslt_t result ;
/* Initialize the board support package */
result = cybsp_init();
if (CY_RSLT_SUCCESS != result)
{
CY_ASSERT(0);
}
/* Enable global interrupts */
__enable_irq();
/* Initialize retarget-io to use the debug UART port */
cy_retarget_io_init(CYBSP_DEBUG_UART_TX, CYBSP_DEBUG_UART_RX, CY_RETARGET_IO_BAUDRATE);
cy_rslt_t rslt;
cyhal_adc_t adc_obj;
cyhal_adc_channel_t adc_chan_0_obj;
/* ADC conversion result. */
int32_t adc_out;
/* Initialize ADC. The ADC block which can connect to pin 10[6] is selected */
rslt = cyhal_adc_init(&adc_obj, P10_6, NULL);
/* Initialize ADC channel, allocate channel number 0 to pin 10[0] as this is the first channel initialized */
const cyhal_adc_channel_config_t channel_config = { .enable_averaging = false, .min_acquisition_ns = 220, .enabled = true };
rslt = cyhal_adc_channel_init_diff(&adc_chan_0_obj, &adc_obj, P10_6, CYHAL_ADC_VNEG, &channel_config);
/* Read the ADC conversion result for corresponding ADC channel. Repeat as necessary. */
printf("adc start\n");
adc_out = cyhal_adc_read(&adc_chan_0_obj);
printf("adc=%ld\n", adc_out);
```
```
int32_t cyhal_adc_read(const cyhal_adc_channel_t *obj)
{
uint32_t old_en_mask = 0u;
if(!obj->adc->continuous_scanning)
{
/* Enable the selected channel only, then perform an on-demand conversion.
* Save the old enabled channel set to restore after we're done */
old_en_mask = SAR_CHAN_EN(obj->adc->base);
Cy_SAR_SetChanMask(obj->adc->base, 1U << obj->channel_idx);
obj->adc->conversion_complete = false;
Cy_SAR_StartConvert(obj->adc->base, CY_SAR_START_CONVERT_SINGLE_SHOT);
}
/* Cy_SAR_IsEndConversion relies on and clears the EOS interrupt status bit.
* We don't know how this read will be used in combination with interrupts,
* so implement our own interrupt-driven EOS flag
*/
while(!obj->adc->conversion_complete) { }
int32_t result = Cy_SAR_GetResult32(obj->adc->base, obj->channel_idx);
if(!obj->adc->continuous_scanning)
{
Cy_SAR_SetChanMask(obj->adc->base, old_en_mask);
}
return result;
}
```
- Labels:
-
ispn:39621:1:0
-
l1:314:1:0
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I tried the same code in 062BLE kit and it seems to work. Let me check in the same hardware and get back to you.
Best Regards,
Vasanth
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
Does it work with FreeRTOS, BLE and another timer interrupt?
If I miss some initialize code for ADC, please let me know.
Regards,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
Ideally it is expected to work. Kindly check example code with HAL ADC (https://github.com/cypresssemiconductorco/mtb-example-psoc6-adc-basic). You can refer ADC initialization part from here.
Best Regards,
Vasanth