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