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