]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/H8S/RTOSDemo/serial/serial.c
79c8f17fa1001500696072025888c7271ca6465f
[freertos] / FreeRTOS / Demo / H8S / RTOSDemo / serial / serial.c
1 /*\r
2     FreeRTOS V7.5.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\r
8      *    FreeRTOS provides completely free yet professionally developed,    *\r
9      *    robust, strictly quality controlled, supported, and cross          *\r
10      *    platform software that has become a de facto standard.             *\r
11      *                                                                       *\r
12      *    Help yourself get started quickly and support the FreeRTOS         *\r
13      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
14      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \r
22     FreeRTOS is free software; you can redistribute it and/or modify it under\r
23     the terms of the GNU General Public License (version 2) as published by the\r
24     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
25 \r
26     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
27     >>! a combined work that includes FreeRTOS without being obliged to provide\r
28     >>! the source code for proprietary components outside of the FreeRTOS\r
29     >>! kernel.\r
30 \r
31     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
32     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
33     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
34     link: http://www.freertos.org/a00114.html\r
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \r
50     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
51     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
52     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
53 \r
54     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
55     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
56     licenses offer ticketed support, indemnification and middleware.\r
57 \r
58     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
59     engineered and independently SIL3 certified version for use in safety and\r
60     mission critical applications that require provable dependability.\r
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 \r
66 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER for port 1.\r
67 \r
68 Note that this driver is written to test the RTOS port and is not intended\r
69 to represent an optimised solution.  In particular no use is made of the DMA\r
70 peripheral. */\r
71 \r
72 /* Standard include files. */\r
73 #include <stdlib.h>\r
74 \r
75 /* Scheduler include files. */\r
76 #include "FreeRTOS.h"\r
77 #include "queue.h"\r
78 #include "task.h"\r
79 \r
80 /* Demo application include files. */\r
81 #include "serial.h"\r
82 \r
83 /* The queues used to communicate between the task code and the interrupt\r
84 service routines. */\r
85 static xQueueHandle xRxedChars; \r
86 static xQueueHandle xCharsForTx; \r
87 \r
88 /* Hardware specific constants. */\r
89 #define serTX_INTERRUPT                         ( ( unsigned char ) 0x80 )\r
90 #define serRX_INTERRUPT                         ( ( unsigned char ) 0x40 )\r
91 #define serTX_ENABLE                            ( ( unsigned char ) 0x20 )\r
92 #define serRX_ENABLE                            ( ( unsigned char ) 0x10 )\r
93 \r
94 /* Macros to turn on and off the serial port THRE interrupt while leaving the\r
95 other register bits in their correct state.   The Rx interrupt is always \r
96 enabled. */\r
97 #define serTX_INTERRUPT_ON()            SCR1 = serTX_INTERRUPT | serRX_INTERRUPT | serTX_ENABLE | serRX_ENABLE;                                                                 \r
98 #define serTX_INTERRUPT_OFF()           SCR1 =                                   serRX_INTERRUPT | serTX_ENABLE | serRX_ENABLE;\r
99 \r
100 /* Bit used to switch on the channel 1 serial port in the module stop \r
101 register. */\r
102 #define serMSTP6                                        ( ( unsigned short ) 0x0040 )\r
103 \r
104 /* Interrupt service routines.  Note that the Rx and Tx service routines can \r
105 cause a context switch and are therefore defined with the saveall attribute in\r
106 addition to the interrupt_handler attribute.  See the FreeRTOS.org WEB site \r
107 documentation for a full explanation.*/\r
108 void vCOM_1_Rx_ISR( void ) __attribute__ ( ( saveall, interrupt_handler ) );\r
109 void vCOM_1_Tx_ISR( void ) __attribute__ ( ( saveall, interrupt_handler ) );\r
110 void vCOM_1_Error_ISR( void ) __attribute__ ( ( interrupt_handler ) );\r
111 \r
112 /*-----------------------------------------------------------*/\r
113 \r
114 /*\r
115  * Initialise port 1 for interrupt driven communications.\r
116  */\r
117 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
118 {\r
119         /* Create the queues used to communicate between the tasks and the\r
120         interrupt service routines. */\r
121         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
122         xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
123 \r
124         /* No parity, 8 data bits and 1 stop bit is the default so does not require \r
125         configuration - setup the remains of the hardware. */\r
126         portENTER_CRITICAL();\r
127         {\r
128                 /* Turn channel 1 on. */\r
129                 MSTPCR &= ~serMSTP6;\r
130 \r
131                 /* Enable the channels and the Rx interrupt.  The Tx interrupt is only \r
132                 enabled when data is being transmitted. */\r
133                 SCR1 = serRX_INTERRUPT | serTX_ENABLE | serRX_ENABLE;\r
134 \r
135                 /* Bit rate settings for 22.1184MHz clock only!. */\r
136                 switch( ulWantedBaud )\r
137                 {\r
138                         case 4800       :       BRR1 = 143;\r
139                                                         break;\r
140                         case 9600       :       BRR1 = 71;\r
141                                                         break;\r
142                         case 19200      :       BRR1 = 35;\r
143                                                         break;\r
144                         case 38400      :       BRR1 = 17;\r
145                                                         break;\r
146                         case 57600      :       BRR1 = 11;\r
147                                                         break;\r
148                         case 115200     :       BRR1 = 5;\r
149                                                         break;\r
150                         default         :       BRR1 = 5;\r
151                                                         break;\r
152                 }\r
153         }\r
154         portEXIT_CRITICAL();    \r
155 \r
156         /* Unlike some ports, this driver code does not allow for more than one\r
157         com port.  We therefore don't return a pointer to a port structure and can\r
158         instead just return NULL. */\r
159         return NULL;\r
160 }\r
161 /*-----------------------------------------------------------*/\r
162 \r
163 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )\r
164 {\r
165         /* Get the next character from the buffer queue.  Return false if no characters\r
166         are available, or arrive before xBlockTime expires. */\r
167         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
168         {\r
169                 return pdTRUE;\r
170         }\r
171         else\r
172         {\r
173                 return pdFALSE;\r
174         }\r
175 }\r
176 /*-----------------------------------------------------------*/\r
177 \r
178 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )\r
179 {\r
180 signed portBASE_TYPE xReturn = pdPASS;\r
181 \r
182         /* Return false if after the block time there is no room on the Tx queue. */\r
183         portENTER_CRITICAL();\r
184         {\r
185                 /* Send a character to the queue of characters waiting transmission.\r
186                 The queue is serviced by the Tx ISR. */\r
187                 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
188                 {\r
189                         /* Could not post onto the queue. */\r
190                         xReturn = pdFAIL;\r
191                 }\r
192                 else\r
193                 {\r
194                         /* The message was posted onto the queue so we turn on the Tx\r
195                         interrupt to allow the Tx ISR to remove the character from the\r
196                         queue. */\r
197                         serTX_INTERRUPT_ON();\r
198                 }\r
199         }\r
200         portEXIT_CRITICAL();\r
201 \r
202         return xReturn;\r
203 }\r
204 /*-----------------------------------------------------------*/\r
205 \r
206 void vSerialClose( xComPortHandle xPort )\r
207 {       \r
208         /* Not supported. */\r
209         ( void ) xPort;\r
210 }\r
211 /*-----------------------------------------------------------*/\r
212 \r
213 void vCOM_1_Rx_ISR( void )\r
214 {\r
215         /* This can cause a context switch so this macro must be the first line\r
216         in the function. */\r
217         portENTER_SWITCHING_ISR();\r
218 \r
219         /* As this is a switching ISR the local variables must be declared as \r
220         static. */\r
221         static char cRxByte;\r
222         static portBASE_TYPE xHigherPriorityTaskWoken;\r
223 \r
224                 xHigherPriorityTaskWoken = pdFALSE;\r
225 \r
226                 /* Get the character. */\r
227                 cRxByte = RDR1;\r
228 \r
229                 /* Post the character onto the queue of received characters - noting\r
230                 whether or not this wakes a task. */\r
231                 xQueueSendFromISR( xRxedChars, &cRxByte, &xHigherPriorityTaskWoken );\r
232 \r
233                 /* Clear the interrupt. */\r
234                 SSR1 &= ~serRX_INTERRUPT;\r
235 \r
236         /* This must be the last line in the function.  We pass cTaskWokenByPost so \r
237         a context switch will occur if the received character woke a task that has\r
238         a priority higher than the task we interrupted. */\r
239         portEXIT_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
240 }\r
241 /*-----------------------------------------------------------*/\r
242 \r
243 void vCOM_1_Tx_ISR( void )\r
244 {\r
245         /* This can cause a context switch so this macro must be the first line\r
246         in the function. */\r
247         portENTER_SWITCHING_ISR();\r
248 \r
249         /* As this is a switching ISR the local variables must be declared as \r
250         static. */\r
251         static char cTxByte;\r
252         static signed portBASE_TYPE xTaskWokenByTx;\r
253 \r
254                 /* This variable is static so must be explicitly reinitialised each\r
255                 time the function executes. */\r
256                 xTaskWokenByTx = pdFALSE;\r
257 \r
258                 /* The interrupt was caused by the THR becoming empty.  Are there any\r
259                 more characters to transmit?  Note whether or not the Tx interrupt has\r
260                 woken a task. */\r
261                 if( xQueueReceiveFromISR( xCharsForTx, &cTxByte, &xTaskWokenByTx ) == pdTRUE )\r
262                 {\r
263                         /* A character was retrieved from the queue so can be sent to the\r
264                         THR now. */                                                     \r
265                         TDR1 = cTxByte;\r
266 \r
267                         /* Clear the interrupt. */\r
268                         SSR1 &= ~serTX_INTERRUPT;\r
269                 }\r
270                 else\r
271                 {\r
272                         /* Queue empty, nothing to send so turn off the Tx interrupt. */\r
273                         serTX_INTERRUPT_OFF();\r
274                 }               \r
275 \r
276         /* This must be the last line in the function.  We pass cTaskWokenByTx so \r
277         a context switch will occur if the Tx'ed character woke a task that has\r
278         a priority higher than the task we interrupted. */\r
279         portEXIT_SWITCHING_ISR( xTaskWokenByTx );\r
280 }\r
281 /*-----------------------------------------------------------*/\r
282 \r
283 /*\r
284  * This ISR cannot cause a context switch so requires no special \r
285  * considerations. \r
286  */\r
287 void vCOM_1_Error_ISR( void )\r
288 {\r
289 volatile unsigned char ucIn;\r
290 \r
291         ucIn = SSR1;\r
292         SSR1 = 0;\r
293 }\r
294 \r