00001 /* ---------------------------------------------------------------------------- 00002 * SAM Software Package License 00003 * ---------------------------------------------------------------------------- 00004 * Copyright (c) 2011, Atmel Corporation 00005 * 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions are met: 00010 * 00011 * - Redistributions of source code must retain the above copyright notice, 00012 * this list of conditions and the disclaimer below. 00013 * 00014 * Atmel's name may not be used to endorse or promote products derived from 00015 * this software without specific prior written permission. 00016 * 00017 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR 00018 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00019 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE 00020 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, 00021 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00022 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 00023 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00024 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00025 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 00026 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 * ---------------------------------------------------------------------------- 00028 */ 00029 00030 /** 00031 * \page getting-started Getting Started Example 00032 * 00033 * \section Purpose 00034 * 00035 * The Getting Started example will help new users get familiar with Atmel's 00036 * samv7 family of Microcontrollers. This basic application shows the startup 00037 * sequence of a chip and how to use its core peripherals. 00038 * 00039 * \section Requirements 00040 * 00041 * This package can be used with SAM V71 Xplained Ultra board. 00042 * 00043 * \section Description 00044 * 00045 * The demonstration program makes two LEDs on the board blink at a fixed rate. 00046 * This rate is generated by using Time tick timer. The blinking can be stopped 00047 * by typing '1' or '2' in the console (one for each LED). 00048 * 00049 * \section Usage 00050 * 00051 * -# Build the program and download it inside the SAM V71 Xplained Ultra board. 00052 * Please refer to the Getting Started with SAM V71 Microcontrollers.pdf 00053 * -# On the computer, open and configure a terminal application 00054 * (e.g. HyperTerminal on Microsoft Windows) with these settings: 00055 * - 115200 baud rates 00056 * - 8 bits of data 00057 * - No parity 00058 * - 1 stop bit 00059 * - No flow control 00060 * -# Start the application. 00061 * -# Two LEDs should start blinking on the board. In the terminal window, the 00062 * following text should appear (values depend on the board and chip used): 00063 * \code 00064 * -- Getting Started Example xxx -- 00065 * -- xxxxxx-xx 00066 * -- Compiled: xxx xx xxxx xx:xx:xx -- 00067 * \endcode 00068 * -# Press '1' to start/stop the LED0 blinking and press '2' to start/stop 00069 * LED1 blinking 00070 * 00071 * \section References 00072 * - getting-started/main.c 00073 * - pio.h 00074 * - pio_it.h 00075 * - led.h 00076 * - trace.h 00077 */ 00078 00079 /** \file 00080 * 00081 * This file contains all the specific code for the getting-started example. 00082 * 00083 */ 00084 00085 /*---------------------------------------------------------------------------- 00086 * Headers 00087 *----------------------------------------------------------------------------*/ 00088 00089 #include "board.h" 00090 #include <stdbool.h> 00091 #include <stdio.h> 00092 00093 /*---------------------------------------------------------------------------- 00094 * Local definitions 00095 *----------------------------------------------------------------------------*/ 00096 00097 /** IRQ priority for PIO (The lower the value, the greater the priority) */ 00098 #define IRQ_PRIOR_PIO 0 00099 00100 /** LED0 blink time, LED1 blink half this time, in ms */ 00101 #define BLINK_PERIOD 1000 00102 00103 /*---------------------------------------------------------------------------- 00104 * Local variables 00105 *----------------------------------------------------------------------------*/ 00106 00107 /** LED0 blinking control. */ 00108 volatile bool bLed0Active = true ; 00109 00110 /** LED1 blinking control. */ 00111 volatile bool bLed1Active = true ; 00112 00113 /** Global timestamps in milliseconds since start of application */ 00114 volatile uint32_t dwTimeStamp = 0; 00115 00116 /** Global timestamps in milliseconds since start of application */ 00117 volatile uint32_t dwTcCounter = 0; 00118 00119 /*---------------------------------------------------------------------------- 00120 * Local functions 00121 *----------------------------------------------------------------------------*/ 00122 00123 /** 00124 * \brief Process Buttons Events 00125 * 00126 * Change active states of LEDs when corresponding button events happened. 00127 */ 00128 static void ProcessButtonEvt( uint8_t ucButton ) 00129 { 00130 if ( ucButton == 0 ) { 00131 bLed0Active = !bLed0Active ; 00132 if ( !bLed0Active ) { 00133 LED_Clear( 0 ); 00134 } 00135 } 00136 else 00137 { 00138 bLed1Active = !bLed1Active ; 00139 00140 /* Enable LED#2 and TC if they were disabled */ 00141 if ( bLed1Active ) { 00142 LED_Set( 1 ); 00143 } 00144 /* Disable LED#2 and TC if they were enabled */ 00145 else{ 00146 LED_Clear( 1 ); 00147 } 00148 } 00149 } 00150 00151 #ifndef NO_PUSHBUTTON 00152 /** 00153 * \brief Handler for Button 1 rising edge interrupt. 00154 * 00155 * Handle process led1 status change. 00156 */ 00157 static void _Button1_Handler( const Pin* pPin ) 00158 { 00159 if ( pPin == &pinPB1 ) { 00160 ProcessButtonEvt( 0 ) ; 00161 } 00162 } 00163 00164 /** 00165 * \brief Handler for Button 2 falling edge interrupt. 00166 * 00167 * Handle process led2 status change. 00168 */ 00169 static void _Button2_Handler( const Pin* pPin ) 00170 { 00171 if ( pPin == &pinPB2 ) { 00172 ProcessButtonEvt( 1 ) ; 00173 } 00174 } 00175 00176 /** 00177 * \brief Configure the Push-buttons 00178 * 00179 * Configure the PIO as inputs and generate corresponding interrupt when 00180 * pressed or released. 00181 */ 00182 static void _ConfigureButtons( void ) 00183 { 00184 /* Configure PIO as inputs. */ 00185 PIO_Configure( &pinPB1, 1 ) ; 00186 PIO_Configure( &pinPB2, 1 ) ; 00187 00188 /* Adjust PIO denounce filter parameters, uses 10 Hz filter. */ 00189 PIO_SetDebounceFilter( &pinPB1, 10 ) ; 00190 PIO_SetDebounceFilter( &pinPB2, 10 ) ; 00191 00192 /* Initialize PIO interrupt handlers, see PIO definition in board.h. */ 00193 PIO_ConfigureIt( &pinPB1, _Button1_Handler ) ; /* Interrupt on rising edge */ 00194 PIO_ConfigureIt( &pinPB2, _Button2_Handler ) ; /* Interrupt on rising edge */ 00195 00196 /* Enable PIO controller IRQs. */ 00197 NVIC_EnableIRQ( (IRQn_Type)pinPB1.id ) ; 00198 NVIC_EnableIRQ( (IRQn_Type)pinPB2.id ) ; 00199 00200 /* Enable PIO line interrupts. */ 00201 PIO_EnableIt( &pinPB1 ) ; 00202 PIO_EnableIt( &pinPB2 ) ; 00203 } 00204 00205 #else 00206 00207 /** 00208 * \brief Handler for DBGU input. 00209 * 00210 * Handle process LED1 or LED2 status change. 00211 */ 00212 static void _DBGU_Handler( void ) 00213 { 00214 uint8_t key; 00215 if ( !DBG_IsRxReady( ) ) return ; 00216 key = DBG_GetChar( ) ; 00217 switch ( key ) { 00218 case '1': case '2': 00219 ProcessButtonEvt( key - '1' ) ; 00220 break; 00221 } 00222 } 00223 #endif 00224 /** 00225 * \brief Configure LEDs 00226 * 00227 * Configures LEDs \#1 and \#2 (cleared by default). 00228 */ 00229 static void _ConfigureLeds( void ) 00230 { 00231 LED_Configure( 0 ) ; 00232 LED_Configure( 1 ) ; 00233 } 00234 00235 00236 /** 00237 * Interrupt handler for TC0 interrupt. Toggles the state of LED\#2. 00238 */ 00239 void TC0_Handler(void) 00240 { 00241 volatile uint32_t dummy; 00242 /* Clear status bit to acknowledge interrupt */ 00243 dummy = TC0->TC_CHANNEL[ 0 ].TC_SR; 00244 00245 /** Toggle LED state. */ 00246 if(bLed1Active) { 00247 LED_Toggle( 1 ); 00248 printf( "2 " ); 00249 } 00250 #ifdef NO_PUSHBUTTON 00251 _DBGU_Handler( ) ; 00252 #endif 00253 00254 } 00255 00256 /** 00257 * Configure Timer Counter 0 to generate an interrupt every 250ms. 00258 */ 00259 static void _ConfigureTc(void) 00260 { 00261 uint32_t div; 00262 uint32_t tcclks; 00263 00264 /** Enable peripheral clock. */ 00265 PMC_EnablePeripheral(ID_TC0); 00266 /** Configure TC for a 4Hz frequency and trigger on RC compare. */ 00267 TC_FindMckDivisor( 4, BOARD_MCK, &div, &tcclks, BOARD_MCK ); 00268 00269 TC_Configure( TC0, 0, tcclks | TC_CMR_CPCTRG ); 00270 TC0->TC_CHANNEL[ 0 ].TC_RC = ( BOARD_MCK / div ) / 4; 00271 00272 /* Configure and enable interrupt on RC compare */ 00273 NVIC_ClearPendingIRQ(TC0_IRQn); 00274 NVIC_EnableIRQ(TC0_IRQn); 00275 00276 TC0->TC_CHANNEL[ 0 ].TC_IER = TC_IER_CPCS ; 00277 00278 /** Start the counter if LED1 is enabled. */ 00279 if ( bLed1Active ) { 00280 TC_Start( TC0, 0 ); 00281 } 00282 } 00283 00284 00285 /*---------------------------------------------------------------------------- 00286 * Exported functions 00287 *----------------------------------------------------------------------------*/ 00288 /** 00289 * \brief getting-started Application entry point. 00290 * 00291 * \return Unused (ANSI-C compatibility). 00292 */ 00293 extern int main( void ) 00294 { 00295 00296 /* Disable watchdog */ 00297 WDT_Disable( WDT ) ; 00298 00299 /* Output example information */ 00300 printf( "\n\r-- Getting Started Example %s --\n\r", SOFTPACK_VERSION ) ; 00301 printf( "-- %s\n\r", BOARD_NAME ) ; 00302 printf( "-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ , COMPILER_NAME); 00303 00304 /* Enable I and D cache */ 00305 SCB_EnableICache(); 00306 SCB_EnableDCache(); 00307 00308 /* Configure systick for 1 ms. */ 00309 TimeTick_Configure (); 00310 00311 printf( "Configure LED PIOs.\n\r" ) ; 00312 _ConfigureLeds() ; 00313 00314 printf( "Configure TC.\n\r" ); 00315 _ConfigureTc() ; 00316 00317 #ifndef NO_PUSHBUTTON 00318 printf( "Configure buttons with denouncing.\n\r" ) ; 00319 _ConfigureButtons() ; 00320 printf( "Press USRBP1 to Start/Stop the blue LED D1 blinking.\n\r" ) ; 00321 printf( "Press USRBP2 to Start/Stop the red LED D2 blinking.\n\r" ) ; 00322 00323 #else 00324 printf( "No push buttons, uses DBG key 1 & 2 instead.\n\r" ) ; 00325 printf( "Press 1 to Start/Stop the blue LED D1 blinking.\n\r" ) ; 00326 printf( "Press 2 to Start/Stop the red LED D2 blinking.\n\r" ) ; 00327 00328 #endif 00329 00330 while ( 1 ) { 00331 /* Wait for LED to be active */ 00332 while( !bLed0Active ); 00333 00334 /* Toggle LED state if active */ 00335 if ( bLed0Active ) { 00336 LED_Toggle( 0 ); 00337 printf( "1 " ); 00338 } 00339 00340 /* Wait for 500ms */ 00341 Wait(1000); 00342 } 00343 }