]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/AVR_ATMega323_WinAVR/serial/serial.c
4bb13e90399e6a52aa6f6bbf5429ae06a669b652
[freertos] / FreeRTOS / Demo / AVR_ATMega323_WinAVR / serial / serial.c
1 /*\r
2     FreeRTOS V8.2.0 - Copyright (C) 2015 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 Changes from V1.2.3\r
72 \r
73         + The function xPortInitMinimal() has been renamed to \r
74           xSerialPortInitMinimal() and the function xPortInit() has been renamed\r
75           to xSerialPortInit().\r
76 \r
77 Changes from V2.0.0\r
78 \r
79         + Delay periods are now specified using variables and constants of\r
80           TickType_t rather than unsigned long.\r
81         + xQueueReceiveFromISR() used in place of xQueueReceive() within the ISR.\r
82 \r
83 Changes from V2.6.0\r
84 \r
85         + Replaced the inb() and outb() functions with direct memory\r
86           access.  This allows the port to be built with the 20050414 build of\r
87           WinAVR.\r
88 */\r
89 \r
90 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. */\r
91 \r
92 \r
93 #include <stdlib.h>\r
94 #include <avr/interrupt.h>\r
95 #include "FreeRTOS.h"\r
96 #include "queue.h"\r
97 #include "task.h"\r
98 #include "serial.h"\r
99 \r
100 #define serBAUD_DIV_CONSTANT                    ( ( unsigned long ) 16 )\r
101 \r
102 /* Constants for writing to UCSRB. */\r
103 #define serRX_INT_ENABLE                                ( ( unsigned char ) 0x80 )\r
104 #define serRX_ENABLE                                    ( ( unsigned char ) 0x10 )\r
105 #define serTX_ENABLE                                    ( ( unsigned char ) 0x08 )\r
106 #define serTX_INT_ENABLE                                ( ( unsigned char ) 0x20 )\r
107 \r
108 /* Constants for writing to UCSRC. */\r
109 #define serUCSRC_SELECT                                 ( ( unsigned char ) 0x80 )\r
110 #define serEIGHT_DATA_BITS                              ( ( unsigned char ) 0x06 )\r
111 \r
112 static QueueHandle_t xRxedChars; \r
113 static QueueHandle_t xCharsForTx; \r
114 \r
115 #define vInterruptOn()                                                                          \\r
116 {                                                                                                                       \\r
117         unsigned char ucByte;                                                           \\r
118                                                                                                                         \\r
119         ucByte = UCSRB;                                                                                 \\r
120         ucByte |= serTX_INT_ENABLE;                                                             \\r
121         UCSRB = ucByte;                                                                                 \\r
122 }                                                                                                                                                               \r
123 /*-----------------------------------------------------------*/\r
124 \r
125 #define vInterruptOff()                                                                         \\r
126 {                                                                                                                       \\r
127         unsigned char ucInByte;                                                         \\r
128                                                                                                                         \\r
129         ucInByte = UCSRB;                                                                               \\r
130         ucInByte &= ~serTX_INT_ENABLE;                                                  \\r
131         UCSRB = ucInByte;                                                                               \\r
132 }\r
133 /*-----------------------------------------------------------*/\r
134 \r
135 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
136 {\r
137 unsigned long ulBaudRateCounter;\r
138 unsigned char ucByte;\r
139 \r
140         portENTER_CRITICAL();\r
141         {\r
142                 /* Create the queues used by the com test task. */\r
143                 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
144                 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
145 \r
146                 /* Calculate the baud rate register value from the equation in the\r
147                 data sheet. */\r
148                 ulBaudRateCounter = ( configCPU_CLOCK_HZ / ( serBAUD_DIV_CONSTANT * ulWantedBaud ) ) - ( unsigned long ) 1;\r
149 \r
150                 /* Set the baud rate. */        \r
151                 ucByte = ( unsigned char ) ( ulBaudRateCounter & ( unsigned long ) 0xff );      \r
152                 UBRRL = ucByte;\r
153 \r
154                 ulBaudRateCounter >>= ( unsigned long ) 8;\r
155                 ucByte = ( unsigned char ) ( ulBaudRateCounter & ( unsigned long ) 0xff );      \r
156                 UBRRH = ucByte;\r
157 \r
158                 /* Enable the Rx interrupt.  The Tx interrupt will get enabled\r
159                 later. Also enable the Rx and Tx. */\r
160                 UCSRB = ( serRX_INT_ENABLE | serRX_ENABLE | serTX_ENABLE );\r
161 \r
162                 /* Set the data bits to 8. */\r
163                 UCSRC = ( serUCSRC_SELECT | serEIGHT_DATA_BITS );\r
164         }\r
165         portEXIT_CRITICAL();\r
166         \r
167         /* Unlike other ports, this serial code does not allow for more than one\r
168         com port.  We therefore don't return a pointer to a port structure and can\r
169         instead just return NULL. */\r
170         return NULL;\r
171 }\r
172 /*-----------------------------------------------------------*/\r
173 \r
174 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
175 {\r
176         /* Only one port is supported. */\r
177         ( void ) pxPort;\r
178 \r
179         /* Get the next character from the buffer.  Return false if no characters\r
180         are available, or arrive before xBlockTime expires. */\r
181         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
182         {\r
183                 return pdTRUE;\r
184         }\r
185         else\r
186         {\r
187                 return pdFALSE;\r
188         }\r
189 }\r
190 /*-----------------------------------------------------------*/\r
191 \r
192 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
193 {\r
194         /* Only one port is supported. */\r
195         ( void ) pxPort;\r
196 \r
197         /* Return false if after the block time there is no room on the Tx queue. */\r
198         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
199         {\r
200                 return pdFAIL;\r
201         }\r
202 \r
203         vInterruptOn();\r
204 \r
205         return pdPASS;\r
206 }\r
207 /*-----------------------------------------------------------*/\r
208 \r
209 void vSerialClose( xComPortHandle xPort )\r
210 {\r
211 unsigned char ucByte;\r
212 \r
213         /* The parameter is not used. */\r
214         ( void ) xPort;\r
215 \r
216         /* Turn off the interrupts.  We may also want to delete the queues and/or\r
217         re-install the original ISR. */\r
218 \r
219         portENTER_CRITICAL();\r
220         {\r
221                 vInterruptOff();\r
222                 ucByte = UCSRB;\r
223                 ucByte &= ~serRX_INT_ENABLE;\r
224                 UCSRB = ucByte;\r
225         }\r
226         portEXIT_CRITICAL();\r
227 }\r
228 /*-----------------------------------------------------------*/\r
229 \r
230 SIGNAL( SIG_UART_RECV )\r
231 {\r
232 signed char cChar;\r
233 signed portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
234 \r
235         /* Get the character and post it on the queue of Rxed characters.\r
236         If the post causes a task to wake force a context switch as the woken task\r
237         may have a higher priority than the task we have interrupted. */\r
238         cChar = UDR;\r
239 \r
240         xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
241 \r
242         if( xHigherPriorityTaskWoken != pdFALSE )\r
243         {\r
244                 taskYIELD();\r
245         }\r
246 }\r
247 /*-----------------------------------------------------------*/\r
248 \r
249 SIGNAL( SIG_UART_DATA )\r
250 {\r
251 signed char cChar, cTaskWoken;\r
252 \r
253         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &cTaskWoken ) == pdTRUE )\r
254         {\r
255                 /* Send the next character queued for Tx. */\r
256                 UDR = cChar;\r
257         }\r
258         else\r
259         {\r
260                 /* Queue empty, nothing to send. */\r
261                 vInterruptOff();\r
262         }\r
263 }\r
264 \r