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