野火科技
直播中

华仔stm32

3年用户 2895经验值
擅长:嵌入式技术
私信 关注
[经验]

【野火启明6M5开发板体验】移植lua

lua现在热度一直在上升,能在单片机上跑,上海合宙的很多片上都跑了lua,stm32上移植lua的例程很多,但是瑞萨的例子没找到,今天花了一天时间初步把Lua移植过来了。

配置好串口

板上的串口转usb是uart4所以就配置sci4:

image.png

内存设置:

这个我试了好多次,设置太少就会报没有内存。unprotected error in call to Lua API (not enough memory)

image.png

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;
}
#else
int 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源码

网址:Lua:下载
下载后解压出来,只需要把src下面的文件拷到工程的src目录里,当然也可以自己新建一个文件夹放进去,并按常规把.c、.h加入工程目录。
image.png
删除lua.c、luac.c这两个文件。
最后的工程如下:
image.png

添加缺少的几个函数

编译后会报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的脚本语言了:

image.png

后面再添加LED等其他的工程。

更多回帖

发帖
×
20
完善资料,
赚取积分