1. EVM标准化的API扩展接口


1.1 数据结构

  • name: 注册接口名称
  • v: 注册接口API实现
  1. typedef struct evm_builtin_t{
  2. const char * name;
  3. evm_val_t v;
  4. }evm_builtin_t;

1.2 evm_module_create

注册 evm_builtin_t列表中所有的API注册到命名为name的模块中

  1. /**
  2. * @brief 创建指定名称的模块
  3. * @param e,虚拟机
  4. * @param name,名称
  5. * @param n,内置成员列表
  6. * @return 内置对象
  7. */
  8. evm_val_t evm_module_create(evm_t * e, const char* name, evm_builtin_t *n);

2. 扩展模块注册流程

  1. /**********定义用户API*****************/
  2. /**********创建模块*****************/
  3. int evm_module(evm_t * e){
  4. evm_runtime = e;
  5. number_of_callbacks = CONFIG_EVM_CALLBACK_SIZE;
  6. callback_list = evm_list_create(e, GC_LIST, number_of_callbacks);
  7. if( !callback_list ) return e->err;
  8. for(uint32_t i = 0; i < number_of_callbacks; i++){
  9. evm_list_set(e, callback_list, i, EVM_VAL_UNDEFINED);
  10. }
  11. args_list = evm_list_create(e, GC_LIST, number_of_callbacks);
  12. if( !args_list ) return e->err;
  13. evm_builtin_t module[] = {
  14. {"delay_ms", evm_mk_native((intptr_t)evm_module_delay_ms)},
  15. {"delay_us", evm_mk_native((intptr_t)evm_module_delay_us)},
  16. #ifdef CONFIG_EVM_GPIO
  17. {"Pin", evm_class_pin(e)},
  18. #endif
  19. #ifdef CONFIG_EVM_LCD
  20. {"LCD", evm_class_lcd(e)},
  21. #endif
  22. #ifdef CONFIG_EVM_LED
  23. {"LED", evm_class_led(e)},
  24. #endif
  25. #ifdef CONFIG_EVM_FLASH
  26. {"Flash", evm_class_flash(e)},
  27. #endif
  28. #ifdef CONFIG_EVM_ADC
  29. {"ADC", evm_class_adc(e)},
  30. #endif
  31. #ifdef CONFIG_EVM_DAC
  32. {"DAC", evm_class_dac(e)},
  33. #endif
  34. #ifdef CONFIG_EVM_RTC
  35. {"RTC", evm_class_rtc(e)},
  36. #endif
  37. #ifdef CONFIG_EVM_SWITCH
  38. {"Switch", evm_class_switch(e)},
  39. #endif
  40. #ifdef CONFIG_EVM_WATCHDOG
  41. {"Watchdog", evm_class_watchdog(e)},
  42. #endif
  43. #ifdef CONFIG_EVM_TIMER
  44. {"Timer", evm_class_timer(e)},
  45. #endif
  46. #ifdef CONFIG_EVM_UART
  47. {"UART", evm_class_uart(e)},
  48. #endif
  49. {NULL, NULL}
  50. };
  51. evm_module_create(e, "evm", module);
  52. return e->err;
  53. }