]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/MB96340_Softune/FreeRTOS_96348hs_SK16FX100PMC/Src/serial/serial.c
Update version number to 8.1.2 after moving the defaulting of configUSE_PORT_OPTIMISE...
[freertos] / FreeRTOS / Demo / MB96340_Softune / FreeRTOS_96348hs_SK16FX100PMC / Src / serial / serial.c
1 /*\r
2     FreeRTOS V8.1.2 - Copyright (C) 2014 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     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.   \r
67  * \r
68  * This file only supports UART 0\r
69  */\r
70 \r
71 /* Standard includes. */\r
72 #include <stdlib.h>\r
73 \r
74 /* Scheduler includes. */\r
75 #include "FreeRTOS.h"\r
76 #include "queue.h"\r
77 #include "task.h"\r
78 \r
79 /* Demo application includes. */\r
80 #include "serial.h"\r
81 \r
82 /* The queue used to hold received characters. */\r
83 static QueueHandle_t                    xRxedChars;\r
84 \r
85 /* The queue used to hold characters waiting transmission. */\r
86 static QueueHandle_t                    xCharsForTx;\r
87 \r
88 static volatile short   sTHREEmpty;\r
89 \r
90 static volatile short   queueFail = pdFALSE;\r
91 \r
92 /*-----------------------------------------------------------*/\r
93 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
94 {\r
95         /* Initialise the hardware. */\r
96         portENTER_CRITICAL();\r
97         {\r
98                 /* Create the queues used by the com test task. */\r
99                 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof(signed char) );\r
100                 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof(signed char) );\r
101 \r
102                 if( xRxedChars == 0 )\r
103                 {\r
104                         queueFail = pdTRUE;\r
105                 }\r
106 \r
107                 if( xCharsForTx == 0 )\r
108                 {\r
109                         queueFail = pdTRUE;\r
110                 }\r
111 \r
112                 /* Initialize UART asynchronous mode */\r
113                 BGR0 = configCLKP1_CLOCK_HZ / ulWantedBaud;\r
114 \r
115                 SCR0 = 0x17;    /* 8N1 */\r
116                 SMR0 = 0x0d;    /* enable SOT3, Reset, normal mode */\r
117                 SSR0 = 0x02;    /* LSB first, enable receive interrupts */\r
118 \r
119                 PIER08_IE2 = 1; /* enable input */\r
120                 DDR08_D2 = 0;   /* switch P08_2 to input */\r
121                 DDR08_D3 = 1;   /* switch P08_3 to output */\r
122         }\r
123         portEXIT_CRITICAL();\r
124 \r
125         /* Unlike other ports, this serial code does not allow for more than one\r
126         com port.  We therefore don't return a pointer to a port structure and can\r
127         instead just return NULL. */\r
128         return NULL;\r
129 }\r
130 /*-----------------------------------------------------------*/\r
131 \r
132 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
133 {\r
134         /* Get the next character from the buffer.  Return false if no characters\r
135         are available, or arrive before xBlockTime expires. */\r
136         if( xQueueReceive(xRxedChars, pcRxedChar, xBlockTime) )\r
137         {\r
138                 return pdTRUE;\r
139         }\r
140         else\r
141         {\r
142                 return pdFALSE;\r
143         }\r
144 }\r
145 /*-----------------------------------------------------------*/\r
146 \r
147 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
148 {\r
149 signed portBASE_TYPE    xReturn;\r
150 \r
151         /* Transmit a character. */\r
152         portENTER_CRITICAL();\r
153         {\r
154                 if( sTHREEmpty == pdTRUE )\r
155                 {\r
156                         /* If sTHREEmpty is true then the UART Tx ISR has indicated that \r
157                         there are no characters queued to be transmitted - so we can\r
158                         write the character directly to the shift Tx register. */\r
159                         sTHREEmpty = pdFALSE;\r
160                         TDR0 = cOutChar;\r
161                         xReturn = pdPASS;\r
162                 }\r
163                 else\r
164                 {\r
165                         /* sTHREEmpty is false, so there are still characters waiting to be\r
166                         transmitted.  We have to queue this character so it gets \r
167                         transmitted     in turn. */\r
168 \r
169                         /* Return false if after the block time there is no room on the Tx \r
170                         queue.  It is ok to block inside a critical section as each task\r
171                         maintains it's own critical section status. */\r
172                         if( xQueueSend(xCharsForTx, &cOutChar, xBlockTime) == pdTRUE )\r
173                         {\r
174                                 xReturn = pdPASS;\r
175                         }\r
176                         else\r
177                         {\r
178                                 xReturn = pdFAIL;\r
179                         }\r
180                 }\r
181 \r
182                 if( pdPASS == xReturn )\r
183                 {\r
184                         /* Turn on the Tx interrupt so the ISR will remove the character from the\r
185                         queue and send it.   This does not need to be in a critical section as\r
186                         if the interrupt has already removed the character the next interrupt\r
187                         will simply turn off the Tx interrupt again. */\r
188                         SSR0_TIE = 1;\r
189                 }\r
190         }\r
191         portEXIT_CRITICAL();\r
192 \r
193         return pdPASS;\r
194 }\r
195 /*-----------------------------------------------------------*/\r
196 \r
197 /*\r
198  * UART RX interrupt service routine.\r
199  */\r
200 __interrupt void UART0_RxISR( void )\r
201 {\r
202 volatile signed char    cChar;\r
203 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
204 \r
205         /* Get the character from the UART and post it on the queue of Rxed \r
206         characters. */\r
207         cChar = RDR0;\r
208 \r
209         xQueueSendFromISR( xRxedChars, ( const void *const ) &cChar, &xHigherPriorityTaskWoken );\r
210 \r
211         if( xHigherPriorityTaskWoken )\r
212         {\r
213                 /*If the post causes a task to wake force a context switch \r
214                 as the woken task may have a higher priority than the task we have \r
215                 interrupted. */\r
216                 portYIELD_FROM_ISR();\r
217         }\r
218 }\r
219 /*-----------------------------------------------------------*/\r
220 \r
221 /*\r
222  * UART Tx interrupt service routine.\r
223  */\r
224 __interrupt void UART0_TxISR( void )\r
225 {\r
226 signed char                     cChar;\r
227 signed portBASE_TYPE    xTaskWoken = pdFALSE;\r
228 \r
229         /* The previous character has been transmitted.  See if there are any\r
230         further characters waiting transmission. */\r
231         if( xQueueReceiveFromISR(xCharsForTx, &cChar, &xTaskWoken) == pdTRUE )\r
232         {\r
233                 /* There was another character queued - transmit it now. */\r
234                 TDR0 = cChar;\r
235         }\r
236         else\r
237         {\r
238                 /* There were no other characters to transmit. */\r
239                 sTHREEmpty = pdTRUE;\r
240 \r
241                 /* Disable transmit interrupts */\r
242                 SSR0_TIE = 0;\r
243         }\r
244 }\r