]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_MB9B500_IAR_Keil/serial.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / CORTEX_MB9B500_IAR_Keil / serial.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 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. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /*\r
30         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.\r
31         \r
32         ***Note*** This example uses queues to send each character into an interrupt\r
33         service routine and out of an interrupt service routine individually.  This\r
34         is done to demonstrate queues being used in an interrupt, and to deliberately\r
35         load the system to test the FreeRTOS port.  It is *NOT* meant to be an\r
36         example of an efficient implementation.  An efficient implementation should\r
37         use FIFOs or DMA if available, and only use FreeRTOS API functions when\r
38         enough has been received to warrant a task being unblocked to process the\r
39         data.\r
40 */\r
41 \r
42 /* Scheduler includes. */\r
43 #include "FreeRTOS.h"\r
44 #include "queue.h"\r
45 #include "semphr.h"\r
46 #include "comtest2.h"\r
47 \r
48 /* Library includes. */\r
49 #include "mb9bf506n.h"\r
50 #include "system_mb9bf50x.h"\r
51 \r
52 /* Demo application includes. */\r
53 #include "serial.h"\r
54 /*-----------------------------------------------------------*/\r
55 \r
56 /* Register bit definitions. */\r
57 #define serRX_INT_ENABLE                0x10\r
58 #define serTX_INT_ENABLE                0x08\r
59 #define serRX_ENABLE                    0x02\r
60 #define serTX_ENABLE                    0x01\r
61 #define serORE_ERROR_BIT                0x08\r
62 #define serFRE_ERROR_BIT                0x10\r
63 #define serPE_ERROR_BIT                 0x20\r
64 #define serRX_INT                               0x04\r
65 #define serTX_INT                               0x02\r
66 \r
67 /* Misc defines. */\r
68 #define serINVALID_QUEUE                                ( ( QueueHandle_t ) 0 )\r
69 #define serNO_BLOCK                                             ( ( TickType_t ) 0 )\r
70 \r
71 /*-----------------------------------------------------------*/\r
72 \r
73 /* The queue used to hold received characters. */\r
74 static QueueHandle_t xRxedChars;\r
75 static QueueHandle_t xCharsForTx;\r
76 \r
77 /*-----------------------------------------------------------*/\r
78 \r
79 /*\r
80  * See the serial2.h header file.\r
81  */\r
82 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
83 {\r
84         /* Create the queues used to hold Rx/Tx characters. */\r
85         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
86         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
87         \r
88         /* If the queues were created correctly then setup the serial port\r
89         hardware. */\r
90         if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )\r
91         {\r
92                 /* Ensure interrupts don't fire during the init process.  Interrupts\r
93                 will be enabled automatically when the first task start running. */\r
94                 portDISABLE_INTERRUPTS();\r
95                 \r
96                 /* Configure P21 and P22 for use by the UART. */\r
97                 FM3_GPIO->PFR2 |= ( 1 << 0x01 ) | ( 1 << 0x02 );\r
98                 \r
99                 /* SIN0_0 and SOT0_0. */\r
100                 FM3_GPIO->EPFR07 |= ( 1 << 6 );\r
101                 \r
102                 /* Reset. */\r
103                 FM3_MFS0_UART->SCR = 0x80;\r
104                 \r
105                 /* Enable output in mode 0. */\r
106                 FM3_MFS0_UART->SMR = 0x01;\r
107                 \r
108                 /* Clear all errors that may already be present. */\r
109                 FM3_MFS0_UART->SSR = 0x00;\r
110                 FM3_MFS0_UART->ESCR = 0x00;\r
111                 \r
112                 FM3_MFS0_UART->BGR = ( configCPU_CLOCK_HZ / 2UL ) / ( ulWantedBaud - 1UL );\r
113 \r
114                 /* Enable Rx, Tx, and the Rx interrupt. */              \r
115                 FM3_MFS0_UART->SCR |= ( serRX_ENABLE | serTX_ENABLE | serRX_INT_ENABLE );\r
116                 \r
117                 /* Configure the NVIC for UART interrupts. */\r
118                 NVIC_ClearPendingIRQ( MFS0RX_IRQn );\r
119                 NVIC_EnableIRQ( MFS0RX_IRQn );\r
120                 \r
121                 /* The priority *MUST* be at or below\r
122                 configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY as FreeRTOS API functions\r
123                 are called in the interrupt handler. */\r
124                 NVIC_SetPriority( MFS0RX_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );\r
125                 \r
126                 /* Do the same for the Tx interrupts. */\r
127                 NVIC_ClearPendingIRQ( MFS0TX_IRQn );\r
128                 NVIC_EnableIRQ( MFS0TX_IRQn );\r
129                 \r
130                 /* The priority *MUST* be at or below\r
131                 configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY as FreeRTOS API functions\r
132                 are called in the interrupt handler. */\r
133                 NVIC_SetPriority( MFS0TX_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );\r
134         }\r
135 \r
136         /* This demo file only supports a single port but we have to return\r
137         something to comply with the standard demo header file. */\r
138         return ( xComPortHandle ) 0;\r
139 }\r
140 /*-----------------------------------------------------------*/\r
141 \r
142 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
143 {\r
144         /* The port handle is not required as this driver only supports one port. */\r
145         ( void ) pxPort;\r
146 \r
147         /* Get the next character from the buffer.  Return false if no characters\r
148         are available, or arrive before xBlockTime expires. */\r
149         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
150         {\r
151                 return pdTRUE;\r
152         }\r
153         else\r
154         {\r
155                 return pdFALSE;\r
156         }\r
157 }\r
158 /*-----------------------------------------------------------*/\r
159 \r
160 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
161 {\r
162 signed char *pxNext;\r
163 \r
164         /* A couple of parameters that this port does not use. */\r
165         ( void ) usStringLength;\r
166         ( void ) pxPort;\r
167 \r
168         /* NOTE: This implementation does not handle the queue being full as no\r
169         block time is used! */\r
170 \r
171         /* The port handle is not required as this driver only supports one UART. */\r
172         ( void ) pxPort;\r
173 \r
174         /* Send each character in the string, one at a time. */\r
175         pxNext = ( signed char * ) pcString;\r
176         while( *pxNext )\r
177         {\r
178                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
179                 pxNext++;\r
180         }\r
181 }\r
182 /*-----------------------------------------------------------*/\r
183 \r
184 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
185 {\r
186 signed portBASE_TYPE xReturn;\r
187 \r
188         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdPASS )\r
189         {\r
190                 xReturn = pdPASS;\r
191                 \r
192                 /* Enable the UART Tx interrupt. */\r
193                 FM3_MFS0_UART->SCR |= serTX_INT_ENABLE;\r
194         }\r
195         else\r
196         {\r
197                 xReturn = pdFAIL;\r
198         }\r
199 \r
200         return xReturn;\r
201 }\r
202 /*-----------------------------------------------------------*/\r
203 \r
204 void vSerialClose( xComPortHandle xPort )\r
205 {\r
206         /* Not supported as not required by the demo application. */\r
207 }\r
208 /*-----------------------------------------------------------*/\r
209 \r
210 void MFS0RX_IRQHandler( void )\r
211 {\r
212 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
213 char cChar;\r
214 \r
215         if( ( FM3_MFS0_UART->SSR & ( serORE_ERROR_BIT | serFRE_ERROR_BIT | serPE_ERROR_BIT ) ) != 0 )\r
216         {\r
217                 /* A PE, ORE or FRE error occurred.  Clear it. */\r
218                 FM3_MFS0_UART->SSR |= ( 1 << 7 );\r
219                 cChar = FM3_MFS0_UART->RDR;\r
220         }\r
221         else if( FM3_MFS0_UART->SSR & serRX_INT )\r
222         {\r
223                 /* A character has been received on the USART, send it to the Rx\r
224                 handler task. */\r
225                 cChar = FM3_MFS0_UART->RDR;\r
226                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
227         }       \r
228 \r
229         /* If sending or receiving from a queue has caused a task to unblock, and\r
230         the unblocked task has a priority equal to or higher than the currently\r
231         running task (the task this ISR interrupted), then xHigherPriorityTaskWoken\r
232         will have automatically been set to pdTRUE within the queue send or receive\r
233         function.  portEND_SWITCHING_ISR() will then ensure that this ISR returns\r
234         directly to the higher priority unblocked task. */\r
235         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
236 }\r
237 /*-----------------------------------------------------------*/\r
238 \r
239 void MFS0TX_IRQHandler( void )\r
240 {\r
241 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
242 char cChar;\r
243 \r
244         if( FM3_MFS0_UART->SSR & serTX_INT )\r
245         {\r
246                 /* The interrupt was caused by the TX register becoming empty.  Are\r
247                 there any more characters to transmit? */\r
248                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )\r
249                 {\r
250                         /* A character was retrieved from the queue so can be sent to the\r
251                         USART now. */\r
252                         FM3_MFS0_UART->TDR = cChar;\r
253                 }\r
254                 else\r
255                 {\r
256                         /* Disable the Tx interrupt. */\r
257                         FM3_MFS0_UART->SCR &= ~serTX_INT_ENABLE;\r
258                 }               \r
259         }       \r
260 \r
261         /* If sending or receiving from a queue has caused a task to unblock, and\r
262         the unblocked task has a priority equal to or higher than the currently\r
263         running task (the task this ISR interrupted), then xHigherPriorityTaskWoken\r
264         will have automatically been set to pdTRUE within the queue send or receive\r
265         function.  portEND_SWITCHING_ISR() will then ensure that this ISR returns\r
266         directly to the higher priority unblocked task. */\r
267         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
268 }\r
269 \r
270 \r
271 \r
272 \r
273 \r
274         \r