]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/HCS12_CodeWarrior_banked/serial/serial.c
Update version number ready for V8.2.1 release.
[freertos] / FreeRTOS / Demo / HCS12_CodeWarrior_banked / serial / serial.c
1 /*\r
2     FreeRTOS V8.2.1 - Copyright (C) 2015 Real Time Engineers Ltd.\r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
12 \r
13     ***************************************************************************\r
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
16     >>!   obliged to provide the source code for proprietary components     !<<\r
17     >>!   outside of the FreeRTOS kernel.                                   !<<\r
18     ***************************************************************************\r
19 \r
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
23     link: http://www.freertos.org/a00114.html\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    FreeRTOS provides completely free yet professionally developed,    *\r
28      *    robust, strictly quality controlled, supported, and cross          *\r
29      *    platform software that is more than just the market leader, it     *\r
30      *    is the industry's de facto standard.                               *\r
31      *                                                                       *\r
32      *    Help yourself get started quickly while simultaneously helping     *\r
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
34      *    tutorial book, reference manual, or both:                          *\r
35      *    http://www.FreeRTOS.org/Documentation                              *\r
36      *                                                                       *\r
37     ***************************************************************************\r
38 \r
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading\r
40     the FAQ page "My application does not run, what could be wrong?".  Have you\r
41     defined configASSERT()?\r
42 \r
43     http://www.FreeRTOS.org/support - In return for receiving this top quality\r
44     embedded software for free we request you assist our global community by\r
45     participating in the support forum.\r
46 \r
47     http://www.FreeRTOS.org/training - Investing in training allows your team to\r
48     be as productive as possible as early as possible.  Now you can receive\r
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers\r
50     Ltd, and the world's leading authority on the world's leading RTOS.\r
51 \r
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
55 \r
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
58 \r
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High\r
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
61     licenses offer ticketed support, indemnification and commercial middleware.\r
62 \r
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
64     engineered and independently SIL3 certified version for use in safety and\r
65     mission critical applications that require provable dependability.\r
66 \r
67     1 tab == 4 spaces!\r
68 */\r
69 \r
70 \r
71 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER for port 1.\r
72 \r
73 Note that this driver is written to test the RTOS port and is not intended\r
74 to represent an optimised solution. */\r
75 \r
76 /* Processor Expert generated includes. */\r
77 #include "com0.h"\r
78 \r
79 /* Scheduler include files. */\r
80 #include "FreeRTOS.h"\r
81 #include "queue.h"\r
82 #include "task.h"\r
83 \r
84 /* Demo application include files. */\r
85 #include "serial.h"\r
86 \r
87 /* The queues used to communicate between the task code and the interrupt\r
88 service routines. */\r
89 static QueueHandle_t xRxedChars; \r
90 static QueueHandle_t xCharsForTx; \r
91 \r
92 /* Interrupt identification bits. */\r
93 #define serOVERRUN_INTERRUPT            ( 0x08 )\r
94 #define serRX_INTERRUPT                         ( 0x20 )\r
95 #define serTX_INTERRUPT                         ( 0x80 )\r
96 \r
97 /*-----------------------------------------------------------*/\r
98 \r
99 \r
100 /*\r
101  * Initialise port for interrupt driven communications.\r
102  */\r
103 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
104 {\r
105         /* Hardware setup is performed by the Processor Expert generated code.  \r
106         This function just creates the queues used to communicate between the \r
107         interrupt code and the task code - then sets the required baud rate. */\r
108 \r
109         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
110         xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
111 \r
112         COM0_SetBaudRateMode( ( char ) ulWantedBaud );\r
113 \r
114         return NULL;\r
115 }\r
116 /*-----------------------------------------------------------*/\r
117 \r
118 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
119 {\r
120         /* Get the next character from the buffer queue.  Return false if no characters\r
121         are available, or arrive before xBlockTime expires. */\r
122         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
123         {\r
124                 return pdTRUE;\r
125         }\r
126         else\r
127         {\r
128                 return pdFALSE;\r
129         }\r
130 }\r
131 /*-----------------------------------------------------------*/\r
132 \r
133 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
134 {\r
135         /* Place the character in the queue of characters to be transmitted. */\r
136         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
137         {\r
138                 return pdFAIL;\r
139         }\r
140 \r
141         /* Turn on the Tx interrupt so the ISR will remove the character from the\r
142         queue and send it.   This does not need to be in a critical section as\r
143         if the interrupt has already removed the character the next interrupt\r
144         will simply turn off the Tx interrupt again. */\r
145         SCI0CR2_SCTIE = 1;;\r
146 \r
147         return pdPASS;\r
148 }\r
149 /*-----------------------------------------------------------*/\r
150 \r
151 void vSerialClose( xComPortHandle xPort )\r
152 {       \r
153         /* Not supported. */\r
154         ( void ) xPort;\r
155 }\r
156 /*-----------------------------------------------------------*/\r
157 \r
158 \r
159 /* \r
160  * Interrupt service routine for the serial port.  Must be in non-banked\r
161  * memory. \r
162  */\r
163 \r
164 #pragma CODE_SEG __NEAR_SEG NON_BANKED\r
165 \r
166 __interrupt void vCOM0_ISR( void )\r
167 {\r
168 volatile unsigned char ucByte, ucStatus;\r
169 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
170 \r
171         /* What caused the interrupt? */\r
172         ucStatus = SCI0SR1;\r
173         \r
174         if( ucStatus & serOVERRUN_INTERRUPT )\r
175         {\r
176                 /* The interrupt was caused by an overrun.  Clear the error by reading\r
177                 the data register. */\r
178                 ucByte = SCI0DRL;\r
179         }\r
180 \r
181         if( ucStatus & serRX_INTERRUPT )\r
182         {       \r
183                 /* The interrupt was caused by a character being received.\r
184                 Read the received byte. */\r
185                 ucByte = SCI0DRL;                      \r
186 \r
187                 /* Post the character onto the queue of received characters - noting\r
188                 whether or not this wakes a task. */\r
189                 xQueueSendFromISR( xRxedChars, ( void * ) &ucByte, &xHigherPriorityTaskWoken );\r
190         }\r
191         \r
192         if( ( ucStatus & serTX_INTERRUPT ) && ( SCI0CR2_SCTIE ) )\r
193         {       \r
194                 /* The interrupt was caused by a character being transmitted. */\r
195                 if( xQueueReceiveFromISR( xCharsForTx, ( void * ) &ucByte, &xHigherPriorityTaskWoken ) == pdTRUE )\r
196                 {\r
197                         /* Clear the SCRF bit. */\r
198                         SCI0DRL = ucByte;\r
199                 }\r
200                 else\r
201                 {\r
202                         /* Disable transmit interrupt */\r
203                         SCI0CR2_SCTIE = 0;                 \r
204                 }\r
205         }\r
206 \r
207         if( xHigherPriorityTaskWoken )\r
208         {\r
209                 portYIELD();\r
210         }\r
211 }\r
212 \r
213 #pragma CODE_SEG DEFAULT\r
214 \r