]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M0+_Atmel_SAMD20_XPlained/RTOSDemo/src/main.c
e8f3ba8e99043b3a23922003672e159534d7e57b
[freertos] / FreeRTOS / Demo / CORTEX_M0+_Atmel_SAMD20_XPlained / RTOSDemo / src / main.c
1 /*\r
2     FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 /* FreeRTOS includes. */\r
66 #include "FreeRTOS.h"\r
67 #include "task.h"\r
68 \r
69 /* Library includes. */\r
70 #include <asf.h>\r
71 \r
72 void usart_read_callback(const struct usart_module *const usart_module);\r
73 void usart_write_callback(const struct usart_module *const usart_module);\r
74 static void prvSetupHardware( void );\r
75 void configure_usart(void);\r
76 void configure_usart_callbacks(void);\r
77 void vApplicationMallocFailedHook( void );\r
78 void vApplicationIdleHook( void );\r
79 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName );\r
80 void vApplicationTickHook( void );\r
81 \r
82 struct usart_module usart_instance;\r
83 #define MAX_RX_BUFFER_LENGTH   5\r
84 volatile uint8_t rx_buffer[MAX_RX_BUFFER_LENGTH];\r
85 #define NUM 1024\r
86 char cChars[ NUM ] = { 0 };\r
87 \r
88 void usart_read_callback(const struct usart_module *const usart_module)\r
89 {\r
90         usart_write_buffer_job(&usart_instance, (uint8_t *)rx_buffer, MAX_RX_BUFFER_LENGTH);\r
91 }\r
92 \r
93 void usart_write_callback(const struct usart_module *const usart_module)\r
94 {\r
95         port_pin_toggle_output_level(LED_0_PIN);\r
96 }\r
97 \r
98 void configure_usart(void)\r
99 {\r
100         struct usart_config config_usart;\r
101         usart_get_config_defaults(&config_usart);\r
102         config_usart.baudrate    = 115200;\r
103         config_usart.mux_setting = EDBG_CDC_SERCOM_MUX_SETTING;\r
104         config_usart.pinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0;\r
105         config_usart.pinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1;\r
106         config_usart.pinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2;\r
107         config_usart.pinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3;\r
108         while (usart_init(&usart_instance,\r
109         EDBG_CDC_MODULE, &config_usart) != STATUS_OK) {\r
110         }\r
111         usart_enable(&usart_instance);\r
112 }\r
113 \r
114 void configure_usart_callbacks(void)\r
115 {\r
116         usart_register_callback(&usart_instance,\r
117         usart_write_callback, USART_CALLBACK_BUFFER_TRANSMITTED);\r
118         usart_register_callback(&usart_instance,\r
119         usart_read_callback, USART_CALLBACK_BUFFER_RECEIVED);\r
120         usart_enable_callback(&usart_instance, USART_CALLBACK_BUFFER_TRANSMITTED);\r
121         usart_enable_callback(&usart_instance, USART_CALLBACK_BUFFER_RECEIVED);\r
122 }\r
123 \r
124 \r
125 \r
126 \r
127 int main (void)\r
128 {\r
129         prvSetupHardware();\r
130 \r
131         configure_usart();\r
132         configure_usart_callbacks();\r
133         system_interrupt_enable_global();\r
134 \r
135         uint8_t string[] = "Hello World!\r\n";\r
136         usart_write_buffer_job(&usart_instance, string, sizeof(string));\r
137 \r
138 \r
139 \r
140         while (true) {\r
141                 usart_read_buffer_job(&usart_instance,\r
142                 (uint8_t *)rx_buffer, MAX_RX_BUFFER_LENGTH);\r
143         }\r
144 \r
145         while (1) {\r
146                 if (port_pin_get_input_level(BUTTON_0_PIN) == BUTTON_0_ACTIVE) {\r
147                         port_pin_set_output_level(LED_0_PIN, LED_0_ACTIVE);\r
148                 } else {\r
149                         port_pin_set_output_level(LED_0_PIN, !LED_0_ACTIVE);\r
150                 }\r
151         }\r
152 }\r
153 /*-----------------------------------------------------------*/\r
154 \r
155 static void prvSetupHardware( void )\r
156 {\r
157         system_init();\r
158 }\r
159 /*-----------------------------------------------------------*/\r
160 \r
161 void vApplicationMallocFailedHook( void )\r
162 {\r
163         /* vApplicationMallocFailedHook() will only be called if\r
164         configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook\r
165         function that will get called if a call to pvPortMalloc() fails.\r
166         pvPortMalloc() is called internally by the kernel whenever a task, queue,\r
167         timer or semaphore is created.  It is also called by various parts of the\r
168         demo application.  If heap_1.c or heap_2.c are used, then the size of the\r
169         heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in\r
170         FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used\r
171         to query the size of free heap space that remains (although it does not\r
172         provide information on how the remaining heap might be fragmented). */\r
173         taskDISABLE_INTERRUPTS();\r
174         for( ;; );\r
175 }\r
176 /*-----------------------------------------------------------*/\r
177 \r
178 void vApplicationIdleHook( void )\r
179 {\r
180         /* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set\r
181         to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle\r
182         task.  It is essential that code added to this hook function never attempts\r
183         to block in any way (for example, call xQueueReceive() with a block time\r
184         specified, or call vTaskDelay()).  If the application makes use of the\r
185         vTaskDelete() API function (as this demo application does) then it is also\r
186         important that vApplicationIdleHook() is permitted to return to its calling\r
187         function, because it is the responsibility of the idle task to clean up\r
188         memory allocated by the kernel to any task that has since been deleted. */\r
189 }\r
190 /*-----------------------------------------------------------*/\r
191 \r
192 void vApplicationStackOverflowHook( xTaskHandle pxTask, signed char *pcTaskName )\r
193 {\r
194         ( void ) pcTaskName;\r
195         ( void ) pxTask;\r
196 \r
197         /* Run time stack overflow checking is performed if\r
198         configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook\r
199         function is called if a stack overflow is detected. */\r
200         taskDISABLE_INTERRUPTS();\r
201         for( ;; );\r
202 }\r
203 /*-----------------------------------------------------------*/\r
204 \r
205 void vApplicationTickHook( void )\r
206 {\r
207         /* This function will be called by each tick interrupt if\r
208         configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h.  User code can be\r
209         added here, but the tick hook is called from an interrupt context, so\r
210         code must not attempt to block, and only the interrupt safe FreeRTOS API\r
211         functions can be used (those that end in FromISR()). */\r
212 }\r
213 \r