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