/*! \brief Main function. Execution starts here. * * \retval 42 Fatal error. */ int main(void) { #ifndef FREERTOS_USED Enable_global_exception(); INTC_init_interrupts(); #endif pcl_switch_to_osc(PCL_OSC0, FOSC0, OSC0_STARTUP); init_dbg_rs232(FOSC0); pcl_configure_usb_clock(); usb_task_init(); #if USB_DEVICE_FEATURE == true device_template_task_init(); #endif #if USB_HOST_FEATURE == true host_template_task_init(); #endif #ifdef FREERTOS_USED vTaskStartScheduler(); portDBG_TRACE("FreeRTOS returned."); return 42; #else while (true) { usb_task(); #if USB_DEVICE_FEATURE == true device_template_task(); #endif #if USB_HOST_FEATURE == true host_template_task(); #endif } #endif // FREERTOS_USED }
/*! \brief Main function. Execution starts here. * * \retval 42 Fatal error. */ int main(void) { // Configure system clocks. if (pcl_configure_clocks(&pcl_freq_param) != PASS) return 42; // Initialize USB clock (on PLL1) pcl_configure_usb_clock(); // Initialize usart comm init_dbg_rs232(pcl_freq_param.pba_f); // Initialize USB task usb_task_init(); #if USB_DEVICE_FEATURE == true // Initialize device generic USB task device_generic_hid_task_init(); #endif // No OS here. Need to call each task in round-robin mode. while (true) { usb_task(); #if USB_DEVICE_FEATURE == true device_generic_hid_task(); #endif } }
int main(void) { init_sys_clocks(); init_dbg_rs232(FPBA_HZ); print_dbg("AVR UC3 DSP DEMO\r\n"); irq_initialize_vectors(); // GUI, Controller and DSP process init gui_init(FCPU_HZ, FHSB_HZ, FPBA_HZ, FPBB_HZ); gui_text_print(GUI_COMMENT_ID, TEXT_IDLE); gui_text_print(GUI_FILTER_ID, filter_active_get_description()); controller_init(FCPU_HZ, FHSB_HZ, FPBA_HZ, FPBB_HZ); twi_init(); dsp_process_init(FCPU_HZ, FHSB_HZ, FPBA_HZ, FPBB_HZ); cpu_irq_enable(); // Main loop while (1) { gui_task(); controller_task(); dsp_process_task(); state_machine_task(); } }
//! @{ void twis_init (void) { twis_slave_fct_t twis_slave_fct; #if BOARD == UC3L_EK /** * \internal For UC3L devices, TWI default pins are, * TWIMS0 -> PB05,PA21 * TWIMS1 -> PB04 * To enable TWI clock/data in another pin, these have * to be assigned to other peripherals or as GPIO. * \endinternal */ gpio_enable_gpio_pin(AVR32_PIN_PB05); gpio_enable_gpio_pin(AVR32_PIN_PA21); #endif const gpio_map_t TWIS_GPIO_MAP = { {TEST_TWIS_TWCK_PIN, TEST_TWIS_TWCK_FUNCTION}, {TEST_TWIS_TWD_PIN, TEST_TWIS_TWD_FUNCTION} }; const twis_options_t TWIS_OPTIONS = { .pba_hz = FPBA_HZ, .speed = TWI_SPEED, .chip = SLAVE_ADDRESS, .smbus = false, }; // Assign I/Os to SPI. gpio_enable_module (TWIS_GPIO_MAP, sizeof (TWIS_GPIO_MAP) / sizeof (TWIS_GPIO_MAP[0])); // Set pointer to user specific application routines twis_slave_fct.rx = &twis_slave_rx; twis_slave_fct.tx = &twis_slave_tx; twis_slave_fct.stop = &twis_slave_stop; // Initialize as master. twis_slave_init (TWIS, &TWIS_OPTIONS, &twis_slave_fct); } //! @} /*! \brief Main function. */ /*! \remarks Main Function */ //! @{ int main (void) { // Configure the system clock init_sys_clocks (); // Init debug serial line init_dbg_rs232 (FPBA_HZ); // Display a header to user print_dbg ("Slave Example\r\n"); print_dbg ("Slave Started\r\n"); // Initialize and enable interrupt irq_initialize_vectors(); cpu_irq_enable(); // Initialize the TWIS Module twis_init (); while (true); }
/*! \brief This example shows how to access an external RAM connected to the SMC module. */ int main(void) { // Get base address of SRAM module volatile uint32_t *sram = SRAM; // Switch to external oscillator 0. pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP); // Initialize debug serial line init_dbg_rs232(FOSC0); // Display a header to user print_dbg("\x1B[2J\x1B[H\r\nSMC Example\r\n"); print_dbg("Board running at "); print_dbg_ulong(FOSC0 / 1000000); print_dbg(" MHz\r\n"); print_dbg("Initializing SRAM..."); // Initialize the external SRAM chip. smc_init(FOSC0); print_dbg("done\r\n\r\n"); print_dbg("Testing SRAM...\r\n"); // Test each address location inside the chip with a write/readback uint32_t total_tests = 0; uint32_t total_errors = 0; for (uint32_t total_tests = 0; total_tests < SRAM_SIZE; total_tests++) { sram[total_tests] = total_tests; if (total_tests != sram[total_tests]) { total_errors++; print_dbg("Error at 0x"); print_dbg_hex((uint32_t)&sram[total_tests]); print_dbg("\r\n"); } } if (total_errors == 0) { print_dbg("SRAM test successfully completed\r\n"); } else { print_dbg("SRAM test completed with "); print_dbg_ulong(total_errors); print_dbg(" errors out of "); print_dbg_ulong(total_tests); print_dbg(" tests\r\n"); } while (true); return 0; }
/*! \brief Main function. Execution starts here. * * \retval 42 Fatal error. */ int main(void) { // Configure system clocks. if (pcl_configure_clocks(&pcl_freq_param) != PASS) return 42; // Initialize USB clock (on PLL1) pcl_configure_usb_clock(); // Initialize usart comm init_dbg_rs232(pcl_freq_param.pba_f); #if BOARD == EVK1105 Disable_global_interrupt(); /* Register interrupt handler to the interrupt controller * up, down buttons on PB22, PB23 -> GPIO_IRQ_6 */ INTC_register_interrupt(&touch_button_isr, AVR32_GPIO_IRQ_6, 0); /* all gpios between PB23 - PB31) */ INTC_register_interrupt(&touch_button_isr, AVR32_GPIO_IRQ_7, 0); Enable_global_interrupt(); #endif // Initialize USB task usb_task_init(); #if USB_DEVICE_FEATURE == true // Initialize device mouse USB task device_mouse_hid_task_init(); #endif #if USB_HOST_FEATURE == true //host_keyboard_hid_task_init(); // Initialize host mouse USB task host_mouse_hid_task_init(); #endif #ifdef FREERTOS_USED // Start OS scheduler vTaskStartScheduler(); portDBG_TRACE("FreeRTOS returned."); return 42; #else // No OS here. Need to call each task in round-robin mode. while (true) { usb_task(); #if USB_DEVICE_FEATURE == true device_mouse_hid_task(); #endif #if USB_HOST_FEATURE == true //host_keyboard_hid_task(); host_mouse_hid_task(); #endif } #endif // FREERTOS_USED }
/*! \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 Main function. */ int main(void) { static const gpio_map_t TWI_GPIO_MAP = { {AVR32_TWI_SDA_0_0_PIN, AVR32_TWI_SDA_0_0_FUNCTION}, {AVR32_TWI_SCL_0_0_PIN, AVR32_TWI_SCL_0_0_FUNCTION} }; twi_options_t opt; twi_slave_fct_t twi_slave_fct; int status; // Switch to oscillator 0 pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP); // Init debug serial line init_dbg_rs232(FOSC0); // Initialize and enable interrupt irq_initialize_vectors(); cpu_irq_enable(); // Display a header to user print_dbg("\x0C\r\nTWI Example\r\nSlave!\r\n"); // TWI gpio pins configuration gpio_enable_module(TWI_GPIO_MAP, sizeof(TWI_GPIO_MAP) / sizeof(TWI_GPIO_MAP[0])); // options settings opt.pba_hz = FOSC0; opt.speed = TWI_SPEED; opt.chip = EEPROM_ADDRESS; // initialize TWI driver with options twi_slave_fct.rx = &twi_slave_rx; twi_slave_fct.tx = &twi_slave_tx; twi_slave_fct.stop = &twi_slave_stop; status = twi_slave_init(&AVR32_TWI, &opt, &twi_slave_fct ); // check init result if (status == TWI_SUCCESS) { // display test result to user print_dbg("Slave start:\tPASS\r\n"); } else { // display test result to user print_dbg("slave start:\tFAIL\r\n"); } while(1); }
/*! \brief Low-level initialization routine called during startup, before the * main function. */ int __low_level_init(void) { // Enable exceptions. Enable_global_exception(); // Initialize interrupt handling. INTC_init_interrupts(); // init debug serial line init_dbg_rs232(FOSC0); // Request initialization of data segments. return 1; }
int main(void) { // Switch main clock from internal RC to external Oscillator 0 pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP); init_dbg_rs232(FOSC0); gpio_enable_gpio_pin(AVR32_PIN_PB00); print_dbg("\r\n\nstart"); while (true) { delay_ms(250); print_dbg("."); gpio_tgl_gpio_pin(AVR32_PIN_PB00); } }
/*! \brief Main function running the example on both the flash array and the * User page. */ int main(void) { // Switch main clock to external oscillator 0 (crystal). pcl_switch_to_osc(PCL_OSC0, FOSC0, OSC0_STARTUP); // Initialize the debug USART module. init_dbg_rs232(FOSC0); // 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); while (true); }
int main(void) { sysclk_init(); init_dbg_rs232(FMCK_HZ); init_gpio(); assign_main_event_handlers(); init_events(); init_tc(); init_spi(); init_adc(); irq_initialize_vectors(); register_interrupts(); cpu_irq_enable(); init_usb_host(); init_monome(); if(flash_is_fresh()) { // nothing has been stored in the flash memory so far // so you need to initialize any variables you store in flash here with appropriate default values } else { // read from flash flash_read(); } clock_pulse = &clock; clock_external = !gpio_get_pin_value(B09); // start timers that track clock, ADCs (including the clock and the param knobs) and the front panel button timer_add(&clockTimer,120,&clockTimer_callback, NULL); timer_add(&keyTimer,50,&keyTimer_callback, NULL); timer_add(&adcTimer,100,&adcTimer_callback, NULL); clock_temp = 10000; // out of ADC range to force tempo // main loop - you probably don't need to do anything here as everything should be done by handlers while (true) { check_events(); } }
/*! \brief Low-level initialization routine called during startup, before the * main function. * * This version comes in replacement to the default one provided by the Newlib * add-ons library. * Newlib add-ons' _init_startup only calls init_exceptions, but Newlib add-ons' * exception and interrupt vectors are defined in the same section and Newlib * add-ons' interrupt vectors are not compatible with the interrupt management * of the INTC module. * More low-level initializations are besides added here. */ int _init_startup(void) { // Import the Exception Vector Base Address. extern void _evba; // Load the Exception Vector Base Address in the corresponding system register. Set_system_register(AVR32_EVBA, (int)&_evba); // Enable exceptions. Enable_global_exception(); // Initialize interrupt handling. INTC_init_interrupts(); // init debug serial line init_dbg_rs232(FOSC0); // Don't-care value for GCC. return 1; }
/*! \brief This is an example demonstrating the accelerometer * functionalities using the accelerometer driver. */ int main(void) { volatile unsigned long i; // switch to oscillator 0 pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP); // Initialize the debug USART module. init_dbg_rs232(FOSC0); acc_init(); // do a loop for (;;) { // slow down operations for ( i=0 ; i < 50000 ; i++); // display a header to user print_dbg("\x1B[2J\x1B[H\r\n\r\nAccelerometer Example\r\n\r\n"); // Get accelerometer acquisition and process data acc_update(); // test for fast or slow changes // depending on that, play with angles or moves if ( is_acc_slow() ) { print_mouse() ; print_angles() ; } else print_move() ; // MEUH // only text here , needs to be combined with PWM // meuh_stop is the "end" signal from PWM meuh_en = is_acc_meuh( meuh_stop ) ; } }
/* \brief Initialize board. * */ void init_board(void) { #ifdef MAX_SPEED init_sys_clocks(); #else pcl_switch_to_osc(PCL_OSC0, FOSC0, OSC0_STARTUP); #endif INTC_init_interrupts(); init_dbg_rs232(FPBA_HZ); // Activate LED0 & LED1 & LED2 & LED3 pins in GPIO output // mode and switch them off. gpio_set_gpio_pin(LED0_GPIO); gpio_set_gpio_pin(LED1_GPIO); gpio_set_gpio_pin(LED2_GPIO); gpio_set_gpio_pin(LED3_GPIO); et024006_Init(FCPU_HZ, FCPU_HZ); gpio_set_gpio_pin(ET024006DHU_BL_PIN); et024006_DrawFilledRect(0, 0, ET024006_WIDTH, ET024006_HEIGHT, WHITE); }
/** \brief Main application entry point - init and loop to display ADC values */ int main(void) { #if defined(EXAMPLE_ADC_TEMPERATURE_CHANNEL) signed short adc_value_temp = -1; #endif #if defined(EXAMPLE_ADC_LIGHT_CHANNEL) signed short adc_value_light = -1; #endif #if defined(EXAMPLE_ADC_POTENTIOMETER_CHANNEL) signed short adc_value_pot = -1; #endif /* Init system clocks */ sysclk_init(); /* init debug serial line */ init_dbg_rs232(sysclk_get_cpu_hz()); /* Assign and enable GPIO pins to the ADC function. */ gpio_enable_module(ADC_GPIO_MAP, sizeof(ADC_GPIO_MAP) / sizeof(ADC_GPIO_MAP[0])); /* Configure the ADC peripheral module. * Lower the ADC clock to match the ADC characteristics (because we * configured the CPU clock to 12MHz, and the ADC clock characteristics are * usually lower; cf. the ADC Characteristic section in the datasheet). */ AVR32_ADC.mr |= 0x1 << AVR32_ADC_MR_PRESCAL_OFFSET; adc_configure(&AVR32_ADC); /* Enable the ADC channels. */ #if defined(EXAMPLE_ADC_TEMPERATURE_CHANNEL) adc_enable(&AVR32_ADC, EXAMPLE_ADC_TEMPERATURE_CHANNEL); #endif #if defined(EXAMPLE_ADC_LIGHT_CHANNEL) adc_enable(&AVR32_ADC, EXAMPLE_ADC_LIGHT_CHANNEL); #endif #if defined(EXAMPLE_ADC_POTENTIOMETER_CHANNEL) adc_enable(&AVR32_ADC, EXAMPLE_ADC_POTENTIOMETER_CHANNEL); #endif /* Display a header to user */ print_dbg("\x1B[2J\x1B[H\r\nADC Example\r\n"); while (true) { /* Start conversions on all enabled channels */ adc_start(&AVR32_ADC); #if defined(EXAMPLE_ADC_TEMPERATURE_CHANNEL) /* Get value for the temperature adc channel */ adc_value_temp = adc_get_value(&AVR32_ADC, EXAMPLE_ADC_TEMPERATURE_CHANNEL); /* Display value to user */ print_dbg("HEX Value for Channel temperature : 0x"); print_dbg_hex(adc_value_temp); print_dbg("\r\n"); #endif #if defined(EXAMPLE_ADC_LIGHT_CHANNEL) /* Get value for the light adc channel */ adc_value_light = adc_get_value(&AVR32_ADC, EXAMPLE_ADC_LIGHT_CHANNEL); /* Display value to user */ print_dbg("HEX Value for Channel light : 0x"); print_dbg_hex(adc_value_light); print_dbg("\r\n"); #endif #if defined(EXAMPLE_ADC_POTENTIOMETER_CHANNEL) /* Get value for the potentiometer adc channel */ adc_value_pot = adc_get_value(&AVR32_ADC, EXAMPLE_ADC_POTENTIOMETER_CHANNEL); /* Display value to user */ print_dbg("HEX Value for Channel pot : 0x"); print_dbg_hex(adc_value_pot); print_dbg("\r\n"); #endif /* Slow down the display of converted values */ delay_ms(500); } return 0; }
// main function int main(void) { bool valid_block_found[NF_N_DEVICES]; U32 u32_nf_ids, i_dev, i_block; U8 maker_id, device_id; U32 i, j; // ECCHRS options. static const ecchrs_options_t ECCHRS_OPTIONS = { .typecorrect = ECCHRS_TYPECORRECT_4_BIT, .pagesize = ECCHRS_PAGESIZE_4_BIT_2112_W }; // switch to oscillator 0 pcl_switch_to_osc(PCL_OSC0, FOSC0, OSC0_STARTUP); // init debug serial interface init_dbg_rs232(FOSC0); nf_init(FOSC0); // init the nand flash driver with the correct HSB frequency nf_unprotect(); print_dbg("\r\nECCHRS example using Nand Flash.\r\n================================\r\n\r\n"); // - Simple test of the NF communication through the SMC. // - Find all bad blocks. // - Find a valid block for the remaining tests. nf_reset_nands(NF_N_DEVICES); print_dbg("\tCPU, HSB is at 12000000Mhz.\r\n"); print_dbg("\tPBA, PBB is at 12000000Mhz.\r\n"); print_dbg("\tDetecting Nand Flash device(s).\r\n"); for( i_dev=0 ; i_dev<NF_N_DEVICES ; i_dev++ ) { // Performs some init here... valid_block_found[i_dev] = false; // Test Maker and Device ids u32_nf_ids = nf_read_id( NF_READ_ID_CMD, i_dev ); maker_id = MSB0(u32_nf_ids); device_id = MSB1(u32_nf_ids); print_dbg("\t\tNF"); print_dbg_hex(i_dev); print_dbg(" [Maker="); print_dbg_hex(maker_id); print_dbg("] [Device="); print_dbg_hex(device_id); print_dbg("]\r\n"); if( maker_id==M_ID_MICRON ) { print_dbg("\t\t Micron chip"); if( device_id==0xDA ){ print_dbg("- MT29F2G08AACWP device\r\n"); } else { print_dbg("- *** Error: unexpected chip detected. Please check the board settings and hardware.\r\n"); return -1; } } else { print_dbg("\t\t *** Error: unexpected chip detected. Please check the board settings and hardware.\r\n"); return -1; } } // Looking for valid blocks for the test. for( i_dev=0 ; i_dev<NF_N_DEVICES ; i_dev++ ) { nf_select(i_dev); for( i_block=0 ; i_block<G_N_BLOCKS ; i_block++ ) { nf_open_page_read( nf_block_2_page(i_block), NF_SPARE_POS + G_OFST_BLK_STATUS ); if( (nf_rd_data()==0xFF) ) { // The block is valid. print_dbg("\tValid block found ("); print_dbg_ulong(i_block); print_dbg(") on NF "); print_dbg_hex(i_dev); print_dbg("\r\n"); valid_block_found[i_dev]= true; valid_block_addr[i_dev] = i_block; break; } } } for( i_dev=0 ; i_dev<NF_N_DEVICES ; i_dev++ ) if( !valid_block_found[i_dev] ) { print_dbg("Error: no valid blocks found.\r\n"); return 0; } print_dbg("\tECCHRS IP ver "); print_dbg_ulong(ecchrs->version); print_dbg("("); print_dbg_hex(ecchrs->version); print_dbg(")"); print_dbg("\r\n"); // Work with NF 0 from now... nf_select(0); // Setup the ECCHRS. ecchrs_init(&AVR32_ECCHRS, &ECCHRS_OPTIONS); // Ensures that the block is erased for the test. print_dbg("\tErasing a free block for the test.\r\n"); nf_erase_block(nf_block_2_page(valid_block_addr[0]), false); // Reset the ECCHRS state machine. ecchrs_reset(&AVR32_ECCHRS); // Program a simple patterns in the first page. print_dbg("\tProgramming the first page with a simple pattern.\r\n"); nf_open_page_write( nf_block_2_page(valid_block_addr[0]), 0); for ( i = 0; i < 2048/2; i++ ) { U16 val = i; nf_wr_data(MSB(val)); nf_wr_data(LSB(val)); } // Extract the ECCs and store them at the end of the page. // [2054; 2063]: codeword 0:9 // [2070; 2079]: codeword 10:19 // [2086; 2095]: codeword 20:29 // [2102; 2111]: codeword 30:39 // Since we use the Random Data Output command, we need to freeze // the ECCHRS in order to keep our ECC codewords unchanged. print_dbg("\tExtracting the ECCHRS codewords and store them in the page.\r\n"); ecchrs_freeze(&AVR32_ECCHRS); // not needed if ECCHRS reaches the end of page. for ( i=0 ; i<4 ; i++ ) { U16 offset = 2048+6+16*i; nf_wr_cmd(NF_RANDOM_DATA_INPUT_CMD); nf_wr_addr( LSB(offset) ); nf_wr_addr( MSB(offset) ); for ( j=0 ; j<10 ; j++ ) nf_wr_data( ecchrs_get_cw(&AVR32_ECCHRS, i*10+j) ); } ecchrs_unfreeze(&AVR32_ECCHRS); nf_wr_cmd(NF_PAGE_PROGRAM_CMD); // Now let's test the ECC verification. print_dbg("\tReading the first page.\r\n"); nf_open_page_read( nf_block_2_page(valid_block_addr[0]), 0); for( i=0 ; i<2048 ; i++ ) nf_rd_data(); print_dbg("\tChecking if the data are valid thanks to the ECCHRS codewords.\r\n"); for ( i=0 ; i<4 ; i++ ) { U16 offset = 2048+6+16*i; ecchrs_freeze(&AVR32_ECCHRS); nf_wr_cmd(NF_RANDOM_READ_CMD_C1); nf_wr_addr( LSB(offset) ); nf_wr_addr( MSB(offset) ); nf_wr_cmd(NF_RANDOM_READ_CMD_C2); ecchrs_unfreeze(&AVR32_ECCHRS); for ( j=0 ; j<10 ; j++ ) nf_rd_data(); } // Check if there is any errors after the read of the page. i = ecchrs_4bit_check_error(&AVR32_ECCHRS); print_dbg("\tSR1 is "); print_dbg_ulong(i); print_dbg("("); print_dbg_hex(i); print_dbg(")"); print_dbg("\r\n"); if(i&(15)) { print_dbg("\tERROR: ECCHRS detects some errors in the sectors 1, 2, 3 or 4\r\n"); } else print_dbg("\tNo error detected.\r\n"); // Let the block free. nf_erase_block(nf_block_2_page(valid_block_addr[0]), false); return 0; }
/*! \brief Sets the SDRAM Controller up, initializes the SDRAM found on the * board and tests it. */ int main(void) { unsigned long sdram_size, progress_inc, i, j, tmp, noErrors = 0; volatile unsigned long *sdram = SDRAM; // Switch to external oscillator 0. pcl_switch_to_osc(PCL_OSC0, FOSC0, OSC0_STARTUP); // Initialize the debug USART module. init_dbg_rs232(FOSC0); // Calculate SDRAM size in words (32 bits). sdram_size = SDRAM_SIZE >> 2; print_dbg("\x0CSDRAM size: "); print_dbg_ulong(SDRAM_SIZE >> 20); print_dbg(" MB\r\n"); // Initialize the external SDRAM chip. sdramc_init(FOSC0); print_dbg("SDRAM initialized\r\n"); // Determine the increment of SDRAM word address requiring an update of the // printed progression status. progress_inc = (sdram_size + 50) / 100; // Fill the SDRAM with the test pattern. for (i = 0, j = 0; i < sdram_size; i++) { if (i == j * progress_inc) { LED_Toggle(LED_SDRAM_WRITE); print_dbg("\rFilling SDRAM with test pattern: "); print_dbg_ulong(j++); print_dbg_char('%'); } sdram[i] = i; } LED_Off(LED_SDRAM_WRITE); print_dbg("\rSDRAM filled with test pattern \r\n"); // Recover the test pattern from the SDRAM and verify it. for (i = 0, j = 0; i < sdram_size; i++) { if (i == j * progress_inc) { LED_Toggle(LED_SDRAM_READ); print_dbg("\rRecovering test pattern from SDRAM: "); print_dbg_ulong(j++); print_dbg_char('%'); } tmp = sdram[i]; if (tmp != i) { noErrors++; } } LED_Off(LED_SDRAM_READ); print_dbg("\rSDRAM tested: "); print_dbg_ulong(noErrors); print_dbg(" corrupted word(s) \r\n"); if (noErrors) { LED_Off(LED_SDRAM_ERRORS); while (1) { LED_Toggle(LED_SDRAM_ERRORS); cpu_delay_ms(200, FOSC0); // Fast blink means errors. } } else { LED_Off(LED_SDRAM_OK); while (1) { LED_Toggle(LED_SDRAM_OK); cpu_delay_ms(1000, FOSC0); // Slow blink means OK. } } }
/*! \brief Main function. Execution starts here. * * \retval 42 Fatal error. */ int main(void) { // Configure system clocks. if (pcl_configure_clocks(&pcl_freq_param) != PASS) return 42; // Initialize usart comm init_dbg_rs232(pcl_freq_param.pba_f); #ifndef FREERTOS_USED # if (defined __GNUC__) // Give the used CPU clock frequency to Newlib, so it can work properly. set_cpu_hz(pcl_freq_param.pba_f); # endif #endif // Initialize USB clock. pcl_configure_usb_clock(); // Initialize USB task usb_task_init(); // Display a welcome banner on USART printf(" ...... ...... \r\n"); printf(" IIIIII IIIII IIII IIIIIIIIIII IIIIIIIIIII. .IIIIIIIIII. \r\n"); printf(" IIIIIII IIIII IIIII IIIIIIIIIIIII IIIIIIIIIIII..IIIIIIIIIII. \r\n"); printf(" IIIIIIIII IIIII IIIII IIIII IIIII I. IIIII.:. IIIII \r\n"); printf(" IIII IIIII IIIII IIII IIIII IIIII .IIII. IIIII \r\n"); printf(" IIIII IIII IIII IIIII IIIIIIIIIIIIII IIIIIIII IIII. \r\n"); printf(" IIII IIIII IIIII IIIII IIIIIIIIIIIII IIIIIIII. .IIII: \r\n"); printf(" IIIII IIIII IIIIIIIII IIIIIIIIIII .IIIII IIIII. \r\n"); printf(" IIIIIIIIIIIIIII IIIIIIII IIIII IIIII .IIII .IIII: \r\n"); printf(" IIIIIIIIIIIIIIII IIIIIII IIIII IIII II:. .IIIII .IIIII. \r\n"); printf(" IIIII IIIII IIIIII IIIII IIIII IIIIIIIIIIIII.IIIIIIIIIIIIII\r\n"); printf(" IIIII IIIII IIIII IIIII IIIII :IIIIIIIIII. IIIIIIIIIIIIII\r\n"); printf(" III \r\n"); printf(" II \r\n"); #if USB_DEVICE_FEATURE == true // Initialize device CDC USB task device_cdc_task_init(); #endif #if USB_HOST_FEATURE == true // Initialize host CDC USB task host_cdc_task_init(); #endif #ifdef FREERTOS_USED // Start OS scheduler vTaskStartScheduler(); portDBG_TRACE("FreeRTOS returned."); return 42; #else // No OS here. Need to call each task in round-robin mode. while (true) { usb_task(); #if USB_DEVICE_FEATURE == true device_cdc_task(); #endif #if USB_HOST_FEATURE == true host_cdc_task(); #endif } #endif // FREERTOS_USED }
/** \brief main function : do init and loop to display ACIFA values */ int main( void ) { /* Init system clocks */ sysclk_init(); /* init debug serial line */ init_dbg_rs232(sysclk_get_cpu_hz()); /* Assign and enable GPIO pins to the AC function. */ gpio_enable_module(ACIFA_GPIO_MAP, sizeof(ACIFA_GPIO_MAP) / sizeof(ACIFA_GPIO_MAP[0])); /* Configure ACIFA0 */ acifa_configure(&AVR32_ACIFA0, ACIFA_COMP_SELA, EXAMPLE_AC_ACMP1_INPUT, EXAMPLE_AC_ACMPN1_INPUT, FOSC0); acifa_configure(&AVR32_ACIFA0, ACIFA_COMP_SELB, EXAMPLE_AC_ACMP2_INPUT, EXAMPLE_AC_ACMPN2_INPUT, FOSC0); /* Start the ACIFA0. */ acifa_start(&AVR32_ACIFA0, (ACIFA_COMP_SELA | ACIFA_COMP_SELB)); /* Configure ACIFA1 */ acifa_configure(&AVR32_ACIFA1, ACIFA_COMP_SELA, EXAMPLE_AC_ACMP0_INPUT, EXAMPLE_AC_ACMPN0_INPUT, FOSC0); /* Start the ACIFA1. */ acifa_start(&AVR32_ACIFA1, ACIFA_COMP_SELA); /* Display a header to user */ print_dbg("\x1B[2J\x1B[H\r\nACIFA Example\r\n"); while (true) { if (acifa_is_aca_inp_higher(&AVR32_ACIFA1)) { print_dbg("ACMP0 > ACMPN0"); print_dbg("\r\n"); } else { print_dbg("ACMP0 < ACMPN0"); print_dbg("\r\n"); } if (acifa_is_aca_inp_higher(&AVR32_ACIFA0)) { print_dbg("ACMP1 > ACMPN1"); print_dbg("\r\n"); } else { print_dbg("ACMP1 < ACMPN1"); print_dbg("\r\n"); } if (acifa_is_acb_inp_higher(&AVR32_ACIFA0)) { print_dbg("ACMP2 > ACMPN2"); print_dbg("\r\n"); } else { print_dbg("ACMP2 < ACMPN2"); print_dbg("\r\n"); } /* Slow down display of comparison values */ delay_ms(100); } }
/** Main function to configure the CAN, and begin transfers. */ int main(void) { /* Initialize the system clocks */ sysclk_init(); /* Setup the generic clock for CAN */ scif_gc_setup(AVR32_SCIF_GCLK_CANIF, SCIF_GCCTRL_OSC0, AVR32_SCIF_GC_NO_DIV_CLOCK, 0); /* Now enable the generic clock */ scif_gc_enable(AVR32_SCIF_GCLK_CANIF); init_dbg_rs232(FPBA_HZ); /* Disable all interrupts. */ Disable_global_interrupt(); /* Initialize interrupt vectors. */ INTC_init_interrupts(); static const gpio_map_t CAN_GPIO_MAP = { {AVR32_CANIF_RXLINE_0_0_PIN, AVR32_CANIF_RXLINE_0_0_FUNCTION}, {AVR32_CANIF_TXLINE_0_0_PIN, AVR32_CANIF_TXLINE_0_0_FUNCTION} }; /* Assign GPIO to CAN. */ gpio_enable_module(CAN_GPIO_MAP, sizeof(CAN_GPIO_MAP) / sizeof(CAN_GPIO_MAP[0])); /* Initialize channel 0 */ can_init(CAN_CHANNEL_EXAMPLE, ((uint32_t)&mob_ram_ch0[0]), CANIF_CHANNEL_MODE_LISTENING, can_out_callback_channel0); /* Enable all interrupts. */ Enable_global_interrupt(); print_dbg("\r\nUC3C CAN Examples 1\r\n"); print_dbg(CAN_Wakeup); /* Initialize CAN Channel 0 */ can_init(CAN_CHANNEL_EXAMPLE, ((uint32_t)&mob_ram_ch0[0]), CANIF_CHANNEL_MODE_NORMAL, can_out_callback_channel0); /* Allocate one mob for RX */ appli_rx_msg.handle = can_mob_alloc(CAN_CHANNEL_EXAMPLE); /* Initialize RX message */ can_rx(CAN_CHANNEL_EXAMPLE, appli_rx_msg.handle, appli_rx_msg.req_type, appli_rx_msg.can_msg); /* Enable Async Wake Up Mode */ pm_asyn_wake_up_enable(CAN_WAKEUP_MASK_EXAMPLE); /* ---------SLEEP MODE PROCEDURE------------- */ /* Disable CAN Channel 0 */ CANIF_disable(CAN_CHANNEL_EXAMPLE); /* Wait CAN Channel 0 is disabled */ while(!CANIF_channel_enable_status(CAN_CHANNEL_EXAMPLE)); /* Enable Wake-Up Mode */ CANIF_enable_wakeup(CAN_CHANNEL_EXAMPLE); /* Go to sleep mode. */ SLEEP(AVR32_PM_SMODE_STATIC); /* ---------SLEEP MODE PROCEDURE------------- */ print_dbg(CAN_WakeupD); /* Initialize again CAN Channel 0 */ can_init(CAN_CHANNEL_EXAMPLE, ((uint32_t)&mob_ram_ch0[0]), CANIF_CHANNEL_MODE_NORMAL, can_out_callback_channel0); /* Allocate one mob for RX */ appli_rx_msg.handle = can_mob_alloc(CAN_CHANNEL_EXAMPLE); /* Initialize RX message */ can_rx(CAN_CHANNEL_EXAMPLE, appli_rx_msg.handle, appli_rx_msg.req_type, appli_rx_msg.can_msg); for (;;) { /* Do nothing; interrupts handle the DAC conversions */ } }
/*! main function */ int main(void) { init_sys_clocks(); // Initialize RS232 debug text output. init_dbg_rs232(FOSC0); print_dbg(MSG_WELCOME); // Enable LED0 and LED1 gpio_enable_gpio_pin(LED0_GPIO); gpio_enable_gpio_pin(LED1_GPIO); // Configure TWI as master twi_init(); // Initialize TPA6130 tpa6130_init(); // Initialize DAC that send audio to TPA6130 tpa6130_dac_start(DEFAULT_DAC_SAMPLE_RATE_HZ, DEFAULT_DAC_NUM_CHANNELS, DEFAULT_DAC_BITS_PER_SAMPLE, DEFAULT_DAC_SWAP_CHANNELS, master_callback, AUDIO_DAC_OUT_OF_SAMPLE_CB | AUDIO_DAC_RELOAD_CB, FOSC0); tpa6130_set_volume(0x2F); tpa6130_get_volume(); int count = 0; int i=0; while(true) { count = 0; // Store sample from the sound_table array while(count < (SOUND_SAMPLES)){ samples[count++] = ((uint8_t)sound_table[i]+0x80) << 8; samples[count++] = ((uint8_t)sound_table[i]+0x80) << 8; i++; if (i >= sizeof(sound_table)) i = 0; } gpio_set_gpio_pin(LED0_GPIO); gpio_clr_gpio_pin(LED1_GPIO); // Play buffer tpa6130_dac_output((void *) samples,SOUND_SAMPLES/2); gpio_clr_gpio_pin(LED0_GPIO); gpio_set_gpio_pin(LED1_GPIO); /* Wait until the reload register is empty. * This means that one transmission is still ongoing * but we are already able to set up the next transmission */ while(!tpa6130_dac_output(NULL, 0)); } }
/** ** USART init. **/ void init_usart(void) { init_dbg_rs232(FCPU_HZ); print_dbg("\x0CPEVC Driver - EXAMPLE 1\r\n"); print_dbg("USART transfer using PEVC, AST and PDCA\r\n"); }
int main(void) { u8 i1; sysclk_init(); init_dbg_rs232(FMCK_HZ); init_gpio(); assign_main_event_handlers(); init_events(); init_tc(); init_spi(); init_adc(); irq_initialize_vectors(); register_interrupts(); cpu_irq_enable(); init_usb_host(); init_monome(); init_i2c_slave(0x30); print_dbg("\r\n\n// meadowphysics //////////////////////////////// "); print_dbg_ulong(sizeof(flashy)); print_dbg(" "); print_dbg_ulong(sizeof(m)); if(flash_is_fresh()) { print_dbg("\r\nfirst run."); flash_unfresh(); flashc_memset32((void*)&(flashy.preset_select), 0, 4, true); // clear out some reasonable defaults for(i1=0;i1<8;i1++) { m.positions[i1] = i1; m.points[i1] = i1; m.points_save[i1] = i1; m.triggers[i1] = 0; m.trig_dests[i1] = 0; m.rules[i1] = 0; m.rule_dests[i1] = i1; } m.positions[0] = m.points[0] = 3; m.trig_dests[0] = 254; // save all presets, clear glyphs for(i1=0;i1<8;i1++) { flashc_memcpy((void *)&flashy.m[i1], &m, sizeof(m), true); glyph[i1] = (1<<i1); flashc_memcpy((void *)&flashy.glyph[i1], &glyph, sizeof(glyph), true); } } else { // load from flash at startup preset_select = flashy.preset_select; flash_read(); for(i1=0;i1<8;i1++) glyph[i1] = flashy.glyph[preset_select][i1]; } LENGTH = 15; SIZE = 16; re = &refresh; process_ii = &mp_process_ii; clock_pulse = &clock; clock_external = !gpio_get_pin_value(B09); timer_add(&clockTimer,120,&clockTimer_callback, NULL); timer_add(&keyTimer,50,&keyTimer_callback, NULL); timer_add(&adcTimer,100,&adcTimer_callback, NULL); clock_temp = 10000; // out of ADC range to force tempo while (true) { check_events(); } }
/*! \brief Main function. Execution starts here. */ int main(void) { U32 n_sector = 0; U32 card_size; // Unit is in sector. U32 bench_start_sector; U16 i = 0; U16 j = 0; t_cpu_time timer; Ctrl_status status; // Set CPU and PBA clock if( PM_FREQ_STATUS_FAIL==pm_configure_clocks(&pm_freq_param) ) return 42; // Initialize HMatrix init_hmatrix(); // Initialize debug RS232 with PBA clock init_dbg_rs232(pm_freq_param.pba_f); // Start test print_dbg("\r\nInitialize SD/MMC driver"); // Initialize SD/MMC driver resources: GPIO, SDIO and SD/MMC. sd_mmc_mci_resources_init(); // Wait for a card to be inserted #if (BOARD == EVK1104) #if EXAMPLE_SD_SLOT == SD_SLOT_8BITS print_dbg("\r\nInsert a MMC/SD card into the SD/MMC 8bits slot..."); #elif EXAMPLE_SD_SLOT == SD_SLOT_4BITS print_dbg("\r\nInsert a MMC/SD card into the SD/MMC 4bits slot..."); #else # error SD_SLOT not supported #endif while (!sd_mmc_mci_mem_check(EXAMPLE_SD_SLOT)); #else # error Board not supported #endif print_dbg("Card detected!\r\n"); // Read Card capacity sd_mmc_mci_read_capacity(EXAMPLE_SD_SLOT, &card_size); print_dbg("\r\nCapacity = "); print_dbg_ulong(card_size*512); print_dbg(" Bytes\r\n"); // Read the first sector number 0 of the card status = sd_mmc_mci_mem_2_ram(EXAMPLE_SD_SLOT, 0, buffer_in); if( status!=CTRL_GOOD ) { print_dbg("\r\nERROR: can not read device.\r\n"); return -1; } // Display the ram_buffer content print_dbg("\r\nFirst sector of the card:\r\n"); for (i=0;i<(512);i++) { print_dbg_char_hex(buffer_in[i]); j++; if (j%32==0) print_dbg("\r\n"), j=0; else if (j%4==0) print_dbg(" "); } // Write some patterns in the first sector number 0 of the card print_dbg("Testing write.\r\n"); if( !test_sd_mmc_write(0) ) return -1; if( !test_sd_mmc_write(1) ) return -1; if( !test_sd_mmc_write(2) ) return -1; if( !test_sd_mmc_write(3) ) return -1; // Bench single-block read operations without DMA // print_dbg("Benching single-block read (without DMA). Please wait..."); n_sector = bench_start_sector = (card_size<BENCH_START_SECTOR) ? 0 : BENCH_START_SECTOR; cpu_set_timeout( cpu_ms_2_cy(BENCH_TIME_MS, pm_freq_param.cpu_f), &timer); while( !cpu_is_timeout(&timer) ) { status = sd_mmc_mci_mem_2_ram(EXAMPLE_SD_SLOT, n_sector, buffer_in); if( status!=CTRL_GOOD ) { print_dbg("\r\nERROR: can not read device.\r\n"); return -1; } n_sector+=1; } display_perf_bps((((U64)n_sector-bench_start_sector)*512)/(BENCH_TIME_MS/1000)); // Bench single-block read operations with DMA // print_dbg("Benching single-block read (with DMA). Please wait..."); n_sector = bench_start_sector = (card_size<BENCH_START_SECTOR) ? 0 : BENCH_START_SECTOR; cpu_set_timeout( cpu_ms_2_cy(BENCH_TIME_MS, pm_freq_param.cpu_f), &timer); while( !cpu_is_timeout(&timer) ) { status = sd_mmc_mci_dma_mem_2_ram(EXAMPLE_SD_SLOT, n_sector, buffer_in); if( status!=CTRL_GOOD ) { print_dbg("\r\nERROR: can not read device.\r\n"); return -1; } n_sector+=1; } display_perf_bps((((U64)n_sector-bench_start_sector)*512)/(BENCH_TIME_MS/1000)); // Bench multi-block read operations without DMA // print_dbg("Benching multi-block read (without DMA). Please wait..."); n_sector = bench_start_sector = (card_size<BENCH_START_SECTOR) ? 0 : BENCH_START_SECTOR; cpu_set_timeout( cpu_ms_2_cy(BENCH_TIME_MS, pm_freq_param.cpu_f), &timer); while( !cpu_is_timeout(&timer) ) { status = sd_mmc_mci_multiple_mem_2_ram(EXAMPLE_SD_SLOT, n_sector, buffer_in, ALLOCATED_SECTORS); if( status!=CTRL_GOOD ) { print_dbg("\r\nERROR: can not read device.\r\n"); return -1; } n_sector+=ALLOCATED_SECTORS; } display_perf_bps((((U64)n_sector-bench_start_sector)*512)/(BENCH_TIME_MS/1000)); // Bench multi-block read operations with DMA // print_dbg("Benching multi-block read (with DMA). Please wait..."); n_sector = bench_start_sector = (card_size<BENCH_START_SECTOR) ? 0 : BENCH_START_SECTOR; cpu_set_timeout( cpu_ms_2_cy(BENCH_TIME_MS, pm_freq_param.cpu_f), &timer); while( !cpu_is_timeout(&timer) ) { status = sd_mmc_mci_dma_multiple_mem_2_ram(EXAMPLE_SD_SLOT, n_sector, buffer_in, ALLOCATED_SECTORS); if( status!=CTRL_GOOD ) { print_dbg("\r\nERROR: can not read device.\r\n"); return -1; } n_sector+=ALLOCATED_SECTORS; } display_perf_bps((((U64)n_sector-bench_start_sector)*512)/(BENCH_TIME_MS/1000)); // Bench single-block write operations without DMA // print_dbg("Benching single-block write (without DMA). Please wait..."); status = sd_mmc_mci_mem_2_ram(EXAMPLE_SD_SLOT, 0, buffer_in); // Backup the first sector number 0 of the card if( status!=CTRL_GOOD ) { print_dbg("\r\nERROR: can not read device.\r\n"); return -1; } n_sector = bench_start_sector = (card_size<BENCH_START_SECTOR) ? 0 : BENCH_START_SECTOR; cpu_set_timeout( cpu_ms_2_cy(BENCH_TIME_MS, pm_freq_param.cpu_f), &timer); while( !cpu_is_timeout(&timer) ) { status = sd_mmc_mci_ram_2_mem(EXAMPLE_SD_SLOT, n_sector, buffer_in); if( status!=CTRL_GOOD ) { print_dbg("\r\nERROR: can not write device.\r\n"); return -1; } n_sector+=1; } display_perf_bps((((U64)n_sector-bench_start_sector)*512)/(BENCH_TIME_MS/1000)); // Bench single-block write operations with DMA // print_dbg("Benching single-block write (with DMA). Please wait..."); status = sd_mmc_mci_mem_2_ram(EXAMPLE_SD_SLOT, 0, buffer_in); // Backup the first sector number 0 of the card if( status!=CTRL_GOOD ) { print_dbg("\r\nERROR: can not read device.\r\n"); return -1; } n_sector = bench_start_sector = (card_size<BENCH_START_SECTOR) ? 0 : BENCH_START_SECTOR; cpu_set_timeout( cpu_ms_2_cy(BENCH_TIME_MS, pm_freq_param.cpu_f), &timer); while( !cpu_is_timeout(&timer) ) { status = sd_mmc_mci_dma_ram_2_mem(EXAMPLE_SD_SLOT, n_sector, buffer_in); if( status!=CTRL_GOOD ) { print_dbg("\r\nERROR: can not write device.\r\n"); return -1; } n_sector+=1; } display_perf_bps((((U64)n_sector-bench_start_sector)*512)/(BENCH_TIME_MS/1000)); // Bench multi-block write operations without DMA // print_dbg("Benching multi-block write (without DMA). Please wait..."); status = sd_mmc_mci_mem_2_ram(EXAMPLE_SD_SLOT, 0, buffer_in); // Backup the first sector number 0 of the card if( status!=CTRL_GOOD ) { print_dbg("\r\nERROR: can not read device.\r\n"); return -1; } n_sector = bench_start_sector = (card_size<BENCH_START_SECTOR) ? 0 : BENCH_START_SECTOR; cpu_set_timeout( cpu_ms_2_cy(BENCH_TIME_MS, pm_freq_param.cpu_f), &timer); while( !cpu_is_timeout(&timer) ) { status = sd_mmc_mci_multiple_ram_2_mem(EXAMPLE_SD_SLOT, n_sector, buffer_in, ALLOCATED_SECTORS); if( status!=CTRL_GOOD ) { print_dbg("\r\nERROR: can not write device.\r\n"); return -1; } n_sector+=ALLOCATED_SECTORS; } display_perf_bps((((U64)n_sector-bench_start_sector)*512)/(BENCH_TIME_MS/1000)); // Bench multi-block write operations with DMA // print_dbg("Benching multi-block write (with DMA). Please wait..."); status = sd_mmc_mci_mem_2_ram(EXAMPLE_SD_SLOT, 0, buffer_in); // Backup the first sector number 0 of the card if( status!=CTRL_GOOD ) { print_dbg("\r\nERROR: can not read device.\r\n"); return -1; } n_sector = bench_start_sector = (card_size<BENCH_START_SECTOR) ? 0 : BENCH_START_SECTOR; cpu_set_timeout( cpu_ms_2_cy(BENCH_TIME_MS, pm_freq_param.cpu_f), &timer); while( !cpu_is_timeout(&timer) ) { status = sd_mmc_mci_dma_multiple_ram_2_mem(EXAMPLE_SD_SLOT, n_sector, buffer_in, ALLOCATED_SECTORS); if( status!=CTRL_GOOD ) { print_dbg("\r\nERROR: can not write device.\r\n"); return -1; } n_sector+=ALLOCATED_SECTORS; } display_perf_bps((((U64)n_sector-bench_start_sector)*512)/(BENCH_TIME_MS/1000)); return 0; }
/** * \brief Main Application Routine * - Initialize the system clocks * - Initialize the sleep manager * - Initialize the power save measures * - Initialize the ACIFB module * - Initialize the AST to trigger ACIFB at regular intervals * - Go to STATIC sleep mode and wake up on a touch status change */ int main(void) { /* Switch on the STATUS LED */ gpio_clr_gpio_pin(STATUS_LED); /* Switch off the error LED. */ gpio_set_gpio_pin(ERROR_LED); /* * Initialize the system clock. * Note: Clock settings are specified in conf_clock.h */ sysclk_init(); /* * Initialize the sleep manager. * Note: CONFIG_SLEEPMGR_ENABLE should have been defined in conf_sleepmgr.h */ sleepmgr_init(); /* Lock required sleep mode. */ sleepmgr_lock_mode(SLEEPMGR_STATIC); /* Initialize the delay routines */ delay_init(sysclk_get_cpu_hz()); /* Initialize the power saving features */ power_save_measures_init(); /* Switch off the error LED. */ gpio_set_gpio_pin(ERROR_LED); #if DEBUG_MESSAGES /* Enable the clock to USART interface */ sysclk_enable_peripheral_clock(DBG_USART); /* Initialize the USART interface to print trace messages */ init_dbg_rs232(sysclk_get_pba_hz()); print_dbg("\r Sleepwalking with ACIFB Module in UC3L \n"); print_dbg("\r Initializing ACIFB Module..... \n"); #endif /* Initialize the Analog Comparator peripheral */ if (ac_init() != STATUS_OK) { #if DEBUG_MESSAGES /* Error initializing the ACIFB peripheral */ print_dbg("\r Error initializing Analog Comparator module \n"); #endif while (1) { delay_ms(LED_DELAY); gpio_tgl_gpio_pin(ERROR_LED); } } #if DEBUG_MESSAGES print_dbg("\r ACIFB Module initialized. \n"); print_dbg("\r Initializing AST Module..... \n"); #endif /* Initialize the AST peripheral */ if (ast_init() != STATUS_OK) { #if DEBUG_MESSAGES print_dbg("\r Error initializing AST module \n"); #endif /* Error initializing the AST peripheral */ while (1) { delay_ms(LED_DELAY); gpio_tgl_gpio_pin(ERROR_LED); } } #if DEBUG_MESSAGES print_dbg("\r AST Module initialized. \n"); #endif /* Application routine */ while (1) { /* Enable Asynchronous wake-up for ACIFB */ pm_asyn_wake_up_enable(AVR32_PM_AWEN_ACIFBWEN_MASK); #if DEBUG_MESSAGES print_dbg("\r Going to STATIC sleep mode \n"); print_dbg( "\r Wake up only when input is higher than threshold \n"); #endif /* Switch off the status LED */ gpio_set_gpio_pin(STATUS_LED); /* Disable GPIO clock before entering sleep mode */ sysclk_disable_pba_module(SYSCLK_GPIO); AVR32_INTC.ipr[0]; /* Enter STATIC sleep mode */ sleepmgr_enter_sleep(); /* Enable GPIO clock after waking from sleep mode */ sysclk_enable_pba_module(SYSCLK_GPIO); /* Switch on the status LED */ gpio_clr_gpio_pin(STATUS_LED); #if DEBUG_MESSAGES print_dbg("\r Output higher than threshold \n"); print_dbg("\n"); #else /* LED on for few ms */ delay_ms(LED_DELAY); #endif /* Clear the wake up & enable it to enter sleep mode again */ pm_asyn_wake_up_disable(AVR32_PM_AWEN_ACIFBWEN_MASK); /* Clear All AST Interrupt request and clear SR */ ast_clear_all_status_flags(&AVR32_AST); } return 0; } /* End of main() */
/*! \brief Initializes SD/MMC resources: GPIO, SPI and SD/MMC. */ static void sd_mmc_resources_init(void) { // GPIO pins used for SD/MMC interface static const gpio_map_t SD_MMC_SPI_GPIO_MAP = { {SD_MMC_SPI_SCK_PIN, SD_MMC_SPI_SCK_FUNCTION }, // SPI Clock. {SD_MMC_SPI_MISO_PIN, SD_MMC_SPI_MISO_FUNCTION}, // MISO. {SD_MMC_SPI_MOSI_PIN, SD_MMC_SPI_MOSI_FUNCTION}, // MOSI. {SD_MMC_SPI_NPCS_PIN, SD_MMC_SPI_NPCS_FUNCTION} // Chip Select NPCS. }; // SPI options. spi_options_t spiOptions = { .reg = SD_MMC_SPI_NPCS, .baudrate = SD_MMC_SPI_MASTER_SPEED, // Defined in conf_sd_mmc_spi.h. .bits = SD_MMC_SPI_BITS, // Defined in conf_sd_mmc_spi.h. .spck_delay = 0, .trans_delay = 0, .stay_act = 1, .spi_mode = 0, .modfdis = 1 }; // Assign I/Os to SPI. gpio_enable_module(SD_MMC_SPI_GPIO_MAP, sizeof(SD_MMC_SPI_GPIO_MAP) / sizeof(SD_MMC_SPI_GPIO_MAP[0])); // Initialize as master. spi_initMaster(SD_MMC_SPI, &spiOptions); // Set SPI selection mode: variable_ps, pcs_decode, delay. spi_selectionMode(SD_MMC_SPI, 0, 0, 0); // Enable SPI module. spi_enable(SD_MMC_SPI); // Initialize SD/MMC driver with SPI clock (PBA). sd_mmc_spi_init(spiOptions, PBA_HZ); } /*! \brief Initialize PDCA (Peripheral DMA Controller A) resources for the SPI transfer and start a dummy transfer */ void local_pdca_init(void) { // this PDCA channel is used for data reception from the SPI pdca_channel_options_t pdca_options_SPI_RX ={ // pdca channel options .addr = ram_buffer, // memory address. We take here the address of the string dummy_data. This string is located in the file dummy.h .size = 512, // transfer counter: here the size of the string .r_addr = NULL, // next memory address after 1st transfer complete .r_size = 0, // next transfer counter not used here .pid = AVR32_PDCA_CHANNEL_USED_RX, // select peripheral ID - data are on reception from SPI1 RX line .transfer_size = PDCA_TRANSFER_SIZE_BYTE // select size of the transfer: 8,16,32 bits }; // this channel is used to activate the clock of the SPI by sending a dummy variables pdca_channel_options_t pdca_options_SPI_TX ={ // pdca channel options .addr = (void *)&dummy_data, // memory address. // We take here the address of the string dummy_data. // This string is located in the file dummy.h .size = 512, // transfer counter: here the size of the string .r_addr = NULL, // next memory address after 1st transfer complete .r_size = 0, // next transfer counter not used here .pid = AVR32_PDCA_CHANNEL_USED_TX, // select peripheral ID - data are on reception from SPI1 RX line .transfer_size = PDCA_TRANSFER_SIZE_BYTE // select size of the transfer: 8,16,32 bits }; // Init PDCA transmission channel pdca_init_channel(AVR32_PDCA_CHANNEL_SPI_TX, &pdca_options_SPI_TX); // Init PDCA Reception channel pdca_init_channel(AVR32_PDCA_CHANNEL_SPI_RX, &pdca_options_SPI_RX); //! \brief Enable pdca transfer interrupt when completed INTC_register_interrupt(&pdca_int_handler, AVR32_PDCA_IRQ_0, AVR32_INTC_INT1); // pdca_channel_spi1_RX = 0 } /*! \brief Main function. Execution starts here. */ int main(void) { int i, j; // Switch the main clock to the external oscillator 0 pcl_switch_to_osc(PCL_OSC0, FOSC0, OSC0_STARTUP); // Initialize debug RS232 with PBA clock init_dbg_rs232(PBA_HZ); //start test print_dbg("\r\nInit SD/MMC Driver"); print_dbg("\r\nInsert SD/MMC..."); // Initialize Interrupt Controller INTC_init_interrupts(); // Initialize SD/MMC driver resources: GPIO, SPI and SD/MMC. sd_mmc_resources_init(); // Wait for a card to be inserted while (!sd_mmc_spi_mem_check()); print_dbg("\r\nCard detected!"); // Read Card capacity sd_mmc_spi_get_capacity(); print_dbg("Capacity = "); print_dbg_ulong(capacity >> 20); print_dbg(" MBytes"); // Enable all interrupts. Enable_global_interrupt(); // Initialize PDCA controller before starting a transfer local_pdca_init(); // Read the first sectors number 1, 2, 3 of the card for(j = 1; j <= 3; j++) { // Configure the PDCA channel: the address of memory ram_buffer to receive the data at sector address j pdca_load_channel( AVR32_PDCA_CHANNEL_SPI_RX, &ram_buffer, 512); pdca_load_channel( AVR32_PDCA_CHANNEL_SPI_TX, (void *)&dummy_data, 512); //send dummy to activate the clock end_of_transfer = false; // open sector number j if(sd_mmc_spi_read_open_PDCA (j)) { print_dbg("\r\nFirst 512 Bytes of Transfer number "); print_dbg_ulong(j); print_dbg(" :\r\n"); spi_write(SD_MMC_SPI,0xFF); // Write a first dummy data to synchronize transfer pdca_enable_interrupt_transfer_complete(AVR32_PDCA_CHANNEL_SPI_RX); pdca_channelrx =(volatile avr32_pdca_channel_t*) pdca_get_handler(AVR32_PDCA_CHANNEL_SPI_RX); // get the correct PDCA channel pointer pdca_channeltx =(volatile avr32_pdca_channel_t*) pdca_get_handler(AVR32_PDCA_CHANNEL_SPI_TX); // get the correct PDCA channel pointer pdca_channelrx->cr = AVR32_PDCA_TEN_MASK; // Enable RX PDCA transfer first pdca_channeltx->cr = AVR32_PDCA_TEN_MASK; // and TX PDCA transfer while(!end_of_transfer); // Display the first 2O bytes of the ram_buffer content for( i = 0; i < 20; i++) { print_dbg_char_hex( (U8)(*(ram_buffer + i))); } } else { print_dbg("\r\n! Unable to open memory \r\n"); } } print_dbg("\r\nEnd of the example.\r\n"); while (1); }
/*! \brief Initializes QT60168 resources: GPIO and SPI */ static void qt60168_resources_init(void) { static const gpio_map_t QT60168_SPI_GPIO_MAP = { {QT60168_SPI_SCK_PIN, QT60168_SPI_SCK_FUNCTION }, // SPI Clock. {QT60168_SPI_MISO_PIN, QT60168_SPI_MISO_FUNCTION }, // MISO. {QT60168_SPI_MOSI_PIN, QT60168_SPI_MOSI_FUNCTION }, // MOSI. {QT60168_SPI_NPCS0_PIN, QT60168_SPI_NPCS0_FUNCTION} // Chip Select NPCS. }; // SPI options. spi_options_t spiOptions = { .reg = QT60168_SPI_NCPS, .baudrate = QT60168_SPI_MASTER_SPEED, // Defined in conf_qt60168.h. .bits = QT60168_SPI_BITS, // Defined in conf_qt60168.h. .spck_delay = 0, .trans_delay = 0, .stay_act = 0, .spi_mode = 3, .modfdis = 1 }; // Assign I/Os to SPI. gpio_enable_module(QT60168_SPI_GPIO_MAP, sizeof(QT60168_SPI_GPIO_MAP) / sizeof(QT60168_SPI_GPIO_MAP[0])); // Initialize as master. spi_initMaster(QT60168_SPI, &spiOptions); // Set selection mode: variable_ps, pcs_decode, delay. spi_selectionMode(QT60168_SPI, 0, 0, 0); // Enable SPI. spi_enable(QT60168_SPI); // Initialize QT60168 with SPI clock Osc0. spi_setupChipReg(QT60168_SPI, &spiOptions, FOSC0); } typedef enum { DEMO_COLOR_ALL=0 , DEMO_COLOR_BLUE , DEMO_COLOR_RED , DEMO_COLOR_GREEN , DEMO_COLOR_MAX } demo_color_t; typedef enum { DEMO_DISPLAY_BOXES=0 , DEMO_DISPLAY_WHEEL , DEMO_DISPLAY_MAX } demo_display_t; /*! \brief Main function */ int main(void) { int i; bool idle=false; // Detect key transition (PRESSED -> RELEASED) U32 x_start; U32 y_start; U32 x_size; U32 y_size; U16 color; const U16 icon[QT60168_TOUCH_NUMBER_OF_SENSORS] = {0, 1*16, 2*16, 3*16, 4*16, 5*16, -1, -1, 6*16, 7*16, 8*16, 9*16, 10*16, 11*16, -1, -1}; demo_color_t demo_color=DEMO_COLOR_ALL; demo_display_t demo_display=DEMO_DISPLAY_WHEEL; bool touch_states[QT60168_TOUCH_NUMBER_OF_SENSORS]; // Switch the main clock to the external oscillator 0 pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP); // Initialize RS232 debug text output. init_dbg_rs232(FOSC0); // Initialize QT60168 resources: GPIO, SPI and QT60168. qt60168_resources_init(); // Initialize QT60168 component. qt60168_init(FOSC0); // Initialize the LCD. et024006_Init( FOSC0/*CPU*/, FOSC0/*HSB*/); // Clear the display i.e. make it black et024006_DrawFilledRect(0, 0, ET024006_WIDTH, ET024006_HEIGHT, BLACK ); // Set the backlight. gpio_set_gpio_pin(ET024006DHU_BL_PIN); // Display welcome string. et024006_PrintString("QT60168 EXAMPLE", (const unsigned char *)&FONT8x8, 110, 5, WHITE, -1); et024006_PrintString("Press the QTouch sensors.", (const unsigned char *)&FONT6x8, 95, 20, WHITE, -1); et024006_PrintString("Color: All", (const unsigned char *)&FONT6x8, 10, 200, WHITE, -1); et024006_PrintString("Display sensors", (const unsigned char *)&FONT6x8, 120, 200, WHITE, -1); et024006_DrawLine(DEMO_START_X, DEMO_START_Y-1, DEMO_START_X+DEMO_SIZE_X, DEMO_START_Y-1, WHITE ); et024006_DrawLine(DEMO_START_X, DEMO_START_Y+DEMO_SIZE_Y+1, DEMO_START_X+DEMO_SIZE_X, DEMO_START_Y+DEMO_SIZE_Y+1, WHITE ); // Memorize the status for each key. for( i=0 ; i<QT60168_TOUCH_NUMBER_OF_SENSORS ; i++ ) touch_states[i] = qt60168_is_key_pressed(i); // Set LED state in a known state. gpio_set_gpio_pin(LED0_GPIO); gpio_set_gpio_pin(LED1_GPIO); gpio_set_gpio_pin(LED2_GPIO); gpio_set_gpio_pin(LED3_GPIO); while(1) { for( i=0 ; i<QT60168_TOUCH_NUMBER_OF_SENSORS ; i++) { // Test Press event on sensors // if( !touch_states[i] && qt60168_is_key_pressed(i) ) { touch_states[i] = true; if( i==QT60168_TOUCH_SENSOR_BUTTON_0 ) { gpio_tgl_gpio_pin(LED0_GPIO); et024006_PrintString("B0", (const unsigned char *)&FONT6x8, 10, 215, WHITE, -1); demo_color=(demo_color+1) % DEMO_COLOR_MAX; // Erase previous line et024006_DrawFilledRect(10, 200, 80, 10, BLACK ); switch( demo_color ) { case DEMO_COLOR_BLUE: et024006_PrintString("Color: Blue", (const unsigned char *)&FONT6x8, 10, 200, WHITE, -1); break; case DEMO_COLOR_RED: et024006_PrintString("Color: Red", (const unsigned char *)&FONT6x8, 10, 200, WHITE, -1); break; case DEMO_COLOR_GREEN: et024006_PrintString("Color: Green", (const unsigned char *)&FONT6x8, 10, 200, WHITE, -1); break; default: et024006_PrintString("Color: All", (const unsigned char *)&FONT6x8, 10, 200, WHITE, -1); break; } } else if( i==QT60168_TOUCH_SENSOR_BUTTON_1 ) { gpio_tgl_gpio_pin(LED1_GPIO); et024006_PrintString("B1", (const unsigned char *)&FONT6x8, 30, 215, WHITE, -1); demo_display=(demo_display+1) % DEMO_DISPLAY_MAX; // Erase previous line et024006_DrawFilledRect(120, 200, 160, 10, BLACK ); switch( demo_display ) { case DEMO_DISPLAY_WHEEL: et024006_PrintString("Display sensors", (const unsigned char *)&FONT6x8, 120, 200, WHITE, -1); break; case DEMO_DISPLAY_BOXES: default: et024006_PrintString("Display random boxes", (const unsigned char *)&FONT6x8, 120, 200, WHITE, -1); break; } // Erase display et024006_DrawFilledRect(DEMO_START_X, DEMO_START_Y, DEMO_SIZE_X, DEMO_SIZE_Y, BLACK ); } else if( i==QT60168_TOUCH_SENSOR_BUTTON_2 ) { gpio_tgl_gpio_pin(LED2_GPIO); et024006_PrintString("B2", (const unsigned char *)&FONT6x8, 50, 215, WHITE, -1); } else if( i==QT60168_TOUCH_SENSOR_BUTTON_3 ) { gpio_tgl_gpio_pin(LED3_GPIO); et024006_PrintString("B3", (const unsigned char *)&FONT6x8, 70, 215, WHITE, -1); } else { // Press transition detected for the wheel idle = false; // Draw Wheel[i] et024006_DrawFilledRect(100 + icon[i], 215-2, 10, 10, WHITE ); } } // Test Release event on sensors // if(touch_states[i] && !qt60168_is_key_pressed(i)) { touch_states[i] = false; if( i==QT60168_TOUCH_SENSOR_BUTTON_0 ) { // Erase "B0" et024006_DrawFilledRect(10, 215-2, 12, 12, BLACK ); } else if( i==QT60168_TOUCH_SENSOR_BUTTON_1 ) { // Erase "B1" et024006_DrawFilledRect(30, 215-2, 12, 12, BLACK ); } else if( i==QT60168_TOUCH_SENSOR_BUTTON_2 ) { // Erase "B2" et024006_DrawFilledRect(50, 215-2, 12, 12, BLACK ); } else if( i==QT60168_TOUCH_SENSOR_BUTTON_3 ) { // Erase "B3" et024006_DrawFilledRect(70, 215-2, 12, 12, BLACK ); } else { // Erase Wheel[i] et024006_DrawFilledRect(100 + icon[i], 215-2, 10, 10, BLACK ); } } } // for... if( demo_display==DEMO_DISPLAY_WHEEL ) { if( touch_states[QT60168_TOUCH_SENSOR_BUTTON_0] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(30, 50, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_BUTTON_1] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(30, 80, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_BUTTON_2] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(30, 110, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_BUTTON_3] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(30, 140, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_0] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X + DEMO_WHEEL_RADIUS*SIN0, DEMO_WHEEL_START_Y - DEMO_WHEEL_RADIUS*COS0, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_1] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X + DEMO_WHEEL_RADIUS*SIN30, DEMO_WHEEL_START_Y - DEMO_WHEEL_RADIUS*COS30, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_2] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X + DEMO_WHEEL_RADIUS*SIN60, DEMO_WHEEL_START_Y - DEMO_WHEEL_RADIUS*COS60, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_3] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X + DEMO_WHEEL_RADIUS*SIN90, DEMO_WHEEL_START_Y - DEMO_WHEEL_RADIUS*COS90, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_4] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X + DEMO_WHEEL_RADIUS*SIN60, DEMO_WHEEL_START_Y + DEMO_WHEEL_RADIUS*COS60, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_5] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X + DEMO_WHEEL_RADIUS*SIN30, DEMO_WHEEL_START_Y + DEMO_WHEEL_RADIUS*COS30, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_6] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X - DEMO_WHEEL_RADIUS*SIN0, DEMO_WHEEL_START_Y + DEMO_WHEEL_RADIUS*COS0, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_7] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X - DEMO_WHEEL_RADIUS*SIN30, DEMO_WHEEL_START_Y + DEMO_WHEEL_RADIUS*COS30, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_8] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X - DEMO_WHEEL_RADIUS*SIN60, DEMO_WHEEL_START_Y + DEMO_WHEEL_RADIUS*COS60, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_9] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X - DEMO_WHEEL_RADIUS*SIN90, DEMO_WHEEL_START_Y + DEMO_WHEEL_RADIUS*COS90, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_10] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X - DEMO_WHEEL_RADIUS*SIN60, DEMO_WHEEL_START_Y - DEMO_WHEEL_RADIUS*COS60, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); if( touch_states[QT60168_TOUCH_SENSOR_WHEEL_11] ) color = WHITE; else color = BLUE; et024006_DrawFilledRect(DEMO_WHEEL_START_X - DEMO_WHEEL_RADIUS*SIN30, DEMO_WHEEL_START_Y - DEMO_WHEEL_RADIUS*COS30, DEMO_WHEEL_SIZE_X, DEMO_WHEEL_SIZE_Y, color ); } else if( !idle && ( demo_display==DEMO_DISPLAY_BOXES ) ) { // Display a box randomly on the screen. idle = true; x_start = DEMO_START_X + rand()%DEMO_SIZE_X; y_start = DEMO_START_Y + rand()%DEMO_SIZE_Y; x_size = rand()%(DEMO_START_X+DEMO_SIZE_X-x_start); y_size = rand()%(DEMO_START_Y+DEMO_SIZE_Y-y_start); color = rand()%0x10000; switch( demo_color ) { case DEMO_COLOR_BLUE: color = color & BLUE; break; case DEMO_COLOR_RED: color = color & RED; break; case DEMO_COLOR_GREEN: color = color & GREEN; break; default: break; } et024006_DrawFilledRect( x_start , y_start , x_size , y_size , color ); } } // while(1)... }
/** * \brief TC Initialization * * Initializes and start the TC module with the following: * - Counter in Up mode with automatic reset on RC compare match. * - fPBA/8 is used as clock source for TC * - Enables RC compare match interrupt * \param tc Base address of the TC module */ static void tc_init(volatile avr32_tc_t *tc) { // Options for waveform generation. static const tc_waveform_opt_t waveform_opt = { // Channel selection. .channel = EXAMPLE_TC_CHANNEL, // Software trigger effect on TIOB. .bswtrg = TC_EVT_EFFECT_NOOP, // External event effect on TIOB. .beevt = TC_EVT_EFFECT_NOOP, // RC compare effect on TIOB. .bcpc = TC_EVT_EFFECT_NOOP, // RB compare effect on TIOB. .bcpb = TC_EVT_EFFECT_NOOP, // Software trigger effect on TIOA. .aswtrg = TC_EVT_EFFECT_NOOP, // External event effect on TIOA. .aeevt = TC_EVT_EFFECT_NOOP, // RC compare effect on TIOA. .acpc = TC_EVT_EFFECT_NOOP, /* * RA compare effect on TIOA. * (other possibilities are none, set and clear). */ .acpa = TC_EVT_EFFECT_NOOP, /* * Waveform selection: Up mode with automatic trigger(reset) * on RC compare. */ .wavsel = TC_WAVEFORM_SEL_UP_MODE_RC_TRIGGER, // External event trigger enable. .enetrg = false, // External event selection. .eevt = 0, // External event edge selection. .eevtedg = TC_SEL_NO_EDGE, // Counter disable when RC compare. .cpcdis = false, // Counter clock stopped with RC compare. .cpcstop = false, // Burst signal selection. .burst = false, // Clock inversion. .clki = false, // Internal source clock 3, connected to fPBA / 8. .tcclks = TC_CLOCK_SOURCE_TC3 }; // Options for enabling TC interrupts static const tc_interrupt_t tc_interrupt = { .etrgs = 0, .ldrbs = 0, .ldras = 0, .cpcs = 1, // Enable interrupt on RC compare alone .cpbs = 0, .cpas = 0, .lovrs = 0, .covfs = 0 }; // Initialize the timer/counter. tc_init_waveform(tc, &waveform_opt); /* * Set the compare triggers. * We configure it to count every 1 milliseconds. * We want: (1 / (fPBA / 8)) * RC = 1 ms, hence RC = (fPBA / 8) / 1000 * to get an interrupt every 10 ms. */ tc_write_rc(tc, EXAMPLE_TC_CHANNEL, (sysclk_get_pba_hz() / 8 / 1000)); // configure the timer interrupt tc_configure_interrupts(tc, EXAMPLE_TC_CHANNEL, &tc_interrupt); // Start the timer/counter. tc_start(tc, EXAMPLE_TC_CHANNEL); } /*! \brief Main function: * - Configure the CPU to run at 12MHz * - Configure the USART * - Register the TC interrupt (GCC only) * - Configure, enable the CPCS (RC compare match) interrupt, and start a * TC channel in waveform mode * - In an infinite loop, update the USART message every second. */ int main(void) { volatile avr32_tc_t *tc = EXAMPLE_TC; uint32_t timer = 0; /** * \note the call to sysclk_init() will disable all non-vital * peripheral clocks, except for the peripheral clocks explicitly * enabled in conf_clock.h. */ sysclk_init(); // Enable the clock to the selected example Timer/counter peripheral module. sysclk_enable_peripheral_clock(EXAMPLE_TC); // Initialize the USART module for trace messages init_dbg_rs232(sysclk_get_pba_hz()); // Disable the interrupts cpu_irq_disable(); #if defined (__GNUC__) // Initialize interrupt vectors. INTC_init_interrupts(); // Register the RTC interrupt handler to the interrupt controller. INTC_register_interrupt(&tc_irq, EXAMPLE_TC_IRQ, EXAMPLE_TC_IRQ_PRIORITY); #endif // Enable the interrupts cpu_irq_enable(); // Initialize the timer module tc_init(tc); while (1) { // Update the display on USART every second. if ((update_timer) && (!(tc_tick%1000))) { timer++; // Set cursor to the position (1; 5) print_dbg("\x1B[5;1H"); // Print the timer value print_dbg("ATMEL AVR UC3 - Timer/Counter Example 3\n\rTimer: "); print_dbg_ulong(timer); print_dbg(" s"); // Reset the timer update flag to wait till next timer interrupt update_timer = false; } } }
/** \brief Main function - init and loop to display ADC values */ int main(void) { /* GPIO pin/ADC-function map. */ const gpio_map_t ADCIFB_GPIO_MAP = { {EXAMPLE_ADCIFB_PIN, EXAMPLE_ADCIFB_FUNCTION} }; /* ADCIFB Configuration */ adcifb_opt_t adcifb_opt = { /* Resolution mode */ .resolution = AVR32_ADCIFB_ACR_RES_12BIT, /* Channels Sample & Hold Time in [0,15] */ .shtim = 15, .ratio_clkadcifb_clkadc = (sysclk_get_pba_hz() / EXAMPLE_TARGET_CLK_ADC_FREQ_HZ), /* * Startup time in [0,127], where * Tstartup = startup * 8 * Tclk_adc (assuming Tstartup ~ 15us max) */ .startup = 3, /* ADCIFB Sleep Mode disabled */ .sleep_mode_enable = false }; uint32_t adc_data; /* Initialize the system clocks */ sysclk_init(); /* Init debug serial line */ init_dbg_rs232(sysclk_get_pba_hz()); /* Assign and enable GPIO pins to the ADC function. */ gpio_enable_module(ADCIFB_GPIO_MAP, sizeof(ADCIFB_GPIO_MAP) / sizeof(ADCIFB_GPIO_MAP[0])); /* Enable and configure the ADCIFB module */ if (adcifb_configure(&AVR32_ADCIFB, &adcifb_opt) != PASS) { /* Config error. */ while (true) { gpio_tgl_gpio_pin(LED0_GPIO); delay_ms(100); } } /* Configure the trigger mode */ /* "No trigger, only software trigger can start conversions". */ if (adcifb_configure_trigger(&AVR32_ADCIFB, AVR32_ADCIFB_TRGMOD_NT, 0) != PASS) { /* Config error. */ while (true) { gpio_tgl_gpio_pin(LED1_GPIO); delay_ms(100); } } /* Enable the ADCIFB channel the battery is connected to. */ adcifb_channels_enable(&AVR32_ADCIFB, EXAMPLE_ADCIFB_CHANNEL_MASK); while (true) { while (adcifb_is_ready(&AVR32_ADCIFB) != true) { /* Wait until the ADC is ready to perform a conversion. */ } /* Start an ADCIFB conversion sequence. */ adcifb_start_conversion_sequence(&AVR32_ADCIFB); while (adcifb_is_drdy(&AVR32_ADCIFB) != true) { /* Wait until the converted data is available. */ } /* Get the last converted data. */ adc_data = adcifb_get_last_data(&AVR32_ADCIFB); /* Display the current voltage of the battery. */ print_dbg("\x1B[2J\x1B[H\r\nADCIFB Example\r\nHEX Value for " EXAMPLE_ADCIFB_CHANNEL_NAME " : 0x"); print_dbg_hex(adc_data & AVR32_ADCIFB_LCDR_LDATA_MASK); print_dbg("\r\n"); delay_ms(500); /* * Note1: there is a resistor bridge between the battery and the * ADC pad on the AT32UC3L-EK. The data converted is thus half * of * the battery voltage. */ /* * Note2: if the battery is not in place, the conversion is out * of * spec because the ADC input is then higher than ADVREF. */ } }