Exemplo n.º 1
0
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();
}
Exemplo n.º 2
0
/*---------------------------------------------------------------------------*/
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);
}
Exemplo n.º 3
0
// 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;
}
Exemplo n.º 4
0
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
}
Exemplo n.º 5
0
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;    
  }  
}
Exemplo n.º 6
0
// 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;
}
Exemplo n.º 7
0
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;
    }
}
Exemplo n.º 8
0
// Lua: res = getmaxdelay( [id] )
static int tmr_getmaxdelay( lua_State* L )
{
  timer_data_type res;
  unsigned id;
  
  id = ( unsigned )luaL_optinteger( L, 1, PLATFORM_TIMER_SYS_ID );
  MOD_CHECK_TIMER( id );
  res = platform_timer_op( id, PLATFORM_TIMER_OP_GET_MAX_DELAY, 0 );
  lua_pushnumber( L, ( lua_Number )res );
  return 1;
}
Exemplo n.º 9
0
// Helper function for the read/start functions
static int tmrh_timer_op( lua_State* L, int op )
{
  unsigned id;
  timer_data_type res;
    
  id = ( unsigned )luaL_optinteger( L, 1, PLATFORM_TIMER_SYS_ID );
  MOD_CHECK_TIMER( id );
  res = platform_timer_op( id, op, 0 );
  lua_pushnumber( L, ( lua_Number )res );
  return 1;  
}
Exemplo n.º 10
0
// Lua: clock = getclock( [id] )
static int tmr_getclock( lua_State* L )
{
  u32 res;
  unsigned id;
  
  id = ( unsigned )luaL_optinteger( L, 1, PLATFORM_TIMER_SYS_ID );
  MOD_CHECK_TIMER( id );
  res = platform_timer_op( id, PLATFORM_TIMER_OP_GET_CLOCK, 0 );
  lua_pushinteger( L, res );
  return 1;
}
Exemplo n.º 11
0
// Lua: res = maxdelay( id )
static int tmr_maxdelay( lua_State* L )
{
  u32 res;
  unsigned id;
  
  id = luaL_checkinteger( L, 1 );
  MOD_CHECK_ID( timer, id );
  res = platform_timer_op( id, PLATFORM_TIMER_OP_GET_MAX_DELAY, 0 );
  lua_pushinteger( L, res );
  return 1;
}
Exemplo n.º 12
0
// Helper function for the read/start functions
static int tmrh_timer_op( lua_State* L, int op )
{
  unsigned id;
  timer_data_type res;
    
  id = luaL_checkinteger( L, 1 );
  MOD_CHECK_ID( timer, id );
  res = platform_timer_op( id, op, 0 );
  lua_pushinteger( L, res );
  return 1;  
}
Exemplo n.º 13
0
// Lua: clock = getclock( id )
static int tmr_getclock( lua_State* L )
{
  u32 res;
  unsigned id;
  
  id = luaL_checkinteger( L, 1 );
  MOD_CHECK_ID( timer, id );
  res = platform_timer_op( id, PLATFORM_TIMER_OP_GET_CLOCK, 0 );
  lua_pushinteger( L, res );
  return 1;
}
Exemplo n.º 14
0
// Lua: realclock = setclock( id, clock )
static int tmr_setclock( lua_State* L )
{
  u32 clock;
  unsigned id;
  
  id = ( unsigned )luaL_optinteger( L, 1, PLATFORM_TIMER_SYS_ID );
  MOD_CHECK_TIMER( id );
  clock = ( u32 )luaL_checkinteger( L, 2 );
  clock = platform_timer_op( id, PLATFORM_TIMER_OP_SET_CLOCK, clock );
  lua_pushinteger( L, clock );
  return 1;
}
Exemplo n.º 15
0
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;    
  }
}
Exemplo n.º 16
0
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;    
  }
}
Exemplo n.º 17
0
// (tmr-getclock ['num]) -> num
any tmr_getclock(any ex) {
  timer_data_type res;
  unsigned id = PLATFORM_TIMER_SYS_ID;
  any x, y;

  x = cdr(ex), y = EVAL(car(x));
  if (plen(ex) > 0) {
    NeedNum(ex, y);
    id = unBox(y);
    MOD_CHECK_TIMER(ex, id);
  }
  res = platform_timer_op(id, PLATFORM_TIMER_OP_GET_CLOCK, 0);
  return box(res);
}
Exemplo n.º 18
0
// (tmr-start ['num]) -> num
any tmr_start(any ex) {
  unsigned id = PLATFORM_TIMER_SYS_ID;
  timer_data_type res;
  any x, y;

  x = cdr(ex), y = EVAL(car(x));
  if (plen(ex) > 0) {
    NeedNum(ex, y);
    id = unBox(y);
    MOD_CHECK_TIMER(ex, id);
  }

  res = platform_timer_op(id, PLATFORM_TIMER_OP_START, 0);
  return box(res);
}
Exemplo n.º 19
0
// (tmr-setclock 'num 'num) -> num
any tmr_setclock(any ex) {
  u32 clock;
  unsigned 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);
  clock = unBox(y); // get clock.

  clock = platform_timer_op(id, PLATFORM_TIMER_OP_SET_CLOCK, clock);
  return box(clock);
}