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