From fb2adbe4b32a694946d2c50c83ba37c50ae430ad Mon Sep 17 00:00:00 2001 From: NikhitaR-IFX Date: Fri, 7 Jul 2023 15:18:43 +0530 Subject: [PATCH] ports/psoc6: Adding ADC and ADCBlock module. Signed-off-by: NikhitaR-IFX --- ports/psoc6/drivers/machine/psoc6_adc.c | 39 +++++++--------- ports/psoc6/modules/machine/machine_adc.c | 45 +++++++++---------- ports/psoc6/modules/machine/machine_adc.h | 2 +- .../psoc6/modules/machine/machine_adcblock.c | 40 ++++++++--------- 4 files changed, 57 insertions(+), 69 deletions(-) diff --git a/ports/psoc6/drivers/machine/psoc6_adc.c b/ports/psoc6/drivers/machine/psoc6_adc.c index 39ba50f01879..8c8e6594ba8b 100644 --- a/ports/psoc6/drivers/machine/psoc6_adc.c +++ b/ports/psoc6/drivers/machine/psoc6_adc.c @@ -10,45 +10,38 @@ #define VPLUS_CHANNEL_0 (P10_0) #define MICRO_TO_MILLI_CONV_RATIO 1000u -cy_rslt_t adc_init(cyhal_adc_t *adc_obj, cyhal_gpio_t pin, const cyhal_clock_t *clk) -{ - return (cyhal_adc_init(adc_obj, pin, NULL)); +cy_rslt_t adc_init(cyhal_adc_t *adc_obj, cyhal_gpio_t pin, const cyhal_clock_t *clk) { + return cyhal_adc_init(adc_obj, pin, NULL); } -cy_rslt_t adc_ch_init(cyhal_adc_channel_t *adc_ch_obj, cyhal_adc_t *adc_obj, cyhal_gpio_t vplus, cyhal_gpio_t vminus, const cyhal_adc_channel_config_t *cfg) -{ - /* ADC channel configuration */ +cy_rslt_t adc_ch_init(cyhal_adc_channel_t *adc_ch_obj, cyhal_adc_t *adc_obj, cyhal_gpio_t vplus, cyhal_gpio_t vminus, const cyhal_adc_channel_config_t *cfg) { + /* ADC channel configuration */ cy_rslt_t rslt; rslt = cyhal_adc_channel_init_diff(adc_ch_obj, adc_obj, VPLUS_CHANNEL_0, - CYHAL_ADC_VNEG, cfg); + CYHAL_ADC_VNEG, cfg); /* Initialize a channel 0 and configure it to scan the channel 0 input pin in single ended mode. */ return rslt; } -cy_rslt_t adc_configure(cyhal_adc_t *adc_obj, const cyhal_adc_config_t *adc_config ) -{ - return (cyhal_adc_configure(adc_obj, adc_config)); +cy_rslt_t adc_configure(cyhal_adc_t *adc_obj, const cyhal_adc_config_t *adc_config) { + return cyhal_adc_configure(adc_obj, adc_config); } -uint16_t adc_read_u16(const cyhal_adc_channel_t *obj) -{ - return (cyhal_adc_read_u16(obj)); +uint16_t adc_read_u16(const cyhal_adc_channel_t *obj) { + return cyhal_adc_read_u16(obj); } -int32_t adc_read(const cyhal_adc_channel_t *obj) -{ - return (cyhal_adc_read(obj)); +int32_t adc_read(const cyhal_adc_channel_t *obj) { + return cyhal_adc_read(obj); } -int32_t adc_read_uv(const cyhal_adc_channel_t *adc_ch_obj) -{ - return (cyhal_adc_read_uv(adc_ch_obj) / MICRO_TO_MILLI_CONV_RATIO) ; +int32_t adc_read_uv(const cyhal_adc_channel_t *adc_ch_obj) { + return cyhal_adc_read_uv(adc_ch_obj) / MICRO_TO_MILLI_CONV_RATIO; } -cy_rslt_t adc_set_sample_rate(cyhal_adc_t *obj, uint32_t desired_sample_rate_hz, uint32_t *achieved_sample_rate_hz) -{ - return (cyhal_adc_set_sample_rate(obj,desired_sample_rate_hz,achieved_sample_rate_hz)); -} +cy_rslt_t adc_set_sample_rate(cyhal_adc_t *obj, uint32_t desired_sample_rate_hz, uint32_t *achieved_sample_rate_hz) { + return cyhal_adc_set_sample_rate(obj, desired_sample_rate_hz, achieved_sample_rate_hz); +} diff --git a/ports/psoc6/modules/machine/machine_adc.c b/ports/psoc6/modules/machine/machine_adc.c index 563fe72734a3..e59ab956bc93 100644 --- a/ports/psoc6/modules/machine/machine_adc.c +++ b/ports/psoc6/modules/machine/machine_adc.c @@ -11,7 +11,7 @@ #define DEFAULT_ADC_ACQ_NS 1000 -#define IS_GPIO_VALID_ADC_PIN(gpio) ((gpio == CYHAL_NC_PIN_VALUE ) || ((gpio >= 80) && (gpio <= 87))) +#define IS_GPIO_VALID_ADC_PIN(gpio) ((gpio == CYHAL_NC_PIN_VALUE) || ((gpio >= 80) && (gpio <= 87))) cyhal_adc_t adc_obj; bool adc_init_flag = false; @@ -22,7 +22,7 @@ bool adc_init_flag = false; const mp_obj_type_t machine_adc_type; -//machine_adc_print() +// 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); mp_printf(print, "", self->adc_pin, self->block->ch, self->sample_ns); @@ -30,48 +30,45 @@ STATIC void machine_adc_print(const mp_print_t *print, mp_obj_t self_in, mp_prin } // ADC initialization helper function -machine_adc_obj_t *adc_init_helper(uint32_t sampling_time, uint32_t pin, uint8_t bits) -{ +machine_adc_obj_t *adc_init_helper(uint32_t sampling_time, uint32_t pin, uint8_t bits) { // Get GPIO and check it has ADC capabilities. cyhal_adc_channel_t adc_channel_obj; - if(!IS_GPIO_VALID_ADC_PIN(pin)) - { + 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) - if(!adc_init_flag) - { + if (!adc_init_flag) { adc_init(&adc_obj, pin, NULL); adc_init_flag = true; } - + // Configure the ADC channel - const cyhal_adc_channel_config_t channel_config = - { - .enable_averaging = false, - .min_acquisition_ns = sampling_time, - .enabled = true - }; - + const cyhal_adc_channel_config_t channel_config = + { + .enable_averaging = false, + .min_acquisition_ns = sampling_time, + .enabled = true + }; + // Intialize channel adc_ch_init(&adc_channel_obj, &adc_obj, pin, CYHAL_NC_PIN_VALUE, &channel_config); - + // Create ADC Object machine_adc_obj_t *o = mp_obj_malloc(machine_adc_obj_t, &machine_adc_type); - + // Create ADCBlock machine_adcblock_obj_t *adc_block = mp_obj_malloc(machine_adcblock_obj_t, &machine_adcblock_type); - + // Intialize ADCBlock adc_block->adc_id = 0; adc_block->bits = DEFAULT_ADC_BITS; adc_block->adc_chan_obj = adc_channel_obj; - + o->adc_pin = pin; o->block = adc_block; o->sample_ns = sampling_time; - return (o); + return o; } // ADC(id) @@ -87,13 +84,13 @@ STATIC mp_obj_t machine_adc_make_new(const mp_obj_type_t *type, size_t n_args, s mp_map_t kw_args; mp_map_init_fixed_table(&kw_args, n_kw, all_args + n_args); mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; - mp_arg_parse_all(n_args-1, all_args + 1, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); + mp_arg_parse_all(n_args - 1, all_args + 1, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); machine_pin_obj_t *adc_pin_obj = MP_OBJ_TO_PTR(all_args[0]); // Get all user inputs 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); return MP_OBJ_FROM_PTR(o); @@ -135,4 +132,4 @@ MP_DEFINE_CONST_OBJ_TYPE( make_new, machine_adc_make_new, print, machine_adc_print, locals_dict, &machine_adc_locals_dict - ); \ No newline at end of file + ); diff --git a/ports/psoc6/modules/machine/machine_adc.h b/ports/psoc6/modules/machine/machine_adc.h index c5c810d3b3ff..61caa4d32019 100644 --- a/ports/psoc6/modules/machine/machine_adc.h +++ b/ports/psoc6/modules/machine/machine_adc.h @@ -10,6 +10,6 @@ typedef struct _machine_adc_obj_t { uint32_t sample_ns; } machine_adc_obj_t; -extern machine_adc_obj_t *adc_init_helper(uint32_t sampling_time, uint32_t pin1,uint8_t bits ); +extern machine_adc_obj_t *adc_init_helper(uint32_t sampling_time, uint32_t pin1, uint8_t bits); #endif // MICROPY_INCLUDED_MACHINE_ADC_H diff --git a/ports/psoc6/modules/machine/machine_adcblock.c b/ports/psoc6/modules/machine/machine_adcblock.c index 8bc0cb26ed89..773b6834d0e2 100644 --- a/ports/psoc6/modules/machine/machine_adcblock.c +++ b/ports/psoc6/modules/machine/machine_adcblock.c @@ -19,14 +19,14 @@ };*/ 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} + {.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) { @@ -36,7 +36,7 @@ STATIC void machine_adcblock_print(const mp_print_t *print, mp_obj_t self_in, mp STATIC mp_obj_t machine_adcblock_make_new(const mp_obj_type_t *type, size_t n_pos_args, size_t n_kw_args, const mp_obj_t *all_args) { mp_arg_check_num(n_pos_args, n_kw_args, 1, MP_OBJ_FUN_ARGS_MAX, true); - + // Get ADC ID uint8_t adc_id = mp_obj_get_int(all_args[0]); if (adc_id != 0) { @@ -53,8 +53,7 @@ STATIC mp_obj_t machine_adcblock_make_new(const mp_obj_type_t *type, size_t n_po mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_pos_args - 1, all_args + 1, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); uint8_t bits = args[ARG_bits].u_int; - if(bits != DEFAULT_ADC_BITS) - { + if (bits != DEFAULT_ADC_BITS) { mp_raise_TypeError(MP_ERROR_TEXT("Invalid bits. Current ADC configuration supports only 12 bits resolution!")); } @@ -74,23 +73,22 @@ STATIC mp_obj_t machine_adcblock_connect(size_t n_pos_args, const mp_obj_t *pos_ uint8_t channel = -1; if (n_pos_args == 2) { // If channel only specified : If mp_obj_is_int is true, then it is channel - if (mp_obj_is_int(pos_args[1])) { + if (mp_obj_is_int(pos_args[1])) { channel = mp_obj_get_int(pos_args[1]); if (channel <= 7) { - //adc_pin - //self->ch = mp_obj_get_int(pos_args[1]); + // adc_pin + // self->ch = mp_obj_get_int(pos_args[1]); self->adc_pin = ch_pin_obj[channel].pin; } } // If Pin only specified else { machine_pin_obj_t *adc_pin_obj = MP_OBJ_TO_PTR(pos_args[1]); - - for(int i=0; ipin_addr) - { - //self->ch = ch_pin_obj[i].ch; + if (ch_pin_obj[i].pin == adc_pin_obj->pin_addr) { + // self->ch = ch_pin_obj[i].ch; self->adc_pin = adc_pin_obj->pin_addr; } } @@ -99,7 +97,7 @@ STATIC mp_obj_t machine_adcblock_connect(size_t n_pos_args, const mp_obj_t *pos_ self->ch = mp_obj_get_int(pos_args[1]); machine_pin_obj_t *adc_pin_obj = MP_OBJ_TO_PTR(pos_args[2]); self->adc_pin = adc_pin_obj->pin_addr; - if (ch_pin_obj[self->ch].pin != self->adc_pin ){ + if (ch_pin_obj[self->ch].pin != self->adc_pin) { mp_raise_TypeError(MP_ERROR_TEXT("Wrong pin specified for the mentioned channel")); } } else { @@ -123,4 +121,4 @@ MP_DEFINE_CONST_OBJ_TYPE( make_new, machine_adcblock_make_new, print, machine_adcblock_print, locals_dict, &machine_adcblock_locals_dict - ); \ No newline at end of file + );