Example #1
0
bool avr_init(void) {
    
    
    /* Enable power reduction on all peripheral modules. */
    PRR0 = (1 << PRTWI)  |   // Disable TWI.
	       (1 << PRTIM2) |   // Disable TIMER2.
	       (1 << PRTIM0) |   // Disable TIMER0.
           (1 << PRTIM1) |   // Disable TIMER1.
           (1 << PRSPI)  |   // Disable SPI.
	       (1 << PRADC);     // Disable ADC.
    
    PRR1 = (1 << PRUSB)  |   // Disable USB.
	       (1 << PRTIM3) |   // Disable TIMER3.
	       (1 << PRUSART1);  // Disable USART1.
	
	ACSR |= (1 << ACD);      // Disable Analog Comparator.
	
	DIDR0 = (1 << ADC7D) |   // Disable digital input buffer for analog input pins.
	        (1 << ADC6D) |   // Disable digital input buffer for analog input pins.
	        (1 << ADC5D) |   // Disable digital input buffer for analog input pins.
	        (1 << ADC4D);    // Disable digital input buffer for analog input pins.
    
	/* Initialize the VRT Runtime Framework. */
	if (true != vrt_init()) {
    } else if (true != vrt_mem_init()) {
    } else if (true != vrt_mem_add_partition(partition_buffer1, BLOCK_SIZE_1, BLOCK_COUNT_1, &partition1)) {
    } else if (true != vrt_mem_add_partition(partition_buffer2, BLOCK_SIZE_2, BLOCK_COUNT_2, &partition2)) {
    } else if (true != vrt_timer_init()) {
    } else {
        /* AVR successfully initialized. */
        LED_INIT();
        return true;
    }
    
    return false;
}
void
VRT_init_dir_random(struct cli *cli, struct director **bp, int idx,
                    const void *priv)
{
    vrt_init(cli, bp, idx, priv, c_random);
}
void
VRT_init_dir_client(struct cli *cli, struct director **bp, int idx,
                    const void *priv)
{
    vrt_init(cli, bp, idx, priv, c_client);
}
void main(void) {
#else
int main(void) {
#endif
    /* Ensure that the watchdog is not running. */
    wdt_disable();
        
    /* Initialize AVR peripheral modules. */
    (bool)avr_init();
    
    /* Check if the RX and TX pins are shorted. If they are shorted, the RZUSBSTICK
     * shall start the bootloader. If not, continue to verify if the application
     * requested to enter the bootloader.
     */
    
    /* Check if the application has requested to enter the bootloader. */
    if ((BOOT_PIN & (1 << BOOT_RX)) != (1 << BOOT_RX)) {
        /* Check that RX goes high when TX is pulled high. */
        BOOT_PORT |= (1 << BOOT_TX);
        
        nop();
        nop();
        nop();
        nop();
        
        if ((BOOT_PIN & (1 << BOOT_RX)) != (1 << BOOT_RX)) {
            start_application();
        }
    } else {
        /* Check if the application has requested to enter the bootloader. */
        uint8_t volatile magic_value = 0xAA;
        EEGET(magic_value, EE_BOOT_MAGIC_ADR);
   
        if (EE_BOOT_MAGIC_VALUE != magic_value) {
            start_application();
        } else {
            EEPUT(EE_BOOT_MAGIC_ADR, 0xFF);
        }
    }
    
    /* Set the interrupt vectors to the bootloader, initialize the LEDs and the
     * VRT kernel.
     */
    ENTER_CRITICAL_REGION();
    uint8_t temp_mcucr = MCUCR;
    MCUCR = (1 << IVCE);
    MCUCR = (1 << IVSEL);
    MCUCR = temp_mcucr;
    LEAVE_CRITICAL_REGION();

    LED_INIT();
    vrt_init();
    
    if (true != eep_init()) {
        error_handler();
    } else if (true != cmd_if_init()) {
        error_handler();
    }
    
    LED_ORANGE_ON();
    
    /* Enable Interrupts. */
    sei();
    
    /* Enter the endless application loop. */
    for (;;) {
        vrt_dispatch_event();
        usb_task();
    }
}