1
0
mirror of https://github.com/elua/elua.git synced 2025-01-25 01:02:54 +08:00

- added the "CPU" module. its role: access data directly (read/write) for 32/16/8 bits variables, and also provide access to CPU constants (should be defined in platform_cpu.h, see lm3s code)

- the "clock" method is now part of the CPU module, thus is out of the platform data module. pd becomes a fully platform independent module.
This commit is contained in:
Bogdan Marinescu 2008-09-26 20:41:11 +00:00
parent a10ac9c656
commit d2c5756200
14 changed files with 250 additions and 28 deletions

View File

@ -128,7 +128,7 @@ uip_files = " src/elua_uip.c " + " ".join( [ "src/uip/%s" % name for name in uip
local_include = local_include + " -Isrc/uip"
# Lua module files
module_names = "pio.c spi.c tmr.c pd.c uart.c term.c pwm.c lpack.c bit.c net.c"
module_names = "pio.c spi.c tmr.c pd.c uart.c term.c pwm.c lpack.c bit.c net.c cpu.c"
module_files = " " + " ".join( [ "src/modules/%s" % name for name in module_names.split() ] )
# Optimizer flags (speed or size)

View File

@ -169,6 +169,7 @@ u32 platform_pwm_op( unsigned id, int op, u32 data );
void platform_cpu_enable_interrupts();
void platform_cpu_disable_interrupts();
u32 platform_cpu_get_frequency();
// *****************************************************************************
// Ethernet specific functions
@ -178,12 +179,6 @@ u32 platform_eth_get_packet_nb( void* buf, u32 maxlen );
void platform_eth_force_interrupt();
u32 platform_eth_get_elapsed_time();
// *****************************************************************************
// Platform data subsection
// Contains functions for platform identification
u32 platform_pd_get_cpu_frequency();
// *****************************************************************************
// Allocator support

View File

@ -36,6 +36,9 @@ LUALIB_API int ( luaopen_bit )( lua_State *L );
#define AUXLIB_NET "net"
LUALIB_API int ( luaopen_net )( lua_State *L );
#define AUXLIB_CPU "cpu"
LUALIB_API int ( luaopen_cpu )( lua_State* L );
// Helper macros
#define MOD_CHECK_ID( mod, id )\

156
src/modules/cpu.c Normal file
View File

@ -0,0 +1,156 @@
// Module for interfacing with CPU
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include "platform.h"
#include "auxmods.h"
#include <string.h>
#define _C( x ) { #x, x }
#include "platform_cpu.h"
// Lua: w32( address, data )
static int cpu_w32( lua_State *L )
{
u32 addr = ( u32 )luaL_checkinteger( L, 1 );
u32 data = ( u32 )luaL_checkinteger( L, 2 );
*( u32* )addr = data;
return 0;
}
// Lua: data = r32( address )
static int cpu_r32( lua_State *L )
{
u32 addr = ( u32 )luaL_checkinteger( L, 1 );
lua_pushinteger( L, ( lua_Integer )( *( u32* )addr ) );
return 1;
}
// Lua: w16( address, data )
static int cpu_w16( lua_State *L )
{
u32 addr = ( u32 )luaL_checkinteger( L, 1 );
u16 data = ( u16 )luaL_checkinteger( L, 2 );
*( u16* )addr = data;
return 0;
}
// Lua: data = r16( address )
static int cpu_r16( lua_State *L )
{
u32 addr = ( u32 )luaL_checkinteger( L, 1 );
lua_pushinteger( L, ( lua_Integer )( *( u16* )addr ) );
return 1;
}
// Lua: w8( address, data )
static int cpu_w8( lua_State *L )
{
u32 addr = ( u32 )luaL_checkinteger( L, 1 );
u8 data = ( u8 )luaL_checkinteger( L, 2 );
*( u8* )addr = data;
return 0;
}
// Lua: data = r8( address )
static int cpu_r8( lua_State *L )
{
u32 addr = ( u32 )luaL_checkinteger( L, 1 );
lua_pushinteger( L, ( lua_Integer )( *( u8* )addr ) );
return 1;
}
// Lua: cli()
static int cpu_cli( lua_State *L )
{
platform_cpu_disable_interrupts();
return 0;
}
// Lua: sei()
static int cpu_sei( lua_State *L )
{
platform_cpu_enable_interrupts();
return 0;
}
// Lua: frequency = clock()
static int cpu_clock( lua_State *L )
{
lua_pushinteger( L, platform_cpu_get_frequency() );
return 1;
}
// CPU constants list
typedef struct
{
const char* name;
u32 val;
} cpu_const_t;
static const cpu_const_t cpu_constants[] =
{
#ifdef PLATFORM_CPU_CONSTANTS
PLATFORM_CPU_CONSTANTS,
#endif
{ NULL, 0 }
};
static int cpu_mt_index( lua_State *L )
{
const char *key = luaL_checkstring( L, 2 );
unsigned i = 0;
while( cpu_constants[ i ].name != NULL )
{
if( !strcmp( cpu_constants[ i ].name, key ) )
{
lua_pushinteger( L, cpu_constants[ i ].val );
return 1;
}
i ++;
}
return 0;
}
// Metatable data
static const luaL_reg cpu_mt_map[] =
{
{ "__index", cpu_mt_index },
{ NULL, NULL }
};
// Module function map
static const luaL_reg cpu_map[] =
{
{ "w32", cpu_w32 },
{ "r32", cpu_r32 },
{ "w16", cpu_w16 },
{ "r16", cpu_r16 },
{ "w8", cpu_w8 },
{ "r8", cpu_r8 },
{ "cli", cpu_cli },
{ "sei", cpu_sei },
{ "clock", cpu_clock },
{ NULL, NULL }
};
LUALIB_API int luaopen_cpu( lua_State *L )
{
// Register methods
luaL_register( L, AUXLIB_CPU, cpu_map );
// Create and set metatable
lua_newtable( L );
luaL_register( L, NULL, cpu_mt_map );
lua_setmetatable( L, -2 );
return 1;
}

View File

@ -29,12 +29,6 @@ static int pd_board( lua_State* L )
lua_pushstring( L, MACRO_NAME( ELUA_BOARD ) );
return 1;
}
// Lua: speed = frequency()
static int pd_clock( lua_State* L )
{
lua_pushinteger( L, platform_pd_get_cpu_frequency() );
return 1;
}
// Module function map
static const luaL_reg pd_map[] =
@ -42,7 +36,6 @@ static const luaL_reg pd_map[] =
{ "platform", pd_platform },
{ "cpu", pd_cpu },
{ "board", pd_board },
{ "clock", pd_clock },
{ NULL, NULL }
};

View File

@ -473,9 +473,9 @@ u32 platform_pwm_op( unsigned id, int op, u32 data )
}
// ****************************************************************************
// Platform data functions
// CPU functions
u32 platform_pd_get_cpu_frequency()
u32 platform_cpu_get_frequency()
{
return BOARD_MCK;
}

View File

@ -0,0 +1,6 @@
// CPU related constants that should be visible to Lua
#ifndef __PLATFORM_CPU_H__
#define __PLATFORM_CPU_H__
#endif

View File

@ -560,6 +560,11 @@ void platform_cpu_disable_interrupts()
IntMasterDisable();
}
u32 platform_cpu_get_frequency()
{
return SysCtlClockGet();
}
// ****************************************************************************
// Ethernet functions
@ -698,14 +703,6 @@ void EthernetIntHandler()
}
#endif // #ifdef ELUA_UIP
// ****************************************************************************
// Platform data
u32 platform_pd_get_cpu_frequency()
{
return SysCtlClockGet();
}
// ****************************************************************************
// Allocator support

View File

@ -0,0 +1,59 @@
// CPU related constants that should be visible to Lua
#ifndef __PLATFORM_CPU_H__
#define __PLATFORM_CPU_H__
#include "hw_ints.h"
#define PLATFORM_CPU_CONSTANTS\
_C( INT_GPIOA ),\
_C( INT_GPIOB ),\
_C( INT_GPIOC ),\
_C( INT_GPIOD ),\
_C( INT_GPIOE ),\
_C( INT_UART0 ),\
_C( INT_UART1 ),\
_C( INT_SSI0 ),\
_C( INT_I2C0 ),\
_C( INT_PWM_FAULT ),\
_C( INT_PWM0 ),\
_C( INT_PWM1 ),\
_C( INT_PWM2 ),\
_C( INT_QEI0 ),\
_C( INT_ADC0 ),\
_C( INT_ADC1 ),\
_C( INT_ADC2 ),\
_C( INT_ADC3 ),\
_C( INT_WATCHDOG ),\
_C( INT_TIMER0A ),\
_C( INT_TIMER0B ),\
_C( INT_TIMER1A ),\
_C( INT_TIMER1B ),\
_C( INT_TIMER2A ),\
_C( INT_TIMER2B ),\
_C( INT_COMP0 ),\
_C( INT_COMP1 ),\
_C( INT_COMP2 ),\
_C( INT_SYSCTL ),\
_C( INT_FLASH ),\
_C( INT_GPIOF ),\
_C( INT_GPIOG ),\
_C( INT_GPIOH ),\
_C( INT_UART2 ),\
_C( INT_SSI1 ),\
_C( INT_TIMER3A ),\
_C( INT_TIMER3B ),\
_C( INT_I2C1 ),\
_C( INT_QEI1 ),\
_C( INT_CAN0 ),\
_C( INT_CAN1 ),\
_C( INT_CAN2 ),\
_C( INT_ETH ),\
_C( INT_HIBERNATE ),\
_C( INT_USB0 ),\
_C( INT_PWM3 ),\
_C( INT_UDMA ),\
_C( INT_UDMAERR )
#endif

View File

@ -16,6 +16,7 @@
{ AUXLIB_PACK, luaopen_pack },\
{ AUXLIB_BIT, luaopen_bit },\
{ AUXLIB_NET, luaopen_net },\
{ AUXLIB_CPU, luaopen_cpu },\
{ LUA_MATHLIBNAME, luaopen_math }
#endif

View File

@ -286,9 +286,9 @@ u32 platform_timer_get_diff_us( unsigned id, timer_data_type end, timer_data_typ
}
// ****************************************************************************
// Platform data functions
// CPU functions
u32 platform_pd_get_cpu_frequency()
u32 platform_cpu_get_frequency()
{
return Fcclk;
}

View File

@ -0,0 +1,6 @@
// CPU related constants that should be visible to Lua
#ifndef __PLATFORM_CPU_H__
#define __PLATFORM_CPU_H__
#endif

View File

@ -404,9 +404,9 @@ u32 platform_timer_get_diff_us( unsigned id, timer_data_type end, timer_data_typ
}
// ****************************************************************************
// Platform data functions
// CPU functions
u32 platform_pd_get_cpu_frequency()
u32 platform_cpu_get_frequency()
{
return SCU_GetMCLKFreqValue() * 1000;
}

View File

@ -0,0 +1,6 @@
// CPU related constants that should be visible to Lua
#ifndef __PLATFORM_CPU_H__
#define __PLATFORM_CPU_H__
#endif