Beispiel #1
0
static int pio_mt_index( lua_State* L )
{
  const char *key = luaL_checkstring( L ,2 );
  int port = 0xFFFF, pin = 0xFFFF, isport = 0, sz;
  
  if( !key || *key != 'P' )
    return 0;
  if( isupper( key[ 1 ] ) ) // PA, PB, ...
  {
    if( PIO_PREFIX != 'A' )
      return 0;
    port = key[ 1 ] - 'A';
    if( key[ 2 ] == '\0' )
      isport = 1;
    else if( key[ 2 ] == '_' )      
    {
      if( sscanf( key + 3, "%d%n", &pin, &sz ) != 1 || sz != strlen( key ) - 3 )
        return 0;      
    }
  }
  else // P0, P1, ...
  {
    if( PIO_PREFIX != '0' )
      return 0;
    if( !strchr( key, '_' ) )   // parse port
    {
      if( sscanf( key + 1, "%d%n", &port, &sz ) != 1  || sz != strlen( key ) - 1 )
        return 0;
      isport = 1;
    }
    else    // parse port_pin
      if( sscanf( key + 1, "%d_%d%n", &port, &pin, &sz ) != 2 || sz != strlen( key ) - 1 )
        return 0;
  }
  sz = -1;
  if( isport )
  {
    if( platform_pio_has_port( port ) )
      sz = PLATFORM_IO_ENCODE( port, 0, 1 );
  }
  else
  {
    if( platform_pio_has_port( port ) && platform_pio_has_pin( port, pin ) )
      sz = PLATFORM_IO_ENCODE( port, pin, 0 );
  }
  if( sz == -1 )
    return 0;
  else
  {
    lua_pushinteger( L, sz );
    return 1;
  }
}
Beispiel #2
0
// EINT3 (INT_GPIO) interrupt handler
static void int_handler_eint3()
{
  elua_int_id id = ELUA_INT_INVALID_INTERRUPT;
  pio_code resnum = 0;
  int pidx, pin;
  
  EXTINT |= 1 << EINT3_BIT; // clear interrupt
  // Look for interrupt source
  // In can only be GPIO0/GPIO2, as the EXT interrupts are not (yet) used
  pidx = ( IO_INT_STAT & 1 ) ? 0 : 1;
  if( *posedge_status[ pidx ] )
  {
    id = INT_GPIO_POSEDGE;
    pin = intlog2( *posedge_status[ pidx ] );
  }
  else
  {
    id = INT_GPIO_NEGEDGE;
    pin = intlog2( *negedge_status[ pidx ] );
  }
  resnum = PLATFORM_IO_ENCODE( pidx * 2, pin, PLATFORM_IO_ENC_PIN );   
  *intclr_regs[ pidx ] = 1 << pin;
  
  // Run the interrupt through eLua
  cmn_int_handler( id, resnum );
  VICVectAddr = 0; // ACK interrupt    
}
static void gpio_common_handler( int port )
{
  u32 base = pio_base[ port ];
  u8 pin, pinmask;
  u32 ibe = HWREG( base + GPIO_O_IBE );
  u32 iev = HWREG( base + GPIO_O_IEV );

  // Check each pin in turn
  for( pin = 0, pinmask = 1; pin < 8; pin ++, pinmask <<= 1 )
    if( HWREG( base + GPIO_O_MIS ) & pinmask ) // interrupt on pin
    {
      if( MAP_GPIOPinRead( base, pinmask ) && ( ( ibe & pinmask ) || ( iev & pinmask ) ) ) // high level and posedge interrupt enabled 
        cmn_int_handler( INT_GPIO_POSEDGE, PLATFORM_IO_ENCODE( port, pin, 0 ) );
      else if( ( ibe & pinmask ) || !( iev & pinmask ) ) // low level and negedge interrupt enabled
        cmn_int_handler( INT_GPIO_NEGEDGE, PLATFORM_IO_ENCODE( port, pin, 0 ) );
      HWREG( base + GPIO_O_ICR ) = pinmask;
    }
}
Beispiel #4
0
static int mbed_pio_mt_index( lua_State* L ) 
{
  const char *key = luaL_checkstring( L, 2 );
  int pin, sz;
  
  if( *key == 'P' || *key == 'p' ) // p5 - p30
  {
    if( sscanf( key + 1, "%d%n", &pin, &sz ) != 1  || sz != strlen( key ) - 1 )
      return 0;
    if(pin < 5 || pin > 30)
      return 0;
    pin -= 5;
  }
  else if ( *key == 'L' ) // LED1-4
  {
    pin = key[3] - '0';
    if ( pin > 4 || pin < 1 )
      return 0;
    pin += 25; // LED pins begin at pin_*[26], led #s begin at 1
  }
  else if ( *key == 'U' ) // USBTX, USBRX
  {
    switch( key[3] )
    {
      case 'T': pin = 30;
      case 'R': pin = 31;
      default: return 0;
    }
  }
  
  sz = PLATFORM_IO_ENCODE( pin_ports[ pin ], pin_nums[ pin ], 0 );

  if( sz == -1 )
    return 0;
  else
  {
    lua_pushinteger( L, sz );
    return 1;
  }
}