W600运行vsfvm脚本系统

[复制链接]
 楼主| vsfopen 发表于 2018-9-10 15:13 | 显示全部楼层 |阅读模式
W600可以运行VSF后,第一个加上的应用就是vsfvm脚本系统。基本上不需要移植,直接可以编译通过,针对W600做了一个应用demo。
项目位于:https://gitee.com/versaloon/vsf_open
工程位于:vsf\example\vsfaio\proj\EWARM_W600

开发板为TB-01,vsfvm的demo还是只是简单的串口答应,和点亮LED,不过脚本里使用了数组、线程等。
串口输出,之后板载的LED就会按照规律闪动:
w600_vsfvm.png

脚本代码(已内置于源代码中):
  1. print("led toggle demo...\r\n");
  2. const LED0_PORT = 1, LED0_PIN = 14;
  3. const LED1_PORT = 1, LED1_PIN = 15;
  4. const LED2_PORT = 1, LED2_PIN = 16;
  5. const LED3_PORT = 1, LED3_PIN = 17;
  6. const LED4_PORT = 1, LED4_PIN = 18;
  7. const LED_NUM = 5;
  8. var ledarr = array_create(2, 1, LED_NUM, 2);
  9. ledarr.set(0, 0,
  10.         LED0_PORT, LED0_PIN,
  11.         LED1_PORT, LED1_PIN,
  12.         LED2_PORT, LED2_PIN,
  13.         LED3_PORT, LED3_PIN,
  14.         LED4_PORT, LED4_PIN);

  15. led_toggle(gpio led, delay)
  16. {
  17.         led.config(GPIO_OUTPP).set();
  18.         while (1)
  19.         {
  20.                 led.toggle();
  21.                 timer_delayms(delay);
  22.         }
  23. }

  24. var idx = 0;
  25. while (idx < 5)
  26. {
  27.         thread(led_toggle, gpio_create(ledarr.get(idx, 0), ledarr.get(idx, 1)), 100 * (1 + idx));
  28.         idx = idx + 1;
  29. }



源代码:
  1. /***************************************************************************
  2. *   Copyright (C) 2009 - 2010 by Simon Qian <SimonQian@SimonQian.com>     *
  3. *                                                                         *
  4. *   This program is free software; you can redistribute it and/or modify  *
  5. *   it under the terms of the GNU General Public License as published by  *
  6. *   the Free Software Foundation; either version 2 of the License, or     *
  7. *   (at your option) any later version.                                   *
  8. *                                                                         *
  9. *   This program is distributed in the hope that it will be useful,       *
  10. *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
  11. *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
  12. *   GNU General Public License for more details.                          *
  13. *                                                                         *
  14. *   You should have received a copy of the GNU General Public License     *
  15. *   along with this program; if not, write to the                         *
  16. *   Free Software Foundation, Inc.,                                       *
  17. *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  18. ***************************************************************************/
  19. #include "vsf.h"
  20. #include "usrapp.h"
  21. #include "vsfvm_objdump.h"

  22. struct usrapp_param_t
  23. {
  24.         char *src;
  25. } static const usrapp_param =
  26. {
  27.         .src = "\
  28. print("led toggle demo...\\r\\n");\r\n\
  29. const LED0_PORT = 1, LED0_PIN = 14;\r\n\
  30. const LED1_PORT = 1, LED1_PIN = 15;\r\n\
  31. const LED2_PORT = 1, LED2_PIN = 16;\r\n\
  32. const LED3_PORT = 1, LED3_PIN = 17;\r\n\
  33. const LED4_PORT = 1, LED4_PIN = 18;\r\n\
  34. const LED_NUM = 5;\r\n\
  35. var ledarr = array_create(2, 1, LED_NUM, 2);\r\n\
  36. ledarr.set(0, 0, \r\n\
  37.         LED0_PORT, LED0_PIN,\r\n\
  38.         LED1_PORT, LED1_PIN,\r\n\
  39.         LED2_PORT, LED2_PIN,\r\n\
  40.         LED3_PORT, LED3_PIN,\r\n\
  41.         LED4_PORT, LED4_PIN);\r\n\
  42. \r\n\
  43. led_toggle(gpio led, delay)\r\n\
  44. {\r\n\
  45.         led.config(GPIO_OUTPP).set();\r\n\
  46.         while (1)\r\n\
  47.         {\r\n\
  48.                 led.toggle();\r\n\
  49.                 timer_delayms(delay);\r\n\
  50.         }\r\n\
  51. }\r\n\
  52. \r\n\
  53. var idx = 0;\r\n\
  54. while (idx < 5)\r\n\
  55. {\r\n\
  56.         thread(led_toggle, gpio_create(ledarr.get(idx, 0), ledarr.get(idx, 1)), 100 * (1 + idx));\r\n\
  57.         idx = idx + 1;\r\n\
  58. }\r\n\
  59. ",
  60. };

  61. static const char *vsfvmc_errcode_str[] =
  62. {
  63.         TO_STR(VSFVMC_ERRCODE_NONE),

  64.         // common error
  65.         TO_STR(VSFVMC_BUG),
  66.         TO_STR(VSFVMC_BYTECODE_TOOLONG),
  67.         TO_STR(VSFVMC_NOT_ENOUGH_RESOURCES),
  68.         TO_STR(VSFVMC_FATAL_ERROR),
  69.         TO_STR(VSFVMC_NOT_SUPPORT),

  70.         // lexer error
  71.         TO_STR(VSFVMC_LEXER_NOT_SUPPORT),
  72.         TO_STR(VSFVMC_LEXER_INVALID_OP),
  73.         TO_STR(VSFVMC_LEXER_INVALID_STRING),
  74.         TO_STR(VSFVMC_LEXER_INVALID_ESCAPE),
  75.         TO_STR(VSFVMC_LEXER_SYMBOL_TOO_LONG),

  76.         // parser error
  77.         TO_STR(VSFVMC_PARSER_UNEXPECTED_TOKEN),
  78.         TO_STR(VSFVMC_PARSER_ALREADY_DEFINED),
  79.         TO_STR(VSFVMC_PARSER_INVALID_CLOSURE),
  80.         TO_STR(VSFVMC_PARSER_INVALID_EXPR),
  81.         TO_STR(VSFVMC_PARSER_UNINITED_CONST),
  82.         TO_STR(VSFVMC_PARSER_INVALID_CONST),
  83.         TO_STR(VSFVMC_PARSER_DIV0),
  84.         TO_STR(VSFVMC_PARSER_EXPECT_FUNC_PARAM),
  85.         TO_STR(VSFVMC_PARSER_TOO_MANY_FUNC_PARAM),
  86.         TO_STR(VSFVMC_PARSER_MEMFUNC_NOT_FOUND),

  87.         // compiler error
  88.         TO_STR(VSFVMC_COMPILER_INVALID_MODULE),
  89.         TO_STR(VSFVMC_COMPILER_INVALID_FUNC),
  90.         TO_STR(VSFVMC_COMPILER_INVALID_FUNC_PARAM),
  91.         TO_STR(VSFVMC_COMPILER_FAIL_USRLIB),
  92. };

  93. static int usrapp_vm_set_bytecode(void *param, uint32_t code, uint32_t pos)
  94. {
  95.         struct usrapp_t *app = (struct usrapp_t *)param;
  96.         if (pos >= dimof(app->vsfvm.token))
  97.                 return -1;

  98.         app->vsfvm.token[pos] = code;
  99.         return 0;
  100. }

  101. struct usrapp_t usrapp =
  102. {
  103.         .debug.uart_stream.index                                = DEBUG_UART_INDEX,
  104.         .debug.uart_stream.mode                                        = VSFHAL_USART_STOPBITS_1 | VSFHAL_USART_PARITY_NONE,
  105.         .debug.uart_stream.int_priority                        = 0xFF,
  106.         .debug.uart_stream.baudrate                                = 115200,

  107.         .debug.uart_stream.stream_tx                        = &usrapp.debug.stream_tx.stream,
  108.         .debug.uart_stream.stream_rx                        = &usrapp.debug.stream_rx.stream,

  109.         .debug.stream_tx.stream.op                                = &vsf_fifostream_op,
  110.         .debug.stream_tx.mem.buffer.buffer                = (uint8_t *)&usrapp.debug.txbuff,
  111.         .debug.stream_tx.mem.buffer.size                = sizeof(usrapp.debug.txbuff),
  112.         .debug.stream_rx.stream.op                                = &vsf_fifostream_op,
  113.         .debug.stream_rx.mem.buffer.buffer                = (uint8_t *)&usrapp.debug.rxbuff,
  114.         .debug.stream_rx.mem.buffer.size                = sizeof(usrapp.debug.rxbuff),

  115.         .vsfvm.vmc.dart.op                                                = &vsfvmc_lexer_op_dart,
  116. };

  117. void usrapp_srt_init(struct usrapp_t *app)
  118. {
  119.         VSFSTREAM_INIT(&app->debug.stream_rx);
  120.         VSFSTREAM_INIT(&app->debug.stream_tx);
  121.         vsf_usart_stream_init(&app->debug.uart_stream);
  122.         vsfdbg_init((struct vsf_stream_t *)&app->debug.stream_tx);
  123. }

  124. void usrapp_srt_poll(struct usrapp_t *app){}

  125. static void usrapp_vm_compile(struct usrapp_t *app)
  126. {
  127.         struct vsfvmc_t *vmc = &app->vsfvm.vmc.vmc;
  128.         int err;

  129.         vsfdbg_prints("start compiling ..." VSFCFG_DEBUG_LINEEND);
  130.         vsfvmc_init(vmc, &usrapp, NULL, usrapp_vm_set_bytecode);
  131.         vsfvmc_register_lexer(vmc, &app->vsfvm.vmc.dart);
  132.         err = vsfvmc_script(vmc, "main.dart");
  133.         if (err < 0) goto err_return;

  134.         err = vsfvmc_input(vmc, usrapp_param.src);
  135.         if (err < 0)
  136.         {
  137.         err_return:
  138.                 err = -err;
  139.                 vsfdbg_printf("command line compile error: %s" VSFCFG_DEBUG_LINEEND,
  140.                         (err >= VSFVMC_ERRCODE_END) ? "unknwon error" : vsfvmc_errcode_str[err]);
  141.                 vsfdbg_printf("compile error around line %d column %d" VSFCFG_DEBUG_LINEEND,
  142.                         vmc->script.lexer.curctx.line + 1, vmc->script.lexer.curctx.col + 1);
  143.         compile_end:
  144.                 vsfvmc_fini(vmc);
  145.                 return;
  146.         }

  147.         err = vsfvmc_input(vmc, "\xFF");
  148.         if (err) goto compile_end;

  149.         vsfdbg_printf("compiled OK, token number : %d" VSFCFG_DEBUG_LINEEND,
  150.                         vmc->bytecode_pos);

  151.         app->vsfvm.token_num = vmc->bytecode_pos;
  152.         vsfdbg_prints("objdump:" VSFCFG_DEBUG_LINEEND);
  153.         vsfvm_objdump(app->vsfvm.token, app->vsfvm.token_num);
  154. }

  155. static void usrapp_vm_run(struct usrapp_t *app)
  156. {
  157.         struct vsfvm_t *vm = &app->vsfvm.vm.vm;
  158.         struct vsfvm_script_t *script = &app->vsfvm.vm.script;

  159.         memset(vm, 0, sizeof(*vm));
  160.         memset(script, 0, sizeof(*script));
  161.         vm->thread_pool.pool_size = 16;
  162.         script->token = app->vsfvm.token;
  163.         script->token_num = app->vsfvm.token_num;

  164.         vsfvm_init(vm);
  165.         vsfvm_script_init(vm, script);
  166. }

  167. void usrapp_nrt_init(struct usrapp_t *app)
  168. {
  169.         vsfvm_ext_register_std();
  170.         vsfvm_ext_register_vsf();

  171.         vsfdbg_prints("source code:" VSFCFG_DEBUG_LINEEND);
  172.         vsfdbg_prints(usrapp_param.src);
  173.         vsfdbg_prints(VSFCFG_DEBUG_LINEEND);

  174.         usrapp_vm_compile(app);
  175.         usrapp_vm_run(app);
  176. }

  177. void usrapp_nrt_poll(struct usrapp_t *app)
  178. {
  179.         vsfvm_poll(&app->vsfvm.vm.vm);
  180. }

  181. bool usrapp_cansleep(struct usrapp_t *app)
  182. {
  183.         return app->vsfvm.vm.vm.appendlist.next == NULL;
  184. }

  185. void usrapp_initial_init(struct usrapp_t *app){}


luibo54696578 发表于 2020-2-22 19:57 | 显示全部楼层
我又回复了~~~~~~~~~~~~~~~~~`

90

主题

325

帖子

8

粉丝
快速回复 在线客服 返回列表 返回顶部