]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A9_RZ_R7S72100_IAR_DS-5/Source/Full-Demo/serial.c
258287ea35ccd91a1179045dc1f67f0af82e830c
[freertos] / FreeRTOS / Demo / CORTEX_A9_RZ_R7S72100_IAR_DS-5 / Source / Full-Demo / serial.c
1 /*\r
2     FreeRTOS V8.1.0 - 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     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 /*\r
67         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART2.\r
68 \r
69         ***Note*** This example uses queues to send each character into an interrupt\r
70         service routine and out of an interrupt service routine individually.  This\r
71         is done to demonstrate queues being used in an interrupt, and to deliberately\r
72         load the system to test the FreeRTOS port.  It is *NOT* meant to be an\r
73         example of an efficient implementation.  An efficient implementation should\r
74         use the DMA, and only use FreeRTOS API functions when enough has been\r
75         received to warrant a task being unblocked to process the data.\r
76 */\r
77 \r
78 /* Scheduler includes. */\r
79 #include "FreeRTOS.h"\r
80 #include "queue.h"\r
81 #include "semphr.h"\r
82 #include "comtest2.h"\r
83 \r
84 /* Driver includes. */\r
85 #include "r_typedefs.h"\r
86 #include "dev_drv.h"\r
87 #include "devdrv_scif_uart.h"\r
88 #include "sio_char.h"\r
89 #include "iodefine.h"\r
90 #include "devdrv_intc.h"\r
91 \r
92 /* Demo application includes. */\r
93 #include "serial.h"\r
94 \r
95 /*-----------------------------------------------------------*/\r
96 \r
97 /* Misc defines. */\r
98 #define serINVALID_QUEUE                                ( ( QueueHandle_t ) 0 )\r
99 #define serNO_BLOCK                                             ( ( TickType_t ) 0 )\r
100 \r
101 /*-----------------------------------------------------------*/\r
102 \r
103 /* Handlers for the Rx and Tx interrupts respectively. */\r
104 static void prvRXI_Handler( uint32_t ulUnusedParameter );\r
105 static void prvTXI_Handler( uint32_t ulUnusedParameter );\r
106 \r
107 /*-----------------------------------------------------------*/\r
108 \r
109 /* The queue used to hold received characters. */\r
110 static QueueHandle_t xRxedChars;\r
111 static QueueHandle_t xCharsForTx;\r
112 \r
113 /*-----------------------------------------------------------*/\r
114 \r
115 /*\r
116  * See the serial2.h header file.\r
117  */\r
118 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
119 {\r
120         /* Baud is set in IoInitScif2(), called in prvSetupHardware() in main.c. */\r
121         ( void ) ulWantedBaud;\r
122 \r
123         /* Create the queues used to hold Rx/Tx characters.  Note the comments at\r
124         the top of this file regarding the use of queues in this manner. */\r
125         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( char ) );\r
126         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( char ) );\r
127 \r
128         /* If the queues were created correctly then setup the serial port\r
129         hardware. */\r
130         if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )\r
131         {\r
132             /* Register RXI and TXI handlers. */\r
133             R_INTC_RegistIntFunc( INTC_ID_RXI2, prvRXI_Handler );\r
134             R_INTC_RegistIntFunc( INTC_ID_TXI2, prvTXI_Handler );\r
135 \r
136             /* Set both interrupts such that they can interrupt the tick.  Also\r
137             set the Rx interrupt above the Tx interrupt in the hope that (for test\r
138             purposes) the Tx interrupt will interrupt the Rx interrupt. */\r
139             R_INTC_SetPriority( INTC_ID_RXI2, configMAX_API_CALL_INTERRUPT_PRIORITY );\r
140             R_INTC_SetPriority( INTC_ID_TXI2, ( configMAX_API_CALL_INTERRUPT_PRIORITY + 1 ) );\r
141 \r
142             /* This driver is intended to test interrupt interactions, and not\r
143             intended to be efficient.  Therefore set the RX trigger level to 1. */\r
144             SCIF2.SCFCR.BIT.RTRG = 0;\r
145             SCIF2.SCFCR.BIT.TTRG = 3;\r
146 \r
147                 /* Enable Rx interrupt.  Tx interrupt will be enabled when a Tx is\r
148                 performed. */\r
149                 SCIF2.SCSCR.BIT.RIE = 1;\r
150                 R_INTC_Enable( INTC_ID_RXI2 );\r
151                 R_INTC_Enable( INTC_ID_TXI2 );\r
152         }\r
153 \r
154         /* This demo file only supports a single port but we have to return\r
155         something to comply with the standard demo header file. */\r
156         return ( xComPortHandle ) 0;\r
157 }\r
158 /*-----------------------------------------------------------*/\r
159 \r
160 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
161 {\r
162         /* The port handle is not required as this driver only supports one port. */\r
163         ( void ) pxPort;\r
164 \r
165         /* Get the next character from the buffer.  Return false if no characters\r
166         are available, or arrive before xBlockTime expires. */\r
167         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
168         {\r
169                 return pdTRUE;\r
170         }\r
171         else\r
172         {\r
173                 return pdFALSE;\r
174         }\r
175 }\r
176 /*-----------------------------------------------------------*/\r
177 \r
178 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
179 {\r
180 char *pxNext;\r
181 \r
182         /* A couple of parameters that this port does not use. */\r
183         ( void ) usStringLength;\r
184         ( void ) pxPort;\r
185 \r
186         /* Send each character in the string, one at a time. */\r
187         pxNext = ( char * ) pcString;\r
188         while( *pxNext )\r
189         {\r
190                 xSerialPutChar( pxPort, *pxNext, portMAX_DELAY );\r
191                 pxNext++;\r
192         }\r
193 }\r
194 /*-----------------------------------------------------------*/\r
195 \r
196 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
197 {\r
198 signed portBASE_TYPE xReturn;\r
199 \r
200         /* Note the comments at the top of this file regarding the use of queues in\r
201         this manner. */\r
202         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdPASS )\r
203         {\r
204                 xReturn = pdPASS;\r
205 \r
206                 /* Enable the interrupt which will remove the character from the\r
207                 queue. */\r
208                 SCIF2.SCSCR.BIT.TIE = 1;\r
209         }\r
210         else\r
211         {\r
212                 xReturn = pdFAIL;\r
213         }\r
214 \r
215         return xReturn;\r
216 }\r
217 /*-----------------------------------------------------------*/\r
218 \r
219 void vSerialClose( xComPortHandle xPort )\r
220 {\r
221         /* Not supported as not required by the demo application. */\r
222 }\r
223 /*-----------------------------------------------------------*/\r
224 \r
225 static void prvRXI_Handler( uint32_t ulUnusedParameter )\r
226 {\r
227 unsigned char ucRxedByte;\r
228 long lHigherPriorityTaskWoken = pdFALSE;\r
229 \r
230         /* The parameter is not used.  It is only present because Renesas drivers\r
231         are used to install the interrupt handlers, and the drivers expect the\r
232         parameter to be present. */\r
233         ( void ) ulUnusedParameter;\r
234 \r
235         /* Note the comments at the top of this file regarding the use of queues in\r
236         this manner. */\r
237         while( ( SCIF2.SCFDR.WORD & 0x1F ) != 0 )\r
238         {\r
239                 ucRxedByte = SCIF2.SCFRDR.BYTE;\r
240                 xQueueSendFromISR( xRxedChars, &ucRxedByte, &lHigherPriorityTaskWoken );\r
241         }\r
242 \r
243         SCIF2.SCFSR.BIT.RDF = 0;\r
244 \r
245         /* If sending to the queue has caused a task to unblock, and the unblocked\r
246         task has a priority equal to or higher than the currently running task (the\r
247         task this ISR interrupted), then lHigherPriorityTaskWoken will have\r
248         automatically been set to pdTRUE within the queue send function.\r
249         portYIELD_FROM_ISR() will then ensure that this ISR returns     directly to the\r
250         higher priority unblocked task. */\r
251         portYIELD_FROM_ISR( lHigherPriorityTaskWoken );\r
252 }\r
253 /*-----------------------------------------------------------*/\r
254 \r
255 static void prvTXI_Handler( uint32_t ulUnusedParameter )\r
256 {\r
257 unsigned char ucByte;\r
258 long lHigherPriorityTaskWoken = pdFALSE;\r
259 \r
260         /* The parameter is not used.  It is only present because Renesas drivers\r
261         are used to install the interrupt handlers, and the drivers expect the\r
262         parameter to be present. */\r
263         ( void ) ulUnusedParameter;\r
264 \r
265         /* Note the comments at the top of this file regarding the use of queues in\r
266         this manner. */\r
267         if( xQueueReceiveFromISR( xCharsForTx, &ucByte, &lHigherPriorityTaskWoken ) == pdPASS )\r
268         {\r
269                 SCIF2.SCFTDR.BYTE = ucByte;\r
270 \r
271                 /* Clear TDRE and TEND flag */\r
272             SCIF2.SCFSR.WORD &= ~0x0060;\r
273         }\r
274         else\r
275         {\r
276                 /* No more characters.  Disable the interrupt. */\r
277                 SCIF2.SCSCR.BIT.TIE = 0;\r
278         }\r
279 \r
280         /* If receiving from the queue has caused a task to unblock, and the\r
281         unblocked task has a priority equal to or higher than the currently running\r
282         task (the task this ISR interrupted), then lHigherPriorityTaskWoken will\r
283         have automatically been set to pdTRUE within the queue receive function.\r
284         portYIELD_FROM_ISR() will then ensure that this ISR returns     directly to the\r
285         higher priority unblocked task. */\r
286         portYIELD_FROM_ISR( lHigherPriorityTaskWoken );\r
287 }\r
288 /*-----------------------------------------------------------*/\r
289 \r
290 \r
291 \r
292 \r