diff --git a/README.md b/README.md index 3932e000..9b313bb7 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # evm3_publish +> 后端框架标准参照IoT.js: https://github.com/jerryscript-project/iotjs/blob/master/docs/api/IoT.js-API-reference.md \ No newline at end of file diff --git a/bsp/bouffalolab/app/main.c b/bsp/bouffalolab/app/main.c index 74787996..e138bd1e 100644 --- a/bsp/bouffalolab/app/main.c +++ b/bsp/bouffalolab/app/main.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -635,6 +636,8 @@ static void aos_loop_proc(void *pvParameters) aos_register_event_filter(EV_WIFI, event_cb_wifi_event, NULL); cmd_stack_wifi(NULL, 0, 0, NULL); + hal_hwtimer_init(); + aos_loop_run(); puts("------------------------------------------\r\n"); @@ -670,7 +673,6 @@ void bfl_main(void) * and baudrate of 2M */ bl_uart_init(0, 16, 7, 255, 255, 2 * 1000 * 1000); - bl_uart_init(1, 4, 3, 255, 255, 115200); puts("Starting bl602 now....\r\n"); _dump_boot_info(); diff --git a/bsp/bouffalolab/makefile b/bsp/bouffalolab/makefile index 2ea33e88..9c0c5a87 100644 --- a/bsp/bouffalolab/makefile +++ b/bsp/bouffalolab/makefile @@ -23,6 +23,9 @@ CFLAGS += -DCONFIG_EVM_MODULE_PROCESS CFLAGS += -DCONFIG_EVM_MODULE_NET CFLAGS += -DCONFIG_EVM_MODULE_DNS CFLAGS += -DCONFIG_EVM_MODULE_BUFFER +CFLAGS += -DCONFIG_EVM_MODULE_TIMERS +CFLAGS += -DCONFIG_EVM_MODULE_GPIO +CFLAGS += -DCONFIG_EVM_MODULE_UART CFLAGS += -DEVM_LANG_ENABLE_REPL CFLAGS += -DEVM_LANG_ENABLE_JAVASCRIPT diff --git a/modules/iotjs/bouffalo.mk b/modules/iotjs/bouffalo.mk index 1ced7c15..75494786 100755 --- a/modules/iotjs/bouffalo.mk +++ b/modules/iotjs/bouffalo.mk @@ -49,7 +49,10 @@ COMPONENT_SRCS += \ common/evm_module_buffer.c \ bouffalolab/evm_main.c \ bouffalolab/evm_module_dns.c \ - bouffalolab/evm_module_net.c + bouffalolab/evm_module_net.c \ + bouffalolab/evm_module_timers.c \ + bouffalolab/evm_module_gpio.c \ + bouffalolab/evm_module_uart.c COMPONENT_OBJS := $(patsubst %.c,%.o, $(COMPONENT_SRCS)) diff --git a/modules/iotjs/bouffalolab/evm_module_gpio.c b/modules/iotjs/bouffalolab/evm_module_gpio.c new file mode 100644 index 00000000..43a86e8a --- /dev/null +++ b/modules/iotjs/bouffalolab/evm_module_gpio.c @@ -0,0 +1,292 @@ +#ifdef CONFIG_EVM_MODULE_GPIO +#include "evm_module.h" +#include "bl_gpio.h" +#include + +evm_val_t *evm_module_gpio_class_instantiate(evm_t *e, evm_val_t *p, int argc, evm_val_t *v); + +typedef struct _gpio_dev_t +{ + int pin; + int direction; + int mode; + int edge; +} _gpio_dev_t; + +static evm_val_t _gpio_open_device(evm_t *e, evm_val_t *p, int argc, evm_val_t *v, int is_sync) +{ + evm_val_t *val = NULL; + evm_val_t *ret_obj; + evm_val_t *cb = NULL; + evm_val_t args; + _gpio_dev_t *dev; + + if (argc < 1) + return EVM_VAL_UNDEFINED; + + if (argc > 1 && evm_is_script(v + 1) && !is_sync) + { + cb = v + 1; + } + + dev = evm_malloc(sizeof(_gpio_dev_t)); + if (!dev) + { + args = evm_mk_foreign_string("Insufficient external memory"); + if (cb) + evm_run_callback(e, cb, NULL, &args, 1); + evm_set_err(e, ec_memory, "Insufficient external memory"); + return EVM_VAL_UNDEFINED; + } + + val = evm_prop_get(e, v, "pin", 0); + if (val == NULL || !evm_is_integer(val)) + { + args = evm_mk_foreign_string("Configuration has no 'pin' member"); + if (cb) + evm_run_callback(e, cb, NULL, &args, 1); + evm_free(dev); + evm_set_err(e, ec_type, "Configuration has no 'pin' member"); + return EVM_VAL_UNDEFINED; + } + dev->pin = evm_2_integer(val); + + val = evm_prop_get(e, v, "direction", 0); + if (val == NULL || !evm_is_integer(val)) + { + args = evm_mk_foreign_string("Configuration has no 'direction' member"); + if (cb) + evm_run_callback(e, cb, NULL, &args, 1); + evm_free(dev); + evm_set_err(e, ec_type, "Configuration has no 'direction' member"); + return EVM_VAL_UNDEFINED; + } + dev->direction = evm_2_integer(val); + + val = evm_prop_get(e, v, "mode", 0); + if (val == NULL || !evm_is_integer(val)) + { + args = evm_mk_foreign_string("Configuration has no 'mode' member"); + if (cb) + evm_run_callback(e, cb, NULL, &args, 1); + evm_free(dev); + evm_set_err(e, ec_type, "Configuration has no 'mode' member"); + return EVM_VAL_UNDEFINED; + } + dev->mode = evm_2_integer(val); + + if (dev->mode) + { + bl_gpio_enable_input(dev->pin, dev->direction ? 1 : 0, dev->direction ? 0 : 1); + } + else + { + bl_gpio_enable_output(dev->pin, dev->direction ? 1 : 0, dev->direction ? 0 : 1); + } + + ret_obj = evm_module_gpio_class_instantiate(e, p, argc, v); + if (ret_obj == NULL) + { + args = evm_mk_foreign_string("Failed to instantiate"); + if (cb) + evm_run_callback(e, cb, NULL, &args, 1); + evm_free(dev); + return EVM_VAL_UNDEFINED; + } + + evm_object_set_ext_data(ret_obj, (intptr_t)dev); + return *ret_obj; +} + +//gpio.open(configuration, callback) +static evm_val_t evm_module_gpio_open(evm_t *e, evm_val_t *p, int argc, evm_val_t *v) +{ + return _gpio_open_device(e, p, argc, v, 0); +} + +//gpio.openSync(configuration) +static evm_val_t evm_module_gpio_openSync(evm_t *e, evm_val_t *p, int argc, evm_val_t *v) +{ + return _gpio_open_device(e, p, argc, v, 1); +} + +//gpiopin.setDirectionSync(direction) +static evm_val_t evm_module_gpio_class_setDirectionSync(evm_t *e, evm_val_t *p, int argc, evm_val_t *v) +{ + _gpio_dev_t *dev = (_gpio_dev_t *)evm_object_get_ext_data(p); + evm_val_t args[2]; + if (!dev) + return EVM_VAL_UNDEFINED; + + if (argc < 1 || !evm_is_integer(v)) + return EVM_VAL_UNDEFINED; + + dev->direction = evm_2_integer(v); + return EVM_VAL_UNDEFINED; +} + +//gpiopin.write(value[, callback]) +static evm_val_t evm_module_gpio_class_write(evm_t *e, evm_val_t *p, int argc, evm_val_t *v) +{ + _gpio_dev_t *dev = (_gpio_dev_t *)evm_object_get_ext_data(p); + + if (!dev) + return EVM_VAL_UNDEFINED; + if (argc < 1 || !evm_is_integer(v)) + return EVM_VAL_UNDEFINED; + + bl_gpio_output_set(dev->pin, evm_2_integer(v) ? 1 : 0); + + evm_val_t args; + if (argc > 1 && evm_is_script(v + 1)) + { + args = evm_mk_null(); + evm_run_callback(e, v + 1, NULL, &args, 1); + } + return EVM_VAL_UNDEFINED; +} + +//gpiopin.writeSync(value) +static evm_val_t evm_module_gpio_class_writeSync(evm_t *e, evm_val_t *p, int argc, evm_val_t *v) +{ + _gpio_dev_t *dev = (_gpio_dev_t *)evm_object_get_ext_data(p); + + if (!dev) + return EVM_VAL_UNDEFINED; + if (argc < 1 || !evm_is_integer(v)) + return EVM_VAL_UNDEFINED; + + bl_gpio_output_set(dev->pin, evm_2_integer(v) ? 1 : 0); + + return EVM_VAL_UNDEFINED; +} + +//gpiopin.read([callback]) +static evm_val_t evm_module_gpio_class_read(evm_t *e, evm_val_t *p, int argc, evm_val_t *v) +{ + _gpio_dev_t *dev = (_gpio_dev_t *)evm_object_get_ext_data(p); + if (!dev) + return EVM_VAL_UNDEFINED; + + uint8_t status; + int ret = bl_gpio_input_get(dev->pin, &status); + if (ret != 0) + return EVM_VAL_UNDEFINED; + + evm_val_t args[2]; + if (argc > 0 && evm_is_script(v)) + { + args[0] = evm_mk_null(); + if (status) + { + args[1] = EVM_VAL_TRUE; + } + else + { + args[1] = EVM_VAL_FALSE; + } + evm_run_callback(e, v, NULL, args, 2); + } + return EVM_VAL_UNDEFINED; +} + +//gpiopin.readSync() +static evm_val_t evm_module_gpio_class_readSync(evm_t *e, evm_val_t *p, int argc, evm_val_t *v) +{ + _gpio_dev_t *dev = (_gpio_dev_t *)evm_object_get_ext_data(p); + int status; + if (!dev) + return EVM_VAL_UNDEFINED; + + status = bl_gpio_input_get_value(dev->pin); + if (status) + { + return EVM_VAL_TRUE; + } + return EVM_VAL_FALSE; +} + +//gpiopin.close([callback]) +static evm_val_t evm_module_gpio_class_close(evm_t *e, evm_val_t *p, int argc, evm_val_t *v) +{ + return EVM_VAL_UNDEFINED; +} + +//gpiopin.closeSync() +static evm_val_t evm_module_gpio_class_closeSync(evm_t *e, evm_val_t *p, int argc, evm_val_t *v) +{ + return EVM_VAL_UNDEFINED; +} + +evm_val_t *evm_module_gpio_class_instantiate(evm_t *e, evm_val_t *p, int argc, evm_val_t *v) +{ + evm_val_t *obj = evm_object_create(e, GC_OBJECT, 7, 0); + evm_val_t *prop = NULL; + if (obj) + { + evm_prop_append(e, obj, "setDirectionSync", evm_mk_native((intptr_t)evm_module_gpio_class_setDirectionSync)); + evm_prop_append(e, obj, "write", evm_mk_native((intptr_t)evm_module_gpio_class_write)); + evm_prop_append(e, obj, "writeSync", evm_mk_native((intptr_t)evm_module_gpio_class_writeSync)); + evm_prop_append(e, obj, "read", evm_mk_native((intptr_t)evm_module_gpio_class_read)); + evm_prop_append(e, obj, "readSync", evm_mk_native((intptr_t)evm_module_gpio_class_readSync)); + evm_prop_append(e, obj, "close", evm_mk_native((intptr_t)evm_module_gpio_class_close)); + evm_prop_append(e, obj, "closeSync", evm_mk_native((intptr_t)evm_module_gpio_class_closeSync)); + } + return obj; +} + +evm_err_t evm_module_gpio(evm_t *e) +{ + evm_val_t *dir_prop = evm_object_create(e, GC_OBJECT, 2, 0); + if (dir_prop) + { + evm_prop_append(e, dir_prop, "IN", evm_mk_number(EVM_GPIO_DIRECTION_IN)); + evm_prop_append(e, dir_prop, "OUT", evm_mk_number(EVM_GPIO_DIRECTION_OUT)); + } + else + { + return e->err; + } + + evm_val_t *mode_prop = evm_object_create(e, GC_OBJECT, 6, 0); + if (mode_prop) + { + evm_prop_append(e, mode_prop, "NONE", evm_mk_number(EVM_GPIO_MODE_NONE)); + evm_prop_append(e, mode_prop, "PULLUP", evm_mk_number(EVM_GPIO_MODE_PULLUP)); + evm_prop_append(e, mode_prop, "PULLDOWN", evm_mk_number(EVM_GPIO_MODE_PULLDOWN)); + evm_prop_append(e, mode_prop, "FLOAT", evm_mk_number(EVM_GPIO_MODE_FLOAT)); + evm_prop_append(e, mode_prop, "PUSHPULL", evm_mk_number(EVM_GPIO_MODE_PUSHPULL)); + evm_prop_append(e, mode_prop, "OPENDRAIN", evm_mk_number(EVM_GPIO_MODE_OPENDRAIN)); + } + else + { + return e->err; + } + + evm_val_t *edge_prop = evm_object_create(e, GC_OBJECT, 4, 0); + if (edge_prop) + { + evm_prop_append(e, edge_prop, "NONE", evm_mk_number(EVM_GPIO_EDGE_NONE)); + evm_prop_append(e, edge_prop, "RISING", evm_mk_number(EVM_GPIO_EDGE_RISING)); + evm_prop_append(e, edge_prop, "FALLING", evm_mk_number(EVM_GPIO_EDGE_FALLING)); + evm_prop_append(e, edge_prop, "BOTH", evm_mk_number(EVM_GPIO_EDGE_BOTH)); + } + else + { + return e->err; + } + + evm_builtin_t builtin[] = { + {"open", evm_mk_native((intptr_t)evm_module_gpio_open)}, + {"openSync", evm_mk_native((intptr_t)evm_module_gpio_openSync)}, + {"DIRECTION", *dir_prop}, + {"MODE", *mode_prop}, + {"EDGE", *edge_prop}, + {NULL, NULL}}; + evm_module_create(e, "gpio", builtin); + evm_pop(e); + evm_pop(e); + evm_pop(e); + return e->err; +} +#endif diff --git a/modules/iotjs/bouffalolab/evm_module_timers.c b/modules/iotjs/bouffalolab/evm_module_timers.c index eaae8a2a..03e2eca7 100644 --- a/modules/iotjs/bouffalolab/evm_module_timers.c +++ b/modules/iotjs/bouffalolab/evm_module_timers.c @@ -1,69 +1,24 @@ #ifdef CONFIG_EVM_MODULE_TIMERS #include "evm_module.h" -#include -#include -#include +#include static evm_t *timer_e; -static void callback_handler(union sigval v) +typedef struct _bl_timer_t { - evm_val_t *callback = evm_module_registry_get(timer_e, v.sival_int); + hw_timer_t *handle; + int id; +} _bl_timer_t; + +static void callback_handler(void) +{ + evm_val_t *callback = evm_module_registry_get(timer_e, 0); // id if (callback == NULL) return; evm_val_t args = *callback; evm_module_next_tick(timer_e, 1, &args); } -static timer_t timer(int id, int delay, int once) -{ - timer_t timerid; - int ret; - - struct sigevent sev; - - // handle in thread when timeout - memset(&sev, 0, sizeof(struct sigevent)); - sev.sigev_value.sival_ptr = &timerid; - sev.sigev_notify = SIGEV_THREAD; - sev.sigev_notify_function = callback_handler; - sev.sigev_value.sival_int = id; - - struct itimerspec its; // duration settings - - ret = timer_create(CLOCK_REALTIME, &sev, &timerid); - if (ret == -1) - { - return NULL; - } - - // set timeout, only once - // it_value the first timeout duration - // it_interval the next timeout duration - if (delay >= 1000) - { - its.it_value.tv_sec = delay / 1000; - its.it_value.tv_nsec = (delay % 1000) * 1000000; - } - else - { - its.it_value.tv_sec = 0; - its.it_value.tv_nsec = delay * 1000000; - } - if( !once ) { - its.it_interval.tv_sec = its.it_value.tv_sec; - its.it_interval.tv_nsec = its.it_value.tv_nsec; - } - - ret = timer_settime(timerid, TIMER_ABSTIME, &its, NULL); - if (ret == -1) - { - return NULL; - } - - return timerid; -} - //setTimeout(callback, delay[, args..]) static evm_val_t evm_module_timers_setTimeout(evm_t *e, evm_val_t *p, int argc, evm_val_t *v) { @@ -75,10 +30,16 @@ static evm_val_t evm_module_timers_setTimeout(evm_t *e, evm_val_t *p, int argc, int id = evm_module_registry_add(e, v); if (id < 0) return EVM_VAL_UNDEFINED; - timer_t timerid = timer(id, evm_2_integer(v + 1), 1); - if (timerid == NULL) + + _bl_timer_t *timer = evm_malloc(sizeof(_bl_timer_t)); + + timer->handle = hal_hwtimer_create(evm_2_integer(v + 1), callback_handler, 0); + timer->id = id; + + if (timer->handle == NULL) return EVM_VAL_UNDEFINED; - evm_object_set_ext_data(v, (intptr_t)timerid); + + evm_object_set_ext_data(v, (intptr_t)timer); return EVM_VAL_UNDEFINED; } @@ -89,15 +50,26 @@ static evm_val_t evm_module_timers_clearTimeout(evm_t *e, evm_val_t *p, int argc { return EVM_VAL_UNDEFINED; } + evm_val_t *callback = evm_module_registry_get(e, evm_2_integer(v)); if (callback == NULL) return EVM_VAL_UNDEFINED; - timer_t timerid = evm_object_get_ext_data(callback); - if (timerid == NULL) + + _bl_timer_t *timer = (_bl_timer_t *)evm_object_get_ext_data(callback); + if (timer->id < 0) return EVM_VAL_UNDEFINED; - timer_delete(timerid); + + int ret = hal_hwtimer_delete(timer->handle); + evm_module_registry_remove(e, evm_2_integer(v)); - return EVM_VAL_UNDEFINED; + if (ret == 0) + { + return EVM_VAL_TRUE; + } + else + { + return EVM_VAL_FALSE; + } } //setInterval(callback, delay[, args..]) @@ -107,14 +79,20 @@ static evm_val_t evm_module_timers_setInterval(evm_t *e, evm_val_t *p, int argc, { return EVM_VAL_UNDEFINED; } + int id = evm_module_registry_add(e, v); if (id < 0) return EVM_VAL_UNDEFINED; - timer_t timerid = timer(id, evm_2_integer(v + 1), 0); - if (timerid == NULL) + + _bl_timer_t *timer = evm_malloc(sizeof(_bl_timer_t)); + timer->handle = hal_hwtimer_create(evm_2_integer(v + 1), callback_handler, 1); + timer->id = id; + + if (timer->handle == NULL) return EVM_VAL_UNDEFINED; - evm_object_set_ext_data(v, (intptr_t)timerid); - return evm_mk_number(id); + + evm_object_set_ext_data(v, (intptr_t)timer); + return EVM_VAL_UNDEFINED; } //clearInterval(timeout) @@ -124,11 +102,30 @@ static evm_val_t evm_module_timers_clearInterval(evm_t *e, evm_val_t *p, int arg { return EVM_VAL_UNDEFINED; } - evm_module_timers_clearTimeout(e, p, argc, v); - return EVM_VAL_UNDEFINED; + + evm_val_t *callback = evm_module_registry_get(e, evm_2_integer(v)); + if (callback == NULL) + return EVM_VAL_UNDEFINED; + + _bl_timer_t *timer = (_bl_timer_t *)evm_object_get_ext_data(callback); + if (timer->id < 0) + return EVM_VAL_UNDEFINED; + + int ret = hal_hwtimer_delete(timer->handle); + + evm_module_registry_remove(e, evm_2_integer(v)); + if (ret == 0) + { + return EVM_VAL_TRUE; + } + else + { + return EVM_VAL_FALSE; + } } -evm_err_t evm_module_timers(evm_t *e) { +evm_err_t evm_module_timers(evm_t *e) +{ timer_e = e; evm_builtin_t builtin[] = { {"setTimeout", evm_mk_native((intptr_t)evm_module_timers_setTimeout)}, diff --git a/modules/iotjs/bouffalolab/evm_module_uart.c b/modules/iotjs/bouffalolab/evm_module_uart.c index b2b32148..f334da86 100644 --- a/modules/iotjs/bouffalolab/evm_module_uart.c +++ b/modules/iotjs/bouffalolab/evm_module_uart.c @@ -1,26 +1,96 @@ #ifdef CONFIG_EVM_MODULE_UART #include "evm_module.h" +#include +#include +#include +#include +#include -#include -#include +#define _UART_READ_BUF_SIZE 512 typedef struct _uart_dev_t { - rt_device_t dev; + char *dev; int baudrate; int databits; + int fd; + int obj_id; + uint8_t buffer[_UART_READ_BUF_SIZE]; } _uart_dev_t; evm_val_t *evm_module_uart_class_instantiate(evm_t *e); +static int get_dts_addr(const char *name, uint32_t *start, uint32_t *off) +{ + uint32_t addr = hal_board_get_factory_addr(); + const void *fdt = (const void *)addr; + uint32_t offset; + + if (!name || !start || !off) + { + return -1; + } + + offset = fdt_subnode_offset(fdt, 0, name); + if (offset <= 0) + { + return -1; + } + + *start = (uint32_t)fdt; + *off = offset; + + return 0; +} + +static evm_val_t evm_module_uart_on(evm_t *e, evm_val_t *p, int argc, evm_val_t *v) +{ + _uart_dev_t *uart = (_uart_dev_t *)evm_object_get_ext_data(p); + if (!uart) + return EVM_VAL_UNDEFINED; + + evm_module_event_add_listener(e, p, evm_2_string(v), v + 1); + return EVM_VAL_UNDEFINED; +} + +static void _uart_thread(_uart_dev_t *uart) +{ + int bytes_read; + while (1) + { + bytes_read = aos_read(uart->fd, uart->buffer, _UART_READ_BUF_SIZE); + if (bytes_read > 0 && bytes_read < _UART_READ_BUF_SIZE) + { + evm_val_t *obj = evm_module_registry_get(evm_runtime, uart->obj_id); + if (obj) + { + evm_val_t *args = evm_buffer_create(evm_runtime, bytes_read); + if (args) + { + memcpy(evm_buffer_addr(args), uart->buffer, bytes_read); + evm_module_event_emit(evm_runtime, obj, "data", 1, args); + evm_pop(evm_runtime); + } + } + } + else if (bytes_read < 0) + { + close(uart->fd); + break; + } + vTaskDelay(10); + } + evm_free(uart); +} + static evm_val_t _uart_open_device(evm_t *e, evm_val_t *p, int argc, evm_val_t *v, int is_sync) { - evm_val_t *val = NULL; + evm_val_t *val = NULL; evm_val_t *ret_obj; evm_val_t *cb = NULL; evm_val_t args; _uart_dev_t *dev; - if( argc < 1) - return EVM_VAL_UNDEFINED; + if (argc < 1) + return EVM_VAL_UNDEFINED; if( argc > 1 && evm_is_script(v + 1) && !is_sync ) { cb = v + 1; @@ -44,18 +114,9 @@ static evm_val_t _uart_open_device(evm_t *e, evm_val_t *p, int argc, evm_val_t * evm_set_err(e, ec_type, "Configuration has no 'device' member"); return EVM_VAL_UNDEFINED; } - - dev->dev = rt_device_find(evm_2_string(val)); - if ( dev->dev == RT_NULL ) { - args = evm_mk_foreign_string("Can't find uart device"); - if( cb ) - evm_run_callback(e, cb, NULL, &args, 1); - evm_free(dev); - evm_set_err(e, ec_type, "Can't find uart device"); - return EVM_VAL_UNDEFINED; - } + dev->dev = evm_2_string(val); - val = evm_prop_get(e, v, "baudRate", 0); + val = evm_prop_get(e, v, "baudRate", 0); if( val == NULL || !evm_is_integer(val) ) { args = evm_mk_foreign_string("Configuration has no 'baudRate' member"); if( cb ) @@ -77,28 +138,37 @@ static evm_val_t _uart_open_device(evm_t *e, evm_val_t *p, int argc, evm_val_t * } dev->databits = evm_2_integer(val); - struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; - config.baud_rate = dev->baudrate; - config.data_bits = dev->databits; - config.bufsz = 512; - rt_device_control(dev->dev, RT_DEVICE_CTRL_CONFIG, &config); - rt_err_t re = rt_device_open(dev->dev, RT_DEVICE_FLAG_INT_RX); - if(re != RT_EOK) { + // id tx_pin rx_pin cts_pin rts_pin baudrate + bl_uart_init(1, 4, 3, 255, 255, dev->baudrate); + + uint32_t fdt = 0, offset = 0; + /* uart */ + if (0 == get_dts_addr("uart", &fdt, &offset)) + { + vfs_uart_init(fdt, offset); + } + + dev->fd = aos_open(dev->dev, 0); + if (dev->fd < 0) + { evm_free(dev); evm_set_err(e, ec_type, "Failed to open uart"); return EVM_VAL_UNDEFINED; } + xTaskCreate(_uart_thread, "uart-task", 100, dev, 13, NULL); + ret_obj = evm_module_uart_class_instantiate(e); - if( ret_obj == NULL ) { - args = evm_mk_foreign_string("Failed to instantiate"); + if (ret_obj == NULL) + { + args = evm_mk_foreign_string("Failed to instantiate"); if( cb ) evm_run_callback(e, cb, NULL, &args, 1); evm_free(dev); return EVM_VAL_UNDEFINED; - } + } - evm_object_set_ext_data(ret_obj, (intptr_t)dev); + evm_object_set_ext_data(ret_obj, (intptr_t)dev); return *ret_obj; } @@ -114,7 +184,8 @@ static evm_val_t evm_module_uart_openSync(evm_t *e, evm_val_t *p, int argc, evm_ return _uart_open_device(e, p, argc, v, 1); } -static rt_err_t _uart_class_write(evm_t *e, evm_val_t *p, int argc, evm_val_t *v) { +static ssize_t _uart_class_write(evm_t *e, evm_val_t *p, int argc, evm_val_t *v) +{ _uart_dev_t *dev = (_uart_dev_t*)evm_object_get_ext_data(p); if( !dev ) return EVM_VAL_UNDEFINED; @@ -124,7 +195,7 @@ static rt_err_t _uart_class_write(evm_t *e, evm_val_t *p, int argc, evm_val_t *v } void *buffer; - rt_size_t size; + uint32_t size; if( evm_is_string(v) ) { buffer = evm_2_string(v); @@ -133,15 +204,16 @@ static rt_err_t _uart_class_write(evm_t *e, evm_val_t *p, int argc, evm_val_t *v buffer = evm_buffer_addr(v); size = evm_buffer_len(v); } - return rt_device_write(dev->dev, 0, buffer, size); + + return aos_write(dev->fd, buffer, size); } //uartport.write(data, callback) static evm_val_t evm_module_uart_class_write(evm_t *e, evm_val_t *p, int argc, evm_val_t *v) { - rt_err_t re = _uart_class_write(e, p, argc, v); + ssize_t ret = _uart_class_write(e, p, argc, v); if( argc > 1 && evm_is_script(v + 1) ) { - evm_val_t args = evm_mk_number(re); + evm_val_t args = evm_mk_number(ret); evm_run_callback(e, v + 1, NULL, &args, 1); } return EVM_VAL_UNDEFINED; @@ -154,20 +226,25 @@ static evm_val_t evm_module_uart_class_writeSync(evm_t *e, evm_val_t *p, int arg return EVM_VAL_UNDEFINED; } -static rt_err_t _uart_class_close(evm_t *e, evm_val_t *p, int argc, evm_val_t *v) { +static int _uart_class_close(evm_t *e, evm_val_t *p, int argc, evm_val_t *v) +{ _uart_dev_t *dev = (_uart_dev_t*)evm_object_get_ext_data(p); - if( !dev ) - return EVM_VAL_UNDEFINED; - return rt_device_close(dev->dev); + if( !dev ) + return -1; + + int ret = aos_close(dev->fd); + evm_free(dev); + return ret; } //uartport.close([callback]) static evm_val_t evm_module_uart_class_close(evm_t *e, evm_val_t *p, int argc, evm_val_t *v) { - rt_err_t re = _uart_class_close(e, p, argc, v); - if( argc > 1 && evm_is_script(v + 1) ) { - evm_val_t args = evm_mk_number(re); - evm_run_callback(e, v + 1, NULL, &args, 1); + int ret = _uart_class_close(e, p, argc, v); + if (argc > 0 && evm_is_script(v)) + { + evm_val_t args = evm_mk_number(ret); + evm_run_callback(e, v, NULL, &args, 1); } return EVM_VAL_UNDEFINED; } @@ -187,17 +264,17 @@ evm_val_t *evm_module_uart_class_instantiate(evm_t *e) evm_prop_append(e, obj, "writeSync", evm_mk_native((intptr_t)evm_module_uart_class_writeSync)); evm_prop_append(e, obj, "close", evm_mk_native((intptr_t)evm_module_uart_class_close)); evm_prop_append(e, obj, "closeSync", evm_mk_native((intptr_t)evm_module_uart_class_closeSync)); - } + evm_prop_append(e, obj, "on", evm_mk_native((intptr_t)evm_module_uart_on)); + } return obj; } evm_err_t evm_module_uart(evm_t *e) { - evm_builtin_t builtin[] = { - {"open", evm_mk_native((intptr_t)evm_module_uart_open)}, - {"openSync", evm_mk_native((intptr_t)evm_module_uart_openSync)}, - {NULL, NULL} - }; - evm_module_create(e, "uart", builtin); + evm_builtin_t builtin[] = { + {"open", evm_mk_native((intptr_t)evm_module_uart_open)}, + {"openSync", evm_mk_native((intptr_t)evm_module_uart_openSync)}, + {NULL, NULL}}; + evm_module_create(e, "uart", builtin); return e->err; } #endif diff --git a/modules/iotjs/linux/evm_module_gpio.c b/modules/iotjs/linux/evm_module_gpio.c index ef09c1df..48103811 100644 --- a/modules/iotjs/linux/evm_module_gpio.c +++ b/modules/iotjs/linux/evm_module_gpio.c @@ -13,8 +13,6 @@ typedef struct _gpio_dev_t { int edge; } _gpio_dev_t; -evm_val_t *evm_module_uart_class_instantiate(evm_t *e); - static void _gpio_set_mode(_gpio_dev_t *dev) { int mode = PIN_MODE_INPUT; switch( dev->direction) { @@ -104,7 +102,7 @@ static evm_val_t _gpio_open_device(evm_t *e, evm_val_t *p, int argc, evm_val_t * _gpio_set_mode(dev); rt_pin_mode(dev->pin, dev->rt_mode); - ret_obj = evm_module_uart_class_instantiate(e); + ret_obj = evm_module_gpio_class_instantiate(e); if( ret_obj == NULL ) { args = evm_mk_foreign_string("Failed to instantiate"); if( cb ) diff --git a/modules/iotjs/linux/evm_module_net.c b/modules/iotjs/linux/evm_module_net.c index ef4f97bc..6ab67ad9 100644 --- a/modules/iotjs/linux/evm_module_net.c +++ b/modules/iotjs/linux/evm_module_net.c @@ -56,8 +56,6 @@ static void _net_client_thread(_net_sock_t *client_sock) { evm_free(client_sock); } - - //server.close([closeListener]) static evm_val_t evm_module_net_server_close(evm_t *e, evm_val_t *p, int argc, evm_val_t *v) { diff --git a/modules/iotjs/linux/evm_module_timers.c b/modules/iotjs/linux/evm_module_timers.c index eaae8a2a..d7d10b6f 100644 --- a/modules/iotjs/linux/evm_module_timers.c +++ b/modules/iotjs/linux/evm_module_timers.c @@ -71,7 +71,6 @@ static evm_val_t evm_module_timers_setTimeout(evm_t *e, evm_val_t *p, int argc, { return EVM_VAL_UNDEFINED; } - int id = evm_module_registry_add(e, v); if (id < 0) return EVM_VAL_UNDEFINED; diff --git a/modules/iotjs/rt-thread/evm_module_gpio.c b/modules/iotjs/rt-thread/evm_module_gpio.c index ef09c1df..e0bf6397 100644 --- a/modules/iotjs/rt-thread/evm_module_gpio.c +++ b/modules/iotjs/rt-thread/evm_module_gpio.c @@ -13,25 +13,31 @@ typedef struct _gpio_dev_t { int edge; } _gpio_dev_t; -evm_val_t *evm_module_uart_class_instantiate(evm_t *e); - static void _gpio_set_mode(_gpio_dev_t *dev) { int mode = PIN_MODE_INPUT; - switch( dev->direction) { - case EVM_GPIO_DIRECTION_IN: mode = PIN_MODE_INPUT;break; - case EVM_GPIO_DIRECTION_OUT:mode = PIN_MODE_OUTPUT;break; - } + switch (dev->direction) + { + case EVM_GPIO_DIRECTION_IN: + mode = PIN_MODE_INPUT; + break; + case EVM_GPIO_DIRECTION_OUT: + mode = PIN_MODE_OUTPUT; + break; + } - switch( dev->mode ) { - case EVM_GPIO_MODE_PUSHPULL: - case EVM_GPIO_MODE_FLOAT: - case EVM_GPIO_MODE_NONE:break; - case EVM_GPIO_MODE_PULLUP: { - if( mode == PIN_MODE_INPUT ) - mode = PIN_MODE_INPUT_PULLUP; - break; - } - case EVM_GPIO_MODE_PULLDOWN: { + switch (dev->mode) + { + case EVM_GPIO_MODE_PUSHPULL: + case EVM_GPIO_MODE_FLOAT: + case EVM_GPIO_MODE_NONE: + break; + case EVM_GPIO_MODE_PULLUP: + { + if (mode == PIN_MODE_INPUT) + mode = PIN_MODE_INPUT_PULLUP; + break; + } + case EVM_GPIO_MODE_PULLDOWN: { if( mode == PIN_MODE_INPUT ) mode = PIN_MODE_INPUT_PULLDOWN; break; @@ -41,8 +47,8 @@ static void _gpio_set_mode(_gpio_dev_t *dev) { mode = PIN_MODE_OUTPUT_OD; break; } - } - dev->rt_mode = mode; + } + dev->rt_mode = mode; } static evm_val_t _gpio_open_device(evm_t *e, evm_val_t *p, int argc, evm_val_t *v, int is_sync) { @@ -104,8 +110,8 @@ static evm_val_t _gpio_open_device(evm_t *e, evm_val_t *p, int argc, evm_val_t * _gpio_set_mode(dev); rt_pin_mode(dev->pin, dev->rt_mode); - ret_obj = evm_module_uart_class_instantiate(e); - if( ret_obj == NULL ) { + ret_obj = evm_module_gpio_class_instantiate(e, p, argc, v); + if( ret_obj == NULL ) { args = evm_mk_foreign_string("Failed to instantiate"); if( cb ) evm_run_callback(e, cb, NULL, &args, 1);