コード例 #1
0
ファイル: wln-cli.c プロジェクト: JonHylands/projects
int main( void )
{
    uint8_t delay;
    uint8_t counter;
    uint8_t doStatus = 1;

    InitTimer();
    InitUART();

    SetDirectionOut( LED );

    SetDirectionOut( MOSI );
    SetDirectionIn(  MISO );
    SetDirectionOut( SCK );
    SetDirectionOut( SS );
    SetDirectionIn(  INT );
    SetDirectionOut( TRIGGER );

    TRIGGER_PORT &= ~TRIGGER_MASK;

    SPI_MasterInit();

    Log( "*****\n" );
    Log( "***** WLN CLI program\n" );
    Log( "*****\n" );

    sei();

    counter = 0;
    while ( 1 )
    {
        LED_PORT ^= LED_MASK;

        if ( doStatus )
        {
            if ( counter == 0 )
            {
                uint8_t status = WLN_ReadStatus();
                WLN_PrintStatus( status );
            }
            counter = ( counter + 1 ) & 0x03;
        }

        // Tick rate is 100/sec so waiting for 50 waits for 1/2 sec

        for ( delay = 0; delay < 50; delay++ ) 
        {
            tick_t prevCount = gTickCount;
            while ( gTickCount == prevCount )
            {
                LogBufDump();
            }

            if ( UART0_IsCharAvailable() )
            {
                char    ch = UART0_GetChar();

                switch ( ch )
                {
                    case '!':
                    {
                        doStatus = !doStatus;
                        Log( "Turning status %s\n", doStatus ? "on" : "off" );
                        break;
                    }

                    case '*':
                    {
                        Log( "Reset\n" );

                        SetDirectionOut( RESET );
                        RESET_PORT &= ~RESET_MASK;
                        break;
                    }

                    case ' ':
                    {
                        WLN_ReadData();
                        break;
                    }

                    default:
                    {
                        Log( "Read: '%c'\n", ch );
                        WLN_WriteByte( ch );
                        break;
                    }
                }

            }
        }
    }

} // main
コード例 #2
0
void BootLoader( int mode )
{
    uint8_t beats;
    uint8_t bootDelay;
    uint8_t beatRate;

#if BOOT_LOG_ENABLED
    FILE   *logFs;
#endif

    uint8_t beatCounter = 0;

#if defined( CFG_FORCE_MASK )

    // We configure the pullup as early as we can, to give the pin a chance
    // to respond.

    CFG_FORCE_DDR  &= ~CFG_FORCE_MASK;  // Configure pin for input
    CFG_FORCE_PORT |= CFG_FORCE_MASK;   // Enable Pullup
#endif

    cli();

#if ( !STANDALONE )

    // For the BootLoader, we want the interrupts to goto the BootLoader
    // interrupt vector. Do the special dance.

#if defined( GICR )
    GICR = ( 1 << IVCE );
    GICR = ( 1 << IVSEL );
#else
    MCUCR = ( 1 << IVCE );
    MCUCR = ( 1 << IVSEL );
#endif
#endif // STANDALONE

#if CFG_USE_UART && 0
    InitHardware();
#else
    InitTimer();
#endif

#if BOOT_LOG_ENABLED
    logFs = fdevopen( UART0_PutCharStdio, NULL );

    LogInit( logFs );
#endif

#if defined( CFG_I2C_LED_MASK )
    CFG_I2C_LED_DDR         |= CFG_I2C_LED_MASK;
#endif
#if defined( CFG_BOOTLOADER_BEAT_MASK )
    CFG_BOOTLOADER_BEAT_DDR |= CFG_BOOTLOADER_BEAT_MASK;
#endif

    // Read our i2c address from the EEPROM

    eeprom_read_block( &gEeprom, (void *)( E2END + 1 - sizeof( gEeprom )), sizeof( gEeprom ));

    // Check to see if the FORCE strap has been installed

    if (( gEeprom.structSize == 0xff ) || ( gEeprom.i2cAddr > 0x7f ))
    {
        // EEPROM is uninitialized. Use default data

        gEeprom.i2cAddr = BL_DEFAULT_I2C_ADDR;
    }
    if (( gEeprom.structSize == 0xff ) || ( gEeprom.bootDelay == 0xff ))
    {
        gEeprom.bootDelay = BL_DEFAULT_BOOT_DELAY;
    }

    I2C_SlaveInit( &gI2cGlobals, gEeprom.i2cAddr, ProcessCommand );

    memset( &gBootLoaderGlobals, 0, sizeof( gBootLoaderGlobals ));
    gBootLoaderGlobals.m_pageAddress = ~0uL;

    // Set gStayInBootLoader to zero before we enable interripts. We do this
    // since the interrupt handler will set it to 1 if an Info command is received.

    gStayInBootloader = 0;
    if (( pgm_read_byte_near( 0x0000 ) == 0xFF ) || ( mode == BOOT_MODE_APP ))
    {
        // No user mode program has been downloaded. We really have no choice but to stay in
        // the bootloader.

        gStayInBootloader = 1;
    }

    sei();

    BOOT_LOG0( "\n" );
    BOOT_LOG0( "*****\n" );
    BOOT_LOG2( "***** I2C BootLoader i2cAddr: 0x%02x bootDelay:%d\n", gEeprom.i2cAddr, gEeprom.bootDelay );
    BOOT_LOG0( "*****\n" );

    bootDelay = gEeprom.bootDelay;
    beats = 0;

    // gStayInBootloader is set if the BL_CMD_GET_INFO packet is received or 
    // no user program is found in flash.

    beatRate = 100; // Toggle every 100 msec

// By default, we pick Port E1 (TXD) as the override pin. For the normal bootloader
// we don't use the UART, so this is fine.

#if ( defined( CFG_FORCE_MASK ) && !CFG_USE_UART )

    // We split the test away from the configuration of the pin to give
    // things a chance to settle.   

    gStayInBootloader |= (( CFG_FORCE_PIN & CFG_FORCE_MASK ) == 0 );

#endif

    while ( gStayInBootloader || ( bootDelay > 0 ))
    {
        tick_t prevCount;

        if ( gStayInBootloader  )
        {
            beatRate = 250;
        }

        if ( ++beatCounter >= beatRate )
        {
            beatCounter = 0;

            CFG_BOOTLOADER_BEAT_PORT ^= CFG_BOOTLOADER_BEAT_MASK;

            if ( ++beats >= 10 )
            {
                // This branch taken once per second

                beats = 0;
                if ( bootDelay > 0 )
                {
                    bootDelay--;
                }
            }
        }

        prevCount = gTickCount;
        while ( gTickCount == prevCount )
        {
#if BOOT_LOG_ENABLED
            LogBufDump();
#endif
        }
    }

    RunUserProgram();

} // BootLoader