static int term_in( int mode ) { if( mode == TERM_INPUT_DONT_WAIT ) return platform_uart_recv( CON_UART_ID, CON_TIMER_ID, 0 ); else return platform_uart_recv( CON_UART_ID, CON_TIMER_ID, PLATFORM_UART_INFINITE_TIMEOUT ); }
static int term_translate( int data ) { int c; if( isprint( data ) ) return data; else if( data == 0x1B ) // escape sequence { // If we don't get a second char, we got a simple "ESC", so return KC_ESC // If we get a second char it must be '[', the next one is relevant for us if( platform_uart_recv( CON_UART_ID, CON_TIMER_ID, TERM_TIMEOUT ) == -1 ) return KC_ESC; if( ( c = platform_uart_recv( CON_UART_ID, CON_TIMER_ID, TERM_TIMEOUT ) ) == -1 ) return KC_UNKNOWN; switch( c ) { case 0x41: return KC_UP; case 0x42: return KC_DOWN; case 0x43: return KC_RIGHT; case 0x44: return KC_LEFT; } } else if( data == 0x0D ) { // CR/LF sequence, read the second char (LF) if applicable platform_uart_recv( CON_UART_ID, CON_TIMER_ID, TERM_TIMEOUT ); return KC_ENTER; } else { switch( data ) { case 0x09: return KC_TAB; case 0x16: return KC_PAGEDOWN; case 0x15: return KC_PAGEUP; case 0x05: return KC_END; case 0x01: return KC_HOME; case 0x7F: case 0x08: return KC_BACKSPACE; } } return KC_UNKNOWN; }
// Lua: data = getchar( id, [ timeout ], [ timer_id ] ) static int uart_getchar( lua_State* L ) { int id, res; char cres; unsigned timer_id = 0; s32 timeout = PLATFORM_UART_INFINITE_TIMEOUT; id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( uart, id ); // Check timeout and timer id if( lua_gettop( L ) >= 2 ) { timeout = luaL_checkinteger( L, 2 ); if( ( timeout < 0 ) && ( timeout != PLATFORM_UART_INFINITE_TIMEOUT ) ) return luaL_error( L, "invalid timeout value" ); if( ( timeout != PLATFORM_UART_INFINITE_TIMEOUT ) && ( timeout != 0 ) ) timer_id = luaL_checkinteger( L, 3 ); } res = platform_uart_recv( id, timer_id, timeout ); if( res == -1 ) lua_pushstring( L, "" ); else { cres = ( char )res; lua_pushlstring( L, &cres, 1 ); } return 1; }
void transport_read_buffer( Transport *tpt, u8 *buffer, int length ) { int n = 0; int c; struct exception e; timer_data_type uart_timeout = PLATFORM_TIMER_INF_TIMEOUT; // not sure whether we should always follow this // int uart_timeout = 100000; while( n < length ) { TRANSPORT_VERIFY_OPEN; if ( adispatch_buff < 0 ) c = platform_uart_recv( tpt->fd, tpt->tmr_id, uart_timeout ); else { c = adispatch_buff; adispatch_buff = -1; } if( c < 0 ) { // uart_timeout = 1000000; // Reset and use timeout of 1s e.errnum = ERR_NODATA; e.type = nonfatal; Throw( e ); } else { buffer[ n ] = ( u8 )c; n++; } // After getting one char of a read remainder // should follow within a timeout of 0.1 sec uart_timeout = 100000; } }
// Helper: ask yes/no // Returns 1 for yes, 0 for no int shellh_ask_yes_no( const char *prompt ) { int c; if( prompt ) printf( "%s ", prompt ); while( 1 ) { #ifdef BUILD_TERM c = term_getch( TERM_INPUT_WAIT ); #else c = platform_uart_recv( CON_UART_ID, 0 /*UNUSED*/, PLATFORM_TIMER_INF_TIMEOUT ); #endif if( c == 'y' || c == 'Y' ) { printf( "y\n" ); return 1; } if( c == 'n' || c == 'N' ) { printf( "n\n" ); return 0; } } // Will never get here return 0; }
void transport_read_buffer (Transport *tpt, u8 *buffer, int length) { int n = 0; int c; struct exception e; while( n < length ) { TRANSPORT_VERIFY_OPEN; c = platform_uart_recv( CON_UART_ID, CON_TIMER_ID, uart_timeout ); if( c < 0 ) { uart_timeout = 1000000; /* Reset and use timeout of 1s */ e.errnum = ERR_NODATA; e.type = nonfatal; Throw( e ); } else { buffer[ n ] = ( u8 ) c; n++; } /* After getting one char of a read, remainder should follow within a timeout of 0.1 sec */ uart_timeout = 100000; } uart_timeout = PLATFORM_UART_INFINITE_TIMEOUT; }
// Lua: data = recv( id, timer_id, timeout ) static int uart_recv( lua_State* L ) { int id, timer_id, timeout, res; id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( uart, id ); timer_id = luaL_checkinteger( L, 2 ); timeout = luaL_checkinteger( L, 3 ); res = platform_uart_recv( id, timer_id, timeout ); lua_pushinteger( L, res ); return 1; }
void nrf_ll_init( void ) { // GPIO configuration nrf_ll_ce_low(); nrf_ll_csn_high(); platform_pio_op( NRF24L01_CE_PORT, 1 << NRF24L01_CE_PIN, PLATFORM_IO_PIN_DIR_OUTPUT ); platform_pio_op( NRF24L01_CSN_PORT, 1 << NRF24L01_CSN_PIN, PLATFORM_IO_PIN_DIR_OUTPUT ); platform_pio_op( NRF24L01_IRQ_PORT, 1 << NRF24L01_IRQ_PIN, PLATFORM_IO_PIN_DIR_INPUT ); // UART configuration is already done in platform.c // Empty UART buffer while( platform_uart_recv( NRF24L01_UART_ID, 0, 0 ) != -1 ); }
void nrf_ll_flush( u16 len ) { int data; unsigned i; for( i = 0; i < len; i ++ ) { data = platform_uart_recv( NRF24L01_UART_ID, 0, NRF24L01_TIMEOUT ); if( data == -1 ) printf( "INVALID DATA RECEIVED ON NRF_LL_FLUSH!\n" ); } }
void nrf_ll_read_packet( u8 *packet, u16 len ) { int data; unsigned i; for( i = 0; i < len; i ++ ) { platform_uart_send( NRF24L01_UART_ID, 0xFF ); data = platform_uart_recv( NRF24L01_UART_ID, 0, NRF24L01_TIMEOUT ); if( data == -1 ) printf( "INVALID DATA RECEIVED ON NRF_LL_READ_PACKET!\n" ); packet[ i ] = data; } }
// Lua: data = getchar( id, [ timeout ], [ timer_id ] ) static int uart_getchar( lua_State* L ) { int id, res; char cres; unsigned timer_id = PLATFORM_TIMER_SYS_ID; timer_data_type timeout = PLATFORM_TIMER_INF_TIMEOUT; id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( uart, id ); // Check timeout and timer id uart_get_timeout_data( L, 2, &timeout, &timer_id ); res = platform_uart_recv( id, timer_id, timeout ); if( res == -1 ) lua_pushstring( L, "" ); else { cres = ( char )res; lua_pushlstring( L, &cres, 1 ); } return 1; }
// Lua: uart.read( id, format, [timeout], [timer_id] ) static int uart_read( lua_State* L ) { int id, res, mode, issign; unsigned timer_id = PLATFORM_TIMER_SYS_ID; s32 maxsize = 0, count = 0; const char *fmt; luaL_Buffer b; char cres; timer_data_type timeout = PLATFORM_TIMER_INF_TIMEOUT; id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( uart, id ); // Check format if( lua_isnumber( L, 2 ) ) { if( ( maxsize = ( s32 )lua_tointeger( L, 2 ) ) < 0 ) return luaL_error( L, "invalid max size" ); mode = UART_READ_MODE_MAXSIZE; } else { fmt = luaL_checkstring( L, 2 ); if( !strcmp( fmt, "*l" ) ) mode = UART_READ_MODE_LINE; else if( !strcmp( fmt, "*n" ) ) mode = UART_READ_MODE_NUMBER; else if( !strcmp( fmt, "*s" ) ) mode = UART_READ_MODE_SPACE; else return luaL_error( L, "invalid format" ); } // Check timeout and timer id uart_get_timeout_data( L, 3, &timeout, &timer_id ); // Read data luaL_buffinit( L, &b ); while( 1 ) { if( ( res = platform_uart_recv( id, timer_id, timeout ) ) == -1 ) break; cres = ( char )res; count ++; issign = ( count == 1 ) && ( ( res == '-' ) || ( res == '+' ) ); // [TODO] this only works for lines that actually end with '\n', other line endings // are not supported. if( ( cres == '\n' ) && ( mode == UART_READ_MODE_LINE ) ) break; if( !isdigit( (unsigned char) cres ) && !issign && ( mode == UART_READ_MODE_NUMBER ) ) break; if( isspace( (unsigned char) cres ) && ( mode == UART_READ_MODE_SPACE ) ) break; luaL_putchar( &b, cres ); if( ( count == maxsize ) && ( mode == UART_READ_MODE_MAXSIZE ) ) break; } luaL_pushresult( &b ); // Return an integer if needed if( mode == UART_READ_MODE_NUMBER ) { res = lua_tointeger( L, -1 ); lua_pop( L, 1 ); lua_pushinteger( L, res ); } return 1; }
static int uart_read( lua_State* L ) { int id, res, mode, issign; unsigned timer_id = 0; s32 timeout = PLATFORM_UART_INFINITE_TIMEOUT, maxsize = 0, count = 0; const char *fmt; luaL_Buffer b; char cres; id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( uart, id ); // Check format if( lua_isnumber( L, 2 ) ) { if( ( maxsize = ( s32 )lua_tointeger( L, 2 ) ) < 0 ) return luaL_error( L, "invalid max size" ); mode = UART_READ_MODE_MAXSIZE; } else { fmt = luaL_checkstring( L, 2 ); if( !strcmp( fmt, "*l" ) ) mode = UART_READ_MODE_LINE; else if( !strcmp( fmt, "*n" ) ) mode = UART_READ_MODE_NUMBER; else if( !strcmp( fmt, "*s" ) ) mode = UART_READ_MODE_SPACE; else return luaL_error( L, "invalid format" ); } // Check timeout and timer id if( lua_gettop( L ) >= 3 ) { timeout = luaL_checkinteger( L, 3 ); if( ( timeout < 0 ) && ( timeout != PLATFORM_UART_INFINITE_TIMEOUT ) ) return luaL_error( L, "invalid timeout value" ); if( ( timeout != PLATFORM_UART_INFINITE_TIMEOUT ) && ( timeout != 0 ) ) timer_id = luaL_checkinteger( L, 4 ); } // Read data luaL_buffinit( L, &b ); while( 1 ) { if( ( res = platform_uart_recv( id, timer_id, timeout ) ) == -1 ) break; cres = ( char )res; count ++; issign = ( count == 1 ) && ( ( res == '-' ) || ( res == '+' ) ); if( ( cres == '\n' ) && ( mode == UART_READ_MODE_LINE ) ) break; if( !isdigit( cres ) && !issign && ( mode == UART_READ_MODE_NUMBER ) ) break; if( isspace( cres ) && ( mode == UART_READ_MODE_SPACE ) ) break; luaL_putchar( &b, cres ); if( ( count == maxsize ) && ( mode == UART_READ_MODE_MAXSIZE ) ) break; } luaL_pushresult( &b ); // Return an integer if needed if( mode == UART_READ_MODE_NUMBER ) { res = lua_tointeger( L, -1 ); lua_pop( L, 1 ); lua_pushinteger( L, res ); } return 1; }
static int uart_recv() { return platform_uart_recv( 0, 0, PLATFORM_UART_INFINITE_TIMEOUT ); }
static int xmodem_recv( u32 timeout ) { return platform_uart_recv( XMODEM_UART_ID, XMODEM_TIMER_ID, timeout ); }
static int uart_recv( s32 to ) { return platform_uart_recv( CON_UART_ID, CON_TIMER_ID, to ); }
// Read a char from serial buffer int transport_get_char(Transport *t) { return platform_uart_recv( t->fd, t->tmr_id, 0 ); }
static int xmodem_recv( u32 timeout ) { return platform_uart_recv( CON_UART_ID, CON_TIMER_ID, timeout ); }
static int uart_recv( timer_data_type to ) { return platform_uart_recv( CON_UART_ID, CON_TIMER_ID, to ); }
static int term_translate( int data ) { int c; if( isprint( data ) ) return data; else if( data == 0x1B ) // escape sequence { // If we don't get a second char, we got a simple "ESC", so return KC_ESC // If we get a second char it must be '[', the next one is relevant for us if( platform_uart_recv( CON_UART_ID, CON_TIMER_ID, TERM_TIMEOUT ) == -1 ) return KC_ESC; if( ( c = platform_uart_recv( CON_UART_ID, CON_TIMER_ID, TERM_TIMEOUT ) ) == -1 ) return KC_UNKNOWN; if( c >= 0x41 && c <= 0x44 ) switch( c ) { case 0x41: return KC_UP; case 0x42: return KC_DOWN; case 0x43: return KC_RIGHT; case 0x44: return KC_LEFT; } else if( c > 48 && c < 55 ) { // Extended sequence: read another byte if( platform_uart_recv( CON_UART_ID, CON_TIMER_ID, TERM_TIMEOUT ) != 126 ) return KC_UNKNOWN; switch( c ) { case 49: return KC_HOME; case 52: return KC_END; case 53: return KC_PAGEUP; case 54: return KC_PAGEDOWN; } } } else if( data == 0x0D ) { // CR/LF sequence, read the second char (LF) if applicable platform_uart_recv( CON_UART_ID, CON_TIMER_ID, TERM_TIMEOUT ); return KC_ENTER; } else { switch( data ) { case 0x09: return KC_TAB; case 0x7F: // return KC_DEL; // bogdanm: some terminal emulators (for example screen) return 0x7F for BACKSPACE :( case 0x08: return KC_BACKSPACE; case 26: return KC_CTRL_Z; case 1: return KC_CTRL_A; case 5: return KC_CTRL_E; case 3: return KC_CTRL_C; case 20: return KC_CTRL_T; case 21: return KC_CTRL_U; case 11: return KC_CTRL_K; } } return KC_UNKNOWN; }
static int xmodem_recv( timer_data_type timeout ) { return platform_uart_recv( CON_UART_ID, CON_TIMER_ID, timeout ); }