]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM9_STR91X_IAR/serial/serial.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / ARM9_STR91X_IAR / serial / 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 UART1.\r
31 */\r
32 \r
33 /* Library includes. */\r
34 #include "91x_lib.h"\r
35 \r
36 /* Scheduler includes. */\r
37 #include "FreeRTOS.h"\r
38 #include "queue.h"\r
39 #include "semphr.h"\r
40 \r
41 /* Demo application includes. */\r
42 #include "serial.h"\r
43 /*-----------------------------------------------------------*/\r
44 \r
45 /* Misc defines. */\r
46 #define serINVALID_QUEUE                                ( ( QueueHandle_t ) 0 )\r
47 #define serNO_BLOCK                                             ( ( TickType_t ) 0 )\r
48 #define serTX_BLOCK_TIME                                ( 40 / portTICK_PERIOD_MS )\r
49 \r
50 /* Interrupt and status bit definitions. */\r
51 #define mainTXRIS 0x20  \r
52 #define mainRXRIS 0x50\r
53 #define serTX_FIFO_FULL 0x20\r
54 #define serCLEAR_ALL_INTERRUPTS 0x3ff\r
55 /*-----------------------------------------------------------*/\r
56 \r
57 /* The queue used to hold received characters. */\r
58 static QueueHandle_t xRxedChars;\r
59 \r
60 /* The semaphore used to wake a task waiting for space to become available\r
61 in the FIFO. */\r
62 static SemaphoreHandle_t xTxFIFOSemaphore;\r
63 \r
64 /*-----------------------------------------------------------*/\r
65 \r
66 /* UART interrupt handler. */\r
67 void UART1_IRQHandler( void );\r
68 \r
69 /* The interrupt service routine - called from the assembly entry point. */\r
70 __arm void UART1_IRQHandler( void );\r
71 \r
72 /*-----------------------------------------------------------*/\r
73 \r
74 /* Flag to indicate whether or not a task is blocked waiting for space on\r
75 the FIFO. */\r
76 static long lTaskWaiting = pdFALSE;\r
77 \r
78 /*\r
79  * See the serial2.h header file.\r
80  */\r
81 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )\r
82 {\r
83 xComPortHandle xReturn;\r
84 UART_InitTypeDef xUART1_Init;\r
85 GPIO_InitTypeDef GPIO_InitStructure;\r
86         \r
87         /* Create the queues used to hold Rx characters. */\r
88         xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );\r
89         \r
90         /* Create the semaphore used to wake a task waiting for space to become\r
91         available in the FIFO. */\r
92         vSemaphoreCreateBinary( xTxFIFOSemaphore );\r
93 \r
94         /* If the queue/semaphore was created correctly then setup the serial port\r
95         hardware. */\r
96         if( ( xRxedChars != serINVALID_QUEUE ) && ( xTxFIFOSemaphore != serINVALID_QUEUE ) )\r
97         {\r
98                 /* Pre take the semaphore so a task will block if it tries to access\r
99                 it. */\r
100                 xSemaphoreTake( xTxFIFOSemaphore, 0 );\r
101                 \r
102                 /* Configure the UART. */\r
103                 xUART1_Init.UART_WordLength = UART_WordLength_8D;\r
104                 xUART1_Init.UART_StopBits = UART_StopBits_1;\r
105                 xUART1_Init.UART_Parity = UART_Parity_No;\r
106                 xUART1_Init.UART_BaudRate = ulWantedBaud;\r
107                 xUART1_Init.UART_HardwareFlowControl = UART_HardwareFlowControl_None;\r
108                 xUART1_Init.UART_Mode = UART_Mode_Tx_Rx;\r
109                 xUART1_Init.UART_FIFO = UART_FIFO_Enable;\r
110 \r
111                 /* Enable the UART1 Clock */\r
112                 SCU_APBPeriphClockConfig( __UART1, ENABLE );\r
113                 \r
114                 /* Enable the GPIO3 Clock */\r
115                 SCU_APBPeriphClockConfig( __GPIO3, ENABLE );\r
116                 \r
117                 /* Configure UART1_Rx pin GPIO3.2 */\r
118                 GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;\r
119                 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;\r
120                 GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;\r
121                 GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;\r
122                 GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1 ;\r
123                 GPIO_Init( GPIO3, &GPIO_InitStructure );\r
124                 \r
125                 /* Configure UART1_Tx pin GPIO3.3 */\r
126                 GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;\r
127                 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;\r
128                 GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;\r
129                 GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;\r
130                 GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt2 ;\r
131                 GPIO_Init( GPIO3, &GPIO_InitStructure );\r
132                 \r
133                 \r
134                 portENTER_CRITICAL();\r
135                 {               \r
136                         /* Configure the UART itself. */\r
137                         UART_DeInit( UART1 );           \r
138                         UART_Init( UART1, &xUART1_Init );\r
139                         UART_ITConfig( UART1, UART_IT_Receive | UART_IT_Transmit, ENABLE );\r
140                         UART1->ICR = serCLEAR_ALL_INTERRUPTS;\r
141                         UART_LoopBackConfig( UART1, DISABLE );\r
142                         UART_IrDACmd( IrDA1, DISABLE );\r
143 \r
144                         /* Configure the VIC for the UART interrupts. */                        \r
145                         VIC_Config( UART1_ITLine, VIC_IRQ, 9 );\r
146                         VIC_ITCmd( UART1_ITLine, ENABLE );\r
147 \r
148                         UART_Cmd( UART1, ENABLE );                      \r
149                         lTaskWaiting = pdFALSE;\r
150                 }\r
151                 portEXIT_CRITICAL();\r
152         }\r
153         else\r
154         {\r
155                 xReturn = ( xComPortHandle ) 0;\r
156         }\r
157 \r
158         /* This demo file only supports a single port but we have to return\r
159         something to comply with the standard demo header file. */\r
160         return xReturn;\r
161 }\r
162 /*-----------------------------------------------------------*/\r
163 \r
164 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )\r
165 {\r
166         /* The port handle is not required as this driver only supports one port. */\r
167         ( void ) pxPort;\r
168 \r
169         /* Get the next character from the buffer.  Return false if no characters\r
170         are available, or arrive before xBlockTime expires. */\r
171         if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )\r
172         {\r
173                 return pdTRUE;\r
174         }\r
175         else\r
176         {\r
177                 return pdFALSE;\r
178         }\r
179 }\r
180 /*-----------------------------------------------------------*/\r
181 \r
182 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )\r
183 {\r
184 signed char *pxNext;\r
185 \r
186         /* A couple of parameters that this port does not use. */\r
187         ( void ) usStringLength;\r
188         ( void ) pxPort;\r
189 \r
190         /* NOTE: This implementation does not handle the queue being full as no\r
191         block time is used! */\r
192 \r
193         /* The port handle is not required as this driver only supports UART1. */\r
194         ( void ) pxPort;\r
195 \r
196         /* Send each character in the string, one at a time. */\r
197         pxNext = ( signed char * ) pcString;\r
198         while( *pxNext )\r
199         {\r
200                 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );\r
201                 pxNext++;\r
202         }\r
203 }\r
204 /*-----------------------------------------------------------*/\r
205 \r
206 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )\r
207 {\r
208 portBASE_TYPE xReturn;\r
209 \r
210         portENTER_CRITICAL();\r
211         {\r
212                 /* Can we write to the FIFO? */\r
213                 if( UART1->FR & serTX_FIFO_FULL )\r
214                 {\r
215                         /* Wait for the interrupt letting us know there is space on the\r
216                         FIFO.  It is ok to block in a critical section, interrupts will be\r
217                         enabled for other tasks once we force a switch. */\r
218                         lTaskWaiting = pdTRUE;\r
219                         \r
220                         /* Just to be a bit different this driver uses a semaphore to\r
221                         block the sending task when the FIFO is full.  The standard COMTest\r
222                         task assumes a queue of adequate length exists so does not use\r
223                         a block time.  For this demo the block time is therefore hard\r
224                         coded. */\r
225                         xReturn = xSemaphoreTake( xTxFIFOSemaphore, serTX_BLOCK_TIME );\r
226                         if( xReturn )\r
227                         {\r
228                                 UART1->DR = cOutChar;\r
229                         }\r
230                 }\r
231                 else\r
232                 {\r
233                         UART1->DR = cOutChar;\r
234                         xReturn = pdPASS;\r
235                 }\r
236         }\r
237         portEXIT_CRITICAL();\r
238 \r
239         return xReturn;\r
240 }\r
241 /*-----------------------------------------------------------*/\r
242 \r
243 void vSerialClose( xComPortHandle xPort )\r
244 {\r
245         /* Not supported as not required by the demo application. */\r
246 }\r
247 /*-----------------------------------------------------------*/\r
248 \r
249 void UART1_IRQHandler( void )\r
250 {\r
251 signed char cChar;\r
252 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
253 \r
254         while( UART1->RIS &     mainRXRIS )\r
255         {\r
256                 /* The interrupt was caused by a character being received.  Grab the\r
257                 character from the DR and place it in the queue of received\r
258                 characters. */\r
259                 cChar = UART1->DR;\r
260                 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );\r
261         }       \r
262         \r
263         if( UART1->RIS & mainTXRIS )\r
264         {\r
265                 if( lTaskWaiting == pdTRUE )\r
266                 {\r
267                         /* This interrupt was caused by space becoming available on the Tx\r
268                         FIFO, wake any task that is waiting to post (if any). */\r
269                         xSemaphoreGiveFromISR( xTxFIFOSemaphore, &xHigherPriorityTaskWoken );\r
270                         lTaskWaiting = pdFALSE;\r
271                 }\r
272                 \r
273                 UART1->ICR = mainTXRIS;\r
274         }\r
275 \r
276         /* If a task was woken by either a character being received or a character\r
277         being transmitted then we may need to switch to another task. */\r
278         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
279 }\r
280 \r
281 \r
282 \r
283 \r
284 \r
285         \r