From 834c5bd8c301474e6be7dfc2d8c680951b917dee Mon Sep 17 00:00:00 2001 From: NikhitaR-IFX Date: Mon, 31 Jul 2023 12:53:28 +0530 Subject: [PATCH] ports/psoc6/modules/machine: Renamed functions for consistency. Signed-off-by: NikhitaR-IFX --- ports/psoc6/modules/machine/machine_adc.c | 65 ++++++----- .../psoc6/modules/machine/machine_adcblock.c | 101 ++++++++++-------- .../psoc6/modules/machine/machine_adcblock.h | 1 + 3 files changed, 94 insertions(+), 73 deletions(-) diff --git a/ports/psoc6/modules/machine/machine_adc.c b/ports/psoc6/modules/machine/machine_adc.c index 20cf07c066e9..ae6095e2cce2 100644 --- a/ports/psoc6/modules/machine/machine_adc.c +++ b/ports/psoc6/modules/machine/machine_adc.c @@ -13,72 +13,81 @@ #include "cyhal.h" #include "cy_pdl.h" -#define DEFAULT_ADC_ACQ_NS 1000 - #define IS_GPIO_VALID_ADC_PIN(gpio) ((gpio == CYHAL_NC_PIN_VALUE) || ((gpio >= 80) && (gpio <= 87))) -extern int16_t get_adc_block_id(uint32_t pin); -extern machine_adc_obj_t *adc_block_get_pin_adc_obj(machine_adcblock_obj_t *adc_block, uint32_t pin); +extern int16_t adc_block_get_block_id_for_pin(uint32_t pin); +extern machine_adc_obj_t *adc_block_retrieve_adc_obj_for_pin(machine_adcblock_obj_t *adc_block, uint32_t pin); extern machine_adcblock_obj_t *adc_block_init_helper(uint8_t adc_id, uint8_t bits); -extern machine_adc_obj_t *adc_block_allocate_new_pin(machine_adcblock_obj_t *adc_block, uint32_t pin); +extern machine_adc_obj_t *adc_block_allocate_new_channel_for_pin(machine_adcblock_obj_t *adc_block, uint32_t pin); /******************************************************************************/ // MicroPython bindings for machine.ADC const mp_obj_type_t machine_adc_type; -machine_adc_obj_t *adc_init_new(machine_adcblock_obj_t *adc_block, uint32_t pin, uint32_t sampling_time) { +// Private helper function to check if ADC Block ID is valid +static inline bool _is_gpio_valid_adc_pin(uint32_t pin) { + if ((pin == CYHAL_NC_PIN_VALUE) || ((pin >= 80) && (pin <= 87))) { + return true; + } + + return false; +} + +// Public helper function to handle new ADC channel creation and initialization +machine_adc_obj_t *adc_create_and_init_new_channel_obj(machine_adcblock_obj_t *adc_block, uint32_t pin, uint32_t sampling_time) { - machine_adc_obj_t *o = adc_block_allocate_new_pin(adc_block, pin); + machine_adc_obj_t *adc_channel = adc_block_allocate_new_channel_for_pin(adc_block, pin); const cyhal_adc_channel_config_t channel_config = { .enable_averaging = false, - .min_acquisition_ns = sampling_time, // TODO: if existing, can we change its configuration (sampling rate?) + .min_acquisition_ns = sampling_time, .enabled = true }; - cyhal_adc_channel_init_diff(&(o->adc_chan_obj), &(adc_block->adc_block_obj), pin, CYHAL_ADC_VNEG, &channel_config); - // TODO: handle error return + cy_rslt_t status = cyhal_adc_channel_init_diff(&(adc_channel->adc_chan_obj), &(adc_block->adc_block_obj), pin, CYHAL_ADC_VNEG, &channel_config); + if (status != CY_RSLT_SUCCESS) { + mp_raise_TypeError(MP_ERROR_TEXT("ADC Channel Initialization failed!")); + } - o->pin = pin; - o->block = adc_block; - o->sample_ns = sampling_time; + adc_channel->pin = pin; + adc_channel->block = adc_block; + adc_channel->sample_ns = sampling_time; - return o; + return adc_channel; } -// 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->pin, self->block->id, self->sample_ns); -} - -// ADC initialization helper function +// Main helper function to create and initialize ADC machine_adc_obj_t *adc_init_helper(uint32_t sampling_time, uint32_t pin) { - // Get GPIO and check it has ADC capabilities. - if (!IS_GPIO_VALID_ADC_PIN(pin)) { + if (!_is_gpio_valid_adc_pin(pin)) { mp_raise_ValueError(MP_ERROR_TEXT("Invalid ADC Pin")); } - int16_t adc_block_id = get_adc_block_id(pin); - printf("\n ADC Block ID: %d\n", adc_block_id); + int16_t adc_block_id = adc_block_get_block_id_for_pin(pin); if (adc_block_id == -1) { mp_raise_ValueError(MP_ERROR_TEXT("No associated ADC Block for specified pin!")); } + // Initialize ADC Block machine_adcblock_obj_t *adc_block = adc_block_init_helper(adc_block_id, DEFAULT_ADC_BITS); - printf("\nADC Bits: %d\n", adc_block->bits); - machine_adc_obj_t *o = adc_block_get_pin_adc_obj(adc_block, pin); + // Retrieve associated channel (ADC obj) for pin + machine_adc_obj_t *o = adc_block_retrieve_adc_obj_for_pin(adc_block, pin); if (o == NULL) { - o = adc_init_new(adc_block, pin, sampling_time); + o = adc_create_and_init_new_channel_obj(adc_block, pin, sampling_time); } return o; } +// 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->pin, self->block->id, self->sample_ns); +} + // ADC(id) STATIC mp_obj_t machine_adc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { // Check number of arguments diff --git a/ports/psoc6/modules/machine/machine_adcblock.c b/ports/psoc6/modules/machine/machine_adcblock.c index 6b70bfa087eb..7a9afc3b2430 100644 --- a/ports/psoc6/modules/machine/machine_adcblock.c +++ b/ports/psoc6/modules/machine/machine_adcblock.c @@ -8,8 +8,7 @@ #include "pins.h" extern machine_adc_obj_t *adc_init_helper(uint32_t sampling_time, uint32_t pin); -extern machine_adc_obj_t *adc_init_new(machine_adcblock_obj_t *adc_block, uint32_t pin, uint32_t sampling_time); - +extern machine_adc_obj_t *adc_create_and_init_new_channel_obj(machine_adcblock_obj_t *adc_block, uint32_t pin, uint32_t sampling_time); machine_adcblock_obj_t *adc_block[MAX_BLOCKS] = {NULL}; @@ -22,7 +21,11 @@ const adc_block_channel_pin_map_t adc_block_pin_map[] = { {ADCBLOCK0, 5, PIN_P10_5} }; // will belong to only a particular bsp -static inline bool is_valid_id(uint8_t adc_id) { +/******************************************************************************/ +// MicroPython bindings for machine.ADC + +// Private helper function to check if ADC Block ID is valid +static inline bool _is_valid_id(uint8_t adc_id) { if (adc_id >= MAX_BLOCKS) { return false; } @@ -30,7 +33,8 @@ static inline bool is_valid_id(uint8_t adc_id) { return true; } -static inline machine_adcblock_obj_t *adc_block_get_allocated(uint8_t adc_block_id) { +// Private helper function to allocate ADC Block provided it's ID +static inline machine_adcblock_obj_t *_get_existing_adc_block_by_id(uint8_t adc_block_id) { for (uint8_t i = 0; i < MAX_BLOCKS; i++) { if (adc_block[i] != NULL) { @@ -43,7 +47,8 @@ static inline machine_adcblock_obj_t *adc_block_get_allocated(uint8_t adc_block_ return NULL; } -static inline uint8_t adc_block_get_free_block() { +// Private helper function to get ADC Block not created yet and is supported by hardware - returned as id/index +static inline uint8_t _get_free_adc_block_id() { uint8_t i; for (i = 0; i < MAX_BLOCKS; i++) { @@ -55,7 +60,9 @@ static inline uint8_t adc_block_get_free_block() { return i; } -static inline uint32_t adc_block_get_any_assoc_pin(uint16_t adc_block_id) { + +// Private helper function to get any associated pin to the given ADC Block id +static inline uint32_t _get_any_pin_for_adc_block_id(uint16_t adc_block_id) { for (uint8_t i = 0; i < MAX_CHANNELS; i++) { if (adc_block_pin_map[i].block_id == adc_block_id) { return adc_block_pin_map[i].pin; @@ -65,18 +72,18 @@ static inline uint32_t adc_block_get_any_assoc_pin(uint16_t adc_block_id) { return 0; // there should always an associated pin for block... } -static inline machine_adcblock_obj_t *adc_block_init_new(uint16_t adc_block_id, uint8_t bits) { - uint8_t free_index = adc_block_get_free_block(); - // TODO: Associate id to index to simplify. Block always exists then, only is it inited or not. +// Private helper function to create and initialize new ADC Block +static inline machine_adcblock_obj_t *_create_and_init_new_adc_block(uint16_t adc_block_id, uint8_t bits) { + uint8_t free_index = _get_free_adc_block_id(); if (free_index <= MAX_BLOCKS) { adc_block[free_index] = mp_obj_malloc(machine_adcblock_obj_t, &machine_adcblock_type); adc_block[free_index]->id = adc_block_id; adc_block[free_index]->bits = bits; - cyhal_adc_init(&(adc_block[free_index]->adc_block_obj), adc_block_get_any_assoc_pin(adc_block_id), NULL); - // TODO: handle return of cyhal - - printf("\n Block created! \n"); + cy_rslt_t status = cyhal_adc_init(&(adc_block[free_index]->adc_block_obj), _get_any_pin_for_adc_block_id(adc_block_id), NULL); + if (status != CY_RSLT_SUCCESS) { + mp_raise_TypeError(MP_ERROR_TEXT("ADC Initialization failed!")); + } return adc_block[free_index]; } @@ -84,68 +91,72 @@ static inline machine_adcblock_obj_t *adc_block_init_new(uint16_t adc_block_id, return NULL; } -// Get adc block id associated to input pin -int16_t get_adc_block_id(uint32_t pin) { - // printf("\n Size of adc_block_pin_map: %d", sizeof(adc_block_pin_map)/sizeof(adc_block_pin_map[0])); +// Private helper function to get channel number for provided pin +int16_t _get_adc_channel_number_for_pin(uint32_t pin) { for (int i = 0; i < (sizeof(adc_block_pin_map) / sizeof(adc_block_pin_map[0])); i++) { if (pin == adc_block_pin_map[i].pin) { - return adc_block_pin_map[i].block_id; + return adc_block_pin_map[i].channel; } } return -1; } -// Helper function to get channel number provided the pin is given -int16_t get_adc_channel_number(uint32_t pin) { +// Private helper function to get pin number for provided channel no. +int16_t _get_adc_pin_number_for_channel(uint16_t channel) { for (int i = 0; i < (sizeof(adc_block_pin_map) / sizeof(adc_block_pin_map[0])); i++) { - if (pin == adc_block_pin_map[i].pin) { - return adc_block_pin_map[i].channel; + if (channel == adc_block_pin_map[i].channel) { + return adc_block_pin_map[i].pin; } } return -1; } -// Helper function to get pin number provided the channel is given -int16_t get_adc_pin_number(uint16_t channel) { +// Public helper function to get adc block id associated to input pin +int16_t adc_block_get_block_id_for_pin(uint32_t pin) { for (int i = 0; i < (sizeof(adc_block_pin_map) / sizeof(adc_block_pin_map[0])); i++) { - if (channel == adc_block_pin_map[i].channel) { - return adc_block_pin_map[i].pin; + if (pin == adc_block_pin_map[i].pin) { + return adc_block_pin_map[i].block_id; } } return -1; } -machine_adc_obj_t *adc_block_get_channel_obj(machine_adcblock_obj_t *adc_block, uint8_t channel) { +// Not Used currently +/*machine_adc_obj_t *adc_block_get_channel_obj(machine_adcblock_obj_t *adc_block, uint8_t channel) { return adc_block->channel[channel]; -} +}*/ -machine_adc_obj_t *adc_block_allocate_new_pin(machine_adcblock_obj_t *adc_block, uint32_t pin) { - int16_t adc_channel_no = get_adc_channel_number(pin); +// Public helper function to allocate a new channel for provided pin. +machine_adc_obj_t *adc_block_allocate_new_channel_for_pin(machine_adcblock_obj_t *adc_block, uint32_t pin) { + int16_t adc_channel_no = _get_adc_channel_number_for_pin(pin); adc_block->channel[adc_channel_no] = mp_obj_malloc(machine_adc_obj_t, &machine_adc_type); return adc_block->channel[adc_channel_no]; } -machine_adc_obj_t *adc_block_get_pin_adc_obj(machine_adcblock_obj_t *adc_block, uint32_t pin) { +// Public helper function to retrieve the associated channel (adc object) for given pin +machine_adc_obj_t *adc_block_retrieve_adc_obj_for_pin(machine_adcblock_obj_t *adc_block, uint32_t pin) { - int16_t adc_channel_no = get_adc_channel_number(pin); + int16_t adc_channel_no = _get_adc_channel_number_for_pin(pin); return adc_block->channel[adc_channel_no]; } +// Main helper function to initialize an ADC Block machine_adcblock_obj_t *adc_block_init_helper(uint8_t adc_id, uint8_t bits) { - if (!is_valid_id(adc_id)) { + if (!_is_valid_id(adc_id)) { mp_raise_TypeError(MP_ERROR_TEXT("Specified ADC id not supported")); } - machine_adcblock_obj_t *adc_block = adc_block_get_allocated(adc_id); + machine_adcblock_obj_t *adc_block = _get_existing_adc_block_by_id(adc_id); + // No existing adc_block for given id. Create new one. if (adc_block == NULL) { - adc_block = adc_block_init_new(adc_id, bits); + adc_block = _create_and_init_new_adc_block(adc_id, bits); } if (adc_block == NULL) { @@ -155,20 +166,22 @@ machine_adcblock_obj_t *adc_block_init_helper(uint8_t adc_id, uint8_t bits) { return adc_block; } +// machine_adcblock_print() STATIC void machine_adcblock_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { machine_adcblock_obj_t *self = MP_OBJ_TO_PTR(self_in); mp_printf(print, "ADCBlock(%u, bits=%u)", self->id, self->bits); } +// ADCBlock constructor 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]); - mp_map_t kw_args; mp_map_init_fixed_table(&kw_args, n_kw_args, all_args + n_pos_args); + // Get ADC ID + uint8_t adc_id = mp_obj_get_int(all_args[0]); + // Get ADC bits enum { ARG_bits }; static const mp_arg_t allowed_args[] = { @@ -184,21 +197,21 @@ STATIC mp_obj_t machine_adcblock_make_new(const mp_obj_type_t *type, size_t n_po return MP_OBJ_FROM_PTR(adc_block_init_helper(adc_id, bits)); } +// ADCBlock connect() STATIC mp_obj_t machine_adcblock_connect(size_t n_pos_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { machine_adcblock_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]); uint8_t channel = -1; uint32_t pin = 0; if (n_pos_args == 2) { - // TODO: generalize for (block, channel, pin) structure // If channel only specified : If mp_obj_is_int is true, then it is channel if (mp_obj_is_int(pos_args[1])) { channel = mp_obj_get_int(pos_args[1]); - pin = get_adc_pin_number(channel); + pin = _get_adc_pin_number_for_channel(channel); } else { machine_pin_obj_t *adc_pin_obj = MP_OBJ_TO_PTR(pos_args[1]); pin = adc_pin_obj->pin_addr; - channel = get_adc_channel_number(pin); + 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]); @@ -206,15 +219,13 @@ STATIC mp_obj_t machine_adcblock_connect(size_t n_pos_args, const mp_obj_t *pos_ } else { mp_raise_TypeError(MP_ERROR_TEXT("Too many positional args")); } - // TODO: check if the adc_obj already exists else create new - machine_adc_obj_t *o = adc_block_get_pin_adc_obj(self, pin); + + machine_adc_obj_t *o = adc_block_retrieve_adc_obj_for_pin(self, pin); if (o == NULL) { - o = adc_init_new(self, pin, 1500); + o = adc_create_and_init_new_channel_obj(self, pin, DEFAULT_ADC_ACQ_NS); } return o; - - // TODO: allocate it in the right channel index of the array. } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_adcblock_connect_obj, 2, machine_adcblock_connect); diff --git a/ports/psoc6/modules/machine/machine_adcblock.h b/ports/psoc6/modules/machine/machine_adcblock.h index 17e4820a4335..5ff77fcd24f6 100644 --- a/ports/psoc6/modules/machine/machine_adcblock.h +++ b/ports/psoc6/modules/machine/machine_adcblock.h @@ -4,6 +4,7 @@ // These should go into the specific BSP #define DEFAULT_ADC_BITS 12 #define ADC_BLOCK_CHANNEL_MAX 6 +#define DEFAULT_ADC_ACQ_NS 1000 #define ADCBLOCK0 (0) #define ADCBLOCK_CHANNEL_MAX (1)