]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/H8S/RTOSDemo/serial/serial.c
4b2ec8c84c8abdff35f7917a2909f874db24ad7e
[freertos] / FreeRTOS / Demo / H8S / RTOSDemo / serial / serial.c
1 /*\r
2  * FreeRTOS Kernel V10.3.0\r
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software.\r
14  *\r
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
21  *\r
22  * http://www.FreeRTOS.org\r
23  * http://aws.amazon.com/freertos\r
24  *\r
25  * 1 tab == 4 spaces!\r
26  */\r
27 \r
28 \r
29 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER for port 1.\r
30 \r
31 Note that this driver is written to test the RTOS port and is not intended\r
32 to represent an optimised solution.  In particular no use is made of the DMA\r
33 peripheral. */\r
34 \r
35 /* Standard include files. */\r
36 #include <stdlib.h>\r
37 \r
38 /* Scheduler include files. */\r
39 #include "FreeRTOS.h"\r
40 #include "queue.h"\r
41 #include "task.h"\r
42 \r
43 /* Demo application include files. */\r
44 #include "serial.h"\r
45 \r
46 /* The queues used to communicate between the task code and the interrupt\r
47 service routines. */\r
48 static QueueHandle_t xRxedChars; \r
49 static QueueHandle_t xCharsForTx; \r
50 \r
51 /* Hardware specific constants. */\r
52 #define serTX_INTERRUPT                         ( ( unsigned char ) 0x80 )\r
53 #define serRX_INTERRUPT                         ( ( unsigned char ) 0x40 )\r
54 #define serTX_ENABLE                            ( ( unsigned char ) 0x20 )\r
55 #define serRX_ENABLE                            ( ( unsigned char ) 0x10 )\r
56 \r
57 /* Macros to turn on and off the serial port THRE interrupt while leaving the\r
58 other register bits in their correct state.   The Rx interrupt is always \r
59 enabled. */\r
60 #define serTX_INTERRUPT_ON()            SCR1 = serTX_INTERRUPT | serRX_INTERRUPT | serTX_ENABLE | serRX_ENABLE;                                                                 \r
61 #define serTX_INTERRUPT_OFF()           SCR1 =                                   serRX_INTERRUPT | serTX_ENABLE | serRX_ENABLE;\r
62 \r
63 /* Bit used to switch on the channel 1 serial port in the module stop \r
64 register. */\r
65 #define serMSTP6                                        ( ( unsigned short ) 0x0040 )\r
66 \r
67 /* Interrupt service routines.  Note that the Rx and Tx service routines can \r
68 cause a context switch and are therefore defined with the saveall attribute in\r
69 addition to the interrupt_handler attribute.  See the FreeRTOS.org WEB site \r
70 documentation for a full explanation.*/\r
71 void vCOM_1_Rx_ISR( void ) __attribute__ ( ( saveall, interrupt_handler ) );\r
72 void vCOM_1_Tx_ISR( void ) __attribute__ ( ( saveall, interrupt_handler ) );\r
73 void vCOM_1_Error_ISR( void ) __attribute__ ( ( interrupt_handler ) );\r
74 \r
75 /*-----------------------------------------------------------*/\r
76 \r
77 /*\r
78  * Initialise port 1 for interrupt driven communications.\r
79  */\r
80 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
81 {\r
82         /* Create the queues used to communicate between the tasks and the\r
83         interrupt service routines. */\r
84         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
85         xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
86 \r
87         /* No parity, 8 data bits and 1 stop bit is the default so does not require \r
88         configuration - setup the remains of the hardware. */\r
89         portENTER_CRITICAL();\r
90         {\r
91                 /* Turn channel 1 on. */\r
92                 MSTPCR &= ~serMSTP6;\r
93 \r
94                 /* Enable the channels and the Rx interrupt.  The Tx interrupt is only \r
95                 enabled when data is being transmitted. */\r
96                 SCR1 = serRX_INTERRUPT | serTX_ENABLE | serRX_ENABLE;\r
97 \r
98                 /* Bit rate settings for 22.1184MHz clock only!. */\r
99                 switch( ulWantedBaud )\r
100                 {\r
101                         case 4800       :       BRR1 = 143;\r
102                                                         break;\r
103                         case 9600       :       BRR1 = 71;\r
104                                                         break;\r
105                         case 19200      :       BRR1 = 35;\r
106                                                         break;\r
107                         case 38400      :       BRR1 = 17;\r
108                                                         break;\r
109                         case 57600      :       BRR1 = 11;\r
110                                                         break;\r
111                         case 115200     :       BRR1 = 5;\r
112                                                         break;\r
113                         default         :       BRR1 = 5;\r
114                                                         break;\r
115                 }\r
116         }\r
117         portEXIT_CRITICAL();    \r
118 \r
119         /* Unlike some ports, this driver code does not allow for more than one\r
120         com port.  We therefore don't return a pointer to a port structure and can\r
121         instead just return NULL. */\r
122         return NULL;\r
123 }\r
124 /*-----------------------------------------------------------*/\r
125 \r
126 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
127 {\r
128         /* Get the next character from the buffer queue.  Return false if no characters\r
129         are available, or arrive before xBlockTime expires. */\r
130         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
131         {\r
132                 return pdTRUE;\r
133         }\r
134         else\r
135         {\r
136                 return pdFALSE;\r
137         }\r
138 }\r
139 /*-----------------------------------------------------------*/\r
140 \r
141 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
142 {\r
143 signed portBASE_TYPE xReturn = pdPASS;\r
144 \r
145         /* Return false if after the block time there is no room on the Tx queue. */\r
146         portENTER_CRITICAL();\r
147         {\r
148                 /* Send a character to the queue of characters waiting transmission.\r
149                 The queue is serviced by the Tx ISR. */\r
150                 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )\r
151                 {\r
152                         /* Could not post onto the queue. */\r
153                         xReturn = pdFAIL;\r
154                 }\r
155                 else\r
156                 {\r
157                         /* The message was posted onto the queue so we turn on the Tx\r
158                         interrupt to allow the Tx ISR to remove the character from the\r
159                         queue. */\r
160                         serTX_INTERRUPT_ON();\r
161                 }\r
162         }\r
163         portEXIT_CRITICAL();\r
164 \r
165         return xReturn;\r
166 }\r
167 /*-----------------------------------------------------------*/\r
168 \r
169 void vSerialClose( xComPortHandle xPort )\r
170 {       \r
171         /* Not supported. */\r
172         ( void ) xPort;\r
173 }\r
174 /*-----------------------------------------------------------*/\r
175 \r
176 void vCOM_1_Rx_ISR( void )\r
177 {\r
178         /* This can cause a context switch so this macro must be the first line\r
179         in the function. */\r
180         portENTER_SWITCHING_ISR();\r
181 \r
182         /* As this is a switching ISR the local variables must be declared as \r
183         static. */\r
184         static char cRxByte;\r
185         static portBASE_TYPE xHigherPriorityTaskWoken;\r
186 \r
187                 xHigherPriorityTaskWoken = pdFALSE;\r
188 \r
189                 /* Get the character. */\r
190                 cRxByte = RDR1;\r
191 \r
192                 /* Post the character onto the queue of received characters - noting\r
193                 whether or not this wakes a task. */\r
194                 xQueueSendFromISR( xRxedChars, &cRxByte, &xHigherPriorityTaskWoken );\r
195 \r
196                 /* Clear the interrupt. */\r
197                 SSR1 &= ~serRX_INTERRUPT;\r
198 \r
199         /* This must be the last line in the function.  We pass cTaskWokenByPost so \r
200         a context switch will occur if the received character woke a task that has\r
201         a priority higher than the task we interrupted. */\r
202         portEXIT_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
203 }\r
204 /*-----------------------------------------------------------*/\r
205 \r
206 void vCOM_1_Tx_ISR( void )\r
207 {\r
208         /* This can cause a context switch so this macro must be the first line\r
209         in the function. */\r
210         portENTER_SWITCHING_ISR();\r
211 \r
212         /* As this is a switching ISR the local variables must be declared as \r
213         static. */\r
214         static char cTxByte;\r
215         static signed portBASE_TYPE xTaskWokenByTx;\r
216 \r
217                 /* This variable is static so must be explicitly reinitialised each\r
218                 time the function executes. */\r
219                 xTaskWokenByTx = pdFALSE;\r
220 \r
221                 /* The interrupt was caused by the THR becoming empty.  Are there any\r
222                 more characters to transmit?  Note whether or not the Tx interrupt has\r
223                 woken a task. */\r
224                 if( xQueueReceiveFromISR( xCharsForTx, &cTxByte, &xTaskWokenByTx ) == pdTRUE )\r
225                 {\r
226                         /* A character was retrieved from the queue so can be sent to the\r
227                         THR now. */                                                     \r
228                         TDR1 = cTxByte;\r
229 \r
230                         /* Clear the interrupt. */\r
231                         SSR1 &= ~serTX_INTERRUPT;\r
232                 }\r
233                 else\r
234                 {\r
235                         /* Queue empty, nothing to send so turn off the Tx interrupt. */\r
236                         serTX_INTERRUPT_OFF();\r
237                 }               \r
238 \r
239         /* This must be the last line in the function.  We pass cTaskWokenByTx so \r
240         a context switch will occur if the Tx'ed character woke a task that has\r
241         a priority higher than the task we interrupted. */\r
242         portEXIT_SWITCHING_ISR( xTaskWokenByTx );\r
243 }\r
244 /*-----------------------------------------------------------*/\r
245 \r
246 /*\r
247  * This ISR cannot cause a context switch so requires no special \r
248  * considerations. \r
249  */\r
250 void vCOM_1_Error_ISR( void )\r
251 {\r
252 volatile unsigned char ucIn;\r
253 \r
254         ucIn = SSR1;\r
255         SSR1 = 0;\r
256 }\r
257 \r