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