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

Put more SERMUX code under #ifdef BUILD_SERMUX

Saves 184 bytes in code segment when !BUILD_SERMUX by replacing constant
runtime checks with compile-time ones.
This commit is contained in:
Martin Guy 2011-04-25 17:16:05 +02:00
parent dfcde5bb47
commit 02cc57c538
2 changed files with 29 additions and 7 deletions

View File

@ -16,8 +16,6 @@ int uart_service_id_out = -1;
u8 uart_got_esc = 0;
int uart_last_sent = -1;
// [TODO] add interrupt support for virtual UARTs
#else // #ifdef BUILD_SERMUX
#define SERMUX_PHYS_ID ( 0xFFFF )
#endif // #ifdef BUILD_SERMUX
// The platform UART functions
@ -148,7 +146,11 @@ static elua_int_c_handler prev_uart_rx_handler;
static void cmn_uart_rx_inthandler( elua_int_resnum resnum )
{
if( buf_is_enabled( BUF_ID_UART, resnum ) || resnum == SERMUX_PHYS_ID )
if( buf_is_enabled( BUF_ID_UART, resnum )
#ifdef BUILD_SERMUX
|| resnum == SERMUX_PHYS_ID
#endif // #ifdef BUILD_SERMUX
)
cmn_rx_handler( resnum, platform_s_uart_recv( resnum, 0 ) );
// Chain to previous handler
@ -159,13 +161,21 @@ static void cmn_uart_rx_inthandler( elua_int_resnum resnum )
int platform_uart_set_buffer( unsigned id, unsigned log2size )
{
if( id == SERMUX_PHYS_ID ) // mere mortals aren't allowed to mess with VUART physical interface buffering
#ifdef BUILD_SERMUX
// mere mortals aren't allowed to mess with VUART physical interface buffering
if( id == SERMUX_PHYS_ID )
return PLATFORM_ERR;
#endif // #ifdef BUILD_SERMUX
#ifdef BUF_ENABLE_UART
if( log2size == 0 )
{
if( id >= SERMUX_SERVICE_ID_FIRST ) // Virtual UARTs need buffers no matter what
#ifdef BUILD_SERMUX
// Virtual UARTs need buffers no matter what
if( id >= SERMUX_SERVICE_ID_FIRST )
return PLATFORM_ERR;
#endif // #ifdef BUILD_SERMUX
// Disable buffering
buf_set( BUF_ID_UART, id, BUF_SIZE_NONE, BUF_DSIZE_U8 );
}
@ -174,11 +184,17 @@ int platform_uart_set_buffer( unsigned id, unsigned log2size )
// Enable buffering
if( buf_set( BUF_ID_UART, id, log2size, BUF_DSIZE_U8 ) == PLATFORM_ERR )
return PLATFORM_ERR;
if( id >= SERMUX_SERVICE_ID_FIRST ) // No need for aditional setup on virtual UARTs
#ifdef BUILD_SERMUX
// No need for additional setup on virtual UARTs
if( id >= SERMUX_SERVICE_ID_FIRST )
return PLATFORM_OK;
#endif // #ifdef BUILD_SERMUX
// Enable UART RX interrupt
if( platform_cpu_set_interrupt( INT_UART_RX, id, PLATFORM_CPU_ENABLE ) != PLATFORM_INT_OK )
return PLATFORM_ERR;
// Setup our C handler
if( elua_int_get_c_handler( INT_UART_RX ) != cmn_uart_rx_inthandler )
prev_uart_rx_handler = elua_int_set_c_handler( INT_UART_RX, cmn_uart_rx_inthandler );
@ -207,8 +223,10 @@ void cmn_uart_setup_sermux()
int platform_uart_set_flow_control( unsigned id, int type )
{
#ifdef BUILD_SERMUX
if( id >= SERMUX_SERVICE_ID_FIRST )
return PLATFORM_ERR;
#endif // #ifdef BUILD_SERMUX
return platform_s_uart_set_flow_control( id, type );
}

View File

@ -30,8 +30,10 @@ static int uart_setup( lua_State* L )
id = luaL_checkinteger( L, 1 );
MOD_CHECK_ID( uart, id );
#ifdef BUILD_SERMUX
if( id >= SERMUX_SERVICE_ID_FIRST )
return luaL_error( L, "uart.setup can't be called on virtual UARTs" );
#endif
baud = luaL_checkinteger( L, 2 );
databits = luaL_checkinteger( L, 3 );
parity = luaL_checkinteger( L, 4 );
@ -187,8 +189,10 @@ static int uart_set_buffer( lua_State *L )
MOD_CHECK_ID( uart, id );
if( size && ( size & ( size - 1 ) ) )
return luaL_error( L, "the buffer size must be a power of 2 or 0" );
#ifdef BUILD_SERMUX
if( size == 0 && id >= SERMUX_SERVICE_ID_FIRST )
return luaL_error( L, "disabling buffers on virtual UARTs is not allowed" );
#endif
if( platform_uart_set_buffer( id, intlog2( size ) ) == PLATFORM_ERR )
return luaL_error( L, "unable to set UART buffer" );
return 0;