2 * FreeRTOS Kernel V10.1.0
\r
3 * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\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
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software.
\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
22 * http://www.FreeRTOS.org
\r
23 * http://aws.amazon.com/freertos
\r
25 * 1 tab == 4 spaces!
\r
29 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART1.
\r
32 /* Library includes. */
\r
33 #include "91x_lib.h"
\r
35 /* Scheduler includes. */
\r
36 #include "FreeRTOS.h"
\r
40 /* Demo application includes. */
\r
42 /*-----------------------------------------------------------*/
\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
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
56 /* The queue used to hold received characters. */
\r
57 static QueueHandle_t xRxedChars;
\r
59 /* The semaphore used to wake a task waiting for space to become available
\r
61 static SemaphoreHandle_t xTxFIFOSemaphore;
\r
63 /*-----------------------------------------------------------*/
\r
65 /* UART interrupt handler. */
\r
66 void UART1_IRQHandler( void );
\r
68 /* The interrupt service routine - called from the assembly entry point. */
\r
69 __arm void UART1_IRQHandler( void );
\r
71 /*-----------------------------------------------------------*/
\r
73 /* Flag to indicate whether or not a task is blocked waiting for space on
\r
75 static long lTaskWaiting = pdFALSE;
\r
78 * See the serial2.h header file.
\r
80 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
82 xComPortHandle xReturn;
\r
83 UART_InitTypeDef xUART1_Init;
\r
84 GPIO_InitTypeDef GPIO_InitStructure;
\r
86 /* Create the queues used to hold Rx characters. */
\r
87 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\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
93 /* If the queue/semaphore was created correctly then setup the serial port
\r
95 if( ( xRxedChars != serINVALID_QUEUE ) && ( xTxFIFOSemaphore != serINVALID_QUEUE ) )
\r
97 /* Pre take the semaphore so a task will block if it tries to access
\r
99 xSemaphoreTake( xTxFIFOSemaphore, 0 );
\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
110 /* Enable the UART1 Clock */
\r
111 SCU_APBPeriphClockConfig( __UART1, ENABLE );
\r
113 /* Enable the GPIO3 Clock */
\r
114 SCU_APBPeriphClockConfig( __GPIO3, ENABLE );
\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
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
133 portENTER_CRITICAL();
\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
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
147 UART_Cmd( UART1, ENABLE );
\r
148 lTaskWaiting = pdFALSE;
\r
150 portEXIT_CRITICAL();
\r
154 xReturn = ( xComPortHandle ) 0;
\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
161 /*-----------------------------------------------------------*/
\r
163 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
165 /* The port handle is not required as this driver only supports one port. */
\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
179 /*-----------------------------------------------------------*/
\r
181 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
183 signed char *pxNext;
\r
185 /* A couple of parameters that this port does not use. */
\r
186 ( void ) usStringLength;
\r
189 /* NOTE: This implementation does not handle the queue being full as no
\r
190 block time is used! */
\r
192 /* The port handle is not required as this driver only supports UART1. */
\r
195 /* Send each character in the string, one at a time. */
\r
196 pxNext = ( signed char * ) pcString;
\r
199 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
\r
203 /*-----------------------------------------------------------*/
\r
205 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
207 portBASE_TYPE xReturn;
\r
209 portENTER_CRITICAL();
\r
211 /* Can we write to the FIFO? */
\r
212 if( UART1->FR & serTX_FIFO_FULL )
\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
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
224 xReturn = xSemaphoreTake( xTxFIFOSemaphore, serTX_BLOCK_TIME );
\r
227 UART1->DR = cOutChar;
\r
232 UART1->DR = cOutChar;
\r
236 portEXIT_CRITICAL();
\r
240 /*-----------------------------------------------------------*/
\r
242 void vSerialClose( xComPortHandle xPort )
\r
244 /* Not supported as not required by the demo application. */
\r
246 /*-----------------------------------------------------------*/
\r
248 void UART1_IRQHandler( void )
\r
251 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
253 while( UART1->RIS & mainRXRIS )
\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
259 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
262 if( UART1->RIS & mainTXRIS )
\r
264 if( lTaskWaiting == pdTRUE )
\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
272 UART1->ICR = mainTXRIS;
\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