]> git.sur5r.net Git - freertos/blob - Demo/H8S/RTOSDemo/serial/serial.c
First version under SVN is V4.0.1
[freertos] / Demo / H8S / RTOSDemo / 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.  In particular no use is made of the DMA\r
38 peripheral. */\r
39 \r
40 /* Standard include files. */\r
41 #include <stdlib.h>\r
42 \r
43 /* Scheduler include files. */\r
44 #include "FreeRTOS.h"\r
45 #include "queue.h"\r
46 #include "task.h"\r
47 \r
48 /* Demo application include files. */\r
49 #include "serial.h"\r
50 \r
51 /* The queues used to communicate between the task code and the interrupt\r
52 service routines. */\r
53 static xQueueHandle xRxedChars; \r
54 static xQueueHandle xCharsForTx; \r
55 \r
56 /* Hardware specific constants. */\r
57 #define serTX_INTERRUPT                         ( ( unsigned portCHAR ) 0x80 )\r
58 #define serRX_INTERRUPT                         ( ( unsigned portCHAR ) 0x40 )\r
59 #define serTX_ENABLE                            ( ( unsigned portCHAR ) 0x20 )\r
60 #define serRX_ENABLE                            ( ( unsigned portCHAR ) 0x10 )\r
61 \r
62 /* Macros to turn on and off the serial port THRE interrupt while leaving the\r
63 other register bits in their correct state.   The Rx interrupt is always \r
64 enabled. */\r
65 #define serTX_INTERRUPT_ON()            SCR1 = serTX_INTERRUPT | serRX_INTERRUPT | serTX_ENABLE | serRX_ENABLE;                                                                 \r
66 #define serTX_INTERRUPT_OFF()           SCR1 =                                   serRX_INTERRUPT | serTX_ENABLE | serRX_ENABLE;\r
67 \r
68 /* Bit used to switch on the channel 1 serial port in the module stop \r
69 register. */\r
70 #define serMSTP6                                        ( ( unsigned portSHORT ) 0x0040 )\r
71 \r
72 /* Interrupt service routines.  Note that the Rx and Tx service routines can \r
73 cause a context switch and are therefore defined with the saveall attribute in\r
74 addition to the interrupt_handler attribute.  See the FreeRTOS.org WEB site \r
75 documentation for a full explanation.*/\r
76 void vCOM_1_Rx_ISR( void ) __attribute__ ( ( saveall, interrupt_handler ) );\r
77 void vCOM_1_Tx_ISR( void ) __attribute__ ( ( saveall, interrupt_handler ) );\r
78 void vCOM_1_Error_ISR( void ) __attribute__ ( ( interrupt_handler ) );\r
79 \r
80 /*-----------------------------------------------------------*/\r
81 \r
82 /*\r
83  * Initialise port 1 for interrupt driven communications.\r
84  */\r
85 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
86 {\r
87         /* Create the queues used to communicate between the tasks and the\r
88         interrupt service routines. */\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         /* No parity, 8 data bits and 1 stop bit is the default so does not require \r
93         configuration - setup the remains of the hardware. */\r
94         portENTER_CRITICAL();\r
95         {\r
96                 /* Turn channel 1 on. */\r
97                 MSTPCR &= ~serMSTP6;\r
98 \r
99                 /* Enable the channels and the Rx interrupt.  The Tx interrupt is only \r
100                 enabled when data is being transmitted. */\r
101                 SCR1 = serRX_INTERRUPT | serTX_ENABLE | serRX_ENABLE;\r
102 \r
103                 /* Bit rate settings for 22.1184MHz clock only!. */\r
104                 switch( ulWantedBaud )\r
105                 {\r
106                         case 4800       :       BRR1 = 143;\r
107                                                         break;\r
108                         case 9600       :       BRR1 = 71;\r
109                                                         break;\r
110                         case 19200      :       BRR1 = 35;\r
111                                                         break;\r
112                         case 38400      :       BRR1 = 17;\r
113                                                         break;\r
114                         case 57600      :       BRR1 = 11;\r
115                                                         break;\r
116                         case 115200     :       BRR1 = 5;\r
117                                                         break;\r
118                         default         :       BRR1 = 5;\r
119                                                         break;\r
120                 }\r
121         }\r
122         portEXIT_CRITICAL();    \r
123 \r
124         /* Unlike some ports, this driver code does not allow for more than one\r
125         com port.  We therefore don't return a pointer to a port structure and can\r
126         instead just return NULL. */\r
127         return NULL;\r
128 }\r
129 /*-----------------------------------------------------------*/\r
130 \r
131 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )\r
132 {\r
133         /* Get the next character from the buffer queue.  Return false if no characters\r
134         are available, or arrive before xBlockTime expires. */\r
135         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
136         {\r
137                 return pdTRUE;\r
138         }\r
139         else\r
140         {\r
141                 return pdFALSE;\r
142         }\r
143 }\r
144 /*-----------------------------------------------------------*/\r
145 \r
146 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )\r
147 {\r
148 signed portBASE_TYPE xReturn = pdPASS;\r
149 \r
150         /* Return false if after the block time there is no room on the Tx queue. */\r
151         portENTER_CRITICAL();\r
152         {\r
153                 /* Send a character to the queue of characters waiting transmission.\r
154                 The queue is serviced by the Tx ISR. */\r
155                 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
156                 {\r
157                         /* Could not post onto the queue. */\r
158                         xReturn = pdFAIL;\r
159                 }\r
160                 else\r
161                 {\r
162                         /* The message was posted onto the queue so we turn on the Tx\r
163                         interrupt to allow the Tx ISR to remove the character from the\r
164                         queue. */\r
165                         serTX_INTERRUPT_ON();\r
166                 }\r
167         }\r
168         portEXIT_CRITICAL();\r
169 \r
170         return xReturn;\r
171 }\r
172 /*-----------------------------------------------------------*/\r
173 \r
174 void vSerialClose( xComPortHandle xPort )\r
175 {       \r
176         /* Not supported. */\r
177         ( void ) xPort;\r
178 }\r
179 /*-----------------------------------------------------------*/\r
180 \r
181 void vCOM_1_Rx_ISR( void )\r
182 {\r
183         /* This can cause a context switch so this macro must be the first line\r
184         in the function. */\r
185         portENTER_SWITCHING_ISR();\r
186 \r
187         /* As this is a switching ISR the local variables must be declared as \r
188         static. */\r
189         static portCHAR cRxByte;\r
190         static portBASE_TYPE xTaskWokenByPost;\r
191 \r
192                 /* Get the character. */\r
193                 cRxByte = RDR1;\r
194 \r
195                 /* Post the character onto the queue of received characters - noting\r
196                 whether or not this wakes a task. */\r
197                 xTaskWokenByPost = xQueueSendFromISR( xRxedChars, &cRxByte, pdFALSE );          \r
198 \r
199                 /* Clear the interrupt. */\r
200                 SSR1 &= ~serRX_INTERRUPT;\r
201 \r
202         /* This must be the last line in the function.  We pass cTaskWokenByPost so \r
203         a context switch will occur if the received character woke a task that has\r
204         a priority higher than the task we interrupted. */\r
205         portEXIT_SWITCHING_ISR( xTaskWokenByPost );\r
206 }\r
207 /*-----------------------------------------------------------*/\r
208 \r
209 void vCOM_1_Tx_ISR( void )\r
210 {\r
211         /* This can cause a context switch so this macro must be the first line\r
212         in the function. */\r
213         portENTER_SWITCHING_ISR();\r
214 \r
215         /* As this is a switching ISR the local variables must be declared as \r
216         static. */\r
217         static portCHAR cTxByte;\r
218         static signed portBASE_TYPE xTaskWokenByTx;\r
219 \r
220                 /* This variable is static so must be explicitly reinitialised each\r
221                 time the function executes. */\r
222                 xTaskWokenByTx = pdFALSE;\r
223 \r
224                 /* The interrupt was caused by the THR becoming empty.  Are there any\r
225                 more characters to transmit?  Note whether or not the Tx interrupt has\r
226                 woken a task. */\r
227                 if( xQueueReceiveFromISR( xCharsForTx, &cTxByte, &xTaskWokenByTx ) == pdTRUE )\r
228                 {\r
229                         /* A character was retrieved from the queue so can be sent to the\r
230                         THR now. */                                                     \r
231                         TDR1 = cTxByte;\r
232 \r
233                         /* Clear the interrupt. */\r
234                         SSR1 &= ~serTX_INTERRUPT;\r
235                 }\r
236                 else\r
237                 {\r
238                         /* Queue empty, nothing to send so turn off the Tx interrupt. */\r
239                         serTX_INTERRUPT_OFF();\r
240                 }               \r
241 \r
242         /* This must be the last line in the function.  We pass cTaskWokenByTx so \r
243         a context switch will occur if the Tx'ed character woke a task that has\r
244         a priority higher than the task we interrupted. */\r
245         portEXIT_SWITCHING_ISR( xTaskWokenByTx );\r
246 }\r
247 /*-----------------------------------------------------------*/\r
248 \r
249 /*\r
250  * This ISR cannot cause a context switch so requires no special \r
251  * considerations. \r
252  */\r
253 void vCOM_1_Error_ISR( void )\r
254 {\r
255 volatile unsigned portCHAR ucIn;\r
256 \r
257         ucIn = SSR1;\r
258         SSR1 = 0;\r
259 }\r
260 \r