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