mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-15 17:02:53 +08:00
add test for hal, use table_map for hal
This commit is contained in:
parent
fa374c72da
commit
2ad9caacc9
@ -1,214 +1,23 @@
|
||||
#include "pika_hal.h"
|
||||
#include "PikaObj.h"
|
||||
|
||||
PIKA_WEAK int pika_gpio_open(pika_dev* dev) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
#define PIKA_HAL_TABLE_FILE_API
|
||||
#include "pika_hal_table.h"
|
||||
|
||||
PIKA_WEAK int pika_gpio_close(pika_dev* dev) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_gpio_read(pika_dev* dev, uint8_t* buf, size_t count) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_gpio_write(pika_dev* dev, uint8_t* buf, size_t count) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_gpio_ioctl(pika_dev* dev, pika_dev_cmd cmd, uintptr_t arg) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_uart_open(pika_dev* dev) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_uart_close(pika_dev* dev) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_uart_read(pika_dev* dev, uint8_t* buf, size_t count) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_uart_write(pika_dev* dev, uint8_t* buf, size_t count) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_uart_ioctl(pika_dev* dev, pika_dev_cmd cmd, uintptr_t arg) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_i2c_open(pika_dev* dev) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_i2c_close(pika_dev* dev) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_i2c_read(pika_dev* dev, uint8_t* buf, size_t count) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_i2c_write(pika_dev* dev, uint8_t* buf, size_t count) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_i2c_ioctl(pika_dev* dev, pika_dev_cmd cmd, uintptr_t arg) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_spi_open(pika_dev* dev) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_spi_close(pika_dev* dev) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_spi_read(pika_dev* dev, uint8_t* buf, size_t count) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_spi_write(pika_dev* dev, uint8_t* buf, size_t count) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_spi_ioctl(pika_dev* dev, pika_dev_cmd cmd, uintptr_t arg) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_pwm_open(pika_dev* dev) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_pwm_close(pika_dev* dev) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_pwm_read(pika_dev* dev, uint8_t* buf, size_t count) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_pwm_write(pika_dev* dev, uint8_t* buf, size_t count) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_pwm_ioctl(pika_dev* dev, pika_dev_cmd cmd, uintptr_t arg) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_adc_open(pika_dev* dev) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_adc_close(pika_dev* dev) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_adc_read(pika_dev* dev, uint8_t* buf, size_t count) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_adc_write(pika_dev* dev, uint8_t* buf, size_t count) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_adc_ioctl(pika_dev* dev, pika_dev_cmd cmd, uintptr_t arg) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_dac_open(pika_dev* dev) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_dac_close(pika_dev* dev) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_dac_read(pika_dev* dev, uint8_t* buf, size_t count) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_dac_write(pika_dev* dev, uint8_t* buf, size_t count) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_dac_ioctl(pika_dev* dev, pika_dev_cmd cmd, uintptr_t arg) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
static const pika_dev_impl pika_dev_impl_list[PIKA_DEV_TYPE_MAX] = {
|
||||
[PIKA_DEV_TYPE_GPIO] =
|
||||
{
|
||||
.open = pika_gpio_open,
|
||||
.close = pika_gpio_close,
|
||||
.read = pika_gpio_read,
|
||||
.write = pika_gpio_write,
|
||||
.ioctl = pika_gpio_ioctl,
|
||||
},
|
||||
[PIKA_DEV_TYPE_UART] =
|
||||
{
|
||||
.open = pika_uart_open,
|
||||
.close = pika_uart_close,
|
||||
.read = pika_uart_read,
|
||||
.write = pika_uart_write,
|
||||
.ioctl = pika_uart_ioctl,
|
||||
},
|
||||
[PIKA_DEV_TYPE_I2C] =
|
||||
{
|
||||
.open = pika_i2c_open,
|
||||
.close = pika_i2c_close,
|
||||
.read = pika_i2c_read,
|
||||
.write = pika_i2c_write,
|
||||
.ioctl = pika_i2c_ioctl,
|
||||
},
|
||||
[PIKA_DEV_TYPE_SPI] =
|
||||
{
|
||||
.open = pika_spi_open,
|
||||
.close = pika_spi_close,
|
||||
.read = pika_spi_read,
|
||||
.write = pika_spi_write,
|
||||
.ioctl = pika_spi_ioctl,
|
||||
},
|
||||
[PIKA_DEV_TYPE_PWM] =
|
||||
{
|
||||
.open = pika_pwm_open,
|
||||
.close = pika_pwm_close,
|
||||
.read = pika_pwm_read,
|
||||
.write = pika_pwm_write,
|
||||
.ioctl = pika_pwm_ioctl,
|
||||
},
|
||||
[PIKA_DEV_TYPE_ADC] =
|
||||
{
|
||||
.open = pika_adc_open,
|
||||
.close = pika_adc_close,
|
||||
.read = pika_adc_read,
|
||||
.write = pika_adc_write,
|
||||
.ioctl = pika_adc_ioctl,
|
||||
},
|
||||
[PIKA_DEV_TYPE_DAC] =
|
||||
{
|
||||
.open = pika_dac_open,
|
||||
.close = pika_dac_close,
|
||||
.read = pika_dac_read,
|
||||
.write = pika_dac_write,
|
||||
.ioctl = pika_dac_ioctl,
|
||||
},
|
||||
static const pika_dev_impl pika_dev_impl_list[_PIKA_DEV_TYPE_MAX] = {
|
||||
#define PIKA_HAL_TABLE_IMPL
|
||||
#include "pika_hal_table.h"
|
||||
};
|
||||
|
||||
static pika_dev_impl* _pika_dev_get_impl(pika_dev_type type) {
|
||||
if (type >= PIKA_DEV_TYPE_MAX) {
|
||||
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) {
|
||||
if (dev_type >= PIKA_DEV_TYPE_MAX) {
|
||||
if (dev_type >= _PIKA_DEV_TYPE_MAX) {
|
||||
return NULL;
|
||||
}
|
||||
pika_dev_impl* impl = _pika_dev_get_impl(dev_type);
|
||||
@ -262,14 +71,14 @@ 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_GPIO_MODE] = 1, [PIKA_DEV_CMD_GPIO_PULL] = 1,
|
||||
[PIKA_DEV_CMD_GPIO_TOGGLE] = 0,
|
||||
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 int _pika_hal_get_arg_cnt(pika_dev_cmd cmd) {
|
||||
if (cmd >= PIKA_DEV_CMD_MAX) {
|
||||
if (cmd >= _PIKA_DEV_CMD_MAX) {
|
||||
return -1;
|
||||
}
|
||||
return _pika_hal_cmd_arg_cnt[cmd];
|
||||
|
@ -7,48 +7,57 @@
|
||||
* 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.
|
||||
*/
|
||||
typedef enum pika_dev_cmd pika_dev_cmd;
|
||||
typedef struct pika_dev pika_dev;
|
||||
|
||||
typedef enum pika_dev_type {
|
||||
PIKA_DEV_TYPE_NONE = 0,
|
||||
PIKA_DEV_TYPE_GPIO,
|
||||
PIKA_DEV_TYPE_UART,
|
||||
PIKA_DEV_TYPE_I2C,
|
||||
PIKA_DEV_TYPE_SPI,
|
||||
PIKA_DEV_TYPE_PWM,
|
||||
PIKA_DEV_TYPE_ADC,
|
||||
PIKA_DEV_TYPE_DAC,
|
||||
PIKA_DEV_TYPE_MAX,
|
||||
#define PIKA_HAL_TABLE_DEV_TYPE
|
||||
#include "pika_hal_table.h"
|
||||
_PIKA_DEV_TYPE_MAX,
|
||||
} pika_dev_type;
|
||||
|
||||
/* posix file like API */
|
||||
pika_dev* pika_hal_open(pika_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_ioctl(pika_dev* dev, pika_dev_cmd cmd, ...);
|
||||
|
||||
typedef enum pika_dev_cmd {
|
||||
PIKA_DEV_CMD_ENABLE,
|
||||
PIKA_DEV_CMD_DISABLE,
|
||||
PIKA_DEV_CMD_SET_CONFIG,
|
||||
PIKA_DEV_CMD_GET_CONFIG,
|
||||
PIKA_DEV_CMD_MAX,
|
||||
PIKA_DEV_CMD_CONFIG,
|
||||
_PIKA_DEV_CMD_MAX,
|
||||
} pika_dev_cmd;
|
||||
|
||||
typedef enum pika_dev_gpio_mode {
|
||||
PIKA_DEV_GPIO_MODE_INPUT,
|
||||
PIKA_DEV_GPIO_MODE_OUTPUT,
|
||||
PIKA_DEV_GPIO_MODE_MAX,
|
||||
} pika_dev_gpio_mode;
|
||||
typedef enum pika_gpio_dir {
|
||||
PIKA_GPIO_DIR_IN,
|
||||
PIKA_GPIO_DIR_OUT,
|
||||
_PIKA_GPIO_DIR_MAX,
|
||||
} pika_gpio_dir;
|
||||
|
||||
typedef enum pika_dev_gpio_pull {
|
||||
PIKA_DEV_GPIO_PULL_NONE,
|
||||
PIKA_DEV_GPIO_PULL_UP,
|
||||
PIKA_DEV_GPIO_PULL_DOWN,
|
||||
PIKA_DEV_GPIO_PULL_MAX,
|
||||
} pika_dev_gpio_pull;
|
||||
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 struct pika_GPIO_config {
|
||||
pika_gpio_dir dir;
|
||||
pika_gpio_pull pull;
|
||||
} pika_GPIO_config;
|
||||
|
||||
typedef struct pika_UART_config {
|
||||
} pika_UART_config;
|
||||
|
||||
typedef struct pika_I2C_config {
|
||||
} pika_I2C_config;
|
||||
|
||||
typedef struct pika_SPI_config {
|
||||
} pika_SPI_config;
|
||||
|
||||
typedef struct pika_ADC_config {
|
||||
} pika_ADC_config;
|
||||
|
||||
typedef struct pika_PWM_config {
|
||||
} pika_PWM_config;
|
||||
|
||||
typedef struct pika_dev {
|
||||
pika_dev_type type;
|
||||
void* user_data;
|
||||
} pika_dev;
|
||||
|
||||
typedef struct pika_dev_impl {
|
||||
int (*open)(pika_dev* dev);
|
||||
@ -58,8 +67,11 @@ typedef struct pika_dev_impl {
|
||||
int (*ioctl)(pika_dev* dev, pika_dev_cmd cmd, uintptr_t cfg);
|
||||
} pika_dev_impl;
|
||||
|
||||
struct pika_dev {
|
||||
pika_dev_type type;
|
||||
};
|
||||
/* posix file like API */
|
||||
pika_dev* pika_hal_open(pika_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, ...);
|
||||
|
||||
#endif
|
||||
|
12
package/PikaStdDevice/pika_hal_table.h
Normal file
12
package/PikaStdDevice/pika_hal_table.h
Normal file
@ -0,0 +1,12 @@
|
||||
#include "pika_hal_table_rule.h"
|
||||
|
||||
/* clang-format off */
|
||||
|
||||
pika_hal_table_add(GPIO)
|
||||
pika_hal_table_add(UART)
|
||||
pika_hal_table_add(I2C)
|
||||
pika_hal_table_add(SPI)
|
||||
pika_hal_table_add(ADC)
|
||||
pika_hal_table_add(PWM)
|
||||
|
||||
/* clang-format on */
|
72
package/PikaStdDevice/pika_hal_table_rule.h
Normal file
72
package/PikaStdDevice/pika_hal_table_rule.h
Normal file
@ -0,0 +1,72 @@
|
||||
#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; \
|
||||
} \
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if defined(PIKA_HAL_TABLE_DEV_TYPE)
|
||||
#define pika_hal_table_add(dev_name) PIKA_HAL_##dev_name,
|
||||
#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, \
|
||||
},
|
||||
#endif
|
||||
|
||||
#undef PIKA_HAL_TABLE_FILE_API
|
||||
#undef PIKA_HAL_TABLE_DEV_TYPE
|
||||
#undef PIKA_HAL_TABLE_IMPL
|
6
port/linux/.vscode/settings.json
vendored
6
port/linux/.vscode/settings.json
vendored
@ -89,7 +89,11 @@
|
||||
"_mqtt__mqtt.h": "c",
|
||||
"requests_response.h": "c",
|
||||
"*.cfg": "c",
|
||||
"pika_hal.h": "c"
|
||||
"pika_hal.h": "c",
|
||||
"__instruction_def.h": "c",
|
||||
"pika_hal_def.h": "c",
|
||||
"pika_hal_table.h": "c",
|
||||
"pika_hal_table_rule.h": "c"
|
||||
},
|
||||
"python.formatting.provider": "autopep8",
|
||||
"C_Cpp.errorSquiggles": "Disabled"
|
||||
|
@ -1,214 +1,23 @@
|
||||
#include "pika_hal.h"
|
||||
#include "PikaObj.h"
|
||||
|
||||
PIKA_WEAK int pika_gpio_open(pika_dev* dev) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
#define PIKA_HAL_TABLE_FILE_API
|
||||
#include "pika_hal_table.h"
|
||||
|
||||
PIKA_WEAK int pika_gpio_close(pika_dev* dev) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_gpio_read(pika_dev* dev, uint8_t* buf, size_t count) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_gpio_write(pika_dev* dev, uint8_t* buf, size_t count) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_gpio_ioctl(pika_dev* dev, pika_dev_cmd cmd, uintptr_t arg) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_uart_open(pika_dev* dev) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_uart_close(pika_dev* dev) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_uart_read(pika_dev* dev, uint8_t* buf, size_t count) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_uart_write(pika_dev* dev, uint8_t* buf, size_t count) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_uart_ioctl(pika_dev* dev, pika_dev_cmd cmd, uintptr_t arg) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_i2c_open(pika_dev* dev) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_i2c_close(pika_dev* dev) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_i2c_read(pika_dev* dev, uint8_t* buf, size_t count) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_i2c_write(pika_dev* dev, uint8_t* buf, size_t count) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_i2c_ioctl(pika_dev* dev, pika_dev_cmd cmd, uintptr_t arg) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_spi_open(pika_dev* dev) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_spi_close(pika_dev* dev) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_spi_read(pika_dev* dev, uint8_t* buf, size_t count) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_spi_write(pika_dev* dev, uint8_t* buf, size_t count) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_spi_ioctl(pika_dev* dev, pika_dev_cmd cmd, uintptr_t arg) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_pwm_open(pika_dev* dev) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_pwm_close(pika_dev* dev) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_pwm_read(pika_dev* dev, uint8_t* buf, size_t count) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_pwm_write(pika_dev* dev, uint8_t* buf, size_t count) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_pwm_ioctl(pika_dev* dev, pika_dev_cmd cmd, uintptr_t arg) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_adc_open(pika_dev* dev) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_adc_close(pika_dev* dev) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_adc_read(pika_dev* dev, uint8_t* buf, size_t count) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_adc_write(pika_dev* dev, uint8_t* buf, size_t count) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_adc_ioctl(pika_dev* dev, pika_dev_cmd cmd, uintptr_t arg) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_dac_open(pika_dev* dev) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_dac_close(pika_dev* dev) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_dac_read(pika_dev* dev, uint8_t* buf, size_t count) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_dac_write(pika_dev* dev, uint8_t* buf, size_t count) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
PIKA_WEAK int pika_dac_ioctl(pika_dev* dev, pika_dev_cmd cmd, uintptr_t arg) {
|
||||
WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
static const pika_dev_impl pika_dev_impl_list[PIKA_DEV_TYPE_MAX] = {
|
||||
[PIKA_DEV_TYPE_GPIO] =
|
||||
{
|
||||
.open = pika_gpio_open,
|
||||
.close = pika_gpio_close,
|
||||
.read = pika_gpio_read,
|
||||
.write = pika_gpio_write,
|
||||
.ioctl = pika_gpio_ioctl,
|
||||
},
|
||||
[PIKA_DEV_TYPE_UART] =
|
||||
{
|
||||
.open = pika_uart_open,
|
||||
.close = pika_uart_close,
|
||||
.read = pika_uart_read,
|
||||
.write = pika_uart_write,
|
||||
.ioctl = pika_uart_ioctl,
|
||||
},
|
||||
[PIKA_DEV_TYPE_I2C] =
|
||||
{
|
||||
.open = pika_i2c_open,
|
||||
.close = pika_i2c_close,
|
||||
.read = pika_i2c_read,
|
||||
.write = pika_i2c_write,
|
||||
.ioctl = pika_i2c_ioctl,
|
||||
},
|
||||
[PIKA_DEV_TYPE_SPI] =
|
||||
{
|
||||
.open = pika_spi_open,
|
||||
.close = pika_spi_close,
|
||||
.read = pika_spi_read,
|
||||
.write = pika_spi_write,
|
||||
.ioctl = pika_spi_ioctl,
|
||||
},
|
||||
[PIKA_DEV_TYPE_PWM] =
|
||||
{
|
||||
.open = pika_pwm_open,
|
||||
.close = pika_pwm_close,
|
||||
.read = pika_pwm_read,
|
||||
.write = pika_pwm_write,
|
||||
.ioctl = pika_pwm_ioctl,
|
||||
},
|
||||
[PIKA_DEV_TYPE_ADC] =
|
||||
{
|
||||
.open = pika_adc_open,
|
||||
.close = pika_adc_close,
|
||||
.read = pika_adc_read,
|
||||
.write = pika_adc_write,
|
||||
.ioctl = pika_adc_ioctl,
|
||||
},
|
||||
[PIKA_DEV_TYPE_DAC] =
|
||||
{
|
||||
.open = pika_dac_open,
|
||||
.close = pika_dac_close,
|
||||
.read = pika_dac_read,
|
||||
.write = pika_dac_write,
|
||||
.ioctl = pika_dac_ioctl,
|
||||
},
|
||||
static const pika_dev_impl pika_dev_impl_list[_PIKA_DEV_TYPE_MAX] = {
|
||||
#define PIKA_HAL_TABLE_IMPL
|
||||
#include "pika_hal_table.h"
|
||||
};
|
||||
|
||||
static pika_dev_impl* _pika_dev_get_impl(pika_dev_type type) {
|
||||
if (type >= PIKA_DEV_TYPE_MAX) {
|
||||
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) {
|
||||
if (dev_type >= PIKA_DEV_TYPE_MAX) {
|
||||
if (dev_type >= _PIKA_DEV_TYPE_MAX) {
|
||||
return NULL;
|
||||
}
|
||||
pika_dev_impl* impl = _pika_dev_get_impl(dev_type);
|
||||
@ -262,14 +71,14 @@ 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_GPIO_MODE] = 1, [PIKA_DEV_CMD_GPIO_PULL] = 1,
|
||||
[PIKA_DEV_CMD_GPIO_TOGGLE] = 0,
|
||||
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 int _pika_hal_get_arg_cnt(pika_dev_cmd cmd) {
|
||||
if (cmd >= PIKA_DEV_CMD_MAX) {
|
||||
if (cmd >= _PIKA_DEV_CMD_MAX) {
|
||||
return -1;
|
||||
}
|
||||
return _pika_hal_cmd_arg_cnt[cmd];
|
||||
|
@ -7,48 +7,57 @@
|
||||
* 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.
|
||||
*/
|
||||
typedef enum pika_dev_cmd pika_dev_cmd;
|
||||
typedef struct pika_dev pika_dev;
|
||||
|
||||
typedef enum pika_dev_type {
|
||||
PIKA_DEV_TYPE_NONE = 0,
|
||||
PIKA_DEV_TYPE_GPIO,
|
||||
PIKA_DEV_TYPE_UART,
|
||||
PIKA_DEV_TYPE_I2C,
|
||||
PIKA_DEV_TYPE_SPI,
|
||||
PIKA_DEV_TYPE_PWM,
|
||||
PIKA_DEV_TYPE_ADC,
|
||||
PIKA_DEV_TYPE_DAC,
|
||||
PIKA_DEV_TYPE_MAX,
|
||||
#define PIKA_HAL_TABLE_DEV_TYPE
|
||||
#include "pika_hal_table.h"
|
||||
_PIKA_DEV_TYPE_MAX,
|
||||
} pika_dev_type;
|
||||
|
||||
/* posix file like API */
|
||||
pika_dev* pika_hal_open(pika_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_ioctl(pika_dev* dev, pika_dev_cmd cmd, ...);
|
||||
|
||||
typedef enum pika_dev_cmd {
|
||||
PIKA_DEV_CMD_ENABLE,
|
||||
PIKA_DEV_CMD_DISABLE,
|
||||
PIKA_DEV_CMD_SET_CONFIG,
|
||||
PIKA_DEV_CMD_GET_CONFIG,
|
||||
PIKA_DEV_CMD_MAX,
|
||||
PIKA_DEV_CMD_CONFIG,
|
||||
_PIKA_DEV_CMD_MAX,
|
||||
} pika_dev_cmd;
|
||||
|
||||
typedef enum pika_dev_gpio_mode {
|
||||
PIKA_DEV_GPIO_MODE_INPUT,
|
||||
PIKA_DEV_GPIO_MODE_OUTPUT,
|
||||
PIKA_DEV_GPIO_MODE_MAX,
|
||||
} pika_dev_gpio_mode;
|
||||
typedef enum pika_gpio_dir {
|
||||
PIKA_GPIO_DIR_IN,
|
||||
PIKA_GPIO_DIR_OUT,
|
||||
_PIKA_GPIO_DIR_MAX,
|
||||
} pika_gpio_dir;
|
||||
|
||||
typedef enum pika_dev_gpio_pull {
|
||||
PIKA_DEV_GPIO_PULL_NONE,
|
||||
PIKA_DEV_GPIO_PULL_UP,
|
||||
PIKA_DEV_GPIO_PULL_DOWN,
|
||||
PIKA_DEV_GPIO_PULL_MAX,
|
||||
} pika_dev_gpio_pull;
|
||||
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 struct pika_GPIO_config {
|
||||
pika_gpio_dir dir;
|
||||
pika_gpio_pull pull;
|
||||
} pika_GPIO_config;
|
||||
|
||||
typedef struct pika_UART_config {
|
||||
} pika_UART_config;
|
||||
|
||||
typedef struct pika_I2C_config {
|
||||
} pika_I2C_config;
|
||||
|
||||
typedef struct pika_SPI_config {
|
||||
} pika_SPI_config;
|
||||
|
||||
typedef struct pika_ADC_config {
|
||||
} pika_ADC_config;
|
||||
|
||||
typedef struct pika_PWM_config {
|
||||
} pika_PWM_config;
|
||||
|
||||
typedef struct pika_dev {
|
||||
pika_dev_type type;
|
||||
void* user_data;
|
||||
} pika_dev;
|
||||
|
||||
typedef struct pika_dev_impl {
|
||||
int (*open)(pika_dev* dev);
|
||||
@ -58,8 +67,11 @@ typedef struct pika_dev_impl {
|
||||
int (*ioctl)(pika_dev* dev, pika_dev_cmd cmd, uintptr_t cfg);
|
||||
} pika_dev_impl;
|
||||
|
||||
struct pika_dev {
|
||||
pika_dev_type type;
|
||||
};
|
||||
/* posix file like API */
|
||||
pika_dev* pika_hal_open(pika_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, ...);
|
||||
|
||||
#endif
|
||||
|
@ -0,0 +1,12 @@
|
||||
#include "pika_hal_table_rule.h"
|
||||
|
||||
/* clang-format off */
|
||||
|
||||
pika_hal_table_add(GPIO)
|
||||
pika_hal_table_add(UART)
|
||||
pika_hal_table_add(I2C)
|
||||
pika_hal_table_add(SPI)
|
||||
pika_hal_table_add(ADC)
|
||||
pika_hal_table_add(PWM)
|
||||
|
||||
/* clang-format on */
|
@ -0,0 +1,72 @@
|
||||
#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; \
|
||||
} \
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#if defined(PIKA_HAL_TABLE_DEV_TYPE)
|
||||
#define pika_hal_table_add(dev_name) PIKA_HAL_##dev_name,
|
||||
#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, \
|
||||
},
|
||||
#endif
|
||||
|
||||
#undef PIKA_HAL_TABLE_FILE_API
|
||||
#undef PIKA_HAL_TABLE_DEV_TYPE
|
||||
#undef PIKA_HAL_TABLE_IMPL
|
9
test/hal-test.cpp
Normal file
9
test/hal-test.cpp
Normal file
@ -0,0 +1,9 @@
|
||||
#include "test_common.h"
|
||||
extern "C" {
|
||||
#include "../package/pikascript/pikascript-lib/PikaStdDevice/pika_hal.h"
|
||||
}
|
||||
|
||||
TEST(hal, open) {
|
||||
pika_dev* io = pika_hal_open(PIKA_HAL_GPIO, "PA10");
|
||||
EXPECT_EQ(0, (uintptr_t)io);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user