mirror of
https://gitee.com/Lyon1998/pikapython.git
synced 2025-01-15 17:02:53 +08:00
fix stddevice
This commit is contained in:
parent
5b65933398
commit
cfef8564f5
@ -160,6 +160,24 @@ class ADC(BaseDev):
|
||||
def platformDisable(self): ...
|
||||
|
||||
|
||||
class DAC(BaseDev):
|
||||
def __init__(self): ...
|
||||
|
||||
def setPin(self, pin: str):
|
||||
"""
|
||||
Use the name of the pin to select the DAC pin.
|
||||
example: `"PA0"`, `"PA1"` ...
|
||||
"""
|
||||
|
||||
def enable(self):
|
||||
"""Enable the DAC."""
|
||||
|
||||
def disable(self):
|
||||
"""Disable the DAC."""
|
||||
|
||||
def write(self, val:float):
|
||||
"""write the DAC value."""
|
||||
|
||||
class UART(BaseDev):
|
||||
def __init__(self): ...
|
||||
|
||||
|
@ -27,14 +27,43 @@ void PikaStdDevice_ADC_setPin(PikaObj* self, char* pin) {
|
||||
obj_setStr(self, "pin", pin);
|
||||
}
|
||||
|
||||
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_ADC, obj_getStr(self, "pin"));
|
||||
if (NULL == dev) {
|
||||
__platform_printf("Error: open ADC '%s' failed.\r\n",
|
||||
obj_getStr(self, "pin"));
|
||||
}
|
||||
obj_setPtr(self, "pika_dev", dev);
|
||||
return dev;
|
||||
}
|
||||
|
||||
void PikaStdDevice_ADC_platformEnable(PikaObj* self) {
|
||||
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
|
||||
pika_dev* dev = _get_dev(self);
|
||||
if (NULL == dev) {
|
||||
__platform_printf("Error: open ADC '%s' failed.\r\n",
|
||||
obj_getStr(self, "pin"));
|
||||
return;
|
||||
}
|
||||
pika_hal_ADC_config cfg = {0};
|
||||
cfg.continue_or_single = PIKA_HAL_ADC_SINGLE;
|
||||
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_CONFIG, &cfg);
|
||||
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_ENABLE);
|
||||
}
|
||||
|
||||
void PikaStdDevice_ADC_platformDisable(PikaObj* self) {
|
||||
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
|
||||
pika_dev* dev = _get_dev(self);
|
||||
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_DISABLE);
|
||||
}
|
||||
|
||||
void PikaStdDevice_ADC_platformRead(PikaObj* self) {
|
||||
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
|
||||
pika_dev* dev = _get_dev(self);
|
||||
pika_hal_ADC_config* cfg = (pika_hal_ADC_config*)dev->ioctl_config;
|
||||
uint32_t val_i = 0;
|
||||
pika_hal_read(dev, &val_i, sizeof(val_i));
|
||||
pika_float val = (pika_float)val_i / (pika_float)cfg->max * cfg->vref;
|
||||
val = val_i * obj_setFloat(self, "val", val);
|
||||
}
|
||||
|
48
package/PikaStdDevice/PikaStdDevice_DAC.c
Normal file
48
package/PikaStdDevice/PikaStdDevice_DAC.c
Normal file
@ -0,0 +1,48 @@
|
||||
#include "PikaStdDevice_DAC.h"
|
||||
#include "BaseObj.h"
|
||||
#include "pika_hal.h"
|
||||
|
||||
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_DAC, obj_getStr(self, "pin"));
|
||||
if (NULL == dev) {
|
||||
__platform_printf("Error: open DAC '%s' failed.\r\n",
|
||||
obj_getStr(self, "pin"));
|
||||
}
|
||||
obj_setPtr(self, "pika_dev", dev);
|
||||
return dev;
|
||||
}
|
||||
|
||||
void PikaStdDevice_DAC_enable(PikaObj* self) {
|
||||
pika_dev* dev = _get_dev(self);
|
||||
pika_hal_DAC_config cfg = {0};
|
||||
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_CONFIG, &cfg);
|
||||
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_ENABLE);
|
||||
}
|
||||
|
||||
void PikaStdDevice_DAC_setPin(PikaObj* self, char* pin) {
|
||||
obj_setStr(self, "pin", pin);
|
||||
}
|
||||
|
||||
void PikaStdDevice_DAC_disable(PikaObj* self) {
|
||||
pika_dev* dev = _get_dev(self);
|
||||
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_DISABLE);
|
||||
}
|
||||
|
||||
void PikaStdDevice_DAC_init(PikaObj* self) {
|
||||
obj_setStr(self, "pin", "PA0");
|
||||
}
|
||||
|
||||
void PikaStdDevice_DAC___init__(PikaObj* self) {
|
||||
PikaStdDevice_DAC_init(self);
|
||||
}
|
||||
|
||||
void PikaStdDevice_DAC_write(PikaObj* self, pika_float val) {
|
||||
pika_dev* dev = _get_dev(self);
|
||||
pika_hal_DAC_config* cfg = (pika_hal_DAC_config*)dev->ioctl_config;
|
||||
uint32_t val_i = (val / cfg->vref) * cfg->max;
|
||||
pika_hal_write(dev, &val_i, sizeof val_i);
|
||||
}
|
@ -77,14 +77,15 @@ void PikaStdDevice_GPIO_setPull(PikaObj* self, char* pull) {
|
||||
}
|
||||
}
|
||||
|
||||
static pika_dev* _get_dev(PikaObj* self){
|
||||
static pika_dev* _get_dev(PikaObj* self) {
|
||||
pika_dev* dev = obj_getPtr(self, "pika_dev");
|
||||
if(NULL != 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"));
|
||||
if (NULL == dev) {
|
||||
__platform_printf("Error: open GPIO '%s' failed.\r\n",
|
||||
obj_getStr(self, "pin"));
|
||||
}
|
||||
obj_setPtr(self, "pika_dev", dev);
|
||||
return dev;
|
||||
@ -119,17 +120,17 @@ void PikaStdDevice_GPIO_platformHigh(PikaObj* self) {
|
||||
void PikaStdDevice_GPIO_platformSetMode(PikaObj* self) {
|
||||
char* mode = obj_getStr(self, "mode");
|
||||
pika_hal_GPIO_config cfg = {0};
|
||||
if(strEqu(mode, "in")){
|
||||
if (strEqu(mode, "in")) {
|
||||
cfg.dir = PIKA_HAL_GPIO_DIR_IN;
|
||||
}else{
|
||||
} else {
|
||||
cfg.dir = PIKA_HAL_GPIO_DIR_OUT;
|
||||
}
|
||||
pika_dev* dev = _get_dev(self);
|
||||
char* pull = obj_getStr(self, "pull");
|
||||
if (strEqu(pull, "up")){
|
||||
if (strEqu(pull, "up")) {
|
||||
cfg.pull = PIKA_HAL_GPIO_PULL_UP;
|
||||
}
|
||||
if (strEqu(pull, "down")){
|
||||
if (strEqu(pull, "down")) {
|
||||
cfg.pull = PIKA_HAL_GPIO_PULL_DOWN;
|
||||
}
|
||||
if (strEqu(pull, "none")) {
|
||||
|
@ -44,20 +44,6 @@ int PikaStdDevice_PWM_getFrequency(PikaObj* self) {
|
||||
return obj_getInt(self, "freq");
|
||||
}
|
||||
|
||||
void PikaStdDevice_PWM_platformEnable(PikaObj* self) {
|
||||
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
void PikaStdDevice_PWM_platformSetDuty(PikaObj* self) {
|
||||
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
void PikaStdDevice_PWM_platformSetFrequency(PikaObj* self) {
|
||||
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
void PikaStdDevice_PWM_platformDisable(PikaObj* self) {
|
||||
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
char* PikaStdDevice_PWM_getName(PikaObj* self) {
|
||||
return obj_getStr(self, "name");
|
||||
}
|
||||
@ -77,3 +63,46 @@ int PikaStdDevice_PWM_getChannel(PikaObj* self) {
|
||||
void PikaStdDevice_PWM_setFreq(PikaObj* self, int freq) {
|
||||
PikaStdDevice_PWM_setFrequency(self, freq);
|
||||
}
|
||||
|
||||
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_PWM, obj_getStr(self, "pin"));
|
||||
if (NULL == dev) {
|
||||
__platform_printf("Error: open PWM '%s' failed.\r\n",
|
||||
obj_getStr(self, "pin"));
|
||||
}
|
||||
obj_setPtr(self, "pika_dev", dev);
|
||||
return dev;
|
||||
}
|
||||
|
||||
void PikaStdDevice_PWM_platformEnable(PikaObj* self) {
|
||||
pika_dev* dev = _get_dev(self);
|
||||
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_ENABLE);
|
||||
}
|
||||
|
||||
void PikaStdDevice_PWM_platformSetDuty(PikaObj* self) {
|
||||
pika_float duty = obj_getFloat(self, "duty"); // 0.0 ~ 1.0
|
||||
uint32_t freq = obj_getInt(self, "freq"); // Hz
|
||||
pika_hal_PWM_config cfg = {0};
|
||||
/* ns */
|
||||
cfg.duty = (uint32_t)(1000000000.0f / freq * duty);
|
||||
pika_hal_ioctl(_get_dev(self), PIKA_HAL_IOCTL_CONFIG, &cfg);
|
||||
}
|
||||
|
||||
void PikaStdDevice_PWM_platformSetFrequency(PikaObj* self) {
|
||||
uint32_t freq = obj_getInt(self, "freq"); // Hz
|
||||
pika_float duty = obj_getFloat(self, "duty"); // 0.0 ~ 1.0
|
||||
pika_hal_PWM_config cfg = {0};
|
||||
/* ns */
|
||||
cfg.period = (uint32_t)(1000000000.0f / freq);
|
||||
cfg.duty = (uint32_t)(1000000000.0f / freq * duty);
|
||||
pika_hal_ioctl(_get_dev(self), PIKA_HAL_IOCTL_CONFIG, &cfg);
|
||||
}
|
||||
|
||||
void PikaStdDevice_PWM_platformDisable(PikaObj* self) {
|
||||
pika_dev* dev = _get_dev(self);
|
||||
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_DISABLE);
|
||||
}
|
||||
|
@ -27,20 +27,23 @@ static size_t _pika_hal_dev_config_size(PIKA_HAL_DEV_TYPE dev_type) {
|
||||
|
||||
pika_dev* pika_hal_open(PIKA_HAL_DEV_TYPE dev_type, char* name) {
|
||||
int ret = -1;
|
||||
pika_dev* dev = NULL;
|
||||
if (dev_type >= _PIKA_DEV_TYPE_MAX) {
|
||||
__platform_printf("Error: dev_type invalied.\r\n");
|
||||
goto __exit;
|
||||
}
|
||||
pika_dev_impl* impl = _pika_dev_get_impl(dev_type);
|
||||
pika_dev* dev = (pika_dev*)pikaMalloc(sizeof(pika_dev));
|
||||
dev = (pika_dev*)pikaMalloc(sizeof(pika_dev));
|
||||
if (dev == NULL) {
|
||||
goto __exit;
|
||||
}
|
||||
memset(dev, 0, sizeof(pika_dev));
|
||||
dev->type = dev_type;
|
||||
dev->ioctl_config = pikaMalloc(_pika_hal_dev_config_size(dev_type));
|
||||
if (dev->ioctl_config == NULL) {
|
||||
goto __exit;
|
||||
}
|
||||
memset(dev->ioctl_config, 0, _pika_hal_dev_config_size(dev_type));
|
||||
ret = impl->open(dev, name);
|
||||
__exit:
|
||||
if (0 == ret) {
|
||||
@ -142,11 +145,19 @@ int pika_hal_ioctl(pika_dev* dev, PIKA_HAL_IOCTL_CMD cmd, ...) {
|
||||
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_origin, dev->ioctl_config);
|
||||
if (ret == 0) {
|
||||
if (cmd_origin == PIKA_HAL_IOCTL_ENABLE) {
|
||||
dev->is_enabled = 1;
|
||||
}
|
||||
if (cmd_origin == PIKA_HAL_IOCTL_DISABLE) {
|
||||
dev->is_enabled = 0;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -160,12 +171,14 @@ int pika_hal_ioctl(pika_dev* dev, PIKA_HAL_IOCTL_CMD cmd, ...) {
|
||||
} \
|
||||
} else { \
|
||||
/* use input value */ \
|
||||
src->item = dst->item; \
|
||||
dst->item = src->item; \
|
||||
}
|
||||
|
||||
int pika_hal_GPIO_ioctl_merge_config(pika_hal_GPIO_config* dst,
|
||||
pika_hal_GPIO_config* src) {
|
||||
// printf("before merge: dst->dir=%d, src->dir=%d\r\n", dst->dir, src->dir);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(dir, PIKA_HAL_GPIO_DIR_IN);
|
||||
// printf("after merge: dst->dir=%d, src->dir=%d\r\n", dst->dir, src->dir);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(pull, PIKA_HAL_GPIO_PULL_NONE);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(speed, PIKA_HAL_GPIO_SPEED_10M);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(event_callback_rising, NULL);
|
||||
@ -190,27 +203,46 @@ int pika_hal_SPI_ioctl_merge_config(pika_hal_SPI_config* dst,
|
||||
_IOCTL_CONFIG_USE_DEFAULT(mode, PIKA_HAL_SPI_MODE_0);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(data_width, PIKA_HAL_SPI_DATA_WIDTH_8);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(speed, PIKA_HAL_SPI_SPEED_2M);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(timeout, PIKA_HAL_SPI_TIMEOUT_1000MS);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pika_hal_IIC_ioctl_merge_config(pika_hal_IIC_config* dst,
|
||||
pika_hal_IIC_config* src) {
|
||||
_IOCTL_CONFIG_USE_DEFAULT(addr, 0);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(address_width, PIKA_HAL_IIC_ADDRESS_WIDTH_7BIT);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(master_or_slave, PIKA_HAL_IIC_MASTER);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(slave_addr, 0);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(mem_addr_ena, PIKA_HAL_IIC_MEM_ADDR_ENA_DISABLE);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(mem_addr_size, PIKA_HAL_IIC_MEM_ADDR_SIZE_8BIT);
|
||||
dst->mem_addr = src->mem_addr;
|
||||
_IOCTL_CONFIG_USE_DEFAULT(speed, PIKA_HAL_IIC_SPEED_100K);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(timeout, PIKA_HAL_IIC_TIMEOUT_1000MS);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pika_hal_PWM_ioctl_merge_config(pika_hal_PWM_config* dst,
|
||||
pika_hal_PWM_config* src) {
|
||||
_IOCTL_CONFIG_USE_DEFAULT(channel, PIKA_HAL_PWM_CHANNEL_0);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(period, PIKA_HAL_PWM_PERIOD_1MS * 10);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(duty, PIKA_HAL_PWM_PERIOD_1MS * 5);
|
||||
// _IOCTL_CONFIG_USE_DEFAULT(duty, 0);
|
||||
dst->duty = src->duty;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pika_hal_ADC_ioctl_merge_config(pika_hal_ADC_config* dst,
|
||||
pika_hal_ADC_config* src) {
|
||||
_IOCTL_CONFIG_USE_DEFAULT(channel, PIKA_HAL_ADC_CHANNEL_0);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(resolution, PIKA_HAL_ADC_RESOLUTION_12);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(sampling_resolution, PIKA_HAL_ADC_RESOLUTION_12);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(sampling_freq, PIKA_HAL_ADC_SAMPLING_FREQ_100);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(continue_or_single, PIKA_HAL_ADC_SINGLE);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(vref, (pika_float)3.3);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(max, 8192);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pika_hal_DAC_ioctl_merge_config(pika_hal_DAC_config* dst,
|
||||
pika_hal_DAC_config* src) {
|
||||
_IOCTL_CONFIG_USE_DEFAULT(speed, PIKA_HAL_DAC_SPEED_1K * 8);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(sampling_resolution, PIKA_HAL_DAC_RESOLUTION_12);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(vref, (pika_float)3.3);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(max, 3300000);
|
||||
return 0;
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
#define _PIKA_DEV_HAL_H
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include "PikaObj.h"
|
||||
|
||||
/*
|
||||
* pika_hal is a C Device HAL lib for PikaScript modules.
|
||||
@ -15,6 +16,7 @@ typedef enum {
|
||||
|
||||
typedef struct {
|
||||
PIKA_HAL_DEV_TYPE type;
|
||||
PIKA_BOOL is_enabled;
|
||||
void* ioctl_config;
|
||||
void* platform_data;
|
||||
} pika_dev;
|
||||
@ -105,6 +107,21 @@ typedef struct {
|
||||
} pika_hal_UART_config;
|
||||
|
||||
typedef uint32_t PIKA_HAL_IIC_SLAVE_ADDR;
|
||||
typedef uint32_t PIKA_HAL_IIC_MEM_ADDR;
|
||||
|
||||
typedef enum PIKA_HAL_IIC_MEM_ADDR_SIZE {
|
||||
_PIKA_HAL_IIC_MEM_ADDR_SIZE_UNUSED = 0,
|
||||
PIKA_HAL_IIC_MEM_ADDR_SIZE_8BIT = 1,
|
||||
PIKA_HAL_IIC_MEM_ADDR_SIZE_16BIT = 2,
|
||||
PIKA_HAL_IIC_MEM_ADDR_SIZE_24BIT = 3,
|
||||
PIKA_HAL_IIC_MEM_ADDR_SIZE_32BIT = 4,
|
||||
} PIKA_HAL_IIC_MEM_ADDR_SIZE;
|
||||
|
||||
typedef enum PIKA_HAL_IIC_MEM_ADDR_ENA {
|
||||
_PIKA_HAL_IIC_MEM_ADDR_ENA_UNUSED = 0,
|
||||
PIKA_HAL_IIC_MEM_ADDR_ENA_DISABLE,
|
||||
PIKA_HAL_IIC_MEM_ADDR_ENA_ENABLE,
|
||||
} PIKA_HAL_IIC_MEM_ADDR_ENA;
|
||||
|
||||
typedef enum {
|
||||
_PIKA_HAL_IIC_SPEED_UNUSED = 0,
|
||||
@ -113,11 +130,57 @@ typedef enum {
|
||||
PIKA_HAL_IIC_SPEED_1M = 1000000,
|
||||
} PIKA_HAL_IIC_SPEED;
|
||||
|
||||
typedef enum PIKA_HAL_IIC_MASTER_OR_SLAVE {
|
||||
_PIKA_HAL_IIC_MASTER_OR_SLAVE_UNUSED = 0,
|
||||
PIKA_HAL_IIC_MASTER,
|
||||
PIKA_HAL_IIC_SLAVE,
|
||||
} PIKA_HAL_IIC_MASTER_OR_SLAVE;
|
||||
|
||||
typedef enum PIKA_HAL_IIC_ADDRESS_WIDTH {
|
||||
_PIKA_HAL_IIC_ADDRESS_WIDTH_UNUSED = 0,
|
||||
PIKA_HAL_IIC_ADDRESS_WIDTH_7BIT = 7,
|
||||
PIKA_HAL_IIC_ADDRESS_WIDTH_10BIT = 10,
|
||||
} PIKA_HAL_IIC_ADDRESS_WIDTH;
|
||||
|
||||
typedef enum PIKA_HAL_IIC_TIMEOUT {
|
||||
_PIKA_HAL_IIC_TIMEOUT_UNUSED = 0,
|
||||
PIKA_HAL_IIC_TIMEOUT_1MS = 1,
|
||||
PIKA_HAL_IIC_TIMEOUT_2MS = 2,
|
||||
PIKA_HAL_IIC_TIMEOUT_5MS = 5,
|
||||
PIKA_HAL_IIC_TIMEOUT_10MS = 10,
|
||||
PIKA_HAL_IIC_TIMEOUT_20MS = 20,
|
||||
PIKA_HAL_IIC_TIMEOUT_50MS = 50,
|
||||
PIKA_HAL_IIC_TIMEOUT_100MS = 100,
|
||||
PIKA_HAL_IIC_TIMEOUT_200MS = 200,
|
||||
PIKA_HAL_IIC_TIMEOUT_500MS = 500,
|
||||
PIKA_HAL_IIC_TIMEOUT_1000MS = 1000,
|
||||
} PIKA_HAL_IIC_TIMEOUT;
|
||||
|
||||
typedef struct {
|
||||
PIKA_HAL_IIC_SLAVE_ADDR addr;
|
||||
PIKA_HAL_IIC_ADDRESS_WIDTH address_width;
|
||||
PIKA_HAL_IIC_MASTER_OR_SLAVE master_or_slave;
|
||||
PIKA_HAL_IIC_SLAVE_ADDR slave_addr;
|
||||
PIKA_HAL_IIC_MEM_ADDR_ENA mem_addr_ena;
|
||||
PIKA_HAL_IIC_MEM_ADDR mem_addr;
|
||||
PIKA_HAL_IIC_MEM_ADDR_SIZE mem_addr_size;
|
||||
PIKA_HAL_IIC_SPEED speed;
|
||||
PIKA_HAL_IIC_TIMEOUT timeout;
|
||||
} pika_hal_IIC_config;
|
||||
|
||||
typedef enum PIKA_HAL_SPI_TIMEOUT {
|
||||
_PIKA_HAL_SPI_TIMEOUT_UNUSED = 0,
|
||||
PIKA_HAL_SPI_TIMEOUT_1MS = 1,
|
||||
PIKA_HAL_SPI_TIMEOUT_2MS = 2,
|
||||
PIKA_HAL_SPI_TIMEOUT_5MS = 5,
|
||||
PIKA_HAL_SPI_TIMEOUT_10MS = 10,
|
||||
PIKA_HAL_SPI_TIMEOUT_20MS = 20,
|
||||
PIKA_HAL_SPI_TIMEOUT_50MS = 50,
|
||||
PIKA_HAL_SPI_TIMEOUT_100MS = 100,
|
||||
PIKA_HAL_SPI_TIMEOUT_200MS = 200,
|
||||
PIKA_HAL_SPI_TIMEOUT_500MS = 500,
|
||||
PIKA_HAL_SPI_TIMEOUT_1000MS = 1000,
|
||||
} PIKA_HAL_SPI_TIMEOUT;
|
||||
|
||||
typedef enum {
|
||||
_PIKA_HAL_SPI_LSB_OR_MSB_UNUSED = 0,
|
||||
PIKA_HAL_SPI_LSB,
|
||||
@ -161,22 +224,9 @@ typedef struct {
|
||||
PIKA_HAL_SPI_MODE mode;
|
||||
PIKA_HAL_SPI_DATA_WIDTH data_width;
|
||||
PIKA_HAL_SPI_SPEED speed;
|
||||
PIKA_HAL_SPI_TIMEOUT timeout;
|
||||
} pika_hal_SPI_config;
|
||||
|
||||
typedef enum {
|
||||
_PIKA_HAL_ADC_CHANNEL_UNUSED = 0,
|
||||
PIKA_HAL_ADC_CHANNEL_0,
|
||||
PIKA_HAL_ADC_CHANNEL_1,
|
||||
PIKA_HAL_ADC_CHANNEL_2,
|
||||
PIKA_HAL_ADC_CHANNEL_3,
|
||||
PIKA_HAL_ADC_CHANNEL_4,
|
||||
PIKA_HAL_ADC_CHANNEL_5,
|
||||
PIKA_HAL_ADC_CHANNEL_6,
|
||||
PIKA_HAL_ADC_CHANNEL_7,
|
||||
PIKA_HAL_ADC_CHANNEL_TEMP,
|
||||
PIKA_HAL_ADC_CHANNEL_VBAT,
|
||||
} PIKA_HAL_ADC_CHANNEL;
|
||||
|
||||
typedef enum {
|
||||
_PIKA_HAL_ADC_RESOLUTION_UNUSED = 0,
|
||||
PIKA_HAL_ADC_RESOLUTION_8 = 8,
|
||||
@ -186,11 +236,65 @@ typedef enum {
|
||||
PIKA_HAL_ADC_RESOLUTION_16 = 16,
|
||||
} PIKA_HAL_ADC_RESOLUTION;
|
||||
|
||||
typedef enum PIKA_HAL_ADC_SAMPLING_FREQ {
|
||||
_PIKA_HAL_ADC_SAMPLING_FREQ_UNUSED = 0,
|
||||
PIKA_HAL_ADC_SAMPLING_FREQ_100 = 100,
|
||||
PIKA_HAL_ADC_SAMPLING_FREQ_1K = 1000,
|
||||
PIKA_HAL_ADC_SAMPLING_FREQ_10K = 10000,
|
||||
PIKA_HAL_ADC_SAMPLING_FREQ_100K = 100000,
|
||||
PIKA_HAL_ADC_SAMPLING_FREQ_1M = 1000000,
|
||||
PIKA_HAL_ADC_SAMPLING_FREQ_10M = 10000000,
|
||||
} PIKA_HAL_ADC_SAMPLING_FREQ;
|
||||
|
||||
typedef enum PIKA_HAL_ADC_CONTINUOU_OR_SINGLE {
|
||||
_PIKA_HAL_ADC_CONTINUOU_OR_SINGLE_UNUSED = 0,
|
||||
PIKA_HAL_ADC_SINGLE,
|
||||
PIKA_HAL_ADC_CONTINUOU,
|
||||
} PIKA_HAL_ADC_CONTINUOU_OR_SINGLE;
|
||||
|
||||
typedef pika_float PIKA_HAL_ADC_VREF;
|
||||
typedef uint32_t PIKA_HAL_ADC_MAX;
|
||||
|
||||
typedef struct {
|
||||
PIKA_HAL_ADC_CHANNEL channel;
|
||||
PIKA_HAL_ADC_RESOLUTION resolution;
|
||||
PIKA_HAL_ADC_RESOLUTION sampling_resolution;
|
||||
PIKA_HAL_ADC_SAMPLING_FREQ sampling_freq;
|
||||
PIKA_HAL_ADC_CONTINUOU_OR_SINGLE continue_or_single;
|
||||
PIKA_HAL_ADC_MAX max;
|
||||
PIKA_HAL_ADC_VREF vref;
|
||||
} pika_hal_ADC_config;
|
||||
|
||||
typedef enum {
|
||||
_PIKA_HAL_DAC_RESOLUTION_UNUSED = 0,
|
||||
PIKA_HAL_DAC_RESOLUTION_8 = 8,
|
||||
PIKA_HAL_DAC_RESOLUTION_10 = 10,
|
||||
PIKA_HAL_DAC_RESOLUTION_12 = 12,
|
||||
PIKA_HAL_DAC_RESOLUTION_14 = 14,
|
||||
PIKA_HAL_DAC_RESOLUTION_16 = 16,
|
||||
} PIKA_HAL_DAC_RESOLUTION;
|
||||
|
||||
typedef enum PIKA_HAL_DAC_SPEED {
|
||||
_PIKA_HAL_DAC_SPEED_UNUSED = 0,
|
||||
PIKA_HAL_DAC_SPEED_100 = 100,
|
||||
PIKA_HAL_DAC_SPEED_1K = 1000,
|
||||
PIKA_HAL_DAC_SPEED_10K = 10000,
|
||||
PIKA_HAL_DAC_SPEED_100K = 100000,
|
||||
PIKA_HAL_DAC_SPEED_1M = 1000000,
|
||||
PIKA_HAL_DAC_SPEED_10M = 10000000,
|
||||
PIKA_HAL_DAC_SPEED_20M = 20000000,
|
||||
PIKA_HAL_DAC_SPEED_50M = 50000000,
|
||||
PIKA_HAL_DAC_SPEED_100M = 100000000,
|
||||
} PIKA_HAL_DAC_SPEED;
|
||||
|
||||
typedef pika_float PIKA_HAL_DAC_VREF;
|
||||
typedef uint32_t PIKA_HAL_DAC_MAX;
|
||||
|
||||
typedef struct pika_hal_DAC_config {
|
||||
PIKA_HAL_DAC_RESOLUTION sampling_resolution;
|
||||
PIKA_HAL_DAC_SPEED speed;
|
||||
PIKA_HAL_DAC_MAX max;
|
||||
PIKA_HAL_DAC_VREF vref;
|
||||
} pika_hal_DAC_config;
|
||||
|
||||
typedef enum {
|
||||
_PIKA_HAL_DAC_UNUSED = 0,
|
||||
PIKA_HAL_PWM_CHANNEL_0,
|
||||
@ -218,7 +322,6 @@ typedef enum {
|
||||
} PIKA_HAL_PWM_DUTY;
|
||||
|
||||
typedef struct {
|
||||
PIKA_HAL_PWM_CHANNEL channel;
|
||||
PIKA_HAL_PWM_PERIOD period;
|
||||
PIKA_HAL_PWM_DUTY duty;
|
||||
} pika_hal_PWM_config;
|
||||
|
@ -7,6 +7,7 @@ pika_hal_table_add(UART)
|
||||
pika_hal_table_add(IIC)
|
||||
pika_hal_table_add(SPI)
|
||||
pika_hal_table_add(ADC)
|
||||
pika_hal_table_add(DAC)
|
||||
pika_hal_table_add(PWM)
|
||||
|
||||
/* clang-format on */
|
||||
|
@ -160,6 +160,24 @@ class ADC(BaseDev):
|
||||
def platformDisable(self): ...
|
||||
|
||||
|
||||
class DAC(BaseDev):
|
||||
def __init__(self): ...
|
||||
|
||||
def setPin(self, pin: str):
|
||||
"""
|
||||
Use the name of the pin to select the DAC pin.
|
||||
example: `"PA0"`, `"PA1"` ...
|
||||
"""
|
||||
|
||||
def enable(self):
|
||||
"""Enable the DAC."""
|
||||
|
||||
def disable(self):
|
||||
"""Disable the DAC."""
|
||||
|
||||
def write(self, val:float):
|
||||
"""write the DAC value."""
|
||||
|
||||
class UART(BaseDev):
|
||||
def __init__(self): ...
|
||||
|
||||
|
@ -27,14 +27,43 @@ void PikaStdDevice_ADC_setPin(PikaObj* self, char* pin) {
|
||||
obj_setStr(self, "pin", pin);
|
||||
}
|
||||
|
||||
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_ADC, obj_getStr(self, "pin"));
|
||||
if (NULL == dev) {
|
||||
__platform_printf("Error: open ADC '%s' failed.\r\n",
|
||||
obj_getStr(self, "pin"));
|
||||
}
|
||||
obj_setPtr(self, "pika_dev", dev);
|
||||
return dev;
|
||||
}
|
||||
|
||||
void PikaStdDevice_ADC_platformEnable(PikaObj* self) {
|
||||
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
|
||||
pika_dev* dev = _get_dev(self);
|
||||
if (NULL == dev) {
|
||||
__platform_printf("Error: open ADC '%s' failed.\r\n",
|
||||
obj_getStr(self, "pin"));
|
||||
return;
|
||||
}
|
||||
pika_hal_ADC_config cfg = {0};
|
||||
cfg.continue_or_single = PIKA_HAL_ADC_SINGLE;
|
||||
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_CONFIG, &cfg);
|
||||
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_ENABLE);
|
||||
}
|
||||
|
||||
void PikaStdDevice_ADC_platformDisable(PikaObj* self) {
|
||||
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
|
||||
pika_dev* dev = _get_dev(self);
|
||||
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_DISABLE);
|
||||
}
|
||||
|
||||
void PikaStdDevice_ADC_platformRead(PikaObj* self) {
|
||||
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
|
||||
pika_dev* dev = _get_dev(self);
|
||||
pika_hal_ADC_config* cfg = (pika_hal_ADC_config*)dev->ioctl_config;
|
||||
uint32_t val_i = 0;
|
||||
pika_hal_read(dev, &val_i, sizeof(val_i));
|
||||
pika_float val = (pika_float)val_i / (pika_float)cfg->max * cfg->vref;
|
||||
val = val_i * obj_setFloat(self, "val", val);
|
||||
}
|
||||
|
@ -0,0 +1,48 @@
|
||||
#include "PikaStdDevice_DAC.h"
|
||||
#include "BaseObj.h"
|
||||
#include "pika_hal.h"
|
||||
|
||||
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_DAC, obj_getStr(self, "pin"));
|
||||
if (NULL == dev) {
|
||||
__platform_printf("Error: open DAC '%s' failed.\r\n",
|
||||
obj_getStr(self, "pin"));
|
||||
}
|
||||
obj_setPtr(self, "pika_dev", dev);
|
||||
return dev;
|
||||
}
|
||||
|
||||
void PikaStdDevice_DAC_enable(PikaObj* self) {
|
||||
pika_dev* dev = _get_dev(self);
|
||||
pika_hal_DAC_config cfg = {0};
|
||||
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_CONFIG, &cfg);
|
||||
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_ENABLE);
|
||||
}
|
||||
|
||||
void PikaStdDevice_DAC_setPin(PikaObj* self, char* pin) {
|
||||
obj_setStr(self, "pin", pin);
|
||||
}
|
||||
|
||||
void PikaStdDevice_DAC_disable(PikaObj* self) {
|
||||
pika_dev* dev = _get_dev(self);
|
||||
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_DISABLE);
|
||||
}
|
||||
|
||||
void PikaStdDevice_DAC_init(PikaObj* self) {
|
||||
obj_setStr(self, "pin", "PA0");
|
||||
}
|
||||
|
||||
void PikaStdDevice_DAC___init__(PikaObj* self) {
|
||||
PikaStdDevice_DAC_init(self);
|
||||
}
|
||||
|
||||
void PikaStdDevice_DAC_write(PikaObj* self, pika_float val) {
|
||||
pika_dev* dev = _get_dev(self);
|
||||
pika_hal_DAC_config* cfg = (pika_hal_DAC_config*)dev->ioctl_config;
|
||||
uint32_t val_i = (val / cfg->vref) * cfg->max;
|
||||
pika_hal_write(dev, &val_i, sizeof val_i);
|
||||
}
|
@ -77,14 +77,15 @@ void PikaStdDevice_GPIO_setPull(PikaObj* self, char* pull) {
|
||||
}
|
||||
}
|
||||
|
||||
static pika_dev* _get_dev(PikaObj* self){
|
||||
static pika_dev* _get_dev(PikaObj* self) {
|
||||
pika_dev* dev = obj_getPtr(self, "pika_dev");
|
||||
if(NULL != 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"));
|
||||
if (NULL == dev) {
|
||||
__platform_printf("Error: open GPIO '%s' failed.\r\n",
|
||||
obj_getStr(self, "pin"));
|
||||
}
|
||||
obj_setPtr(self, "pika_dev", dev);
|
||||
return dev;
|
||||
@ -119,17 +120,17 @@ void PikaStdDevice_GPIO_platformHigh(PikaObj* self) {
|
||||
void PikaStdDevice_GPIO_platformSetMode(PikaObj* self) {
|
||||
char* mode = obj_getStr(self, "mode");
|
||||
pika_hal_GPIO_config cfg = {0};
|
||||
if(strEqu(mode, "in")){
|
||||
if (strEqu(mode, "in")) {
|
||||
cfg.dir = PIKA_HAL_GPIO_DIR_IN;
|
||||
}else{
|
||||
} else {
|
||||
cfg.dir = PIKA_HAL_GPIO_DIR_OUT;
|
||||
}
|
||||
pika_dev* dev = _get_dev(self);
|
||||
char* pull = obj_getStr(self, "pull");
|
||||
if (strEqu(pull, "up")){
|
||||
if (strEqu(pull, "up")) {
|
||||
cfg.pull = PIKA_HAL_GPIO_PULL_UP;
|
||||
}
|
||||
if (strEqu(pull, "down")){
|
||||
if (strEqu(pull, "down")) {
|
||||
cfg.pull = PIKA_HAL_GPIO_PULL_DOWN;
|
||||
}
|
||||
if (strEqu(pull, "none")) {
|
||||
|
@ -44,20 +44,6 @@ int PikaStdDevice_PWM_getFrequency(PikaObj* self) {
|
||||
return obj_getInt(self, "freq");
|
||||
}
|
||||
|
||||
void PikaStdDevice_PWM_platformEnable(PikaObj* self) {
|
||||
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
void PikaStdDevice_PWM_platformSetDuty(PikaObj* self) {
|
||||
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
void PikaStdDevice_PWM_platformSetFrequency(PikaObj* self) {
|
||||
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
void PikaStdDevice_PWM_platformDisable(PikaObj* self) {
|
||||
ABSTRACT_METHOD_NEED_OVERRIDE_ERROR();
|
||||
}
|
||||
|
||||
char* PikaStdDevice_PWM_getName(PikaObj* self) {
|
||||
return obj_getStr(self, "name");
|
||||
}
|
||||
@ -77,3 +63,46 @@ int PikaStdDevice_PWM_getChannel(PikaObj* self) {
|
||||
void PikaStdDevice_PWM_setFreq(PikaObj* self, int freq) {
|
||||
PikaStdDevice_PWM_setFrequency(self, freq);
|
||||
}
|
||||
|
||||
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_PWM, obj_getStr(self, "pin"));
|
||||
if (NULL == dev) {
|
||||
__platform_printf("Error: open PWM '%s' failed.\r\n",
|
||||
obj_getStr(self, "pin"));
|
||||
}
|
||||
obj_setPtr(self, "pika_dev", dev);
|
||||
return dev;
|
||||
}
|
||||
|
||||
void PikaStdDevice_PWM_platformEnable(PikaObj* self) {
|
||||
pika_dev* dev = _get_dev(self);
|
||||
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_ENABLE);
|
||||
}
|
||||
|
||||
void PikaStdDevice_PWM_platformSetDuty(PikaObj* self) {
|
||||
pika_float duty = obj_getFloat(self, "duty"); // 0.0 ~ 1.0
|
||||
uint32_t freq = obj_getInt(self, "freq"); // Hz
|
||||
pika_hal_PWM_config cfg = {0};
|
||||
/* ns */
|
||||
cfg.duty = (uint32_t)(1000000000.0f / freq * duty);
|
||||
pika_hal_ioctl(_get_dev(self), PIKA_HAL_IOCTL_CONFIG, &cfg);
|
||||
}
|
||||
|
||||
void PikaStdDevice_PWM_platformSetFrequency(PikaObj* self) {
|
||||
uint32_t freq = obj_getInt(self, "freq"); // Hz
|
||||
pika_float duty = obj_getFloat(self, "duty"); // 0.0 ~ 1.0
|
||||
pika_hal_PWM_config cfg = {0};
|
||||
/* ns */
|
||||
cfg.period = (uint32_t)(1000000000.0f / freq);
|
||||
cfg.duty = (uint32_t)(1000000000.0f / freq * duty);
|
||||
pika_hal_ioctl(_get_dev(self), PIKA_HAL_IOCTL_CONFIG, &cfg);
|
||||
}
|
||||
|
||||
void PikaStdDevice_PWM_platformDisable(PikaObj* self) {
|
||||
pika_dev* dev = _get_dev(self);
|
||||
pika_hal_ioctl(dev, PIKA_HAL_IOCTL_DISABLE);
|
||||
}
|
||||
|
@ -27,20 +27,23 @@ static size_t _pika_hal_dev_config_size(PIKA_HAL_DEV_TYPE dev_type) {
|
||||
|
||||
pika_dev* pika_hal_open(PIKA_HAL_DEV_TYPE dev_type, char* name) {
|
||||
int ret = -1;
|
||||
pika_dev* dev = NULL;
|
||||
if (dev_type >= _PIKA_DEV_TYPE_MAX) {
|
||||
__platform_printf("Error: dev_type invalied.\r\n");
|
||||
goto __exit;
|
||||
}
|
||||
pika_dev_impl* impl = _pika_dev_get_impl(dev_type);
|
||||
pika_dev* dev = (pika_dev*)pikaMalloc(sizeof(pika_dev));
|
||||
dev = (pika_dev*)pikaMalloc(sizeof(pika_dev));
|
||||
if (dev == NULL) {
|
||||
goto __exit;
|
||||
}
|
||||
memset(dev, 0, sizeof(pika_dev));
|
||||
dev->type = dev_type;
|
||||
dev->ioctl_config = pikaMalloc(_pika_hal_dev_config_size(dev_type));
|
||||
if (dev->ioctl_config == NULL) {
|
||||
goto __exit;
|
||||
}
|
||||
memset(dev->ioctl_config, 0, _pika_hal_dev_config_size(dev_type));
|
||||
ret = impl->open(dev, name);
|
||||
__exit:
|
||||
if (0 == ret) {
|
||||
@ -142,11 +145,19 @@ int pika_hal_ioctl(pika_dev* dev, PIKA_HAL_IOCTL_CMD cmd, ...) {
|
||||
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_origin, dev->ioctl_config);
|
||||
if (ret == 0) {
|
||||
if (cmd_origin == PIKA_HAL_IOCTL_ENABLE) {
|
||||
dev->is_enabled = 1;
|
||||
}
|
||||
if (cmd_origin == PIKA_HAL_IOCTL_DISABLE) {
|
||||
dev->is_enabled = 0;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -160,12 +171,14 @@ int pika_hal_ioctl(pika_dev* dev, PIKA_HAL_IOCTL_CMD cmd, ...) {
|
||||
} \
|
||||
} else { \
|
||||
/* use input value */ \
|
||||
src->item = dst->item; \
|
||||
dst->item = src->item; \
|
||||
}
|
||||
|
||||
int pika_hal_GPIO_ioctl_merge_config(pika_hal_GPIO_config* dst,
|
||||
pika_hal_GPIO_config* src) {
|
||||
// printf("before merge: dst->dir=%d, src->dir=%d\r\n", dst->dir, src->dir);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(dir, PIKA_HAL_GPIO_DIR_IN);
|
||||
// printf("after merge: dst->dir=%d, src->dir=%d\r\n", dst->dir, src->dir);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(pull, PIKA_HAL_GPIO_PULL_NONE);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(speed, PIKA_HAL_GPIO_SPEED_10M);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(event_callback_rising, NULL);
|
||||
@ -190,27 +203,46 @@ int pika_hal_SPI_ioctl_merge_config(pika_hal_SPI_config* dst,
|
||||
_IOCTL_CONFIG_USE_DEFAULT(mode, PIKA_HAL_SPI_MODE_0);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(data_width, PIKA_HAL_SPI_DATA_WIDTH_8);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(speed, PIKA_HAL_SPI_SPEED_2M);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(timeout, PIKA_HAL_SPI_TIMEOUT_1000MS);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pika_hal_IIC_ioctl_merge_config(pika_hal_IIC_config* dst,
|
||||
pika_hal_IIC_config* src) {
|
||||
_IOCTL_CONFIG_USE_DEFAULT(addr, 0);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(address_width, PIKA_HAL_IIC_ADDRESS_WIDTH_7BIT);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(master_or_slave, PIKA_HAL_IIC_MASTER);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(slave_addr, 0);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(mem_addr_ena, PIKA_HAL_IIC_MEM_ADDR_ENA_DISABLE);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(mem_addr_size, PIKA_HAL_IIC_MEM_ADDR_SIZE_8BIT);
|
||||
dst->mem_addr = src->mem_addr;
|
||||
_IOCTL_CONFIG_USE_DEFAULT(speed, PIKA_HAL_IIC_SPEED_100K);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(timeout, PIKA_HAL_IIC_TIMEOUT_1000MS);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pika_hal_PWM_ioctl_merge_config(pika_hal_PWM_config* dst,
|
||||
pika_hal_PWM_config* src) {
|
||||
_IOCTL_CONFIG_USE_DEFAULT(channel, PIKA_HAL_PWM_CHANNEL_0);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(period, PIKA_HAL_PWM_PERIOD_1MS * 10);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(duty, PIKA_HAL_PWM_PERIOD_1MS * 5);
|
||||
// _IOCTL_CONFIG_USE_DEFAULT(duty, 0);
|
||||
dst->duty = src->duty;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pika_hal_ADC_ioctl_merge_config(pika_hal_ADC_config* dst,
|
||||
pika_hal_ADC_config* src) {
|
||||
_IOCTL_CONFIG_USE_DEFAULT(channel, PIKA_HAL_ADC_CHANNEL_0);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(resolution, PIKA_HAL_ADC_RESOLUTION_12);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(sampling_resolution, PIKA_HAL_ADC_RESOLUTION_12);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(sampling_freq, PIKA_HAL_ADC_SAMPLING_FREQ_100);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(continue_or_single, PIKA_HAL_ADC_SINGLE);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(vref, (pika_float)3.3);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(max, 8192);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int pika_hal_DAC_ioctl_merge_config(pika_hal_DAC_config* dst,
|
||||
pika_hal_DAC_config* src) {
|
||||
_IOCTL_CONFIG_USE_DEFAULT(speed, PIKA_HAL_DAC_SPEED_1K * 8);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(sampling_resolution, PIKA_HAL_DAC_RESOLUTION_12);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(vref, (pika_float)3.3);
|
||||
_IOCTL_CONFIG_USE_DEFAULT(max, 3300000);
|
||||
return 0;
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
#define _PIKA_DEV_HAL_H
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#include "PikaObj.h"
|
||||
|
||||
/*
|
||||
* pika_hal is a C Device HAL lib for PikaScript modules.
|
||||
@ -15,6 +16,7 @@ typedef enum {
|
||||
|
||||
typedef struct {
|
||||
PIKA_HAL_DEV_TYPE type;
|
||||
PIKA_BOOL is_enabled;
|
||||
void* ioctl_config;
|
||||
void* platform_data;
|
||||
} pika_dev;
|
||||
@ -105,6 +107,21 @@ typedef struct {
|
||||
} pika_hal_UART_config;
|
||||
|
||||
typedef uint32_t PIKA_HAL_IIC_SLAVE_ADDR;
|
||||
typedef uint32_t PIKA_HAL_IIC_MEM_ADDR;
|
||||
|
||||
typedef enum PIKA_HAL_IIC_MEM_ADDR_SIZE {
|
||||
_PIKA_HAL_IIC_MEM_ADDR_SIZE_UNUSED = 0,
|
||||
PIKA_HAL_IIC_MEM_ADDR_SIZE_8BIT = 1,
|
||||
PIKA_HAL_IIC_MEM_ADDR_SIZE_16BIT = 2,
|
||||
PIKA_HAL_IIC_MEM_ADDR_SIZE_24BIT = 3,
|
||||
PIKA_HAL_IIC_MEM_ADDR_SIZE_32BIT = 4,
|
||||
} PIKA_HAL_IIC_MEM_ADDR_SIZE;
|
||||
|
||||
typedef enum PIKA_HAL_IIC_MEM_ADDR_ENA {
|
||||
_PIKA_HAL_IIC_MEM_ADDR_ENA_UNUSED = 0,
|
||||
PIKA_HAL_IIC_MEM_ADDR_ENA_DISABLE,
|
||||
PIKA_HAL_IIC_MEM_ADDR_ENA_ENABLE,
|
||||
} PIKA_HAL_IIC_MEM_ADDR_ENA;
|
||||
|
||||
typedef enum {
|
||||
_PIKA_HAL_IIC_SPEED_UNUSED = 0,
|
||||
@ -113,11 +130,57 @@ typedef enum {
|
||||
PIKA_HAL_IIC_SPEED_1M = 1000000,
|
||||
} PIKA_HAL_IIC_SPEED;
|
||||
|
||||
typedef enum PIKA_HAL_IIC_MASTER_OR_SLAVE {
|
||||
_PIKA_HAL_IIC_MASTER_OR_SLAVE_UNUSED = 0,
|
||||
PIKA_HAL_IIC_MASTER,
|
||||
PIKA_HAL_IIC_SLAVE,
|
||||
} PIKA_HAL_IIC_MASTER_OR_SLAVE;
|
||||
|
||||
typedef enum PIKA_HAL_IIC_ADDRESS_WIDTH {
|
||||
_PIKA_HAL_IIC_ADDRESS_WIDTH_UNUSED = 0,
|
||||
PIKA_HAL_IIC_ADDRESS_WIDTH_7BIT = 7,
|
||||
PIKA_HAL_IIC_ADDRESS_WIDTH_10BIT = 10,
|
||||
} PIKA_HAL_IIC_ADDRESS_WIDTH;
|
||||
|
||||
typedef enum PIKA_HAL_IIC_TIMEOUT {
|
||||
_PIKA_HAL_IIC_TIMEOUT_UNUSED = 0,
|
||||
PIKA_HAL_IIC_TIMEOUT_1MS = 1,
|
||||
PIKA_HAL_IIC_TIMEOUT_2MS = 2,
|
||||
PIKA_HAL_IIC_TIMEOUT_5MS = 5,
|
||||
PIKA_HAL_IIC_TIMEOUT_10MS = 10,
|
||||
PIKA_HAL_IIC_TIMEOUT_20MS = 20,
|
||||
PIKA_HAL_IIC_TIMEOUT_50MS = 50,
|
||||
PIKA_HAL_IIC_TIMEOUT_100MS = 100,
|
||||
PIKA_HAL_IIC_TIMEOUT_200MS = 200,
|
||||
PIKA_HAL_IIC_TIMEOUT_500MS = 500,
|
||||
PIKA_HAL_IIC_TIMEOUT_1000MS = 1000,
|
||||
} PIKA_HAL_IIC_TIMEOUT;
|
||||
|
||||
typedef struct {
|
||||
PIKA_HAL_IIC_SLAVE_ADDR addr;
|
||||
PIKA_HAL_IIC_ADDRESS_WIDTH address_width;
|
||||
PIKA_HAL_IIC_MASTER_OR_SLAVE master_or_slave;
|
||||
PIKA_HAL_IIC_SLAVE_ADDR slave_addr;
|
||||
PIKA_HAL_IIC_MEM_ADDR_ENA mem_addr_ena;
|
||||
PIKA_HAL_IIC_MEM_ADDR mem_addr;
|
||||
PIKA_HAL_IIC_MEM_ADDR_SIZE mem_addr_size;
|
||||
PIKA_HAL_IIC_SPEED speed;
|
||||
PIKA_HAL_IIC_TIMEOUT timeout;
|
||||
} pika_hal_IIC_config;
|
||||
|
||||
typedef enum PIKA_HAL_SPI_TIMEOUT {
|
||||
_PIKA_HAL_SPI_TIMEOUT_UNUSED = 0,
|
||||
PIKA_HAL_SPI_TIMEOUT_1MS = 1,
|
||||
PIKA_HAL_SPI_TIMEOUT_2MS = 2,
|
||||
PIKA_HAL_SPI_TIMEOUT_5MS = 5,
|
||||
PIKA_HAL_SPI_TIMEOUT_10MS = 10,
|
||||
PIKA_HAL_SPI_TIMEOUT_20MS = 20,
|
||||
PIKA_HAL_SPI_TIMEOUT_50MS = 50,
|
||||
PIKA_HAL_SPI_TIMEOUT_100MS = 100,
|
||||
PIKA_HAL_SPI_TIMEOUT_200MS = 200,
|
||||
PIKA_HAL_SPI_TIMEOUT_500MS = 500,
|
||||
PIKA_HAL_SPI_TIMEOUT_1000MS = 1000,
|
||||
} PIKA_HAL_SPI_TIMEOUT;
|
||||
|
||||
typedef enum {
|
||||
_PIKA_HAL_SPI_LSB_OR_MSB_UNUSED = 0,
|
||||
PIKA_HAL_SPI_LSB,
|
||||
@ -161,22 +224,9 @@ typedef struct {
|
||||
PIKA_HAL_SPI_MODE mode;
|
||||
PIKA_HAL_SPI_DATA_WIDTH data_width;
|
||||
PIKA_HAL_SPI_SPEED speed;
|
||||
PIKA_HAL_SPI_TIMEOUT timeout;
|
||||
} pika_hal_SPI_config;
|
||||
|
||||
typedef enum {
|
||||
_PIKA_HAL_ADC_CHANNEL_UNUSED = 0,
|
||||
PIKA_HAL_ADC_CHANNEL_0,
|
||||
PIKA_HAL_ADC_CHANNEL_1,
|
||||
PIKA_HAL_ADC_CHANNEL_2,
|
||||
PIKA_HAL_ADC_CHANNEL_3,
|
||||
PIKA_HAL_ADC_CHANNEL_4,
|
||||
PIKA_HAL_ADC_CHANNEL_5,
|
||||
PIKA_HAL_ADC_CHANNEL_6,
|
||||
PIKA_HAL_ADC_CHANNEL_7,
|
||||
PIKA_HAL_ADC_CHANNEL_TEMP,
|
||||
PIKA_HAL_ADC_CHANNEL_VBAT,
|
||||
} PIKA_HAL_ADC_CHANNEL;
|
||||
|
||||
typedef enum {
|
||||
_PIKA_HAL_ADC_RESOLUTION_UNUSED = 0,
|
||||
PIKA_HAL_ADC_RESOLUTION_8 = 8,
|
||||
@ -186,11 +236,65 @@ typedef enum {
|
||||
PIKA_HAL_ADC_RESOLUTION_16 = 16,
|
||||
} PIKA_HAL_ADC_RESOLUTION;
|
||||
|
||||
typedef enum PIKA_HAL_ADC_SAMPLING_FREQ {
|
||||
_PIKA_HAL_ADC_SAMPLING_FREQ_UNUSED = 0,
|
||||
PIKA_HAL_ADC_SAMPLING_FREQ_100 = 100,
|
||||
PIKA_HAL_ADC_SAMPLING_FREQ_1K = 1000,
|
||||
PIKA_HAL_ADC_SAMPLING_FREQ_10K = 10000,
|
||||
PIKA_HAL_ADC_SAMPLING_FREQ_100K = 100000,
|
||||
PIKA_HAL_ADC_SAMPLING_FREQ_1M = 1000000,
|
||||
PIKA_HAL_ADC_SAMPLING_FREQ_10M = 10000000,
|
||||
} PIKA_HAL_ADC_SAMPLING_FREQ;
|
||||
|
||||
typedef enum PIKA_HAL_ADC_CONTINUOU_OR_SINGLE {
|
||||
_PIKA_HAL_ADC_CONTINUOU_OR_SINGLE_UNUSED = 0,
|
||||
PIKA_HAL_ADC_SINGLE,
|
||||
PIKA_HAL_ADC_CONTINUOU,
|
||||
} PIKA_HAL_ADC_CONTINUOU_OR_SINGLE;
|
||||
|
||||
typedef pika_float PIKA_HAL_ADC_VREF;
|
||||
typedef uint32_t PIKA_HAL_ADC_MAX;
|
||||
|
||||
typedef struct {
|
||||
PIKA_HAL_ADC_CHANNEL channel;
|
||||
PIKA_HAL_ADC_RESOLUTION resolution;
|
||||
PIKA_HAL_ADC_RESOLUTION sampling_resolution;
|
||||
PIKA_HAL_ADC_SAMPLING_FREQ sampling_freq;
|
||||
PIKA_HAL_ADC_CONTINUOU_OR_SINGLE continue_or_single;
|
||||
PIKA_HAL_ADC_MAX max;
|
||||
PIKA_HAL_ADC_VREF vref;
|
||||
} pika_hal_ADC_config;
|
||||
|
||||
typedef enum {
|
||||
_PIKA_HAL_DAC_RESOLUTION_UNUSED = 0,
|
||||
PIKA_HAL_DAC_RESOLUTION_8 = 8,
|
||||
PIKA_HAL_DAC_RESOLUTION_10 = 10,
|
||||
PIKA_HAL_DAC_RESOLUTION_12 = 12,
|
||||
PIKA_HAL_DAC_RESOLUTION_14 = 14,
|
||||
PIKA_HAL_DAC_RESOLUTION_16 = 16,
|
||||
} PIKA_HAL_DAC_RESOLUTION;
|
||||
|
||||
typedef enum PIKA_HAL_DAC_SPEED {
|
||||
_PIKA_HAL_DAC_SPEED_UNUSED = 0,
|
||||
PIKA_HAL_DAC_SPEED_100 = 100,
|
||||
PIKA_HAL_DAC_SPEED_1K = 1000,
|
||||
PIKA_HAL_DAC_SPEED_10K = 10000,
|
||||
PIKA_HAL_DAC_SPEED_100K = 100000,
|
||||
PIKA_HAL_DAC_SPEED_1M = 1000000,
|
||||
PIKA_HAL_DAC_SPEED_10M = 10000000,
|
||||
PIKA_HAL_DAC_SPEED_20M = 20000000,
|
||||
PIKA_HAL_DAC_SPEED_50M = 50000000,
|
||||
PIKA_HAL_DAC_SPEED_100M = 100000000,
|
||||
} PIKA_HAL_DAC_SPEED;
|
||||
|
||||
typedef pika_float PIKA_HAL_DAC_VREF;
|
||||
typedef uint32_t PIKA_HAL_DAC_MAX;
|
||||
|
||||
typedef struct pika_hal_DAC_config {
|
||||
PIKA_HAL_DAC_RESOLUTION sampling_resolution;
|
||||
PIKA_HAL_DAC_SPEED speed;
|
||||
PIKA_HAL_DAC_MAX max;
|
||||
PIKA_HAL_DAC_VREF vref;
|
||||
} pika_hal_DAC_config;
|
||||
|
||||
typedef enum {
|
||||
_PIKA_HAL_DAC_UNUSED = 0,
|
||||
PIKA_HAL_PWM_CHANNEL_0,
|
||||
@ -218,7 +322,6 @@ typedef enum {
|
||||
} PIKA_HAL_PWM_DUTY;
|
||||
|
||||
typedef struct {
|
||||
PIKA_HAL_PWM_CHANNEL channel;
|
||||
PIKA_HAL_PWM_PERIOD period;
|
||||
PIKA_HAL_PWM_DUTY duty;
|
||||
} pika_hal_PWM_config;
|
||||
|
@ -7,6 +7,7 @@ pika_hal_table_add(UART)
|
||||
pika_hal_table_add(IIC)
|
||||
pika_hal_table_add(SPI)
|
||||
pika_hal_table_add(ADC)
|
||||
pika_hal_table_add(DAC)
|
||||
pika_hal_table_add(PWM)
|
||||
|
||||
/* clang-format on */
|
||||
|
Loading…
x
Reference in New Issue
Block a user