static void InitMainClockPll(void) { CLKSYS_XOSC_Config( OSC_FRQRANGE_2TO9_gc, 0, OSC_XOSCSEL_XTAL_16KCLK_gc ); CLKSYS_Enable( OSC_XOSCEN_bm ); do {} while ( CLKSYS_IsReady( OSC_XOSCRDY_bm ) == 0 ); PORTE.OUTSET = PIN0_bm; CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_XOSC_gc ); CLKSYS_Disable( OSC_RC2MEN_bm ); CLKSYS_Disable( OSC_RC32MEN_bm ); }
void setup_clocks(void) { // Enable the 32 MHz internal oscillator. CLKSYS_Enable(OSC_RC32MEN_bm); // Set the multiplication factor and clock reference for the PLL. // The USB clock runs at 48 MHz. // 32 MHz / 4 = 4 MHz, 48 MHz / 4 MHz = 6 (factor). OSC.PLLCTRL = (OSC_PLLSRC_RC32M_gc | (6 << OSC_PLLFAC_gp)); // Wait for the 32 MHz clock to be ready. do { } while (CLKSYS_IsReady(OSC_RC32MRDY_bm) == 0); // Switch to the 32 MHz clock for the main system clock. // XXX Check the return value! CLKSYS_Main_ClockSource_Select(CLK_SCLKSEL_RC32M_gc); // Enable the PLL. CLKSYS_Enable(OSC_PLLEN_bm | OSC_RC32MEN_bm); // Turn off all the other clocks. CLKSYS_Disable(OSC_RC32KEN_bm | OSC_RC2MEN_bm | OSC_XOSCEN_bm); // Wait for the PLL to be ready. do { } while (CLKSYS_IsReady(OSC_PLLRDY_bm) == 0); }
/*! \brief Initalizes TWI slave driver structure. * * Initialize the instance of the TWI Slave and set the appropriate values. * * \param twi The TWI_Slave_t struct instance. * \param module Pointer to the TWI module. * \param processDataFunction Pointer to the function that handles incoming data. */ void TWI_SlaveInitializeDriver(TWI_Slave_t *twi, TWI_t *module, void (*processDataFunction) (void)) { twi->interface = module; twi->Process_Data = processDataFunction; twi->bytesReceived = 0; twi->bytesSent = 0; twi->status = TWIS_STATUS_READY; twi->result = TWIS_RESULT_UNKNOWN; twi->abort = false; CLKSYS_Enable( OSC_RC32MEN_bm ); do {} while ( CLKSYS_IsReady( OSC_RC32MRDY_bm ) == 0 ); /* 32MHz Internal RC*/ CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_RC32M_gc ); CLKSYS_Disable( OSC_RC2MEN_bm | OSC_RC32KEN_bm ); }
/** * Set up all the processor clock inputs. */ void setup_processor_clocks(void) { // Enable the 32 MHz internal oscillator. CLKSYS_Enable(OSC_RC32MEN_bm); // Wait for the 32 MHz clock to be ready. do { } while (CLKSYS_IsReady(OSC_RC32MRDY_bm) == 0); // Switch to the 32 MHz clock for the main system clock. // XXX Check the return value! CLKSYS_Main_ClockSource_Select(CLK_SCLKSEL_RC32M_gc); // Turn off all the other clocks. // XXX What about the external clock? CLKSYS_Disable(OSC_PLLEN_bm | OSC_RC32KEN_bm | OSC_RC2MEN_bm); // Enable automatic calibration of the 32MHz clock. OSC.DFLLCTRL = OSC_RC32MCREF_XOSC32K_gc; DFLLRC32M.CTRL = DFLL_ENABLE_bm; }
/** \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')
int main(void) { // Configure switches PORTCFG.MPCMASK = 0xFF; // Configure several PINxCTRL registers at the same time SWITCHPORTL.PIN0CTRL = (SWITCHPORTL.PIN0CTRL & ~PORT_OPC_gm) | PORT_OPC_PULLUP_gc; //Enable pull-up to get a defined level on the switches SWITCHPORTL.DIRCLR = 0xFF; // Set port as input // Configure LEDs PORTCFG.MPCMASK = 0xFF; // Configure several PINxCTRL registers at the same time LEDPORT.PIN0CTRL = PORT_INVEN_bm; // Invert input to turn STK600 leds on when port value is 1 LEDPORT.DIRSET = 0xFF; // Set port as output LEDPORT.OUT = 0xA0; // Set initial value unsigned int counter = 0; while (1) { /* Change the clock source to the internal 2MHz. Disable PLL. * Configure PLL with the 2 MHz RC oscillator as source and * multiply by 15 to get 30 MHz PLL clock and enable it. Wait * for it to be stable and set prescaler C to divide by two * to set the CPU clock to 15 MHz */ if ( (SWITCHPORTL.IN & PIN0_bm) == 0 ) { CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_RC2M_gc ); CLKSYS_Disable( OSC_PLLEN_bm ); CLKSYS_PLL_Config( OSC_PLLSRC_RC2M_gc, 15 ); CLKSYS_Enable( OSC_PLLEN_bm ); CLKSYS_Prescalers_Config( CLK_PSADIV_1_gc, CLK_PSBCDIV_1_2_gc ); do {} while ( CLKSYS_IsReady( OSC_PLLRDY_bm ) == 0 ); CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_PLL_gc ); LEDPORT.OUTCLR = 0x1F; LEDPORT.OUTSET = PIN0_bm; } /* Configure PLL with the 2 MHz RC oscillator as source and * multiply by 15 to get 30 MHz PLL clock and enable it. Wait * for it to be stable and set prescaler B and C to divide by two * to set the CPU clock to 7.5 MHz */ if ( (SWITCHPORTL.IN & PIN1_bm) == 0 ) { CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_RC2M_gc ); CLKSYS_Disable( OSC_PLLEN_bm ); CLKSYS_PLL_Config( OSC_PLLSRC_RC2M_gc, 15 ); CLKSYS_Enable( OSC_PLLEN_bm ); CLKSYS_Prescalers_Config( CLK_PSADIV_1_gc, CLK_PSBCDIV_2_2_gc ); do {} while ( CLKSYS_IsReady( OSC_PLLRDY_bm ) == 0 ); CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_PLL_gc ); LEDPORT.OUTCLR = 0x1F; LEDPORT.OUTSET = PIN1_bm; } /* Configure PLL with the 2 MHz RC oscillator as source and * multiply by 6 to get 12 MHz PLL clock and enable it. Wait * for it to be stable and set prescaler B and C to divide by two * to set the CPU clock to 3 MHz */ if ( (SWITCHPORTL.IN & PIN2_bm) == 0 ) { CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_RC2M_gc ); CLKSYS_Disable( OSC_PLLEN_bm ); CLKSYS_PLL_Config( OSC_PLLSRC_RC2M_gc, 6 ); CLKSYS_Enable( OSC_PLLEN_bm ); CLKSYS_Prescalers_Config( CLK_PSADIV_1_gc, CLK_PSBCDIV_2_2_gc ); do {} while ( CLKSYS_IsReady( OSC_PLLRDY_bm ) == 0 ); CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_PLL_gc ); LEDPORT.OUTCLR = 0x1F; LEDPORT.OUTSET = PIN2_bm; } /* Dynamically change prescaler: * Set prescaler A to divide by 2, prescaler B with no division, * and prescaler C to divide by 2. This will give the CPU a freqency * of 1/4th of the input freqency. Input freqency will depend on the * previous switch that has been pressed. */ if ( (SWITCHPORTL.IN & PIN3_bm) == 0 ) { CLKSYS_Prescalers_Config( CLK_PSADIV_2_gc, CLK_PSBCDIV_1_2_gc ); LEDPORT.OUTSET = PIN3_bm; } /* * Add code here for generating 62 MHz to clkper4 and 15.5 MHz to the CPU */ if ( (SWITCHPORTL.IN & PIN4_bm) == 0 ) { CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_RC2M_gc ); CLKSYS_Disable( OSC_PLLEN_bm ); CLKSYS_PLL_Config( OSC_PLLSRC_RC2M_gc, 31 ); CLKSYS_Enable( OSC_PLLEN_bm ); CLKSYS_Prescalers_Config( CLK_PSADIV_1_gc, CLK_PSBCDIV_2_2_gc ); do {} while ( CLKSYS_IsReady( OSC_PLLRDY_bm ) == 0 ); CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_PLL_gc ); LEDPORT.OUTCLR = 0x1F; LEDPORT.OUTSET = PIN4_bm; } // Blink LEDS after the loop has been run several times // This gives us visual feedback on the MCU speed counter++; if (counter > 20000 ) { LEDPORT.OUTTGL = 0xE0; counter=0; } } }