1*1b2596b5SMatthias Ringwald /** 2*1b2596b5SMatthias Ringwald * \file 3*1b2596b5SMatthias Ringwald * 4*1b2596b5SMatthias Ringwald * \brief Getting Started Application. 5*1b2596b5SMatthias Ringwald * 6*1b2596b5SMatthias Ringwald * Copyright (c) 2011-2016 Atmel Corporation. All rights reserved. 7*1b2596b5SMatthias Ringwald * 8*1b2596b5SMatthias Ringwald * \asf_license_start 9*1b2596b5SMatthias Ringwald * 10*1b2596b5SMatthias Ringwald * \page License 11*1b2596b5SMatthias Ringwald * 12*1b2596b5SMatthias Ringwald * Redistribution and use in source and binary forms, with or without 13*1b2596b5SMatthias Ringwald * modification, are permitted provided that the following conditions are met: 14*1b2596b5SMatthias Ringwald * 15*1b2596b5SMatthias Ringwald * 1. Redistributions of source code must retain the above copyright notice, 16*1b2596b5SMatthias Ringwald * this list of conditions and the following disclaimer. 17*1b2596b5SMatthias Ringwald * 18*1b2596b5SMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright notice, 19*1b2596b5SMatthias Ringwald * this list of conditions and the following disclaimer in the documentation 20*1b2596b5SMatthias Ringwald * and/or other materials provided with the distribution. 21*1b2596b5SMatthias Ringwald * 22*1b2596b5SMatthias Ringwald * 3. The name of Atmel may not be used to endorse or promote products derived 23*1b2596b5SMatthias Ringwald * from this software without specific prior written permission. 24*1b2596b5SMatthias Ringwald * 25*1b2596b5SMatthias Ringwald * 4. This software may only be redistributed and used in connection with an 26*1b2596b5SMatthias Ringwald * Atmel microcontroller product. 27*1b2596b5SMatthias Ringwald * 28*1b2596b5SMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED 29*1b2596b5SMatthias Ringwald * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 30*1b2596b5SMatthias Ringwald * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE 31*1b2596b5SMatthias Ringwald * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR 32*1b2596b5SMatthias Ringwald * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 33*1b2596b5SMatthias Ringwald * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 34*1b2596b5SMatthias Ringwald * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 35*1b2596b5SMatthias Ringwald * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 36*1b2596b5SMatthias Ringwald * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 37*1b2596b5SMatthias Ringwald * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 38*1b2596b5SMatthias Ringwald * POSSIBILITY OF SUCH DAMAGE. 39*1b2596b5SMatthias Ringwald * 40*1b2596b5SMatthias Ringwald * \asf_license_stop 41*1b2596b5SMatthias Ringwald * 42*1b2596b5SMatthias Ringwald */ 43*1b2596b5SMatthias Ringwald 44*1b2596b5SMatthias Ringwald /** 45*1b2596b5SMatthias Ringwald * \mainpage Getting Started Application 46*1b2596b5SMatthias Ringwald * 47*1b2596b5SMatthias Ringwald * \section Purpose 48*1b2596b5SMatthias Ringwald * 49*1b2596b5SMatthias Ringwald * The Getting Started example will help new users get familiar with Atmel's 50*1b2596b5SMatthias Ringwald * SAM family of microcontrollers. This basic application shows the startup 51*1b2596b5SMatthias Ringwald * sequence of a chip and how to use its core peripherals. 52*1b2596b5SMatthias Ringwald * 53*1b2596b5SMatthias Ringwald * \section Requirements 54*1b2596b5SMatthias Ringwald * 55*1b2596b5SMatthias Ringwald * This package can be used with SAM evaluation kits. 56*1b2596b5SMatthias Ringwald * 57*1b2596b5SMatthias Ringwald * \section Description 58*1b2596b5SMatthias Ringwald * 59*1b2596b5SMatthias Ringwald * The demonstration program makes the LED(s) on the board blink at a fixed rate. 60*1b2596b5SMatthias Ringwald * This rate is generated by using Time tick timer. The blinking can be stopped 61*1b2596b5SMatthias Ringwald * using the push button. 62*1b2596b5SMatthias Ringwald * 63*1b2596b5SMatthias Ringwald * \section Usage 64*1b2596b5SMatthias Ringwald * 65*1b2596b5SMatthias Ringwald * -# Build the program and download it inside the evaluation board. 66*1b2596b5SMatthias Ringwald * -# On the computer, open and configure a terminal application 67*1b2596b5SMatthias Ringwald * (e.g. HyperTerminal on Microsoft Windows) with these settings: 68*1b2596b5SMatthias Ringwald * - 115200 bauds 69*1b2596b5SMatthias Ringwald * - 8 bits of data 70*1b2596b5SMatthias Ringwald * - No parity 71*1b2596b5SMatthias Ringwald * - 1 stop bit 72*1b2596b5SMatthias Ringwald * - No flow control 73*1b2596b5SMatthias Ringwald * -# Start the application. 74*1b2596b5SMatthias Ringwald * -# The LED(s) should start blinking on the board. In the terminal window, the 75*1b2596b5SMatthias Ringwald * following text should appear (values depend on the board and chip used): 76*1b2596b5SMatthias Ringwald * \code 77*1b2596b5SMatthias Ringwald -- Getting Started Example xxx -- 78*1b2596b5SMatthias Ringwald -- xxxxxx-xx 79*1b2596b5SMatthias Ringwald -- Compiled: xxx xx xxxx xx:xx:xx -- 80*1b2596b5SMatthias Ringwald \endcode 81*1b2596b5SMatthias Ringwald * -# Pressing and release button 1 should make one LED stop & restart 82*1b2596b5SMatthias Ringwald * blinking. 83*1b2596b5SMatthias Ringwald * -# If the button 2 available, pressing button 2 should make the other LED 84*1b2596b5SMatthias Ringwald * stop & restart blinking. 85*1b2596b5SMatthias Ringwald * 86*1b2596b5SMatthias Ringwald */ 87*1b2596b5SMatthias Ringwald /* 88*1b2596b5SMatthias Ringwald * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a> 89*1b2596b5SMatthias Ringwald */ 90*1b2596b5SMatthias Ringwald 91*1b2596b5SMatthias Ringwald #include "asf.h" 92*1b2596b5SMatthias Ringwald #include "stdio_serial.h" 93*1b2596b5SMatthias Ringwald #include "conf_board.h" 94*1b2596b5SMatthias Ringwald #include "conf_clock.h" 95*1b2596b5SMatthias Ringwald 96*1b2596b5SMatthias Ringwald /** IRQ priority for PIO (The lower the value, the greater the priority) */ 97*1b2596b5SMatthias Ringwald // [main_def_pio_irq_prior] 98*1b2596b5SMatthias Ringwald #define IRQ_PRIOR_PIO 0 99*1b2596b5SMatthias Ringwald // [main_def_pio_irq_prior] 100*1b2596b5SMatthias Ringwald 101*1b2596b5SMatthias Ringwald /** LED0 blink time, LED1 blink half this time, in ms */ 102*1b2596b5SMatthias Ringwald #define BLINK_PERIOD 1000 103*1b2596b5SMatthias Ringwald 104*1b2596b5SMatthias Ringwald #define STRING_EOL "\r" 105*1b2596b5SMatthias Ringwald #define STRING_HEADER "-- Getting Started Example --\r\n" \ 106*1b2596b5SMatthias Ringwald "-- "BOARD_NAME" --\r\n" \ 107*1b2596b5SMatthias Ringwald "-- Compiled: "__DATE__" "__TIME__" --"STRING_EOL 108*1b2596b5SMatthias Ringwald 109*1b2596b5SMatthias Ringwald /** LED0 blinking control. */ 110*1b2596b5SMatthias Ringwald // [main_var_led0_control] 111*1b2596b5SMatthias Ringwald volatile bool g_b_led0_active = true; 112*1b2596b5SMatthias Ringwald // [main_var_led0_control] 113*1b2596b5SMatthias Ringwald 114*1b2596b5SMatthias Ringwald #ifdef LED1_GPIO 115*1b2596b5SMatthias Ringwald /** LED1 blinking control. */ 116*1b2596b5SMatthias Ringwald // [main_var_led1_control] 117*1b2596b5SMatthias Ringwald volatile bool g_b_led1_active = true; 118*1b2596b5SMatthias Ringwald // [main_var_led1_control] 119*1b2596b5SMatthias Ringwald #endif 120*1b2596b5SMatthias Ringwald 121*1b2596b5SMatthias Ringwald /** Global g_ul_ms_ticks in milliseconds since start of application */ 122*1b2596b5SMatthias Ringwald // [main_var_ticks] 123*1b2596b5SMatthias Ringwald volatile uint32_t g_ul_ms_ticks = 0; 124*1b2596b5SMatthias Ringwald // [main_var_ticks] 125*1b2596b5SMatthias Ringwald 126*1b2596b5SMatthias Ringwald /// @cond 0 127*1b2596b5SMatthias Ringwald /**INDENT-OFF**/ 128*1b2596b5SMatthias Ringwald #ifdef __cplusplus 129*1b2596b5SMatthias Ringwald extern "C" { 130*1b2596b5SMatthias Ringwald #endif 131*1b2596b5SMatthias Ringwald /**INDENT-ON**/ 132*1b2596b5SMatthias Ringwald /// @endcond 133*1b2596b5SMatthias Ringwald 134*1b2596b5SMatthias Ringwald /** 135*1b2596b5SMatthias Ringwald * \brief Process Buttons Events 136*1b2596b5SMatthias Ringwald * 137*1b2596b5SMatthias Ringwald * Change active states of LEDs when corresponding button events happened. 138*1b2596b5SMatthias Ringwald */ 139*1b2596b5SMatthias Ringwald static void ProcessButtonEvt(uint8_t uc_button) 140*1b2596b5SMatthias Ringwald { 141*1b2596b5SMatthias Ringwald // [main_button1_evnt_process] 142*1b2596b5SMatthias Ringwald if (uc_button == 0) { 143*1b2596b5SMatthias Ringwald g_b_led0_active = !g_b_led0_active; 144*1b2596b5SMatthias Ringwald if (!g_b_led0_active) { 145*1b2596b5SMatthias Ringwald ioport_set_pin_level(LED0_GPIO, IOPORT_PIN_LEVEL_HIGH); 146*1b2596b5SMatthias Ringwald } 147*1b2596b5SMatthias Ringwald } 148*1b2596b5SMatthias Ringwald // [main_button1_evnt_process] 149*1b2596b5SMatthias Ringwald #ifdef LED1_GPIO 150*1b2596b5SMatthias Ringwald else { 151*1b2596b5SMatthias Ringwald // [main_button2_evnt_process] 152*1b2596b5SMatthias Ringwald g_b_led1_active = !g_b_led1_active; 153*1b2596b5SMatthias Ringwald 154*1b2596b5SMatthias Ringwald /* Enable LED#2 and TC if they were enabled */ 155*1b2596b5SMatthias Ringwald if (g_b_led1_active) { 156*1b2596b5SMatthias Ringwald ioport_set_pin_level(LED1_GPIO, IOPORT_PIN_LEVEL_LOW); 157*1b2596b5SMatthias Ringwald tc_start(TC0, 0); 158*1b2596b5SMatthias Ringwald } 159*1b2596b5SMatthias Ringwald /* Disable LED#2 and TC if they were disabled */ 160*1b2596b5SMatthias Ringwald else { 161*1b2596b5SMatthias Ringwald ioport_set_pin_level(LED1_GPIO, IOPORT_PIN_LEVEL_HIGH); 162*1b2596b5SMatthias Ringwald tc_stop(TC0, 0); 163*1b2596b5SMatthias Ringwald } 164*1b2596b5SMatthias Ringwald // [main_button2_evnt_process] 165*1b2596b5SMatthias Ringwald } 166*1b2596b5SMatthias Ringwald #endif 167*1b2596b5SMatthias Ringwald } 168*1b2596b5SMatthias Ringwald 169*1b2596b5SMatthias Ringwald /** 170*1b2596b5SMatthias Ringwald * \brief Handler for System Tick interrupt. 171*1b2596b5SMatthias Ringwald * 172*1b2596b5SMatthias Ringwald * Process System Tick Event 173*1b2596b5SMatthias Ringwald * Increments the g_ul_ms_ticks counter. 174*1b2596b5SMatthias Ringwald */ 175*1b2596b5SMatthias Ringwald // [main_systick_handler] 176*1b2596b5SMatthias Ringwald void SysTick_Handler(void) 177*1b2596b5SMatthias Ringwald { 178*1b2596b5SMatthias Ringwald g_ul_ms_ticks++; 179*1b2596b5SMatthias Ringwald } 180*1b2596b5SMatthias Ringwald // [main_systick_handler] 181*1b2596b5SMatthias Ringwald 182*1b2596b5SMatthias Ringwald /** 183*1b2596b5SMatthias Ringwald * \brief Handler for Button 1 rising edge interrupt. 184*1b2596b5SMatthias Ringwald * 185*1b2596b5SMatthias Ringwald * Handle process led1 status change. 186*1b2596b5SMatthias Ringwald */ 187*1b2596b5SMatthias Ringwald // [main_button1_handler] 188*1b2596b5SMatthias Ringwald static void Button1_Handler(uint32_t id, uint32_t mask) 189*1b2596b5SMatthias Ringwald { 190*1b2596b5SMatthias Ringwald if (PIN_PUSHBUTTON_1_ID == id && PIN_PUSHBUTTON_1_MASK == mask) { 191*1b2596b5SMatthias Ringwald ProcessButtonEvt(0); 192*1b2596b5SMatthias Ringwald } 193*1b2596b5SMatthias Ringwald } 194*1b2596b5SMatthias Ringwald // [main_button1_handler] 195*1b2596b5SMatthias Ringwald 196*1b2596b5SMatthias Ringwald #ifndef BOARD_NO_PUSHBUTTON_2 197*1b2596b5SMatthias Ringwald /** 198*1b2596b5SMatthias Ringwald * \brief Handler for Button 2 falling edge interrupt. 199*1b2596b5SMatthias Ringwald * 200*1b2596b5SMatthias Ringwald * Handle process led2 status change. 201*1b2596b5SMatthias Ringwald */ 202*1b2596b5SMatthias Ringwald // [main_button2_handler] 203*1b2596b5SMatthias Ringwald static void Button2_Handler(uint32_t id, uint32_t mask) 204*1b2596b5SMatthias Ringwald { 205*1b2596b5SMatthias Ringwald if (PIN_PUSHBUTTON_2_ID == id && PIN_PUSHBUTTON_2_MASK == mask) { 206*1b2596b5SMatthias Ringwald ProcessButtonEvt(1); 207*1b2596b5SMatthias Ringwald } 208*1b2596b5SMatthias Ringwald } 209*1b2596b5SMatthias Ringwald // [main_button2_handler] 210*1b2596b5SMatthias Ringwald #endif 211*1b2596b5SMatthias Ringwald 212*1b2596b5SMatthias Ringwald /** 213*1b2596b5SMatthias Ringwald * \brief Configure the Pushbuttons 214*1b2596b5SMatthias Ringwald * 215*1b2596b5SMatthias Ringwald * Configure the PIO as inputs and generate corresponding interrupt when 216*1b2596b5SMatthias Ringwald * pressed or released. 217*1b2596b5SMatthias Ringwald */ 218*1b2596b5SMatthias Ringwald static void configure_buttons(void) 219*1b2596b5SMatthias Ringwald { 220*1b2596b5SMatthias Ringwald // [main_button1_configure] 221*1b2596b5SMatthias Ringwald /* Configure Pushbutton 1 */ 222*1b2596b5SMatthias Ringwald pmc_enable_periph_clk(PIN_PUSHBUTTON_1_ID); 223*1b2596b5SMatthias Ringwald pio_set_debounce_filter(PIN_PUSHBUTTON_1_PIO, PIN_PUSHBUTTON_1_MASK, 10); 224*1b2596b5SMatthias Ringwald /* Interrupt on rising edge */ 225*1b2596b5SMatthias Ringwald pio_handler_set(PIN_PUSHBUTTON_1_PIO, PIN_PUSHBUTTON_1_ID, 226*1b2596b5SMatthias Ringwald PIN_PUSHBUTTON_1_MASK, PIN_PUSHBUTTON_1_ATTR, Button1_Handler); 227*1b2596b5SMatthias Ringwald NVIC_EnableIRQ((IRQn_Type) PIN_PUSHBUTTON_1_ID); 228*1b2596b5SMatthias Ringwald pio_handler_set_priority(PIN_PUSHBUTTON_1_PIO, 229*1b2596b5SMatthias Ringwald (IRQn_Type) PIN_PUSHBUTTON_1_ID, IRQ_PRIOR_PIO); 230*1b2596b5SMatthias Ringwald pio_enable_interrupt(PIN_PUSHBUTTON_1_PIO, PIN_PUSHBUTTON_1_MASK); 231*1b2596b5SMatthias Ringwald // [main_button1_configure] 232*1b2596b5SMatthias Ringwald #ifndef BOARD_NO_PUSHBUTTON_2 233*1b2596b5SMatthias Ringwald // [main_button2_configure] 234*1b2596b5SMatthias Ringwald /* Configure Pushbutton 2 */ 235*1b2596b5SMatthias Ringwald pmc_enable_periph_clk(PIN_PUSHBUTTON_2_ID); 236*1b2596b5SMatthias Ringwald pio_set_debounce_filter(PIN_PUSHBUTTON_2_PIO, PIN_PUSHBUTTON_2_MASK, 10); 237*1b2596b5SMatthias Ringwald /* Interrupt on falling edge */ 238*1b2596b5SMatthias Ringwald pio_handler_set(PIN_PUSHBUTTON_2_PIO, PIN_PUSHBUTTON_2_ID, 239*1b2596b5SMatthias Ringwald PIN_PUSHBUTTON_2_MASK, PIN_PUSHBUTTON_2_ATTR, Button2_Handler); 240*1b2596b5SMatthias Ringwald NVIC_EnableIRQ((IRQn_Type) PIN_PUSHBUTTON_2_ID); 241*1b2596b5SMatthias Ringwald pio_handler_set_priority(PIN_PUSHBUTTON_2_PIO, 242*1b2596b5SMatthias Ringwald (IRQn_Type) PIN_PUSHBUTTON_2_ID, IRQ_PRIOR_PIO); 243*1b2596b5SMatthias Ringwald pio_enable_interrupt(PIN_PUSHBUTTON_2_PIO, PIN_PUSHBUTTON_2_MASK); 244*1b2596b5SMatthias Ringwald // [main_button2_configure] 245*1b2596b5SMatthias Ringwald #endif 246*1b2596b5SMatthias Ringwald } 247*1b2596b5SMatthias Ringwald 248*1b2596b5SMatthias Ringwald /** 249*1b2596b5SMatthias Ringwald * Interrupt handler for TC0 interrupt. Toggles the state of LED\#2. 250*1b2596b5SMatthias Ringwald */ 251*1b2596b5SMatthias Ringwald // [main_tc0_handler] 252*1b2596b5SMatthias Ringwald #ifndef BOARD_NO_LED_1 253*1b2596b5SMatthias Ringwald void TC0_Handler(void) 254*1b2596b5SMatthias Ringwald { 255*1b2596b5SMatthias Ringwald volatile uint32_t ul_dummy; 256*1b2596b5SMatthias Ringwald 257*1b2596b5SMatthias Ringwald /* Clear status bit to acknowledge interrupt */ 258*1b2596b5SMatthias Ringwald ul_dummy = tc_get_status(TC0, 0); 259*1b2596b5SMatthias Ringwald 260*1b2596b5SMatthias Ringwald /* Avoid compiler warning */ 261*1b2596b5SMatthias Ringwald UNUSED(ul_dummy); 262*1b2596b5SMatthias Ringwald 263*1b2596b5SMatthias Ringwald #ifdef LED1_GPIO 264*1b2596b5SMatthias Ringwald /** Toggle LED state. */ 265*1b2596b5SMatthias Ringwald ioport_toggle_pin_level(LED1_GPIO); 266*1b2596b5SMatthias Ringwald #endif 267*1b2596b5SMatthias Ringwald 268*1b2596b5SMatthias Ringwald printf("2 "); 269*1b2596b5SMatthias Ringwald } 270*1b2596b5SMatthias Ringwald // [main_tc0_handler] 271*1b2596b5SMatthias Ringwald 272*1b2596b5SMatthias Ringwald /** 273*1b2596b5SMatthias Ringwald * Configure Timer Counter 0 to generate an interrupt every 250ms. 274*1b2596b5SMatthias Ringwald */ 275*1b2596b5SMatthias Ringwald // [main_tc_configure] 276*1b2596b5SMatthias Ringwald static void configure_tc(void) 277*1b2596b5SMatthias Ringwald { 278*1b2596b5SMatthias Ringwald uint32_t ul_div; 279*1b2596b5SMatthias Ringwald uint32_t ul_tcclks; 280*1b2596b5SMatthias Ringwald uint32_t ul_sysclk = sysclk_get_cpu_hz(); 281*1b2596b5SMatthias Ringwald 282*1b2596b5SMatthias Ringwald /* Configure PMC */ 283*1b2596b5SMatthias Ringwald pmc_enable_periph_clk(ID_TC0); 284*1b2596b5SMatthias Ringwald #if SAMG55 285*1b2596b5SMatthias Ringwald /* Enable PCK output */ 286*1b2596b5SMatthias Ringwald pmc_disable_pck(PMC_PCK_3); 287*1b2596b5SMatthias Ringwald pmc_switch_pck_to_sclk(PMC_PCK_3, PMC_PCK_PRES(0)); 288*1b2596b5SMatthias Ringwald pmc_enable_pck(PMC_PCK_3); 289*1b2596b5SMatthias Ringwald #endif 290*1b2596b5SMatthias Ringwald 291*1b2596b5SMatthias Ringwald /** Configure TC for a 4Hz frequency and trigger on RC compare. */ 292*1b2596b5SMatthias Ringwald tc_find_mck_divisor(4, ul_sysclk, &ul_div, &ul_tcclks, ul_sysclk); 293*1b2596b5SMatthias Ringwald tc_init(TC0, 0, ul_tcclks | TC_CMR_CPCTRG); 294*1b2596b5SMatthias Ringwald tc_write_rc(TC0, 0, (ul_sysclk / ul_div) / 4); 295*1b2596b5SMatthias Ringwald 296*1b2596b5SMatthias Ringwald /* Configure and enable interrupt on RC compare */ 297*1b2596b5SMatthias Ringwald NVIC_EnableIRQ((IRQn_Type) ID_TC0); 298*1b2596b5SMatthias Ringwald tc_enable_interrupt(TC0, 0, TC_IER_CPCS); 299*1b2596b5SMatthias Ringwald 300*1b2596b5SMatthias Ringwald #ifdef LED1_GPIO 301*1b2596b5SMatthias Ringwald /** Start the counter if LED1 is enabled. */ 302*1b2596b5SMatthias Ringwald if (g_b_led1_active) { 303*1b2596b5SMatthias Ringwald tc_start(TC0, 0); 304*1b2596b5SMatthias Ringwald } 305*1b2596b5SMatthias Ringwald #else 306*1b2596b5SMatthias Ringwald tc_start(TC0, 0); 307*1b2596b5SMatthias Ringwald #endif 308*1b2596b5SMatthias Ringwald } 309*1b2596b5SMatthias Ringwald #endif 310*1b2596b5SMatthias Ringwald // [main_tc_configure] 311*1b2596b5SMatthias Ringwald 312*1b2596b5SMatthias Ringwald /** 313*1b2596b5SMatthias Ringwald * Configure UART console. 314*1b2596b5SMatthias Ringwald */ 315*1b2596b5SMatthias Ringwald // [main_console_configure] 316*1b2596b5SMatthias Ringwald static void configure_console(void) 317*1b2596b5SMatthias Ringwald { 318*1b2596b5SMatthias Ringwald const usart_serial_options_t uart_serial_options = { 319*1b2596b5SMatthias Ringwald .baudrate = CONF_UART_BAUDRATE, 320*1b2596b5SMatthias Ringwald #ifdef CONF_UART_CHAR_LENGTH 321*1b2596b5SMatthias Ringwald .charlength = CONF_UART_CHAR_LENGTH, 322*1b2596b5SMatthias Ringwald #endif 323*1b2596b5SMatthias Ringwald .paritytype = CONF_UART_PARITY, 324*1b2596b5SMatthias Ringwald #ifdef CONF_UART_STOP_BITS 325*1b2596b5SMatthias Ringwald .stopbits = CONF_UART_STOP_BITS, 326*1b2596b5SMatthias Ringwald #endif 327*1b2596b5SMatthias Ringwald }; 328*1b2596b5SMatthias Ringwald 329*1b2596b5SMatthias Ringwald /* Configure console UART. */ 330*1b2596b5SMatthias Ringwald sysclk_enable_peripheral_clock(CONSOLE_UART_ID); 331*1b2596b5SMatthias Ringwald stdio_serial_init(CONF_UART, &uart_serial_options); 332*1b2596b5SMatthias Ringwald } 333*1b2596b5SMatthias Ringwald 334*1b2596b5SMatthias Ringwald // [main_console_configure] 335*1b2596b5SMatthias Ringwald 336*1b2596b5SMatthias Ringwald /** 337*1b2596b5SMatthias Ringwald * \brief Wait for the given number of milliseconds (using the g_ul_ms_ticks 338*1b2596b5SMatthias Ringwald * generated by the SAM's microcontrollers's system tick). 339*1b2596b5SMatthias Ringwald * 340*1b2596b5SMatthias Ringwald * \param ul_dly_ticks Delay to wait for, in milliseconds. 341*1b2596b5SMatthias Ringwald */ 342*1b2596b5SMatthias Ringwald // [main_ms_delay] 343*1b2596b5SMatthias Ringwald static void mdelay(uint32_t ul_dly_ticks) 344*1b2596b5SMatthias Ringwald { 345*1b2596b5SMatthias Ringwald uint32_t ul_cur_ticks; 346*1b2596b5SMatthias Ringwald 347*1b2596b5SMatthias Ringwald ul_cur_ticks = g_ul_ms_ticks; 348*1b2596b5SMatthias Ringwald while ((g_ul_ms_ticks - ul_cur_ticks) < ul_dly_ticks); 349*1b2596b5SMatthias Ringwald } 350*1b2596b5SMatthias Ringwald // [main_ms_delay] 351*1b2596b5SMatthias Ringwald 352*1b2596b5SMatthias Ringwald /** 353*1b2596b5SMatthias Ringwald * \brief getting-started Application entry point. 354*1b2596b5SMatthias Ringwald * 355*1b2596b5SMatthias Ringwald * \return Unused (ANSI-C compatibility). 356*1b2596b5SMatthias Ringwald */ 357*1b2596b5SMatthias Ringwald // [main] 358*1b2596b5SMatthias Ringwald int main(void) 359*1b2596b5SMatthias Ringwald { 360*1b2596b5SMatthias Ringwald //! [main_step_sys_init] 361*1b2596b5SMatthias Ringwald /* Initialize the SAM system */ 362*1b2596b5SMatthias Ringwald sysclk_init(); 363*1b2596b5SMatthias Ringwald board_init(); 364*1b2596b5SMatthias Ringwald //! [main_step_sys_init] 365*1b2596b5SMatthias Ringwald 366*1b2596b5SMatthias Ringwald #ifndef BOARD_NO_PUSHBUTTON_2 367*1b2596b5SMatthias Ringwald #if (SAMV71 || SAMV70 || SAMS70 || SAME70) 368*1b2596b5SMatthias Ringwald if (GPIO_PUSH_BUTTON_2 == PIO_PB12_IDX) { 369*1b2596b5SMatthias Ringwald matrix_set_system_io(matrix_get_system_io() | CCFG_SYSIO_SYSIO12); 370*1b2596b5SMatthias Ringwald } 371*1b2596b5SMatthias Ringwald ioport_set_pin_dir(GPIO_PUSH_BUTTON_2, IOPORT_DIR_INPUT); 372*1b2596b5SMatthias Ringwald ioport_set_pin_mode(GPIO_PUSH_BUTTON_2, GPIO_PUSH_BUTTON_2_FLAGS); 373*1b2596b5SMatthias Ringwald ioport_set_pin_sense_mode(GPIO_PUSH_BUTTON_2, GPIO_PUSH_BUTTON_2_SENSE); 374*1b2596b5SMatthias Ringwald #endif 375*1b2596b5SMatthias Ringwald #endif 376*1b2596b5SMatthias Ringwald //! [main_step_console_init] 377*1b2596b5SMatthias Ringwald /* Initialize the console uart */ 378*1b2596b5SMatthias Ringwald configure_console(); 379*1b2596b5SMatthias Ringwald //! [main_step_console_init] 380*1b2596b5SMatthias Ringwald 381*1b2596b5SMatthias Ringwald /* Output example information */ 382*1b2596b5SMatthias Ringwald puts(STRING_HEADER); 383*1b2596b5SMatthias Ringwald 384*1b2596b5SMatthias Ringwald /* Configure systick for 1 ms */ 385*1b2596b5SMatthias Ringwald puts("Configure system tick to get 1ms tick period.\r"); 386*1b2596b5SMatthias Ringwald //! [main_step_systick_init] 387*1b2596b5SMatthias Ringwald if (SysTick_Config(sysclk_get_cpu_hz() / 1000)) { 388*1b2596b5SMatthias Ringwald puts("-F- Systick configuration error\r"); 389*1b2596b5SMatthias Ringwald while (1); 390*1b2596b5SMatthias Ringwald } 391*1b2596b5SMatthias Ringwald //! [main_step_systick_init] 392*1b2596b5SMatthias Ringwald 393*1b2596b5SMatthias Ringwald #ifndef BOARD_NO_LED_1 394*1b2596b5SMatthias Ringwald puts("Configure TC.\r"); 395*1b2596b5SMatthias Ringwald //! [main_step_tc_init] 396*1b2596b5SMatthias Ringwald configure_tc(); 397*1b2596b5SMatthias Ringwald //! [main_step_tc_init] 398*1b2596b5SMatthias Ringwald #endif 399*1b2596b5SMatthias Ringwald 400*1b2596b5SMatthias Ringwald puts("Configure buttons with debouncing.\r"); 401*1b2596b5SMatthias Ringwald //! [main_step_btn_init] 402*1b2596b5SMatthias Ringwald configure_buttons(); 403*1b2596b5SMatthias Ringwald //! [main_step_btn_init] 404*1b2596b5SMatthias Ringwald 405*1b2596b5SMatthias Ringwald printf("Press %s to Start/Stop the %s blinking.\r\n", 406*1b2596b5SMatthias Ringwald PUSHBUTTON_1_NAME, LED_0_NAME); 407*1b2596b5SMatthias Ringwald 408*1b2596b5SMatthias Ringwald #ifndef BOARD_NO_PUSHBUTTON_2 409*1b2596b5SMatthias Ringwald printf("Press %s to Start/Stop the %s blinking.\r\n", 410*1b2596b5SMatthias Ringwald PUSHBUTTON_2_NAME, LED_1_NAME); 411*1b2596b5SMatthias Ringwald #endif 412*1b2596b5SMatthias Ringwald 413*1b2596b5SMatthias Ringwald //! [main_step_loop] 414*1b2596b5SMatthias Ringwald while (1) { 415*1b2596b5SMatthias Ringwald /* Wait for LED to be active */ 416*1b2596b5SMatthias Ringwald while (!g_b_led0_active); 417*1b2596b5SMatthias Ringwald 418*1b2596b5SMatthias Ringwald /* Toggle LED state if active */ 419*1b2596b5SMatthias Ringwald if (g_b_led0_active) { 420*1b2596b5SMatthias Ringwald ioport_toggle_pin_level(LED0_GPIO); 421*1b2596b5SMatthias Ringwald printf("1 "); 422*1b2596b5SMatthias Ringwald } 423*1b2596b5SMatthias Ringwald 424*1b2596b5SMatthias Ringwald /* Wait for 500ms */ 425*1b2596b5SMatthias Ringwald mdelay(300); 426*1b2596b5SMatthias Ringwald } 427*1b2596b5SMatthias Ringwald //! [main_step_loop] 428*1b2596b5SMatthias Ringwald } 429*1b2596b5SMatthias Ringwald // [main] 430*1b2596b5SMatthias Ringwald /// @cond 0 431*1b2596b5SMatthias Ringwald /**INDENT-OFF**/ 432*1b2596b5SMatthias Ringwald #ifdef __cplusplus 433*1b2596b5SMatthias Ringwald } 434*1b2596b5SMatthias Ringwald #endif 435*1b2596b5SMatthias Ringwald /**INDENT-ON**/ 436*1b2596b5SMatthias Ringwald /// @endcond 437