Skip to content

Commit

Permalink
ports/psoc6: Finishing ADC and ADCBlock module.
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 Jul 11, 2023
1 parent c558a19 commit 9ac12c2
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 21 deletions.
5 changes: 2 additions & 3 deletions docs/psoc6/quickref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ Use the :ref:`machine.ADC <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 <machine.ADCBlock>` 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:
Expand Down Expand Up @@ -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
9 changes: 4 additions & 5 deletions ports/psoc6/modules/machine/machine_adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
13 changes: 0 additions & 13 deletions ports/psoc6/modules/machine/machine_adcblock.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,13 @@
#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},
{.ch = 2, .pin = PIN_P10_2},
{.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) {
Expand Down
1 change: 1 addition & 0 deletions ports/psoc6/modules/machine/machine_adcblock.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
23 changes: 23 additions & 0 deletions tests/psoc6/adc.py
Original file line number Diff line number Diff line change
@@ -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")
29 changes: 29 additions & 0 deletions tests/psoc6/adcblock.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 9ac12c2

Please sign in to comment.