diff --git a/docs/psoc6/quickref.rst b/docs/psoc6/quickref.rst index ee163b4ec2611..7a81c18f63323 100644 --- a/docs/psoc6/quickref.rst +++ b/docs/psoc6/quickref.rst @@ -438,7 +438,7 @@ Use the :ref:`machine.ADC ` class:: adc = ADC(Pin("P10_0")) # create an ADC object on ADC pin val = adc.read_u16() # read a raw analog value in the range 0-65535 - val = adc.read_uv() # read an analog value in micro volts ranging 0.0V - 3.3V + val = adc.read_uv() # read an analog value in micro volts The PSoC6 port also supports :ref:`machine.ADC ` API to have control over the ADC configuration. Currently PSoC6 supports only 1 12-bit SAR ADC with the following channel to pin mapping and the defaults are set accordingly: @@ -469,5 +469,4 @@ To use the APIs: adcBlock = ADCBlock(0, bits=12) # create an ADCBlock 0 object adc = adcBlock.connect(0, Pin("P10_0")) # connect channel 0 to pin P10_0 - val = adc.read_u16() # read a raw analog value in the range 0-65535 - val = adc.read_uv() # read an analog value in micro volts ranging 0.0V - 3.3V + val = adc.read_uv() # read an analog value in micro volts diff --git a/ports/psoc6/modules/machine/machine_adc.c b/ports/psoc6/modules/machine/machine_adc.c index e59ab956bc93f..ccc93f25d6d4d 100644 --- a/ports/psoc6/modules/machine/machine_adc.c +++ b/ports/psoc6/modules/machine/machine_adc.c @@ -36,7 +36,7 @@ machine_adc_obj_t *adc_init_helper(uint32_t sampling_time, uint32_t pin, uint8_t if (!IS_GPIO_VALID_ADC_PIN(pin)) { mp_raise_ValueError(MP_ERROR_TEXT("Invalid ADC Pin")); } - // Intialize the ADC block (required only once per execution) + // Initialize the ADC block (required only once per execution) if (!adc_init_flag) { adc_init(&adc_obj, pin, NULL); adc_init_flag = true; @@ -50,7 +50,7 @@ machine_adc_obj_t *adc_init_helper(uint32_t sampling_time, uint32_t pin, uint8_t .enabled = true }; - // Intialize channel + // Initialize channel adc_ch_init(&adc_channel_obj, &adc_obj, pin, CYHAL_NC_PIN_VALUE, &channel_config); // Create ADC Object @@ -59,7 +59,7 @@ machine_adc_obj_t *adc_init_helper(uint32_t sampling_time, uint32_t pin, uint8_t // Create ADCBlock machine_adcblock_obj_t *adc_block = mp_obj_malloc(machine_adcblock_obj_t, &machine_adcblock_type); - // Intialize ADCBlock + // Initialize ADCBlock adc_block->adc_id = 0; adc_block->bits = DEFAULT_ADC_BITS; adc_block->adc_chan_obj = adc_channel_obj; @@ -88,7 +88,7 @@ STATIC mp_obj_t machine_adc_make_new(const mp_obj_type_t *type, size_t n_args, s machine_pin_obj_t *adc_pin_obj = MP_OBJ_TO_PTR(all_args[0]); - // Get all user inputs + // Get user input sampling time uint32_t sampling_time = args[ARG_sample_ns].u_int; machine_adc_obj_t *o = adc_init_helper(sampling_time, adc_pin_obj->pin_addr, DEFAULT_ADC_BITS); @@ -99,7 +99,6 @@ STATIC mp_obj_t machine_adc_make_new(const mp_obj_type_t *type, size_t n_args, s // block() STATIC mp_obj_t machine_adc_block(mp_obj_t self_in) { const machine_adc_obj_t *self = MP_OBJ_TO_PTR(self_in); - printf("\n Block: \n"); return MP_OBJ_FROM_PTR(self->block); } STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_adc_block_obj, machine_adc_block); diff --git a/ports/psoc6/modules/machine/machine_adcblock.c b/ports/psoc6/modules/machine/machine_adcblock.c index 773b6834d0e22..a8ba52f0e7a69 100644 --- a/ports/psoc6/modules/machine/machine_adcblock.c +++ b/ports/psoc6/modules/machine/machine_adcblock.c @@ -7,17 +7,6 @@ #include "pins.h" #include "machine_adc.h" -/*STATIC const machine_adcblock_obj_t machine_adcblock_obj[] = { - {{&machine_adcblock_type}, .adc_id=0, .bits=DEFAULT_ADC_BITS, .ch=0, .adc_pin=PIN_P10_0,{0}}, - {{&machine_adcblock_type}, .adc_id=0, .bits=DEFAULT_ADC_BITS, .ch=1, .adc_pin=PIN_P10_1,{0}}, - {{&machine_adcblock_type}, .adc_id=0, .bits=DEFAULT_ADC_BITS, .ch=2, .adc_pin=PIN_P10_2,{0}}, - {{&machine_adcblock_type}, .adc_id=0, .bits=DEFAULT_ADC_BITS, .ch=3, .adc_pin=PIN_P10_3,{0}}, - {{&machine_adcblock_type}, .adc_id=0, .bits=DEFAULT_ADC_BITS, .ch=4, .adc_pin=PIN_P10_4,{0}}, - {{&machine_adcblock_type}, .adc_id=0, .bits=DEFAULT_ADC_BITS, .ch=5, .adc_pin=PIN_P10_5,{0}}, - {{&machine_adcblock_type}, .adc_id=0, .bits=DEFAULT_ADC_BITS, .ch=6, .adc_pin=PIN_P10_6,{0}}, - {{&machine_adcblock_type}, .adc_id=0, .bits=DEFAULT_ADC_BITS, .ch=7, .adc_pin=PIN_P10_7,{0}}, -};*/ - STATIC const ch_pin_map_t ch_pin_obj[] = { {.ch = 0, .pin = PIN_P10_0}, {.ch = 1, .pin = PIN_P10_1}, @@ -25,8 +14,6 @@ STATIC const ch_pin_map_t ch_pin_obj[] = { {.ch = 3, .pin = PIN_P10_3}, {.ch = 4, .pin = PIN_P10_4}, {.ch = 5, .pin = PIN_P10_5}, - {.ch = 6, .pin = PIN_P10_6}, - {.ch = 7, .pin = PIN_P10_7} }; STATIC void machine_adcblock_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { diff --git a/ports/psoc6/modules/machine/machine_adcblock.h b/ports/psoc6/modules/machine/machine_adcblock.h index 27490f257cbb4..bea8c83b9a12b 100644 --- a/ports/psoc6/modules/machine/machine_adcblock.h +++ b/ports/psoc6/modules/machine/machine_adcblock.h @@ -17,6 +17,7 @@ typedef struct _machine_adcblock_obj_t { typedef struct _ch_pin_map_t { uint8_t ch; uint32_t pin; + bool flag; } ch_pin_map_t; extern void machine_adcblock_init_helper(machine_adcblock_obj_t *self, uint8_t id, uint8_t bits, cyhal_adc_channel_t adc_chan_obj); diff --git a/tests/psoc6/adc.py b/tests/psoc6/adc.py new file mode 100644 index 0000000000000..a78e11875ec85 --- /dev/null +++ b/tests/psoc6/adc.py @@ -0,0 +1,23 @@ +""" +ADC test for the PSoC6 port. +""" + +from machine import ADC, Pin + +adc_pin = Pin("P10_0") +adc_wrong_pin = Pin("P13_7") + +adc = ADC(adc_pin) +print(adc) + +adc = ADC(adc_pin, sample_ns=1200) +print(adc) + +print(adc.read_uv() > 0) +print(adc.read_u16() > 0) + +# Exceptions should be raised +try: + adc = ADC(adc_wrong_pin) +except: + print("Invalid ADC Pin") diff --git a/tests/psoc6/adcblock.py b/tests/psoc6/adcblock.py new file mode 100644 index 0000000000000..e5ee32af99e98 --- /dev/null +++ b/tests/psoc6/adcblock.py @@ -0,0 +1,29 @@ +""" +ADCBlock test for the PSoC6 port. +""" +from machine import ADCBlock, Pin + +pin = Pin("P10_0") + +# Negative tests +try: + adcBlock = ADCBlock(1) +except: + print("TypeError: Specified ADC id not supported. Currently only block 0 is configured!") + +try: + adcBlock = ADCBlock(0, bits=10) +except: + print("TypeError: Invalid bits. Current ADC configuration supports only 12 bits resolution!") + +adcBlock = ADCBlock(0, bits=12) + +try: + adcPin = adcBlock.connect(1, pin) +except: + print("TypeError: Wrong pin specified for the mentioned channel") + + +adcPin = adcBlock.connect(0, pin) + +print(adcPin.read_uv() > 0)