From f699b34ad08a039ebb52a6ce7454cbe0323246ad Mon Sep 17 00:00:00 2001 From: NikhitaR-IFX Date: Mon, 31 Jul 2023 18:43:37 +0530 Subject: [PATCH] ports/psoc6: Fixing connect function, adding deinit and fixed tests. Signed-off-by: NikhitaR-IFX --- ports/psoc6/main.c | 2 ++ ports/psoc6/modules/machine/machine_adcblock.c | 17 ++++++++++++++++- ports/psoc6/modules/machine/machine_adcblock.h | 1 + tests/psoc6/adcblock.py | 8 +++----- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/ports/psoc6/main.c b/ports/psoc6/main.c index bcd99e6c5afc..3f5cc997d3c6 100644 --- a/ports/psoc6/main.c +++ b/ports/psoc6/main.c @@ -51,6 +51,7 @@ extern void machine_init(void); extern void machine_deinit(void); extern void network_init(void); extern void network_deinit(void); +extern void adcblock_deinit(void); void mpy_task(void *arg); @@ -170,6 +171,7 @@ void mpy_task(void *arg) { // Deinitialize modules machine_deinit(); + adcblock_deinit(); #if MICROPY_PY_NETWORK mod_network_deinit(); network_deinit(); diff --git a/ports/psoc6/modules/machine/machine_adcblock.c b/ports/psoc6/modules/machine/machine_adcblock.c index 7a9afc3b2430..f573405feda4 100644 --- a/ports/psoc6/modules/machine/machine_adcblock.c +++ b/ports/psoc6/modules/machine/machine_adcblock.c @@ -156,6 +156,7 @@ machine_adcblock_obj_t *adc_block_init_helper(uint8_t adc_id, uint8_t bits) { // No existing adc_block for given id. Create new one. if (adc_block == NULL) { + printf("\nHERE\n"); adc_block = _create_and_init_new_adc_block(adc_id, bits); } @@ -214,8 +215,14 @@ STATIC mp_obj_t machine_adcblock_connect(size_t n_pos_args, const mp_obj_t *pos_ channel = _get_adc_channel_number_for_pin(pin); } } else if (n_pos_args == 3) { - machine_pin_obj_t *adc_pin_obj = MP_OBJ_TO_PTR(pos_args[1]); + channel = mp_obj_get_int(pos_args[1]); + uint32_t exp_pin = _get_adc_pin_number_for_channel(channel); + + machine_pin_obj_t *adc_pin_obj = MP_OBJ_TO_PTR(pos_args[2]); pin = adc_pin_obj->pin_addr; + if (exp_pin != pin) { + mp_raise_TypeError(MP_ERROR_TEXT("Wrong pin specified for the mentioned channel")); + } } else { mp_raise_TypeError(MP_ERROR_TEXT("Too many positional args")); } @@ -229,6 +236,14 @@ STATIC mp_obj_t machine_adcblock_connect(size_t n_pos_args, const mp_obj_t *pos_ } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_adcblock_connect_obj, 2, machine_adcblock_connect); +void adcblock_deinit(void) { + + for (uint8_t i = 0; i < MAX_BLOCKS; i++) { + cyhal_adc_free(&adc_block[i]->adc_block_obj); + adc_block[i] = NULL; + } + +} STATIC const mp_rom_map_elem_t machine_adcblock_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&machine_adcblock_connect_obj) }, diff --git a/ports/psoc6/modules/machine/machine_adcblock.h b/ports/psoc6/modules/machine/machine_adcblock.h index 5ff77fcd24f6..ca3f33b6aabf 100644 --- a/ports/psoc6/modules/machine/machine_adcblock.h +++ b/ports/psoc6/modules/machine/machine_adcblock.h @@ -30,4 +30,5 @@ typedef struct uint32_t pin; }adc_block_channel_pin_map_t; +void adcblock_deinit(void); #endif // MICROPY_INCLUDED_MACHINE_ADCBLOCK_H diff --git a/tests/psoc6/adcblock.py b/tests/psoc6/adcblock.py index 3d47eb9f52f3..c31eee20f8c3 100644 --- a/tests/psoc6/adcblock.py +++ b/tests/psoc6/adcblock.py @@ -3,9 +3,7 @@ """ from machine import Pin, ADCBlock -print("ADCBlock tests") - -pin = Pin("P10_0") +pin = Pin("P10_3") # Negative tests try: @@ -21,11 +19,11 @@ adcBlock = ADCBlock(0) try: - adcPin = adcBlock.connect(1, pin) + adcPin = adcBlock.connect(0, pin) except: print("TypeError: Wrong pin specified for the mentioned channel") -adcPin = adcBlock.connect(0, pin) +adcPin = adcBlock.connect(3, pin) print(adcPin.read_uv() > 0)