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
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
48 /* End user code. Do not edit comment generated here */
\r
50 /***********************************************************************************************************************
\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
61 /* End user code. Do not edit comment generated here */
\r
62 #include "r_cg_userdefine.h"
\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
74 /* Global used to receive a character from the PC terminal */
\r
77 /* Flag used to control transmission to PC terminal */
\r
78 volatile uint8_t g_tx_flag = FALSE;
\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
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
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
97 /* End user code. Do not edit comment generated here */
\r
99 /***********************************************************************************************************************
\r
100 * Function Name: r_sci1_transmit_interrupt
\r
101 * Description : 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
108 if (g_sci1_tx_count > 0U)
\r
110 SCI1.TDR = *gp_sci1_tx_address;
\r
111 gp_sci1_tx_address++;
\r
116 SCI1.SCR.BIT.TIE = 0U;
\r
117 SCI1.SCR.BIT.TEIE = 1U;
\r
120 /***********************************************************************************************************************
\r
121 * Function Name: r_sci1_transmitend_interrupt
\r
122 * Description : 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
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
135 r_sci1_callback_transmitend();
\r
137 /***********************************************************************************************************************
\r
138 * Function Name: r_sci1_receive_interrupt
\r
139 * Description : 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
146 if (g_sci1_rx_length > g_sci1_rx_count)
\r
148 *gp_sci1_rx_address = SCI1.RDR;
\r
149 gp_sci1_rx_address++;
\r
152 if (g_sci1_rx_length == g_sci1_rx_count)
\r
154 r_sci1_callback_receiveend();
\r
158 /***********************************************************************************************************************
\r
159 * Function Name: r_sci1_receiveerror_interrupt
\r
160 * Description : 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
169 r_sci1_callback_receiveerror();
\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
175 /***********************************************************************************************************************
\r
176 * Function Name: r_sci1_callback_transmitend
\r
177 * Description : This function is a callback function when SCI1 finishes transmission.
\r
179 * Return Value : None
\r
180 ***********************************************************************************************************************/
\r
181 void r_sci1_callback_transmitend(void)
\r
183 /* Start user code. Do not edit comment generated here */
\r
184 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
\r
186 /* The sci1_txdone flag is used by the auto generated API only. */
\r
187 sci1_txdone = TRUE;
\r
189 if( xSendingTask != NULL )
\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
196 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
198 /* End user code. Do not edit comment generated here */
\r
200 /***********************************************************************************************************************
\r
201 * Function Name: r_sci1_callback_receiveend
\r
202 * Description : This function is a callback function when SCI1 finishes reception.
\r
204 * Return Value : None
\r
205 ***********************************************************************************************************************/
\r
206 void r_sci1_callback_receiveend(void)
\r
208 /* Start user code. Do not edit comment generated here */
\r
209 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
\r
211 configASSERT( xRxQueue );
\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
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
227 /* Set up SCI1 receive buffer again */
\r
228 R_SCI1_Serial_Receive((uint8_t *) &g_rx_char, 1);
\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
235 /***********************************************************************************************************************
\r
236 * Function Name: r_sci1_callback_receiveerror
\r
237 * Description : This function is a callback function when SCI1 reception encounters error.
\r
239 * Return Value : None
\r
240 ***********************************************************************************************************************/
\r
241 void r_sci1_callback_receiveerror(void)
\r
243 /* Start user code. Do not edit comment generated here */
\r
244 /* End user code. Do not edit comment generated here */
\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
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
260 MD_STATUS status = MD_OK;
\r
262 /* clear the flag before initiating a new transmission */
\r
263 sci1_txdone = FALSE;
\r
265 /* Send the data using the API */
\r
266 status = R_SCI1_Serial_Send(tx_buf, tx_num);
\r
268 /* Wait for the transmit end flag */
\r
269 while (FALSE == sci1_txdone)
\r
275 /***********************************************************************************************************************
\r
276 * End of function R_SCI1_AsyncTransmit
\r
277 ***********************************************************************************************************************/
\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
283 ( void ) ulWantedBaud;
\r
284 ( void ) uxQueueLength;
\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
294 /* Set up SCI1 receive buffer */
\r
295 R_SCI1_Serial_Receive((uint8_t *) &g_rx_char, 1);
\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
301 /* Enable SCI1 operations */
\r
304 /* Only one UART is supported, so it doesn't matter what is returned
\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
313 const TickType_t xMaxBlockTime = pdMS_TO_TICKS( 5000 );
\r
315 /* Only one port is supported. */
\r
318 /* Clear the flag before initiating a new transmission */
\r
319 sci1_txdone = FALSE;
\r
321 /* Don't send the string unless the previous string has been sent. */
\r
322 if( ( xSendingTask == NULL ) && ( usStringLength > 0 ) )
\r
324 /* Ensure the calling task's notification state is not already
\r
326 vTaskNotifyClear( NULL );
\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
332 /* Send the string using the auto-generated API. */
\r
333 R_SCI1_Serial_Send( ( uint8_t * ) pcString, usStringLength );
\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
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
345 /* Only one UART is supported. */
\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
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
357 /* Just mapped to vSerialPutString() so the block time is not used. */
\r
358 ( void ) xBlockTime;
\r
360 vSerialPutString( pxPort, &cOutChar, sizeof( cOutChar ) );
\r
364 /* End user code. Do not edit comment generated here */
\r