]> begriffs open source - cmsis-freertos/blob - Demo/CORTEX_MB9B500_IAR_Keil/serial.c
Update cmsis_os2.c
[cmsis-freertos] / Demo / CORTEX_MB9B500_IAR_Keil / serial.c
1 /*
2     FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
3     All rights reserved
4
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
6
7     This file is part of the FreeRTOS distribution.
8
9     FreeRTOS is free software; you can redistribute it and/or modify it under
10     the terms of the GNU General Public License (version 2) as published by the
11     Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
12
13     ***************************************************************************
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<
16     >>!   obliged to provide the source code for proprietary components     !<<
17     >>!   outside of the FreeRTOS kernel.                                   !<<
18     ***************************************************************************
19
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following
23     link: http://www.freertos.org/a00114.html
24
25     ***************************************************************************
26      *                                                                       *
27      *    FreeRTOS provides completely free yet professionally developed,    *
28      *    robust, strictly quality controlled, supported, and cross          *
29      *    platform software that is more than just the market leader, it     *
30      *    is the industry's de facto standard.                               *
31      *                                                                       *
32      *    Help yourself get started quickly while simultaneously helping     *
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *
34      *    tutorial book, reference manual, or both:                          *
35      *    http://www.FreeRTOS.org/Documentation                              *
36      *                                                                       *
37     ***************************************************************************
38
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading
40     the FAQ page "My application does not run, what could be wrong?".  Have you
41     defined configASSERT()?
42
43     http://www.FreeRTOS.org/support - In return for receiving this top quality
44     embedded software for free we request you assist our global community by
45     participating in the support forum.
46
47     http://www.FreeRTOS.org/training - Investing in training allows your team to
48     be as productive as possible as early as possible.  Now you can receive
49     FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50     Ltd, and the world's leading authority on the world's leading RTOS.
51
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.
55
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
58
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS
61     licenses offer ticketed support, indemnification and commercial middleware.
62
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64     engineered and independently SIL3 certified version for use in safety and
65     mission critical applications that require provable dependability.
66
67     1 tab == 4 spaces!
68 */
69
70 /*
71         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.
72         
73         ***Note*** This example uses queues to send each character into an interrupt
74         service routine and out of an interrupt service routine individually.  This
75         is done to demonstrate queues being used in an interrupt, and to deliberately
76         load the system to test the FreeRTOS port.  It is *NOT* meant to be an
77         example of an efficient implementation.  An efficient implementation should
78         use FIFOs or DMA if available, and only use FreeRTOS API functions when
79         enough has been received to warrant a task being unblocked to process the
80         data.
81 */
82
83 /* Scheduler includes. */
84 #include "FreeRTOS.h"
85 #include "queue.h"
86 #include "semphr.h"
87 #include "comtest2.h"
88
89 /* Library includes. */
90 #include "mb9bf506n.h"
91 #include "system_mb9bf50x.h"
92
93 /* Demo application includes. */
94 #include "serial.h"
95 /*-----------------------------------------------------------*/
96
97 /* Register bit definitions. */
98 #define serRX_INT_ENABLE                0x10
99 #define serTX_INT_ENABLE                0x08
100 #define serRX_ENABLE                    0x02
101 #define serTX_ENABLE                    0x01
102 #define serORE_ERROR_BIT                0x08
103 #define serFRE_ERROR_BIT                0x10
104 #define serPE_ERROR_BIT                 0x20
105 #define serRX_INT                               0x04
106 #define serTX_INT                               0x02
107
108 /* Misc defines. */
109 #define serINVALID_QUEUE                                ( ( QueueHandle_t ) 0 )
110 #define serNO_BLOCK                                             ( ( TickType_t ) 0 )
111
112 /*-----------------------------------------------------------*/
113
114 /* The queue used to hold received characters. */
115 static QueueHandle_t xRxedChars;
116 static QueueHandle_t xCharsForTx;
117
118 /*-----------------------------------------------------------*/
119
120 /*
121  * See the serial2.h header file.
122  */
123 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
124 {
125         /* Create the queues used to hold Rx/Tx characters. */
126         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
127         xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
128         
129         /* If the queues were created correctly then setup the serial port
130         hardware. */
131         if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )
132         {
133                 /* Ensure interrupts don't fire during the init process.  Interrupts
134                 will be enabled automatically when the first task start running. */
135                 portDISABLE_INTERRUPTS();
136                 
137                 /* Configure P21 and P22 for use by the UART. */
138                 FM3_GPIO->PFR2 |= ( 1 << 0x01 ) | ( 1 << 0x02 );
139                 
140                 /* SIN0_0 and SOT0_0. */
141                 FM3_GPIO->EPFR07 |= ( 1 << 6 );
142                 
143                 /* Reset. */
144                 FM3_MFS0_UART->SCR = 0x80;
145                 
146                 /* Enable output in mode 0. */
147                 FM3_MFS0_UART->SMR = 0x01;
148                 
149                 /* Clear all errors that may already be present. */
150                 FM3_MFS0_UART->SSR = 0x00;
151                 FM3_MFS0_UART->ESCR = 0x00;
152                 
153                 FM3_MFS0_UART->BGR = ( configCPU_CLOCK_HZ / 2UL ) / ( ulWantedBaud - 1UL );
154
155                 /* Enable Rx, Tx, and the Rx interrupt. */              
156                 FM3_MFS0_UART->SCR |= ( serRX_ENABLE | serTX_ENABLE | serRX_INT_ENABLE );
157                 
158                 /* Configure the NVIC for UART interrupts. */
159                 NVIC_ClearPendingIRQ( MFS0RX_IRQn );
160                 NVIC_EnableIRQ( MFS0RX_IRQn );
161                 
162                 /* The priority *MUST* be at or below
163                 configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY as FreeRTOS API functions
164                 are called in the interrupt handler. */
165                 NVIC_SetPriority( MFS0RX_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
166                 
167                 /* Do the same for the Tx interrupts. */
168                 NVIC_ClearPendingIRQ( MFS0TX_IRQn );
169                 NVIC_EnableIRQ( MFS0TX_IRQn );
170                 
171                 /* The priority *MUST* be at or below
172                 configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY as FreeRTOS API functions
173                 are called in the interrupt handler. */
174                 NVIC_SetPriority( MFS0TX_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
175         }
176
177         /* This demo file only supports a single port but we have to return
178         something to comply with the standard demo header file. */
179         return ( xComPortHandle ) 0;
180 }
181 /*-----------------------------------------------------------*/
182
183 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
184 {
185         /* The port handle is not required as this driver only supports one port. */
186         ( void ) pxPort;
187
188         /* Get the next character from the buffer.  Return false if no characters
189         are available, or arrive before xBlockTime expires. */
190         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
191         {
192                 return pdTRUE;
193         }
194         else
195         {
196                 return pdFALSE;
197         }
198 }
199 /*-----------------------------------------------------------*/
200
201 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
202 {
203 signed char *pxNext;
204
205         /* A couple of parameters that this port does not use. */
206         ( void ) usStringLength;
207         ( void ) pxPort;
208
209         /* NOTE: This implementation does not handle the queue being full as no
210         block time is used! */
211
212         /* The port handle is not required as this driver only supports one UART. */
213         ( void ) pxPort;
214
215         /* Send each character in the string, one at a time. */
216         pxNext = ( signed char * ) pcString;
217         while( *pxNext )
218         {
219                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
220                 pxNext++;
221         }
222 }
223 /*-----------------------------------------------------------*/
224
225 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
226 {
227 signed portBASE_TYPE xReturn;
228
229         if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdPASS )
230         {
231                 xReturn = pdPASS;
232                 
233                 /* Enable the UART Tx interrupt. */
234                 FM3_MFS0_UART->SCR |= serTX_INT_ENABLE;
235         }
236         else
237         {
238                 xReturn = pdFAIL;
239         }
240
241         return xReturn;
242 }
243 /*-----------------------------------------------------------*/
244
245 void vSerialClose( xComPortHandle xPort )
246 {
247         /* Not supported as not required by the demo application. */
248 }
249 /*-----------------------------------------------------------*/
250
251 void MFS0RX_IRQHandler( void )
252 {
253 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
254 char cChar;
255
256         if( ( FM3_MFS0_UART->SSR & ( serORE_ERROR_BIT | serFRE_ERROR_BIT | serPE_ERROR_BIT ) ) != 0 )
257         {
258                 /* A PE, ORE or FRE error occurred.  Clear it. */
259                 FM3_MFS0_UART->SSR |= ( 1 << 7 );
260                 cChar = FM3_MFS0_UART->RDR;
261         }
262         else if( FM3_MFS0_UART->SSR & serRX_INT )
263         {
264                 /* A character has been received on the USART, send it to the Rx
265                 handler task. */
266                 cChar = FM3_MFS0_UART->RDR;
267                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
268         }       
269
270         /* If sending or receiving from a queue has caused a task to unblock, and
271         the unblocked task has a priority equal to or higher than the currently
272         running task (the task this ISR interrupted), then xHigherPriorityTaskWoken
273         will have automatically been set to pdTRUE within the queue send or receive
274         function.  portEND_SWITCHING_ISR() will then ensure that this ISR returns
275         directly to the higher priority unblocked task. */
276         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
277 }
278 /*-----------------------------------------------------------*/
279
280 void MFS0TX_IRQHandler( void )
281 {
282 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
283 char cChar;
284
285         if( FM3_MFS0_UART->SSR & serTX_INT )
286         {
287                 /* The interrupt was caused by the TX register becoming empty.  Are
288                 there any more characters to transmit? */
289                 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
290                 {
291                         /* A character was retrieved from the queue so can be sent to the
292                         USART now. */
293                         FM3_MFS0_UART->TDR = cChar;
294                 }
295                 else
296                 {
297                         /* Disable the Tx interrupt. */
298                         FM3_MFS0_UART->SCR &= ~serTX_INT_ENABLE;
299                 }               
300         }       
301
302         /* If sending or receiving from a queue has caused a task to unblock, and
303         the unblocked task has a priority equal to or higher than the currently
304         running task (the task this ISR interrupted), then xHigherPriorityTaskWoken
305         will have automatically been set to pdTRUE within the queue send or receive
306         function.  portEND_SWITCHING_ISR() will then ensure that this ISR returns
307         directly to the higher priority unblocked task. */
308         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
309 }
310
311
312
313
314
315