]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/NEC_V850ES_IAR/serial/serial.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / NEC_V850ES_IAR / serial / serial.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /*\r
30         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.\r
31 \r
32         *NOTE* - This file is designed to test some of the RTOS features - it is\r
33         not intended to represent an efficient implementation!\r
34 */\r
35 \r
36 /* Standard includes. */\r
37 #include <stdlib.h>\r
38 \r
39 /* Scheduler includes. */\r
40 #include "FreeRTOS.h"\r
41 #include "queue.h"\r
42 \r
43 /* Demo application includes. */\r
44 #include "serial.h"\r
45 \r
46 \r
47 /* Hardware specifics. */\r
48 #define serRX_DATA_PIN          ( 0x01 )\r
49 #define serTX_DATA_PIN          ( 0x02 )\r
50 #define serCLOCK_Fxx_DIV_8              0x03\r
51 #define serUPWR         ( 0x80 )\r
52 #define serUTXE         ( 0x40 )\r
53 #define serURXE         ( 0x20 )\r
54 #define serUCL          ( 0x02 )\r
55 #define serLSB          ( 0x10 )\r
56 \r
57 /* Misc. */\r
58 #define serINVALID_QUEUE                                ( ( QueueHandle_t ) 0 )\r
59 #define serHANDLE                                               ( ( xComPortHandle ) 1 )\r
60 #define serNO_BLOCK                                             ( ( TickType_t ) 0 )\r
61 \r
62 /*-----------------------------------------------------------*/\r
63 \r
64 /* Queues used to hold received characters, and characters waiting to be\r
65 transmitted. */\r
66 static QueueHandle_t xRxedChars;\r
67 static QueueHandle_t xCharsForTx;\r
68 \r
69 /*-----------------------------------------------------------*/\r
70 \r
71 /* Interrupt entry point written in the assembler file serialISR.s85. */\r
72 extern void vSerialISREntry( void );\r
73 \r
74 /* Flag to indicate whether or not there are characters already queued to send. */\r
75 static volatile unsigned long ulTxInProgress = pdFALSE;\r
76 \r
77 /*-----------------------------------------------------------*/\r
78 \r
79 /*\r
80  * See the serial2.h header file.\r
81  */\r
82 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
83 {\r
84 xComPortHandle xReturn = serHANDLE;\r
85 const unsigned long ulFuclk = ( configCPU_CLOCK_HZ / 2 ) / 8UL;\r
86 \r
87         /* Create the queues used to hold Rx and Tx characters. */\r
88         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
89         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
90 \r
91         /* If the queues were created correctly then setup the serial port\r
92         hardware. */\r
93         if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )\r
94         {\r
95                 portENTER_CRITICAL();\r
96                 {\r
97                         /* Set the UART0 Rx and Tx pins to their alternative function. */\r
98                         PMC3 |= ( serRX_DATA_PIN | serTX_DATA_PIN );\r
99                         PM3 &= ~( serTX_DATA_PIN );\r
100 \r
101                         /* Setup clock for required baud. */                    \r
102                         UD0CTL1 = serCLOCK_Fxx_DIV_8;\r
103                         UD0CTL2 = ulFuclk / ( 2 * ulWantedBaud );\r
104 \r
105                         /* Enable, n81. */                      \r
106                         UD0CTL0 = ( serUPWR | serUTXE | serURXE | serUCL | serLSB );\r
107                         \r
108                         /* Enable interrupts for both Rx and Tx. */\r
109                         UD0TIC  = 0x07;\r
110                         UD0RIC  = 0x07;\r
111                         \r
112                         ulTxInProgress = pdFALSE;\r
113                 }\r
114                 portEXIT_CRITICAL();\r
115         }\r
116         else\r
117         {\r
118                 xReturn = ( xComPortHandle ) 0;\r
119         }\r
120 \r
121         /* This demo file only supports a single port but we have to return\r
122         something to comply with the standard demo header file. */\r
123         return xReturn;\r
124 }\r
125 /*-----------------------------------------------------------*/\r
126 \r
127 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
128 {\r
129         /* The port handle is not required as this driver only supports one port. */\r
130         ( void ) pxPort;\r
131 \r
132         /* Get the next character from the buffer.  Return false if no characters\r
133         are available, or arrive before xBlockTime expires. */\r
134         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
135         {\r
136                 return pdTRUE;\r
137         }\r
138         else\r
139         {\r
140                 return pdFALSE;\r
141         }\r
142 }\r
143 /*-----------------------------------------------------------*/\r
144 \r
145 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
146 {\r
147 signed char *pxNext;\r
148 \r
149         /* A couple of parameters that this port does not use. */\r
150         ( void ) usStringLength;\r
151         ( void ) pxPort;\r
152 \r
153         /* NOTE: This implementation does not handle the queue being full as no\r
154         block time is used! */\r
155 \r
156         /* The port handle is not required as this driver only supports UART0. */\r
157         ( void ) pxPort;\r
158 \r
159         /* Send each character in the string, one at a time. */\r
160         pxNext = ( signed char * ) pcString;\r
161         while( *pxNext )\r
162         {\r
163                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
164                 pxNext++;\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 portBASE_TYPE xReturn = pdPASS;\r
172 \r
173         portENTER_CRITICAL();\r
174         {\r
175                 /* There are currently no characters queued up to send so write the\r
176                 character directly to the UART. */\r
177                 if( ulTxInProgress == pdFALSE )\r
178                 {\r
179                         UD0TX = cOutChar;\r
180                         ulTxInProgress = pdTRUE;\r
181                 }\r
182                 else\r
183                 {\r
184                         /* The UART is already busy so write the character to the Tx queue.\r
185                         The queue is drained from within the Tx interrupt. */\r
186                         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
187                         {\r
188                                 xReturn = pdFAIL;\r
189                         }\r
190                 }\r
191         }\r
192         portEXIT_CRITICAL();\r
193         \r
194         return xReturn;\r
195 }\r
196 /*-----------------------------------------------------------*/\r
197 \r
198 void vSerialClose( xComPortHandle xPort )\r
199 {\r
200         /* Not supported as not required by the demo application. */\r
201 }\r
202 /*-----------------------------------------------------------*/\r
203 \r
204 /* Tx interrupt handler.  This is called from the asm file wrapper. */\r
205 void vUARTTxISRHandler( void )\r
206 {\r
207 char cChar;\r
208 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
209 \r
210         /* Are there any more characters queue to transmit? */\r
211         if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
212         {\r
213                 /* Send the next character. */\r
214                 UD0TX = cChar;\r
215         }\r
216         else\r
217         {\r
218                 /* The UART is no longer active. */\r
219                 ulTxInProgress = pdFALSE;\r
220         }\r
221         \r
222         /* If reading a character from the Rx queue caused a task to unblock, and\r
223         the unblocked task has a priority higher than the currently running task,\r
224         then xHigherPriorityTaskWoken will have been set to true and a context\r
225         switch should occur now. */\r
226         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
227 }\r
228 /*-----------------------------------------------------------*/\r
229 \r
230 /* Rx interrupt handler.  This is called from the asm file wrapper. */\r
231 void vUARTRxISRHandler( void )\r
232 {\r
233 char cChar;\r
234 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
235 \r
236         /* Send the received character to the Rx queue. */\r
237         cChar = UD0RX;\r
238         xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
239         \r
240         /* If sending a character to the Tx queue caused a task to unblock, and\r
241         the unblocked task has a priority higher than the currently running task,\r
242         then xHigherPriorityTaskWoken will have been set to true and a context\r
243         switch should occur now. */\r
244         portYIELD_FROM_ISR( xHigherPriorityTaskWoken ); \r
245 }\r
246 \r
247 \r
248 \r
249 \r
250         \r