From a2e09db1fd4e715f7647c4bc35f31b46e82cb67e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E4=BD=90=E6=9E=97?= Date: Thu, 30 Sep 2021 09:30:51 +0800 Subject: [PATCH 1/2] v0.3.0 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 唐佐林 --- lite-python/Makefile | 46 ++- lite-python/README.md | 43 +-- lite-python/demo/{ => Hi3861}/BUILD.gn | 12 +- lite-python/demo/Hi3861/dt_python_demo.c | 130 ++++++++ lite-python/demo/Hi3861/dtpython.h | 73 +++++ lite-python/demo/Hi3861/sdk_adapter_1.0.c | 279 ++++++++++++++++ lite-python/demo/Hi3861/sdk_adapter_1.1.c | 113 +++++++ lite-python/demo/Hi3861/test.c | 175 ++++++++++ lite-python/{demo/test.c => dev/adc_ex.c} | 118 +++---- lite-python/dev/adc_ex.h | 38 +++ lite-python/dev/adc_impl.c | 135 ++++++++ lite-python/dev/adc_impl.h | 42 +++ lite-python/dev/adc_qstr.h | 37 +++ lite-python/dev/file_reader.c | 172 ++++++++++ lite-python/dev/file_reader.h | 40 +++ lite-python/dev/gpio_ex.c | 69 ++++ lite-python/dev/gpio_ex.h | 38 +++ lite-python/dev/gpio_impl.c | 266 +++++++++++++++ lite-python/dev/gpio_impl.h | 62 ++++ lite-python/dev/gpio_qstr.h | 52 +++ lite-python/dev/i2c_ex.h | 34 ++ lite-python/dev/i2c_impl.c | 170 ++++++++++ lite-python/dev/i2c_impl.h | 43 +++ lite-python/dev/i2c_qstr.h | 36 ++ lite-python/dev/modadc.c | 58 ++++ lite-python/dev/modgpio.c | 83 +++++ lite-python/dev/modi2c.c | 57 ++++ lite-python/dev/modos.c | 55 ++++ lite-python/dev/modpwm.c | 54 +++ lite-python/dev/modspi.c | 73 +++++ lite-python/dev/moduart.c | 84 +++++ .../{demo/dt_python_demo.c => dev/os_impl.c} | 123 +++---- lite-python/dev/os_impl.h | 41 +++ lite-python/dev/os_qstr.h | 35 ++ lite-python/dev/pwm_impl.c | 51 +++ lite-python/dev/pwm_impl.h | 41 +++ lite-python/dev/pwm_qstr.h | 35 ++ lite-python/dev/setting.mk | 28 ++ lite-python/dev/spi_ex.h | 48 +++ lite-python/dev/spi_impl.c | 189 +++++++++++ lite-python/dev/spi_impl.h | 46 +++ lite-python/dev/spi_qstr.h | 38 +++ lite-python/dev/uart_impl.c | 134 ++++++++ lite-python/dev/uart_impl.h | 45 +++ lite-python/dev/uart_qstr.h | 50 +++ lite-python/dev/utility.c | 72 ++++ lite-python/dev/utility.h | 150 +++++++++ lite-python/dtpython.c | 310 ++++++++---------- lite-python/{demo => }/dtpython.h | 25 +- lite-python/mpconfigport.h | 50 ++- lite-python/py/builtinimport.c | 27 +- lite-python/py/lexer.c | 1 + lite-python/py/mpconfig.h | 2 +- 53 files changed, 3843 insertions(+), 385 deletions(-) rename lite-python/demo/{ => Hi3861}/BUILD.gn (82%) create mode 100755 lite-python/demo/Hi3861/dt_python_demo.c create mode 100755 lite-python/demo/Hi3861/dtpython.h create mode 100755 lite-python/demo/Hi3861/sdk_adapter_1.0.c create mode 100755 lite-python/demo/Hi3861/sdk_adapter_1.1.c create mode 100755 lite-python/demo/Hi3861/test.c rename lite-python/{demo/test.c => dev/adc_ex.c} (59%) create mode 100755 lite-python/dev/adc_ex.h create mode 100755 lite-python/dev/adc_impl.c create mode 100755 lite-python/dev/adc_impl.h create mode 100755 lite-python/dev/adc_qstr.h create mode 100755 lite-python/dev/file_reader.c create mode 100755 lite-python/dev/file_reader.h create mode 100755 lite-python/dev/gpio_ex.c create mode 100755 lite-python/dev/gpio_ex.h create mode 100755 lite-python/dev/gpio_impl.c create mode 100755 lite-python/dev/gpio_impl.h create mode 100755 lite-python/dev/gpio_qstr.h create mode 100755 lite-python/dev/i2c_ex.h create mode 100755 lite-python/dev/i2c_impl.c create mode 100755 lite-python/dev/i2c_impl.h create mode 100755 lite-python/dev/i2c_qstr.h create mode 100755 lite-python/dev/modadc.c create mode 100755 lite-python/dev/modgpio.c create mode 100755 lite-python/dev/modi2c.c create mode 100755 lite-python/dev/modos.c create mode 100755 lite-python/dev/modpwm.c create mode 100755 lite-python/dev/modspi.c create mode 100755 lite-python/dev/moduart.c rename lite-python/{demo/dt_python_demo.c => dev/os_impl.c} (51%) create mode 100755 lite-python/dev/os_impl.h create mode 100755 lite-python/dev/os_qstr.h create mode 100755 lite-python/dev/pwm_impl.c create mode 100755 lite-python/dev/pwm_impl.h create mode 100755 lite-python/dev/pwm_qstr.h create mode 100755 lite-python/dev/setting.mk create mode 100755 lite-python/dev/spi_ex.h create mode 100755 lite-python/dev/spi_impl.c create mode 100755 lite-python/dev/spi_impl.h create mode 100755 lite-python/dev/spi_qstr.h create mode 100755 lite-python/dev/uart_impl.c create mode 100755 lite-python/dev/uart_impl.h create mode 100755 lite-python/dev/uart_qstr.h create mode 100755 lite-python/dev/utility.c create mode 100755 lite-python/dev/utility.h rename lite-python/{demo => }/dtpython.h (70%) diff --git a/lite-python/Makefile b/lite-python/Makefile index 135091b..cb61f70 100755 --- a/lite-python/Makefile +++ b/lite-python/Makefile @@ -1,6 +1,8 @@ # MIT License # -# Copyright (c) 2020 唐佐林 +# Copyright (c) 2021 唐佐林 +# WeChat : delphi_tang +# EMail: delphi_tang@dt4sw.com # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -20,40 +22,50 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +PYPATH = py +LIBPATH = lib +DEVPATH = dev +PROPATH = /path/to/code-v1.1/root +CMPPATH = /path/compiler/root/bin +CMPPREFIX = riscv32-unknown-elf- + include ./py/mkenv.mk -# qstr definitions (must come before including py.mk) -QSTR_DEFS = qstrdefsport.h +include $(DEVPATH)/setting.mk -# MicroPython feature configurations -MICROPY_ROM_TEXT_COMPRESSION ?= 1 +QSTR_DEFS += qstrdefsport.h -# include py core make definitions -include $(TOP)/py/py.mk +MICROPY_ROM_TEXT_COMPRESSION ?= 1 -CROSS_COMPILE ?= riscv32-unknown-elf- +include ./py/py.mk +CROSS_COMPILE = $(CMPPATH)/$(CMPPREFIX) INC += -I. INC += -I$(TOP) INC += -I$(BUILD) +INC += -I$(LIBPATH) +INC += -I$(PYPATH) +INC += -I$(DEVPATH) -CFLAGS = $(INC) -Wall -Werror -std=c99 -ARFLAGS = rc - -CSUPEROPT = -Os # save some code space - +CFLAGS += $(INC) -Wall -Werror -std=gnu99 CFLAGS += -Os -DNDEBUG CFLAGS += -fdata-sections -ffunction-sections +ARFLAGS = rc + +CSUPEROPT = -Os + LIBS = SRC_C = \ + $(wildcard $(DEVPATH)/*.c) \ + $(LIBPATH)/utils/printf.c \ + $(LIBPATH)/utils/stdout_helpers.c \ + $(LIBPATH)/mp-readline/readline.c \ + $(LIBPATH)/utils/pyexec.c \ dtpython.c \ - uart_core.c \ - lib/utils/printf.c \ - lib/utils/stdout_helpers.c \ - lib/utils/pyexec.c + uart_core.c OBJ = $(PY_CORE_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) diff --git a/lite-python/README.md b/lite-python/README.md index 97de662..7b7d011 100755 --- a/lite-python/README.md +++ b/lite-python/README.md @@ -1,20 +1,23 @@ -# Python for OHOS +# Python for HOS #### 介绍 -这个仓库是为了能够在OpenHarmony设备上使用 Python 进行应用程序开发而创建。 -[1. 使用Python开发OpenHarmony设备程序(0-初体验)](https://harmonyos.51cto.com/posts/1887) - +这个仓库是为了能够在鸿蒙设备上使用 Python 进行应用程序开发而创建。 +[0. 使用Python开发鸿蒙设备程序(0-初体验)](https://harmonyos.51cto.com/posts/1887) +[1. 使用Python开发鸿蒙设备程序(1-GPIO外设控制)](https://harmonyos.51cto.com/posts/8428) +[2. 使用Python开发鸿蒙设备程序(2-I2C应用实例)](https://harmonyos.51cto.com/posts/8594) + #### 软件架构 -这个仓库的 Baseline 是 [MicroPython v1.13](https://github.com/micropython/micropython/tree/v1.13),在 MicroPython 的基础上进行了必要的剪裁以满足 OHOS 上的应用开发需求。 +这个仓库的 Baseline 是 [MicroPython v1.13](https://github.com/micropython/micropython/tree/v1.13),在 MicroPython 的基础上进行了必要的剪裁以满足 OpenHarmony 上的应用开发需求。 #### 编译说明 1. 编译环境: 1)OS - Ubuntu 16+ 2)Make - 3.81+ 3)Python - 3.8+ -2. 配置交叉编译器: +2. 配置项路径: 1)打开源码根目录中的 Makefile - 2)对变量 CROSS_COMPILE 进行赋值,如:CROSS_COMPILE ?= /home/harmony/gcc_riscv32/bin/riscv32-unknown-elf- + 2)配置项目路径,如: PROPATH = /home/harmony/harmony/code/code-1.0 + 3)配置编译器路径,如:CMPPATH = /home/harmony/gcc_riscv32/bin 3. 在源码根目录中执行 make #### 使用说明 @@ -23,29 +26,31 @@ 2. 在设备应用中加载 Python 并执行代码 ``` -#include "dtpython.h" - -extern const char* c_test_py; // test.py -extern const char* c_another_py; // another.py - -static void DTPython_Demo_Entry(void) +static void* DTPython_Demo_Task(const char* arg) { - printf("[DTPython_Demo] DTPython_Demo_Entry()\n"); + printf("[dt4sw] DTPython_Demo_Task()\n"); - DTPython_Init(); // 初始化Python环境 + PrepareScript("main.py", c_main_py); // 在设备上创建文件:main.py + PrepareScript("test.py", c_test_py); // 在设备上创建文件:test.py + PrepareScript("another.py", c_another_py); // 在设备上创建文件:another.py + + DTPython_Init(); // 初始化Python环境 DTPython_RunCode("print(\'Python Code Begin\')"); // 执行Python语句:print('Python Code Begin') - DTPython_RunCode("s = \'HOS Device Development\'"); // 执行Python语句:s = 'HOS Device Development' + DTPython_RunCode("s = \'Created by Delphi Tang\'"); // 执行Python语句:s = 'Created by Delphi Tang' + DTPython_RunCode("print(s)"); // 执行Python语句:print(s) - DTPython_RunCode(c_test_py); // 模拟执行Python文件:DTPython_RunFile("test.py"); - DTPython_RunCode(c_another_py); // 模拟执行Python文件:DTPython_RunFile("another.py"); + DTPython_RunFile("main.py"); // 执行Python文件:main.py DTPython_RunCode("print(\'Python Code End\')"); // 执行Python语句:print('Python Code End') - DTPython_Deinit(); // 清理Python环境 + DTPython_Deinit(); // 清理Python环境 + + return (void*)arg; } + ``` diff --git a/lite-python/demo/BUILD.gn b/lite-python/demo/Hi3861/BUILD.gn similarity index 82% rename from lite-python/demo/BUILD.gn rename to lite-python/demo/Hi3861/BUILD.gn index c62b67e..a8e5c9f 100755 --- a/lite-python/demo/BUILD.gn +++ b/lite-python/demo/Hi3861/BUILD.gn @@ -1,6 +1,8 @@ # MIT License # -# Copyright (c) 2020 唐佐林 +# Copyright (c) 2021 唐佐林 +# WeChat : delphi_tang +# EMail: delphi_tang@dt4sw.com # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -20,18 +22,16 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. - static_library("dt_python_demo") { sources = [ "dt_python_demo.c", + #"sdk_adapter_1.0.c" + #"sdk_adapter_1.1.c", "test.c" ] include_dirs = [ - "//utils/native/lite/include", - "//kernel/liteos_m/components/cmsis/2.0", - "//base/iot_hardware/interfaces/kits/wifiiot_lite", - "//vendor/hisi/hi3861/hi3861/include" + "//utils/native/lite/include" ] } diff --git a/lite-python/demo/Hi3861/dt_python_demo.c b/lite-python/demo/Hi3861/dt_python_demo.c new file mode 100755 index 0000000..60c7186 --- /dev/null +++ b/lite-python/demo/Hi3861/dt_python_demo.c @@ -0,0 +1,130 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + + +#include +#include +#include +#include "ohos_init.h" +#include "cmsis_os2.h" +#include "utils_file.h" +#include "dtpython.h" + +extern const char* c_gpio_py; // gpio.py +extern const char* c_i2c_py; // i2c.py +extern const char* c_test_py; // test.py +extern const char* c_another_py; // another.py + +static int WriteScript(const char* name, const char* script) +{ + int fd = UtilsFileOpen(name, O_CREAT_FS | O_TRUNC_FS | O_WRONLY_FS, 0); + + if( fd > 0 ) + { + int n = strlen(script); + int i = 0; + + for(i=0; i 0 ) + { + printf("\'%s\' is existed!\n", name); + + UtilsFileClose(fd); + } + else + { + printf("Create \'%s\'...\n", name); + + if( WriteScript(name, script) > 0 ) + { + printf("\'%s\' is created!\n", name); + } + } +} + +static void* DTPython_Demo_Task(const char* arg) +{ + printf("[dt4sw] DTPython_Demo_Task()\n"); + + PrepareScript("gpio.py", c_gpio_py); + PrepareScript("i2c.py", c_i2c_py); + PrepareScript("test.py", c_test_py); + PrepareScript("another.py", c_another_py); + + DTPython_Init(); // 初始化Python环境 + + DTPython_RunCode("print(\'Python Code Begin\')"); // 执行Python语句:print('Python Code Begin') + + DTPython_RunCode("s = \'Created by Delphi Tang\'"); // 执行Python语句:s = 'Created by Delphi Tang' + + DTPython_RunCode("print(s)"); // 执行Python语句:print(s) + + DTPython_RunFile("i2c.py"); // 执行Python文件:xxx.py + + DTPython_RunCode("print(\'Python Code End\')"); // 执行Python语句:print('Python Code End') + + DTPython_Deinit(); // 清理Python环境 + + return (void*)arg; +} + +static void DTPython_Demo_Entry(void) +{ + osThreadAttr_t attr = {0}; + + printf("[dt4sw] DTPython_Demo_Entry()\n"); + + attr.name = "DTPython_Demo_Task"; + attr.attr_bits = 0U; + attr.cb_mem = NULL; + attr.cb_size = 0U; + attr.stack_mem = NULL; + attr.stack_size = 1024 * 30; + attr.priority = osPriorityNormal; + + if (osThreadNew((osThreadFunc_t)DTPython_Demo_Task, NULL, &attr) == NULL) + { + printf("[dt4sw] Falied to create DTPython Demo Task!\n"); + } +} + +SYS_RUN(DTPython_Demo_Entry); diff --git a/lite-python/demo/Hi3861/dtpython.h b/lite-python/demo/Hi3861/dtpython.h new file mode 100755 index 0000000..e8790f8 --- /dev/null +++ b/lite-python/demo/Hi3861/dtpython.h @@ -0,0 +1,73 @@ +/* + ======================================================================================= + File: + dtpython.h + + Description: + This header file is for the interface of DTPython project which is an experiment to make HOS board + to support Python. DTPython is made from MicroPython, but is NOT MicroPython anymore, + because the basic purpose of DTPython is to supply another option to develop HOS device application. + + Author: + Delphi Tang (唐佐林) + http://www.dt4sw.com/ + + Revision History: + V0.0.1 (Initial Version) + ======================================================================================= + +*/ + + +#ifndef DTPYTHON_H +#define DTPYTHON_H + +/* + Description: + To initialize Python enviroment. + + Parameter: + None + + Return Value: + None +*/ +void DTPython_Init(void); + +/* + Description: + To run a python file. + + Parameter: + file -- python file name, such as "test.py" + + Return Value: + None +*/ +void DTPython_RunFile(const char* file); + +/* + Description: + To run python code statements. + + Parameter: + statement -- python statements, such as "a = 1 + 2" + + Return Value: + None +*/ +void DTPython_RunCode(const char* statement); + +/* + Description: + To clean up Python enviroment. + + Parameter: + None + + Return Value: + None +*/ +void DTPython_Deinit(void); + +#endif \ No newline at end of file diff --git a/lite-python/demo/Hi3861/sdk_adapter_1.0.c b/lite-python/demo/Hi3861/sdk_adapter_1.0.c new file mode 100755 index 0000000..acb14c1 --- /dev/null +++ b/lite-python/demo/Hi3861/sdk_adapter_1.0.c @@ -0,0 +1,279 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + + +#include "hi_io.h" +#include "hi_gpio.h" +#include "hi_adc.h" +#include "hi_i2c.h" +#include "hi_spi.h" +#include "hi_uart.h" +#include "hi_pwm.h" + +unsigned int IoTGpioInit(unsigned int id) +{ + (void)id; + return hi_gpio_init(); +} + +unsigned int IoTGpioDeinit(unsigned int id) +{ + (void)id; + return hi_gpio_deinit(); +} + +unsigned int IoTGpioSetDir(unsigned int id, unsigned int dir) +{ + return hi_gpio_set_dir(id, dir); +} + +unsigned int IoTGpioGetDir(unsigned int id, unsigned int* dir) +{ + return hi_gpio_get_dir(id, dir); +} + +unsigned int IoTGpioSetOutputVal(unsigned int id, unsigned int val) +{ + return hi_gpio_set_ouput_val(id, val); +} + +unsigned int IoTGpioGetOutputVal(unsigned int id, unsigned int *val) +{ + return hi_gpio_get_output_val(id, val); +} + +unsigned int IoTGpioGetInputVal(unsigned int id, unsigned int *val) +{ + return hi_gpio_get_input_val(id, val); +} + +unsigned int IoTGpioRegisterIsrFunc(unsigned int id, unsigned int intType, unsigned int intPolarity, void(*func)(void*), void* arg) +{ + return hi_gpio_register_isr_function(id, intType, intPolarity, func, arg); +} + +unsigned int IoTGpioUnregisterIsrFunc(unsigned int id) +{ + return hi_gpio_unregister_isr_function(id); +} + +unsigned int IoTGpioSetIsrMask(unsigned int id, unsigned char mask) +{ + return hi_gpio_set_isr_mask(id, mask); +} + +unsigned int IoTGpioSetIsrMode(unsigned int id, unsigned int intType, unsigned int intPolarity) +{ + return hi_gpio_set_isr_mode(id, intType, intPolarity); +} + +unsigned int IoTI2cInit(unsigned int id, unsigned int baudrate) +{ + return hi_i2c_init(id, baudrate); +} + + +unsigned int IoTI2cDeinit(unsigned int id) +{ + return hi_i2c_deinit(id); +} + + +unsigned int IoTI2cWrite(unsigned int id, unsigned short deviceAddr, unsigned char* data, unsigned int dataLen) +{ + hi_i2c_data tosend = {0}; + + tosend.send_buf = data; + tosend.send_len = dataLen; + + return hi_i2c_write(id, deviceAddr, &tosend); +} + +unsigned int IoTI2cRead(unsigned int id, unsigned short deviceAddr, unsigned char* data, unsigned int dataLen) +{ + hi_i2c_data toread = {0}; + + toread.receive_buf = data; + toread.receive_len = dataLen; + + return hi_i2c_read(id, deviceAddr, &toread); +} + +unsigned int IoTI2cSetBaudrate(unsigned int id, unsigned int baudrate) +{ + return hi_i2c_set_baudrate(id, baudrate); +} + +unsigned int IoTI2cWriteread(unsigned int id, unsigned short deviceAddr, hi_i2c_data* i2cData) +{ + return hi_i2c_writeread(id, deviceAddr, i2cData); +} + +typedef struct +{ + unsigned int baudRate; + unsigned int dataBits; + unsigned int stopBits; + unsigned int parity; + unsigned int rxBlock; + unsigned int txBlock; + unsigned char pad; +} IotUartAttribute; + +unsigned int IoTUartInit(unsigned int id, IotUartAttribute* param) +{ + hi_uart_attribute attr = {0}; + + attr.baud_rate = param->baudRate; + attr.data_bits = param->dataBits; + attr.stop_bits = param->stopBits; + attr.parity = param->parity; + + return hi_uart_init(id, &attr, NULL); +} + +int IoTUartRead(unsigned int id, unsigned char* data, unsigned int dataLen) +{ + return hi_uart_read(id, data, dataLen); +} + +int IoTUartWrite(unsigned int id, unsigned char* data, unsigned int dataLen) +{ + return hi_uart_write(id, data, dataLen); +} + +unsigned int IoTUartDeinit(unsigned int id) +{ + return hi_uart_deinit(id); +} + +unsigned int IoTUartSetFlowCtrl(unsigned int id, unsigned int flowCtrl) +{ + return hi_uart_set_flow_ctrl(id, flowCtrl); +} + +unsigned int IoTPwmInit(unsigned int port) +{ + return hi_pwm_init(port); +} + +unsigned int IoTPwmDeinit(unsigned int port) +{ + return hi_pwm_deinit(port); +} + +unsigned int IoTPwmStart(unsigned int port, unsigned short duty, unsigned int freq) +{ + return hi_pwm_start(port, duty, freq); +} + +unsigned int IoTPwmStop(unsigned int port) +{ + return hi_pwm_stop(port); +} + +unsigned int DTGpioSetFunc(unsigned int id, unsigned char val) +{ + return hi_io_set_func(id, val); +} + +unsigned int DTGpioSetPull(unsigned int id, unsigned char val) +{ + return hi_io_set_pull(id, val); +} + +typedef struct +{ + unsigned int cpol; + unsigned int cpha; + unsigned int protocol; + unsigned int dataWidth; + unsigned int endian; + unsigned int freq; +} SpiInfo; + +unsigned int DTSpiInit(unsigned int id, SpiInfo info) +{ + hi_spi_cfg_basic_info hiInfo = {0}; + hi_spi_cfg_init_param param = {0}; + + hiInfo.cpol = info.cpol; + hiInfo.cpha = info.cpha; + hiInfo.fram_mode = info.protocol; + hiInfo.data_width = info.dataWidth; + hiInfo.endian = info.endian; + hiInfo.freq = info.freq; + + return hi_spi_init(id, param, &hiInfo); +} + +unsigned int DTSpiDeinit(unsigned int id) +{ + return hi_spi_deinit(id); +} + +unsigned int DTSpiHostWrite(unsigned int id, unsigned char *writeData, unsigned int byteLen) +{ + return hi_spi_host_write(id, writeData, byteLen); +} +unsigned int DTSpiHostRead(unsigned int id, unsigned char *readData, unsigned int byteLen) +{ + return hi_spi_host_read(id, readData, byteLen); +} + +unsigned int DTSpiSetInfo(unsigned int id, SpiInfo info) +{ + hi_spi_cfg_basic_info hiInfo = {0}; + + hiInfo.cpol = info.cpol; + hiInfo.cpha = info.cpha; + hiInfo.fram_mode = info.protocol; + hiInfo.data_width = info.dataWidth; + hiInfo.endian = info.endian; + hiInfo.freq = info.freq; + + return hi_spi_set_basic_info(id, &hiInfo); +} + +unsigned int DTI2cWriteRead(unsigned int id, unsigned short deviceAddr, unsigned char* txBuf, unsigned int txLen, unsigned char* rxBuf, unsigned int rxLen) +{ + hi_i2c_data data = {0}; + + data.send_buf = txBuf; + data.send_len = txLen; + data.receive_buf = rxBuf; + data.receive_len = rxLen; + + return hi_i2c_writeread(id, deviceAddr, &data); +} + +unsigned int DTAdcRead(unsigned int channel, unsigned short* data, int equModel, int curBais, unsigned short rstCnt) +{ + return hi_adc_read(channel, data, equModel, curBais, rstCnt); +} + diff --git a/lite-python/demo/Hi3861/sdk_adapter_1.1.c b/lite-python/demo/Hi3861/sdk_adapter_1.1.c new file mode 100755 index 0000000..6a934e9 --- /dev/null +++ b/lite-python/demo/Hi3861/sdk_adapter_1.1.c @@ -0,0 +1,113 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +#include "hi_io.h" +#include "hi_adc.h" +#include "hi_i2c.h" +#include "hi_spi.h" + +unsigned int DTGpioSetFunc(unsigned int id, unsigned char val) +{ + return hi_io_set_func(id, val); +} + +unsigned int DTGpioSetPull(unsigned int id, unsigned char val) +{ + return hi_io_set_pull(id, val); +} + +typedef struct +{ + unsigned int cpol; + unsigned int cpha; + unsigned int protocol; + unsigned int dataWidth; + unsigned int endian; + unsigned int freq; +} SpiInfo; + +unsigned int DTSpiInit(unsigned int id, SpiInfo info) +{ + hi_spi_cfg_basic_info hiInfo = {0}; + hi_spi_cfg_init_param param = {0}; + + hiInfo.cpol = info.cpol; + hiInfo.cpha = info.cpha; + hiInfo.fram_mode = info.protocol; + hiInfo.data_width = info.dataWidth; + hiInfo.endian = info.endian; + hiInfo.freq = info.freq; + + return hi_spi_init(id, param, &hiInfo); +} + +unsigned int DTSpiDeinit(unsigned int id) +{ + return hi_spi_deinit(id); +} + +unsigned int DTSpiHostWrite(unsigned int id, unsigned char *writeData, unsigned int byteLen) +{ + return hi_spi_host_write(id, writeData, byteLen); +} +unsigned int DTSpiHostRead(unsigned int id, unsigned char *readData, unsigned int byteLen) +{ + return hi_spi_host_read(id, readData, byteLen); +} + +unsigned int DTSpiSetInfo(unsigned int id, SpiInfo info) +{ + hi_spi_cfg_basic_info hiInfo = {0}; + + hiInfo.cpol = info.cpol; + hiInfo.cpha = info.cpha; + hiInfo.fram_mode = info.protocol; + hiInfo.data_width = info.dataWidth; + hiInfo.endian = info.endian; + hiInfo.freq = info.freq; + + return hi_spi_set_basic_info(id, &hiInfo); +} + +unsigned int DTI2cWriteRead(unsigned int id, unsigned short deviceAddr, unsigned char* txBuf, unsigned int txLen, unsigned char* rxBuf, unsigned int rxLen) +{ + hi_i2c_data data = {0}; + + data.send_buf = txBuf; + data.send_len = txLen; + data.receive_buf = rxBuf; + data.receive_len = rxLen; + + return hi_i2c_writeread(id, deviceAddr, &data); +} + +unsigned int DTAdcRead(unsigned int channel, unsigned short* data, int equModel, int curBais, unsigned short rstCnt) +{ + return hi_adc_read(channel, data, equModel, curBais, rstCnt); +} + diff --git a/lite-python/demo/Hi3861/test.c b/lite-python/demo/Hi3861/test.c new file mode 100755 index 0000000..5eb0cc5 --- /dev/null +++ b/lite-python/demo/Hi3861/test.c @@ -0,0 +1,175 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +const char* c_gpio_py = +{ +"import gpio" "\r\n" +"import os" "\r\n" + "\r\n" +"led = 7" "\r\n" +"button = 11" "\r\n" + "\r\n" +"led_on = False" "\r\n" + "\r\n" +"def button_callback(arg) :" "\r\n" +" cur_tick = os.get_tick()" "\r\n" + "\r\n" +" if cur_tick - arg[0] > 20:" "\r\n" +" global led_on" "\r\n" +" if led_on :" "\r\n" +" gpio.set_output(led, 0)" "\r\n" +" led_on = False" "\r\n" +" print('LED is OFF!')" "\r\n" +" else :" "\r\n" +" gpio.set_output(led, 1)" "\r\n" +" led_on = True" "\r\n" +" print('LED is ON!')" "\r\n" + "\r\n" +" arg[0] = cur_tick" "\r\n" + "\r\n" + "\r\n" +"gpio.gpio_init(led)" "\r\n" +"gpio.gpio_init(button)" "\r\n" + "\r\n" +"func = gpio.query_func_value(led, 'gpio')" "\r\n" + "\r\n" +"gpio.set_func(led, func)" "\r\n" +"gpio.set_dir(led, gpio.dir_out)" "\r\n" +"gpio.set_output(led, 0)" "\r\n" + "\r\n" +"func = gpio.query_func_value(button, 'gpio')" "\r\n" + "\r\n" +"gpio.set_func(button, func)" "\r\n" +"gpio.set_dir(button, gpio.dir_in)" "\r\n" + "\r\n" +"pull_up = gpio.query_pull_value('up')" "\r\n" + "\r\n" +"gpio.set_pull(button, pull_up)" "\r\n" +"gpio.set_isr_mode(button, gpio.fall_low)" "\r\n" +"gpio.register_isr_func(button, button_callback, [os.get_tick()])" "\r\n" + "\r\n" +"while True:" "\r\n" +" os.sleep(1)" "\r\n" + "\r\n" +"gpio.gpio_deinit(led)" "\r\n" +"gpio.gpio_deinit(button)" "\r\n" +}; + +const char* c_i2c_py = +{ +"import gpio" "\r\n" +"import i2c" "\r\n" +"import os" "\r\n" + "\r\n" +"sht30_addr = 0x44 << 1" "\r\n" + "\r\n" +"def cal_t(t):" "\r\n" +" t = t & ~0x3" "\r\n" +" t = t * 175 // 65536 - 45" "\r\n" +" return t" "\r\n" + "\r\n" +"def cal_h(h):" "\r\n" +" h = h & ~0x3" "\r\n" +" h = h * 100 // 65536" "\r\n" +" return h" "\r\n" + "\r\n" +"def init():" "\r\n" +" gpio.gpio_init(0)" "\r\n" +" func = gpio.query_func_value(0, \'I2C1_SDA\')" "\r\n" +" gpio.set_func(0, func)" "\r\n" + "\r\n" +" gpio.gpio_init(1)" "\r\n" +" func = gpio.query_func_value(1, \'I2C1_SCL\')" "\r\n" +" gpio.set_func(1, func)" "\r\n" + "\r\n" +" i2c.i2c_init(1, 400000)" "\r\n" +" i2c.write(1, sht30_addr, [0x22, 0x36])" "\r\n" + "\r\n" +"def read():" "\r\n" +" r , d = i2c.write_read(1, sht30_addr, [0xE0, 0x00], 6)" "\r\n" + "\r\n" +" if r == 0:" "\r\n" +" t = (d[0] << 8) | d[1]" "\r\n" +" h = (d[3] << 8) | d[4]" "\r\n" +" return cal_t(t), cal_h(h)" "\r\n" +" else:" "\r\n" +" return None, None" "\r\n" + "\r\n" +"init()" "\r\n" + "\r\n" +"while True:" "\r\n" +" t , h = read()" "\r\n" + "\r\n" +" if t != None:" "\r\n" +" print(\'temperature = \' + str(t))" "\r\n" + "\r\n" +" if h != None:" "\r\n" +" print(\'humidity = \' + str(h))" "\r\n" + "\r\n" +" os.sleep(3)" "\r\n" +}; + +const char* c_test_py = +{ +"from another import Person" "\r\n" + "\r\n" +"def SayHello(n) :" "\r\n" +" for i in range(n) :" "\r\n" +" print(\"Hello World: \" + str(i))" "\r\n" + "\r\n" +"a = 4" "\r\n" +"b = 5" "\r\n" + "\r\n" +"print(\'a + b = \', a + b)" "\r\n" + "\r\n" +"SayHello(6)" "\r\n" + "\r\n" +"s1 = \'Delphi\'" "\r\n" +"s2 = \'Tang\'" "\r\n" + "\r\n" +"if s1 == s2 :" "\r\n" +" print(\'Equal\')" "\r\n" +"else :" "\r\n" +" print(\'No Equal\')" "\r\n" + "\r\n" +"print(\'s1 + s2 = \', s1 + \' \' + s2)" "\r\n" + "\r\n" +"p = Person(\'Delphi Tang\')" "\r\n" +"print(p)" "\r\n" +"p.SayHello();" "\r\n" +}; + +const char* c_another_py = +{ +"class Person:" "\r\n" +" def __init__(self, name):" "\r\n" +" self.name = name" "\r\n" + "\r\n" +" def SayHello(self):" "\r\n" +" print(\'My name is:\', self.name)" "\r\n" +}; \ No newline at end of file diff --git a/lite-python/demo/test.c b/lite-python/dev/adc_ex.c similarity index 59% rename from lite-python/demo/test.c rename to lite-python/dev/adc_ex.c index f4108af..4b5964d 100755 --- a/lite-python/demo/test.c +++ b/lite-python/dev/adc_ex.c @@ -1,67 +1,51 @@ -/**************************************************************************** - -MIT License - -Copyright (c) 2020 唐佐林 - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - -*****************************************************************************/ - -const char* c_test_py = -{ -"\r\n" -"def SayHello(n) :\r\n" -" for i in range(n) :\r\n" -" print(\"Hello World: \" + str(i))\r\n" -"\r\n" -"a = 4\r\n" -"b = 5\r\n" -"\r\n" -"print(\'a + b = \', a + b)\r\n" -"\r\n" -"SayHello(6)\r\n" -" \r\n" -"s1 = \'Delphi\'\r\n" -"s2 = \'Tang\'\r\n" -"\r\n" -"if s1 == s2 :\r\n" -" print(\'Equal\')\r\n" -"else :\r\n" -" print(\'No Equal\')\r\n" -" \r\n" -"print(\'s1 + s2 = \', s1 + \' \' + s2)\r\n" -}; - -const char* c_another_py = -{ -"class Person:\r\n" -" def __init__(self, name):\r\n" -" self.name = name\r\n" -" \r\n" -" def SayHello(self):\r\n" -" print(\'My name is:\', self.name)\r\n" -"\r\n" -"p = Person(\'Delphi Tang\')\r\n" -"\r\n" -"p.SayHello();\r\n" -"\r\n" -"print(p)\r\n" -}; - +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +#include +#include "utility.h" +#include "adc_ex.h" + +static const key_val_t g_bais_value[] = +{ + +}; + +static const key_val_t g_model_value[] = +{ + +}; + +int adc_query_bais_value(const char* bais) +{ + return query_value(bais, g_bais_value, dim(g_bais_value)); +} + +int adc_query_model_value(const char* model) +{ + return query_value(model, g_model_value, dim(g_model_value)); +} diff --git a/lite-python/dev/adc_ex.h b/lite-python/dev/adc_ex.h new file mode 100755 index 0000000..a69378c --- /dev/null +++ b/lite-python/dev/adc_ex.h @@ -0,0 +1,38 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +#ifndef ADC_EX_H +#define ADC_EX_H + +unsigned int DTAdcRead(unsigned int channel, unsigned short* data, int equModel, int curBais, unsigned short rstCnt); + +int adc_query_bais_value(const char* bais); +int adc_query_model_value(const char* model); + +#endif + diff --git a/lite-python/dev/adc_impl.c b/lite-python/dev/adc_impl.c new file mode 100755 index 0000000..92223f7 --- /dev/null +++ b/lite-python/dev/adc_impl.c @@ -0,0 +1,135 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +#include "adc_impl.h" +#include "adc_ex.h" + +#define ADC_CHANNEL_NUM 8 + +typedef struct +{ + int model; + int curbais; + int count; +} adc_cfg; + +static adc_cfg g_config[ADC_CHANNEL_NUM] = {0}; + +mp_obj_t mp_adc_set_bais(mp_obj_t index, mp_obj_t bais) +{ + unsigned int channel = mp_obj_get_int(index); + int cur_bais = mp_obj_get_int(bais); + int r = 0; + + if( channel < ADC_CHANNEL_NUM ) + { + g_config[channel].curbais = cur_bais; + } + else + { + r = -1; + } + + return mp_obj_new_int(r); +} + +mp_obj_t mp_adc_set_model(mp_obj_t index, mp_obj_t model) +{ + unsigned int channel = mp_obj_get_int(index); + int equ_model = mp_obj_get_int(model); + int r = 0; + + if( channel < ADC_CHANNEL_NUM ) + { + g_config[channel].model = equ_model; + } + else + { + r = -1; + } + + return mp_obj_new_int(r); +} + +mp_obj_t mp_adc_set_reset_count(mp_obj_t index, mp_obj_t count) +{ + unsigned int channel = mp_obj_get_int(index); + unsigned short rstcnt = (unsigned short)mp_obj_get_int(count); + int r = 0; + + if( channel < ADC_CHANNEL_NUM ) + { + g_config[channel].count = rstcnt; + } + else + { + r = -1; + } + + return mp_obj_new_int(r); +} + +mp_obj_t mp_adc_read(mp_obj_t index) +{ + unsigned int channel = mp_obj_get_int(index); + unsigned int r = -1; + unsigned short data = 0; + mp_obj_t ret[2] = + { + mp_obj_new_int(r), + mp_obj_new_int(data), + }; + + if( channel < ADC_CHANNEL_NUM ) + { + r = DTAdcRead(channel, + &data, + g_config[channel].model, + g_config[channel].curbais, + g_config[channel].count); + + ret[0] = mp_obj_new_int(r); + ret[1] = mp_obj_new_int(data); + } + else + { + r = -1; + } + + return mp_obj_new_tuple(2, ret); +} + +mp_obj_t mp_adc_query_bais_value(mp_obj_t key) +{ + return mp_obj_new_int(adc_query_bais_value(mp_obj_str_get_str(key))); +} + +mp_obj_t mp_adc_query_model_value(mp_obj_t key) +{ + return mp_obj_new_int(adc_query_model_value(mp_obj_str_get_str(key))); +} diff --git a/lite-python/dev/adc_impl.h b/lite-python/dev/adc_impl.h new file mode 100755 index 0000000..29a5e20 --- /dev/null +++ b/lite-python/dev/adc_impl.h @@ -0,0 +1,42 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +#ifndef ADC_IMPL_H +#define ADC_IMPL_H + +#include "py/objtype.h" +#include "py/runtime.h" + +mp_obj_t mp_adc_set_bais(mp_obj_t index, mp_obj_t bais); +mp_obj_t mp_adc_set_model(mp_obj_t index, mp_obj_t model); +mp_obj_t mp_adc_set_reset_count(mp_obj_t index, mp_obj_t count); +mp_obj_t mp_adc_read(mp_obj_t index); +mp_obj_t mp_adc_query_bais_value(mp_obj_t key); +mp_obj_t mp_adc_query_model_value(mp_obj_t key); + +#endif diff --git a/lite-python/dev/adc_qstr.h b/lite-python/dev/adc_qstr.h new file mode 100755 index 0000000..a32e79a --- /dev/null +++ b/lite-python/dev/adc_qstr.h @@ -0,0 +1,37 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +Q(_adc_begin_) +Q(set_model) +Q(set_bais) +Q(set_reset_count) +Q(read) +Q(query_model_value) +Q(query_bais_value) +Q(_adc_end_) + diff --git a/lite-python/dev/file_reader.c b/lite-python/dev/file_reader.c new file mode 100755 index 0000000..182357c --- /dev/null +++ b/lite-python/dev/file_reader.c @@ -0,0 +1,172 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +#include "utils_file.h" +#include "file_reader.h" + +typedef struct +{ + bool close_fd; + int fd; + size_t len; + size_t pos; + char buf[20]; +} mp_reader_custom_t; + +static const char* file_name_checker(const char* filename) +{ + return ((filename[0] == '.') && (filename[1] == '/')) ? (filename + 2) : filename; +} + +static mp_uint_t mp_reader_custom_readbyte(void* data) +{ + mp_reader_custom_t* reader = (mp_reader_custom_t*)data; + mp_uint_t ret = MP_READER_EOF; + + if( reader->pos >= reader->len ) + { + if( reader->len > 0 ) + { + MP_THREAD_GIL_EXIT(); + + int n = UtilsFileRead(reader->fd, reader->buf, sizeof(reader->buf)); + + MP_THREAD_GIL_ENTER(); + + if( n > 0 ) + { + reader->pos = 0; + reader->len = n; + + ret = reader->buf[reader->pos++]; + } + else + { + reader->len = 0; + + ret = MP_READER_EOF; + } + } + } + else + { + ret = reader->buf[reader->pos++]; + } + + return ret; +} + +static void mp_reader_custom_close(void* data) +{ + mp_reader_custom_t* reader = (mp_reader_custom_t*)data; + + if( reader->close_fd ) + { + MP_THREAD_GIL_EXIT(); + + UtilsFileClose(reader->fd); + + MP_THREAD_GIL_ENTER(); + } + + m_del_obj(mp_reader_custom_t, reader); +} + +void mp_reader_new_file_from_fd(mp_reader_t* reader, int fd, bool close_fd) +{ + mp_reader_custom_t* rp = m_new_obj(mp_reader_custom_t); + + rp->close_fd = close_fd; + rp->fd = fd; + + MP_THREAD_GIL_EXIT(); + + int n = UtilsFileRead(rp->fd, rp->buf, sizeof(rp->buf)); + + if( n == -1 ) + { + if( close_fd ) + { + UtilsFileClose(fd); + } + + MP_THREAD_GIL_ENTER(); + + mp_raise_OSError(errno); + } + + MP_THREAD_GIL_ENTER(); + + rp->len = n; + rp->pos = 0; + + reader->data = rp; + reader->readbyte = mp_reader_custom_readbyte; + reader->close = mp_reader_custom_close; +} + +void mp_reader_new_file(mp_reader_t* reader, const char* filename) +{ + MP_THREAD_GIL_EXIT(); + + int fd = UtilsFileOpen(file_name_checker(filename), O_RDONLY_FS, 0); + + MP_THREAD_GIL_ENTER(); + + if( fd < 0) + { + mp_raise_OSError(errno); + } + + mp_reader_new_file_from_fd(reader, fd, true); +} + +mp_lexer_t* mp_lexer_new_from_file(const char* filename) +{ + mp_reader_t reader = {0}; + + mp_reader_new_file(&reader, file_name_checker(filename)); + + return mp_lexer_new(qstr_from_str(file_name_checker(filename)), reader); +} + +mp_import_stat_t mp_import_stat(const char* path) +{ + mp_import_stat_t ret = MP_IMPORT_STAT_NO_EXIST; + + int fd = UtilsFileOpen(file_name_checker(path), O_RDONLY_FS, 0); + + if( fd > 0 ) + { + ret = MP_IMPORT_STAT_FILE; + UtilsFileClose(fd); + } + + return ret; +} + diff --git a/lite-python/dev/file_reader.h b/lite-python/dev/file_reader.h new file mode 100755 index 0000000..1d02f6e --- /dev/null +++ b/lite-python/dev/file_reader.h @@ -0,0 +1,40 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +#ifndef FILE_READER_H +#define FILE_READER_H + +#include "compile.h" +#include "mperrno.h" + +mp_lexer_t* mp_lexer_new_from_file(const char* filename); +mp_import_stat_t mp_import_stat(const char* path); +void mp_reader_new_file(mp_reader_t* reader, const char* filename); +void mp_reader_new_file_from_fd(mp_reader_t* reader, int fd, bool close_fd); + +#endif diff --git a/lite-python/dev/gpio_ex.c b/lite-python/dev/gpio_ex.c new file mode 100755 index 0000000..8d208df --- /dev/null +++ b/lite-python/dev/gpio_ex.c @@ -0,0 +1,69 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +#include +#include "utility.h" +#include "gpio_ex.h" + +typedef struct +{ + const int gpio; + key_val_t func; +} gpio_func_val_t; + +static const gpio_func_val_t g_func_value[] = +{ + +}; + +static const key_val_t g_pull_value[] = +{ + +}; + +int gpio_query_func_value(unsigned int gpio, const char* func) +{ + int ret = -1; + int i = 0; + + for(i=0; icall ) + { + if( (0 <= index) && (index < GPIO_IDX_MAX) && g_callback[index].type ) + { + IotGpioIntType itype = 0; + IotGpioIntPolarity ipolarity = 0; + + g_callback[index].func = func; + g_callback[index].arg = arg; + + switch( g_callback[index].type ) + { + case GPIO_TYPE_LEVEL_LOW: + itype = IOT_INT_TYPE_LEVEL; + ipolarity = IOT_GPIO_EDGE_FALL_LEVEL_LOW; + break; + case GPIO_TYPE_LEVEL_HIGH: + itype = IOT_INT_TYPE_LEVEL; + ipolarity = IOT_GPIO_EDGE_RISE_LEVEL_HIGH; + break; + case GPIO_TYPE_EDGE_FALL_LOW: + itype = IOT_INT_TYPE_EDGE; + ipolarity = IOT_GPIO_EDGE_FALL_LEVEL_LOW; + break; + case GPIO_TYPE_EDGE_RISE_HIGH: + itype = IOT_INT_TYPE_EDGE; + ipolarity = IOT_GPIO_EDGE_RISE_LEVEL_HIGH; + break; + } + + r = IoTGpioRegisterIsrFunc(index, itype, ipolarity, gpio_callback_func, (char*)(size_t)index); + } + } + else + { + mp_raise_TypeError(MP_ERROR_TEXT("func is not callable")); + } + + return mp_obj_new_int(r); +} + +mp_obj_t mp_gpio_unregister_isr_func(mp_obj_t idx) +{ + int index = mp_obj_get_int(idx); + unsigned int r = -1; + + if( (0 <= index) && (index < GPIO_IDX_MAX) ) + { + g_callback[index].type = 0; + + r = IoTGpioUnregisterIsrFunc(index); + } + + return mp_obj_new_int(r); +} + +mp_obj_t mp_gpio_set_pull(mp_obj_t idx, mp_obj_t val) +{ + return mp_obj_new_int(DTGpioSetPull(mp_obj_get_int(idx), mp_obj_get_int(val))); +} + +mp_obj_t mp_gpio_set_func(mp_obj_t idx, mp_obj_t val) +{ + return mp_obj_new_int(DTGpioSetFunc(mp_obj_get_int(idx), mp_obj_get_int(val))); +} + +mp_obj_t mp_gpio_query_pull_value(mp_obj_t key) +{ + return mp_obj_new_int(gpio_query_pull_value(mp_obj_str_get_str(key))); +} + +mp_obj_t mp_gpio_query_func_value(mp_obj_t idx, mp_obj_t key) +{ + return mp_obj_new_int(gpio_query_func_value(mp_obj_get_int(idx), mp_obj_str_get_str(key))); +} diff --git a/lite-python/dev/gpio_impl.h b/lite-python/dev/gpio_impl.h new file mode 100755 index 0000000..2693245 --- /dev/null +++ b/lite-python/dev/gpio_impl.h @@ -0,0 +1,62 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +#ifndef GPIO_IMPL_H +#define GPIO_IMPL_H + +#include "py/objtype.h" +#include "py/runtime.h" +#include "iot_gpio.h" + +enum +{ + GPIO_TYPE_LEVEL_LOW = 1, + GPIO_TYPE_LEVEL_HIGH, + GPIO_TYPE_EDGE_FALL_LOW, + GPIO_TYPE_EDGE_RISE_HIGH, + GPIO_TYPE_MAX +}; + +mp_obj_t mp_gpio_init(mp_obj_t idx); +mp_obj_t mp_gpio_deinit(mp_obj_t idx); +mp_obj_t mp_gpio_get_dir(mp_obj_t idx); +mp_obj_t mp_gpio_set_dir(mp_obj_t idx, mp_obj_t dir); +mp_obj_t mp_gpio_get_output(mp_obj_t idx); +mp_obj_t mp_gpio_set_output(mp_obj_t idx, mp_obj_t val); +mp_obj_t mp_gpio_get_input(mp_obj_t idx); +mp_obj_t mp_gpio_set_pull(mp_obj_t idx, mp_obj_t val); +mp_obj_t mp_gpio_set_func(mp_obj_t idx, mp_obj_t val); +mp_obj_t mp_gpio_query_func_value(mp_obj_t idx, mp_obj_t key); +mp_obj_t mp_gpio_query_pull_value(mp_obj_t key); +mp_obj_t mp_gpio_set_isr_mode(mp_obj_t idx, mp_obj_t mode); +mp_obj_t mp_gpio_get_isr_mode(mp_obj_t idx); +mp_obj_t mp_gpio_register_isr_func(mp_obj_t idx, mp_obj_t func, mp_obj_t arg); +mp_obj_t mp_gpio_unregister_isr_func(mp_obj_t idx); +mp_obj_t mp_gpio_set_isr_mask(mp_obj_t idx, mp_obj_t mask); + +#endif diff --git a/lite-python/dev/gpio_qstr.h b/lite-python/dev/gpio_qstr.h new file mode 100755 index 0000000..963e409 --- /dev/null +++ b/lite-python/dev/gpio_qstr.h @@ -0,0 +1,52 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +Q(_gpio_begin_) +Q(gpio_init) +Q(gpio_deinit) +Q(get_dir) +Q(set_dir) +Q(get_output) +Q(set_output) +Q(get_input) +Q(set_pull) +Q(set_func) +Q(query_pull_value) +Q(query_func_value) +Q(get_isr_mode) +Q(set_isr_mode) +Q(unregister_isr_func) +Q(register_isr_func) +Q(set_isr_mask) +Q(level_low) +Q(level_high) +Q(fall_low) +Q(rise_high) +Q(dir_in) +Q(dir_out) +Q(_gpio_end_) diff --git a/lite-python/dev/i2c_ex.h b/lite-python/dev/i2c_ex.h new file mode 100755 index 0000000..474ef99 --- /dev/null +++ b/lite-python/dev/i2c_ex.h @@ -0,0 +1,34 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +#ifndef I2C_EX_H +#define I2C_EX_H + +unsigned int DTI2cWriteRead(unsigned int id, unsigned short deviceAddr, unsigned char* txBuf, unsigned int txLen, unsigned char* rxBuf, unsigned int rxLen); + +#endif diff --git a/lite-python/dev/i2c_impl.c b/lite-python/dev/i2c_impl.c new file mode 100755 index 0000000..76fd06c --- /dev/null +++ b/lite-python/dev/i2c_impl.c @@ -0,0 +1,170 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +#include +#include "mperrno.h" +#include "utility.h" +#include "i2c_impl.h" +#include "i2c_ex.h" + +mp_obj_t mp_i2c_init(mp_obj_t idx, mp_obj_t baudrate) +{ + int id = mp_obj_get_int(idx); + unsigned int br = mp_obj_get_int(baudrate); + unsigned int r = IoTI2cInit(id, br); + + return mp_obj_new_int(r); +} + +mp_obj_t mp_i2c_deinit(mp_obj_t idx) +{ + int id = mp_obj_get_int(idx); + unsigned int r = IoTI2cDeinit(id); + + return mp_obj_new_int(r); +} + +mp_obj_t mp_i2c_write(mp_obj_t idx, mp_obj_t addr, mp_obj_t data) +{ + unsigned int r = -1; + int id = mp_obj_get_int(idx); + int devAddr = mp_obj_get_int(addr); + + if( mp_obj_is_type(data, &mp_type_list) ) + { + unsigned char* bytes = NULL; + size_t slen = pylist_to_integer_array(data, bytes); + + if( bytes ) + { + r = IoTI2cWrite(id, devAddr, bytes, slen); + } + else + { + mp_raise_OSError(MP_ENOMEM); + } + + free(bytes); + } + else + { + mp_raise_TypeError(MP_ERROR_TEXT("can't convert to list")); + } + + return mp_obj_new_int(r); +} + +mp_obj_t mp_i2c_read(mp_obj_t idx, mp_obj_t addr, mp_obj_t len) +{ + unsigned int r = -1; + int id = mp_obj_get_int(idx); + int devAddr = mp_obj_get_int(addr); + size_t rlen = mp_obj_get_int(len); + unsigned char* buffer = malloc(sizeof(*buffer) * rlen); + mp_obj_t ret[2] = + { + mp_obj_new_int(-1), + mp_const_none, + }; + + if( buffer ) + { + mp_obj_t pylist = mp_const_none; + + r = IoTI2cRead(id, devAddr, buffer, rlen); + + if( (r == 0) && ((pylist = integer_array_to_pylist(buffer, rlen)) != mp_const_none) ) + { + ret[0] = mp_obj_new_int(r); + ret[1] = pylist; + } + } + else + { + mp_raise_OSError(MP_ENOMEM); + } + + free(buffer); + + return mp_obj_new_tuple(2, ret); +} + +mp_obj_t mp_i2c_write_read(size_t n_args, const mp_obj_t* args) +{ + unsigned int r = -1; + int id = mp_obj_get_int(args[0]); + int devAddr = mp_obj_get_int(args[1]); + mp_obj_t ret[2] = + { + mp_obj_new_int(r), + mp_const_none, + }; + + if( mp_obj_is_type(args[2], &mp_type_list) ) + { + unsigned char* bytes = NULL; + size_t slen = pylist_to_integer_array(args[2], bytes); + size_t rlen = mp_obj_get_int(args[3]); + unsigned char* buffer = malloc(sizeof(*buffer) * rlen); + + if( bytes && buffer ) + { + mp_obj_t pylist = mp_const_none; + + r = DTI2cWriteRead(id, devAddr, bytes, slen, buffer, rlen); + + if( (r == 0) && ((pylist = integer_array_to_pylist(buffer, rlen)) != mp_const_none) ) + { + ret[0] = mp_obj_new_int(r); + ret[1] = pylist; + } + } + else + { + mp_raise_OSError(MP_ENOMEM); + } + + free(bytes); + free(buffer); + } + else + { + mp_raise_TypeError(MP_ERROR_TEXT("can't convert to list")); + } + + return mp_obj_new_tuple(2, ret); +} + +mp_obj_t mp_i2c_set_baudrate(mp_obj_t idx, mp_obj_t baudrate) +{ + int id = mp_obj_get_int(idx); + unsigned int br = mp_obj_get_int(baudrate); + unsigned int r = IoTI2cSetBaudrate(id, br); + + return mp_obj_new_int(r); +} diff --git a/lite-python/dev/i2c_impl.h b/lite-python/dev/i2c_impl.h new file mode 100755 index 0000000..a5095fa --- /dev/null +++ b/lite-python/dev/i2c_impl.h @@ -0,0 +1,43 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +#ifndef I2C_IMPL_H +#define I2C_IMPL_H + +#include "py/objtype.h" +#include "py/runtime.h" +#include "iot_i2c.h" + +mp_obj_t mp_i2c_init(mp_obj_t idx, mp_obj_t baudrate); +mp_obj_t mp_i2c_deinit(mp_obj_t idx); +mp_obj_t mp_i2c_write(mp_obj_t idx, mp_obj_t addr, mp_obj_t data); +mp_obj_t mp_i2c_read(mp_obj_t idx, mp_obj_t addr, mp_obj_t len); +mp_obj_t mp_i2c_write_read(size_t n_args, const mp_obj_t* args); +mp_obj_t mp_i2c_set_baudrate(mp_obj_t idx, mp_obj_t baudrate); + +#endif diff --git a/lite-python/dev/i2c_qstr.h b/lite-python/dev/i2c_qstr.h new file mode 100755 index 0000000..8e437b4 --- /dev/null +++ b/lite-python/dev/i2c_qstr.h @@ -0,0 +1,36 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +Q(_i2c_begin_) +Q(i2c_init) +Q(i2c_deinit) +Q(write) +Q(read) +Q(write_read) +Q(set_baudrate) +Q(_i2c_end_) diff --git a/lite-python/dev/modadc.c b/lite-python/dev/modadc.c new file mode 100755 index 0000000..76e80c1 --- /dev/null +++ b/lite-python/dev/modadc.c @@ -0,0 +1,58 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +#include "adc_impl.h" + +STATIC const MP_DEFINE_CONST_FUN_OBJ_2(mp_adc_set_model_obj, mp_adc_set_model); +STATIC const MP_DEFINE_CONST_FUN_OBJ_2(mp_adc_set_bais_obj, mp_adc_set_bais); +STATIC const MP_DEFINE_CONST_FUN_OBJ_2(mp_adc_set_reset_count_obj, mp_adc_set_reset_count); +STATIC const MP_DEFINE_CONST_FUN_OBJ_1(mp_adc_read_obj, mp_adc_read); +STATIC const MP_DEFINE_CONST_FUN_OBJ_1(mp_adc_query_bais_value_obj, mp_adc_query_bais_value); +STATIC const MP_DEFINE_CONST_FUN_OBJ_1(mp_adc_query_model_value_obj, mp_adc_query_model_value); + +STATIC const mp_rom_map_elem_t mp_module_adc_globals_table[] = +{ + {MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_adc)}, + {MP_ROM_QSTR(MP_QSTR_set_model), MP_ROM_PTR(&mp_adc_set_model_obj)}, + {MP_ROM_QSTR(MP_QSTR_set_bais), MP_ROM_PTR(&mp_adc_set_bais_obj)}, + {MP_ROM_QSTR(MP_QSTR_set_reset_count), MP_ROM_PTR(&mp_adc_set_reset_count_obj)}, + {MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_adc_read_obj)}, + {MP_ROM_QSTR(MP_QSTR_query_bais_value), MP_ROM_PTR(&mp_adc_query_bais_value_obj)}, + {MP_ROM_QSTR(MP_QSTR_query_model_value), MP_ROM_PTR(&mp_adc_query_model_value_obj)}, +}; + +STATIC MP_DEFINE_CONST_DICT(mp_module_adc_globals, mp_module_adc_globals_table); + +const mp_obj_module_t mp_module_adc = +{ + .base = {&mp_type_module}, + .globals = (mp_obj_dict_t*)&mp_module_adc_globals, +}; + +MP_REGISTER_MODULE(MP_QSTR_adc, mp_module_adc, 1); + diff --git a/lite-python/dev/modgpio.c b/lite-python/dev/modgpio.c new file mode 100755 index 0000000..6412378 --- /dev/null +++ b/lite-python/dev/modgpio.c @@ -0,0 +1,83 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +#include "gpio_impl.h" + +STATIC const MP_DEFINE_CONST_FUN_OBJ_1(mp_gpio_init_obj, mp_gpio_init); +STATIC const MP_DEFINE_CONST_FUN_OBJ_1(mp_gpio_deinit_obj, mp_gpio_deinit); +STATIC const MP_DEFINE_CONST_FUN_OBJ_1(mp_gpio_get_dir_obj, mp_gpio_get_dir); +STATIC const MP_DEFINE_CONST_FUN_OBJ_2(mp_gpio_set_dir_obj, mp_gpio_set_dir); +STATIC const MP_DEFINE_CONST_FUN_OBJ_1(mp_gpio_get_output_obj, mp_gpio_get_output); +STATIC const MP_DEFINE_CONST_FUN_OBJ_2(mp_gpio_set_output_obj, mp_gpio_set_output); +STATIC const MP_DEFINE_CONST_FUN_OBJ_1(mp_gpio_get_input_obj, mp_gpio_get_input); +STATIC const MP_DEFINE_CONST_FUN_OBJ_2(mp_gpio_set_pull_obj, mp_gpio_set_pull); +STATIC const MP_DEFINE_CONST_FUN_OBJ_2(mp_gpio_set_func_obj, mp_gpio_set_func); +STATIC const MP_DEFINE_CONST_FUN_OBJ_2(mp_gpio_query_func_value_obj, mp_gpio_query_func_value); +STATIC const MP_DEFINE_CONST_FUN_OBJ_1(mp_gpio_query_pull_value_obj, mp_gpio_query_pull_value); +STATIC const MP_DEFINE_CONST_FUN_OBJ_1(mp_gpio_get_isr_mode_obj, mp_gpio_get_isr_mode); +STATIC const MP_DEFINE_CONST_FUN_OBJ_2(mp_gpio_set_isr_mode_obj, mp_gpio_set_isr_mode); +STATIC const MP_DEFINE_CONST_FUN_OBJ_1(mp_gpio_unregister_isr_func_obj, mp_gpio_unregister_isr_func); +STATIC const MP_DEFINE_CONST_FUN_OBJ_3(mp_gpio_register_isr_func_obj, mp_gpio_register_isr_func); +STATIC const MP_DEFINE_CONST_FUN_OBJ_2(mp_gpio_set_isr_mask_obj, mp_gpio_set_isr_mask); + +STATIC const mp_rom_map_elem_t mp_module_gpio_globals_table[] = +{ + {MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_gpio)}, + {MP_ROM_QSTR(MP_QSTR_level_low), MP_ROM_INT(GPIO_TYPE_LEVEL_LOW)}, + {MP_ROM_QSTR(MP_QSTR_level_high), MP_ROM_INT(GPIO_TYPE_LEVEL_HIGH)}, + {MP_ROM_QSTR(MP_QSTR_fall_low), MP_ROM_INT(GPIO_TYPE_EDGE_FALL_LOW)}, + {MP_ROM_QSTR(MP_QSTR_rise_high), MP_ROM_INT(GPIO_TYPE_EDGE_RISE_HIGH)}, + {MP_ROM_QSTR(MP_QSTR_dir_in), MP_ROM_INT(IOT_GPIO_DIR_IN)}, + {MP_ROM_QSTR(MP_QSTR_dir_out), MP_ROM_INT(IOT_GPIO_DIR_OUT)}, + {MP_ROM_QSTR(MP_QSTR_gpio_init), MP_ROM_PTR(&mp_gpio_init_obj)}, + {MP_ROM_QSTR(MP_QSTR_gpio_deinit), MP_ROM_PTR(&mp_gpio_deinit_obj)}, + {MP_ROM_QSTR(MP_QSTR_set_dir), MP_ROM_PTR(&mp_gpio_set_dir_obj)}, + {MP_ROM_QSTR(MP_QSTR_get_dir), MP_ROM_PTR(&mp_gpio_get_dir_obj)}, + {MP_ROM_QSTR(MP_QSTR_set_output), MP_ROM_PTR(&mp_gpio_set_output_obj)}, + {MP_ROM_QSTR(MP_QSTR_get_output), MP_ROM_PTR(&mp_gpio_get_output_obj)}, + {MP_ROM_QSTR(MP_QSTR_get_input), MP_ROM_PTR(&mp_gpio_get_input_obj)}, + {MP_ROM_QSTR(MP_QSTR_set_pull), MP_ROM_PTR(&mp_gpio_set_pull_obj)}, + {MP_ROM_QSTR(MP_QSTR_set_func), MP_ROM_PTR(&mp_gpio_set_func_obj)}, + {MP_ROM_QSTR(MP_QSTR_query_func_value), MP_ROM_PTR(&mp_gpio_query_func_value_obj)}, + {MP_ROM_QSTR(MP_QSTR_query_pull_value), MP_ROM_PTR(&mp_gpio_query_pull_value_obj)}, + {MP_ROM_QSTR(MP_QSTR_set_isr_mode), MP_ROM_PTR(&mp_gpio_set_isr_mode_obj)}, + {MP_ROM_QSTR(MP_QSTR_get_isr_mode), MP_ROM_PTR(&mp_gpio_get_isr_mode_obj)}, + {MP_ROM_QSTR(MP_QSTR_register_isr_func), MP_ROM_PTR(&mp_gpio_register_isr_func_obj)}, + {MP_ROM_QSTR(MP_QSTR_unregister_isr_func), MP_ROM_PTR(&mp_gpio_unregister_isr_func_obj)}, + {MP_ROM_QSTR(MP_QSTR_set_isr_mask), MP_ROM_PTR(&mp_gpio_set_isr_mask_obj)}, +}; + +STATIC MP_DEFINE_CONST_DICT(mp_module_gpio_globals, mp_module_gpio_globals_table); + +const mp_obj_module_t mp_module_gpio = +{ + .base = {&mp_type_module}, + .globals = (mp_obj_dict_t*)&mp_module_gpio_globals, +}; + +MP_REGISTER_MODULE(MP_QSTR_gpio, mp_module_gpio, 1); diff --git a/lite-python/dev/modi2c.c b/lite-python/dev/modi2c.c new file mode 100755 index 0000000..d1643d1 --- /dev/null +++ b/lite-python/dev/modi2c.c @@ -0,0 +1,57 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +#include "i2c_impl.h" + +STATIC const MP_DEFINE_CONST_FUN_OBJ_2(mp_i2c_init_obj, mp_i2c_init); +STATIC const MP_DEFINE_CONST_FUN_OBJ_1(mp_i2c_deinit_obj, mp_i2c_deinit); +STATIC const MP_DEFINE_CONST_FUN_OBJ_3(mp_i2c_write_obj, mp_i2c_write); +STATIC const MP_DEFINE_CONST_FUN_OBJ_3(mp_i2c_read_obj, mp_i2c_read); +STATIC const MP_DEFINE_CONST_FUN_OBJ_2(mp_i2c_set_baudrate_obj, mp_i2c_set_baudrate); +STATIC const MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_i2c_write_read_obj, 4, 4, mp_i2c_write_read); + +STATIC const mp_rom_map_elem_t mp_module_i2c_globals_table[] = +{ + {MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_i2c)}, + {MP_ROM_QSTR(MP_QSTR_i2c_init), MP_ROM_PTR(&mp_i2c_init_obj)}, + {MP_ROM_QSTR(MP_QSTR_i2c_deinit), MP_ROM_PTR(&mp_i2c_deinit_obj)}, + {MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_i2c_write_obj)}, + {MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_i2c_read_obj)}, + {MP_ROM_QSTR(MP_QSTR_write_read), MP_ROM_PTR(&mp_i2c_write_read_obj)}, + {MP_ROM_QSTR(MP_QSTR_set_baudrate), MP_ROM_PTR(&mp_i2c_set_baudrate_obj)}, +}; + +STATIC MP_DEFINE_CONST_DICT(mp_module_i2c_globals, mp_module_i2c_globals_table); + +const mp_obj_module_t mp_module_i2c = +{ + .base = {&mp_type_module}, + .globals = (mp_obj_dict_t*)&mp_module_i2c_globals, +}; + +MP_REGISTER_MODULE(MP_QSTR_i2c, mp_module_i2c, 1); diff --git a/lite-python/dev/modos.c b/lite-python/dev/modos.c new file mode 100755 index 0000000..296ef8e --- /dev/null +++ b/lite-python/dev/modos.c @@ -0,0 +1,55 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +#include "os_impl.h" + +STATIC const MP_DEFINE_CONST_FUN_OBJ_1(mp_os_sleep_obj, mp_os_sleep); +STATIC const MP_DEFINE_CONST_FUN_OBJ_1(mp_os_usleep_obj, mp_os_usleep); +STATIC const MP_DEFINE_CONST_FUN_OBJ_1(mp_os_delay_obj, mp_os_delay); +STATIC const MP_DEFINE_CONST_FUN_OBJ_1(mp_os_delay_until_obj, mp_os_delay_until); +STATIC const MP_DEFINE_CONST_FUN_OBJ_0(mp_os_get_tick_obj, mp_os_get_tick); + +STATIC const mp_rom_map_elem_t mp_module_os_globals_table[] = +{ + {MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_os)}, + {MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mp_os_sleep_obj)}, + {MP_ROM_QSTR(MP_QSTR_usleep), MP_ROM_PTR(&mp_os_usleep_obj)}, + {MP_ROM_QSTR(MP_QSTR_delay), MP_ROM_PTR(&mp_os_delay_obj)}, + {MP_ROM_QSTR(MP_QSTR_delay_until), MP_ROM_PTR(&mp_os_delay_until_obj)}, + {MP_ROM_QSTR(MP_QSTR_get_tick), MP_ROM_PTR(&mp_os_get_tick_obj)}, +}; + +STATIC MP_DEFINE_CONST_DICT(mp_module_os_globals, mp_module_os_globals_table); + +const mp_obj_module_t mp_module_os = +{ + .base = {&mp_type_module}, + .globals = (mp_obj_dict_t*)&mp_module_os_globals, +}; + +MP_REGISTER_MODULE(MP_QSTR_os, mp_module_os, 1); diff --git a/lite-python/dev/modpwm.c b/lite-python/dev/modpwm.c new file mode 100755 index 0000000..36fe043 --- /dev/null +++ b/lite-python/dev/modpwm.c @@ -0,0 +1,54 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +#include "pwm_impl.h" + +STATIC const MP_DEFINE_CONST_FUN_OBJ_1(mp_pwm_init_obj, mp_pwm_init); +STATIC const MP_DEFINE_CONST_FUN_OBJ_1(mp_pwm_deinit_obj, mp_pwm_deinit); +STATIC const MP_DEFINE_CONST_FUN_OBJ_3(mp_pwm_start_obj, mp_pwm_start); +STATIC const MP_DEFINE_CONST_FUN_OBJ_1(mp_pwm_stop_obj, mp_pwm_stop); + +STATIC const mp_rom_map_elem_t mp_module_pwm_globals_table[] = +{ + {MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_adc)}, + {MP_ROM_QSTR(MP_QSTR_pwm_init), MP_ROM_PTR(&mp_pwm_init_obj)}, + {MP_ROM_QSTR(MP_QSTR_pwm_deinit), MP_ROM_PTR(&mp_pwm_deinit_obj)}, + {MP_ROM_QSTR(MP_QSTR_start), MP_ROM_PTR(&mp_pwm_start_obj)}, + {MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&mp_pwm_stop_obj)}, +}; + +STATIC MP_DEFINE_CONST_DICT(mp_module_pwm_globals, mp_module_pwm_globals_table); + +const mp_obj_module_t mp_module_pwm = +{ + .base = {&mp_type_module}, + .globals = (mp_obj_dict_t*)&mp_module_pwm_globals, +}; + +MP_REGISTER_MODULE(MP_QSTR_pwm, mp_module_pwm, 1); + diff --git a/lite-python/dev/modspi.c b/lite-python/dev/modspi.c new file mode 100755 index 0000000..363c482 --- /dev/null +++ b/lite-python/dev/modspi.c @@ -0,0 +1,73 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +#include +#include "mperrno.h" +#include "utility.h" +#include "spi_impl.h" + +STATIC const type_field_info_t g_field_offset[] = +{ + type_offset_value("cpol", SpiInfo, cpol, 0, 1), + type_offset_value("cpha", SpiInfo, cpha, 0, 1), + type_offset_value("protocol", SpiInfo, protocol, 0, 4), + type_offset_value("data_width", SpiInfo, dataWidth, 0, 16), + type_offset_value("endian", SpiInfo, endian, 0, 1), + type_offset_value("freq", SpiInfo, freq, 0, 0xFFFFFFFF), +}; + +class_func(spi_info, g_field_offset); + +STATIC const MP_DEFINE_CONST_FUN_OBJ_2(mp_spi_init_obj, mp_spi_init); +STATIC const MP_DEFINE_CONST_FUN_OBJ_1(mp_spi_deinit_obj, mp_spi_deinit); +STATIC const MP_DEFINE_CONST_FUN_OBJ_2(mp_spi_host_write_obj, mp_spi_host_write); +STATIC const MP_DEFINE_CONST_FUN_OBJ_2(mp_spi_host_read_obj, mp_spi_host_read); +STATIC const MP_DEFINE_CONST_FUN_OBJ_3(mp_spi_host_write_read_obj, mp_spi_host_write_read); +STATIC const MP_DEFINE_CONST_FUN_OBJ_2(mp_spi_set_info_obj, mp_spi_set_info); + +STATIC const mp_rom_map_elem_t mp_module_spi_globals_table[] = +{ + {MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_spi)}, + {MP_ROM_QSTR(MP_QSTR_spi_info), MP_ROM_PTR(&class_object(spi_info))}, + {MP_ROM_QSTR(MP_QSTR_spi_init), MP_ROM_PTR(&mp_spi_init_obj)}, + {MP_ROM_QSTR(MP_QSTR_spi_deinit), MP_ROM_PTR(&mp_spi_deinit_obj)}, + {MP_ROM_QSTR(MP_QSTR_host_write), MP_ROM_PTR(&mp_spi_host_write_obj)}, + {MP_ROM_QSTR(MP_QSTR_host_read), MP_ROM_PTR(&mp_spi_host_read_obj)}, + {MP_ROM_QSTR(MP_QSTR_host_write_read), MP_ROM_PTR(&mp_spi_host_write_read_obj)}, + {MP_ROM_QSTR(MP_QSTR_set_info), MP_ROM_PTR(&mp_spi_set_info_obj)}, +}; + +STATIC MP_DEFINE_CONST_DICT(mp_module_spi_globals, mp_module_spi_globals_table); + +const mp_obj_module_t mp_module_spi = +{ + .base = {&mp_type_module}, + .globals = (mp_obj_dict_t*)&mp_module_spi_globals, +}; + +MP_REGISTER_MODULE(MP_QSTR_spi, mp_module_spi, 1); diff --git a/lite-python/dev/moduart.c b/lite-python/dev/moduart.c new file mode 100755 index 0000000..1b19937 --- /dev/null +++ b/lite-python/dev/moduart.c @@ -0,0 +1,84 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +#include +#include "mperrno.h" +#include "utility.h" +#include "uart_impl.h" + +STATIC const type_field_info_t g_field_offset[] = +{ + type_offset_value("baudRate", IotUartAttribute, baudRate, 0, 0xFFFFFFFF), + type_offset_value("dataBits", IotUartAttribute, dataBits, IOT_UART_DATA_BIT_5, IOT_UART_DATA_BIT_8), + type_offset_value("stopBits", IotUartAttribute, stopBits, IOT_UART_STOP_BIT_1, IOT_UART_STOP_BIT_2), + type_offset_value("parity", IotUartAttribute, parity, IOT_UART_PARITY_NONE, IOT_UART_PARITY_EVEN), +}; + +class_func(uart_attr, g_field_offset); + +STATIC const MP_DEFINE_CONST_FUN_OBJ_2(mp_uart_init_obj, mp_uart_init); +STATIC const MP_DEFINE_CONST_FUN_OBJ_1(mp_uart_deinit_obj, mp_uart_deinit); +STATIC const MP_DEFINE_CONST_FUN_OBJ_2(mp_uart_write_obj, mp_uart_write); +STATIC const MP_DEFINE_CONST_FUN_OBJ_1(mp_uart_read_obj, mp_uart_read); +STATIC const MP_DEFINE_CONST_FUN_OBJ_2(mp_uart_set_flow_ctrl_obj, mp_uart_set_flow_ctrl); + +STATIC const mp_rom_map_elem_t mp_module_uart_globals_table[] = +{ + {MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_uart)}, + {MP_ROM_QSTR(MP_QSTR_data_bit_5), MP_ROM_INT(IOT_UART_DATA_BIT_5)}, + {MP_ROM_QSTR(MP_QSTR_data_bit_6), MP_ROM_INT(IOT_UART_DATA_BIT_6)}, + {MP_ROM_QSTR(MP_QSTR_data_bit_7), MP_ROM_INT(IOT_UART_DATA_BIT_7)}, + {MP_ROM_QSTR(MP_QSTR_data_bit_8), MP_ROM_INT(IOT_UART_DATA_BIT_8)}, + {MP_ROM_QSTR(MP_QSTR_stop_bit_1), MP_ROM_INT(IOT_UART_STOP_BIT_1)}, + {MP_ROM_QSTR(MP_QSTR_stop_bit_2), MP_ROM_INT(IOT_UART_STOP_BIT_2)}, + {MP_ROM_QSTR(MP_QSTR_parity_none), MP_ROM_INT(IOT_UART_PARITY_NONE)}, + {MP_ROM_QSTR(MP_QSTR_parity_odd), MP_ROM_INT(IOT_UART_PARITY_ODD)}, + {MP_ROM_QSTR(MP_QSTR_parity_even), MP_ROM_INT(IOT_UART_PARITY_EVEN)}, + {MP_ROM_QSTR(MP_QSTR_flow_ctrl_none), MP_ROM_INT(IOT_FLOW_CTRL_NONE)}, + {MP_ROM_QSTR(MP_QSTR_flow_ctrl_rts_cts), MP_ROM_INT(IOT_FLOW_CTRL_RTS_CTS)}, + {MP_ROM_QSTR(MP_QSTR_flow_ctrl_rts_only), MP_ROM_INT(IOT_FLOW_CTRL_RTS_ONLY)}, + {MP_ROM_QSTR(MP_QSTR_flow_ctrl_cts_only), MP_ROM_INT(IOT_FLOW_CTRL_CTS_ONLY)}, + {MP_ROM_QSTR(MP_QSTR_uart_attr), MP_ROM_PTR(&class_object(uart_attr))}, + {MP_ROM_QSTR(MP_QSTR_uart_init), MP_ROM_PTR(&mp_uart_init_obj)}, + {MP_ROM_QSTR(MP_QSTR_uart_deinit), MP_ROM_PTR(&mp_uart_deinit_obj)}, + {MP_ROM_QSTR(MP_QSTR_write), MP_ROM_PTR(&mp_uart_write_obj)}, + {MP_ROM_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_uart_read_obj)}, + {MP_ROM_QSTR(MP_QSTR_set_flow_ctrl), MP_ROM_PTR(&mp_uart_set_flow_ctrl_obj)}, +}; + +STATIC MP_DEFINE_CONST_DICT(mp_module_uart_globals, mp_module_uart_globals_table); + +const mp_obj_module_t mp_module_uart = +{ + .base = {&mp_type_module}, + .globals = (mp_obj_dict_t*)&mp_module_uart_globals, +}; + +MP_REGISTER_MODULE(MP_QSTR_uart, mp_module_uart, 1); + + diff --git a/lite-python/demo/dt_python_demo.c b/lite-python/dev/os_impl.c similarity index 51% rename from lite-python/demo/dt_python_demo.c rename to lite-python/dev/os_impl.c index 67de6de..379aa81 100755 --- a/lite-python/demo/dt_python_demo.c +++ b/lite-python/dev/os_impl.c @@ -1,59 +1,64 @@ -/**************************************************************************** - -MIT License - -Copyright (c) 2020 唐佐林 - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - -*****************************************************************************/ - -#include -#include -#include "ohos_init.h" -#include "cmsis_os2.h" -#include "wifiiot_gpio.h" -#include "wifiiot_gpio_ex.h" -#include "wifiiot_pwm.h" - -#include "dtpython.h" - -extern const char* c_test_py; // test.py -extern const char* c_another_py; // another.py - -static void DTPython_Demo_Entry(void) -{ - printf("[DTPython_Demo] DTPython_Demo_Entry()\n"); - - DTPython_Init(); // 初始化Python环境 - - DTPython_RunCode("print(\'Python Code Begin\')"); // 执行Python语句:print('Python Code Begin') - - DTPython_RunCode("s = \'HOS Device Development\'"); // 执行Python语句:s = 'HOS Device Development' - DTPython_RunCode("print(s)"); // 执行Python语句:print(s) - - DTPython_RunCode(c_test_py); // 模拟执行Python文件:DTPython_RunFile("test.py"); - DTPython_RunCode(c_another_py); // 模拟执行Python文件:DTPython_RunFile("another.py"); - - DTPython_RunCode("print(\'Python Code End\')"); // 执行Python语句:print('Python Code End') - - DTPython_Deinit(); // 清理Python环境 -} - -SYS_RUN(DTPython_Demo_Entry); +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +#include +#include "cmsis_os2.h" +#include "os_impl.h" + +mp_obj_t mp_os_sleep(mp_obj_t s) +{ + sleep(mp_obj_get_int_truncated(s)); + + return mp_const_none; +} + +mp_obj_t mp_os_usleep(mp_obj_t us) +{ + usleep(mp_obj_get_int_truncated(us)); + + return mp_const_none; +} + +mp_obj_t mp_os_delay(mp_obj_t tks) +{ + osDelay(mp_obj_get_int_truncated(tks)); + + return mp_const_none; +} + +mp_obj_t mp_os_delay_until(mp_obj_t tks) +{ + osDelayUntil(mp_obj_get_int_truncated(tks)); + + return mp_const_none; +} + +mp_obj_t mp_os_get_tick() +{ + return mp_obj_new_int_from_uint(osKernelGetTickCount()); +} diff --git a/lite-python/dev/os_impl.h b/lite-python/dev/os_impl.h new file mode 100755 index 0000000..83866e0 --- /dev/null +++ b/lite-python/dev/os_impl.h @@ -0,0 +1,41 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +#ifndef OS_IMPL_H +#define OS_IMPL_H + +#include "py/objtype.h" +#include "py/runtime.h" + +mp_obj_t mp_os_sleep(mp_obj_t s); +mp_obj_t mp_os_usleep(mp_obj_t us); +mp_obj_t mp_os_delay(mp_obj_t tks); +mp_obj_t mp_os_delay_until(mp_obj_t tks); +mp_obj_t mp_os_get_tick(); + +#endif diff --git a/lite-python/dev/os_qstr.h b/lite-python/dev/os_qstr.h new file mode 100755 index 0000000..927387d --- /dev/null +++ b/lite-python/dev/os_qstr.h @@ -0,0 +1,35 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +Q(_os_begin_) +Q(sleep) +Q(usleep) +Q(delay) +Q(delay_until) +Q(get_tick) +Q(_os_end_) diff --git a/lite-python/dev/pwm_impl.c b/lite-python/dev/pwm_impl.c new file mode 100755 index 0000000..807d1d0 --- /dev/null +++ b/lite-python/dev/pwm_impl.c @@ -0,0 +1,51 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +#include "pwm_impl.h" + +mp_obj_t mp_pwm_init(mp_obj_t port) +{ + return mp_obj_new_int(IoTPwmInit(mp_obj_get_int(port))); +} + +mp_obj_t mp_pwm_deinit(mp_obj_t port) +{ + return mp_obj_new_int(IoTPwmDeinit(mp_obj_get_int(port))); +} + +mp_obj_t mp_pwm_start(mp_obj_t port, mp_obj_t duty, mp_obj_t freq) +{ + return mp_obj_new_int(IoTPwmStart(mp_obj_get_int(port), mp_obj_get_int(duty), mp_obj_get_int(freq))); +} + +mp_obj_t mp_pwm_stop(mp_obj_t port) +{ + return mp_obj_new_int(IoTPwmStop(mp_obj_get_int(port))); +} + + diff --git a/lite-python/dev/pwm_impl.h b/lite-python/dev/pwm_impl.h new file mode 100755 index 0000000..0994670 --- /dev/null +++ b/lite-python/dev/pwm_impl.h @@ -0,0 +1,41 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +#ifndef PWM_IMPL_H +#define PWM_IMPL_H + +#include "py/objtype.h" +#include "py/runtime.h" +#include "iot_pwm.h" + +mp_obj_t mp_pwm_init(mp_obj_t port); +mp_obj_t mp_pwm_deinit(mp_obj_t port); +mp_obj_t mp_pwm_start(mp_obj_t port, mp_obj_t duty, mp_obj_t freq); +mp_obj_t mp_pwm_stop(mp_obj_t port); + +#endif diff --git a/lite-python/dev/pwm_qstr.h b/lite-python/dev/pwm_qstr.h new file mode 100755 index 0000000..0bc6909 --- /dev/null +++ b/lite-python/dev/pwm_qstr.h @@ -0,0 +1,35 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +Q(_pwm_begin_) +Q(pwm_init) +Q(pwm_deinit) +Q(start) +Q(stop) +Q(_pwm_end_) + diff --git a/lite-python/dev/setting.mk b/lite-python/dev/setting.mk new file mode 100755 index 0000000..8ada5f7 --- /dev/null +++ b/lite-python/dev/setting.mk @@ -0,0 +1,28 @@ +# MIT License +# +# Copyright (c) 2021 唐佐林 +# WeChat : delphi_tang +# EMail: delphi_tang@dt4sw.com +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in all +# copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +QSTR_DEFS += $(wildcard $(DEVPATH)/*_qstr.h) +INC += -I$(PROPATH)/base/iot_hardware/peripheral/interfaces/kits +INC += -I$(PROPATH)/kernel/liteos_m/kal/cmsis +INC += -I$(PROPATH)/utils/native/lite/include diff --git a/lite-python/dev/spi_ex.h b/lite-python/dev/spi_ex.h new file mode 100755 index 0000000..5f541ba --- /dev/null +++ b/lite-python/dev/spi_ex.h @@ -0,0 +1,48 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +#ifndef SPI_EX_H +#define SPI_EX_H + +typedef struct +{ + unsigned int cpol; + unsigned int cpha; + unsigned int protocol; + unsigned int dataWidth; + unsigned int endian; + unsigned int freq; +} SpiInfo; + +unsigned int DTSpiInit(unsigned int id, SpiInfo info); +unsigned int DTSpiDeinit(unsigned int id); +unsigned int DTSpiHostWrite(unsigned int id, unsigned char* writeData, unsigned int byteLen); +unsigned int DTSpiHostRead(unsigned int id, unsigned char* readData, unsigned int byteLen); +unsigned int DTSpiSetInfo(unsigned int id, SpiInfo info); + +#endif diff --git a/lite-python/dev/spi_impl.c b/lite-python/dev/spi_impl.c new file mode 100755 index 0000000..f8445e1 --- /dev/null +++ b/lite-python/dev/spi_impl.c @@ -0,0 +1,189 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +#include +#include "mperrno.h" +#include "utility.h" +#include "spi_impl.h" +#include "spi_ex.h" + +extern const mp_obj_type_t class_object(spi_info); + +mp_obj_t mp_spi_init(mp_obj_t idx, mp_obj_t info) +{ + int id = mp_obj_get_int(idx); + unsigned int r = -1; + + if( mp_obj_is_type(info, &class_object(spi_info)) ) + { + class_name(spi_info)* pInfo = MP_OBJ_TO_PTR(info); + + r = DTSpiInit(id, pInfo->member); + } + else + { + mp_raise_TypeError(MP_ERROR_TEXT("can't convert to spi_info")); + } + + return mp_obj_new_int(r); +} + +mp_obj_t mp_spi_deinit(mp_obj_t idx) +{ + return mp_obj_new_int(DTSpiDeinit(mp_obj_get_int(idx))); +} + +mp_obj_t mp_spi_host_write(mp_obj_t idx, mp_obj_t data) +{ + unsigned int r = -1; + int id = mp_obj_get_int(idx); + + if( mp_obj_is_type(data, &mp_type_list) ) + { + unsigned char* bytes = NULL; + size_t len = pylist_to_integer_array(data, bytes); + + if( bytes ) + { + r = DTSpiHostWrite(id, bytes, len) >= 0 ? 0 : -1; + } + else + { + mp_raise_OSError(MP_ENOMEM); + } + + free(bytes); + } + else + { + mp_raise_TypeError(MP_ERROR_TEXT("can't convert to list")); + } + + return mp_obj_new_int(r); +} + +mp_obj_t mp_spi_host_read(mp_obj_t idx, mp_obj_t len) +{ + unsigned int r = -1; + int id = mp_obj_get_int(idx); + size_t rlen = mp_obj_get_int(len); + unsigned char* buffer = malloc(sizeof(*buffer) * rlen); + mp_obj_t ret[2] = + { + mp_obj_new_int(-1), + mp_const_none, + }; + + if( buffer ) + { + mp_obj_t pylist = mp_const_none; + + r = DTSpiHostRead(id, buffer, rlen) >= 0 ? 0 : -1; + + if( (r == 0) && ((pylist = integer_array_to_pylist(buffer, rlen)) != mp_const_none) ) + { + ret[0] = mp_obj_new_int(0); + ret[1] = pylist; + } + } + else + { + mp_raise_OSError(MP_ENOMEM); + } + + free(buffer); + + return mp_obj_new_tuple(2, ret); +} + +mp_obj_t mp_spi_host_write_read(mp_obj_t idx, mp_obj_t data, mp_obj_t n) +{ + unsigned int r = -1; + int id = mp_obj_get_int(idx); + mp_obj_t ret[2] = + { + mp_obj_new_int(-1), + mp_const_none, + }; + + if( mp_obj_is_type(data, &mp_type_list) ) + { + size_t rlen = mp_obj_get_int(n); + unsigned char* bytes = NULL; + size_t slen = pylist_to_integer_array(data, bytes); + unsigned char* buffer = malloc(sizeof(*buffer) * rlen); + + if( bytes && buffer ) + { + mp_obj_t pylist = mp_const_none; + + r = DTSpiHostWrite(id, bytes, slen) >= 0 ? 0 : -1; + r = (r == 0) ? (DTSpiHostRead(id, buffer, rlen) >= 0 ? 0 : -1) : -1; + + if( (r == 0) && ((pylist = integer_array_to_pylist(buffer, rlen)) != mp_const_none) ) + { + ret[0] = mp_obj_new_int(0); + ret[1] = pylist; + } + } + else + { + mp_raise_OSError(MP_ENOMEM); + } + + free(bytes); + free(buffer); + } + else + { + mp_raise_TypeError(MP_ERROR_TEXT("can't convert to list")); + } + + return mp_obj_new_tuple(2, ret); +} + +mp_obj_t mp_spi_set_info(mp_obj_t idx, mp_obj_t info) +{ + int id = mp_obj_get_int(idx); + unsigned int r = -1; + + if( mp_obj_is_type(info, &class_object(spi_info)) ) + { + class_name(spi_info)* pInfo = MP_OBJ_TO_PTR(info); + + r = DTSpiSetInfo(id, pInfo->member); + } + else + { + mp_raise_TypeError(MP_ERROR_TEXT("can't convert to spi_info")); + } + + return mp_obj_new_int(r); +} + + diff --git a/lite-python/dev/spi_impl.h b/lite-python/dev/spi_impl.h new file mode 100755 index 0000000..2d89426 --- /dev/null +++ b/lite-python/dev/spi_impl.h @@ -0,0 +1,46 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +#ifndef UART_IMPL_H +#define UART_IMPL_H + +#include "py/objtype.h" +#include "py/runtime.h" +#include "spi_ex.h" + +class_type(spi_info, SpiInfo); + +mp_obj_t mp_spi_init(mp_obj_t idx, mp_obj_t info); +mp_obj_t mp_spi_deinit(mp_obj_t idx); +mp_obj_t mp_spi_host_write(mp_obj_t idx, mp_obj_t data); +mp_obj_t mp_spi_host_read(mp_obj_t idx, mp_obj_t n); +mp_obj_t mp_spi_host_write_read(mp_obj_t idx, mp_obj_t data, mp_obj_t n); +mp_obj_t mp_spi_set_info(mp_obj_t idx, mp_obj_t info); + + +#endif diff --git a/lite-python/dev/spi_qstr.h b/lite-python/dev/spi_qstr.h new file mode 100755 index 0000000..17396f4 --- /dev/null +++ b/lite-python/dev/spi_qstr.h @@ -0,0 +1,38 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +Q(_spi_begin_) +Q(spi_info) +Q(spi_init) +Q(spi_deinit) +Q(host_write) +Q(host_read) +Q(host_write_read) +Q(set_info) +Q(_spi_end_) + diff --git a/lite-python/dev/uart_impl.c b/lite-python/dev/uart_impl.c new file mode 100755 index 0000000..76475ca --- /dev/null +++ b/lite-python/dev/uart_impl.c @@ -0,0 +1,134 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +#include +#include "mperrno.h" +#include "utility.h" +#include "uart_impl.h" + +extern const mp_obj_type_t class_object(uart_attr); + +mp_obj_t mp_uart_init(mp_obj_t idx, mp_obj_t attr) +{ + int id = mp_obj_get_int(idx); + unsigned int r = -1; + + if( mp_obj_is_type(attr, &class_object(uart_attr)) ) + { + class_name(uart_attr)* pAttr = MP_OBJ_TO_PTR(attr); + + pAttr->member.rxBlock = IOT_UART_BLOCK_STATE_BLOCK; + pAttr->member.txBlock = IOT_UART_BLOCK_STATE_BLOCK; + + r = IoTUartInit(id, &pAttr->member); + } + else + { + mp_raise_TypeError(MP_ERROR_TEXT("can't convert to uart_attr")); + } + + return mp_obj_new_int(r); +} + +mp_obj_t mp_uart_deinit(mp_obj_t idx) +{ + return mp_obj_new_int(IoTUartDeinit(mp_obj_get_int(idx))); +} + +mp_obj_t mp_uart_write(mp_obj_t idx, mp_obj_t data) +{ + unsigned int r = -1; + int id = mp_obj_get_int(idx); + + if( mp_obj_is_type(data, &mp_type_list) ) + { + unsigned char* bytes = NULL; + size_t len = pylist_to_integer_array(data, bytes); + + if( bytes ) + { + r = IoTUartWrite(id, bytes, len) >= 0 ? 0 : -1; + } + else + { + mp_raise_OSError(MP_ENOMEM); + } + + free(bytes); + } + else + { + mp_raise_TypeError(MP_ERROR_TEXT("can't convert to list")); + } + + return mp_obj_new_int(r); +} + +mp_obj_t mp_uart_read(mp_obj_t idx) +{ + enum { BUFF_SIZE = 256 }; + + unsigned int r = -1; + int id = mp_obj_get_int(idx); + unsigned char* buffer = malloc(sizeof(*buffer) * BUFF_SIZE); + mp_obj_t ret[2] = + { + mp_obj_new_int(-1), + mp_const_none, + }; + + if( buffer ) + { + mp_obj_t pylist = mp_const_none; + + r = IoTUartRead(id, buffer, BUFF_SIZE) >= 0 ? 0 : -1; + + if( (r == 0) && ((pylist = integer_array_to_pylist(buffer, r)) != mp_const_none) ) + { + ret[0] = mp_obj_new_int(0); + ret[1] = pylist; + } + } + else + { + mp_raise_OSError(MP_ENOMEM); + } + + free(buffer); + + return mp_obj_new_tuple(2, ret); +} + +mp_obj_t mp_uart_set_flow_ctrl(mp_obj_t idx, mp_obj_t ctrl) +{ + int id = mp_obj_get_int(idx); + unsigned int fc = mp_obj_get_int(ctrl); + unsigned int r = IoTUartSetFlowCtrl(id, (IotFlowCtrl)fc); + + return mp_obj_new_int(r); +} diff --git a/lite-python/dev/uart_impl.h b/lite-python/dev/uart_impl.h new file mode 100755 index 0000000..1c83eee --- /dev/null +++ b/lite-python/dev/uart_impl.h @@ -0,0 +1,45 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +#ifndef UART_IMPL_H +#define UART_IMPL_H + +#include "py/objtype.h" +#include "py/runtime.h" +#include "iot_uart.h" + +class_type(uart_attr, IotUartAttribute); + +mp_obj_t mp_uart_init(mp_obj_t idx, mp_obj_t attr); +mp_obj_t mp_uart_deinit(mp_obj_t idx); +mp_obj_t mp_uart_write(mp_obj_t idx, mp_obj_t data); +mp_obj_t mp_uart_read(mp_obj_t idx); +mp_obj_t mp_uart_set_flow_ctrl(mp_obj_t idx, mp_obj_t ctrl); + +#endif + diff --git a/lite-python/dev/uart_qstr.h b/lite-python/dev/uart_qstr.h new file mode 100755 index 0000000..be58b86 --- /dev/null +++ b/lite-python/dev/uart_qstr.h @@ -0,0 +1,50 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +Q(_uart_begin_) +Q(uart_attr) +Q(uart_init) +Q(uart_deinit) +Q(write) +Q(read) +Q(set_flow_ctrl) +Q(data_bit_5) +Q(data_bit_6) +Q(data_bit_7) +Q(data_bit_8) +Q(stop_bit_1) +Q(stop_bit_2) +Q(parity_none) +Q(parity_odd) +Q(parity_even) +Q(flow_ctrl_none) +Q(flow_ctrl_rts_cts) +Q(flow_ctrl_rts_only) +Q(flow_ctrl_cts_only) +Q(_uart_end_) + diff --git a/lite-python/dev/utility.c b/lite-python/dev/utility.c new file mode 100755 index 0000000..5431098 --- /dev/null +++ b/lite-python/dev/utility.c @@ -0,0 +1,72 @@ +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +#include "utility.h" + +void object_print(const mp_print_t* print, mp_obj_t self_in, mp_print_kind_t kind) +{ + mp_printf(print, "<%s object at %p>", mp_obj_get_type_str(self_in), MP_OBJ_TO_PTR(self_in)); +} + +const type_field_info_t* find_field_info(const char* name, const type_field_info_t* iftbl, size_t size) +{ + const type_field_info_t* ret = NULL; + int i = 0; + + for(i=0; i +#include "py/objtype.h" +#include "py/runtime.h" + +typedef struct +{ + const char* name; + unsigned int offset; + unsigned int size; + unsigned int min; + unsigned int max; +} type_field_info_t; + +typedef struct +{ + const char* key; + const int val; +} key_val_t; + +void object_print(const mp_print_t* print, mp_obj_t self_in, mp_print_kind_t kind); +const type_field_info_t* find_field_info(const char* name, const type_field_info_t* iftbl, size_t size); +int query_value(const char* key, const key_val_t* kvt, size_t len); + +#define dim(a) (sizeof(a)/sizeof(*a)) + +#define type_offset_value(name, type, member, min, max) {name, offsetof(type, member), sizeof(((type*)0)->member), min, max} + +#define pylist_to_integer_array(pylist, pint) \ +({ \ + size_t len = 0; \ + mp_obj_t* item = NULL; \ + mp_obj_list_get(pylist, &len, &item); \ + pint = malloc(sizeof(*pint) * len); \ + if( pint ) \ + { \ + int i = 0; \ + for(i=0; ibase.type = &class_object(n); \ + return MP_OBJ_FROM_PTR(self); \ +} \ +STATIC void n##_query(mp_obj_t self_in, qstr attr, mp_obj_t* dest) \ +{ \ + mp_##n##_t* self = MP_OBJ_TO_PTR(self_in); \ + const type_field_info_t* info = find_field_info(qstr_str(attr), a, dim(a)); \ + if( info ) \ + { \ + if( (dest[0] == MP_OBJ_NULL) && (dest[1] == MP_OBJ_NULL) ) \ + { \ + unsigned int value = 0; \ + memcpy(&value, (char*)&self->member + info->offset, info->size); \ + dest[0] = mp_obj_new_int_from_uint(value); \ + } \ + else if( (dest[0] == MP_OBJ_SENTINEL) && (dest[1] != MP_OBJ_NULL) ) \ + { \ + unsigned int value = mp_obj_get_int(dest[1]); \ + if( (info->min <= value) && (value <= info->max) ) \ + { \ + memcpy((char*)&self->member + info->offset, &value, info->size); \ + dest[0] = MP_OBJ_NULL; \ + } \ + else \ + { \ + mp_raise_OSError(MP_EINVAL); \ + } \ + } \ + } \ +} + +#endif diff --git a/lite-python/dtpython.c b/lite-python/dtpython.c index 56065c7..20cae80 100755 --- a/lite-python/dtpython.c +++ b/lite-python/dtpython.c @@ -1,168 +1,142 @@ -/**************************************************************************** - -MIT License - -Copyright (c) 2020 唐佐林 - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - -*****************************************************************************/ - -/* - ======================================================================================= - File: - dtpython.c - - Description: - The implementation of the interface for using Python in C code. - - Author: - Delphi Tang (唐佐林) - http://www.dt4sw.com/ - - Revision History: - V0.1.0 (Initial Version) - ======================================================================================= -*/ - - -#include -#include -#include -#include - -#include "py/compile.h" -#include "py/runtime.h" -#include "py/repl.h" -#include "py/gc.h" -#include "py/mperrno.h" -#include "lib/utils/pyexec.h" - -#if MICROPY_ENABLE_COMPILER -static void do_str(const char *src, mp_parse_input_kind_t input_kind) { - nlr_buf_t nlr; - if (nlr_push(&nlr) == 0) { - mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0); - qstr source_name = lex->source_name; - mp_parse_tree_t parse_tree = mp_parse(lex, input_kind); - mp_obj_t module_fun = mp_compile(&parse_tree, source_name, true); - mp_call_function_0(module_fun); - nlr_pop(); - } else { - // uncaught exception - mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val); - } -} - -static void do_file(const char *filename) { - FILE* fp = fopen(filename, "r"); - - if( fp != NULL ) - { - char* buf = NULL; - long len = 0; - - fseek(fp, 0, SEEK_END); - - len = ftell(fp); - - fseek(fp, 0, SEEK_SET); - - if( (buf = (char*)malloc(len + 1)) != NULL ) - { - len = fread(buf, 1, len, fp); - buf[len] = 0; - - do_str(buf, MP_PARSE_FILE_INPUT); - } - - free(buf); - } - - fclose(fp); -} -#endif - -static char *stack_top; -#if MICROPY_ENABLE_GC -static char heap[10240]; -#endif - -#if MICROPY_ENABLE_GC -void gc_collect(void) { - // WARNING: This gc_collect implementation doesn't try to get root - // pointers from CPU registers, and thus may function incorrectly. - void *dummy; - gc_collect_start(); - gc_collect_root(&dummy, ((mp_uint_t)stack_top - (mp_uint_t)&dummy) / sizeof(mp_uint_t)); - gc_collect_end(); - gc_dump_info(); -} -#endif - -void nlr_jump_fail(void *val) { - while (1) { - ; - } -} - -mp_lexer_t *mp_lexer_new_from_file(const char *filename) { - mp_raise_OSError(MP_ENOENT); -} - -mp_import_stat_t mp_import_stat(const char *path) { - return MP_IMPORT_STAT_NO_EXIST; -} - -mp_obj_t mp_builtin_open(size_t n_args, const mp_obj_t *args, mp_map_t *kwargs) { - return mp_const_none; -} - -MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open); - -void DTPython_Init() -{ - int stack_dummy; - stack_top = (char *)&stack_dummy; - - gc_init(heap, heap + sizeof(heap)); - - mp_init(); -} - -void DTPython_RunFile(const char* file) -{ - if(file != NULL) - { - do_file(file); - } -} - -void DTPython_RunCode(const char* statement) -{ - if( statement != NULL ) - { - do_str(statement, MP_PARSE_FILE_INPUT); - } -} - -void DTPython_Deinit() -{ - mp_deinit(); -} +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +#include "py/compile.h" +#include "py/runtime.h" +#include "py/repl.h" +#include "py/gc.h" +#include "py/mperrno.h" +#include "py/objstr.h" +#include "utils/pyexec.h" + +static char* g_stack_top = NULL; +static char g_heap[1024 * 25] = {0}; + +static void do_str(const char* src, mp_parse_input_kind_t input_kind) +{ + nlr_buf_t nlr = {0}; + + if( nlr_push(&nlr) == 0 ) + { + mp_lexer_t* lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0); + qstr source_name = lex->source_name; + mp_parse_tree_t parse_tree = mp_parse(lex, input_kind); + mp_obj_t module_fun = mp_compile(&parse_tree, source_name, true); + + mp_call_function_0(module_fun); + + m_free(lex); + + nlr_pop(); + } + else + { + mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val); + } +} + +static void do_file(const char* filename) +{ + nlr_buf_t nlr = {0}; + + if( nlr_push(&nlr) == 0 ) + { + mp_lexer_t* lex = mp_lexer_new_from_file(filename); + qstr source_name = lex->source_name; + mp_parse_tree_t parse_tree = mp_parse(lex, MP_PARSE_FILE_INPUT); + mp_obj_t module_fun = mp_compile(&parse_tree, source_name, false); + + mp_call_function_0(module_fun); + + m_free(lex); + + nlr_pop(); + } + else + { + mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val); + } +} + +void gc_collect(void) +{ + // WARNING: This gc_collect implementation doesn't try to get root + // pointers from CPU registers, and thus may function incorrectly. + void* dummy = NULL; + + gc_collect_start(); + gc_collect_root(&dummy, ((mp_uint_t)g_stack_top - (mp_uint_t)&dummy) / sizeof(mp_uint_t)); + gc_collect_end(); + gc_dump_info(); +} + +void nlr_jump_fail(void* val) +{ + while( 1 ); +} + +void DTPython_Init() +{ + static mp_obj_str_t mp_sys_dot_obj = {{&mp_type_str}, 0, 1, (const byte *)"."}; + int stack_dummy = 0; + mp_obj_list_t* sys_path = NULL; + + g_stack_top = (char*)&stack_dummy; + + gc_init(g_heap, g_heap + sizeof(g_heap)); + + + sys_path = &MP_STATE_VM(mp_sys_path_obj); + + mp_obj_list_init(sys_path, 1); + + sys_path->items[0] = &mp_sys_dot_obj; + + mp_init(); +} + +void DTPython_RunFile(const char* file) +{ + if(file != NULL) + { + do_file(file); + } +} + +void DTPython_RunCode(const char* statement) +{ + if( statement != NULL ) + { + do_str(statement, MP_PARSE_FILE_INPUT); + } +} + +void DTPython_Deinit() +{ + mp_deinit(); +} diff --git a/lite-python/demo/dtpython.h b/lite-python/dtpython.h similarity index 70% rename from lite-python/demo/dtpython.h rename to lite-python/dtpython.h index 6470bf5..a3d0677 100755 --- a/lite-python/demo/dtpython.h +++ b/lite-python/dtpython.h @@ -2,7 +2,9 @@ MIT License -Copyright (c) 2020 唐佐林 +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -24,27 +26,6 @@ SOFTWARE. *****************************************************************************/ -/* - ======================================================================================= - File: - dtpython.h - - Description: - This header file is for the interface of DTPython project which is an experiment to make HOS board - to support Python. DTPython is made from MicroPython, but is NOT MicroPython anymore, - because the basic purpose of DTPython is to supply another option to develop HOS device application. - - Author: - Delphi Tang (唐佐林) - http://www.dt4sw.com/ - - Revision History: - V0.0.1 (Initial Version) - ======================================================================================= - -*/ - - #ifndef DTPYTHON_H #define DTPYTHON_H diff --git a/lite-python/mpconfigport.h b/lite-python/mpconfigport.h index 97b8efd..e75daf4 100755 --- a/lite-python/mpconfigport.h +++ b/lite-python/mpconfigport.h @@ -6,7 +6,8 @@ // config option to 0. If you do this then you won't get a REPL prompt, but you // will still be able to execute pre-compiled scripts, compiled with mpy-cross. #define MICROPY_ENABLE_COMPILER (1) - +#define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ) +// #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT) #define MICROPY_QSTR_BYTES_IN_HASH (1) // #define MICROPY_QSTR_EXTRA_POOL mp_qstr_frozen_const_pool #define MICROPY_ALLOC_PATH_MAX (256) @@ -20,25 +21,25 @@ #define MICROPY_BUILTIN_METHOD_CHECK_SELF_ARG (0) #define MICROPY_PY_ASYNC_AWAIT (0) #define MICROPY_PY_ASSIGN_EXPR (0) -#define MICROPY_PY_BUILTINS_BYTEARRAY (0) -#define MICROPY_PY_BUILTINS_DICT_FROMKEYS (0) -#define MICROPY_PY_BUILTINS_ENUMERATE (0) +#define MICROPY_PY_BUILTINS_BYTEARRAY (1) +#define MICROPY_PY_BUILTINS_DICT_FROMKEYS (1) +#define MICROPY_PY_BUILTINS_ENUMERATE (1) #define MICROPY_PY_BUILTINS_FILTER (0) #define MICROPY_PY_BUILTINS_REVERSED (0) -#define MICROPY_PY_BUILTINS_SET (0) -#define MICROPY_PY_BUILTINS_SLICE (0) +#define MICROPY_PY_BUILTINS_SET (1) +#define MICROPY_PY_BUILTINS_SLICE (1) #define MICROPY_PY_BUILTINS_PROPERTY (0) -#define MICROPY_PY_BUILTINS_MIN_MAX (0) -#define MICROPY_PY_BUILTINS_STR_COUNT (0) -#define MICROPY_PY_BUILTINS_STR_OP_MODULO (0) +#define MICROPY_PY_BUILTINS_MIN_MAX (1) +#define MICROPY_PY_BUILTINS_STR_COUNT (1) +#define MICROPY_PY_BUILTINS_STR_OP_MODULO (1) #define MICROPY_PY___FILE__ (0) -#define MICROPY_PY_GC (0) -#define MICROPY_PY_ARRAY (0) -#define MICROPY_PY_ATTRTUPLE (0) -#define MICROPY_PY_COLLECTIONS (0) +#define MICROPY_PY_GC (1) +#define MICROPY_PY_ARRAY (1) +#define MICROPY_PY_ATTRTUPLE (1) +#define MICROPY_PY_COLLECTIONS (1) #define MICROPY_PY_IO (0) #define MICROPY_PY_STRUCT (0) -#define MICROPY_PY_SYS (0) +#define MICROPY_PY_SYS (1) #define MICROPY_MODULE_FROZEN_MPY (0) #define MICROPY_CPYTHON_COMPAT (0) #define MICROPY_MODULE_GETATTR (0) @@ -49,14 +50,27 @@ typedef intptr_t mp_int_t; // must be pointer size typedef uintptr_t mp_uint_t; // must be pointer size typedef long mp_off_t; -// extra built in names to add to the global namespace -#define MICROPY_PORT_BUILTINS \ - { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&mp_builtin_open_obj) }, +extern const struct _mp_obj_module_t mp_module_gpio; +extern const struct _mp_obj_module_t mp_module_i2c; +extern const struct _mp_obj_module_t mp_module_os; +extern const struct _mp_obj_module_t mp_module_adc; +extern const struct _mp_obj_module_t mp_module_uart; +extern const struct _mp_obj_module_t mp_module_spi; +extern const struct _mp_obj_module_t mp_module_pwm; +#define MICROPY_PORT_BUILTIN_MODULES \ + {MP_OBJ_NEW_QSTR(MP_QSTR_gpio), (mp_obj_t)&mp_module_gpio}, \ + {MP_OBJ_NEW_QSTR(MP_QSTR_i2c), (mp_obj_t)&mp_module_i2c}, \ + {MP_OBJ_NEW_QSTR(MP_QSTR_os), (mp_obj_t)&mp_module_os}, \ + {MP_OBJ_NEW_QSTR(MP_QSTR_adc), (mp_obj_t)&mp_module_adc}, \ + {MP_OBJ_NEW_QSTR(MP_QSTR_uart), (mp_obj_t)&mp_module_uart}, \ + {MP_OBJ_NEW_QSTR(MP_QSTR_spi), (mp_obj_t)&mp_module_spi}, \ + {MP_OBJ_NEW_QSTR(MP_QSTR_pwm), (mp_obj_t)&mp_module_pwm}, \ + // We need to provide a declaration/definition of alloca() #include -#define MICROPY_HW_BOARD_NAME "minimal" +#define MICROPY_HW_BOARD_NAME "oh-board" #define MICROPY_HW_MCU_NAME "unknown-cpu" #ifdef __linux__ diff --git a/lite-python/py/builtinimport.c b/lite-python/py/builtinimport.c index bdc82e7..406c742 100755 --- a/lite-python/py/builtinimport.c +++ b/lite-python/py/builtinimport.c @@ -48,6 +48,8 @@ #define PATH_SEP_CHAR '/' +#define printf(...) ((void)0) + bool mp_obj_is_package(mp_obj_t module) { mp_obj_t dest[2]; mp_load_method_maybe(module, MP_QSTR___path__, dest); @@ -63,6 +65,7 @@ STATIC mp_import_stat_t mp_import_stat_any(const char *path) { return st; } #endif + return mp_import_stat(path); } @@ -101,7 +104,7 @@ STATIC mp_import_stat_t find_file(const char *file_str, uint file_len, vstr_t *d size_t path_num; mp_obj_t *path_items; mp_obj_list_get(mp_sys_path, &path_num, &path_items); - + printf("Delphi: %s : %d : path_num = %u\n", __FUNCTION__, __LINE__, path_num); if (path_num != 0) { // go through each path looking for a directory or file for (size_t i = 0; i < path_num; i++) { @@ -113,6 +116,7 @@ STATIC mp_import_stat_t find_file(const char *file_str, uint file_len, vstr_t *d vstr_add_char(dest, PATH_SEP_CHAR); } vstr_add_strn(dest, file_str, file_len); + printf("Delphi: %s : %d : dest = %s\n", __FUNCTION__, __LINE__, vstr_null_terminated_str(dest)); mp_import_stat_t stat = stat_dir_or_file(dest); if (stat != MP_IMPORT_STAT_NO_EXIST) { return stat; @@ -183,7 +187,7 @@ STATIC void do_load(mp_obj_t module_obj, vstr_t *file) { #if MICROPY_MODULE_FROZEN || MICROPY_ENABLE_COMPILER || (MICROPY_PERSISTENT_CODE_LOAD && MICROPY_HAS_FILE_READER) char *file_str = vstr_null_terminated_str(file); #endif - + printf("Delphi: %s : %d : file_str = %s\n", __FUNCTION__, __LINE__, file_str); // If we support frozen modules (either as str or mpy) then try to find the // requested filename in the list of frozen module filenames. #if MICROPY_MODULE_FROZEN @@ -296,6 +300,11 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) { DEBUG_printf(", is_package: %d", is_pkg); DEBUG_printf("\n"); #endif + + printf("Current module/package: "); + mp_obj_print(this_name_q, PRINT_REPR); + printf(", is_package: %d", is_pkg); + printf("\n"); size_t this_name_l; const char *this_name = mp_obj_str_get_data(this_name_q, &this_name_l); @@ -326,7 +335,7 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) { qstr new_mod_q = qstr_from_strn(new_mod, new_mod_l); mp_local_free(new_mod); - DEBUG_printf("Resolved base name for relative import: '%s'\n", qstr_str(new_mod_q)); + printf("Resolved base name for relative import: '%s'\n", qstr_str(new_mod_q)); module_name = MP_OBJ_NEW_QSTR(new_mod_q); mod_str = qstr_str(new_mod_q); mod_len = new_mod_l; @@ -354,7 +363,7 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) { qstr pkg_name = qstr_from_strn(mod_str, p - mod_str); return mp_module_get(pkg_name); } - DEBUG_printf("Module not yet loaded\n"); + printf("Module not yet loaded\n"); uint last = 0; VSTR_FIXED(path, MICROPY_ALLOC_PATH_MAX) @@ -366,21 +375,22 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) { if (i == mod_len || mod_str[i] == '.') { // create a qstr for the module name up to this depth qstr mod_name = qstr_from_strn(mod_str, i); - DEBUG_printf("Processing module: %s\n", qstr_str(mod_name)); - DEBUG_printf("Previous path: =%.*s=\n", vstr_len(&path), vstr_str(&path)); + printf("Processing module: %s\n", qstr_str(mod_name)); + printf("Previous path: =%u.%s=\n", vstr_len(&path), vstr_str(&path)); // find the file corresponding to the module name mp_import_stat_t stat; if (vstr_len(&path) == 0) { // first module in the dotted-name; search for a directory or file stat = find_file(mod_str, i, &path); + printf("Delphi: %s : %d : stat = %d\n", __FUNCTION__, __LINE__, stat); } else { // latter module in the dotted-name; append to path vstr_add_char(&path, PATH_SEP_CHAR); vstr_add_strn(&path, mod_str + last, i - last); stat = stat_dir_or_file(&path); } - DEBUG_printf("Current path: %.*s\n", vstr_len(&path), vstr_str(&path)); + printf("Current path: %u.%s\n", vstr_len(&path), vstr_str(&path)); if (stat == MP_IMPORT_STAT_NO_EXIST) { module_obj = MP_OBJ_NULL; @@ -432,7 +442,7 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) { } if (stat == MP_IMPORT_STAT_DIR) { - DEBUG_printf("%.*s is dir\n", vstr_len(&path), vstr_str(&path)); + printf("%u.*%s is dir\n", vstr_len(&path), vstr_str(&path)); // https://docs.python.org/3/reference/import.html // "Specifically, any module that contains a __path__ attribute is considered a package." mp_store_attr(module_obj, MP_QSTR___path__, mp_obj_new_str(vstr_str(&path), vstr_len(&path))); @@ -446,6 +456,7 @@ mp_obj_t mp_builtin___import__(size_t n_args, const mp_obj_t *args) { } path.len = orig_path_len; } else { // MP_IMPORT_STAT_FILE + printf("Delphi: %s : %d : path = %s\n", __FUNCTION__, __LINE__, vstr_str(&path)); do_load(module_obj, &path); // This should be the last component in the import path. If there are // remaining components then it's an ImportError because the current path diff --git a/lite-python/py/lexer.c b/lite-python/py/lexer.c index 7d2a251..6b71745 100755 --- a/lite-python/py/lexer.c +++ b/lite-python/py/lexer.c @@ -735,6 +735,7 @@ mp_lexer_t *mp_lexer_new_from_str_len(qstr src_name, const char *str, size_t len mp_lexer_t *mp_lexer_new_from_file(const char *filename) { mp_reader_t reader; + printf("Delphi: %s : %d : filename = %s\n", __FUNCTION__, __LINE__, filename); mp_reader_new_file(&reader, filename); return mp_lexer_new(qstr_from_str(filename), reader); } diff --git a/lite-python/py/mpconfig.h b/lite-python/py/mpconfig.h index deb074b..b81191c 100755 --- a/lite-python/py/mpconfig.h +++ b/lite-python/py/mpconfig.h @@ -839,7 +839,7 @@ typedef double mp_float_t; // This costs some code size and makes store/delete of instance // attributes slower for the classes that use this feature #ifndef MICROPY_PY_DELATTR_SETATTR -#define MICROPY_PY_DELATTR_SETATTR (0) +#define MICROPY_PY_DELATTR_SETATTR (1) #endif // Support for async/await/async for/async with -- Gitee From d939a446c433cf8357734f69286bebc37e5c29ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=90=E4=BD=90=E6=9E=97?= Date: Thu, 30 Sep 2021 01:55:21 +0000 Subject: [PATCH 2/2] update lite-python/mpconfigport.h. --- lite-python/mpconfigport.h | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/lite-python/mpconfigport.h b/lite-python/mpconfigport.h index e75daf4..fa40dc3 100755 --- a/lite-python/mpconfigport.h +++ b/lite-python/mpconfigport.h @@ -1,10 +1,33 @@ -#include +/**************************************************************************** + +MIT License + +Copyright (c) 2021 唐佐林 +WeChat : delphi_tang +EMail: delphi_tang@dt4sw.com + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: -// options to control how MicroPython is built +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +*****************************************************************************/ + +#include -// You can disable the built-in MicroPython compiler by setting the following -// config option to 0. If you do this then you won't get a REPL prompt, but you -// will still be able to execute pre-compiled scripts, compiled with mpy-cross. #define MICROPY_ENABLE_COMPILER (1) #define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_MPZ) // #define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT) -- Gitee