lua 的认识,M3 上的高级脚本语言,移植 elua for GD32F103RET6

转载自——http://bbs.mydigit.cn/read.php?tid=1735714

一、关于Lua脚本语言
Lua 是一个小巧的脚本语言。是巴西里约热内卢天主教大学(Pontifical Catholic University of Rio de Janeiro)里的一个研究小组,由Roberto Ierusalimschy、Waldemar Celes 和 Luiz Henrique de Figueiredo所组成并于1993年开发。 
Lua语言设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能
Lua由标准C编写而成,几乎在所有操作系统和平台上都可以编译,运行。
Lua并没有提供强大的库,这是由它的定位决定的。所以Lua不适合作为开发独立应用程序的语言。
Lua脚本可以很容易的被C/C++代码调用,也可以反过来调用C/C++的函数,这使得Lua在应用程序中可以被广泛应用。不仅仅作为扩展脚本,也可以作为普通的配置文件,代替XML,Ini等文件格式,并且更容易理解和维护。

一个完整的Lua解释器不过200k,在目前所有脚本引擎中,Lua的速度是最快的。这一切都决定了Lua是作为嵌入式脚本的最佳选择。
以上资料来自百度百科: http://baike.baidu.com/view/416116.htm


二、移植和构建 elua  for  gd32执行文件:
1 从http://www.eluaproject.net/ 下载全部源程序;
2 定义板子,在elua-master\boards\kown 下建立一个文本文件:gd32f1-re.lua

复制代码

  1. -- gd32103ret6 build configuration
  2.  
  3. return {
  4.   cpu = 'gd32f103re',
  5.   components = {
  6.     sercon = { uart = 0, speed = 115200, buf_size = 128 },
  7.     wofs = true,
  8.     romfs = true,
  9.     shell = true,
  10.     term = { lines = 25, cols = 80 },
  11.     cints = true,
  12.     luaints = true,
  13.     linenoise = { shell_lines = 10, lua_lines = 50 },
  14.     stm32_enc = true,
  15.     rpc = { uart = 0, speed = 115200},
  16.     adc = { buf_size = 4 },
  17.     xmodem = true,
  18.   },
  19.   config = {
  20.     egc = { mode = "alloc" },
  21.     vtmr = { num = 4, freq = 10 },
  22.     clocks = { external = 12000000, cpu = 120000000 },
  23.   },
  24.   macros = {
  25.     {"GD32F103RET6"                           },
  26.     { "CPU",                   "GD32F103RET6" },
  27.     { "OSC",                   "12000000" },
  28.     { "F_CPU",                 "120000000" },
  29.   },
  30.   modules = {
  31.     generic = { 'all', '-i2c', '-net' },
  32.     platform = 'all',
  33.   }
  34. }

3 定义cpu ,打开 elua_master\build_data.lua,在 platform_list  表中 stm32 中增加GD32F103RE的mcu:

    stm32 = { cpus = { 'STM32F103ZE', 'STM32F103RE','GD32F103RE' }, arch = 'cortexm' },
4 板子定义头文件:在elua_master\src\platform\stm32\ 增加一个 cpu_gd32f103re.h:

复制代码

  1. // CPU definition file for gd32F103RE
  2.  
  3. #ifndef __CPU_GD32F103RE_H__
  4. #define __CPU_GD32F103RE_H__
  5.  
  6. #include "type.h"
  7. #include "stacks.h"
  8. #include "stm32f10x.h"
  9. #include "platform_ints.h"
  10.  
  11. // Number of resources (0 if not available/not implemented)
  12. #define NUM_PIO               7
  13. #define NUM_SPI               2
  14. #define NUM_UART              5
  15. #define NUM_TIMER             5
  16. #define NUM_PHYS_TIMER        5
  17. #define NUM_PWM               4
  18. #define NUM_ADC               16
  19. #define NUM_CAN               1
  20.  
  21. #if !defined(GD32F103RET6)
  22. #define GD32F103RET6          //RET6 12 OSC 120m
  23. #endif
  24.     
  25. //#define STM32F103RET6
  26.  
  27. #define ADC_BIT_RESOLUTION    12
  28.  
  29. u32 platform_s_cpu_get_frequency();
  30. #define CPU_FREQUENCY         platform_s_cpu_get_frequency()
  31.  
  32. // PIO prefix ('0' for P0, P1, ... or 'A' for PA, PB, ...)
  33. #define PIO_PREFIX            'A'
  34. // Pins per port configuration:
  35. // #define PIO_PINS_PER_PORT (n) if each port has the same number of pins, or
  36. // #define PIO_PIN_ARRAY { n1, n2, ... } to define pins per port in an array
  37. // Use #define PIO_PINS_PER_PORT 0 if this isn't needed
  38. #define PIO_PINS_PER_PORT     16
  39.  
  40. // Internal memory data
  41. #define SRAM_SIZE                       ( 64 * 1024 )
  42. #define INTERNAL_RAM1_FIRST_FREE         end
  43. #define INTERNAL_RAM1_LAST_FREE          ( SRAM_BASE + SRAM_SIZE - STACK_SIZE_TOTAL - 1 )
  44.  
  45. #define INTERNAL_FLASH_SIZE             ( 512 * 1024 )
  46. #define INTERNAL_FLASH_SECTOR_SIZE        2048
  47. #define INTERNAL_FLASH_START_ADDRESS      0x08000000
  48.  
  49. // Interrupt list for this CPU
  50. #define PLATFORM_CPU_CONSTANTS_INTS\
  51.   _C( INT_GPIO_POSEDGE ),     \
  52.   _C( INT_GPIO_NEGEDGE ),     \
  53.   _C( INT_TMR_MATCH ),        \
  54.   _C( INT_UART_RX ),
  55.  
  56. #endif // #ifndef __CPU_STM32F103RE_H__

5  根据 第2步 中的一个宏定义 GD32F103RET6,在stm32及stm32/FWLIB中 和RCC有关的文件中修改相关参数:
按:#if  defined(GD32F103RET6)
               ............. //按 gd32 使用 12M 晶振,120M主频修改相关代码,及启动延时等修改;
       #else
              ..............//原来的代码
      #endif
6 因为代码生产也是使用了lua脚本,因此需要下载个 pc版本的lua解释程序,LuaForWindows_v5.1.4-46.exe(百度搜索,下载安装)
7 做个批处理命令脚本:在elua_master 下建个 arm_gcc_sh.cmd 脚本文件,将 GCC工具链路径和lua 运行路径加入path 环境变量,我脚本如下:

复制代码

  1. @REM Launch the bash shell under win32
  2. @ECHO OFF
  3. set old_PATH = %PATH%
  4. PATH =\App\arduino166\hardware\tools\arm-none-eabi\5.4.0-2016q2\bin;C:\Program Files\Lua\5.1;%PATH%
  5. echo **************************************************
  6. echo **** lua build_elua.lua board=stm32f4-ve prog ****
  7. echo **** lua build_elua.lua board=stm32f1-re prog ****
  8. echo **** lua build_elua.lua board=gd32f1-re prog ****
  9. echo **************************************************
  10. cmd
  11. PATH = %OLD_PATH%

8  运行arm_gcc_sh.cmd 并键入命令:lua build_elua.lua board=gd32f1-re prog:



9 成功编译,可在elua_master  找到 elua_lua_gd32f1-re.hex,将该文件烧写到gd32中,联上usb转串口,115200bps 8N1 ,reset后可见开机信息:

表明,程序无误(第一行 hello mydigit.cn  是我在 /rom 下建了个示范程序  autorun.lua自动开机运行的结果)
10 输入help 得到全部 shell下的命令:



11 输入 ls ,可列出 rom盘和wo盘中的文件:


12 输入 lua 回车即可进入lua交互环境:



现在可以试试 lua命名了:
print (“hello mydigi.cn”)
也可以执行其中的脚本:
dofile(“/rom/led.lua”)
支持的lua模块:(详 http://www.eluaproject.net/ )


13  12M晶振,115200 串口1,  gd32f103ret6 elua 执行文件尝鲜版本(尚未全部测试):elua_lua_gd32f1-re.rar,下载   
代码 长度200多k,还有200多k 空间作为 一个内建 /WO 虚拟盘存放 lua脚本、数据,玩玩还是可以的。
14  下一步增加一个 /MMC  SD盘,更方便存放数据和脚本;
15 其它: elua-master\doc 中全部模块的使用文档,elua-master\examples中有各种lua应用的脚本代码,在此基础上可以试试很多原来需要经过复杂编程、烧写、排错等步骤,现在直接运行lua代码就可以达到了。

 

 

 

你可能感兴趣的:(lua)