]> begriffs open source - cmsis-freertos/blob - Demo/ARM7_LPC2106_GCC/serial/serial.c
Update README.md - branch main is now the base branch
[cmsis-freertos] / Demo / ARM7_LPC2106_GCC / serial / serial.c
1 /*
2  * FreeRTOS V202111.00
3  * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9  * the Software, and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * http://www.FreeRTOS.org
23  * http://aws.amazon.com/freertos
24  *
25  * 1 tab == 4 spaces!
26  */
27
28 /*
29         Changes from V2.4.0
30
31                 + Made serial ISR handling more complete and robust.
32
33         Changes from V2.4.1
34
35                 + Split serial.c into serial.c and serialISR.c.  serial.c can be 
36                   compiled using ARM or THUMB modes.  serialISR.c must always be
37                   compiled in ARM mode.
38                 + Another small change to cSerialPutChar().
39
40         Changed from V2.5.1
41
42                 + In cSerialPutChar() an extra check is made to ensure the post to
43                   the queue was successful if then attempting to retrieve the posted
44                   character.
45
46 */
47
48 /* 
49         BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0. 
50
51         This file contains all the serial port components that can be compiled to
52         either ARM or THUMB mode.  Components that must be compiled to ARM mode are
53         contained in serialISR.c.
54 */
55
56 /* Standard includes. */
57 #include <stdlib.h>
58
59 /* Scheduler includes. */
60 #include "FreeRTOS.h"
61 #include "queue.h"
62 #include "task.h"
63
64 /* Demo application includes. */
65 #include "serial.h"
66
67 /*-----------------------------------------------------------*/
68
69 /* Constants to setup and access the UART. */
70 #define serDLAB                                                 ( ( unsigned char ) 0x80 )
71 #define serENABLE_INTERRUPTS                    ( ( unsigned char ) 0x03 )
72 #define serNO_PARITY                                    ( ( unsigned char ) 0x00 )
73 #define ser1_STOP_BIT                                   ( ( unsigned char ) 0x00 )
74 #define ser8_BIT_CHARS                                  ( ( unsigned char ) 0x03 )
75 #define serFIFO_ON                                              ( ( unsigned char ) 0x01 )
76 #define serCLEAR_FIFO                                   ( ( unsigned char ) 0x06 )
77 #define serWANTED_CLOCK_SCALING                 ( ( unsigned long ) 16 )
78
79 /* Constants to setup and access the VIC. */
80 #define serUART0_VIC_CHANNEL                    ( ( unsigned long ) 0x0006 )
81 #define serUART0_VIC_CHANNEL_BIT                ( ( unsigned long ) 0x0040 )
82 #define serUART0_VIC_ENABLE                             ( ( unsigned long ) 0x0020 )
83 #define serCLEAR_VIC_INTERRUPT                  ( ( unsigned long ) 0 )
84
85 #define serINVALID_QUEUE                                ( ( QueueHandle_t ) 0 )
86 #define serHANDLE                                               ( ( xComPortHandle ) 1 )
87 #define serNO_BLOCK                                             ( ( TickType_t ) 0 )
88
89 /*-----------------------------------------------------------*/
90
91 /* Queues used to hold received characters, and characters waiting to be
92 transmitted. */
93 static QueueHandle_t xRxedChars; 
94 static QueueHandle_t xCharsForTx; 
95
96 /*-----------------------------------------------------------*/
97
98 /* Communication flag between the interrupt service routine and serial API. */
99 static volatile long *plTHREEmpty;
100
101 /* 
102  * The queues are created in serialISR.c as they are used from the ISR.
103  * Obtain references to the queues and THRE Empty flag. 
104  */
105 extern void vSerialISRCreateQueues(     unsigned portBASE_TYPE uxQueueLength, QueueHandle_t *pxRxedChars, QueueHandle_t *pxCharsForTx, long volatile **pplTHREEmptyFlag );
106
107 /*-----------------------------------------------------------*/
108
109 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
110 {
111 unsigned long ulDivisor, ulWantedClock;
112 xComPortHandle xReturn = serHANDLE;
113 extern void ( vUART_ISR_Wrapper )( void );
114
115         /* The queues are used in the serial ISR routine, so are created from
116         serialISR.c (which is always compiled to ARM mode. */
117         vSerialISRCreateQueues( uxQueueLength, &xRxedChars, &xCharsForTx, &plTHREEmpty );
118
119         if( 
120                 ( xRxedChars != serINVALID_QUEUE ) && 
121                 ( xCharsForTx != serINVALID_QUEUE ) && 
122                 ( ulWantedBaud != ( unsigned long ) 0 ) 
123           )
124         {
125                 portENTER_CRITICAL();
126                 {
127                         /* Setup the baud rate:  Calculate the divisor value. */
128                         ulWantedClock = ulWantedBaud * serWANTED_CLOCK_SCALING;
129                         ulDivisor = configCPU_CLOCK_HZ / ulWantedClock;
130
131                         /* Set the DLAB bit so we can access the divisor. */
132                         UART0_LCR |= serDLAB;
133
134                         /* Setup the divisor. */
135                         UART0_DLL = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );
136                         ulDivisor >>= 8;
137                         UART0_DLM = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );
138
139                         /* Turn on the FIFO's and clear the buffers. */
140                         UART0_FCR = ( serFIFO_ON | serCLEAR_FIFO );
141
142                         /* Setup transmission format. */
143                         UART0_LCR = serNO_PARITY | ser1_STOP_BIT | ser8_BIT_CHARS;
144
145                         /* Setup the VIC for the UART. */
146                         VICIntSelect &= ~( serUART0_VIC_CHANNEL_BIT );
147                         VICIntEnable |= serUART0_VIC_CHANNEL_BIT;
148                         VICVectAddr1 = ( long ) vUART_ISR_Wrapper;
149                         VICVectCntl1 = serUART0_VIC_CHANNEL | serUART0_VIC_ENABLE;
150
151                         /* Enable UART0 interrupts. */
152                         UART0_IER |= serENABLE_INTERRUPTS;
153                 }
154                 portEXIT_CRITICAL();
155         }
156         else
157         {
158                 xReturn = ( xComPortHandle ) 0;
159         }
160
161         return xReturn;
162 }
163 /*-----------------------------------------------------------*/
164
165 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
166 {
167         /* The port handle is not required as this driver only supports UART0. */
168         ( void ) pxPort;
169
170         /* Get the next character from the buffer.  Return false if no characters
171         are available, or arrive before xBlockTime expires. */
172         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
173         {
174                 return pdTRUE;
175         }
176         else
177         {
178                 return pdFALSE;
179         }
180 }
181 /*-----------------------------------------------------------*/
182
183 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
184 {
185 signed char *pxNext;
186
187         /* NOTE: This implementation does not handle the queue being full as no
188         block time is used! */
189
190         /* The port handle is not required as this driver only supports UART0. */
191         ( void ) pxPort;
192         ( void ) usStringLength;
193
194         /* Send each character in the string, one at a time. */
195         pxNext = ( signed char * ) pcString;
196         while( *pxNext )
197         {
198                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
199                 pxNext++;
200         }
201 }
202 /*-----------------------------------------------------------*/
203
204 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
205 {
206 signed portBASE_TYPE xReturn;
207
208         /* This demo driver only supports one port so the parameter is not used. */
209         ( void ) pxPort;
210
211         portENTER_CRITICAL();
212         {
213                 /* Is there space to write directly to the UART? */
214                 if( *plTHREEmpty == ( long ) pdTRUE )
215                 {
216                         /* We wrote the character directly to the UART, so was 
217                         successful. */
218                         *plTHREEmpty = pdFALSE;
219                         UART0_THR = cOutChar;
220                         xReturn = pdPASS;
221                 }
222                 else 
223                 {
224                         /* We cannot write directly to the UART, so queue the character.
225                         Block for a maximum of xBlockTime if there is no space in the
226                         queue. */
227                         xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );
228
229                         /* Depending on queue sizing and task prioritisation:  While we 
230                         were blocked waiting to post interrupts were not disabled.  It is 
231                         possible that the serial ISR has emptied the Tx queue, in which
232                         case we need to start the Tx off again. */
233                         if( ( *plTHREEmpty == ( long ) pdTRUE ) && ( xReturn == pdPASS ) )
234                         {
235                                 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );
236                                 *plTHREEmpty = pdFALSE;
237                                 UART0_THR = cOutChar;
238                         }
239                 }
240         }
241         portEXIT_CRITICAL();
242
243         return xReturn;
244 }
245 /*-----------------------------------------------------------*/
246
247 void vSerialClose( xComPortHandle xPort )
248 {
249         /* Not supported as not required by the demo application. */
250         ( void ) xPort;
251 }
252 /*-----------------------------------------------------------*/
253
254
255
256
257
258