/** * \brief Application main loop. */ int main(void) { board_init(); /** * \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 USART peripheral module. * The clocks to all non-essential peripherals are disabled in the * conf_clock.h file, so we need to re-enable our USART clock here. */ sysclk_enable_peripheral_clock(EXAMPLE_USART); /** * Write the example USART module configuration to the hardware * registers. The example USART is selected in the conf_example_usart.h * file. */ usart_init_rs232(EXAMPLE_USART, &usart_opt, sysclk_get_peripheral_bus_hz(EXAMPLE_USART)); /** * Write a welcome message on the USART and busy-wait for the listener * to press the enter key. */ usart_write_line(EXAMPLE_USART, "Hello, this is the AVR UC3 MCU saying" " hello! (press enter)\r\n"); /** * Busy-wait for a carriage return sent from the listener. Any received * characters are echoed back. */ do { } while (usart_get_echo_line(EXAMPLE_USART) == USART_FAILURE); /** * Send a goodbye message before entering a forever non-return loop. */ usart_write_line(EXAMPLE_USART, "Goodbye.\r\n"); for (;;) { /* Intentionally loop forever. */ } }
/*! \brief This is an example demonstrating the SPI mode of USART IP * functionalities using the USART driver. */ int main(void) { #if BOARD == STK600_RCUC3D // Configure OSC0 in crystal mode, external crystal with a FOSC0 Hz frequency. scif_configure_osc_crystalmode(SCIF_OSC0, FOSC0); // Enable the OSC0 scif_enable_osc(SCIF_OSC0, OSC0_STARTUP, true); // Set the main clock source as being OSC0. pm_set_mclk_source(PM_CLK_SRC_OSC0); #else // Switch main clock to external oscillator 0 (crystal). pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP); #endif static const gpio_map_t USART_GPIO_MAP = { {EXAMPLE_USART_RX_PIN, EXAMPLE_USART_RX_FUNCTION}, {EXAMPLE_USART_TX_PIN, EXAMPLE_USART_TX_FUNCTION} }; static const usart_options_t USART_OPTIONS = { .baudrate = 57600, .charlength = 8, .paritytype = USART_NO_PARITY, .stopbits = USART_1_STOPBIT, .channelmode = USART_NORMAL_CHMODE }; // Assign GPIO to USART. 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, FOSC0); static const gpio_map_t USART_SPI_GPIO_MAP = { {EXAMPLE_USART_SPI_SCK_PIN, EXAMPLE_USART_SPI_SCK_FUNCTION }, {EXAMPLE_USART_SPI_MISO_PIN, EXAMPLE_USART_SPI_MISO_FUNCTION}, {EXAMPLE_USART_SPI_MOSI_PIN, EXAMPLE_USART_SPI_MOSI_FUNCTION}, {EXAMPLE_USART_SPI_NSS_PIN, EXAMPLE_USART_SPI_NSS_FUNCTION } }; static const usart_spi_options_t USART_SPI_OPTIONS = { .baudrate = 60000, .charlength = 8, .spimode = 0, .channelmode = USART_NORMAL_CHMODE }; // Assign GPIO to SPI. gpio_enable_module(USART_SPI_GPIO_MAP, sizeof(USART_SPI_GPIO_MAP) / sizeof(USART_SPI_GPIO_MAP[0])); // Initialize USART in SPI mode. usart_init_spi_master(EXAMPLE_USART_SPI, &USART_SPI_OPTIONS, FOSC0); // Show startup message usart_write_line(EXAMPLE_USART, "Basic write in USART SPI Mode (press enter)\r\n"); // Press enter to continue. while (usart_get_echo_line(EXAMPLE_USART) == USART_FAILURE); // Get and echo characters until end of line. usart_write_line(EXAMPLE_USART, "Writing SPI test pattern.\r\n"); while (true) { usart_spi_selectChip(EXAMPLE_USART_SPI); usart_putchar(EXAMPLE_USART_SPI, 0x55); usart_putchar(EXAMPLE_USART_SPI, 0xAA); usart_spi_unselectChip(EXAMPLE_USART_SPI); } }