Skip to content

Commit

Permalink
ports/psoc6/modules/machine: Renamed functions for consistency.
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 31, 2023
1 parent 2cea77f commit 834c5bd
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 73 deletions.
65 changes: 37 additions & 28 deletions ports/psoc6/modules/machine/machine_adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, "<ADC Pin=%u, ADCBlock_id=%d, sampling_time_ns=%ld>", 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, "<ADC Pin=%u, ADCBlock_id=%d, sampling_time_ns=%ld>", 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
Expand Down
101 changes: 56 additions & 45 deletions ports/psoc6/modules/machine/machine_adcblock.c
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -22,15 +21,20 @@ 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;
}

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) {
Expand All @@ -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++)
{
Expand All @@ -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;
Expand All @@ -65,87 +72,91 @@ 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];
}

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) {
Expand All @@ -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[] = {
Expand All @@ -184,37 +197,35 @@ 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]);
pin = adc_pin_obj->pin_addr;
} 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);

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 @@ -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)
Expand Down

0 comments on commit 834c5bd

Please sign in to comment.