]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M7_SAME70_Xplained_AtmelStudio/src/main.c
ea418159bbd4301869dd1abc2e726bc4f1f9e23a
[freertos] / FreeRTOS / Demo / CORTEX_M7_SAME70_Xplained_AtmelStudio / src / main.c
1 /*\r
2     FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 /******************************************************************************\r
71  * This project provides two demo applications.  A simple blinky style project,\r
72  * and a more comprehensive test and demo application.  The\r
73  * mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting (defined in this file) is used to\r
74  * select between the two.  The simply blinky demo is implemented and described\r
75  * in main_blinky.c.  The more comprehensive test and demo application is\r
76  * implemented and described in main_full.c.\r
77  *\r
78  * This file implements the code that is not demo specific, including the\r
79  * hardware setup and standard FreeRTOS hook functions.\r
80  *\r
81  * ENSURE TO READ THE DOCUMENTATION PAGE FOR THIS PORT AND DEMO APPLICATION ON\r
82  * THE http://www.FreeRTOS.org WEB SITE FOR FULL INFORMATION ON USING THIS DEMO\r
83  * APPLICATION, AND ITS ASSOCIATE FreeRTOS ARCHITECTURE PORT!\r
84  *\r
85  */\r
86 \r
87 /* Scheduler include files. */\r
88 #include "FreeRTOS.h"\r
89 #include "task.h"\r
90 #include "semphr.h"\r
91 \r
92 /* Library includes. */\r
93 #include "board.h"\r
94 #include "asf.h"\r
95 \r
96 /* Set mainCREATE_SIMPLE_BLINKY_DEMO_ONLY to one to run the simple blinky demo,\r
97 or 0 to run the more comprehensive test and demo application. */\r
98 #define mainCREATE_SIMPLE_BLINKY_DEMO_ONLY      1\r
99 \r
100 /*-----------------------------------------------------------*/\r
101 \r
102 /*\r
103  * Configure the hardware as necessary to run this demo.\r
104  */\r
105 static void prvSetupHardware( void );\r
106 \r
107 /*\r
108  * main_blinky() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 1.\r
109  * main_full() is used when mainCREATE_SIMPLE_BLINKY_DEMO_ONLY is set to 0.\r
110  */\r
111 #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1\r
112         extern void main_blinky( void );\r
113 #else\r
114         extern void main_full( void );\r
115 #endif /* #if mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 */\r
116 \r
117 /* Prototypes for the standard FreeRTOS callback/hook functions implemented\r
118 within this file. */\r
119 void vApplicationMallocFailedHook( void );\r
120 void vApplicationIdleHook( void );\r
121 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );\r
122 void vApplicationTickHook( void );\r
123 \r
124 /*-----------------------------------------------------------*/\r
125 \r
126 int main( void )\r
127 {\r
128         /* Configure the hardware ready to run the demo. */\r
129         prvSetupHardware();\r
130 \r
131         /* The mainCREATE_SIMPLE_BLINKY_DEMO_ONLY setting is described at the top\r
132         of this file. */\r
133         #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 1 )\r
134         {\r
135                 main_blinky();\r
136         }\r
137         #else\r
138         {\r
139                 main_full();\r
140         }\r
141         #endif\r
142 \r
143         return 0;\r
144 }\r
145 /*-----------------------------------------------------------*/\r
146 \r
147 static void prvSetupHardware( void )\r
148 {\r
149         sysclk_init();\r
150         board_init();\r
151 }\r
152 /*-----------------------------------------------------------*/\r
153 \r
154 void vApplicationMallocFailedHook( void )\r
155 {\r
156         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
157         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
158         internally by FreeRTOS API functions that create tasks, queues, software\r
159         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
160         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
161 \r
162         /* Force an assert. */\r
163         configASSERT( ( volatile void * ) NULL );\r
164 }\r
165 /*-----------------------------------------------------------*/\r
166 \r
167 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
168 {\r
169         ( void ) pcTaskName;\r
170         ( void ) pxTask;\r
171 \r
172         /* Run time stack overflow checking is performed if\r
173         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
174         function is called if a stack overflow is detected. */\r
175 \r
176         /* Force an assert. */\r
177         configASSERT( ( volatile void * ) NULL );\r
178 }\r
179 /*-----------------------------------------------------------*/\r
180 \r
181 void vApplicationIdleHook( void )\r
182 {\r
183 volatile size_t xFreeHeapSpace;\r
184 \r
185         /* This is just a trivial example of an idle hook.  It is called on each\r
186         cycle of the idle task.  It must *NOT* attempt to block.  In this case the\r
187         idle task just queries the amount of FreeRTOS heap that remains.  See the\r
188         memory management section on the http://www.FreeRTOS.org web site for memory\r
189         management options.  If there is a lot of heap memory free then the\r
190         configTOTAL_HEAP_SIZE value in FreeRTOSConfig.h can be reduced to free up\r
191         RAM. */\r
192         xFreeHeapSpace = xPortGetFreeHeapSize();\r
193 \r
194         /* Remove compiler warning about xFreeHeapSpace being set but never used. */\r
195         ( void ) xFreeHeapSpace;\r
196 }\r
197 /*-----------------------------------------------------------*/\r
198 \r
199 void vApplicationTickHook( void )\r
200 {\r
201         #if( mainCREATE_SIMPLE_BLINKY_DEMO_ONLY == 0 )\r
202         {\r
203         extern void vFullDemoTickHook( void );\r
204         \r
205                 /* The full demo includes some tests that execute in an interrupt\r
206                 context, and the tick hook is used for this purpose. */\r
207                 vFullDemoTickHook();    \r
208         }\r
209         #endif\r
210 }\r
211 /*-----------------------------------------------------------*/\r
212 \r
213 \r
214 \r
215 \r
216 \r
217 \r
218 \r
219 \r
220 \r
221 \r
222 \r
223 \r
224 \r
225 \r
226 \r
227 \r
228 \r
229 \r
230 \r
231 #if 0\r
232 \r
233 \r
234 \r
235 /**\r
236  * \file\r
237  *\r
238  * \brief Getting Started Application.\r
239  *\r
240  * Copyright (c) 2011-2015 Atmel Corporation. All rights reserved.\r
241  *\r
242  * \asf_license_start\r
243  *\r
244  * \page License\r
245  *\r
246  * Redistribution and use in source and binary forms, with or without\r
247  * modification, are permitted provided that the following conditions are met:\r
248  *\r
249  * 1. Redistributions of source code must retain the above copyright notice,\r
250  *    this list of conditions and the following disclaimer.\r
251  *\r
252  * 2. Redistributions in binary form must reproduce the above copyright notice,\r
253  *    this list of conditions and the following disclaimer in the documentation\r
254  *    and/or other materials provided with the distribution.\r
255  *\r
256  * 3. The name of Atmel may not be used to endorse or promote products derived\r
257  *    from this software without specific prior written permission.\r
258  *\r
259  * 4. This software may only be redistributed and used in connection with an\r
260  *    Atmel microcontroller product.\r
261  *\r
262  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED\r
263  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\r
264  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE\r
265  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR\r
266  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
267  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS\r
268  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
269  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\r
270  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN\r
271  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
272  * POSSIBILITY OF SUCH DAMAGE.\r
273  *\r
274  * \asf_license_stop\r
275  *\r
276  */\r
277 \r
278 /**\r
279  * \mainpage Getting Started Application\r
280  *\r
281  * \section Purpose\r
282  *\r
283  * The Getting Started example will help new users get familiar with Atmel's\r
284  * SAM family of microcontrollers. This basic application shows the startup\r
285  * sequence of a chip and how to use its core peripherals.\r
286  *\r
287  * \section Requirements\r
288  *\r
289  * This package can be used with SAM evaluation kits.\r
290  *\r
291  * \section Description\r
292  *\r
293  * The demonstration program makes the LED(s) on the board blink at a fixed rate.\r
294  * This rate is generated by using Time tick timer. The blinking can be stopped\r
295  * using the push button.\r
296  *\r
297  * \section Usage\r
298  *\r
299  * -# Build the program and download it inside the evaluation board.\r
300  * -# On the computer, open and configure a terminal application\r
301  *    (e.g. HyperTerminal on Microsoft Windows) with these settings:\r
302  *   - 115200 bauds\r
303  *   - 8 bits of data\r
304  *   - No parity\r
305  *   - 1 stop bit\r
306  *   - No flow control\r
307  * -# Start the application.\r
308  * -# The LED(s) should start blinking on the board. In the terminal window, the\r
309  *    following text should appear (values depend on the board and chip used):\r
310  *    \code\r
311         -- Getting Started Example xxx --\r
312         -- xxxxxx-xx\r
313         -- Compiled: xxx xx xxxx xx:xx:xx --\r
314 \endcode\r
315  * -# Pressing and release button 1 should make one LED stop & restart\r
316  *    blinking.\r
317  * -# If the button 2 available, pressing button 2 should make the other LED\r
318  *    stop & restart blinking.\r
319  *\r
320  */\r
321 /*\r
322  * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>\r
323  */\r
324 \r
325 #include "FreeRTOS.h"\r
326 #include "task.h"\r
327 \r
328 #include "asf.h"\r
329 #include "stdio_serial.h"\r
330 #include "conf_board.h"\r
331 #include "conf_clock.h"\r
332 \r
333 /** IRQ priority for PIO (The lower the value, the greater the priority) */\r
334 // [main_def_pio_irq_prior]\r
335 #define IRQ_PRIOR_PIO    0\r
336 // [main_def_pio_irq_prior]\r
337 \r
338 /** LED0 blink time, LED1 blink half this time, in ms */\r
339 #define BLINK_PERIOD     1000\r
340 \r
341 #define STRING_EOL    "\r"\r
342 #define STRING_HEADER "-- Getting Started Example --\r\n" \\r
343                 "-- "BOARD_NAME" --\r\n" \\r
344                 "-- Compiled: "__DATE__" "__TIME__" --"STRING_EOL\r
345 \r
346 /** LED0 blinking control. */\r
347 // [main_var_led0_control]\r
348 volatile bool g_b_led0_active = true;\r
349 // [main_var_led0_control]\r
350 \r
351 #ifdef LED1_GPIO\r
352 /** LED1 blinking control. */\r
353 // [main_var_led1_control]\r
354 volatile bool g_b_led1_active = true;\r
355 // [main_var_led1_control]\r
356 #endif\r
357 \r
358 /** Global g_ul_ms_ticks in milliseconds since start of application */\r
359 // [main_var_ticks]\r
360 volatile uint32_t g_ul_ms_ticks = 0;\r
361 // [main_var_ticks]\r
362 \r
363 /// @cond 0\r
364 /**INDENT-OFF**/\r
365 #ifdef __cplusplus\r
366 extern "C" {\r
367 #endif\r
368 /**INDENT-ON**/\r
369 /// @endcond\r
370 \r
371 \r
372 /* Prototypes for the standard FreeRTOS callback/hook functions implemented\r
373 within this file. */\r
374 void vApplicationMallocFailedHook( void );\r
375 void vApplicationIdleHook( void );\r
376 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );\r
377 void vApplicationTickHook( void );\r
378 \r
379 \r
380 /**\r
381  *  \brief Process Buttons Events\r
382  *\r
383  *  Change active states of LEDs when corresponding button events happened.\r
384  */\r
385 static void ProcessButtonEvt(uint8_t uc_button)\r
386 {\r
387 // [main_button1_evnt_process]\r
388         if (uc_button == 0) {\r
389                 g_b_led0_active = !g_b_led0_active;\r
390                 if (!g_b_led0_active) {\r
391                         ioport_set_pin_level(LED0_GPIO, IOPORT_PIN_LEVEL_HIGH);\r
392                 }\r
393         }\r
394 // [main_button1_evnt_process]\r
395 #ifdef LED1_GPIO \r
396         else {\r
397 // [main_button2_evnt_process]\r
398                 g_b_led1_active = !g_b_led1_active;\r
399 \r
400                 /* Enable LED#2 and TC if they were enabled */\r
401                 if (g_b_led1_active) {\r
402                         ioport_set_pin_level(LED1_GPIO, IOPORT_PIN_LEVEL_LOW);\r
403                         tc_start(TC0, 0);\r
404                 }\r
405                 /* Disable LED#2 and TC if they were disabled */\r
406                 else {\r
407                         ioport_set_pin_level(LED1_GPIO, IOPORT_PIN_LEVEL_HIGH);\r
408                         tc_stop(TC0, 0);\r
409                 }\r
410 // [main_button2_evnt_process]\r
411         }\r
412 #endif\r
413 }\r
414 \r
415 /**\r
416  *  \brief Handler for System Tick interrupt.\r
417  *\r
418  *  Process System Tick Event\r
419  *  Increments the g_ul_ms_ticks counter.\r
420  */\r
421 // [main_systick_handler]\r
422 void _SysTick_Handler(void)\r
423 {\r
424         g_ul_ms_ticks++;\r
425 }\r
426 // [main_systick_handler]\r
427 \r
428 /**\r
429  *  \brief Handler for Button 1 rising edge interrupt.\r
430  *\r
431  *  Handle process led1 status change.\r
432  */\r
433 // [main_button1_handler]\r
434 static void Button1_Handler(uint32_t id, uint32_t mask)\r
435 {\r
436         if (PIN_PUSHBUTTON_1_ID == id && PIN_PUSHBUTTON_1_MASK == mask) {\r
437                 ProcessButtonEvt(0);\r
438         }\r
439 }\r
440 // [main_button1_handler]\r
441 \r
442 #ifndef BOARD_NO_PUSHBUTTON_2\r
443 /**\r
444  *  \brief Handler for Button 2 falling edge interrupt.\r
445  *\r
446  *  Handle process led2 status change.\r
447  */\r
448 // [main_button2_handler] \r
449 static void Button2_Handler(uint32_t id, uint32_t mask)\r
450 {\r
451         if (PIN_PUSHBUTTON_2_ID == id && PIN_PUSHBUTTON_2_MASK == mask) {\r
452                 ProcessButtonEvt(1);\r
453         }\r
454 }\r
455 // [main_button2_handler]\r
456 #endif\r
457 \r
458 /**\r
459  *  \brief Configure the Pushbuttons\r
460  *\r
461  *  Configure the PIO as inputs and generate corresponding interrupt when\r
462  *  pressed or released.\r
463  */\r
464 static void configure_buttons(void)\r
465 {\r
466 // [main_button1_configure]\r
467         /* Configure Pushbutton 1 */\r
468         pmc_enable_periph_clk(PIN_PUSHBUTTON_1_ID);\r
469         pio_set_debounce_filter(PIN_PUSHBUTTON_1_PIO, PIN_PUSHBUTTON_1_MASK, 10);\r
470         /* Interrupt on rising edge  */\r
471         pio_handler_set(PIN_PUSHBUTTON_1_PIO, PIN_PUSHBUTTON_1_ID,\r
472                         PIN_PUSHBUTTON_1_MASK, PIN_PUSHBUTTON_1_ATTR, Button1_Handler);\r
473         NVIC_EnableIRQ((IRQn_Type) PIN_PUSHBUTTON_1_ID);\r
474         pio_handler_set_priority(PIN_PUSHBUTTON_1_PIO,\r
475                         (IRQn_Type) PIN_PUSHBUTTON_1_ID, IRQ_PRIOR_PIO);\r
476         pio_enable_interrupt(PIN_PUSHBUTTON_1_PIO, PIN_PUSHBUTTON_1_MASK);\r
477 // [main_button1_configure]\r
478 #ifndef BOARD_NO_PUSHBUTTON_2\r
479 // [main_button2_configure]\r
480         /* Configure Pushbutton 2 */\r
481         pmc_enable_periph_clk(PIN_PUSHBUTTON_2_ID);\r
482         pio_set_debounce_filter(PIN_PUSHBUTTON_2_PIO, PIN_PUSHBUTTON_2_MASK, 10);\r
483         /* Interrupt on falling edge */\r
484         pio_handler_set(PIN_PUSHBUTTON_2_PIO, PIN_PUSHBUTTON_2_ID,\r
485                         PIN_PUSHBUTTON_2_MASK, PIN_PUSHBUTTON_2_ATTR, Button2_Handler);\r
486         NVIC_EnableIRQ((IRQn_Type) PIN_PUSHBUTTON_2_ID);\r
487         pio_handler_set_priority(PIN_PUSHBUTTON_2_PIO,\r
488                         (IRQn_Type) PIN_PUSHBUTTON_2_ID, IRQ_PRIOR_PIO);\r
489         pio_enable_interrupt(PIN_PUSHBUTTON_2_PIO, PIN_PUSHBUTTON_2_MASK);\r
490 // [main_button2_configure]\r
491 #endif\r
492 }\r
493 \r
494 /**\r
495  *  Interrupt handler for TC0 interrupt. Toggles the state of LED\#2.\r
496  */\r
497 // [main_tc0_handler]\r
498 #ifndef BOARD_NO_LED_1\r
499 void TC0_Handler(void)\r
500 {\r
501         volatile uint32_t ul_dummy;\r
502 \r
503         /* Clear status bit to acknowledge interrupt */\r
504         ul_dummy = tc_get_status(TC0, 0);\r
505 \r
506         /* Avoid compiler warning */\r
507         UNUSED(ul_dummy);\r
508 \r
509 #ifdef LED1_GPIO\r
510         /** Toggle LED state. */\r
511         ioport_toggle_pin_level(LED1_GPIO);\r
512 #endif\r
513 \r
514         printf("2 ");\r
515 }\r
516 // [main_tc0_handler]\r
517 \r
518 /**\r
519  *  Configure Timer Counter 0 to generate an interrupt every 250ms.\r
520  */\r
521 // [main_tc_configure]\r
522 static void configure_tc(void)\r
523 {\r
524         uint32_t ul_div;\r
525         uint32_t ul_tcclks;\r
526         uint32_t ul_sysclk = sysclk_get_cpu_hz();\r
527 \r
528         /* Configure PMC */\r
529         pmc_enable_periph_clk(ID_TC0);\r
530 #if SAMG55\r
531         /* Enable PCK output */\r
532         pmc_disable_pck(PMC_PCK_3);\r
533         pmc_switch_pck_to_sclk(PMC_PCK_3, PMC_PCK_PRES_CLK_1);\r
534         pmc_enable_pck(PMC_PCK_3);\r
535 #endif\r
536 \r
537         /** Configure TC for a 4Hz frequency and trigger on RC compare. */\r
538         tc_find_mck_divisor(4, ul_sysclk, &ul_div, &ul_tcclks, ul_sysclk);\r
539         tc_init(TC0, 0, ul_tcclks | TC_CMR_CPCTRG);\r
540         tc_write_rc(TC0, 0, (ul_sysclk / ul_div) / 4);\r
541 \r
542         /* Configure and enable interrupt on RC compare */\r
543         NVIC_EnableIRQ((IRQn_Type) ID_TC0);\r
544         tc_enable_interrupt(TC0, 0, TC_IER_CPCS);\r
545 \r
546 #ifdef LED1_GPIO\r
547         /** Start the counter if LED1 is enabled. */\r
548         if (g_b_led1_active) {\r
549                 tc_start(TC0, 0);\r
550         }\r
551 #else\r
552         tc_start(TC0, 0);\r
553 #endif\r
554 }\r
555 #endif\r
556 // [main_tc_configure]\r
557 \r
558 /**\r
559  *  Configure UART console.\r
560  */\r
561 // [main_console_configure]\r
562 static void configure_console(void)\r
563 {\r
564         const usart_serial_options_t uart_serial_options = {\r
565                 .baudrate = CONF_UART_BAUDRATE,\r
566 #ifdef CONF_UART_CHAR_LENGTH\r
567                 .charlength = CONF_UART_CHAR_LENGTH,\r
568 #endif\r
569                 .paritytype = CONF_UART_PARITY,\r
570 #ifdef CONF_UART_STOP_BITS\r
571                 .stopbits = CONF_UART_STOP_BITS,\r
572 #endif\r
573         };\r
574 \r
575         /* Configure console UART. */\r
576         sysclk_enable_peripheral_clock(CONSOLE_UART_ID);\r
577         stdio_serial_init(CONF_UART, &uart_serial_options);\r
578 }\r
579 \r
580 // [main_console_configure]\r
581 \r
582 /**\r
583  * \brief Wait for the given number of milliseconds (using the g_ul_ms_ticks\r
584  * generated by the SAM's microcontrollers's system tick).\r
585  *\r
586  * \param ul_dly_ticks  Delay to wait for, in milliseconds.\r
587  */\r
588 // [main_ms_delay]\r
589 static void mdelay(uint32_t ul_dly_ticks)\r
590 {\r
591         uint32_t ul_cur_ticks;\r
592 \r
593         ul_cur_ticks = g_ul_ms_ticks;\r
594         while ((g_ul_ms_ticks - ul_cur_ticks) < ul_dly_ticks);\r
595 }\r
596 // [main_ms_delay]\r
597 \r
598 /**\r
599  *  \brief getting-started Application entry point.\r
600  *\r
601  *  \return Unused (ANSI-C compatibility).\r
602  */\r
603 // [main]\r
604 int main(void)\r
605 {\r
606 //! [main_step_sys_init]\r
607         /* Initialize the SAM system */\r
608         sysclk_init();\r
609         board_init();\r
610 //! [main_step_sys_init]\r
611 \r
612 #ifndef BOARD_NO_PUSHBUTTON_2\r
613 #if (SAMV71 || SAMV70 || SAMS70 || SAME70)\r
614         if (GPIO_PUSH_BUTTON_2 == PIO_PB12_IDX) {\r
615                 matrix_set_system_io(matrix_get_system_io() | CCFG_SYSIO_SYSIO12);\r
616         }\r
617         ioport_set_pin_dir(GPIO_PUSH_BUTTON_2, IOPORT_DIR_INPUT);\r
618         ioport_set_pin_mode(GPIO_PUSH_BUTTON_2, GPIO_PUSH_BUTTON_2_FLAGS);\r
619         ioport_set_pin_sense_mode(GPIO_PUSH_BUTTON_2, GPIO_PUSH_BUTTON_2_SENSE);\r
620 #endif\r
621 #endif\r
622 //! [main_step_console_init]\r
623         /* Initialize the console uart */\r
624         configure_console();\r
625 //! [main_step_console_init]\r
626 \r
627         /* Output example information */\r
628         puts(STRING_HEADER);\r
629 \r
630         /* Configure systick for 1 ms */\r
631         puts("Configure system tick to get 1ms tick period.\r");\r
632 //! [main_step_systick_init]\r
633         if (SysTick_Config(sysclk_get_cpu_hz() / 1000)) {\r
634                 puts("-F- Systick configuration error\r");\r
635                 while (1);\r
636         }\r
637 //! [main_step_systick_init]\r
638 \r
639 #ifndef BOARD_NO_LED_1\r
640         puts("Configure TC.\r");\r
641 //! [main_step_tc_init]\r
642         configure_tc();\r
643 //! [main_step_tc_init]\r
644 #endif\r
645 \r
646         puts("Configure buttons with debouncing.\r");\r
647 //! [main_step_btn_init]\r
648         configure_buttons();\r
649 //! [main_step_btn_init]\r
650 \r
651         printf("Press %s to Start/Stop the %s blinking.\r\n",\r
652                         PUSHBUTTON_1_NAME, LED_0_NAME);\r
653 \r
654 #ifndef BOARD_NO_PUSHBUTTON_2\r
655         printf("Press %s to Start/Stop the %s blinking.\r\n",\r
656                         PUSHBUTTON_2_NAME, LED_1_NAME);\r
657 #endif\r
658 \r
659 //! [main_step_loop]\r
660         while (1) {\r
661                 /* Wait for LED to be active */\r
662                 while (!g_b_led0_active);\r
663 \r
664                 /* Toggle LED state if active */\r
665                 if (g_b_led0_active) {\r
666                         ioport_toggle_pin_level(LED0_GPIO);\r
667                         printf("1 ");\r
668                 }\r
669 \r
670                 /* Wait for 500ms */\r
671                 mdelay(500);\r
672         }\r
673 //! [main_step_loop]\r
674 }\r
675 // [main]\r
676 /// @cond 0\r
677 /**INDENT-OFF**/\r
678 #ifdef __cplusplus\r
679 }\r
680 #endif\r
681 /**INDENT-ON**/\r
682 /// @endcond\r
683 \r
684 void vApplicationMallocFailedHook( void )\r
685 {\r
686         /* Called if a call to pvPortMalloc() fails because there is insufficient\r
687         free memory available in the FreeRTOS heap.  pvPortMalloc() is called\r
688         internally by FreeRTOS API functions that create tasks, queues, software\r
689         timers, and semaphores.  The size of the FreeRTOS heap is set by the\r
690         configTOTAL_HEAP_SIZE configuration constant in FreeRTOSConfig.h. */\r
691 \r
692         /* Force an assert. */\r
693         configASSERT( ( volatile void * ) NULL );\r
694 }\r
695 /*-----------------------------------------------------------*/\r
696 \r
697 void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )\r
698 {\r
699         ( void ) pcTaskName;\r
700         ( void ) pxTask;\r
701 \r
702         /* Run time stack overflow checking is performed if\r
703         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
704         function is called if a stack overflow is detected. */\r
705 \r
706         /* Force an assert. */\r
707         configASSERT( ( volatile void * ) NULL );\r
708 }\r
709 /*-----------------------------------------------------------*/\r
710 \r
711 void vApplicationIdleHook( void )\r
712 {\r
713 volatile size_t xFreeHeapSpace;\r
714 \r
715         /* This is just a trivial example of an idle hook.  It is called on each\r
716         cycle of the idle task.  It must *NOT* attempt to block.  In this case the\r
717         idle task just queries the amount of FreeRTOS heap that remains.  See the\r
718         memory management section on the http://www.FreeRTOS.org web site for memory\r
719         management options.  If there is a lot of heap memory free then the\r
720         configTOTAL_HEAP_SIZE value in FreeRTOSConfig.h can be reduced to free up\r
721         RAM. */\r
722         xFreeHeapSpace = xPortGetFreeHeapSize();\r
723 \r
724         /* Remove compiler warning about xFreeHeapSpace being set but never used. */\r
725         ( void ) xFreeHeapSpace;\r
726 }\r
727 /*-----------------------------------------------------------*/\r
728 \r
729 void vApplicationTickHook( void )\r
730 {\r
731 }\r
732 /*-----------------------------------------------------------*/\r
733 \r
734 #endif\r