]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RX100_RX113-RSK_Renesas_e2studio/src/cg_src/r_cg_sci_user.c
commit 9f316c246baafa15c542a5aea81a94f26e3d6507
[freertos] / FreeRTOS / Demo / RX100_RX113-RSK_Renesas_e2studio / src / cg_src / r_cg_sci_user.c
1 /***********************************************************************************************************************\r
2 * DISCLAIMER\r
3 * This software is supplied by Renesas Electronics Corporation and is only intended for use with Renesas products.\r
4 * No other uses are authorized. This software is owned by Renesas Electronics Corporation and is protected under all\r
5 * applicable laws, including copyright laws.\r
6 * THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIESREGARDING THIS SOFTWARE, WHETHER EXPRESS, IMPLIED\r
7 * OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
8 * NON-INFRINGEMENT.  ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY\r
9 * LAW, NEITHER RENESAS ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE FOR ANY DIRECT,\r
10 * INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR\r
11 * ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.\r
12 * Renesas reserves the right, without notice, to make changes to this software and to discontinue the availability\r
13 * of this software. By using this software, you agree to the additional terms and conditions found by accessing the\r
14 * following link:\r
15 * http://www.renesas.com/disclaimer\r
16 *\r
17 * Copyright (C) 2015 Renesas Electronics Corporation. All rights reserved.\r
18 ***********************************************************************************************************************/\r
19 \r
20 /***********************************************************************************************************************\r
21 * File Name    : r_cg_sci_user.c\r
22 * Version      : Code Generator for RX113 V1.02.01.02 [28 May 2015]\r
23 * Device(s)    : R5F51138AxFP\r
24 * Tool-Chain   : CCRX\r
25 * Description  : This file implements device driver for SCI module.\r
26 * Creation Date: 21/09/2015\r
27 ***********************************************************************************************************************/\r
28 \r
29 /***********************************************************************************************************************\r
30 Pragma directive\r
31 ***********************************************************************************************************************/\r
32 /* Start user code for pragma. Do not edit comment generated here */\r
33 \r
34 \r
35 /*\r
36  * This file originated from an example project for the RSK - it has been\r
37  * adapted to allow it to be used in the FreeRTOS demo.  Functions required by\r
38  * UARTCommandConsole.c have been added.\r
39  */\r
40 \r
41 \r
42 \r
43 /* End user code. Do not edit comment generated here */\r
44 \r
45 /***********************************************************************************************************************\r
46 Includes\r
47 ***********************************************************************************************************************/\r
48 #include "r_cg_macrodriver.h"\r
49 #include "r_cg_sci.h"\r
50 /* Start user code for include. Do not edit comment generated here */\r
51 #include "rskrx113def.h"\r
52 #include "FreeRTOS.h"\r
53 #include "task.h"\r
54 #include "queue.h"\r
55 #include "serial.h"\r
56 /* End user code. Do not edit comment generated here */\r
57 #include "r_cg_userdefine.h"\r
58 \r
59 /***********************************************************************************************************************\r
60 Global variables and functions\r
61 ***********************************************************************************************************************/\r
62 extern uint8_t * gp_sci1_tx_address;         /* SCI1 send buffer address */\r
63 extern uint16_t  g_sci1_tx_count;            /* SCI1 send data number */\r
64 extern uint8_t * gp_sci1_rx_address;         /* SCI1 receive buffer address */\r
65 extern uint16_t  g_sci1_rx_count;            /* SCI1 receive data number */\r
66 extern uint16_t  g_sci1_rx_length;           /* SCI1 receive data length */\r
67 /* Start user code for global. Do not edit comment generated here */\r
68 \r
69 /* Global used to receive a character from the PC terminal */\r
70 uint8_t g_rx_char;\r
71 \r
72 /* Flag used to control transmission to PC terminal */\r
73 volatile uint8_t g_tx_flag = FALSE;\r
74 \r
75 /* Characters received from the UART are stored in this queue, ready to be\r
76 received by the application.  ***NOTE*** Using a queue in this way is very\r
77 convenient, but also very inefficient.  It can be used here because characters\r
78 will only arrive slowly.  In a higher bandwidth system a circular RAM buffer or\r
79 DMA should be used in place of this queue. */\r
80 static QueueHandle_t xRxQueue = NULL;\r
81 \r
82 /* When a task calls vSerialPutString() its handle is stored in xSendingTask,\r
83 before being placed into the Blocked state (so does not use any CPU time) to\r
84 wait for the transmission to end.  The task handle is then used from the UART\r
85 transmit end interrupt to remove the task from the Blocked state. */\r
86 static TaskHandle_t xSendingTask = NULL;\r
87 \r
88 /* Flag used locally to detect transmission complete.  This is used by the\r
89 auto generated API only. */\r
90 static volatile uint8_t sci1_txdone;\r
91 \r
92 /* End user code. Do not edit comment generated here */\r
93 \r
94 /***********************************************************************************************************************\r
95 * Function Name: r_sci1_transmit_interrupt\r
96 * Description  : None\r
97 * Arguments    : None\r
98 * Return Value : None\r
99 ***********************************************************************************************************************/\r
100 #if FAST_INTERRUPT_VECTOR == VECT_SCI1_TXI1\r
101 #pragma interrupt r_sci1_transmit_interrupt(vect=VECT(SCI1,TXI1),fint)\r
102 #else\r
103 #pragma interrupt r_sci1_transmit_interrupt(vect=VECT(SCI1,TXI1))\r
104 #endif\r
105 static void r_sci1_transmit_interrupt(void)\r
106 {\r
107     if (g_sci1_tx_count > 0U)\r
108     {\r
109         SCI1.TDR = *gp_sci1_tx_address;\r
110         gp_sci1_tx_address++;\r
111         g_sci1_tx_count--;\r
112     }\r
113     else\r
114     {\r
115         SCI1.SCR.BIT.TIE = 0U;\r
116         SCI1.SCR.BIT.TEIE = 1U;\r
117     }\r
118 }\r
119 /***********************************************************************************************************************\r
120 * Function Name: r_sci1_transmitend_interrupt\r
121 * Description  : None\r
122 * Arguments    : None\r
123 * Return Value : None\r
124 ***********************************************************************************************************************/\r
125 #if FAST_INTERRUPT_VECTOR == VECT_SCI1_TEI1\r
126 #pragma interrupt r_sci1_transmitend_interrupt(vect=VECT(SCI1,TEI1),fint)\r
127 #else\r
128 #pragma interrupt r_sci1_transmitend_interrupt(vect=VECT(SCI1,TEI1))\r
129 #endif\r
130 static void r_sci1_transmitend_interrupt(void)\r
131 {\r
132     /* Set TXD1 pin */\r
133     PORT1.PMR.BYTE &= 0xBFU;\r
134     SCI1.SCR.BIT.TIE = 0U;\r
135     SCI1.SCR.BIT.TE = 0U;\r
136     SCI1.SCR.BIT.TEIE = 0U;\r
137 \r
138     r_sci1_callback_transmitend();\r
139 }\r
140 /***********************************************************************************************************************\r
141 * Function Name: r_sci1_receive_interrupt\r
142 * Description  : None\r
143 * Arguments    : None\r
144 * Return Value : None\r
145 ***********************************************************************************************************************/\r
146 #if FAST_INTERRUPT_VECTOR == VECT_SCI1_RXI1\r
147 #pragma interrupt r_sci1_receive_interrupt(vect=VECT(SCI1,RXI1),fint)\r
148 #else\r
149 #pragma interrupt r_sci1_receive_interrupt(vect=VECT(SCI1,RXI1))\r
150 #endif\r
151 static void r_sci1_receive_interrupt(void)\r
152 {\r
153     if (g_sci1_rx_length > g_sci1_rx_count)\r
154     {\r
155         *gp_sci1_rx_address = SCI1.RDR;\r
156         gp_sci1_rx_address++;\r
157         g_sci1_rx_count++;\r
158 \r
159         if (g_sci1_rx_length == g_sci1_rx_count)\r
160         {\r
161             r_sci1_callback_receiveend();\r
162         }\r
163     }\r
164 }\r
165 /***********************************************************************************************************************\r
166 * Function Name: r_sci1_receiveerror_interrupt\r
167 * Description  : None\r
168 * Arguments    : None\r
169 * Return Value : None\r
170 ***********************************************************************************************************************/\r
171 #if FAST_INTERRUPT_VECTOR == VECT_SCI1_ERI1\r
172 #pragma interrupt r_sci1_receiveerror_interrupt(vect=VECT(SCI1,ERI1),fint)\r
173 #else\r
174 #pragma interrupt r_sci1_receiveerror_interrupt(vect=VECT(SCI1,ERI1))\r
175 #endif\r
176 static void r_sci1_receiveerror_interrupt(void)\r
177 {\r
178     uint8_t err_type;\r
179 \r
180     r_sci1_callback_receiveerror();\r
181 \r
182     /* Clear overrun, framing and parity error flags */\r
183     err_type = SCI1.SSR.BYTE;\r
184     SCI1.SSR.BYTE = err_type & ( uint8_t ) 0xC7;\r
185 }\r
186 /***********************************************************************************************************************\r
187 * Function Name: r_sci1_callback_transmitend\r
188 * Description  : This function is a callback function when SCI1 finishes transmission.\r
189 * Arguments    : None\r
190 * Return Value : None\r
191 ***********************************************************************************************************************/\r
192 void r_sci1_callback_transmitend(void)\r
193 {\r
194     /* Start user code. Do not edit comment generated here */\r
195     BaseType_t xHigherPriorityTaskWoken = pdFALSE;\r
196 \r
197     /* The sci1_txdone flag is used by the auto generated API only. */\r
198     sci1_txdone = TRUE;\r
199 \r
200     if( xSendingTask != NULL )\r
201     {\r
202         /* A task is waiting for the end of the Tx, unblock it now.\r
203         http://www.freertos.org/vTaskNotifyGiveFromISR.html */\r
204         vTaskNotifyGiveFromISR( xSendingTask, &xHigherPriorityTaskWoken );\r
205         xSendingTask = NULL;\r
206 \r
207         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
208     }\r
209     /* End user code. Do not edit comment generated here */\r
210 }\r
211 /***********************************************************************************************************************\r
212 * Function Name: r_sci1_callback_receiveend\r
213 * Description  : This function is a callback function when SCI1 finishes reception.\r
214 * Arguments    : None\r
215 * Return Value : None\r
216 ***********************************************************************************************************************/\r
217 void r_sci1_callback_receiveend(void)\r
218 {\r
219     /* Start user code. Do not edit comment generated here */\r
220     BaseType_t xHigherPriorityTaskWoken = pdFALSE;\r
221 \r
222     configASSERT( xRxQueue );\r
223 \r
224     /* Transmitting generates an interrupt for each character, which consumes\r
225     CPU time, and can cause standard demo RTOS tasks that monitor their own\r
226     performance to fail asserts - so don't receive new CLI commands if a\r
227     transmit is not already in progress. */\r
228     if( sci1_txdone == TRUE )\r
229     {\r
230         /* Characters received from the UART are stored in this queue, ready to be\r
231         received by the application.  ***NOTE*** Using a queue in this way is very\r
232         convenient, but also very inefficient.  It can be used here because\r
233         characters will only arrive slowly.  In a higher bandwidth system a circular\r
234         RAM buffer or DMA should be used in place of this queue. */\r
235         xQueueSendFromISR( xRxQueue, &g_rx_char, &xHigherPriorityTaskWoken );\r
236     }\r
237 \r
238     /* Set up SCI1 receive buffer again */\r
239     R_SCI1_Serial_Receive((uint8_t *) &g_rx_char, 1);\r
240 \r
241     /* See http://www.freertos.org/xQueueOverwriteFromISR.html for information\r
242     on the semantics of this ISR. */\r
243     portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
244     /* End user code. Do not edit comment generated here */\r
245 }\r
246 /***********************************************************************************************************************\r
247 * Function Name: r_sci1_callback_receiveerror\r
248 * Description  : This function is a callback function when SCI1 reception encounters error.\r
249 * Arguments    : None\r
250 * Return Value : None\r
251 ***********************************************************************************************************************/\r
252 void r_sci1_callback_receiveerror(void)\r
253 {\r
254     /* Start user code. Do not edit comment generated here */\r
255     /* End user code. Do not edit comment generated here */\r
256 }\r
257 \r
258 /* Start user code for adding. Do not edit comment generated here */\r
259 /***********************************************************************************************************************\r
260  * Function Name: R_SCI1_AsyncTransmit\r
261  * Description  : This function sends SCI1 data and waits for the transmit end flag.\r
262  * Arguments    : tx_buf -\r
263  *                    transfer buffer pointer\r
264  *                tx_num -\r
265  *                    buffer size\r
266  * Return Value : status -\r
267  *                    MD_OK or MD_ARGERROR\r
268  ***********************************************************************************************************************/\r
269 MD_STATUS R_SCI1_AsyncTransmit (uint8_t * const tx_buf, const uint16_t tx_num)\r
270 {\r
271     MD_STATUS status = MD_OK;\r
272 \r
273     /* clear the flag before initiating a new transmission */\r
274     sci1_txdone = FALSE;\r
275 \r
276     /* Send the data using the API */\r
277     status = R_SCI1_Serial_Send(tx_buf, tx_num);\r
278 \r
279     /* Wait for the transmit end flag */\r
280     while (FALSE == sci1_txdone)\r
281     {\r
282         /* Wait */\r
283     }\r
284     return (status);\r
285 }\r
286 /***********************************************************************************************************************\r
287  * End of function R_SCI1_AsyncTransmit\r
288  ***********************************************************************************************************************/\r
289 \r
290 /* Function required in order to link UARTCommandConsole.c - which is used by\r
291 multiple different demo application. */\r
292 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
293 {\r
294     ( void ) ulWantedBaud;\r
295     ( void ) uxQueueLength;\r
296 \r
297     /* Characters received from the UART are stored in this queue, ready to be\r
298     received by the application.  ***NOTE*** Using a queue in this way is very\r
299     convenient, but also very inefficient.  It can be used here because\r
300     characters will only arrive slowly.  In a higher bandwidth system a circular\r
301     RAM buffer or DMA should be used in place of this queue. */\r
302     xRxQueue = xQueueCreate( uxQueueLength, sizeof( char ) );\r
303     configASSERT( xRxQueue );\r
304 \r
305     /* Set up SCI1 receive buffer */\r
306     R_SCI1_Serial_Receive((uint8_t *) &g_rx_char, 1);\r
307 \r
308     /* Ensure the interrupt priority is at or below\r
309     configMAX_SYSCALL_INTERRUPT_PRIORITY. */\r
310     IPR( SCI1, ERI1 ) = configMAX_SYSCALL_INTERRUPT_PRIORITY - 1;\r
311 \r
312     /* Enable SCI1 operations */\r
313     R_SCI1_Start();\r
314 \r
315     /* Only one UART is supported, so it doesn't matter what is returned\r
316     here. */\r
317     return 0;\r
318 }\r
319 \r
320 /* Function required in order to link UARTCommandConsole.c - which is used by\r
321 multiple different demo application. */\r
322 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
323 {\r
324 const TickType_t xMaxBlockTime = pdMS_TO_TICKS( 5000 );\r
325 \r
326     /* Only one port is supported. */\r
327     ( void ) pxPort;\r
328 \r
329     /* Clear the flag before initiating a new transmission */\r
330     sci1_txdone = FALSE;\r
331 \r
332     /* Don't send the string unless the previous string has been sent. */\r
333     if( ( xSendingTask == NULL ) && ( usStringLength > 0 ) )\r
334     {\r
335         /* Ensure the calling task's notification state is not already\r
336         pending. */\r
337         xTaskNotifyStateClear( NULL );\r
338 \r
339         /* Store the handle of the transmitting task.  This is used to unblock\r
340         the task when the transmission has completed. */\r
341         xSendingTask = xTaskGetCurrentTaskHandle();\r
342 \r
343         /* Send the string using the auto-generated API. */\r
344         R_SCI1_Serial_Send( ( uint8_t * ) pcString, usStringLength );\r
345 \r
346         /* Wait in the Blocked state (so not using any CPU time) until the\r
347         transmission has completed. */\r
348         ulTaskNotifyTake( pdTRUE, xMaxBlockTime );\r
349     }\r
350 }\r
351 \r
352 /* Function required in order to link UARTCommandConsole.c - which is used by\r
353 multiple different demo application. */\r
354 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
355 {\r
356     /* Only one UART is supported. */\r
357     ( void ) pxPort;\r
358 \r
359     /* Return a received character, if any are available.  Otherwise block to\r
360     wait for a character. */\r
361     return xQueueReceive( xRxQueue, pcRxedChar, xBlockTime );\r
362 }\r
363 \r
364 /* Function required in order to link UARTCommandConsole.c - which is used by\r
365 multiple different demo application. */\r
366 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
367 {\r
368     /* Just mapped to vSerialPutString() so the block time is not used. */\r
369     ( void ) xBlockTime;\r
370 \r
371     vSerialPutString( pxPort, &cOutChar, sizeof( cOutChar ) );\r
372     return pdPASS;\r
373 }\r
374 \r
375 /* End user code. Do not edit comment generated here */\r