2 serial.c for using FreeRTOS
\r
3 Copyright (C) 2005 Robotronics Inc.
\r
7 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER for port 1.
\r
9 GCC demo modifications by Jeff Smith, Robotronics Inc. 2005
\r
13 #include <sys/sio.h>
\r
15 /* Scheduler include files. */
\r
16 #include "FreeRTOS.h"
\r
20 /* Demo application include files. */
\r
24 /* The queues used to communicate between the task code and the interrupt
\r
25 service routines. */
\r
26 static xQueueHandle xRxedChars;
\r
27 static xQueueHandle xCharsForTx;
\r
29 /* Interrupt identification bits. */
\r
30 #define serOVERRUN_INTERRUPT ( '\x08' )
\r
31 #define serRX_INTERRUPT ( 0x20 )
\r
32 #define serTX_INTERRUPT ( 0x80 )
\r
34 /*-----------------------------------------------------------*/
\r
38 * Initialise port for interrupt driven communications.
\r
40 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
42 /* Hardware setup is performed by the Processor Expert generated code.
\r
43 This function just creates the queues used to communicate between the
\r
44 interrupt code and the task code - then sets the required baud rate. */
\r
46 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
47 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
49 SCI_SetBaudRateMode( ( portCHAR ) ulWantedBaud );
\r
53 /*-----------------------------------------------------------*/
\r
55 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
\r
57 /* Get the next character from the buffer queue. Return false if no characters
\r
58 are available, or arrive before xBlockTime expires. */
\r
59 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
68 /*-----------------------------------------------------------*/
\r
70 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
\r
72 /* Place the character in the queue of characters to be transmitted. */
\r
73 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
78 /* Turn on the Tx interrupt so the ISR will remove the character from the
\r
79 queue and send it. This does not need to be in a critical section as
\r
80 if the interrupt has already removed the character the next interrupt
\r
81 will simply turn off the Tx interrupt again. */
\r
82 SCICR2 |= 0x80; // TIE
\r
86 /*-----------------------------------------------------------*/
\r
88 void vSerialClose( xComPortHandle xPort )
\r
90 /* Not supported. */
\r
93 /*-----------------------------------------------------------*/
\r
97 * Interrupt service routine for the serial port. Must be in non-banked
\r
101 void ATTR_INT ATTR_NEAR vCOM_ISR( void );
\r
103 void vCOM_ISR( void )
\r
105 volatile unsigned portCHAR ucByte, ucStatus;
\r
106 portBASE_TYPE xTaskWokenByPost = pdFALSE, xTaskWokenByTx = pdFALSE;
\r
108 /* What caused the interrupt? */
\r
111 if( ucStatus & serOVERRUN_INTERRUPT )
\r
113 /* The interrupt was caused by an overrun. Clear the error by reading
\r
114 the data register. */
\r
118 if( ucStatus & serRX_INTERRUPT )
\r
120 /* The interrupt was caused by a character being received.
\r
121 Read the received byte. */
\r
124 /* Post the character onto the queue of received characters - noting
\r
125 whether or not this wakes a task. */
\r
126 xTaskWokenByPost = xQueueSendFromISR( xRxedChars, ( void * ) &ucByte, pdFALSE );
\r
129 if( ( ucStatus & serTX_INTERRUPT ) && ( SCICR2 & 0x80 ) )
\r
131 /* The interrupt was caused by a character being transmitted. */
\r
132 if( xQueueReceiveFromISR( xCharsForTx, ( void * ) &ucByte, &xTaskWokenByTx ) == pdTRUE )
\r
134 /* Clear the SCRF bit. */
\r
139 /* Disable transmit interrupt */
\r
140 SCICR2 &= ~0x80; // TIE
\r
144 if( ( xTaskWokenByPost ) || ( xTaskWokenByTx ) )
\r