]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/AVR_ATMega323_WinAVR/serial/serial.c
7c5f0130d74d03bb4c840e42c5b18c7ae62f122a
[freertos] / FreeRTOS / Demo / AVR_ATMega323_WinAVR / serial / serial.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 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. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /*\r
30 Changes from V1.2.3\r
31 \r
32         + The function xPortInitMinimal() has been renamed to \r
33           xSerialPortInitMinimal() and the function xPortInit() has been renamed\r
34           to xSerialPortInit().\r
35 \r
36 Changes from V2.0.0\r
37 \r
38         + Delay periods are now specified using variables and constants of\r
39           TickType_t rather than unsigned long.\r
40         + xQueueReceiveFromISR() used in place of xQueueReceive() within the ISR.\r
41 \r
42 Changes from V2.6.0\r
43 \r
44         + Replaced the inb() and outb() functions with direct memory\r
45           access.  This allows the port to be built with the 20050414 build of\r
46           WinAVR.\r
47 */\r
48 \r
49 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. */\r
50 \r
51 \r
52 #include <stdlib.h>\r
53 #include <avr/interrupt.h>\r
54 #include "FreeRTOS.h"\r
55 #include "queue.h"\r
56 #include "task.h"\r
57 #include "serial.h"\r
58 \r
59 #define serBAUD_DIV_CONSTANT                    ( ( unsigned long ) 16 )\r
60 \r
61 /* Constants for writing to UCSRB. */\r
62 #define serRX_INT_ENABLE                                ( ( unsigned char ) 0x80 )\r
63 #define serRX_ENABLE                                    ( ( unsigned char ) 0x10 )\r
64 #define serTX_ENABLE                                    ( ( unsigned char ) 0x08 )\r
65 #define serTX_INT_ENABLE                                ( ( unsigned char ) 0x20 )\r
66 \r
67 /* Constants for writing to UCSRC. */\r
68 #define serUCSRC_SELECT                                 ( ( unsigned char ) 0x80 )\r
69 #define serEIGHT_DATA_BITS                              ( ( unsigned char ) 0x06 )\r
70 \r
71 static QueueHandle_t xRxedChars; \r
72 static QueueHandle_t xCharsForTx; \r
73 \r
74 #define vInterruptOn()                                                                          \\r
75 {                                                                                                                       \\r
76         unsigned char ucByte;                                                           \\r
77                                                                                                                         \\r
78         ucByte = UCSRB;                                                                                 \\r
79         ucByte |= serTX_INT_ENABLE;                                                             \\r
80         UCSRB = ucByte;                                                                                 \\r
81 }                                                                                                                                                               \r
82 /*-----------------------------------------------------------*/\r
83 \r
84 #define vInterruptOff()                                                                         \\r
85 {                                                                                                                       \\r
86         unsigned char ucInByte;                                                         \\r
87                                                                                                                         \\r
88         ucInByte = UCSRB;                                                                               \\r
89         ucInByte &= ~serTX_INT_ENABLE;                                                  \\r
90         UCSRB = ucInByte;                                                                               \\r
91 }\r
92 /*-----------------------------------------------------------*/\r
93 \r
94 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
95 {\r
96 unsigned long ulBaudRateCounter;\r
97 unsigned char ucByte;\r
98 \r
99         portENTER_CRITICAL();\r
100         {\r
101                 /* Create the queues used by the com test task. */\r
102                 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
103                 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
104 \r
105                 /* Calculate the baud rate register value from the equation in the\r
106                 data sheet. */\r
107                 ulBaudRateCounter = ( configCPU_CLOCK_HZ / ( serBAUD_DIV_CONSTANT * ulWantedBaud ) ) - ( unsigned long ) 1;\r
108 \r
109                 /* Set the baud rate. */        \r
110                 ucByte = ( unsigned char ) ( ulBaudRateCounter & ( unsigned long ) 0xff );      \r
111                 UBRRL = ucByte;\r
112 \r
113                 ulBaudRateCounter >>= ( unsigned long ) 8;\r
114                 ucByte = ( unsigned char ) ( ulBaudRateCounter & ( unsigned long ) 0xff );      \r
115                 UBRRH = ucByte;\r
116 \r
117                 /* Enable the Rx interrupt.  The Tx interrupt will get enabled\r
118                 later. Also enable the Rx and Tx. */\r
119                 UCSRB = ( serRX_INT_ENABLE | serRX_ENABLE | serTX_ENABLE );\r
120 \r
121                 /* Set the data bits to 8. */\r
122                 UCSRC = ( serUCSRC_SELECT | serEIGHT_DATA_BITS );\r
123         }\r
124         portEXIT_CRITICAL();\r
125         \r
126         /* Unlike other ports, this serial code does not allow for more than one\r
127         com port.  We therefore don't return a pointer to a port structure and can\r
128         instead just return NULL. */\r
129         return NULL;\r
130 }\r
131 /*-----------------------------------------------------------*/\r
132 \r
133 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
134 {\r
135         /* Only one port is supported. */\r
136         ( void ) pxPort;\r
137 \r
138         /* Get the next character from the buffer.  Return false if no characters\r
139         are available, or arrive before xBlockTime expires. */\r
140         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
141         {\r
142                 return pdTRUE;\r
143         }\r
144         else\r
145         {\r
146                 return pdFALSE;\r
147         }\r
148 }\r
149 /*-----------------------------------------------------------*/\r
150 \r
151 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
152 {\r
153         /* Only one port is supported. */\r
154         ( void ) pxPort;\r
155 \r
156         /* Return false if after the block time there is no room on the Tx queue. */\r
157         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
158         {\r
159                 return pdFAIL;\r
160         }\r
161 \r
162         vInterruptOn();\r
163 \r
164         return pdPASS;\r
165 }\r
166 /*-----------------------------------------------------------*/\r
167 \r
168 void vSerialClose( xComPortHandle xPort )\r
169 {\r
170 unsigned char ucByte;\r
171 \r
172         /* The parameter is not used. */\r
173         ( void ) xPort;\r
174 \r
175         /* Turn off the interrupts.  We may also want to delete the queues and/or\r
176         re-install the original ISR. */\r
177 \r
178         portENTER_CRITICAL();\r
179         {\r
180                 vInterruptOff();\r
181                 ucByte = UCSRB;\r
182                 ucByte &= ~serRX_INT_ENABLE;\r
183                 UCSRB = ucByte;\r
184         }\r
185         portEXIT_CRITICAL();\r
186 }\r
187 /*-----------------------------------------------------------*/\r
188 \r
189 SIGNAL( SIG_UART_RECV )\r
190 {\r
191 signed char cChar;\r
192 signed portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
193 \r
194         /* Get the character and post it on the queue of Rxed characters.\r
195         If the post causes a task to wake force a context switch as the woken task\r
196         may have a higher priority than the task we have interrupted. */\r
197         cChar = UDR;\r
198 \r
199         xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
200 \r
201         if( xHigherPriorityTaskWoken != pdFALSE )\r
202         {\r
203                 taskYIELD();\r
204         }\r
205 }\r
206 /*-----------------------------------------------------------*/\r
207 \r
208 SIGNAL( SIG_UART_DATA )\r
209 {\r
210 signed char cChar, cTaskWoken;\r
211 \r
212         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &cTaskWoken ) == pdTRUE )\r
213         {\r
214                 /* Send the next character queued for Tx. */\r
215                 UDR = cChar;\r
216         }\r
217         else\r
218         {\r
219                 /* Queue empty, nothing to send. */\r
220                 vInterruptOff();\r
221         }\r
222 }\r
223 \r