From 65978eb9859fc37fea77daeee1d8be5a8984c057 Mon Sep 17 00:00:00 2001 From: NikhitaR-IFX Date: Tue, 5 Mar 2024 11:48:12 +0530 Subject: [PATCH] ports/psoc6: Fix ADC read_uv(). Signed-off-by: NikhitaR-IFX --- ports/psoc6/machine_adc.c | 13 ++++++++++++- ports/psoc6/machine_adcblock.c | 14 ++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/ports/psoc6/machine_adc.c b/ports/psoc6/machine_adc.c index 5e57bd94130c..cfce639a1780 100644 --- a/ports/psoc6/machine_adc.c +++ b/ports/psoc6/machine_adc.c @@ -63,6 +63,12 @@ machine_adc_obj_t *machine_adc_make_init(uint32_t sampling_time, mp_obj_t pin_na return adc; } +// Helper function to get resolution +uint8_t adc_get_resolution(machine_adc_obj_t *adc) { + printf("ADC bits: %d", adc->block->bits); + return adc->block->bits; +} + // machine_adc_print() STATIC void machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); @@ -111,7 +117,11 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_block_obj, machine_adc_block); // read_u16() STATIC mp_obj_t machine_adc_read_u16(mp_obj_t self_in) { machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); - return MP_OBJ_NEW_SMALL_INT(cyhal_adc_read_u16(&(self->adc_chan_obj))); + return MP_OBJ_NEW_SMALL_INT(cyhal_adc_read(&(self->adc_chan_obj))); + // !ToDo:This currently return value scaled from 0-2047, while it should be 0-4096. + // Acc to MPY Docs, this should be mapped to range of 0-65535 (using taylow series?) as below. + // mp_int_t bits = (mp_int_t)adc_get_resolution(self); + // mp_uint_t u16 = adc_raw_result << (16 - bits) | adc_raw_result >> (2 * bits - 16); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_u16_obj, machine_adc_read_u16); @@ -122,6 +132,7 @@ STATIC mp_obj_t machine_adc_read_uv(mp_obj_t self_in) { } STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_read_uv_obj, machine_adc_read_uv); + STATIC const mp_rom_map_elem_t machine_adc_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_adc_deinit_obj) }, { MP_ROM_QSTR(MP_QSTR_read_u16), MP_ROM_PTR(&machine_adc_read_u16_obj) }, diff --git a/ports/psoc6/machine_adcblock.c b/ports/psoc6/machine_adcblock.c index 520265225a05..2e354ea02919 100644 --- a/ports/psoc6/machine_adcblock.c +++ b/ports/psoc6/machine_adcblock.c @@ -146,6 +146,20 @@ static void _adc_block_obj_init(machine_adcblock_obj_t *adc_block, uint16_t adc_ if (status != CY_RSLT_SUCCESS) { mp_raise_TypeError(MP_ERROR_TEXT("ADC Initialization failed!")); } + const cyhal_adc_config_t adc_config = { + .continuous_scanning = false, // Continuous Scanning is disabled + .average_count = 1, // Average count disabled + .vref = CYHAL_ADC_REF_VDDA, // VREF for Single ended channel set to VDDA + .vneg = CYHAL_ADC_VNEG_VSSA, // VNEG for Single ended channel set to VSSA + .resolution = 12u, // 12-bit resolution + .ext_vref = NC, // No connection + .bypass_pin = NC + }; // No connection + + status = cyhal_adc_configure(&(adc_block->adc_obj), &adc_config); + if (status != CY_RSLT_SUCCESS) { + mp_raise_msg_varg(&mp_type_ValueError, MP_ERROR_TEXT("ADC configuration update failed. Error: %ld ! \n"), status); + } adc_block->id = adc_block_id; adc_block->bits = bits;