2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 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. 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
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
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
30 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.
\r
33 /* Scheduler includes. */
\r
34 #include "FreeRTOS.h"
\r
38 /* Library includes. */
\r
39 #include "stm32f10x_lib.h"
\r
41 /* Demo application includes. */
\r
43 /*-----------------------------------------------------------*/
\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
50 /*-----------------------------------------------------------*/
\r
52 /* The queue used to hold received characters. */
\r
53 static QueueHandle_t xRxedChars;
\r
54 static QueueHandle_t xCharsForTx;
\r
56 /*-----------------------------------------------------------*/
\r
58 /* UART interrupt handler. */
\r
59 void vUARTInterruptHandler( void );
\r
61 /*-----------------------------------------------------------*/
\r
64 * See the serial2.h header file.
\r
66 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
68 xComPortHandle xReturn;
\r
69 USART_InitTypeDef USART_InitStructure;
\r
70 NVIC_InitTypeDef NVIC_InitStructure;
\r
71 GPIO_InitTypeDef GPIO_InitStructure;
\r
73 /* Create the queues used to hold Rx/Tx characters. */
\r
74 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
75 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
77 /* If the queue/semaphore was created correctly then setup the serial port
\r
79 if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )
\r
81 /* Enable USART1 clock */
\r
82 RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE );
\r
84 /* Configure USART1 Rx (PA10) as input floating */
\r
85 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
\r
86 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
\r
87 GPIO_Init( GPIOA, &GPIO_InitStructure );
\r
89 /* Configure USART1 Tx (PA9) as alternate function push-pull */
\r
90 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
\r
91 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
\r
92 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
\r
93 GPIO_Init( GPIOA, &GPIO_InitStructure );
\r
95 USART_InitStructure.USART_BaudRate = ulWantedBaud;
\r
96 USART_InitStructure.USART_WordLength = USART_WordLength_8b;
\r
97 USART_InitStructure.USART_StopBits = USART_StopBits_1;
\r
98 USART_InitStructure.USART_Parity = USART_Parity_No ;
\r
99 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
\r
100 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
\r
101 USART_InitStructure.USART_Clock = USART_Clock_Disable;
\r
102 USART_InitStructure.USART_CPOL = USART_CPOL_Low;
\r
103 USART_InitStructure.USART_CPHA = USART_CPHA_2Edge;
\r
104 USART_InitStructure.USART_LastBit = USART_LastBit_Disable;
\r
106 USART_Init( USART1, &USART_InitStructure );
\r
108 USART_ITConfig( USART1, USART_IT_RXNE, ENABLE );
\r
110 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
\r
111 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = configLIBRARY_KERNEL_INTERRUPT_PRIORITY;
\r
112 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
\r
113 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
\r
114 NVIC_Init( &NVIC_InitStructure );
\r
116 USART_Cmd( USART1, ENABLE );
\r
120 xReturn = ( xComPortHandle ) 0;
\r
123 /* This demo file only supports a single port but we have to return
\r
124 something to comply with the standard demo header file. */
\r
127 /*-----------------------------------------------------------*/
\r
129 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
131 /* The port handle is not required as this driver only supports one port. */
\r
134 /* Get the next character from the buffer. Return false if no characters
\r
135 are available, or arrive before xBlockTime expires. */
\r
136 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
145 /*-----------------------------------------------------------*/
\r
147 void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
\r
149 signed char *pxNext;
\r
151 /* A couple of parameters that this port does not use. */
\r
152 ( void ) usStringLength;
\r
155 /* NOTE: This implementation does not handle the queue being full as no
\r
156 block time is used! */
\r
158 /* The port handle is not required as this driver only supports UART1. */
\r
161 /* Send each character in the string, one at a time. */
\r
162 pxNext = ( signed char * ) pcString;
\r
165 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
\r
169 /*-----------------------------------------------------------*/
\r
171 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
173 signed portBASE_TYPE xReturn;
\r
175 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdPASS )
\r
178 USART_ITConfig( USART1, USART_IT_TXE, ENABLE );
\r
187 /*-----------------------------------------------------------*/
\r
189 void vSerialClose( xComPortHandle xPort )
\r
191 /* Not supported as not required by the demo application. */
\r
193 /*-----------------------------------------------------------*/
\r
195 void vUARTInterruptHandler( void )
\r
197 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
200 if( USART_GetITStatus( USART1, USART_IT_TXE ) == SET )
\r
202 /* The interrupt was caused by the THR becoming empty. Are there any
\r
203 more characters to transmit? */
\r
204 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
206 /* A character was retrieved from the queue so can be sent to the
\r
208 USART_SendData( USART1, cChar );
\r
212 USART_ITConfig( USART1, USART_IT_TXE, DISABLE );
\r
216 if( USART_GetITStatus( USART1, USART_IT_RXNE ) == SET )
\r
218 cChar = USART_ReceiveData( USART1 );
\r
219 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
222 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r