STM32H743 transplant LUA

Raw materials and tools

A set of on-time atomic mercury development board (including H743 core board), a set of JLINK simulator and a computer equipped with Keil5.

start

Main process reference articles:
STM32 embedded LUA development (control small light flashing)
The original text is transplanted to STM32F103. The general process of H743 we use is the same.

  1. Use STM32CubeMX software to configure and generate Keil project.
  2. Download the LUA source code and add the required files to the Keil project created before.
  3. Modify the source code and compile.
  4. Add LUA initialization and script running functions in the main function.
  5. Compile and download.
  6. verification.

Configure and generate projects

Here, refer to STM32H743 Development Guide (mercury version) V1.0 – HAL library version of punctual atom, and configure H743 functions and pins according to the hardware of mercury board. The main hardware used are LED and USART1. The corresponding configuration contents mainly include Clock, GPIO, SYS, etc.
After configuration, click "GENERATE CODE" to generate the project.

Download LUA source code and add it to the project

  1. Download address I downloaded version 5.3.5, which is now 5.4.4. I can choose the version I need to download.
  2. After downloading, unzip it, then create a new "Lua" folder under the project folder, and put the ". c" file and ". h" file in the source code in the new "Src" and "Inc" folders under Lua respectively.
  3. Use Keil to open the project, right-click the project – > Add group on the Projec panel, and name it "LUA", double-click LUA, locate the newly created "Lua/Src" folder, and add the ". c" file. Here you can exclude the two files "lua.c" and "luac.c".
  4. Add header file directory "Lua/Inc".

Modify source code

  1. Modify stack and heap size
    Modify the "startup_stm32h743xx.s" file
Stack_Size		EQU     0x00001000 	;0x400

                AREA    STACK, NOINIT, READWRITE, ALIGN=3
Stack_Mem       SPACE   Stack_Size
__initial_sp


; <h> Heap Configuration
;   <o>  Heap Size (in Bytes) <0x0-0xFFFFFFFF:8>
; </h>

Heap_Size      EQU     0x00015000	;0x200

                AREA    HEAP, NOINIT, READWRITE, ALIGN=3
__heap_base
Heap_Mem        SPACE   Heap_Size
__heap_limit
  1. In main C add the following code
    Reference here MCU script language - porting lua to STM32 MDK
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#include "time.h"


typedef int FILEHANDLE;

#pragma import(__use_no_semihosting_swi)

#pragma import(_main_redirection)

const char __stdin_name[150];

const char __stdout_name[150];

const char __stderr_name[150];

FILEHANDLE _sys_open(const char *name, int openmode)

{

return 0;

}

int _sys_close(FILEHANDLE fh)

{

return 0;

}

int _sys_write(FILEHANDLE fh, const unsigned char *buf, unsigned len, int mode)

{

return 0;

}

int _sys_read(FILEHANDLE fh, unsigned char*buf, unsigned len, int mode)

{

return 0;

}

    //Check whether the handle is a terminal

int _sys_istty(FILEHANDLE fh)

{

return 0;

}

int _sys_seek(FILEHANDLE fh, long pos)

{

return 0;

}

    //Flushes the buffer associated with the handle

int _sys_ensure(FILEHANDLE fh)

{

return 0;

}

    //Returns the current length of the file

long _sys_flen(FILEHANDLE fh)

{

return 0;

}

void _sys_exit(int status)

{

//while(1);

}

int _sys_tmpnam(char *name, int fileno, unsigned maxlength)

{

return 0;

}

    //Write a character to the console

void _ttywrch(int ch)

{

}

int remove(const char *filename)

{

return 0;

}

char *_sys_command_string(char *cmd, int len)

{

return NULL;

}

time_t time(time_t * time)
{
	return 0;
}

//Redefine fputc function 
int fputc(int ch, FILE *f)
{ 	
	while((USART1->ISR&0X40)==0);//Send circularly until sending is completed   
	USART1->TDR=(u8)ch;      
	return ch;
}

This step is very important. It is used to rewrite some underlying functions. If it is not rewritten, the runtime will always be stuck in the position of BEAB BKPT 0xAB of the assembly.

  1. Add lua function function to realize, including the on, off and delay functions of LED0. The code is as follows:
static int lua_led_on(lua_State *L)
{
  LED0(1);
	printf("LED0->1\r\n");
  return 1;
}
static int lua_led_off(lua_State *L)
{
  LED0(0);
	printf("LED0->0\r\n");
  return 1;
}
 
static int lua_delay(lua_State *L)
{
  int num;
  num= lua_tointeger(L, 1);
  delay_ms(num);
  return 1;
}
  1. Register lua function and initialize a simple script:
static const struct luaL_Reg mylib[]=
{
  {"led_on",lua_led_on},
  {"led_off",lua_led_off},
  {"delay",lua_delay},
  {NULL,NULL}
};

const char LUA_SCRIPT_Test[] =
"  \
off = 500     \
on = 500       \
 led_on() \
 delay(off)    \
 led_off()        \
 delay(on)      \
";

Modify main function

//lua initialization part
	lua_State *L;
	L=luaL_newstate();			//Create LUA state machine
	luaopen_base(L);
	luaL_setfuncs(L,mylib,0);
  while (1)
  {
		luaL_dostring(L,LUA_SCRIPT_Test);		//Run LUA script
  }

Compile and download

Operation and verification

As shown in the figure

be accomplished

Keywords: Single-Chip Microcomputer stm32 lua

Added by jebster on Fri, 04 Feb 2022 11:18:13 +0200