128 lines
3.0 KiB
C
Raw Normal View History

2022-11-20 22:16:41 +08:00
#ifndef _PIKA_DEV_HAL_H
#define _PIKA_DEV_HAL_H
#include <stddef.h>
#include <stdint.h>
/*
2022-11-21 02:03:01 +08:00
* pika_hal is a C Device HAL lib for PikaScript modules.
* the API of pika_hal is similar to the posix file API.
2022-11-20 22:16:41 +08:00
*/
2022-11-21 14:10:05 +08:00
typedef enum {
#define PIKA_HAL_TABLE_DEV_TYPE
#include "pika_hal_table.h"
_PIKA_DEV_TYPE_MAX,
2022-11-21 14:10:05 +08:00
} PIKA_HAL_DEV_TYPE;
typedef enum {
PIKA_HAL_IOCTL_ENABLE,
PIKA_HAL_IOCTL_DISABLE,
PIKA_HAL_IOCTL_CONFIG,
_PIKA_HAL_IOCTL_MAX,
} PIKA_HAL_IOCTL_CMD;
typedef enum {
2022-11-21 17:04:23 +08:00
_PIKA_HAL_GPIO_DIR_UNUSED = 0,
2022-11-21 14:10:05 +08:00
PIKA_HAL_GPIO_DIR_IN,
PIKA_HAL_GPIO_DIR_OUT,
_PIKA_HAL_GPIO_DIR_MAX,
} PIKA_HAL_GPIO_DIR;
typedef enum {
2022-11-21 17:04:23 +08:00
_PIKA_HAL_GPIO_PULL_UNUSED = 0,
2022-11-21 14:10:05 +08:00
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 struct {
PIKA_HAL_GPIO_DIR dir;
PIKA_HAL_GPIO_PULL pull;
} pika_hal_GPIO_config;
typedef struct {
} pika_hal_UART_config;
typedef struct {
} pika_hal_I2C_config;
typedef enum {
2022-11-21 17:04:23 +08:00
_PIKA_HAL_SPI_LSB_OR_MSB_UNUSED = 0,
2022-11-21 14:10:05 +08:00
PIKA_HAL_SPI_LSB,
PIKA_HAL_SPI_MSB,
_PIKA_HAL_SPI_LSB_OR_MSB_MAX,
} PIKA_HAL_SPI_LSB_OR_MSB;
typedef enum {
2022-11-21 17:04:23 +08:00
_PIKA_HAL_SPI_MASTER_OR_SLAVE_UNUSED = 0,
2022-11-21 14:10:05 +08:00
PIKA_HAL_SPI_MASTER,
PIKA_HAL_SPI_SLAVE,
_PIKA_HAL_SPI_MASTER_OR_SLAVE_MAX,
} PIKA_HAL_SPI_MASTER_OR_SLAVE;
typedef enum {
2022-11-21 17:04:23 +08:00
_PIKA_HAL_SPI_MODE_UNUSED = 0,
2022-11-21 14:10:05 +08:00
PIKA_HAL_SPI_MODE_0,
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 {
2022-11-21 17:04:23 +08:00
_PIKA_HAL_SPI_DATA_UNUSED = 0,
2022-11-21 14:10:05 +08:00
PIKA_HAL_SPI_DATA_WIDTH_8BIT,
PIKA_HAL_SPI_DATA_WIDTH_16BIT,
_PIKA_HAL_SPI_DATA_WIDTH_MAX,
} PIKA_HAL_SPI_DATA_WIDTH;
typedef enum {
2022-11-21 17:04:23 +08:00
_PIKA_HAL_SPI_SPEED_UNUSED = 0,
2022-11-21 14:10:05 +08:00
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_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;
2022-11-20 22:16:41 +08:00
typedef struct pika_dev_impl {
2022-11-21 02:03:01 +08:00
int (*open)(pika_dev* dev, char* name);
2022-11-20 22:16:41 +08:00
int (*close)(pika_dev* dev);
2022-11-21 02:03:01 +08:00
int (*read)(pika_dev* dev, void* buf, size_t count);
int (*write)(pika_dev* dev, void* buf, size_t count);
2022-11-21 14:10:05 +08:00
int (*ioctl)(pika_dev* dev, PIKA_HAL_IOCTL_CMD cmd, uintptr_t cfg);
2022-11-20 22:16:41 +08:00
} pika_dev_impl;
/* posix file like API */
2022-11-21 14:10:05 +08:00
pika_dev* pika_hal_open(PIKA_HAL_DEV_TYPE dev_type, char* name);
int pika_hal_close(pika_dev* dev);
2022-11-21 02:03:01 +08:00
int pika_hal_read(pika_dev* dev, void* buf, size_t len);
int pika_hal_write(pika_dev* dev, void* buf, size_t len);
2022-11-21 14:10:05 +08:00
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"
2022-11-20 22:16:41 +08:00
#endif