u32 platform_s_timer_op( unsigned id, int op, u32 data ) { u32 res = 0; PREG TxTCR = ( PREG )tmr_tcr[ id ]; PREG TxTC = ( PREG )tmr_tc[ id ]; switch( op ) { case PLATFORM_TIMER_OP_START: *TxTCR = TMR_ENABLE | TMR_RESET; *TxTCR = TMR_ENABLE; break; case PLATFORM_TIMER_OP_READ: res = *TxTC; break; case PLATFORM_TIMER_OP_GET_MAX_DELAY: res = platform_timer_get_diff_us( id, 0, 0xFFFFFFFF ); break; case PLATFORM_TIMER_OP_GET_MIN_DELAY: res = platform_timer_get_diff_us( id, 0, 1 ); break; case PLATFORM_TIMER_OP_SET_CLOCK: res = platform_timer_set_clock( id, data ); break; case PLATFORM_TIMER_OP_GET_CLOCK: res = platform_timer_get_clock( id ); break; } return res; }
/*---------------------------------------------------------------------------*/ static PT_THREAD(handle_dhcp(void)) { PT_BEGIN(&s.pt); /* try_again:*/ s.state = STATE_SENDING; s.ticks = CLOCK_SECOND; do { send_discover(); s.timer_init = platform_timer_op( ELUA_DHCP_TIMER_ID, PLATFORM_TIMER_OP_START, 0 ); PT_WAIT_UNTIL(&s.pt, uip_newdata() || platform_timer_get_diff_us( ELUA_DHCP_TIMER_ID, s.timer_init, platform_timer_op( ELUA_DHCP_TIMER_ID, PLATFORM_TIMER_OP_READ, 0 ) ) >= s.ticks ); if(uip_newdata() && parse_msg() == DHCPOFFER) { uip_flags &= ~UIP_NEWDATA; s.state = STATE_OFFER_RECEIVED; break; } uip_flags &= ~UIP_NEWDATA; if(s.ticks < CLOCK_SECOND * 60) { s.ticks *= 2; } else { s.ipaddr[0] = 0; goto dhcp_failed; } } while(s.state != STATE_OFFER_RECEIVED); s.ticks = CLOCK_SECOND; do { send_request(); s.timer_init = platform_timer_op( ELUA_DHCP_TIMER_ID, PLATFORM_TIMER_OP_START, 0 ); PT_WAIT_UNTIL(&s.pt, uip_newdata() || platform_timer_get_diff_us( ELUA_DHCP_TIMER_ID, s.timer_init, platform_timer_op( ELUA_DHCP_TIMER_ID, PLATFORM_TIMER_OP_READ, 0 ) ) >= s.ticks ); if(uip_newdata() && parse_msg() == DHCPACK) { uip_flags &= ~UIP_NEWDATA; s.state = STATE_CONFIG_RECEIVED; break; } uip_flags &= ~UIP_NEWDATA; if(s.ticks <= CLOCK_SECOND * 10) { s.ticks += CLOCK_SECOND; } else { PT_RESTART(&s.pt); } } while(s.state != STATE_CONFIG_RECEIVED); dhcp_failed: dhcpc_configured(&s); /* * PT_END restarts the thread so we do this instead. Eventually we * should reacquire expired leases here. */ while(1) { PT_YIELD(&s.pt); } PT_END(&s.pt); }
// Accept a connection on the given port, return its socket id (and the IP of the remote host by side effect) int elua_accept( u16 port, unsigned timer_id, u32 to_us, elua_net_ip* pfrom ) { u32 tmrstart = 0; int old_status; if( !elua_uip_configured ) return -1; #ifdef BUILD_CON_TCP if( port == ELUA_NET_TELNET_PORT ) return -1; #endif old_status = platform_cpu_set_global_interrupts( PLATFORM_CPU_DISABLE ); uip_unlisten( htons( port ) ); uip_listen( htons( port ) ); platform_cpu_set_global_interrupts( old_status ); elua_uip_accept_sock = -1; elua_uip_accept_request = 1; if( to_us > 0 ) tmrstart = platform_timer_op( timer_id, PLATFORM_TIMER_OP_START, 0 ); while( 1 ) { if( elua_uip_accept_request == 0 ) break; if( to_us > 0 && platform_timer_get_diff_us( timer_id, tmrstart, platform_timer_op( timer_id, PLATFORM_TIMER_OP_READ, 0 ) ) >= to_us ) { elua_uip_accept_request = 0; break; } } *pfrom = elua_uip_accept_remote; return elua_uip_accept_sock; }
// Internal "read" function static elua_net_size elua_net_recv_internal( int s, void* buf, elua_net_size maxsize, s16 readto, unsigned timer_id, u32 to_us, int with_buffer ) { volatile struct elua_uip_state *pstate = ( volatile struct elua_uip_state* )&( uip_conns[ s ].appstate ); u32 tmrstart = 0; int old_status; if( !ELUA_UIP_IS_SOCK_OK( s ) || !uip_conn_active( s ) ) return -1; if( maxsize == 0 ) return 0; elua_prep_socket_state( pstate, buf, maxsize, readto, with_buffer, ELUA_UIP_STATE_RECV ); if( to_us > 0 ) tmrstart = platform_timer_op( timer_id, PLATFORM_TIMER_OP_START, 0 ); while( 1 ) { if( pstate->state == ELUA_UIP_STATE_IDLE ) break; if( to_us > 0 && platform_timer_get_diff_us( timer_id, tmrstart, platform_timer_op( timer_id, PLATFORM_TIMER_OP_READ, 0 ) ) >= to_us ) { old_status = platform_cpu_set_global_interrupts( PLATFORM_CPU_DISABLE ); if( pstate->state != ELUA_UIP_STATE_IDLE ) { pstate->res = ELUA_NET_ERR_TIMEDOUT; pstate->state = ELUA_UIP_STATE_IDLE; } platform_cpu_set_global_interrupts( old_status ); break; } } return maxsize - pstate->len; }
int platform_uart_recv( unsigned id, unsigned timer_id, int timeout ) { u32 base = uart_base[ id ]; timer_data_type tmr_start, tmr_crt; int res; if( timeout == 0 ) { return UARTCharGetNonBlocking( base ); } else if( timeout == PLATFORM_UART_INFINITE_TIMEOUT ) { // Receive char blocking return UARTCharGet( base ); } else { // Receive char with the specified timeout tmr_start = platform_timer_op( timer_id, PLATFORM_TIMER_OP_START, 0 ); while( 1 ) { if( ( res = UARTCharGetNonBlocking( base ) ) >= 0 ) break; tmr_crt = platform_timer_op( timer_id, PLATFORM_TIMER_OP_READ, 0 ); if( platform_timer_get_diff_us( timer_id, tmr_crt, tmr_start ) >= timeout ) break; } return res; } }
int platform_uart_recv( unsigned id, unsigned timer_id, int timeout ) { timer_data_type tmr_start, tmr_crt; int res; if( timeout == 0 ) { // Return data only if already available return uart_read_nb(); } else if( timeout == PLATFORM_UART_INFINITE_TIMEOUT ) { // Wait for data return uart_read(); } else { // Receive char with the specified timeout tmr_start = platform_timer_op( timer_id, PLATFORM_TIMER_OP_START,0 ); while( 1 ) { if( ( res = uart_read_nb() ) > 0 ) break; tmr_crt = platform_timer_op( timer_id, PLATFORM_TIMER_OP_READ, 0 ); if( platform_timer_get_diff_us( timer_id, tmr_crt, tmr_start ) >= timeout ) break; } return res; } }
int ps2_get_rawkey( s32 timeout ) { timer_data_type tmr_start, tmr_crt; if( timeout == 0 ) { if( ps2_r_idx == ps2_w_idx ) return -1; } else { if( timeout == PLATFORM_UART_INFINITE_TIMEOUT ) while( ps2_r_idx == ps2_w_idx ); else { tmr_start = platform_timer_op( PS2_TIMER_ID, PLATFORM_TIMER_OP_START, 0 ); while( 1 ) { if( ps2_r_idx != ps2_w_idx ) break; tmr_crt = platform_timer_op( PS2_TIMER_ID, PLATFORM_TIMER_OP_READ, 0 ); if( platform_timer_get_diff_us( PS2_TIMER_ID, tmr_crt, tmr_start ) >= timeout ) return -1; } } } return ps2h_read_data(); }
int platform_uart_recv( unsigned id, unsigned timer_id, s32 timeout ) { #ifdef __DAVE3__ return cmn_recv_helper( id, timeout ); #else timer_data_type tmr_start, tmr_crt; int res; if( timeout == 0 ) return cmn_recv_helper( id, timeout ); else if( timeout == PLATFORM_UART_INFINITE_TIMEOUT ) return cmn_recv_helper( id, timeout ); else { // Receive char with the specified timeout tmr_start = platform_timer_op( timer_id, PLATFORM_TIMER_OP_START, 0 ); while( 1 ) { if( ( res = cmn_recv_helper( id, 0 ) ) >= 0 ) break; tmr_crt = platform_timer_op( timer_id, PLATFORM_TIMER_OP_READ, 0 ); if( platform_timer_get_diff_us( timer_id, tmr_crt, tmr_start ) >= timeout ) break; } return res; } #endif }
// Lua: time_us = gettimediff( id, start, end ) static int tmr_gettimediff( lua_State* L ) { timer_data_type start, end, res; unsigned id; id = ( unsigned )luaL_optinteger( L, 1, PLATFORM_TIMER_SYS_ID ); MOD_CHECK_TIMER( id ); start = ( timer_data_type )luaL_checknumber( L, 2 ); end = ( timer_data_type )luaL_checknumber( L, 3 ); res = platform_timer_get_diff_us( id, start, end ); lua_pushnumber( L, ( lua_Number )res ); return 1; }
// Lua: time_us = diff( id, end, start ) static int tmr_diff( lua_State* L ) { timer_data_type end, start; u32 res; unsigned id; id = luaL_checkinteger( L, 1 ); MOD_CHECK_ID( timer, id ); end = luaL_checkinteger( L, 2 ); start = luaL_checkinteger( L, 3 ); res = platform_timer_get_diff_us( id, end, start ); lua_pushinteger( L, res ); return 1; }
// (tmr-gettimediff 'num 'num 'num) -> num any tmr_gettimediff(any ex) { timer_data_type start, end, res; unsigned id = PLATFORM_TIMER_SYS_ID; any x, y; x = cdr(ex), y = EVAL(car(x)); NeedNum(ex, y); id = unBox(y); // get id. MOD_CHECK_TIMER(ex, id); x = cdr(x), y = EVAL(car(x)); NeedNum(ex, y); start = unBox(y); // get start. x = cdr(x), y = EVAL(car(x)); NeedNum(ex, y); end = unBox(y); // get end. res = platform_timer_get_diff_us(id, start, end); return box(res); }
int platform_uart_recv( unsigned id, unsigned timer_id, int timeout ) { timer_data_type tmr_start, tmr_crt; int res; if( timeout == 0 ) { // Return data only if already available if( UART_GetFlagStatus(STR9_UART, UART_FLAG_RxFIFOEmpty) != SET ) return UART_ReceiveData( STR9_UART ); else return -1; } else if( timeout == PLATFORM_UART_INFINITE_TIMEOUT ) { // Wait for data while( UART_GetFlagStatus(STR9_UART, UART_FLAG_RxFIFOEmpty) == SET ); return UART_ReceiveData( STR9_UART ); } else { // Receive char with the specified timeout tmr_start = platform_timer_op( timer_id, PLATFORM_TIMER_OP_START,0 ); while( 1 ) { if( UART_GetFlagStatus(STR9_UART, UART_FLAG_RxFIFOEmpty) != SET ) { res = UART_ReceiveData( STR9_UART ); break; } else res = -1; tmr_crt = platform_timer_op( timer_id, PLATFORM_TIMER_OP_READ, 0 ); if( platform_timer_get_diff_us( timer_id, tmr_crt, tmr_start ) >= timeout ) break; } return res; } }
int platform_uart_recv( unsigned id, unsigned timer_id, int timeout ) { AT91S_USART* base = id == 0 ? AT91C_BASE_US0 : AT91C_BASE_US1; timer_data_type tmr_start, tmr_crt; int res; if( timeout == 0 ) { // Return data only if already available if( USART_IsDataAvailable( base ) ) return USART_Read( base, 0 ); else return -1; } else if( timeout == PLATFORM_UART_INFINITE_TIMEOUT ) { // Wait for data return USART_Read( base, 0 ); } else { // Receive char with the specified timeout tmr_start = platform_timer_op( timer_id, PLATFORM_TIMER_OP_START,0 ); while( 1 ) { if( USART_IsDataAvailable( base ) ) { res = USART_Read( base, 0 ); break; } else res = -1; tmr_crt = platform_timer_op( timer_id, PLATFORM_TIMER_OP_READ, 0 ); if( platform_timer_get_diff_us( timer_id, tmr_crt, tmr_start ) >= timeout ) break; } return res; } }