From d2c5756200647dd2e80b3069089adb721659c6f3 Mon Sep 17 00:00:00 2001 From: Bogdan Marinescu Date: Fri, 26 Sep 2008 20:41:11 +0000 Subject: [PATCH] - 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. --- SConstruct | 2 +- inc/platform.h | 7 +- src/modules/auxmods.h | 3 + src/modules/cpu.c | 156 ++++++++++++++++++++++++++ src/modules/pd.c | 7 -- src/platform/at91sam7x/platform.c | 4 +- src/platform/at91sam7x/platform_cpu.h | 6 + src/platform/lm3s/platform.c | 13 +-- src/platform/lm3s/platform_cpu.h | 59 ++++++++++ src/platform/lm3s/platform_libs.h | 1 + src/platform/lpc288x/platform.c | 4 +- src/platform/lpc288x/platform_cpu.h | 6 + src/platform/str9/platform.c | 4 +- src/platform/str9/platform_cpu.h | 6 + 14 files changed, 250 insertions(+), 28 deletions(-) create mode 100644 src/modules/cpu.c create mode 100644 src/platform/at91sam7x/platform_cpu.h create mode 100644 src/platform/lm3s/platform_cpu.h create mode 100644 src/platform/lpc288x/platform_cpu.h create mode 100644 src/platform/str9/platform_cpu.h diff --git a/SConstruct b/SConstruct index 2a7124ba..a20ed611 100644 --- a/SConstruct +++ b/SConstruct @@ -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) diff --git a/inc/platform.h b/inc/platform.h index ec9d0b7e..4bb6053e 100644 --- a/inc/platform.h +++ b/inc/platform.h @@ -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 diff --git a/src/modules/auxmods.h b/src/modules/auxmods.h index 287cf0db..f0e04f9a 100644 --- a/src/modules/auxmods.h +++ b/src/modules/auxmods.h @@ -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 )\ diff --git a/src/modules/cpu.c b/src/modules/cpu.c new file mode 100644 index 00000000..16d0dba4 --- /dev/null +++ b/src/modules/cpu.c @@ -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 + +#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; +} diff --git a/src/modules/pd.c b/src/modules/pd.c index cfc90c23..578fd9a4 100644 --- a/src/modules/pd.c +++ b/src/modules/pd.c @@ -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 } }; diff --git a/src/platform/at91sam7x/platform.c b/src/platform/at91sam7x/platform.c index 85772dc4..edbb146b 100644 --- a/src/platform/at91sam7x/platform.c +++ b/src/platform/at91sam7x/platform.c @@ -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; } diff --git a/src/platform/at91sam7x/platform_cpu.h b/src/platform/at91sam7x/platform_cpu.h new file mode 100644 index 00000000..5d575e86 --- /dev/null +++ b/src/platform/at91sam7x/platform_cpu.h @@ -0,0 +1,6 @@ +// CPU related constants that should be visible to Lua + +#ifndef __PLATFORM_CPU_H__ +#define __PLATFORM_CPU_H__ + +#endif diff --git a/src/platform/lm3s/platform.c b/src/platform/lm3s/platform.c index 1500d21d..2d254fc9 100644 --- a/src/platform/lm3s/platform.c +++ b/src/platform/lm3s/platform.c @@ -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 diff --git a/src/platform/lm3s/platform_cpu.h b/src/platform/lm3s/platform_cpu.h new file mode 100644 index 00000000..b513ebd8 --- /dev/null +++ b/src/platform/lm3s/platform_cpu.h @@ -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 + diff --git a/src/platform/lm3s/platform_libs.h b/src/platform/lm3s/platform_libs.h index a307b2b9..349e86b5 100644 --- a/src/platform/lm3s/platform_libs.h +++ b/src/platform/lm3s/platform_libs.h @@ -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 diff --git a/src/platform/lpc288x/platform.c b/src/platform/lpc288x/platform.c index 087d7f2a..a1068ca6 100644 --- a/src/platform/lpc288x/platform.c +++ b/src/platform/lpc288x/platform.c @@ -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; } diff --git a/src/platform/lpc288x/platform_cpu.h b/src/platform/lpc288x/platform_cpu.h new file mode 100644 index 00000000..5d575e86 --- /dev/null +++ b/src/platform/lpc288x/platform_cpu.h @@ -0,0 +1,6 @@ +// CPU related constants that should be visible to Lua + +#ifndef __PLATFORM_CPU_H__ +#define __PLATFORM_CPU_H__ + +#endif diff --git a/src/platform/str9/platform.c b/src/platform/str9/platform.c index 07ba95dc..1c61ba95 100644 --- a/src/platform/str9/platform.c +++ b/src/platform/str9/platform.c @@ -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; } diff --git a/src/platform/str9/platform_cpu.h b/src/platform/str9/platform_cpu.h new file mode 100644 index 00000000..5d575e86 --- /dev/null +++ b/src/platform/str9/platform_cpu.h @@ -0,0 +1,6 @@ +// CPU related constants that should be visible to Lua + +#ifndef __PLATFORM_CPU_H__ +#define __PLATFORM_CPU_H__ + +#endif