]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A9_RZ_R7S72100_IAR_DS-5/Source/Full-Demo/serial.c
Update version number ready for the V8.2.3 release.
[freertos] / FreeRTOS / Demo / CORTEX_A9_RZ_R7S72100_IAR_DS-5 / Source / Full-Demo / serial.c
1 /*\r
2     FreeRTOS V8.2.3 - 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 /*\r
71         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART2.\r
72 \r
73         ***Note*** This example uses queues to send each character into an interrupt\r
74         service routine and out of an interrupt service routine individually.  This\r
75         is done to demonstrate queues being used in an interrupt, and to deliberately\r
76         load the system to test the FreeRTOS port.  It is *NOT* meant to be an\r
77         example of an efficient implementation.  An efficient implementation should\r
78         use the DMA, and only use FreeRTOS API functions when enough has been\r
79         received to warrant a task being unblocked to process the data.\r
80 */\r
81 \r
82 /* Scheduler includes. */\r
83 #include "FreeRTOS.h"\r
84 #include "queue.h"\r
85 #include "semphr.h"\r
86 #include "comtest2.h"\r
87 \r
88 /* Driver includes. */\r
89 #include "r_typedefs.h"\r
90 #include "dev_drv.h"\r
91 #include "devdrv_scif_uart.h"\r
92 #include "sio_char.h"\r
93 #include "iodefine.h"\r
94 #include "devdrv_intc.h"\r
95 \r
96 /* Demo application includes. */\r
97 #include "serial.h"\r
98 \r
99 /*-----------------------------------------------------------*/\r
100 \r
101 /* Misc defines. */\r
102 #define serINVALID_QUEUE                                ( ( QueueHandle_t ) 0 )\r
103 #define serNO_BLOCK                                             ( ( TickType_t ) 0 )\r
104 \r
105 /*-----------------------------------------------------------*/\r
106 \r
107 /* Handlers for the Rx and Tx interrupts respectively. */\r
108 static void prvRXI_Handler( uint32_t ulUnusedParameter );\r
109 static void prvTXI_Handler( uint32_t ulUnusedParameter );\r
110 \r
111 /*-----------------------------------------------------------*/\r
112 \r
113 /* The queue used to hold received characters. */\r
114 static QueueHandle_t xRxedChars;\r
115 static QueueHandle_t xCharsForTx;\r
116 \r
117 /*-----------------------------------------------------------*/\r
118 \r
119 /*\r
120  * See the serial2.h header file.\r
121  */\r
122 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
123 {\r
124         /* Baud is set in IoInitScif2(), called in prvSetupHardware() in main.c. */\r
125         ( void ) ulWantedBaud;\r
126 \r
127         /* Create the queues used to hold Rx/Tx characters.  Note the comments at\r
128         the top of this file regarding the use of queues in this manner. */\r
129         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );\r
130         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( char ) );\r
131 \r
132         /* If the queues were created correctly then setup the serial port\r
133         hardware. */\r
134         if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )\r
135         {\r
136             /* Register RXI and TXI handlers. */\r
137             R_INTC_RegistIntFunc( INTC_ID_RXI2, prvRXI_Handler );\r
138             R_INTC_RegistIntFunc( INTC_ID_TXI2, prvTXI_Handler );\r
139 \r
140             /* Set both interrupts such that they can interrupt the tick.  Also\r
141             set the Rx interrupt above the Tx interrupt in the hope that (for test\r
142             purposes) the Tx interrupt will interrupt the Rx interrupt. */\r
143             R_INTC_SetPriority( INTC_ID_RXI2, configMAX_API_CALL_INTERRUPT_PRIORITY );\r
144             R_INTC_SetPriority( INTC_ID_TXI2, ( configMAX_API_CALL_INTERRUPT_PRIORITY + 1 ) );\r
145 \r
146             /* This driver is intended to test interrupt interactions, and not\r
147             intended to be efficient.  Therefore set the RX trigger level to 1. */\r
148             SCIF2.SCFCR.BIT.RTRG = 0;\r
149             SCIF2.SCFCR.BIT.TTRG = 3;\r
150 \r
151                 /* Enable Rx interrupt.  Tx interrupt will be enabled when a Tx is\r
152                 performed. */\r
153                 SCIF2.SCSCR.BIT.RIE = 1;\r
154                 R_INTC_Enable( INTC_ID_RXI2 );\r
155                 R_INTC_Enable( INTC_ID_TXI2 );\r
156         }\r
157 \r
158         /* This demo file only supports a single port but we have to return\r
159         something to comply with the standard demo header file. */\r
160         return ( xComPortHandle ) 0;\r
161 }\r
162 /*-----------------------------------------------------------*/\r
163 \r
164 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
165 {\r
166         /* The port handle is not required as this driver only supports one port. */\r
167         ( void ) pxPort;\r
168 \r
169         /* Get the next character from the buffer.  Return false if no characters\r
170         are available, or arrive before xBlockTime expires. */\r
171         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
172         {\r
173                 return pdTRUE;\r
174         }\r
175         else\r
176         {\r
177                 return pdFALSE;\r
178         }\r
179 }\r
180 /*-----------------------------------------------------------*/\r
181 \r
182 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
183 {\r
184 char *pxNext;\r
185 \r
186         /* A couple of parameters that this port does not use. */\r
187         ( void ) usStringLength;\r
188         ( void ) pxPort;\r
189 \r
190         /* Send each character in the string, one at a time. */\r
191         pxNext = ( char * ) pcString;\r
192         while( *pxNext )\r
193         {\r
194                 xSerialPutChar( pxPort, *pxNext, portMAX_DELAY );\r
195                 pxNext++;\r
196         }\r
197 }\r
198 /*-----------------------------------------------------------*/\r
199 \r
200 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
201 {\r
202 signed portBASE_TYPE xReturn;\r
203 \r
204         /* Note the comments at the top of this file regarding the use of queues in\r
205         this manner. */\r
206         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdPASS )\r
207         {\r
208                 xReturn = pdPASS;\r
209 \r
210                 /* Enable the interrupt which will remove the character from the\r
211                 queue. */\r
212                 SCIF2.SCSCR.BIT.TIE = 1;\r
213         }\r
214         else\r
215         {\r
216                 xReturn = pdFAIL;\r
217         }\r
218 \r
219         return xReturn;\r
220 }\r
221 /*-----------------------------------------------------------*/\r
222 \r
223 void vSerialClose( xComPortHandle xPort )\r
224 {\r
225         /* Not supported as not required by the demo application. */\r
226 }\r
227 /*-----------------------------------------------------------*/\r
228 \r
229 static void prvRXI_Handler( uint32_t ulUnusedParameter )\r
230 {\r
231 unsigned char ucRxedByte;\r
232 long lHigherPriorityTaskWoken = pdFALSE;\r
233 \r
234         /* The parameter is not used.  It is only present because Renesas drivers\r
235         are used to install the interrupt handlers, and the drivers expect the\r
236         parameter to be present. */\r
237         ( void ) ulUnusedParameter;\r
238 \r
239         /* Note the comments at the top of this file regarding the use of queues in\r
240         this manner. */\r
241         while( ( SCIF2.SCFDR.WORD & 0x1F ) != 0 )\r
242         {\r
243                 ucRxedByte = SCIF2.SCFRDR.BYTE;\r
244                 xQueueSendFromISR( xRxedChars, &ucRxedByte, &lHigherPriorityTaskWoken );\r
245         }\r
246 \r
247         SCIF2.SCFSR.BIT.RDF = 0;\r
248 \r
249         /* If sending to the queue has caused a task to unblock, and the unblocked\r
250         task has a priority equal to or higher than the currently running task (the\r
251         task this ISR interrupted), then lHigherPriorityTaskWoken will have\r
252         automatically been set to pdTRUE within the queue send function.\r
253         portYIELD_FROM_ISR() will then ensure that this ISR returns     directly to the\r
254         higher priority unblocked task. */\r
255         portYIELD_FROM_ISR( lHigherPriorityTaskWoken );\r
256 }\r
257 /*-----------------------------------------------------------*/\r
258 \r
259 static void prvTXI_Handler( uint32_t ulUnusedParameter )\r
260 {\r
261 unsigned char ucByte;\r
262 long lHigherPriorityTaskWoken = pdFALSE;\r
263 \r
264         /* The parameter is not used.  It is only present because Renesas drivers\r
265         are used to install the interrupt handlers, and the drivers expect the\r
266         parameter to be present. */\r
267         ( void ) ulUnusedParameter;\r
268 \r
269         /* Note the comments at the top of this file regarding the use of queues in\r
270         this manner. */\r
271         if( xQueueReceiveFromISR( xCharsForTx, &ucByte, &lHigherPriorityTaskWoken ) == pdPASS )\r
272         {\r
273                 SCIF2.SCFTDR.BYTE = ucByte;\r
274 \r
275                 /* Clear TDRE and TEND flag */\r
276             SCIF2.SCFSR.WORD &= ~0x0060;\r
277         }\r
278         else\r
279         {\r
280                 /* No more characters.  Disable the interrupt. */\r
281                 SCIF2.SCSCR.BIT.TIE = 0;\r
282         }\r
283 \r
284         /* If receiving from the queue has caused a task to unblock, and the\r
285         unblocked task has a priority equal to or higher than the currently running\r
286         task (the task this ISR interrupted), then lHigherPriorityTaskWoken will\r
287         have automatically been set to pdTRUE within the queue receive function.\r
288         portYIELD_FROM_ISR() will then ensure that this ISR returns     directly to the\r
289         higher priority unblocked task. */\r
290         portYIELD_FROM_ISR( lHigherPriorityTaskWoken );\r
291 }\r
292 /*-----------------------------------------------------------*/\r
293 \r
294 \r
295 \r
296 \r