rm _MAX enum

support default config and merge unused config
This commit is contained in:
lyon 2022-11-21 20:49:43 +08:00
parent 7e3c43dfa9
commit 89e00e2043
6 changed files with 376 additions and 122 deletions

View File

@ -9,6 +9,9 @@ static const pika_dev_impl pika_dev_impl_list[] = {
#include "pika_hal_table.h"
};
#define _PIKA_DEV_TYPE_MAX \
(sizeof pika_dev_impl_list / sizeof pika_dev_impl_list[0])
static pika_dev_impl* _pika_dev_get_impl(PIKA_HAL_DEV_TYPE type) {
if (type >= _PIKA_DEV_TYPE_MAX) {
return NULL;
@ -16,39 +19,61 @@ static pika_dev_impl* _pika_dev_get_impl(PIKA_HAL_DEV_TYPE type) {
return (pika_dev_impl*)&pika_dev_impl_list[type];
}
static size_t _pika_hal_dev_config_size(PIKA_HAL_DEV_TYPE dev_type) {
#define PIKA_HAL_TABLE_DEV_CONFIG_SIZE
#include "pika_hal_table.h"
return 0;
}
pika_dev* pika_hal_open(PIKA_HAL_DEV_TYPE dev_type, char* name) {
int ret = -1;
if (dev_type >= _PIKA_DEV_TYPE_MAX) {
__platform_printf("Error: dev_type invalied.\r\n");
return NULL;
goto __exit;
}
pika_dev_impl* impl = _pika_dev_get_impl(dev_type);
if (impl->open == NULL) {
__platform_printf("Error: dev_open_impl not implamented.\r\n");
return NULL;
}
pika_dev* dev = (pika_dev*)pikaMalloc(sizeof(pika_dev));
if (dev == NULL) {
return NULL;
goto __exit;
}
dev->type = dev_type;
if (impl->open(dev, name) != 0) {
pikaFree(dev, sizeof(pika_dev));
__platform_printf("Error: dev_open failed.\r\n");
return NULL;
dev->ioctl_config = pikaMalloc(_pika_hal_dev_config_size(dev_type));
if (dev->ioctl_config == NULL) {
goto __exit;
}
return dev;
ret = impl->open(dev, name);
__exit:
if (0 == ret) {
return dev;
}
/* error */
__platform_printf("Error: dev_open failed.\r\n");
if (dev->ioctl_config) {
pikaFree(dev->ioctl_config, _pika_hal_dev_config_size(dev_type));
}
if (dev) {
pikaFree(dev, sizeof(pika_dev));
}
return NULL;
}
int pika_hal_close(pika_dev* dev) {
int ret = -1;
if (dev == NULL) {
return -1;
goto __exit;
}
pika_dev_impl* impl = _pika_dev_get_impl(dev->type);
if (impl->close == NULL) {
return -1;
goto __exit;
}
ret = impl->close(dev);
__exit:
if (NULL != dev) {
pikaFree(dev, sizeof(pika_dev));
}
if (NULL != dev->ioctl_config) {
pikaFree(dev->ioctl_config, _pika_hal_dev_config_size(dev->type));
}
int ret = impl->close(dev);
pikaFree(dev, sizeof(pika_dev));
return ret;
}
@ -74,20 +99,30 @@ int pika_hal_write(pika_dev* dev, void* buf, size_t len) {
return impl->write(dev, buf, len);
}
static const int _pika_hal_cmd_arg_cnt[_PIKA_HAL_IOCTL_MAX] = {
[PIKA_HAL_IOCTL_ENABLE] = 0,
static const int _pika_hal_cmd_arg_cnt[] = {
[PIKA_HAL_IOCTL_CONFIG] = 1,
[PIKA_HAL_IOCTL_ENABLE] = 0,
[PIKA_HAL_IOCTL_DISABLE] = 0,
};
#define _PIKA_HAL_CMD_ARG_CNT_MAX \
(sizeof _pika_hal_cmd_arg_cnt / sizeof _pika_hal_cmd_arg_cnt[0])
static int _pika_hal_get_arg_cnt(PIKA_HAL_IOCTL_CMD cmd) {
if (cmd >= _PIKA_HAL_IOCTL_MAX) {
if (cmd >= _PIKA_HAL_CMD_ARG_CNT_MAX) {
return -1;
}
return _pika_hal_cmd_arg_cnt[cmd];
}
int _pika_hal_ioctl_merge_config(pika_dev* dev, void* config_in) {
#define PIKA_HAL_TABLE_IOCTL_MERGE_CONFIG
#include "pika_hal_table.h"
return -1;
}
int pika_hal_ioctl(pika_dev* dev, PIKA_HAL_IOCTL_CMD cmd, ...) {
int ret = -1;
if (dev == NULL) {
return -1;
}
@ -99,10 +134,82 @@ int pika_hal_ioctl(pika_dev* dev, PIKA_HAL_IOCTL_CMD cmd, ...) {
if (impl->ioctl == NULL) {
return -1;
}
va_list args;
va_start(args, arg_cnt);
uintptr_t arg = va_arg(args, uintptr_t);
int ret = impl->ioctl(dev, cmd, arg);
va_end(args);
void* config_in = NULL;
if (arg_cnt != 0) {
va_list args;
va_start(args, arg_cnt);
config_in = va_arg(args, void*);
ret = _pika_hal_ioctl_merge_config(dev, config_in);
va_end(args);
}
if (0 != ret) {
return ret;
}
ret = impl->ioctl(dev, cmd, dev->ioctl_config);
return ret;
}
#define _ioctl_config_set_default(item, default) \
if (src->item == 0) { \
if (dst->item == 0) { \
/* use default value */ \
dst->item = default; \
} else { \
/* keep exist value */ \
} \
} else { \
/* use input value */ \
src->item = dst->item; \
}
int pika_hal_GPIO_ioctl_merge_config(pika_hal_GPIO_config* dst,
pika_hal_GPIO_config* src) {
_ioctl_config_set_default(dir, PIKA_HAL_GPIO_DIR_IN);
_ioctl_config_set_default(pull, PIKA_HAL_GPIO_PULL_NONE);
_ioctl_config_set_default(speed, PIKA_HAL_GPIO_SPEED_10M);
_ioctl_config_set_default(event_callback_rising, NULL);
_ioctl_config_set_default(event_callback_falling, NULL);
return 0;
}
int pika_hal_UART_ioctl_merge_config(pika_hal_UART_config* dst,
pika_hal_UART_config* src) {
_ioctl_config_set_default(baudrate, PIKA_HAL_UART_BAUDRATE_115200);
_ioctl_config_set_default(data_bits, PIKA_HAL_UART_DATA_BITS_8);
_ioctl_config_set_default(stop_bits, PIKA_HAL_UART_STOP_BITS_1);
_ioctl_config_set_default(parity, PIKA_HAL_UART_PARITY_NONE);
_ioctl_config_set_default(event_callback_rx, NULL);
return 0;
}
int pika_hal_SPI_ioctl_merge_config(pika_hal_SPI_config* dst,
pika_hal_SPI_config* src) {
_ioctl_config_set_default(lsb_or_msb, PIKA_HAL_SPI_MSB);
_ioctl_config_set_default(master_or_slave, PIKA_HAL_SPI_MASTER);
_ioctl_config_set_default(mode, PIKA_HAL_SPI_MODE_0);
_ioctl_config_set_default(data_width, PIKA_HAL_SPI_DATA_WIDTH_8);
_ioctl_config_set_default(speed, PIKA_HAL_SPI_SPEED_10M);
return 0;
}
int pika_hal_IIC_ioctl_merge_config(pika_hal_IIC_config* dst,
pika_hal_IIC_config* src) {
_ioctl_config_set_default(addr, 0);
_ioctl_config_set_default(speed, PIKA_HAL_IIC_SPEED_100K);
return 0;
}
int pika_hal_PWM_ioctl_merge_config(pika_hal_PWM_config* dst,
pika_hal_PWM_config* src) {
_ioctl_config_set_default(channel, PIKA_HAL_PWM_CHANNEL_0);
_ioctl_config_set_default(period, PIKA_HAL_PWM_PERIOD_1MS * 10);
_ioctl_config_set_default(duty, PIKA_HAL_PWM_PERIOD_1MS * 5);
return 0;
}
int pika_hal_ADC_ioctl_merge_config(pika_hal_ADC_config* dst,
pika_hal_ADC_config* src) {
_ioctl_config_set_default(channel, PIKA_HAL_ADC_CHANNEL_0);
_ioctl_config_set_default(resolution, PIKA_HAL_ADC_RESOLUTION_12);
return 0;
}

View File

@ -11,11 +11,11 @@
typedef enum {
#define PIKA_HAL_TABLE_DEV_TYPE
#include "pika_hal_table.h"
_PIKA_DEV_TYPE_MAX,
} PIKA_HAL_DEV_TYPE;
typedef struct {
PIKA_HAL_DEV_TYPE type;
void* ioctl_config;
void* platform_data;
} pika_dev;
@ -23,7 +23,6 @@ typedef enum {
PIKA_HAL_IOCTL_CONFIG,
PIKA_HAL_IOCTL_ENABLE,
PIKA_HAL_IOCTL_DISABLE,
_PIKA_HAL_IOCTL_MAX,
} PIKA_HAL_IOCTL_CMD;
/* posix file like API */
@ -37,7 +36,6 @@ typedef enum {
_PIKA_HAL_GPIO_DIR_UNUSED = 0,
PIKA_HAL_GPIO_DIR_IN,
PIKA_HAL_GPIO_DIR_OUT,
_PIKA_HAL_GPIO_DIR_MAX,
} PIKA_HAL_GPIO_DIR;
typedef enum {
@ -45,22 +43,25 @@ typedef enum {
PIKA_HAL_GPIO_PULL_NONE,
PIKA_HAL_GPIO_PULL_UP,
PIKA_HAL_GPIO_PULL_DOWN,
_PIKA_HAL_GPIO_PULL_MAX,
} PIKA_HAL_GPIO_PULL;
typedef enum {
_PIKA_HAL_GPIO_SPEED_UNUSED = 0,
PIKA_HAL_GPIO_SPEED_LOW,
PIKA_HAL_GPIO_SPEED_HIGH,
_PIKA_HAL_GPIO_SPEED_MAX,
PIKA_HAL_GPIO_SPEED_1M = 1000000,
PIKA_HAL_GPIO_SPEED_2M = 2000000,
PIKA_HAL_GPIO_SPEED_5M = 5000000,
PIKA_HAL_GPIO_SPEED_10M = 10000000,
PIKA_HAL_GPIO_SPEED_20M = 20000000,
PIKA_HAL_GPIO_SPEED_50M = 50000000,
PIKA_HAL_GPIO_SPEED_100M = 100000000,
} PIKA_HAL_GPIO_SPEED;
typedef struct {
PIKA_HAL_GPIO_DIR dir;
PIKA_HAL_GPIO_PULL pull;
PIKA_HAL_GPIO_SPEED speed;
void (*evnet_callback_rising)(pika_dev* dev);
void (*evnet_callback_falling)(pika_dev* dev);
void (*event_callback_rising)(pika_dev* dev);
void (*event_callback_falling)(pika_dev* dev);
} pika_hal_GPIO_config;
typedef enum {
@ -93,7 +94,6 @@ typedef enum {
PIKA_HAL_UART_PARITY_NONE,
PIKA_HAL_UART_PARITY_ODD,
PIKA_HAL_UART_PARITY_EVEN,
_PIKA_HAL_UART_PARITY_MAX,
} PIKA_HAL_UART_PARITY;
typedef struct {
@ -101,16 +101,16 @@ typedef struct {
PIKA_HAL_UART_DATA_BITS data_bits;
PIKA_HAL_UART_STOP_BITS stop_bits;
PIKA_HAL_UART_PARITY parity;
void (*evnet_callback_rx)(pika_dev* dev);
void (*event_callback_rx)(pika_dev* dev);
} pika_hal_UART_config;
typedef uint32_t PIKA_HAL_IIC_SLAVE_ADDR;
typedef enum {
_PIKA_HAL_IIC_SPEED_UNUSED = 0,
PIKA_HAL_IIC_SPEED_LOW,
PIKA_HAL_IIC_SPEED_HIGH,
_PIKA_HAL_IIC_SPEED_MAX,
PIKA_HAL_IIC_SPEED_100K = 100000,
PIKA_HAL_IIC_SPEED_400K = 400000,
PIKA_HAL_IIC_SPEED_1M = 1000000,
} PIKA_HAL_IIC_SPEED;
typedef struct {
@ -122,14 +122,12 @@ typedef enum {
_PIKA_HAL_SPI_LSB_OR_MSB_UNUSED = 0,
PIKA_HAL_SPI_LSB,
PIKA_HAL_SPI_MSB,
_PIKA_HAL_SPI_LSB_OR_MSB_MAX,
} PIKA_HAL_SPI_LSB_OR_MSB;
typedef enum {
_PIKA_HAL_SPI_MASTER_OR_SLAVE_UNUSED = 0,
PIKA_HAL_SPI_MASTER,
PIKA_HAL_SPI_SLAVE,
_PIKA_HAL_SPI_MASTER_OR_SLAVE_MAX,
} PIKA_HAL_SPI_MASTER_OR_SLAVE;
typedef enum {
@ -138,20 +136,23 @@ typedef enum {
PIKA_HAL_SPI_MODE_1,
PIKA_HAL_SPI_MODE_2,
PIKA_HAL_SPI_MODE_3,
_PIKA_HAL_SPI_MODE_MAX,
} PIKA_HAL_SPI_MODE;
typedef enum {
_PIKA_HAL_SPI_DATA_UNUSED = 0,
PIKA_HAL_SPI_DATA_WIDTH_8BIT = 8,
PIKA_HAL_SPI_DATA_WIDTH_16BIT = 16,
PIKA_HAL_SPI_DATA_WIDTH_8 = 8,
PIKA_HAL_SPI_DATA_WIDTH_16 = 16,
} PIKA_HAL_SPI_DATA_WIDTH;
typedef enum {
_PIKA_HAL_SPI_SPEED_UNUSED = 0,
PIKA_HAL_SPI_SPEED_LOW,
PIKA_HAL_SPI_SPEED_HIGH,
_PIKA_HAL_SPI_SPEED_MAX,
PIKA_HAL_SPI_SPEED_1M = 1000000,
PIKA_HAL_SPI_SPEED_2M = 2000000,
PIKA_HAL_SPI_SPEED_5M = 5000000,
PIKA_HAL_SPI_SPEED_10M = 10000000,
PIKA_HAL_SPI_SPEED_20M = 20000000,
PIKA_HAL_SPI_SPEED_50M = 50000000,
PIKA_HAL_SPI_SPEED_100M = 100000000,
} PIKA_HAL_SPI_SPEED;
typedef struct {
@ -174,29 +175,20 @@ typedef enum {
PIKA_HAL_ADC_CHANNEL_7,
PIKA_HAL_ADC_CHANNEL_TEMP,
PIKA_HAL_ADC_CHANNEL_VBAT,
_PIKA_HAL_ADC_CHANNEL_MAX,
} PIKA_HAL_ADC_CHANNEL;
typedef enum {
_PIKA_HAL_ADC_RESOLUTION_UNUSED = 0,
PIKA_HAL_ADC_RESOLUTION_8BIT = 8,
PIKA_HAL_ADC_RESOLUTION_10BIT = 10,
PIKA_HAL_ADC_RESOLUTION_12BIT = 12,
PIKA_HAL_ADC_RESOLUTION_14BIT = 14,
PIKA_HAL_ADC_RESOLUTION_16BIT = 16,
PIKA_HAL_ADC_RESOLUTION_8 = 8,
PIKA_HAL_ADC_RESOLUTION_10 = 10,
PIKA_HAL_ADC_RESOLUTION_12 = 12,
PIKA_HAL_ADC_RESOLUTION_14 = 14,
PIKA_HAL_ADC_RESOLUTION_16 = 16,
} PIKA_HAL_ADC_RESOLUTION;
typedef enum {
_PIKA_HAL_ADC_SPEED_UNUSED = 0,
PIKA_HAL_ADC_SPEED_LOW,
PIKA_HAL_ADC_SPEED_HIGH,
_PIKA_HAL_ADC_SPEED_MAX,
} PIKA_HAL_ADC_SPEED;
typedef struct {
PIKA_HAL_ADC_CHANNEL channel;
PIKA_HAL_ADC_RESOLUTION resolution;
PIKA_HAL_ADC_SPEED speed;
} pika_hal_ADC_config;
typedef enum {
@ -236,11 +228,15 @@ typedef struct pika_dev_impl {
int (*close)(pika_dev* dev);
int (*read)(pika_dev* dev, void* buf, size_t count);
int (*write)(pika_dev* dev, void* buf, size_t count);
int (*ioctl)(pika_dev* dev, PIKA_HAL_IOCTL_CMD cmd, uintptr_t cfg);
int (*ioctl)(pika_dev* dev, PIKA_HAL_IOCTL_CMD cmd, void* cfg);
} pika_dev_impl;
/* platform API */
#define PIKA_HAL_TABLE_PLATFORM_API
#include "pika_hal_table.h"
/* config merge headers */
#define PIKA_HAL_TABLE_IOCTL_MERGE_CONFIG_HEADER
#include "pika_hal_table.h"
#endif

View File

@ -35,7 +35,7 @@
return -1; \
} \
int pika_hal_##dev_name##_ioctl(pika_dev* dev, PIKA_HAL_IOCTL_CMD cmd, \
uintptr_t arg) { \
void* arg) { \
if (NULL == dev) { \
return -1; \
} \
@ -83,7 +83,31 @@
pika_dev* dev, pika_hal_##dev_name##_config* cfg);
#endif
#if defined(PIKA_HAL_TABLE_DEV_CONFIG_SIZE)
#define pika_hal_table_add(dev_name) \
if (dev_type == PIKA_HAL_##dev_name) { \
return sizeof(pika_hal_##dev_name##_config); \
}
#endif
#if defined(PIKA_HAL_TABLE_IOCTL_MERGE_CONFIG)
#define pika_hal_table_add(dev_name) \
if (dev->type == PIKA_HAL_##dev_name) { \
return pika_hal_##dev_name##_ioctl_merge_config(dev->ioctl_config, \
config_in); \
}
#endif
#if defined(PIKA_HAL_TABLE_IOCTL_MERGE_CONFIG_HEADER)
#define pika_hal_table_add(dev_name) \
int pika_hal_##dev_name##_ioctl_merge_config( \
pika_hal_##dev_name##_config* dst, pika_hal_##dev_name##_config* src);
#endif
#undef PIKA_HAL_TABLE_FILE_API
#undef PIKA_HAL_TABLE_DEV_TYPE
#undef PIKA_HAL_TABLE_IMPL
#undef PIKA_HAL_TABLE_PLATFORM_API
#undef PIKA_HAL_TABLE_DEV_CONFIG_SIZE
#undef PIKA_HAL_TABLE_IOCTL_MERGE_CONFIG
#undef PIKA_HAL_TABLE_IOCTL_MERGE_CONFIG_HEADER

View File

@ -9,6 +9,9 @@ static const pika_dev_impl pika_dev_impl_list[] = {
#include "pika_hal_table.h"
};
#define _PIKA_DEV_TYPE_MAX \
(sizeof pika_dev_impl_list / sizeof pika_dev_impl_list[0])
static pika_dev_impl* _pika_dev_get_impl(PIKA_HAL_DEV_TYPE type) {
if (type >= _PIKA_DEV_TYPE_MAX) {
return NULL;
@ -16,39 +19,61 @@ static pika_dev_impl* _pika_dev_get_impl(PIKA_HAL_DEV_TYPE type) {
return (pika_dev_impl*)&pika_dev_impl_list[type];
}
static size_t _pika_hal_dev_config_size(PIKA_HAL_DEV_TYPE dev_type) {
#define PIKA_HAL_TABLE_DEV_CONFIG_SIZE
#include "pika_hal_table.h"
return 0;
}
pika_dev* pika_hal_open(PIKA_HAL_DEV_TYPE dev_type, char* name) {
int ret = -1;
if (dev_type >= _PIKA_DEV_TYPE_MAX) {
__platform_printf("Error: dev_type invalied.\r\n");
return NULL;
goto __exit;
}
pika_dev_impl* impl = _pika_dev_get_impl(dev_type);
if (impl->open == NULL) {
__platform_printf("Error: dev_open_impl not implamented.\r\n");
return NULL;
}
pika_dev* dev = (pika_dev*)pikaMalloc(sizeof(pika_dev));
if (dev == NULL) {
return NULL;
goto __exit;
}
dev->type = dev_type;
if (impl->open(dev, name) != 0) {
pikaFree(dev, sizeof(pika_dev));
__platform_printf("Error: dev_open failed.\r\n");
return NULL;
dev->ioctl_config = pikaMalloc(_pika_hal_dev_config_size(dev_type));
if (dev->ioctl_config == NULL) {
goto __exit;
}
return dev;
ret = impl->open(dev, name);
__exit:
if (0 == ret) {
return dev;
}
/* error */
__platform_printf("Error: dev_open failed.\r\n");
if (dev->ioctl_config) {
pikaFree(dev->ioctl_config, _pika_hal_dev_config_size(dev_type));
}
if (dev) {
pikaFree(dev, sizeof(pika_dev));
}
return NULL;
}
int pika_hal_close(pika_dev* dev) {
int ret = -1;
if (dev == NULL) {
return -1;
goto __exit;
}
pika_dev_impl* impl = _pika_dev_get_impl(dev->type);
if (impl->close == NULL) {
return -1;
goto __exit;
}
ret = impl->close(dev);
__exit:
if (NULL != dev) {
pikaFree(dev, sizeof(pika_dev));
}
if (NULL != dev->ioctl_config) {
pikaFree(dev->ioctl_config, _pika_hal_dev_config_size(dev->type));
}
int ret = impl->close(dev);
pikaFree(dev, sizeof(pika_dev));
return ret;
}
@ -74,20 +99,30 @@ int pika_hal_write(pika_dev* dev, void* buf, size_t len) {
return impl->write(dev, buf, len);
}
static const int _pika_hal_cmd_arg_cnt[_PIKA_HAL_IOCTL_MAX] = {
[PIKA_HAL_IOCTL_ENABLE] = 0,
static const int _pika_hal_cmd_arg_cnt[] = {
[PIKA_HAL_IOCTL_CONFIG] = 1,
[PIKA_HAL_IOCTL_ENABLE] = 0,
[PIKA_HAL_IOCTL_DISABLE] = 0,
};
#define _PIKA_HAL_CMD_ARG_CNT_MAX \
(sizeof _pika_hal_cmd_arg_cnt / sizeof _pika_hal_cmd_arg_cnt[0])
static int _pika_hal_get_arg_cnt(PIKA_HAL_IOCTL_CMD cmd) {
if (cmd >= _PIKA_HAL_IOCTL_MAX) {
if (cmd >= _PIKA_HAL_CMD_ARG_CNT_MAX) {
return -1;
}
return _pika_hal_cmd_arg_cnt[cmd];
}
int _pika_hal_ioctl_merge_config(pika_dev* dev, void* config_in) {
#define PIKA_HAL_TABLE_IOCTL_MERGE_CONFIG
#include "pika_hal_table.h"
return -1;
}
int pika_hal_ioctl(pika_dev* dev, PIKA_HAL_IOCTL_CMD cmd, ...) {
int ret = -1;
if (dev == NULL) {
return -1;
}
@ -99,10 +134,82 @@ int pika_hal_ioctl(pika_dev* dev, PIKA_HAL_IOCTL_CMD cmd, ...) {
if (impl->ioctl == NULL) {
return -1;
}
va_list args;
va_start(args, arg_cnt);
uintptr_t arg = va_arg(args, uintptr_t);
int ret = impl->ioctl(dev, cmd, arg);
va_end(args);
void* config_in = NULL;
if (arg_cnt != 0) {
va_list args;
va_start(args, arg_cnt);
config_in = va_arg(args, void*);
ret = _pika_hal_ioctl_merge_config(dev, config_in);
va_end(args);
}
if (0 != ret) {
return ret;
}
ret = impl->ioctl(dev, cmd, dev->ioctl_config);
return ret;
}
#define _ioctl_config_set_default(item, default) \
if (src->item == 0) { \
if (dst->item == 0) { \
/* use default value */ \
dst->item = default; \
} else { \
/* keep exist value */ \
} \
} else { \
/* use input value */ \
src->item = dst->item; \
}
int pika_hal_GPIO_ioctl_merge_config(pika_hal_GPIO_config* dst,
pika_hal_GPIO_config* src) {
_ioctl_config_set_default(dir, PIKA_HAL_GPIO_DIR_IN);
_ioctl_config_set_default(pull, PIKA_HAL_GPIO_PULL_NONE);
_ioctl_config_set_default(speed, PIKA_HAL_GPIO_SPEED_10M);
_ioctl_config_set_default(event_callback_rising, NULL);
_ioctl_config_set_default(event_callback_falling, NULL);
return 0;
}
int pika_hal_UART_ioctl_merge_config(pika_hal_UART_config* dst,
pika_hal_UART_config* src) {
_ioctl_config_set_default(baudrate, PIKA_HAL_UART_BAUDRATE_115200);
_ioctl_config_set_default(data_bits, PIKA_HAL_UART_DATA_BITS_8);
_ioctl_config_set_default(stop_bits, PIKA_HAL_UART_STOP_BITS_1);
_ioctl_config_set_default(parity, PIKA_HAL_UART_PARITY_NONE);
_ioctl_config_set_default(event_callback_rx, NULL);
return 0;
}
int pika_hal_SPI_ioctl_merge_config(pika_hal_SPI_config* dst,
pika_hal_SPI_config* src) {
_ioctl_config_set_default(lsb_or_msb, PIKA_HAL_SPI_MSB);
_ioctl_config_set_default(master_or_slave, PIKA_HAL_SPI_MASTER);
_ioctl_config_set_default(mode, PIKA_HAL_SPI_MODE_0);
_ioctl_config_set_default(data_width, PIKA_HAL_SPI_DATA_WIDTH_8);
_ioctl_config_set_default(speed, PIKA_HAL_SPI_SPEED_10M);
return 0;
}
int pika_hal_IIC_ioctl_merge_config(pika_hal_IIC_config* dst,
pika_hal_IIC_config* src) {
_ioctl_config_set_default(addr, 0);
_ioctl_config_set_default(speed, PIKA_HAL_IIC_SPEED_100K);
return 0;
}
int pika_hal_PWM_ioctl_merge_config(pika_hal_PWM_config* dst,
pika_hal_PWM_config* src) {
_ioctl_config_set_default(channel, PIKA_HAL_PWM_CHANNEL_0);
_ioctl_config_set_default(period, PIKA_HAL_PWM_PERIOD_1MS * 10);
_ioctl_config_set_default(duty, PIKA_HAL_PWM_PERIOD_1MS * 5);
return 0;
}
int pika_hal_ADC_ioctl_merge_config(pika_hal_ADC_config* dst,
pika_hal_ADC_config* src) {
_ioctl_config_set_default(channel, PIKA_HAL_ADC_CHANNEL_0);
_ioctl_config_set_default(resolution, PIKA_HAL_ADC_RESOLUTION_12);
return 0;
}

View File

@ -11,11 +11,11 @@
typedef enum {
#define PIKA_HAL_TABLE_DEV_TYPE
#include "pika_hal_table.h"
_PIKA_DEV_TYPE_MAX,
} PIKA_HAL_DEV_TYPE;
typedef struct {
PIKA_HAL_DEV_TYPE type;
void* ioctl_config;
void* platform_data;
} pika_dev;
@ -23,7 +23,6 @@ typedef enum {
PIKA_HAL_IOCTL_CONFIG,
PIKA_HAL_IOCTL_ENABLE,
PIKA_HAL_IOCTL_DISABLE,
_PIKA_HAL_IOCTL_MAX,
} PIKA_HAL_IOCTL_CMD;
/* posix file like API */
@ -37,7 +36,6 @@ typedef enum {
_PIKA_HAL_GPIO_DIR_UNUSED = 0,
PIKA_HAL_GPIO_DIR_IN,
PIKA_HAL_GPIO_DIR_OUT,
_PIKA_HAL_GPIO_DIR_MAX,
} PIKA_HAL_GPIO_DIR;
typedef enum {
@ -45,22 +43,25 @@ typedef enum {
PIKA_HAL_GPIO_PULL_NONE,
PIKA_HAL_GPIO_PULL_UP,
PIKA_HAL_GPIO_PULL_DOWN,
_PIKA_HAL_GPIO_PULL_MAX,
} PIKA_HAL_GPIO_PULL;
typedef enum {
_PIKA_HAL_GPIO_SPEED_UNUSED = 0,
PIKA_HAL_GPIO_SPEED_LOW,
PIKA_HAL_GPIO_SPEED_HIGH,
_PIKA_HAL_GPIO_SPEED_MAX,
PIKA_HAL_GPIO_SPEED_1M = 1000000,
PIKA_HAL_GPIO_SPEED_2M = 2000000,
PIKA_HAL_GPIO_SPEED_5M = 5000000,
PIKA_HAL_GPIO_SPEED_10M = 10000000,
PIKA_HAL_GPIO_SPEED_20M = 20000000,
PIKA_HAL_GPIO_SPEED_50M = 50000000,
PIKA_HAL_GPIO_SPEED_100M = 100000000,
} PIKA_HAL_GPIO_SPEED;
typedef struct {
PIKA_HAL_GPIO_DIR dir;
PIKA_HAL_GPIO_PULL pull;
PIKA_HAL_GPIO_SPEED speed;
void (*evnet_callback_rising)(pika_dev* dev);
void (*evnet_callback_falling)(pika_dev* dev);
void (*event_callback_rising)(pika_dev* dev);
void (*event_callback_falling)(pika_dev* dev);
} pika_hal_GPIO_config;
typedef enum {
@ -93,7 +94,6 @@ typedef enum {
PIKA_HAL_UART_PARITY_NONE,
PIKA_HAL_UART_PARITY_ODD,
PIKA_HAL_UART_PARITY_EVEN,
_PIKA_HAL_UART_PARITY_MAX,
} PIKA_HAL_UART_PARITY;
typedef struct {
@ -101,16 +101,16 @@ typedef struct {
PIKA_HAL_UART_DATA_BITS data_bits;
PIKA_HAL_UART_STOP_BITS stop_bits;
PIKA_HAL_UART_PARITY parity;
void (*evnet_callback_rx)(pika_dev* dev);
void (*event_callback_rx)(pika_dev* dev);
} pika_hal_UART_config;
typedef uint32_t PIKA_HAL_IIC_SLAVE_ADDR;
typedef enum {
_PIKA_HAL_IIC_SPEED_UNUSED = 0,
PIKA_HAL_IIC_SPEED_LOW,
PIKA_HAL_IIC_SPEED_HIGH,
_PIKA_HAL_IIC_SPEED_MAX,
PIKA_HAL_IIC_SPEED_100K = 100000,
PIKA_HAL_IIC_SPEED_400K = 400000,
PIKA_HAL_IIC_SPEED_1M = 1000000,
} PIKA_HAL_IIC_SPEED;
typedef struct {
@ -122,14 +122,12 @@ typedef enum {
_PIKA_HAL_SPI_LSB_OR_MSB_UNUSED = 0,
PIKA_HAL_SPI_LSB,
PIKA_HAL_SPI_MSB,
_PIKA_HAL_SPI_LSB_OR_MSB_MAX,
} PIKA_HAL_SPI_LSB_OR_MSB;
typedef enum {
_PIKA_HAL_SPI_MASTER_OR_SLAVE_UNUSED = 0,
PIKA_HAL_SPI_MASTER,
PIKA_HAL_SPI_SLAVE,
_PIKA_HAL_SPI_MASTER_OR_SLAVE_MAX,
} PIKA_HAL_SPI_MASTER_OR_SLAVE;
typedef enum {
@ -138,20 +136,23 @@ typedef enum {
PIKA_HAL_SPI_MODE_1,
PIKA_HAL_SPI_MODE_2,
PIKA_HAL_SPI_MODE_3,
_PIKA_HAL_SPI_MODE_MAX,
} PIKA_HAL_SPI_MODE;
typedef enum {
_PIKA_HAL_SPI_DATA_UNUSED = 0,
PIKA_HAL_SPI_DATA_WIDTH_8BIT = 8,
PIKA_HAL_SPI_DATA_WIDTH_16BIT = 16,
PIKA_HAL_SPI_DATA_WIDTH_8 = 8,
PIKA_HAL_SPI_DATA_WIDTH_16 = 16,
} PIKA_HAL_SPI_DATA_WIDTH;
typedef enum {
_PIKA_HAL_SPI_SPEED_UNUSED = 0,
PIKA_HAL_SPI_SPEED_LOW,
PIKA_HAL_SPI_SPEED_HIGH,
_PIKA_HAL_SPI_SPEED_MAX,
PIKA_HAL_SPI_SPEED_1M = 1000000,
PIKA_HAL_SPI_SPEED_2M = 2000000,
PIKA_HAL_SPI_SPEED_5M = 5000000,
PIKA_HAL_SPI_SPEED_10M = 10000000,
PIKA_HAL_SPI_SPEED_20M = 20000000,
PIKA_HAL_SPI_SPEED_50M = 50000000,
PIKA_HAL_SPI_SPEED_100M = 100000000,
} PIKA_HAL_SPI_SPEED;
typedef struct {
@ -174,29 +175,20 @@ typedef enum {
PIKA_HAL_ADC_CHANNEL_7,
PIKA_HAL_ADC_CHANNEL_TEMP,
PIKA_HAL_ADC_CHANNEL_VBAT,
_PIKA_HAL_ADC_CHANNEL_MAX,
} PIKA_HAL_ADC_CHANNEL;
typedef enum {
_PIKA_HAL_ADC_RESOLUTION_UNUSED = 0,
PIKA_HAL_ADC_RESOLUTION_8BIT = 8,
PIKA_HAL_ADC_RESOLUTION_10BIT = 10,
PIKA_HAL_ADC_RESOLUTION_12BIT = 12,
PIKA_HAL_ADC_RESOLUTION_14BIT = 14,
PIKA_HAL_ADC_RESOLUTION_16BIT = 16,
PIKA_HAL_ADC_RESOLUTION_8 = 8,
PIKA_HAL_ADC_RESOLUTION_10 = 10,
PIKA_HAL_ADC_RESOLUTION_12 = 12,
PIKA_HAL_ADC_RESOLUTION_14 = 14,
PIKA_HAL_ADC_RESOLUTION_16 = 16,
} PIKA_HAL_ADC_RESOLUTION;
typedef enum {
_PIKA_HAL_ADC_SPEED_UNUSED = 0,
PIKA_HAL_ADC_SPEED_LOW,
PIKA_HAL_ADC_SPEED_HIGH,
_PIKA_HAL_ADC_SPEED_MAX,
} PIKA_HAL_ADC_SPEED;
typedef struct {
PIKA_HAL_ADC_CHANNEL channel;
PIKA_HAL_ADC_RESOLUTION resolution;
PIKA_HAL_ADC_SPEED speed;
} pika_hal_ADC_config;
typedef enum {
@ -236,11 +228,15 @@ typedef struct pika_dev_impl {
int (*close)(pika_dev* dev);
int (*read)(pika_dev* dev, void* buf, size_t count);
int (*write)(pika_dev* dev, void* buf, size_t count);
int (*ioctl)(pika_dev* dev, PIKA_HAL_IOCTL_CMD cmd, uintptr_t cfg);
int (*ioctl)(pika_dev* dev, PIKA_HAL_IOCTL_CMD cmd, void* cfg);
} pika_dev_impl;
/* platform API */
#define PIKA_HAL_TABLE_PLATFORM_API
#include "pika_hal_table.h"
/* config merge headers */
#define PIKA_HAL_TABLE_IOCTL_MERGE_CONFIG_HEADER
#include "pika_hal_table.h"
#endif

View File

@ -35,7 +35,7 @@
return -1; \
} \
int pika_hal_##dev_name##_ioctl(pika_dev* dev, PIKA_HAL_IOCTL_CMD cmd, \
uintptr_t arg) { \
void* arg) { \
if (NULL == dev) { \
return -1; \
} \
@ -83,7 +83,31 @@
pika_dev* dev, pika_hal_##dev_name##_config* cfg);
#endif
#if defined(PIKA_HAL_TABLE_DEV_CONFIG_SIZE)
#define pika_hal_table_add(dev_name) \
if (dev_type == PIKA_HAL_##dev_name) { \
return sizeof(pika_hal_##dev_name##_config); \
}
#endif
#if defined(PIKA_HAL_TABLE_IOCTL_MERGE_CONFIG)
#define pika_hal_table_add(dev_name) \
if (dev->type == PIKA_HAL_##dev_name) { \
return pika_hal_##dev_name##_ioctl_merge_config(dev->ioctl_config, \
config_in); \
}
#endif
#if defined(PIKA_HAL_TABLE_IOCTL_MERGE_CONFIG_HEADER)
#define pika_hal_table_add(dev_name) \
int pika_hal_##dev_name##_ioctl_merge_config( \
pika_hal_##dev_name##_config* dst, pika_hal_##dev_name##_config* src);
#endif
#undef PIKA_HAL_TABLE_FILE_API
#undef PIKA_HAL_TABLE_DEV_TYPE
#undef PIKA_HAL_TABLE_IMPL
#undef PIKA_HAL_TABLE_PLATFORM_API
#undef PIKA_HAL_TABLE_DEV_CONFIG_SIZE
#undef PIKA_HAL_TABLE_IOCTL_MERGE_CONFIG
#undef PIKA_HAL_TABLE_IOCTL_MERGE_CONFIG_HEADER