/** \brief Initialize and set cpu and periheral clocks. * * CPU clock frequencies set are: * -CPU: 32HMZ * -Peripheral Prescaling: NONE */ void setup_clocks(void) { // set 32MHZ oscillator as CPU clock source CLKSYS_Enable(OSC_RC32MEN_bm); // enable do { nop(); } while (!CLKSYS_IsReady(OSC_RC32MRDY_bm)); // wait til stable CLKSYS_Main_ClockSource_Select(CLK_SCLKSEL_RC32M_gc); // select for CPU // disable all presacalers, until we decide otherwise CLKSYS_Prescalers_Config(CLK_PSADIV_1_gc, CLK_PSBCDIV_1_1_gc); // set up external 32KHz oscillator (NOTE: first param is ignored) CLKSYS_XOSC_Config(OSC_FRQRANGE_04TO2_gc, false, OSC_XOSCSEL_32KHz_gc); // set internal 32KHz oscillator as source for DFLL and autocalibrate 32MHz CLKSYS_Enable(OSC_XOSCEN_bm); //enable do { nop(); } while (!CLKSYS_IsReady(OSC_XOSCRDY_bm)); // wait til stable CLKSYS_AutoCalibration_Enable(OSC_RC32MCREF_bm, true); // true == ext 32KHz // disable unused oscillators (internal 2MHz and 32KHz oscillators) CLKSYS_Disable(OSC_RC2MEN_bm | OSC_RC32KEN_bm); }
int main(void) { ADDR_T address = 0; unsigned int temp_int=0; unsigned char val; /* Initialization */ void (*funcptr)( void ) = 0x0000; // Set up function pointer to RESET vector. PMIC_SetVectorLocationToBoot(); // Configure 32KHz OSC CLKSYS_Enable( OSC_RC32KEN_bm ); do {} while ( CLKSYS_IsReady( OSC_RC32KEN_bm ) == 0 ); CLKSYS_Enable( OSC_RC32MEN_bm ); do {} while ( CLKSYS_IsReady( OSC_RC32MRDY_bm ) == 0 ); CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_RC32M_gc ); CLKSYS_Disable( OSC_RC2MEN_bm); CLKSYS_AutoCalibration_Enable( OSC_RC32MCREF0_bm, true ); eeprom_disable_mapping(); PROGPORT |= (1<<PROG_NO); // Enable pull-up on PROG_NO line on PROGPORT. /* Branch to bootloader or application code? */ if( /*!(PROGPIN & (1<<PROG_NO))*/1 ) // If PROGPIN is pulled low, enter programming mode. { initbootuart(); // Initialize UART. /* Main loop */ for(;;) { val = recchar(); // Wait for command character. // Check autoincrement status. if(val=='a') { sendchar('Y'); // Yes, we do autoincrement. } // Set address (2 bytes). else if(val == 'A') { // NOTE: Flash addresses are given in words, not bytes. address = recchar(); address <<= 8; address |= recchar(); // Read address high and low byte. sendchar('\r'); // Send OK back. } // Set extended address (3 bytes). else if(val == 'H') { // NOTE: Flash addresses are given in words, not bytes. address = (uint32_t)recchar() << 16; address |= (uint16_t)recchar() << 8; address |= recchar(); sendchar('\r'); // Send OK back. } // Chip erase. else if(val=='e') { for(address = 0; address < APP_END; address += PAGESIZE) { // NOTE: Here we use address as a byte-address, not word-address, for convenience. nvm_wait_until_ready(); #ifdef __ICCAVR__ #pragma diag_suppress=Pe1053 // Suppress warning for conversion from long-type address to flash ptr. #endif EraseApplicationPage( address ); #ifdef __ICCAVR__ #pragma diag_default=Pe1053 // Back to default. #endif } nvm_eeprom_erase_all(); sendchar('\r'); // Send OK back. } #ifndef REMOVE_BLOCK_SUPPORT // Check block load support. else if(val=='b') { sendchar('Y'); // Report block load supported. sendchar((BLOCKSIZE>>8) & 0xFF); // MSB first. sendchar(BLOCKSIZE&0xFF); // Report BLOCKSIZE (bytes). } // Start block load. else if(val=='B')