基于cubemx 的 h750vb 移植LUA解释器

因为我想实现的功能是在把 *.lua文件丢到FLASH虚拟的U盘里,重新上电后运行。
网上的资料不多,而且大多没有关于dofile函数的实现。
所以,我整理了一下,形成了这篇博客。
  1. 创建stm32cubemx的H750vb工程,并完成外设的驱动:
    基于cubemx 的 h750vb 移植LUA解释器

     这部分网上有很多实现方法这里不在赘述:
    
    • usart驱动
    • spi flash驱动 这里我使用的是spi2口
    • fatfs文件系统 并 挂载spi flash分区
    • usb的msc模式

    修改堆栈大小。 stack size设为0x2000; heap size设为0x4000.

  2. 去Lua官网下载源文件
    http://www.lua.org/download.html
    我使用的是当前最新版本 5.4.3,将加载到自己的工程中(不添加lua.c和luac.c)。
    参考armfly大神的分享,及其在H7-TOOL中的实现方法:
    http://www.armbbs.cn/forum.php?mod=viewthread&tid=97441
    启用MicroLIB库,同时增加 lua_sys.c 文件代码如下:

#include "usart.h"
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <ff.h>
/*lua相关*/
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"



FATFS fs;
FIL g_file;
/*
*********************************************************************************************************
*    函 数 名: 无
*    功能说明: 此处,重写了 stdio 中定义的与文件操作的相关函数,使其满足Lua解释器的文件调用操作,
*             暂时只实现了读取功能。
*    形    参: 无
*    返 回 值: 无
*********************************************************************************************************
*/
extern _ARMABI int remove(const char * /*filename*/) __attribute__((__nonnull__(1)));
   /*
    * causes the file whose name is the string pointed to by filename to be
    * removed. Subsequent attempts to open the file will fail, unless it is
    * created anew. If the file is open, the behaviour of the remove function
    * is implementation-defined.
    * Returns: zero if the operation succeeds, nonzero if it fails.
    */
extern _ARMABI int rename(const char * /*old*/, const char * /*new*/) __attribute__((__nonnull__(1,2)));
   /*
    * causes the file whose name is the string pointed to by old to be
    * henceforth known by the name given by the string pointed to by new. The
    * file named old is effectively removed. If a file named by the string
    * pointed to by new exists prior to the call of the rename function, the
    * behaviour is implementation-defined.
    * Returns: zero if the operation succeeds, nonzero if it fails, in which
    *          case if the file existed previously it is still known by its
    *          original name.
    */
extern _ARMABI FILE *tmpfile(void);
   /*
    * creates a temporary binary file that will be automatically removed when
    * it is closed or at program termination. The file is opened for update.
    * Returns: a pointer to the stream of the file that it created. If the file
    *          cannot be created, a null pointer is returned.
    */
extern _ARMABI char *tmpnam(char * /*s*/);
   /*
    * generates a string that is not the same as the name of an existing file.
    * The tmpnam function generates a different string each time it is called,
    * up to TMP_MAX times. If it is called more than TMP_MAX times, the
    * behaviour is implementation-defined.
    * Returns: If the argument is a null pointer, the tmpnam function leaves
    *          its result in an internal static object and returns a pointer to
    *          that object. Subsequent calls to the tmpnam function may modify
    *          the same object. if the argument is not a null pointer, it is
    *          assumed to point to an array of at least L_tmpnam characters;
    *          the tmpnam function writes its result in that array and returns
    *          the argument as its value.
    */

//extern _ARMABI int fclose(FILE * /*stream*/) __attribute__((__nonnull__(1)));
_ARMABI int fclose(FILE * stream)
   /*
    * causes the stream pointed to by stream to be flushed and the associated
    * file to be closed. Any unwritten buffered data for the stream are
    * delivered to the host environment to be written to the file; any unread
    * buffered data are discarded. The stream is disassociated from the file.
    * If the associated buffer was automatically allocated, it is deallocated.
    * Returns: zero if the stream was succesfully closed, or nonzero if any
    *          errors were detected or if the stream was already closed.
    */
{
    FIL *fp;
    FRESULT result;

    fp = (FIL *)stream;
    result = f_close(fp);

    if (result != FR_OK)
    {
        return result;
    }
    return 0;
}

extern _ARMABI int fflush(FILE * /*stream*/);
   /*
    * If the stream points to an output or update stream in which the most
    * recent operation was output, the fflush function causes any unwritten
    * data for that stream to be delivered to the host environment to be
    * written to the file. If the stream points to an input or update stream,
    * the fflush function undoes the effect of any preceding ungetc operation
    * on the stream.
    * Returns: nonzero if a write error occurs.
    */

_ARMABI FILE *fopen(const char * filename /*filename*/,
                           const char * mode /*mode*/)
   /*
    * opens the file whose name is the string pointed to by filename, and
    * associates a stream with it.
    * The argument mode points to a string beginning with one of the following
    * sequences:
    * "r"         open text file for reading
    * "w"         create text file for writing, or truncate to zero length
    * "a"         append; open text file or create for writing at eof
    * "rb"        open binary file for reading
    * "wb"        create binary file for writing, or truncate to zero length
    * "ab"        append; open binary file or create for writing at eof
    * "r+"        open text file for update (reading and writing)
    * "w+"        create text file for update, or truncate to zero length
    * "a+"        append; open text file or create for update, writing at eof
    * "r+b"/"rb+" open binary file for update (reading and writing)
    * "w+b"/"wb+" create binary file for update, or truncate to zero length
    * "a+b"/"ab+" append; open binary file or create for update, writing at eof
    *
    * Opening a file with read mode ('r' as the first character in the mode
    * argument) fails if the file does not exist or cannot be read.
    * Opening a file with append mode ('a' as the first character in the mode
    * argument) causes all subsequent writes to be forced to the current end of
    * file, regardless of intervening calls to the fseek function. In some
    * implementations, opening a binary file with append mode ('b' as the
    * second or third character in the mode argument) may initially position
    * the file position indicator beyond the last data written, because of the
    * NUL padding.
    * When a file is opened with update mode ('+' as the second or third
    * character in the mode argument), both input and output may be performed
    * on the associated stream. However, output may not be directly followed
    * by input without an intervening call to the fflush fuction or to a file
    * positioning function (fseek, fsetpos, or rewind), and input be not be
    * directly followed by output without an intervening call to the fflush
    * fuction or to a file positioning function, unless the input operation
    * encounters end-of-file. Opening a file with update mode may open or
    * create a binary stream in some implementations. When opened, a stream
    * is fully buffered if and only if it does not refer to an interactive
    * device. The error and end-of-file indicators for the stream are
    * cleared.
    * Returns: a pointer to the object controlling the stream. If the open
    *          operation fails, fopen returns a null pointer.
    */
{
    /*
        fatfs官网有兼容方式定义格式
        http://elm-chan.org/fsw/ff/doc/open.html

        POSIX        FatFs
        "r"            FA_READ
        "r+"        FA_READ | FA_WRITE
        "w"            FA_CREATE_ALWAYS | FA_WRITE
        "w+"        FA_CREATE_ALWAYS | FA_WRITE | FA_READ
        "a"     FA_OPEN_APPEND | FA_WRITE
        "a+"    FA_OPEN_APPEND | FA_WRITE | FA_READ
        "wx"    FA_CREATE_NEW | FA_WRITE
        "w+x"   FA_CREATE_NEW | FA_WRITE | FA_READ
    */
    FRESULT result;
    BYTE ff_mode;

    if (strcmp(mode, "r") == 0)         ff_mode = FA_READ;
    else if (strcmp(mode, "r+") == 0)   ff_mode = FA_READ | FA_WRITE;
    else if (strcmp(mode, "w") == 0)    ff_mode = FA_CREATE_ALWAYS | FA_WRITE;
    else if (strcmp(mode, "w+") == 0)   ff_mode = FA_CREATE_ALWAYS | FA_WRITE | FA_READ;
    else if (strcmp(mode, "a") == 0)    ff_mode = FA_OPEN_APPEND | FA_WRITE;
    else if (strcmp(mode, "a+") == 0)   ff_mode = FA_OPEN_APPEND | FA_WRITE | FA_READ;
    else if (strcmp(mode, "wx") == 0)   ff_mode = FA_CREATE_NEW | FA_WRITE;
    else if (strcmp(mode, "w+x") == 0)  ff_mode = FA_CREATE_NEW | FA_WRITE | FA_READ;

    /* 打开文件 */
    f_close(&g_file);
    result = f_open(&g_file, filename, ff_mode);
    if (result == FR_OK)
    {
        return (FILE *)&g_file;
    }

    return 0;
}

_ARMABI FILE *freopen(const char * filename /*filename*/,
                    const char * mode /*mode*/,
                    FILE * stream /*stream*/)
   /*
    * opens the file whose name is the string pointed to by filename and
    * associates the stream pointed to by stream with it. The mode argument is
    * used just as in the fopen function.
    * The freopen function first attempts to close any file that is associated
    * with the specified stream. Failure to close the file successfully is
    * ignored. The error and end-of-file indicators for the stream are cleared.
    * Returns: a null pointer if the operation fails. Otherwise, freopen
    *          returns the value of the stream.
    */  
{  
    return 0;
}

extern _ARMABI void setbuf(FILE * __restrict /*stream*/,
                    char * __restrict /*buf*/) __attribute__((__nonnull__(1)));
   /*
    * Except that it returns no value, the setbuf function is equivalent to the
    * setvbuf function invoked with the values _IOFBF for mode and BUFSIZ for
    * size, or (if buf is a null pointer), with the value _IONBF for mode.
    * Returns: no value.
    */
extern _ARMABI int setvbuf(FILE * __restrict /*stream*/,
                   char * __restrict /*buf*/,
                   int /*mode*/, size_t /*size*/) __attribute__((__nonnull__(1)));
   /*
    * may be used after the stream pointed to by stream has been associated
    * with an open file but before it is read or written. The argument mode
    * determines how stream will be buffered, as follows: _IOFBF causes
    * input/output to be fully buffered; _IOLBF causes output to be line
    * buffered (the buffer will be flushed when a new-line character is
    * written, when the buffer is full, or when input is requested); _IONBF
    * causes input/output to be completely unbuffered. If buf is not the null
    * pointer, the array it points to may be used instead of an automatically
    * allocated buffer (the buffer must have a lifetime at least as great as
    * the open stream, so the stream should be closed before a buffer that has
    * automatic storage duration is deallocated upon block exit). The argument
    * size specifies the size of the array. The contents of the array at any
    * time are indeterminate.
    * Returns: zero on success, or nonzero if an invalid value is given for
    *          mode or size, or if the request cannot be honoured.
    */

extern _ARMABI size_t fread(void * ptr /*ptr*/,
                    size_t size/*size*/, size_t nmemb/*nmemb*/, FILE * stream /*stream*/)
   /*
    * reads into the array pointed to by ptr, up to nmemb members whose size is
    * specified by size, from the stream pointed to by stream. The file
    * position indicator (if defined) is advanced by the number of characters
    * successfully read. If an error occurs, the resulting value of the file
    * position indicator is indeterminate. If a partial member is read, its
    * value is indeterminate. The ferror or feof function shall be used to
    * distinguish between a read error and end-of-file.
    * Returns: the number of members successfully read, which may be less than
    *          nmemb if a read error or end-of-file is encountered. If size or
    *          nmemb is zero, fread returns zero and the contents of the array
    *          and the state of the stream remain unchanged.
    */
{
    FRESULT result;
    uint32_t br = 0;
    FIL *fp;

    fp = (FIL *)stream;   

    result = f_read(fp, ptr, nmemb,  &br);
    if (result == FR_OK)
    {
        return br;
    }

    return 0;
}

extern _ARMABI int setvbuf(FILE * __restrict /*stream*/,
                   char * __restrict /*buf*/,
                   int /*mode*/, size_t /*size*/) __attribute__((__nonnull__(1)));
   /*
    * may be used after the stream pointed to by stream has been associated
    * with an open file but before it is read or written. The argument mode
    * determines how stream will be buffered, as follows: _IOFBF causes
    * input/output to be fully buffered; _IOLBF causes output to be line
    * buffered (the buffer will be flushed when a new-line character is
    * written, when the buffer is full, or when input is requested); _IONBF
    * causes input/output to be completely unbuffered. If buf is not the null
    * pointer, the array it points to may be used instead of an automatically
    * allocated buffer (the buffer must have a lifetime at least as great as
    * the open stream, so the stream should be closed before a buffer that has
    * automatic storage duration is deallocated upon block exit). The argument
    * size specifies the size of the array. The contents of the array at any
    * time are indeterminate.
    * Returns: zero on success, or nonzero if an invalid value is given for
    *          mode or size, or if the request cannot be honoured.
    */

extern _ARMABI size_t fwrite(const void * __restrict /*ptr*/,
                    size_t /*size*/, size_t /*nmemb*/, FILE * __restrict /*stream*/) __attribute__((__nonnull__(1,4)));
   /*
    * writes, from the array pointed to by ptr up to nmemb members whose size
    * is specified by size, to the stream pointed to by stream. The file
    * position indicator (if defined) is advanced by the number of characters
    * successfully written. If an error occurs, the resulting value of the file
    * position indicator is indeterminate.
    * Returns: the number of members successfully written, which will be less
    *          than nmemb only if a write error is encountered.
    */

extern _ARMABI int fseek(FILE * /*stream*/, long int /*offset*/, int /*whence*/) __attribute__((__nonnull__(1)));
   /*
    * sets the file position indicator for the stream pointed to by stream.
    * For a binary stream, the new position is at the signed number of
    * characters specified by offset away from the point specified by whence.
    * The specified point is the beginning of the file for SEEK_SET, the
    * current position in the file for SEEK_CUR, or end-of-file for SEEK_END.
    * A binary stream need not meaningfully support fseek calls with a whence
    * value of SEEK_END.
    * For a text stream, either offset shall be zero, or offset shall be a
    * value returned by an earlier call to the ftell function on the same
    * stream and whence shall be SEEK_SET.
    * The fseek function clears the end-of-file indicator and undoes any
    * effects of the ungetc function on the same stream. After an fseek call,
    * the next operation on an update stream may be either input or output.
    * Returns: nonzero only for a request that cannot be satisfied.
    */
_ARMABI int fseek(FILE *stream, long int offset, int whence)
{
    return 0;
}

extern _ARMABI long int ftell(FILE * /*stream*/) __attribute__((__nonnull__(1)));
   /*
    * obtains the current value of the file position indicator for the stream
    * pointed to by stream. For a binary stream, the value is the number of
    * characters from the beginning of the file. For a text stream, the file
    * position indicator contains unspecified information, usable by the fseek
    * function for returning the file position indicator to its position at the
    * time of the ftell call; the difference between two such return values is
    * not necessarily a meaningful measure of the number of characters written
    * or read.
    * Returns: if successful, the current value of the file position indicator.
    *          On failure, the ftell function returns -1L and sets the integer
    *          expression errno to an implementation-defined nonzero value.
    */
_ARMABI long int ftell(FILE *stream)  
{
    FIL *fp;

    fp = (FIL *)stream;   

    return fp->fptr;
}

extern _ARMABI void clearerr(FILE * /*stream*/) __attribute__((__nonnull__(1)));
   /*
    * clears the end-of-file and error indicators for the stream pointed to by
    * stream. These indicators are cleared only when the file is opened or by
    * an explicit call to the clearerr function or to the rewind function.
    * Returns: no value.
    */
void clearerr(FILE *stream)
{
    FIL *fp;

    fp = (FIL *)stream;

    fp->err = 0;
}


extern _ARMABI int feof(FILE * /*stream*/) __attribute__((__nonnull__(1)));
   /*
    * tests the end-of-file indicator for the stream pointed to by stream.
    * Returns: nonzero iff the end-of-file indicator is set for stream.
    */
_ARMABI int feof(FILE *stream)
{
    FIL *fp;

    fp = (FIL *)stream;
    if (fp->fptr >= fp->obj.objsize)
    {
        return 1;
    }
    return 0;   
}

extern _ARMABI int ferror(FILE * /*stream*/) __attribute__((__nonnull__(1)));
   /*
    * tests the error indicator for the stream pointed to by stream.
    * Returns: nonzero iff the error indicator is set for stream.
    */
_ARMABI int ferror(FILE *stream)
{
    FIL *fp;

    fp = (FIL *)stream;

    return fp->err;
}


/*
*********************************************************************************************************
*    函 数 名: fgetc
*    功能说明: 重定义getc函数,这样可以使用getchar函数从串口1输入数据
*    形    参: 无
*    返 回 值: 无
*********************************************************************************************************
*/
int fgetc(FILE *f)
{
    if (f == &__stdin)
    {     
        return 0;   
    }
    else    /* 文件操作, lua dofile()会执行这个分支 */
    {
        FRESULT result;
        uint32_t br = 0;
        FIL *fp;
        char buf[2];

        fp = (FIL *)f;   

        result = f_read(fp, buf, 1,  &br);
        if (result == FR_OK)
        {
            return buf[0];
        }

        return 0;
    }
}

/*
*********************************************************************************************************
*    函 数 名: fputc
*    功能说明: 重定义putc函数,这样可以使用printf函数从串口1打印输出
*    形    参: 无
*    返 回 值: 无
*********************************************************************************************************
*/
int fputc(int ch, FILE *f)
{
    HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
    return 0;
}


time_t time(time_t *timer)
{

    return HAL_GetTick();
}

void exit(int status)
{
    //  return 1;
}

int system(const char *filename)
{
    return 0;
}

测试方法:
main.c 中添加

/* USER CODE BEGIN 0 */
static int lua_led_on(lua_State *L)
{
    HAL_GPIO_WritePin(LEDG_GPIO_Port, LEDG_Pin, GPIO_PIN_SET);
    return 1;
}
static int lua_led_off(lua_State *L)
{
    HAL_GPIO_WritePin(LEDG_GPIO_Port, LEDG_Pin, GPIO_PIN_RESET);
    return 1;
}

static int lua_delay(lua_State *L)
{
    int num;
    num = lua_tointeger(L, 1);
    HAL_Delay(num);
    return 1;
}

static const struct luaL_Reg mylib[] =
    {
        {"led_on", lua_led_on},
        {"led_off", lua_led_off},
        {"delay", lua_delay},
        {NULL, NULL}};


void Lua_Test(void)
{
    lua_State *L;
    L = luaL_newstate(); /* 建立Lua运行环境 */
    //luaL_openlibs(L); //
    luaopen_base(L);            //注册基础函数
    luaL_setfuncs(L, mylib, 0); //注册自定义函数

    //文件程序自动执行,已测试OK
    char strProgramName[] = "0:/lua.lua";

    if (luaL_dofile(L, strProgramName))
        printf("Lua文件错误或不存在!\r\n");

    lua_close(L);/* 关闭Lua 并释放内存 */
}
/* USER CODE END 0 */

main函数中添加:

int main(void)
{
  /* USER CODE BEGIN 1 */
  uint32_t cnt =0;
  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  MX_GPIO_Init();
  MX_USART1_UART_Init();
  MX_FATFS_Init();
  MX_QUADSPI_Init();
  MX_SPI2_Init();
#if 0
  /* usb初始化时会打断程序运行,需要根据实际使用情况修改 */
  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_USART1_UART_Init();
  MX_FATFS_Init();
  MX_QUADSPI_Init();
  MX_SPI2_Init();
  MX_USB_DEVICE_Init();
  /* USER CODE BEGIN 2 */
#endif
  Lua_Test();
  MX_USB_DEVICE_Init();
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    
    HAL_GPIO_TogglePin(LEDR_GPIO_Port, LEDR_Pin);
    HAL_Delay(100);
    cnt++;
    if (cnt == 25)
    {
      printf("This is sram \r\n");
      cnt = 0;
    }
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

编译下载运行:
此时仅有 LEDR 快速闪烁,串口打印如下信息:

Lua文件错误或不存在!
This is sram
This is sram

同时PC上会提示发现u盘。

准备 lua.lua文件,内容如下:
(实现 LEDG 500ms闪烁 5次)

off = 500
on = 500
i = 0
while i < 5 do
i = i+1
led_on()
delay(off)
led_off()
delay(on)
end 

将文件考入U盘。
基于cubemx 的 h750vb 移植LUA解释器

重新上电后,即可看见运行效果,先 LEDG 闪烁 5次,然后 LEDR 开始快速闪烁。
至此移植完成。

上一篇:基于PWM用stm32f103c8t6+L298N+CubeMX控制电机转速


下一篇:STM32H743+CubeMX-ADC+DMA采样三路模拟量(并行),没过采样。