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