1 /***********************************************************************************************************************
\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
15 * http://www.renesas.com/disclaimer
\r
17 * Copyright (C) 2015 Renesas Electronics Corporation. All rights reserved.
\r
18 ***********************************************************************************************************************/
\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
25 * Description : This file implements device driver for SCI module.
\r
26 * Creation Date: 21/09/2015
\r
27 ***********************************************************************************************************************/
\r
29 /***********************************************************************************************************************
\r
31 ***********************************************************************************************************************/
\r
32 /* Start user code for pragma. Do not edit comment generated here */
\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
43 /* End user code. Do not edit comment generated here */
\r
45 /***********************************************************************************************************************
\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
56 /* End user code. Do not edit comment generated here */
\r
57 #include "r_cg_userdefine.h"
\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
69 /* Global used to receive a character from the PC terminal */
\r
72 /* Flag used to control transmission to PC terminal */
\r
73 volatile uint8_t g_tx_flag = FALSE;
\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
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
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
92 /* End user code. Do not edit comment generated here */
\r
94 /***********************************************************************************************************************
\r
95 * Function Name: r_sci1_transmit_interrupt
\r
96 * Description : 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
103 #pragma interrupt r_sci1_transmit_interrupt(vect=VECT(SCI1,TXI1))
\r
105 static void r_sci1_transmit_interrupt(void)
\r
107 if (g_sci1_tx_count > 0U)
\r
109 SCI1.TDR = *gp_sci1_tx_address;
\r
110 gp_sci1_tx_address++;
\r
115 SCI1.SCR.BIT.TIE = 0U;
\r
116 SCI1.SCR.BIT.TEIE = 1U;
\r
119 /***********************************************************************************************************************
\r
120 * Function Name: r_sci1_transmitend_interrupt
\r
121 * Description : 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
128 #pragma interrupt r_sci1_transmitend_interrupt(vect=VECT(SCI1,TEI1))
\r
130 static void r_sci1_transmitend_interrupt(void)
\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
138 r_sci1_callback_transmitend();
\r
140 /***********************************************************************************************************************
\r
141 * Function Name: r_sci1_receive_interrupt
\r
142 * Description : 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
149 #pragma interrupt r_sci1_receive_interrupt(vect=VECT(SCI1,RXI1))
\r
151 static void r_sci1_receive_interrupt(void)
\r
153 if (g_sci1_rx_length > g_sci1_rx_count)
\r
155 *gp_sci1_rx_address = SCI1.RDR;
\r
156 gp_sci1_rx_address++;
\r
159 if (g_sci1_rx_length == g_sci1_rx_count)
\r
161 r_sci1_callback_receiveend();
\r
165 /***********************************************************************************************************************
\r
166 * Function Name: r_sci1_receiveerror_interrupt
\r
167 * Description : 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
174 #pragma interrupt r_sci1_receiveerror_interrupt(vect=VECT(SCI1,ERI1))
\r
176 static void r_sci1_receiveerror_interrupt(void)
\r
180 r_sci1_callback_receiveerror();
\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
186 /***********************************************************************************************************************
\r
187 * Function Name: r_sci1_callback_transmitend
\r
188 * Description : This function is a callback function when SCI1 finishes transmission.
\r
190 * Return Value : None
\r
191 ***********************************************************************************************************************/
\r
192 void r_sci1_callback_transmitend(void)
\r
194 /* Start user code. Do not edit comment generated here */
\r
195 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
\r
197 /* The sci1_txdone flag is used by the auto generated API only. */
\r
198 sci1_txdone = TRUE;
\r
200 if( xSendingTask != NULL )
\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
207 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
209 /* End user code. Do not edit comment generated here */
\r
211 /***********************************************************************************************************************
\r
212 * Function Name: r_sci1_callback_receiveend
\r
213 * Description : This function is a callback function when SCI1 finishes reception.
\r
215 * Return Value : None
\r
216 ***********************************************************************************************************************/
\r
217 void r_sci1_callback_receiveend(void)
\r
219 /* Start user code. Do not edit comment generated here */
\r
220 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
\r
222 configASSERT( xRxQueue );
\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
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
238 /* Set up SCI1 receive buffer again */
\r
239 R_SCI1_Serial_Receive((uint8_t *) &g_rx_char, 1);
\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
246 /***********************************************************************************************************************
\r
247 * Function Name: r_sci1_callback_receiveerror
\r
248 * Description : This function is a callback function when SCI1 reception encounters error.
\r
250 * Return Value : None
\r
251 ***********************************************************************************************************************/
\r
252 void r_sci1_callback_receiveerror(void)
\r
254 /* Start user code. Do not edit comment generated here */
\r
255 /* End user code. Do not edit comment generated here */
\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
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
271 MD_STATUS status = MD_OK;
\r
273 /* clear the flag before initiating a new transmission */
\r
274 sci1_txdone = FALSE;
\r
276 /* Send the data using the API */
\r
277 status = R_SCI1_Serial_Send(tx_buf, tx_num);
\r
279 /* Wait for the transmit end flag */
\r
280 while (FALSE == sci1_txdone)
\r
286 /***********************************************************************************************************************
\r
287 * End of function R_SCI1_AsyncTransmit
\r
288 ***********************************************************************************************************************/
\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
294 ( void ) ulWantedBaud;
\r
295 ( void ) uxQueueLength;
\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
305 /* Set up SCI1 receive buffer */
\r
306 R_SCI1_Serial_Receive((uint8_t *) &g_rx_char, 1);
\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
312 /* Enable SCI1 operations */
\r
315 /* Only one UART is supported, so it doesn't matter what is returned
\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
324 const TickType_t xMaxBlockTime = pdMS_TO_TICKS( 5000 );
\r
326 /* Only one port is supported. */
\r
329 /* Clear the flag before initiating a new transmission */
\r
330 sci1_txdone = FALSE;
\r
332 /* Don't send the string unless the previous string has been sent. */
\r
333 if( xSendingTask == NULL )
\r
335 /* Ensure the calling task's notification state is not already
\r
337 vTaskNotifyClear( NULL );
\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
343 /* Send the string using the auto-generated API. */
\r
344 R_SCI1_Serial_Send( ( uint8_t * ) pcString, usStringLength );
\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
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
356 /* Only one UART is supported. */
\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
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
368 /* Just mapped to vSerialPutString() so the block time is not used. */
\r
369 ( void ) xBlockTime;
\r
371 vSerialPutString( pxPort, &cOutChar, sizeof( cOutChar ) );
\r
375 /* End user code. Do not edit comment generated here */
\r