update PikaStdDevice to port/linux

use unused as defult enum
This commit is contained in:
lyon 2022-11-21 16:37:52 +08:00
parent b8a8c9d8f5
commit 2344903b89
5 changed files with 238 additions and 135 deletions

View File

@ -24,7 +24,7 @@ typedef enum {
typedef enum {
PIKA_HAL_GPIO_DIR_IN,
PIKA_HAL_GPIO_DIR_OUT,
PIKA_HAL_GPIO_DIR_DEFAULT = 0,
_PIKA_HAL_GPIO_DIR_UNUSED = 0,
_PIKA_HAL_GPIO_DIR_MAX,
} PIKA_HAL_GPIO_DIR;
@ -32,7 +32,7 @@ typedef enum {
PIKA_HAL_GPIO_PULL_NONE,
PIKA_HAL_GPIO_PULL_UP,
PIKA_HAL_GPIO_PULL_DOWN,
PIKA_HAL_GPIO_PULL_DEFAULT = 0,
_PIKA_HAL_GPIO_PULL_UNUSED = 0,
_PIKA_HAL_GPIO_PULL_MAX,
} PIKA_HAL_GPIO_PULL;
@ -50,14 +50,14 @@ typedef struct {
typedef enum {
PIKA_HAL_SPI_LSB,
PIKA_HAL_SPI_MSB,
PIKA_HAL_SPI_LSB_OR_MSB_DEFAULT = 0,
_PIKA_HAL_SPI_LSB_OR_MSB_UNUSED = 0,
_PIKA_HAL_SPI_LSB_OR_MSB_MAX,
} PIKA_HAL_SPI_LSB_OR_MSB;
typedef enum {
PIKA_HAL_SPI_MASTER,
PIKA_HAL_SPI_SLAVE,
PIKA_HAL_SPI_MASTER_OR_SLAVE_DEFAULT = 0,
_PIKA_HAL_SPI_MASTER_OR_SLAVE_UNUSED = 0,
_PIKA_HAL_SPI_MASTER_OR_SLAVE_MAX,
} PIKA_HAL_SPI_MASTER_OR_SLAVE;
@ -66,14 +66,14 @@ typedef enum {
PIKA_HAL_SPI_MODE_1,
PIKA_HAL_SPI_MODE_2,
PIKA_HAL_SPI_MODE_3,
PIKA_HAL_SPI_MODE_DEFAULT = 0,
_PIKA_HAL_SPI_MODE_UNUSED = 0,
_PIKA_HAL_SPI_MODE_MAX,
} PIKA_HAL_SPI_MODE;
typedef enum {
PIKA_HAL_SPI_DATA_WIDTH_8BIT,
PIKA_HAL_SPI_DATA_WIDTH_16BIT,
PIKA_HAL_SPI_DATA_DEFAULT = 0,
_PIKA_HAL_SPI_DATA_UNUSED = 0,
_PIKA_HAL_SPI_DATA_WIDTH_MAX,
} PIKA_HAL_SPI_DATA_WIDTH;
@ -82,7 +82,7 @@ typedef enum {
PIKA_HAL_SPI_SPEED_MEDIUM,
PIKA_HAL_SPI_SPEED_HIGH,
PIKA_HAL_SPI_SPEED_VERY_HIGH,
PIKA_HAL_SPI_SPEED_DEFAULT = 0,
_PIKA_HAL_SPI_SPEED_UNUSED = 0,
_PIKA_HAL_SPI_SPEED_MAX,
} PIKA_HAL_SPI_SPEED;

View File

@ -9,6 +9,7 @@ void PikaStdDevice_GPIO_init(PikaObj* self) {
obj_setInt(self, "isOn", 0);
obj_setStr(self, "pull", "none");
obj_setInt(self, "id", -999);
obj_setPtr(self, "pika_dev", NULL);
}
int PikaStdDevice_GPIO_getId(PikaObj* self) {
@ -76,38 +77,70 @@ void PikaStdDevice_GPIO_setPull(PikaObj* self, char* pull) {
}
}
static pika_dev* _get_dev(PikaObj* self){
pika_dev* dev = obj_getPtr(self, "pika_dev");
if(NULL != dev){
return dev;
}
dev = pika_hal_open(PIKA_HAL_GPIO, obj_getStr(self, "pin"));
if(NULL == dev){
__platform_printf("Error: open GPIO '%s' failed.\r\n", obj_getStr(self, "pin"));
}
obj_setPtr(self, "pika_dev", dev);
return dev;
}
void PikaStdDevice_GPIO_setPin(PikaObj* self, char* pinName) {
obj_setStr(self, "pin", pinName);
}
void PikaStdDevice_GPIO_platformDisable(PikaObj* self) {
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
pika_dev* dev = _get_dev(self);
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_DISABLE);
}
void PikaStdDevice_GPIO_platformEnable(PikaObj* self) {
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
pika_dev* dev = _get_dev(self);
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_ENABLE);
}
void PikaStdDevice_GPIO_platformLow(PikaObj* self) {
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
pika_dev* dev = _get_dev(self);
uint32_t val = 0;
pika_hal_write(dev, &val, sizeof(val));
}
void PikaStdDevice_GPIO_platformHigh(PikaObj* self) {
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
pika_dev* dev = _get_dev(self);
uint32_t val = 1;
pika_hal_write(dev, &val, sizeof(val));
}
void PikaStdDevice_GPIO_platformSetMode(PikaObj* self) {
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
}
void PikaStdDevice_GPIO_platformOff(PikaObj* self) {
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
}
void PikaStdDevice_GPIO_platformOn(PikaObj* self) {
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
char* mode = obj_getStr(self, "mode");
pika_hal_GPIO_config cfg = {0};
if(strEqu(mode, "in")){
cfg.dir = PIKA_HAL_GPIO_DIR_IN;
}else{
cfg.dir = PIKA_HAL_GPIO_DIR_OUT;
}
pika_dev* dev = _get_dev(self);
char* pull = obj_getStr(self, "pull");
if (strEqu(pull, "up")){
cfg.pull = PIKA_HAL_GPIO_PULL_UP;
}
if (strEqu(pull, "down")){
cfg.pull = PIKA_HAL_GPIO_PULL_DOWN;
}
if (strEqu(pull, "none")) {
cfg.pull = PIKA_HAL_GPIO_PULL_NONE;
}
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_CONFIG, &cfg);
}
void PikaStdDevice_GPIO_platformRead(PikaObj* self) {
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
pika_dev* dev = _get_dev(self);
uint32_t val = 0;
pika_hal_read(dev, &val, sizeof(val));
obj_setInt(self, "readBuff", val);
}

View File

@ -4,24 +4,26 @@
#define PIKA_HAL_TABLE_FILE_API
#include "pika_hal_table.h"
static const pika_dev_impl pika_dev_impl_list[_PIKA_DEV_TYPE_MAX] = {
static const pika_dev_impl pika_dev_impl_list[] = {
#define PIKA_HAL_TABLE_IMPL
#include "pika_hal_table.h"
};
static pika_dev_impl* _pika_dev_get_impl(pika_dev_type type) {
static pika_dev_impl* _pika_dev_get_impl(PIKA_HAL_DEV_TYPE type) {
if (type >= _PIKA_DEV_TYPE_MAX) {
return NULL;
}
return (pika_dev_impl*)&pika_dev_impl_list[type];
}
pika_dev* pika_hal_open(pika_dev_type dev_type, char* name) {
pika_dev* pika_hal_open(PIKA_HAL_DEV_TYPE dev_type, char* name) {
if (dev_type >= _PIKA_DEV_TYPE_MAX) {
__platform_printf("Error: dev_type invalied.\r\n");
return NULL;
}
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));
@ -29,8 +31,9 @@ pika_dev* pika_hal_open(pika_dev_type dev_type, char* name) {
return NULL;
}
dev->type = dev_type;
if (impl->open(dev) != 0) {
if (impl->open(dev, name) != 0) {
pikaFree(dev, sizeof(pika_dev));
__platform_printf("Error: dev_open failed.\r\n");
return NULL;
}
return dev;
@ -49,7 +52,7 @@ int pika_hal_close(pika_dev* dev) {
return ret;
}
int pika_hal_read(pika_dev* dev, uint8_t* buf, size_t len) {
int pika_hal_read(pika_dev* dev, void* buf, size_t len) {
if (dev == NULL) {
return -1;
}
@ -60,7 +63,7 @@ int pika_hal_read(pika_dev* dev, uint8_t* buf, size_t len) {
return impl->read(dev, buf, len);
}
int pika_hal_write(pika_dev* dev, uint8_t* buf, size_t len) {
int pika_hal_write(pika_dev* dev, void* buf, size_t len) {
if (dev == NULL) {
return -1;
}
@ -71,20 +74,20 @@ int pika_hal_write(pika_dev* dev, uint8_t* buf, size_t len) {
return impl->write(dev, buf, len);
}
static const int _pika_hal_cmd_arg_cnt[_PIKA_DEV_CMD_MAX] = {
[PIKA_DEV_CMD_ENABLE] = 0,
[PIKA_DEV_CMD_DISABLE] = 0,
[PIKA_DEV_CMD_CONFIG] = 1,
static const int _pika_hal_cmd_arg_cnt[_PIKA_HAL_IOCTL_MAX] = {
[PIKA_HAL_IOCTL_ENABLE] = 0,
[PIKA_HAL_IOCTL_DISABLE] = 0,
[PIKA_HAL_IOCTL_CONFIG] = 1,
};
static int _pika_hal_get_arg_cnt(pika_dev_cmd cmd) {
if (cmd >= _PIKA_DEV_CMD_MAX) {
static int _pika_hal_get_arg_cnt(PIKA_HAL_IOCTL_CMD cmd) {
if (cmd >= _PIKA_HAL_IOCTL_MAX) {
return -1;
}
return _pika_hal_cmd_arg_cnt[cmd];
}
int pika_hal_ioctl(pika_dev* dev, pika_dev_cmd cmd, ...) {
int pika_hal_ioctl(pika_dev* dev, PIKA_HAL_IOCTL_CMD cmd, ...) {
if (dev == NULL) {
return -1;
}

View File

@ -4,74 +4,124 @@
#include <stdint.h>
/*
* pika_dev_hal is a C HAL lib for PikaScript modules.
* the api of pika_dev_hal is similar to the api of posix file API.
* pika_hal is a C Device HAL lib for PikaScript modules.
* the API of pika_hal is similar to the posix file API.
*/
typedef enum pika_dev_type {
typedef enum {
#define PIKA_HAL_TABLE_DEV_TYPE
#include "pika_hal_table.h"
_PIKA_DEV_TYPE_MAX,
} pika_dev_type;
} PIKA_HAL_DEV_TYPE;
typedef enum pika_dev_cmd {
PIKA_DEV_CMD_ENABLE,
PIKA_DEV_CMD_DISABLE,
PIKA_DEV_CMD_CONFIG,
_PIKA_DEV_CMD_MAX,
} pika_dev_cmd;
typedef enum {
PIKA_HAL_IOCTL_ENABLE,
PIKA_HAL_IOCTL_DISABLE,
PIKA_HAL_IOCTL_CONFIG,
_PIKA_HAL_IOCTL_MAX,
} PIKA_HAL_IOCTL_CMD;
typedef enum pika_gpio_dir {
PIKA_GPIO_DIR_IN,
PIKA_GPIO_DIR_OUT,
_PIKA_GPIO_DIR_MAX,
} pika_gpio_dir;
typedef enum {
PIKA_HAL_GPIO_DIR_IN,
PIKA_HAL_GPIO_DIR_OUT,
_PIKA_HAL_GPIO_DIR_UNUSED = 0,
_PIKA_HAL_GPIO_DIR_MAX,
} PIKA_HAL_GPIO_DIR;
typedef enum pika_gpio_pull {
PIKA_GPIO_PULL_NONE,
PIKA_GPIO_PULL_UP,
PIKA_GPIO_PULL_DOWN,
_PIKA_GPIO_PULL_MAX,
} pika_gpio_pull;
typedef enum {
PIKA_HAL_GPIO_PULL_NONE,
PIKA_HAL_GPIO_PULL_UP,
PIKA_HAL_GPIO_PULL_DOWN,
_PIKA_HAL_GPIO_PULL_UNUSED = 0,
_PIKA_HAL_GPIO_PULL_MAX,
} PIKA_HAL_GPIO_PULL;
typedef struct pika_GPIO_config {
pika_gpio_dir dir;
pika_gpio_pull pull;
} pika_GPIO_config;
typedef struct {
PIKA_HAL_GPIO_DIR dir;
PIKA_HAL_GPIO_PULL pull;
} pika_hal_GPIO_config;
typedef struct pika_UART_config {
} pika_UART_config;
typedef struct {
} pika_hal_UART_config;
typedef struct pika_I2C_config {
} pika_I2C_config;
typedef struct {
} pika_hal_I2C_config;
typedef struct pika_SPI_config {
} pika_SPI_config;
typedef enum {
PIKA_HAL_SPI_LSB,
PIKA_HAL_SPI_MSB,
_PIKA_HAL_SPI_LSB_OR_MSB_UNUSED = 0,
_PIKA_HAL_SPI_LSB_OR_MSB_MAX,
} PIKA_HAL_SPI_LSB_OR_MSB;
typedef struct pika_ADC_config {
} pika_ADC_config;
typedef enum {
PIKA_HAL_SPI_MASTER,
PIKA_HAL_SPI_SLAVE,
_PIKA_HAL_SPI_MASTER_OR_SLAVE_UNUSED = 0,
_PIKA_HAL_SPI_MASTER_OR_SLAVE_MAX,
} PIKA_HAL_SPI_MASTER_OR_SLAVE;
typedef struct pika_PWM_config {
} pika_PWM_config;
typedef enum {
PIKA_HAL_SPI_MODE_0,
PIKA_HAL_SPI_MODE_1,
PIKA_HAL_SPI_MODE_2,
PIKA_HAL_SPI_MODE_3,
_PIKA_HAL_SPI_MODE_UNUSED = 0,
_PIKA_HAL_SPI_MODE_MAX,
} PIKA_HAL_SPI_MODE;
typedef struct pika_dev {
pika_dev_type type;
void* user_data;
typedef enum {
PIKA_HAL_SPI_DATA_WIDTH_8BIT,
PIKA_HAL_SPI_DATA_WIDTH_16BIT,
_PIKA_HAL_SPI_DATA_UNUSED = 0,
_PIKA_HAL_SPI_DATA_WIDTH_MAX,
} PIKA_HAL_SPI_DATA_WIDTH;
typedef enum {
PIKA_HAL_SPI_SPEED_LOW,
PIKA_HAL_SPI_SPEED_MEDIUM,
PIKA_HAL_SPI_SPEED_HIGH,
PIKA_HAL_SPI_SPEED_VERY_HIGH,
_PIKA_HAL_SPI_SPEED_UNUSED = 0,
_PIKA_HAL_SPI_SPEED_MAX,
} PIKA_HAL_SPI_SPEED;
typedef struct {
PIKA_HAL_SPI_LSB_OR_MSB lsb_or_msb;
PIKA_HAL_SPI_MASTER_OR_SLAVE master_or_slave;
PIKA_HAL_SPI_MODE mode;
PIKA_HAL_SPI_DATA_WIDTH data_width;
PIKA_HAL_SPI_SPEED speed;
} pika_hal_SPI_config;
typedef struct {
} pika_hal_ADC_config;
typedef struct {
} pika_hal_PWM_config;
typedef struct {
PIKA_HAL_DEV_TYPE type;
void* platform_data;
} pika_dev;
typedef struct pika_dev_impl {
int (*open)(pika_dev* dev);
int (*open)(pika_dev* dev, char* name);
int (*close)(pika_dev* dev);
int (*read)(pika_dev* dev, uint8_t* buf, size_t count);
int (*write)(pika_dev* dev, uint8_t* buf, size_t count);
int (*ioctl)(pika_dev* dev, pika_dev_cmd cmd, uintptr_t cfg);
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);
} pika_dev_impl;
/* posix file like API */
pika_dev* pika_hal_open(pika_dev_type dev_type, char* name);
pika_dev* pika_hal_open(PIKA_HAL_DEV_TYPE dev_type, char* name);
int pika_hal_close(pika_dev* dev);
int pika_hal_read(pika_dev* dev, uint8_t* buf, size_t len);
int pika_hal_write(pika_dev* dev, uint8_t* buf, size_t len);
int PIKA_HAL_TABLE_FILE_API(pika_dev* dev, pika_dev_cmd cmd, ...);
int pika_hal_read(pika_dev* dev, void* buf, size_t len);
int pika_hal_write(pika_dev* dev, void* buf, size_t len);
int pika_hal_ioctl(pika_dev* dev, PIKA_HAL_IOCTL_CMD cmd, ...);
/* platform API */
#define PIKA_HAL_TABLE_PLATFORM_API
#include "pika_hal_table.h"
#endif

View File

@ -1,53 +1,55 @@
#undef pika_hal_table_add
#if defined(PIKA_HAL_TABLE_FILE_API)
#define pika_hal_table_add(dev_name) \
PIKA_WEAK int pika_##dev_name##_open(pika_dev* dev) { \
WEAK_FUNCTION_NEED_OVERRIDE_ERROR() \
return -1; \
} \
PIKA_WEAK int pika_##dev_name##_close(pika_dev* dev) { \
WEAK_FUNCTION_NEED_OVERRIDE_ERROR() \
return -1; \
} \
PIKA_WEAK int pika_##dev_name##_read(pika_dev* dev, uint8_t* buf, \
size_t len) { \
WEAK_FUNCTION_NEED_OVERRIDE_ERROR() \
return -1; \
} \
PIKA_WEAK int pika_##dev_name##_write(pika_dev* dev, uint8_t* buf, \
size_t len) { \
WEAK_FUNCTION_NEED_OVERRIDE_ERROR() \
return -1; \
} \
PIKA_WEAK int pika_##dev_name##_ioctl_enable(pika_dev* dev) { \
WEAK_FUNCTION_NEED_OVERRIDE_ERROR() \
return -1; \
} \
PIKA_WEAK int pika_##dev_name##_ioctl_disable(pika_dev* dev) { \
WEAK_FUNCTION_NEED_OVERRIDE_ERROR() \
return -1; \
} \
PIKA_WEAK int pika_##dev_name##_ioctl_config( \
pika_dev* dev, pika_##dev_name##_config* cfg) { \
WEAK_FUNCTION_NEED_OVERRIDE_ERROR() \
return -1; \
} \
int pika_##dev_name##_ioctl(pika_dev* dev, pika_dev_cmd cmd, \
uintptr_t arg) { \
if (NULL == dev) { \
return -1; \
} \
switch (cmd) { \
case PIKA_DEV_CMD_ENABLE: \
return pika_##dev_name##_ioctl_enable(dev); \
case PIKA_DEV_CMD_DISABLE: \
return pika_##dev_name##_ioctl_disable(dev); \
case PIKA_DEV_CMD_CONFIG: \
return pika_##dev_name##_ioctl_config( \
dev, (pika_##dev_name##_config*)arg); \
default: \
return -1; \
} \
#define pika_hal_table_add(dev_name) \
PIKA_WEAK int pika_hal_platform_##dev_name##_open(pika_dev* dev, \
char* name) { \
WEAK_FUNCTION_NEED_OVERRIDE_ERROR() \
return -1; \
} \
PIKA_WEAK int pika_hal_platform_##dev_name##_close(pika_dev* dev) { \
WEAK_FUNCTION_NEED_OVERRIDE_ERROR() \
return -1; \
} \
PIKA_WEAK int pika_hal_platform_##dev_name##_read(pika_dev* dev, \
void* buf, size_t len) { \
WEAK_FUNCTION_NEED_OVERRIDE_ERROR() \
return -1; \
} \
PIKA_WEAK int pika_hal_platform_##dev_name##_write( \
pika_dev* dev, void* buf, size_t len) { \
WEAK_FUNCTION_NEED_OVERRIDE_ERROR() \
return -1; \
} \
PIKA_WEAK int pika_hal_platform_##dev_name##_ioctl_enable(pika_dev* dev) { \
WEAK_FUNCTION_NEED_OVERRIDE_ERROR() \
return -1; \
} \
PIKA_WEAK int pika_hal_platform_##dev_name##_ioctl_disable( \
pika_dev* dev) { \
WEAK_FUNCTION_NEED_OVERRIDE_ERROR() \
return -1; \
} \
PIKA_WEAK int pika_hal_platform_##dev_name##_ioctl_config( \
pika_dev* dev, pika_hal_##dev_name##_config* cfg) { \
WEAK_FUNCTION_NEED_OVERRIDE_ERROR() \
return -1; \
} \
int pika_hal_##dev_name##_ioctl(pika_dev* dev, PIKA_HAL_IOCTL_CMD cmd, \
uintptr_t arg) { \
if (NULL == dev) { \
return -1; \
} \
switch (cmd) { \
case PIKA_HAL_IOCTL_ENABLE: \
return pika_hal_platform_##dev_name##_ioctl_enable(dev); \
case PIKA_HAL_IOCTL_DISABLE: \
return pika_hal_platform_##dev_name##_ioctl_disable(dev); \
case PIKA_HAL_IOCTL_CONFIG: \
return pika_hal_platform_##dev_name##_ioctl_config( \
dev, (pika_hal_##dev_name##_config*)arg); \
default: \
return -1; \
} \
}
#endif
@ -57,16 +59,31 @@
#endif
#if defined(PIKA_HAL_TABLE_IMPL)
#define pika_hal_table_add(dev_name) \
[PIKA_HAL_##dev_name] = { \
.open = pika_##dev_name##_open, \
.close = pika_##dev_name##_close, \
.read = pika_##dev_name##_read, \
.write = pika_##dev_name##_write, \
.ioctl = pika_##dev_name##_ioctl, \
#define pika_hal_table_add(dev_name) \
[PIKA_HAL_##dev_name] = { \
.open = pika_hal_platform_##dev_name##_open, \
.close = pika_hal_platform_##dev_name##_close, \
.read = pika_hal_platform_##dev_name##_read, \
.write = pika_hal_platform_##dev_name##_write, \
.ioctl = pika_hal_##dev_name##_ioctl, \
},
#endif
#if defined(PIKA_HAL_TABLE_PLATFORM_API)
#define pika_hal_table_add(dev_name) \
int pika_hal_platform_##dev_name##_open(pika_dev* dev, char* name); \
int pika_hal_platform_##dev_name##_close(pika_dev* dev); \
int pika_hal_platform_##dev_name##_read(pika_dev* dev, void* buf, \
size_t len); \
int pika_hal_platform_##dev_name##_write(pika_dev* dev, void* buf, \
size_t len); \
int pika_hal_platform_##dev_name##_ioctl_enable(pika_dev* dev); \
int pika_hal_platform_##dev_name##_ioctl_disable(pika_dev* dev); \
int pika_hal_platform_##dev_name##_ioctl_config( \
pika_dev* dev, pika_hal_##dev_name##_config* cfg);
#endif
#undef PIKA_HAL_TABLE_FILE_API
#undef PIKA_HAL_TABLE_DEV_TYPE
#undef PIKA_HAL_TABLE_IMPL
#undef PIKA_HAL_TABLE_PLATFORM_API