]> git.sur5r.net Git - freertos/blob - Demo/HCS12_CodeWarrior_banked/serial/serial.c
Update version numbers to V4.8.0
[freertos] / Demo / HCS12_CodeWarrior_banked / serial / serial.c
1 /*\r
2         FreeRTOS.org V4.8.0 - Copyright (C) 2003-2008 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         ***************************************************************************\r
28         *                                                                                                                                                 *\r
29         * SAVE TIME AND MONEY!  Why not get us to quote to get FreeRTOS.org               *\r
30         * running on your hardware - or even write all or part of your application*\r
31         * for you?  See http://www.OpenRTOS.com for details.                                      *\r
32         *                                                                                                                                                 *\r
33         ***************************************************************************\r
34         ***************************************************************************\r
35 \r
36         Please ensure to read the configuration and relevant port sections of the\r
37         online documentation.\r
38 \r
39         http://www.FreeRTOS.org - Documentation, latest information, license and \r
40         contact details.\r
41 \r
42         http://www.SafeRTOS.com - A version that is certified for use in safety \r
43         critical systems.\r
44 \r
45         http://www.OpenRTOS.com - Commercial support, development, porting, \r
46         licensing and training services.\r
47 */\r
48 \r
49 \r
50 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER for port 1.\r
51 \r
52 Note that this driver is written to test the RTOS port and is not intended\r
53 to represent an optimised solution. */\r
54 \r
55 /* Processor Expert generated includes. */\r
56 #include "com0.h"\r
57 \r
58 /* Scheduler include files. */\r
59 #include "FreeRTOS.h"\r
60 #include "queue.h"\r
61 #include "task.h"\r
62 \r
63 /* Demo application include files. */\r
64 #include "serial.h"\r
65 \r
66 /* The queues used to communicate between the task code and the interrupt\r
67 service routines. */\r
68 static xQueueHandle xRxedChars; \r
69 static xQueueHandle xCharsForTx; \r
70 \r
71 /* Interrupt identification bits. */\r
72 #define serOVERRUN_INTERRUPT            ( 0x08 )\r
73 #define serRX_INTERRUPT                         ( 0x20 )\r
74 #define serTX_INTERRUPT                         ( 0x80 )\r
75 \r
76 /*-----------------------------------------------------------*/\r
77 \r
78 \r
79 /*\r
80  * Initialise port for interrupt driven communications.\r
81  */\r
82 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
83 {\r
84         /* Hardware setup is performed by the Processor Expert generated code.  \r
85         This function just creates the queues used to communicate between the \r
86         interrupt code and the task code - then sets the required baud rate. */\r
87 \r
88         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
89         xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );\r
90 \r
91         COM0_SetBaudRateMode( ( portCHAR ) ulWantedBaud );\r
92 \r
93         return NULL;\r
94 }\r
95 /*-----------------------------------------------------------*/\r
96 \r
97 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
98 {\r
99         /* Get the next character from the buffer queue.  Return false if no characters\r
100         are available, or arrive before xBlockTime expires. */\r
101         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
102         {\r
103                 return pdTRUE;\r
104         }\r
105         else\r
106         {\r
107                 return pdFALSE;\r
108         }\r
109 }\r
110 /*-----------------------------------------------------------*/\r
111 \r
112 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
113 {\r
114         /* Place the character in the queue of characters to be transmitted. */\r
115         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
116         {\r
117                 return pdFAIL;\r
118         }\r
119 \r
120         /* Turn on the Tx interrupt so the ISR will remove the character from the\r
121         queue and send it.   This does not need to be in a critical section as\r
122         if the interrupt has already removed the character the next interrupt\r
123         will simply turn off the Tx interrupt again. */\r
124         SCI0CR2_SCTIE = 1;;\r
125 \r
126         return pdPASS;\r
127 }\r
128 /*-----------------------------------------------------------*/\r
129 \r
130 void vSerialClose( xComPortHandle xPort )\r
131 {       \r
132         /* Not supported. */\r
133         ( void ) xPort;\r
134 }\r
135 /*-----------------------------------------------------------*/\r
136 \r
137 \r
138 /* \r
139  * Interrupt service routine for the serial port.  Must be in non-banked\r
140  * memory. \r
141  */\r
142 \r
143 #pragma CODE_SEG __NEAR_SEG NON_BANKED\r
144 \r
145 __interrupt void vCOM0_ISR( void )\r
146 {\r
147 volatile unsigned portCHAR ucByte, ucStatus;\r
148 portBASE_TYPE xTaskWokenByPost = pdFALSE, xTaskWokenByTx = pdFALSE;\r
149 \r
150         /* What caused the interrupt? */\r
151         ucStatus = SCI0SR1;\r
152         \r
153         if( ucStatus & serOVERRUN_INTERRUPT )\r
154         {\r
155                 /* The interrupt was caused by an overrun.  Clear the error by reading\r
156                 the data register. */\r
157                 ucByte = SCI0DRL;\r
158         }\r
159 \r
160         if( ucStatus & serRX_INTERRUPT )\r
161         {       \r
162                 /* The interrupt was caused by a character being received.\r
163                 Read the received byte. */\r
164                 ucByte = SCI0DRL;                      \r
165 \r
166                 /* Post the character onto the queue of received characters - noting\r
167                 whether or not this wakes a task. */\r
168                 xTaskWokenByPost = xQueueSendFromISR( xRxedChars, ( void * ) &ucByte, pdFALSE );                \r
169         }\r
170         \r
171         if( ( ucStatus & serTX_INTERRUPT ) && ( SCI0CR2_SCTIE ) )\r
172         {       \r
173                 /* The interrupt was caused by a character being transmitted. */\r
174                 if( xQueueReceiveFromISR( xCharsForTx, ( void * ) &ucByte, &xTaskWokenByTx ) == pdTRUE )\r
175                 {\r
176                         /* Clear the SCRF bit. */\r
177                         SCI0DRL = ucByte;\r
178                 }\r
179                 else\r
180                 {\r
181                         /* Disable transmit interrupt */\r
182                         SCI0CR2_SCTIE = 0;                 \r
183                 }\r
184         }\r
185 \r
186         if( ( xTaskWokenByPost ) || ( xTaskWokenByTx ) )\r
187         {\r
188                 portYIELD();\r
189         }\r
190 }\r
191 \r
192 #pragma CODE_SEG DEFAULT\r
193 \r