]> git.sur5r.net Git - freertos/blob - Demo/HCS12_CodeWarrior_banked/serial/serial.c
Update in preparation for the V4.3.1 release.
[freertos] / Demo / HCS12_CodeWarrior_banked / serial / serial.c
1 /*\r
2         FreeRTOS.org V4.3.1 - Copyright (C) 2003-2007 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section \r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26         ***************************************************************************\r
27         See http://www.FreeRTOS.org for documentation, latest information, license \r
28         and contact details.  Please ensure to read the configuration and relevant \r
29         port sections of the online documentation.\r
30 \r
31         Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along\r
32         with commercial development and support options.\r
33         ***************************************************************************\r
34 */\r
35 \r
36 \r
37 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER for port 1.\r
38 \r
39 Note that this driver is written to test the RTOS port and is not intended\r
40 to represent an optimised solution. */\r
41 \r
42 /* Processor Expert generated includes. */\r
43 #include "com0.h"\r
44 \r
45 /* Scheduler include files. */\r
46 #include "FreeRTOS.h"\r
47 #include "queue.h"\r
48 #include "task.h"\r
49 \r
50 /* Demo application include files. */\r
51 #include "serial.h"\r
52 \r
53 /* The queues used to communicate between the task code and the interrupt\r
54 service routines. */\r
55 static xQueueHandle xRxedChars; \r
56 static xQueueHandle xCharsForTx; \r
57 \r
58 /* Interrupt identification bits. */\r
59 #define serOVERRUN_INTERRUPT            ( 0x08 )\r
60 #define serRX_INTERRUPT                         ( 0x20 )\r
61 #define serTX_INTERRUPT                         ( 0x80 )\r
62 \r
63 /*-----------------------------------------------------------*/\r
64 \r
65 \r
66 /*\r
67  * Initialise port for interrupt driven communications.\r
68  */\r
69 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
70 {\r
71         /* Hardware setup is performed by the Processor Expert generated code.  \r
72         This function just creates the queues used to communicate between the \r
73         interrupt code and the task code - then sets the required baud rate. */\r
74 \r
75         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
76         xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
77 \r
78         COM0_SetBaudRateMode( ( portCHAR ) ulWantedBaud );\r
79 \r
80         return NULL;\r
81 }\r
82 /*-----------------------------------------------------------*/\r
83 \r
84 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
85 {\r
86         /* Get the next character from the buffer queue.  Return false if no characters\r
87         are available, or arrive before xBlockTime expires. */\r
88         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
89         {\r
90                 return pdTRUE;\r
91         }\r
92         else\r
93         {\r
94                 return pdFALSE;\r
95         }\r
96 }\r
97 /*-----------------------------------------------------------*/\r
98 \r
99 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
100 {\r
101         /* Place the character in the queue of characters to be transmitted. */\r
102         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
103         {\r
104                 return pdFAIL;\r
105         }\r
106 \r
107         /* Turn on the Tx interrupt so the ISR will remove the character from the\r
108         queue and send it.   This does not need to be in a critical section as\r
109         if the interrupt has already removed the character the next interrupt\r
110         will simply turn off the Tx interrupt again. */\r
111         SCI0CR2_SCTIE = 1;;\r
112 \r
113         return pdPASS;\r
114 }\r
115 /*-----------------------------------------------------------*/\r
116 \r
117 void vSerialClose( xComPortHandle xPort )\r
118 {       \r
119         /* Not supported. */\r
120         ( void ) xPort;\r
121 }\r
122 /*-----------------------------------------------------------*/\r
123 \r
124 \r
125 /* \r
126  * Interrupt service routine for the serial port.  Must be in non-banked\r
127  * memory. \r
128  */\r
129 \r
130 #pragma CODE_SEG __NEAR_SEG NON_BANKED\r
131 \r
132 __interrupt void vCOM0_ISR( void )\r
133 {\r
134 volatile unsigned portCHAR ucByte, ucStatus;\r
135 portBASE_TYPE xTaskWokenByPost = pdFALSE, xTaskWokenByTx = pdFALSE;\r
136 \r
137         /* What caused the interrupt? */\r
138         ucStatus = SCI0SR1;\r
139         \r
140         if( ucStatus & serOVERRUN_INTERRUPT )\r
141         {\r
142                 /* The interrupt was caused by an overrun.  Clear the error by reading\r
143                 the data register. */\r
144                 ucByte = SCI0DRL;\r
145         }\r
146 \r
147         if( ucStatus & serRX_INTERRUPT )\r
148         {       \r
149                 /* The interrupt was caused by a character being received.\r
150                 Read the received byte. */\r
151                 ucByte = SCI0DRL;                      \r
152 \r
153                 /* Post the character onto the queue of received characters - noting\r
154                 whether or not this wakes a task. */\r
155                 xTaskWokenByPost = xQueueSendFromISR( xRxedChars, ( void * ) &ucByte, pdFALSE );                \r
156         }\r
157         \r
158         if( ( ucStatus & serTX_INTERRUPT ) && ( SCI0CR2_SCTIE ) )\r
159         {       \r
160                 /* The interrupt was caused by a character being transmitted. */\r
161                 if( xQueueReceiveFromISR( xCharsForTx, ( void * ) &ucByte, &xTaskWokenByTx ) == pdTRUE )\r
162                 {\r
163                         /* Clear the SCRF bit. */\r
164                         SCI0DRL = ucByte;\r
165                 }\r
166                 else\r
167                 {\r
168                         /* Disable transmit interrupt */\r
169                         SCI0CR2_SCTIE = 0;                 \r
170                 }\r
171         }\r
172 \r
173         if( ( xTaskWokenByPost ) || ( xTaskWokenByTx ) )\r
174         {\r
175                 portYIELD();\r
176         }\r
177 }\r
178 \r
179 #pragma CODE_SEG DEFAULT\r
180 \r