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