Exploring Cypress ModusToolbox Device Configurator (MTB DC) 2.1
The last two weeks I've spent learning how to use MTB Device Configurator 2.1. In my previous post I've described my initial journey with MTB. I was able to fix Device Configurator 2.1 errors by enabling AREF and populating SAR ADC mandatory parameters. I've configured 12-bit SAR ADC and AREF in Peripherals tab:
As well I connected Thermistor pins to SAR ADC in Pins tab:
And I was able to build the code and deploy it to my board without errors. But I was not able to find the way to initialize and use ADC components produced by the tool.
MTB Debuger
Than I've decided to verify with MTB Debugger if SAR ADC has been initialized successfully.
While I was trying to do it I've got an error Open On-Chip Debugger:
I've reset the board and it started debug process without errors. So I was able to verify that there was no errors in cycfg_peripherals.c init_cycfg_peripherals().
Hardware Abstraction Layer (HAL) ADC
At this point I've realized that I need to move forward as time goes fast. So I switched my focus to a more simple option as it was suggested by saicheong . It is not as versatile and less precise, but allows a simple ADC read from a single channel. I've used a sample code provided in the document. I've added required headers to my Hello World program.
#include "cyhal_hw_types.h" #include "cyhal_adc.h"
and created a function to initialize and read the analog output from the thermistor.
cy_rslt_t rslt; cyhal_adc_t adc_obj; cyhal_adc_channel_t adc_chan_0_obj; void read_my_adc(cy_rslt_t rslt, cyhal_adc_t* adc_obj, cyhal_adc_channel_t* adc_chan_0_obj) { /* 16 bit ADC conversion result */ uint16_t adc_out; /* Initialize ADC. The ADC block which can connect to pin 10[0] is selected */ rslt = cyhal_adc_init(&*adc_obj, P10_1, NULL); /* Check the return of cyhal_adc_init to confirm whether initialization was successful */ if (rslt == CY_RSLT_SUCCESS) { /* Initialize ADC channel, allocate channel number 0 to pin 10[0] as this is the first channel initialized */ rslt = cyhal_adc_channel_init(&*adc_chan_0_obj, &*adc_obj, P10_1); /* Check the return of cyhal_adc_channel_init to confirm whether channel initialization was successful */ if (rslt == CY_RSLT_SUCCESS) { /* Read the 16 bit ADC conversion result for corresponding ADC channel */ adc_out = cyhal_adc_read_u16(&*adc_chan_0_obj); printf("ADC OUT:%i \r\n", adc_out); } /* Release ADC channel object after use */ cyhal_adc_channel_free(&*adc_chan_0_obj); } /* Release ADC object after use */ cyhal_adc_free(&*adc_obj); }
It was giving me an error in the initialization. I've realized that it is conflicting for the same resources as used by the code generated by MTB DC. I've commented section of the code in cycfg.c
#include "cycfg.h" void init_cycfg_all(void) { init_cycfg_system(); init_cycfg_clocks(); init_cycfg_routing(); //TODO fix //init_cycfg_peripherals(); //init_cycfg_pins(); }
After this change I've started getting results from the thermistor:
ADC OUT:19104 ADC OUT:19280 ADC OUT:19232 ADC OUT:65520 ADC OUT:65520 ADC OUT:65520 ADC OUT:19280 ADC OUT:19088 ADC OUT:65520 ADC OUT:65520 ADC OUT:63632 ADC OUT:65520 ADC OUT:65520 ADC OUT:65520 ADC OUT:18896 ADC OUT:4656 ADC OUT:19136
I've tested it by touching the thermistor on the board. When it happened the ADC reading was going from ~19000 to 65520, which is equal to 1111 1111 1111 0000 in a binary format. As ADC is 12 bit, so I assume it reached the maximum value. I think the reason for this is that thermistor provides output in range 0-3.3v and HAL ADC configured to a maximum of 1.2v using internal reference voltage.
Next Steps
I'm going to work to connect ICSG007A ICStation Heartbeat Sensor to the board and start reading it.