]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MB96340_Softune/FreeRTOS_96348hs_SK16FX100PMC/Src/serial/serial.c
Update version number.
[freertos] / FreeRTOS / Demo / MB96340_Softune / FreeRTOS_96348hs_SK16FX100PMC / Src / serial / serial.c
1 /*\r
2     FreeRTOS V7.5.1 - 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 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.   \r
66  * \r
67  * This file only supports UART 0\r
68  */\r
69 \r
70 /* Standard includes. */\r
71 #include <stdlib.h>\r
72 \r
73 /* Scheduler includes. */\r
74 #include "FreeRTOS.h"\r
75 #include "queue.h"\r
76 #include "task.h"\r
77 \r
78 /* Demo application includes. */\r
79 #include "serial.h"\r
80 \r
81 /* The queue used to hold received characters. */\r
82 static xQueueHandle                     xRxedChars;\r
83 \r
84 /* The queue used to hold characters waiting transmission. */\r
85 static xQueueHandle                     xCharsForTx;\r
86 \r
87 static volatile portSHORT       sTHREEmpty;\r
88 \r
89 static volatile portSHORT       queueFail = pdFALSE;\r
90 \r
91 /*-----------------------------------------------------------*/\r
92 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
93 {\r
94         /* Initialise the hardware. */\r
95         portENTER_CRITICAL();\r
96         {\r
97                 /* Create the queues used by the com test task. */\r
98                 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof(signed portCHAR) );\r
99                 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof(signed portCHAR) );\r
100 \r
101                 if( xRxedChars == 0 )\r
102                 {\r
103                         queueFail = pdTRUE;\r
104                 }\r
105 \r
106                 if( xCharsForTx == 0 )\r
107                 {\r
108                         queueFail = pdTRUE;\r
109                 }\r
110 \r
111                 /* Initialize UART asynchronous mode */\r
112                 BGR0 = configCLKP1_CLOCK_HZ / ulWantedBaud;\r
113 \r
114                 SCR0 = 0x17;    /* 8N1 */\r
115                 SMR0 = 0x0d;    /* enable SOT3, Reset, normal mode */\r
116                 SSR0 = 0x02;    /* LSB first, enable receive interrupts */\r
117 \r
118                 PIER08_IE2 = 1; /* enable input */\r
119                 DDR08_D2 = 0;   /* switch P08_2 to input */\r
120                 DDR08_D3 = 1;   /* switch P08_3 to output */\r
121         }\r
122         portEXIT_CRITICAL();\r
123 \r
124         /* Unlike other ports, this serial code does not allow for more than one\r
125         com port.  We therefore don't return a pointer to a port structure and can\r
126         instead just return NULL. */\r
127         return NULL;\r
128 }\r
129 /*-----------------------------------------------------------*/\r
130 \r
131 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
132 {\r
133         /* Get the next character from the buffer.  Return false if no characters\r
134         are available, or arrive before xBlockTime expires. */\r
135         if( xQueueReceive(xRxedChars, pcRxedChar, xBlockTime) )\r
136         {\r
137                 return pdTRUE;\r
138         }\r
139         else\r
140         {\r
141                 return pdFALSE;\r
142         }\r
143 }\r
144 /*-----------------------------------------------------------*/\r
145 \r
146 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
147 {\r
148 signed portBASE_TYPE    xReturn;\r
149 \r
150         /* Transmit a character. */\r
151         portENTER_CRITICAL();\r
152         {\r
153                 if( sTHREEmpty == pdTRUE )\r
154                 {\r
155                         /* If sTHREEmpty is true then the UART Tx ISR has indicated that \r
156                         there are no characters queued to be transmitted - so we can\r
157                         write the character directly to the shift Tx register. */\r
158                         sTHREEmpty = pdFALSE;\r
159                         TDR0 = cOutChar;\r
160                         xReturn = pdPASS;\r
161                 }\r
162                 else\r
163                 {\r
164                         /* sTHREEmpty is false, so there are still characters waiting to be\r
165                         transmitted.  We have to queue this character so it gets \r
166                         transmitted     in turn. */\r
167 \r
168                         /* Return false if after the block time there is no room on the Tx \r
169                         queue.  It is ok to block inside a critical section as each task\r
170                         maintains it's own critical section status. */\r
171                         if( xQueueSend(xCharsForTx, &cOutChar, xBlockTime) == pdTRUE )\r
172                         {\r
173                                 xReturn = pdPASS;\r
174                         }\r
175                         else\r
176                         {\r
177                                 xReturn = pdFAIL;\r
178                         }\r
179                 }\r
180 \r
181                 if( pdPASS == xReturn )\r
182                 {\r
183                         /* Turn on the Tx interrupt so the ISR will remove the character from the\r
184                         queue and send it.   This does not need to be in a critical section as\r
185                         if the interrupt has already removed the character the next interrupt\r
186                         will simply turn off the Tx interrupt again. */\r
187                         SSR0_TIE = 1;\r
188                 }\r
189         }\r
190         portEXIT_CRITICAL();\r
191 \r
192         return pdPASS;\r
193 }\r
194 /*-----------------------------------------------------------*/\r
195 \r
196 /*\r
197  * UART RX interrupt service routine.\r
198  */\r
199 __interrupt void UART0_RxISR( void )\r
200 {\r
201 volatile signed portCHAR        cChar;\r
202 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
203 \r
204         /* Get the character from the UART and post it on the queue of Rxed \r
205         characters. */\r
206         cChar = RDR0;\r
207 \r
208         xQueueSendFromISR( xRxedChars, ( const void *const ) &cChar, &xHigherPriorityTaskWoken );\r
209 \r
210         if( xHigherPriorityTaskWoken )\r
211         {\r
212                 /*If the post causes a task to wake force a context switch \r
213                 as the woken task may have a higher priority than the task we have \r
214                 interrupted. */\r
215                 portYIELD_FROM_ISR();\r
216         }\r
217 }\r
218 /*-----------------------------------------------------------*/\r
219 \r
220 /*\r
221  * UART Tx interrupt service routine.\r
222  */\r
223 __interrupt void UART0_TxISR( void )\r
224 {\r
225 signed portCHAR                 cChar;\r
226 signed portBASE_TYPE    xTaskWoken = pdFALSE;\r
227 \r
228         /* The previous character has been transmitted.  See if there are any\r
229         further characters waiting transmission. */\r
230         if( xQueueReceiveFromISR(xCharsForTx, &cChar, &xTaskWoken) == pdTRUE )\r
231         {\r
232                 /* There was another character queued - transmit it now. */\r
233                 TDR0 = cChar;\r
234         }\r
235         else\r
236         {\r
237                 /* There were no other characters to transmit. */\r
238                 sTHREEmpty = pdTRUE;\r
239 \r
240                 /* Disable transmit interrupts */\r
241                 SSR0_TIE = 0;\r
242         }\r
243 }\r