查看: 755|回复: 1

RA6M5移植lua

[复制链接]

38

主题

126

帖子

9374

积分

论坛元老

Rank: 8Rank: 8

积分
9374
发表于 2022-12-19 07:29:13 | 显示全部楼层 |阅读模式
lua现在热度一直在上升,能在单片机上跑,上海合宙的很多片上都跑了lua,stm32上移植lua的例程很多,但是瑞萨的例子没找到,今天花了一天时间初步把Lua移植过来了。
配置好串口
板上的串口转usb是uart4所以就配置sci4:
内存设置:
这个我试了好多次,设置太少就会报没有内存。unprotected error in call to Lua API (not enough memory)
debug-uart
新建bsp文件夹,把uart初始化、printf配置好,这里不详说,代码如下:
#include "bsp_debug_uart.h"#include "r_sci_uart.h"#include "hal_data.h"/* µ÷ÊÔ´®¿Ú UART4 ³õʼ»¯ */void bsp_uart_init(void){        fsp_err_t err = FSP_SUCCESS;        err = R_SCI_UART_Open (&debug_uart_ctrl, &debug_uart_cfg);        assert(FSP_SUCCESS == err);}/* ·¢ËÍÍê³É±êÖ¾ */volatile bool uart_send_complete_flag = false;/* ´®¿ÚÖжϻص÷ */void debug_uart_callback(uart_callback_args_t * p_args){        switch (p_args->event)        {                case UART_EVENT_RX_CHAR:                {                /* °Ñ´®¿Ú½ÓÊÕµ½µÄÊý¾Ý·¢ËÍ»ØÈ¥ */                //R_SCI_UART_Write(&debug_uart_ctrl, (uint8_t *)&(p_args->data), 1);                break;                }                case UART_EVENT_TX_COMPLETE:                {                //uart_send_complete_flag = true;                break;                }                default:                break;        }}        /* Öض¨Ïò printf Êä³ö */#if defined __GNUC__ && !defined __clang__int _write(int fd, char *pBuffer, int size); //·ÀÖ¹±àÒ뾯¸æint _write(int fd, char *pBuffer, int size){        (void)fd;        R_SCI_UART_Write(&g_uart0_ctrl, (uint8_t *)pBuffer, (uint32_t)size);        while(uart_send_complete_flag == false);        uart_send_complete_flag = false;        return size;}#elseint fputc(int ch, FILE *f){        (void)f;        R_SCI_UART_Write(&debug_uart_ctrl, (uint8_t *)&ch, 1);        while(uart_send_complete_flag == false);        uart_send_complete_flag = false;        return ch;}#endif
bsp_debug_uart.h
#ifndef __BSP_UART_H__#define __BSP_UART_H__#include "hal_data.h"#include "stdio.h"void bsp_uart_init(void);#endif
到这里串口配置就完成了。
下载lua源码
网址:[color=rgb(12, 147, 228) !important]Lua:下载
下载后解压出来,只需要把src下面的文件拷到工程的src目录里,当然也可以自己新建一个文件夹放进去,并按常规把.c、.h加入工程目录。

删除lua.c、luac.c这两个文件。
最后的工程如下:
添加缺少的几个函数
编译后会报time的错误 ,把下面几个函数添加到lmathlib_c里面:
volatile uint32_t s_ms_u32 = 0;time_t time(time_t * time){ if(time != 0)        {        *time = s_ms_u32/1000;        }        return s_ms_u32/1000;}void exit(int status){   }int system(const char * string){    return 0;}
这样编译就可以通过了。
编写测试函数
在hal_entry.c中加入lua头文件、及测试函数如下:
#include "hal_data.h"#include "bsp_debug_uart.h"#include "lua.h"#include "lauxlib.h"#include "lualib.h"#include "time.h"#include "stdlib.h"#include "stdio.h"const char lua_test[] = {    "print(\"Hello,I am lua!\n--this is newline printf\")\n"    "function foo()\n"    "  local i = 0\n"    "  local sum = 1\n"    "  while i <= 10 do\n"    "    sum = sum * 2\n"    "    i = i + 1\n"    "  end\n"    "return sum\n"    "end\n"    "print(\"sum =\", foo())\n"    "print(\"and sum = 2^11 =\", 2 ^ 11)\n"    "print(\"exp(200) =\", math.exp(200))\n"};/* ??Lua */static int do_file_script(void){    lua_State *L;              L = luaL_newstate(); /* ??Lua???? */    luaL_openlibs(L);    luaopen_base(L);    luaL_dostring(L, lua_test); /* ??Lua?? */    lua_close(L);    return 0;}/*******************************************************************************************************************//** * main() is generated by the RA Configuration editor and is used to generate threads if an RTOS is used.  This function * is called by main() when no RTOS is used. **********************************************************************************************************************/void hal_entry(void){    /* TODO: add your own code here */                bsp_uart_init();                printf("start...RAM5");                do_file_script();#if BSP_TZ_SECURE_BUILD    /* Enter non-secure code */    R_BSP_NonSecureEnter();#endif}
下载到开发板后,就运行了lua的脚本语言了:
后面再添加LED等其他的工程。

回复

使用道具 举报

3

主题

195

帖子

1165

积分

金牌会员

Rank: 6Rank: 6

积分
1165
发表于 2023-3-14 11:50:19 | 显示全部楼层

不错,学习一下
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

用户排行榜

RA助手

主题: 116帖子:133精华:0

RA_Lance

主题: 92帖子:132精华:9

lugl

主题: 38帖子:126精华:0

xujiwei263

主题: 16帖子:73精华:0

books咦

主题: 11帖子:11精华:2

Juggernaut

主题: 9帖子:95精华:0
快速回复 返回顶部 返回列表