]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/RX100_RX113-RSK_GCC_e2studio_IAR/src/cg_src/r_cg_sci_user_GCC.c
commit 9f316c246baafa15c542a5aea81a94f26e3d6507
[freertos] / FreeRTOS / Demo / RX100_RX113-RSK_GCC_e2studio_IAR / src / cg_src / r_cg_sci_user_GCC.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  * ***NOTE***: Transmitting generates an interrupt for each character, which\r
41  * consumes    CPU time, and can cause standard demo RTOS tasks that monitor their\r
42  * own performance to fail asserts - therefore when using GCC it is best to\r
43  * compile this file with maximum speed optimisation.\r
44  */\r
45 \r
46 \r
47 \r
48 /* End user code. Do not edit comment generated here */\r
49 \r
50 /***********************************************************************************************************************\r
51 Includes\r
52 ***********************************************************************************************************************/\r
53 #include "r_cg_macrodriver.h"\r
54 #include "r_cg_sci.h"\r
55 /* Start user code for include. Do not edit comment generated here */\r
56 #include "rskrx113def.h"\r
57 #include "FreeRTOS.h"\r
58 #include "task.h"\r
59 #include "queue.h"\r
60 #include "serial.h"\r
61 /* End user code. Do not edit comment generated here */\r
62 #include "r_cg_userdefine.h"\r
63 \r
64 /***********************************************************************************************************************\r
65 Global variables and functions\r
66 ***********************************************************************************************************************/\r
67 extern uint8_t * gp_sci1_tx_address;         /* SCI1 send buffer address */\r
68 extern uint16_t  g_sci1_tx_count;            /* SCI1 send data number */\r
69 extern uint8_t * gp_sci1_rx_address;         /* SCI1 receive buffer address */\r
70 extern uint16_t  g_sci1_rx_count;            /* SCI1 receive data number */\r
71 extern uint16_t  g_sci1_rx_length;           /* SCI1 receive data length */\r
72 /* Start user code for global. Do not edit comment generated here */\r
73 \r
74 /* Global used to receive a character from the PC terminal */\r
75 uint8_t g_rx_char;\r
76 \r
77 /* Flag used to control transmission to PC terminal */\r
78 volatile uint8_t g_tx_flag = FALSE;\r
79 \r
80 /* Characters received from the UART are stored in this queue, ready to be\r
81 received by the application.  ***NOTE*** Using a queue in this way is very\r
82 convenient, but also very inefficient.  It can be used here because characters\r
83 will only arrive slowly.  In a higher bandwidth system a circular RAM buffer or\r
84 DMA should be used in place of this queue. */\r
85 static QueueHandle_t xRxQueue = NULL;\r
86 \r
87 /* When a task calls vSerialPutString() its handle is stored in xSendingTask,\r
88 before being placed into the Blocked state (so does not use any CPU time) to\r
89 wait for the transmission to end.  The task handle is then used from the UART\r
90 transmit end interrupt to remove the task from the Blocked state. */\r
91 static TaskHandle_t xSendingTask = NULL;\r
92 \r
93 /* Flag used locally to detect transmission complete.  This is used by the\r
94 auto generated API only. */\r
95 static volatile uint8_t sci1_txdone;\r
96 \r
97 /* End user code. Do not edit comment generated here */\r
98 \r
99 /***********************************************************************************************************************\r
100 * Function Name: r_sci1_transmit_interrupt\r
101 * Description  : None\r
102 * Arguments    : None\r
103 * Return Value : None\r
104 ***********************************************************************************************************************/\r
105 void r_sci1_transmit_interrupt(void) __attribute__((interrupt));\r
106 void r_sci1_transmit_interrupt(void)\r
107 {\r
108     if (g_sci1_tx_count > 0U)\r
109     {\r
110         SCI1.TDR = *gp_sci1_tx_address;\r
111         gp_sci1_tx_address++;\r
112         g_sci1_tx_count--;\r
113     }\r
114     else\r
115     {\r
116         SCI1.SCR.BIT.TIE = 0U;\r
117         SCI1.SCR.BIT.TEIE = 1U;\r
118     }\r
119 }\r
120 /***********************************************************************************************************************\r
121 * Function Name: r_sci1_transmitend_interrupt\r
122 * Description  : None\r
123 * Arguments    : None\r
124 * Return Value : None\r
125 ***********************************************************************************************************************/\r
126 void r_sci1_transmitend_interrupt(void) __attribute__((interrupt));\r
127 void r_sci1_transmitend_interrupt(void)\r
128 {\r
129     /* Set TXD1 pin */\r
130     PORT1.PMR.BYTE &= 0xBFU;\r
131     SCI1.SCR.BIT.TIE = 0U;\r
132     SCI1.SCR.BIT.TE = 0U;\r
133     SCI1.SCR.BIT.TEIE = 0U;\r
134 \r
135     r_sci1_callback_transmitend();\r
136 }\r
137 /***********************************************************************************************************************\r
138 * Function Name: r_sci1_receive_interrupt\r
139 * Description  : None\r
140 * Arguments    : None\r
141 * Return Value : None\r
142 ***********************************************************************************************************************/\r
143 void r_sci1_receive_interrupt(void) __attribute__((interrupt));\r
144 void r_sci1_receive_interrupt(void)\r
145 {\r
146     if (g_sci1_rx_length > g_sci1_rx_count)\r
147     {\r
148         *gp_sci1_rx_address = SCI1.RDR;\r
149         gp_sci1_rx_address++;\r
150         g_sci1_rx_count++;\r
151 \r
152         if (g_sci1_rx_length == g_sci1_rx_count)\r
153         {\r
154             r_sci1_callback_receiveend();\r
155         }\r
156     }\r
157 }\r
158 /***********************************************************************************************************************\r
159 * Function Name: r_sci1_receiveerror_interrupt\r
160 * Description  : None\r
161 * Arguments    : None\r
162 * Return Value : None\r
163 ***********************************************************************************************************************/\r
164 void r_sci1_receiveerror_interrupt(void) __attribute__((interrupt));\r
165 void r_sci1_receiveerror_interrupt(void)\r
166 {\r
167     uint8_t err_type;\r
168 \r
169     r_sci1_callback_receiveerror();\r
170 \r
171     /* Clear overrun, framing and parity error flags */\r
172     err_type = SCI1.SSR.BYTE;\r
173     SCI1.SSR.BYTE = err_type & ( uint8_t ) 0xC7;\r
174 }\r
175 /***********************************************************************************************************************\r
176 * Function Name: r_sci1_callback_transmitend\r
177 * Description  : This function is a callback function when SCI1 finishes transmission.\r
178 * Arguments    : None\r
179 * Return Value : None\r
180 ***********************************************************************************************************************/\r
181 void r_sci1_callback_transmitend(void)\r
182 {\r
183     /* Start user code. Do not edit comment generated here */\r
184     BaseType_t xHigherPriorityTaskWoken = pdFALSE;\r
185 \r
186     /* The sci1_txdone flag is used by the auto generated API only. */\r
187     sci1_txdone = TRUE;\r
188 \r
189     if( xSendingTask != NULL )\r
190     {\r
191         /* A task is waiting for the end of the Tx, unblock it now.\r
192         http://www.freertos.org/vTaskNotifyGiveFromISR.html */\r
193         vTaskNotifyGiveFromISR( xSendingTask, &xHigherPriorityTaskWoken );\r
194         xSendingTask = NULL;\r
195 \r
196         portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
197     }\r
198     /* End user code. Do not edit comment generated here */\r
199 }\r
200 /***********************************************************************************************************************\r
201 * Function Name: r_sci1_callback_receiveend\r
202 * Description  : This function is a callback function when SCI1 finishes reception.\r
203 * Arguments    : None\r
204 * Return Value : None\r
205 ***********************************************************************************************************************/\r
206 void r_sci1_callback_receiveend(void)\r
207 {\r
208     /* Start user code. Do not edit comment generated here */\r
209     BaseType_t xHigherPriorityTaskWoken = pdFALSE;\r
210 \r
211     configASSERT( xRxQueue );\r
212 \r
213     /* Transmitting generates an interrupt for each character, which consumes\r
214     CPU time, and can cause standard demo RTOS tasks that monitor their own\r
215     performance to fail asserts - so don't receive new CLI commands if a\r
216     transmit is not already in progress. */\r
217     if( sci1_txdone == TRUE )\r
218     {\r
219         /* Characters received from the UART are stored in this queue, ready to be\r
220         received by the application.  ***NOTE*** Using a queue in this way is very\r
221         convenient, but also very inefficient.  It can be used here because\r
222         characters will only arrive slowly.  In a higher bandwidth system a circular\r
223         RAM buffer or DMA should be used in place of this queue. */\r
224         xQueueSendFromISR( xRxQueue, &g_rx_char, &xHigherPriorityTaskWoken );\r
225     }\r
226 \r
227     /* Set up SCI1 receive buffer again */\r
228     R_SCI1_Serial_Receive((uint8_t *) &g_rx_char, 1);\r
229 \r
230     /* See http://www.freertos.org/xQueueOverwriteFromISR.html for information\r
231     on the semantics of this ISR. */\r
232     portYIELD_FROM_ISR( xHigherPriorityTaskWoken );\r
233     /* End user code. Do not edit comment generated here */\r
234 }\r
235 /***********************************************************************************************************************\r
236 * Function Name: r_sci1_callback_receiveerror\r
237 * Description  : This function is a callback function when SCI1 reception encounters error.\r
238 * Arguments    : None\r
239 * Return Value : None\r
240 ***********************************************************************************************************************/\r
241 void r_sci1_callback_receiveerror(void)\r
242 {\r
243     /* Start user code. Do not edit comment generated here */\r
244     /* End user code. Do not edit comment generated here */\r
245 }\r
246 \r
247 /* Start user code for adding. Do not edit comment generated here */\r
248 /***********************************************************************************************************************\r
249  * Function Name: R_SCI1_AsyncTransmit\r
250  * Description  : This function sends SCI1 data and waits for the transmit end flag.\r
251  * Arguments    : tx_buf -\r
252  *                    transfer buffer pointer\r
253  *                tx_num -\r
254  *                    buffer size\r
255  * Return Value : status -\r
256  *                    MD_OK or MD_ARGERROR\r
257  ***********************************************************************************************************************/\r
258 MD_STATUS R_SCI1_AsyncTransmit (uint8_t * const tx_buf, const uint16_t tx_num)\r
259 {\r
260     MD_STATUS status = MD_OK;\r
261 \r
262     /* clear the flag before initiating a new transmission */\r
263     sci1_txdone = FALSE;\r
264 \r
265     /* Send the data using the API */\r
266     status = R_SCI1_Serial_Send(tx_buf, tx_num);\r
267 \r
268     /* Wait for the transmit end flag */\r
269     while (FALSE == sci1_txdone)\r
270     {\r
271         /* Wait */\r
272     }\r
273     return (status);\r
274 }\r
275 /***********************************************************************************************************************\r
276  * End of function R_SCI1_AsyncTransmit\r
277  ***********************************************************************************************************************/\r
278 \r
279 /* Function required in order to link UARTCommandConsole.c - which is used by\r
280 multiple different demo application. */\r
281 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
282 {\r
283     ( void ) ulWantedBaud;\r
284     ( void ) uxQueueLength;\r
285 \r
286     /* Characters received from the UART are stored in this queue, ready to be\r
287     received by the application.  ***NOTE*** Using a queue in this way is very\r
288     convenient, but also very inefficient.  It can be used here because\r
289     characters will only arrive slowly.  In a higher bandwidth system a circular\r
290     RAM buffer or DMA should be used in place of this queue. */\r
291     xRxQueue = xQueueCreate( uxQueueLength, sizeof( char ) );\r
292     configASSERT( xRxQueue );\r
293 \r
294     /* Set up SCI1 receive buffer */\r
295     R_SCI1_Serial_Receive((uint8_t *) &g_rx_char, 1);\r
296 \r
297     /* Ensure the interrupt priority is at or below\r
298     configMAX_SYSCALL_INTERRUPT_PRIORITY. */\r
299     IPR( SCI1, ERI1 ) = configKERNEL_INTERRUPT_PRIORITY + 1;\r
300 \r
301     /* Enable SCI1 operations */\r
302     R_SCI1_Start();\r
303 \r
304     /* Only one UART is supported, so it doesn't matter what is returned\r
305     here. */\r
306     return 0;\r
307 }\r
308 \r
309 /* Function required in order to link UARTCommandConsole.c - which is used by\r
310 multiple different demo application. */\r
311 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
312 {\r
313 const TickType_t xMaxBlockTime = pdMS_TO_TICKS( 5000 );\r
314 \r
315     /* Only one port is supported. */\r
316     ( void ) pxPort;\r
317 \r
318     /* Clear the flag before initiating a new transmission */\r
319     sci1_txdone = FALSE;\r
320 \r
321     /* Don't send the string unless the previous string has been sent. */\r
322     if( ( xSendingTask == NULL ) && ( usStringLength > 0 ) )\r
323     {\r
324         /* Ensure the calling task's notification state is not already\r
325         pending. */\r
326         xTaskNotifyStateClear( NULL );\r
327 \r
328         /* Store the handle of the transmitting task.  This is used to unblock\r
329         the task when the transmission has completed. */\r
330         xSendingTask = xTaskGetCurrentTaskHandle();\r
331 \r
332         /* Send the string using the auto-generated API. */\r
333         R_SCI1_Serial_Send( ( uint8_t * ) pcString, usStringLength );\r
334 \r
335         /* Wait in the Blocked state (so not using any CPU time) until the\r
336         transmission has completed. */\r
337         ulTaskNotifyTake( pdTRUE, xMaxBlockTime );\r
338     }\r
339 }\r
340 \r
341 /* Function required in order to link UARTCommandConsole.c - which is used by\r
342 multiple different demo application. */\r
343 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
344 {\r
345     /* Only one UART is supported. */\r
346     ( void ) pxPort;\r
347 \r
348     /* Return a received character, if any are available.  Otherwise block to\r
349     wait for a character. */\r
350     return xQueueReceive( xRxQueue, pcRxedChar, xBlockTime );\r
351 }\r
352 \r
353 /* Function required in order to link UARTCommandConsole.c - which is used by\r
354 multiple different demo application. */\r
355 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
356 {\r
357     /* Just mapped to vSerialPutString() so the block time is not used. */\r
358     ( void ) xBlockTime;\r
359 \r
360     vSerialPutString( pxPort, &cOutChar, sizeof( cOutChar ) );\r
361     return pdPASS;\r
362 }\r
363 \r
364 /* End user code. Do not edit comment generated here */\r