Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static alloc RFC #2386

Closed
wants to merge 11 commits into from
32 changes: 29 additions & 3 deletions drivers/accel/adxl355/adxl355.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@
/******************************************************************************/
/************************ Variable Declarations ******************************/
/******************************************************************************/
#if CONFIG_DYNAMIC_ALLOC == 0

#ifndef CONFIG_ADXL355_INSTANCES
#define CONFIG_ADXL355_INSTANCES 1
#endif

#endif

static struct no_os_spi_desc adxl355_spi[CONFIG_ADXL355_INSTANCES];
static struct no_os_i2c_desc adxl355_i2c[CONFIG_ADXL355_INSTANCES];
static uint32_t adxl355_index;

static uint8_t shadow_reg_val[5] = {0, 0, 0, 0, 0};
static const uint8_t adxl355_scale_mul[4] = {0, 1, 2, 4};
static const uint8_t adxl355_part_id[] = {
Expand Down Expand Up @@ -150,10 +162,21 @@ int adxl355_init(struct adxl355_dev **device,
return -EINVAL;
}

dev = (struct adxl355_dev *)no_os_calloc(1, sizeof(*dev));
if (CONFIG_DYNAMIC_ALLOC){
dev = (struct adxl355_dev *)no_os_calloc(1, sizeof(*dev));

if (!dev)
return -ENOMEM;
if (!dev)
return -ENOMEM;
} else {
if (adxl355_index >= CONFIG_ADXL355_INSTANCES)
return -ENOMEM;

dev = *device;
if (init_param.comm_type == ADXL355_SPI_COMM)
dev->com_desc.spi_desc = &adxl355_spi[adxl355_index];
else
dev->com_desc.i2c_desc = &adxl355_i2c[adxl355_index];
}

dev->comm_type = init_param.comm_type;

Expand Down Expand Up @@ -207,6 +230,9 @@ int adxl355_init(struct adxl355_dev **device,

*device = dev;

if (!CONFIG_DYNAMIC_ALLOC)
adxl355_index++;

return ret;
error_com:
if (dev->comm_type == ADXL355_SPI_COMM)
Expand Down
34 changes: 30 additions & 4 deletions drivers/api/no_os_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,25 @@
*/
static void *i2c_table[I2C_MAX_BUS_NUMBER + 1];

#if CONFIG_DYNAMIC_ALLOC == 0

#ifndef CONFIG_I2C_INSTANCES
#define CONFIG_I2C_INSTANCES 5
#endif

#else

#ifndef CONFIG_I2C_INSTANCES
#define CONFIG_I2C_INSTANCES 0
#endif

#endif

static struct no_os_i2c_desc i2c_desc[CONFIG_I2C_INSTANCES];
static struct no_os_i2cbus_desc *i2c_bus[I2C_MAX_BUS_NUMBER + 1];
static uint32_t i2c_bus_index;
static uint32_t i2c_desc_index;

/**
* @brief Initialize the I2C communication peripheral.
* @param desc - The I2C descriptor.
Expand All @@ -63,14 +82,15 @@ int32_t no_os_i2c_init(struct no_os_i2c_desc **desc,
return -EINVAL;
// Initializing BUS descriptor
if (i2c_table[param->device_id] == NULL) {
ret = no_os_i2cbus_init(param);
ret = no_os_i2cbus_init(*desc, param);
if (ret)
return ret;
}
// Initilize I2C descriptor
ret = param->platform_ops->i2c_ops_init(desc, param);
if (ret)
return ret;

(*desc)->bus = i2c_table[param->device_id];
(*desc)->bus->slave_number++;
(*desc)->platform_ops = param->platform_ops;
Expand All @@ -83,10 +103,16 @@ int32_t no_os_i2c_init(struct no_os_i2c_desc **desc,
* @param param - The structure that containes the i2c bus parameters
* @return 0 in case of success, error code otherwise
*/
int32_t no_os_i2cbus_init(const struct no_os_i2c_init_param *param)
int32_t no_os_i2cbus_init(struct no_os_i2c_desc *desc, const struct no_os_i2c_init_param *param)
{
struct no_os_i2cbus_desc *bus = (struct no_os_i2cbus_desc *)no_os_calloc(1,
sizeof(struct no_os_i2cbus_desc));
struct no_os_i2cbus_desc *bus;

if (CONFIG_DYNAMIC_ALLOC){
bus = (struct no_os_i2cbus_desc *)no_os_calloc(1,
sizeof(struct no_os_i2cbus_desc));
} else {
bus = desc->bus;
}

if (!bus)
return -ENOMEM;
Expand Down
10 changes: 10 additions & 0 deletions drivers/platform/maxim/max32650/maxim_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
#define MAX_DELAY_SCLK 255
#define NS_PER_US 1000

#if CONFIG_DYNAMIC_ALLOC == 0
static struct max_spi_state max_spi_state[CONFIG_SPI_NUM_DEVS];
static uint32_t spi_index;
#endif

/******************************************************************************/
/************************ Functions Definitions *******************************/
/******************************************************************************/
Expand Down Expand Up @@ -388,6 +393,7 @@ int32_t max_spi_init(struct no_os_spi_desc **desc,
if (!param || !param->extra)
return -EINVAL;

#if CONFIG_DYNAMIC_ALLOC == 1
descriptor = no_os_calloc(1, sizeof(*descriptor));
if (!descriptor)
return -ENOMEM;
Expand All @@ -397,6 +403,10 @@ int32_t max_spi_init(struct no_os_spi_desc **desc,
ret = -ENOMEM;
goto err;
}
#else
descriptor = *desc;
st = &max_spi_state[spi_index++];
#endif

eparam = param->extra;
descriptor->device_id = param->device_id;
Expand Down
117 changes: 89 additions & 28 deletions drivers/platform/maxim/max32690/maxim_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,31 @@

#define I2C_MASTER_MODE 1

#define MAX_I2C_CLK(id) ((id == 0) ? MXC_SYS_PERIPH_CLOCK_I2C0 : \
(id == 1) ? MXC_SYS_PERIPH_CLOCK_I2C1: \
(id == 2) ? MXC_SYS_PERIPH_CLOCK_I2C2: 0)

#if CONFIG_DYNAMIC_ALLOC == 0

#ifndef CONFIG_I2C_INSTANCES
#define CONFIG_I2C_INSTANCES 5
#endif

#ifndef CONFIG_MAX_I2C_XFER_SIZE
#define CONFIG_MAX_I2C_XFER_SIZE 32
#endif

#else

#define CONFIG_I2C_INSTANCES 0
#define CONFIG_MAX_I2C_XFER_SIZE 0

#endif

static struct max_i2c_extra max_i2c_extra[CONFIG_I2C_INSTANCES];
static uint8_t max_i2c_data[CONFIG_I2C_INSTANCES][CONFIG_MAX_I2C_XFER_SIZE];
static uint32_t max_i2c_index;

/** Used to know how many instances are created */
static uint32_t nb_created_desc[MXC_I2C_INSTANCES];

Expand Down Expand Up @@ -78,11 +103,20 @@ void I2C2_IRQHandler(void)
* @param vssel - the VDDIO level.
* @return 0 in case of success, -EINVAL otherwise.
*/
static int32_t _max_i2c_pins_config(uint32_t device_id, mxc_gpio_vssel_t vssel)
static int32_t _max_i2c_pins_config(struct no_os_i2c_init_param *param,
mxc_gpio_vssel_t vssel)
{
struct max_i2c_init_param *max_param = param->extra;
mxc_gpio_cfg_t i2c_pins;

switch (device_id) {
if (max_param->sda_pinctrl.port && max_param->scl_pinctrl.port) {
MXC_GPIO_Config(&max_param->sda_pinctrl);
MXC_GPIO_Config(&max_param->scl_pinctrl);

return 0;
}

switch (param->device_id) {
case 0:
i2c_pins = gpio_cfg_i2c0;
break;
Expand Down Expand Up @@ -127,14 +161,21 @@ static int32_t max_i2c_init(struct no_os_i2c_desc **desc,
if (param->device_id >= MXC_I2C_INSTANCES)
return -EINVAL;

*desc = no_os_calloc(1, sizeof(**desc));
if (!(*desc))
return -ENOMEM;
if (CONFIG_DYNAMIC_ALLOC){
*desc = no_os_calloc(1, sizeof(**desc));
if (!(*desc))
return -ENOMEM;

max_i2c = no_os_calloc(1, sizeof(*max_i2c));
if (!max_i2c) {
ret = -ENOMEM;
goto error_desc;
}
} else {
if (max_i2c_index >= CONFIG_I2C_INSTANCES)
return -ENOMEM;

max_i2c = no_os_calloc(1, sizeof(*max_i2c));
if (!max_i2c) {
ret = -ENOMEM;
goto error_desc;
max_i2c = (*desc)->extra;
}

eparam = param->extra;
Expand All @@ -156,19 +197,20 @@ static int32_t max_i2c_init(struct no_os_i2c_desc **desc,
(*desc)->slave_address = param->slave_address;
(*desc)->extra = max_i2c;

ret = _max_i2c_pins_config(param, eparam->vssel);
if (ret)
return ret;

if (nb_created_desc[param->device_id] == 0) {
MXC_I2C_Shutdown(max_i2c->handler);
MXC_SYS_ClockEnable(MAX_I2C_CLK(param->device_id));
/** The last parameter (slave address) is ignored in master mode */
if((MXC_I2C_Init(i2c_regs, I2C_MASTER_MODE, 0)) != E_NO_ERROR) {
if((MXC_I2C_RevA_Init(i2c_regs, I2C_MASTER_MODE, 0)) != E_NO_ERROR) {
ret = -1;
goto error_extra;
}
}

ret = _max_i2c_pins_config((*desc)->device_id, eparam->vssel);
if (ret)
return ret;

freq = MXC_I2C_GetFrequency(i2c_regs);
freq = no_os_min(freq, (*desc)->max_speed_hz);
ret = MXC_I2C_SetFrequency(i2c_regs, freq);
Expand All @@ -177,7 +219,11 @@ static int32_t max_i2c_init(struct no_os_i2c_desc **desc,

nb_created_desc[param->device_id]++;

if (!CONFIG_DYNAMIC_ALLOC)
max_i2c_index++;

return 0;

error_extra:
no_os_free(max_i2c);
error_desc:
Expand Down Expand Up @@ -207,8 +253,13 @@ static int32_t max_i2c_remove(struct no_os_i2c_desc *desc)
NVIC_DisableIRQ(MXC_I2C_GET_IRQ(desc->device_id));
}

no_os_free(max_i2c);
no_os_free(desc);
if (CONFIG_DYNAMIC_ALLOC){
no_os_free(max_i2c);
no_os_free(desc);
} else {
if (max_i2c_index)
max_i2c_index--;
}

return 0;
}
Expand Down Expand Up @@ -241,17 +292,22 @@ static int32_t max_i2c_write(struct no_os_i2c_desc *desc,

if (stop_bit == 0) {
max_i2c_desc->prologue_size = bytes_number;
if (max_i2c_desc->prologue_data) {
ptr = realloc(max_i2c_desc->prologue_data, bytes_number);
max_i2c_desc->prologue_data = ptr;
if (CONFIG_DYNAMIC_ALLOC){
if (max_i2c_desc->prologue_data) {
ptr = realloc(max_i2c_desc->prologue_data, bytes_number);
max_i2c_desc->prologue_data = ptr;
} else {
max_i2c_desc->prologue_data = malloc(bytes_number);
if (!max_i2c_desc->prologue_data)
return -ENOMEM;
}
} else {
max_i2c_desc->prologue_data = malloc(bytes_number);
if (!max_i2c_desc->prologue_data)
if (bytes_number > CONFIG_MAX_I2C_XFER_SIZE)
return -ENOMEM;
}
memcpy(max_i2c_desc->prologue_data, data, bytes_number);
memcpy(max_i2c_desc->prologue_data, data, bytes_number);

return 0;
return 0;
}
}

req.i2c = i2c_regs;
Expand Down Expand Up @@ -308,10 +364,15 @@ static int32_t max_i2c_read(struct no_os_i2c_desc *desc,

ret = MXC_I2C_MasterTransaction(&req);

if (max_i2c_desc->prologue_size != 0) {
no_os_free(max_i2c_desc->prologue_data);
max_i2c_desc->prologue_size = 0;
max_i2c_desc->prologue_data = NULL;
if (CONFIG_DYNAMIC_ALLOC){
if (max_i2c_desc->prologue_size != 0) {
no_os_free(max_i2c_desc->prologue_data);
max_i2c_desc->prologue_size = 0;
max_i2c_desc->prologue_data = NULL;
}
} else {
if (max_i2c_desc->prologue_size != 0)
max_i2c_desc->prologue_size = 0;
}

return ret;
Expand Down
14 changes: 14 additions & 0 deletions drivers/platform/maxim/max32690/maxim_i2c.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
(i) == 2 ? MXC_I2C2 : 0)
#endif

#define MAX_GPIO_PINCTRL(port, pin, func, pad, vssel) \
{MXC_GPIO##port, MXC_GPIO_PIN_##pin, MXC_GPIO_FUNC_ALT##func, pad, vssel, MXC_GPIO_DRVSTR_0}

/**
* @struct max_i2c_extra
* @brief MAXIM specific I2C handler structure
Expand All @@ -63,6 +66,8 @@ struct max_i2c_extra {

struct max_i2c_init_param {
mxc_gpio_vssel_t vssel;
mxc_gpio_cfg_t sda_pinctrl;
mxc_gpio_cfg_t scl_pinctrl;
};

/**
Expand All @@ -81,4 +86,13 @@ enum max_i2c_speed {
*/
extern const struct no_os_i2c_platform_ops max_i2c_ops;

#if CONFIG_DYNAMIC_ALLOC == 0

#define NO_OS_PLATFORM_I2C_PRIV \
&(struct max_i2c_extra){ \
.prologue_data = (int[32]){} \
}

#endif

#endif /* MAXIM_I2C_H_ */
Loading