Skip to content

Commit

Permalink
ports/psoc6: Fix ADC read_uv().
Browse files Browse the repository at this point in the history
Signed-off-by: NikhitaR-IFX <Nikhita.Rajasekhar@infineon.com>
  • Loading branch information
NikhitaR-IFX committed Mar 5, 2024
1 parent c72ab50 commit 65978eb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
13 changes: 12 additions & 1 deletion ports/psoc6/machine_adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand All @@ -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) },
Expand Down
14 changes: 14 additions & 0 deletions ports/psoc6/machine_adcblock.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 65978eb

Please sign in to comment.