/*! \brief Main function running the example on both the flash array and the * User page. */ int main(void) { #if BOARD == UC3L_EK // Note: on the AT32UC3L-EK board, there is no crystal/external clock connected // to the OSC0 pinout XIN0/XOUT0. We shall then program the DFLL and switch the // main clock source to the DFLL. pcl_configure_clocks(&pcl_dfll_freq_param); // Note: since it is dynamically computing the appropriate field values of the // configuration registers from the parameters structure, this function is not // optimal in terms of code size. For a code size optimal solution, it is better // to create a new function from pcl_configure_clocks_dfll0() and modify it // to use preprocessor computation from pre-defined target frequencies. #else // Configure Osc0 in crystal mode (i.e. use of an external crystal source, with // frequency FOSC0) with an appropriate startup time then switch the main clock // source to Osc0. pcl_switch_to_osc(PCL_OSC0, FOSC0, OSC0_STARTUP); #endif // Initialize the debug USART module. init_dbg_rs232(EXAMPLE_TARGET_PBACLK_FREQ_HZ); // Apply the example to the flash array. flash_rw_example("\x0C=== Using a piece of the flash array as NVRAM ===\r\n", &flash_nvram_data); // Apply the example to the User page. flash_rw_example("\r\n\r\n=== Using a piece of the User page as NVRAM ===\r\n", &user_nvram_data); //*** Sleep mode // This program won't be doing anything else from now on, so it might as well // sleep. // Modules communicating with external circuits should normally be disabled // before entering a sleep mode that will stop the module operation. // For this application, we must disable the USART module that the DEBUG // software module is using. pcl_disable_module(DBG_USART_CLOCK_MASK); // Since we're going into a sleep mode deeper than IDLE, all HSB masters must // be stopped before entering the sleep mode. pcl_disable_module(AVR32_PDCA_CLK_HSB); pcl_disable_module(AVR32_PDCA_CLK_PBA); // If there is a chance that any PB write operations are incomplete, the CPU // should perform a read operation from any register on the PB bus before // executing the sleep instruction. AVR32_INTC.ipr[0]; // Dummy read // Go to STATIC sleep mode. SLEEP(AVR32_PM_SMODE_STATIC); while (true); }
/*! \brief The main function. */ int main(void) { int32_t result; #if UC3L /* UC3L series only */ // Note: on the AT32UC3L-EK board, there is no crystal/external clock connected // to the OSC0 pinout XIN0/XOUT0. We shall then program the DFLL and switch the // main clock source to the DFLL. pcl_configure_clocks(&pcl_dfll_freq_param); // Note: since it is dynamically computing the appropriate field values of the // configuration registers from the parameters structure, this function is not // optimal in terms of code size. For a code size optimal solution, it is better // to create a new function from pcl_configure_clocks_dfll0() and modify it // to use preprocessor computation from pre-defined target frequencies. #else // else for all other series // Configure Osc0 in crystal mode (i.e. use of an external crystal source, with // frequency FOSC0) with an appropriate startup time then switch the main clock // source to Osc0. pcl_switch_to_osc(PCL_OSC0, FOSC0, OSC0_STARTUP); #endif const gpio_map_t usart_gpio_map = { {EXAMPLE_USART_RX_PIN, EXAMPLE_USART_RX_FUNCTION}, {EXAMPLE_USART_TX_PIN, EXAMPLE_USART_TX_FUNCTION} }; // Assign GPIO to USART. gpio_enable_module(usart_gpio_map, sizeof(usart_gpio_map) / sizeof(usart_gpio_map[0])); static const usart_options_t usart_options = { .baudrate = 57600, .charlength = 8, .paritytype = USART_NO_PARITY, .stopbits = USART_1_STOPBIT, .channelmode = 0 }; // Initialize USART in RS232 mode. usart_init_rs232(EXAMPLE_USART, &usart_options, FPBA); usart_write_line(EXAMPLE_USART, "\x1B[2J\x1B[H\r\nATMEL\r\n"); usart_write_line(EXAMPLE_USART, "AVR UC3 - HMATRIX example\r\n\r\n"); // First test with AVR32_HMATRIX_DEFMSTR_TYPE_NO_DEFAULT print(EXAMPLE_USART, "- Test 1 ----------------------------------------\r\n"); print(EXAMPLE_USART, "------ All Slave Default Master Types are: ------\r\n"); print(EXAMPLE_USART, " - No Default Master\r\n"); print(EXAMPLE_USART, " - leds Toggle: "); print_ulong(EXAMPLE_USART, NB_TOGGLE); print(EXAMPLE_USART, " times\r\n"); #if defined(AVR32_HMATRIX) configure_hmatrix(AVR32_HMATRIX_DEFMSTR_TYPE_NO_DEFAULT); #elif defined(AVR32_HMATRIXB) configure_hmatrix(AVR32_HMATRIXB_DEFMSTR_TYPE_NO_DEFAULT); #endif result = toggle_led(NB_TOGGLE); // Second test with AVR32_HMATRIX_DEFMSTR_TYPE_LAST_DEFAULT print(EXAMPLE_USART, "- Test 2 ----------------------------------------\r\n"); print(EXAMPLE_USART, "------ All Slave Default Master Types are: ------\r\n"); print(EXAMPLE_USART, " - Last Default Master\r\n"); print(EXAMPLE_USART, " - No Default Master\r\n"); print(EXAMPLE_USART, " - leds Toggle: "); print_ulong(EXAMPLE_USART, NB_TOGGLE); print(EXAMPLE_USART, " times\r\n"); #if defined(AVR32_HMATRIX) configure_hmatrix(AVR32_HMATRIX_DEFMSTR_TYPE_LAST_DEFAULT); #elif defined(AVR32_HMATRIXB) configure_hmatrix(AVR32_HMATRIXB_DEFMSTR_TYPE_LAST_DEFAULT); #endif result -= toggle_led(NB_TOGGLE); print(EXAMPLE_USART, "--------------------------------------------------\r\n"); print_ulong(EXAMPLE_USART, result); print(EXAMPLE_USART, " Cycles saved between test 1 and test 2\r\nDone!"); //*** Sleep mode // This program won't be doing anything else from now on, so it might as well // sleep. // Modules communicating with external circuits should normally be disabled // before entering a sleep mode that will stop the module operation. // Make sure the USART dumps the last message completely before turning it off. while(!usart_tx_empty(EXAMPLE_USART)); pcl_disable_module(EXAMPLE_USART_CLOCK_MASK); // Since we're going into a sleep mode deeper than IDLE, all HSB masters must // be stopped before entering the sleep mode: none is acting currently in this // example so nothing to do for this example. // If there is a chance that any PB write operations are incomplete, the CPU // should perform a read operation from any register on the PB bus before // executing the sleep instruction. AVR32_INTC.ipr[0]; // Dummy read // Go to STATIC sleep mode. SLEEP(AVR32_PM_SMODE_STATIC); while (true); }
/*! * \brief main function : do init and loop (poll if configured so) */ int main(void) { char temp[20]; char *ptemp; uint32_t ast_alarm; static const gpio_map_t USART_GPIO_MAP = { {EXAMPLE_USART_RX_PIN, EXAMPLE_USART_RX_FUNCTION}, {EXAMPLE_USART_TX_PIN, EXAMPLE_USART_TX_FUNCTION} }; /* USART options */ static const usart_options_t USART_OPTIONS = { .baudrate = 57600, .charlength = 8, .paritytype = USART_NO_PARITY, .stopbits = USART_1_STOPBIT, .channelmode = 0 }; #if BOARD == UC3L_EK scif_osc32_opt_t opt = { /* 2-pin Crystal connected to XIN32/XOUT32 and high current * mode. */ SCIF_OSC_MODE_2PIN_CRYSTAL_HICUR, /* oscillator startup time */ AVR32_SCIF_OSCCTRL32_STARTUP_0_RCOSC, /* select alternate xin32_2 and xout32_2 for 32kHz crystal * oscillator */ true, /* disable the 1kHz output */ false, /* enable the 32kHz output */ true }; #else scif_osc32_opt_t opt; opt.mode = SCIF_OSC_MODE_2PIN_CRYSTAL; opt.startup = AVR32_SCIF_OSCCTRL32_STARTUP_0_RCOSC; #endif #if BOARD == UC3L_EK /* * Note: on the AT32UC3L-EK board, there is no crystal/external clock * connected to the OSC0 pinout XIN0/XOUT0. We shall then program the * DFLL and switch the main clock source to the DFLL. */ pcl_configure_clocks(&pcl_dfll_freq_param); /* * Note: since it is dynamically computing the appropriate field values * of the configuration registers from the parameters structure, this * function is not optimal in terms of code size. For a code size * optimal solution, it is better to create a new function from * pcl_configure_clocks_dfll0() and modify it to use preprocessor * computation from pre-defined target frequencies. */ #else pcl_switch_to_osc(PCL_OSC0, FOSC0, OSC0_STARTUP); #endif /* Start OSC_32KHZ */ scif_start_osc32(&opt, true); /* Assign GPIO pins to USART0. */ gpio_enable_module(USART_GPIO_MAP, sizeof(USART_GPIO_MAP) / sizeof(USART_GPIO_MAP[0])); /* Initialize USART in RS232 mode */ usart_init_rs232(EXAMPLE_USART, &USART_OPTIONS, FPBA); /* Welcome sentence // 2-pin Crystal and high current mode. */ /* Crystal is connected to XIN32/XOUT32. */ usart_write_line(EXAMPLE_USART, "\x1B[2J\x1B[H\r\nATMEL\r\n"); usart_write_line(EXAMPLE_USART, "AVR32 UC3 - AST example 2\r\n"); usart_write_line(EXAMPLE_USART, "AST 32 KHz oscillator counter example.\r\n"); usart_write_line(EXAMPLE_USART, "Alarm0 wakeup from static sleep mode every second.\r\n"); /* Using counter mode and set it to 0 */ unsigned long ast_counter = 0; /* Initialize the AST */ if (!ast_init_counter(&AVR32_AST, AST_OSC_32KHZ, AST_PSEL_32KHZ_1HZ, ast_counter)) { usart_write_line(EXAMPLE_USART, "Error initializing the AST\r\n"); while (1) { } } /* Alarm 0 sends a wakeup signal to the Power manager */ ast_enable_alarm_async_wakeup(&AVR32_AST, 0); /* Enable the AST */ ast_enable(&AVR32_AST); while (1) { /* disable alarm 0 */ ast_disable_alarm0(&AVR32_AST); /* ast_init_counter Set Alarm to current time+30 seconds */ ast_alarm = ast_counter + 1; ast_set_alarm0_value(&AVR32_AST, ast_alarm); /* Enable alarm 0 */ ast_enable_alarm0(&AVR32_AST); /* * Precautions when entering a sleep mode * Modules communicating with external circuits should normally * be disabled before entering a sleep mode that will stop the * module operation. * Make sure the USART dumps the last message completely before * turning it off. */ while (!usart_tx_empty(EXAMPLE_USART)) { } pcl_disable_module(EXAMPLE_USART_CLOCK_MASK); /* * Since we're going into a sleep mode deeper than IDLE, all HSB * masters must be stopped before entering the sleep mode. * Note: since we're not using the PDCA, we don't have to stop *it. */ /* * If there is a chance that any PB write operations are *incomplete, * the CPU should perform a read operation from any register on *the * PB bus before executing the sleep instruction. */ AVR32_INTC.ipr[0]; /* Dummy read */ /* Go into static sleep mode */ SLEEP(AVR32_PM_SMODE_STATIC); /* We're out of the static sleep mode now => re-enable the USART * module */ pcl_enable_module(EXAMPLE_USART_CLOCK_MASK); /* After wake up, clear the Alarm0 */ ast_clear_alarm_status_flag(&AVR32_AST, 0); /* Toggle Led0 */ gpio_tgl_gpio_pin(LED0_GPIO); /* Set cursor to the position (1; 6) */ usart_write_line(EXAMPLE_USART, "\x1B[6;1H"); ast_counter = ast_get_counter_value(&AVR32_AST); usart_write_line(EXAMPLE_USART, "Timer: "); ptemp = print_i(temp, ast_counter); usart_write_line(EXAMPLE_USART, ptemp); usart_write_line(EXAMPLE_USART, " sec "); } }