chibios_with_elua/ext/ch_lua.c

135 lines
2.7 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "platform.h"
#include "romfs.h"
#include "xmodem.h"
#include "elua/inc/shell.h"
#include "elua/src/lua/lua.h"
#include "elua/src/lua/lauxlib.h"
#include "elua/src/lua/lualib.h"
#include "term.h"
#include "platform_conf.h"
#include "elua_rfs.h"
#ifdef ELUA_SIMULATOR
#include "hostif.h"
#endif
// Validate eLua configuratin options
#include "validate.h"
#include "mmcfs.h"
#include "romfs.h"
#include "semifs.h"
// Define here your autorun/boot files,
// in the order you want eLua to search for them
const char *boot_order[] = {
#if defined(BUILD_MMCFS)
"/mmc/autorun.lua",
"/mmc/autorun.lc",
#endif
#if defined(BUILD_WOFS)
"/wo/autorun.lua",
"/wo/autorun.lc",
#endif
#if defined(BUILD_ROMFS)
"/rom/autorun.lua",
"/rom/autorun.lc",
#endif
#if defined(BUILD_SEMIFS)
"/semi/autorun.lua",
"/semi/autorun.lc",
#endif
};
extern char etext[];
#ifdef ELUA_BOOT_RPC
void boot_rpc( void )
{
lua_State *L = lua_open();
luaL_openlibs(L); /* open libraries */
// Set up UART for 8N1 w/ adjustable baud rate
platform_uart_setup( RPC_UART_ID, RPC_UART_SPEED, 8, PLATFORM_UART_PARITY_NONE, PLATFORM_UART_STOPBITS_1 );
// Start RPC Server
lua_getglobal( L, "rpc" );
lua_getfield( L, -1, "server" );
lua_pushnumber( L, RPC_UART_ID );
lua_pushnumber( L, RPC_TIMER_ID );
lua_pcall( L, 2, 0, 0 );
}
#endif // #ifdef ELUA_BOOT_RPC
// ****************************************************************************
// Program entry point
//~ int main( void )
int main_lua( void )
{
int i;
FILE* fp;
// Initialize platform first
if( platform_init() != PLATFORM_OK )
{
// This should never happen
while( 1 );
}
// Initialize device manager
dm_init();
// Register the ROM filesystem
romfs_init();
// Register the MMC filesystem
//~ mmcfs_init();
// Register the Semihosting filesystem
semifs_init();
// Register the remote filesystem
remotefs_init();
// Search for autorun files in the defined order and execute the 1st if found
for( i = 0; i < sizeof( boot_order ) / sizeof( *boot_order ); i++ )
{
//elua/src/newlib/stubs.c _open_r
if( ( fp = fopen( boot_order[ i ], "r" ) ) != NULL )
{
fclose( fp );
char* lua_argv[] = { (char *)"lua", (char *)boot_order[i], NULL };
lua_main( 2, lua_argv );
break; // autoruns only the first found
}
}
#ifdef ELUA_BOOT_RPC
boot_rpc();
#else
printf("hello\n\r");
// Run the shell
if( shell_init() == 0 )
{
// Start Lua directly
char* lua_argv[] = { (char *)"lua", NULL };
lua_main( 1, lua_argv );
}
else
shell_start();
#endif // #ifdef ELUA_BOOT_RPC
#ifdef ELUA_SIMULATOR
hostif_exit(0);
return 0;
#else
while( 1 );
#endif
}