2 * FreeRTOS Kernel V10.3.0
\r
3 * Copyright (C) 2020 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 INTERRUPT DRIVEN SERIAL PORT DRIVER.
\r
33 /******************************************************************************
\r
34 *** NOTE: COM0 == USART1, COM1 == USART2
\r
35 ******************************************************************************/
\r
38 /* Scheduler includes. */
\r
39 #include "FreeRTOS.h"
\r
44 /* Library includes. */
\r
45 #include "stm32f10x_lib.h"
\r
47 /* Driver includes. */
\r
48 #include "STM32_USART.h"
\r
49 /*-----------------------------------------------------------*/
\r
51 /* The number of COM ports that can be controlled at the same time. */
\r
52 #define serNUM_COM_PORTS ( 2 )
\r
54 /* Queues are used to hold characters that are waiting to be transmitted. This
\r
55 constant sets the maximum number of characters that can be contained in such a
\r
56 queue at any one time. */
\r
57 #define serTX_QUEUE_LEN ( 100 )
\r
59 /* Queues are used to hold characters that have been received but not yet
\r
60 processed. This constant sets the maximum number of characters that can be
\r
61 contained in such a queue. */
\r
62 #define serRX_QUEUE_LEN ( 100 )
\r
64 /* The maximum amount of time that calls to lSerialPutString() should wait for
\r
65 there to be space to post each character to the queue of characters waiting
\r
66 transmission. NOTE! This is the time to wait per character - not the time to
\r
67 wait for the entire string. */
\r
68 #define serPUT_STRING_CHAR_DELAY ( 5 / portTICK_PERIOD_MS )
\r
70 /*-----------------------------------------------------------*/
\r
72 /* References to the USART peripheral addresses themselves. */
\r
73 static USART_TypeDef * const xUARTS[ serNUM_COM_PORTS ] = { ( ( USART_TypeDef * ) USART1_BASE ), ( ( USART_TypeDef * ) USART2_BASE ) };
\r
75 /* Queues used to hold characters waiting to be transmitted - one queue per port. */
\r
76 static QueueHandle_t xCharsForTx[ serNUM_COM_PORTS ] = { 0 };
\r
78 /* Queues holding received characters - one queue per port. */
\r
79 static QueueHandle_t xRxedChars[ serNUM_COM_PORTS ] = { 0 };
\r
81 /*-----------------------------------------------------------*/
\r
83 /* UART interrupt handlers, as named in the vector table. */
\r
84 void USART1_IRQHandler( void );
\r
85 void USART2_IRQHandler( void );
\r
87 /*-----------------------------------------------------------*/
\r
90 * See header file for parameter descriptions.
\r
92 long lCOMPortInit( unsigned long ulPort, unsigned long ulWantedBaud )
\r
94 long lReturn = pdFAIL;
\r
95 USART_InitTypeDef USART_InitStructure;
\r
96 NVIC_InitTypeDef NVIC_InitStructure;
\r
97 GPIO_InitTypeDef GPIO_InitStructure;
\r
99 if( ulPort < serNUM_COM_PORTS )
\r
101 /* The common (not port dependent) part of the initialisation. */
\r
102 USART_InitStructure.USART_BaudRate = ulWantedBaud;
\r
103 USART_InitStructure.USART_WordLength = USART_WordLength_8b;
\r
104 USART_InitStructure.USART_StopBits = USART_StopBits_1;
\r
105 USART_InitStructure.USART_Parity = USART_Parity_No;
\r
106 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
\r
107 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
\r
108 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = configLIBRARY_KERNEL_INTERRUPT_PRIORITY;
\r
109 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
\r
110 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
\r
113 /* Init the buffer structures with the buffer for the COM port being
\r
114 initialised, and perform any non-common initialisation necessary. This
\r
115 does not check to see if the COM port has already been initialised. */
\r
118 /* Create the queue of chars that are waiting to be sent to COM0. */
\r
119 xCharsForTx[ 0 ] = xQueueCreate( serTX_QUEUE_LEN, sizeof( char ) );
\r
121 /* Create the queue used to hold characters received from COM0. */
\r
122 xRxedChars[ 0 ] = xQueueCreate( serRX_QUEUE_LEN, sizeof( char ) );
\r
124 /* Enable COM0 clock - the ST libraries start numbering from UART1. */
\r
125 RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE );
\r
127 /* Configure USART1 Rx (PA10) as input floating */
\r
128 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
\r
129 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
\r
130 GPIO_Init( GPIOA, &GPIO_InitStructure );
\r
132 /* Configure USART1 Tx (PA9) as alternate function push-pull */
\r
133 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
\r
134 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
\r
135 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
\r
136 GPIO_Init( GPIOA, &GPIO_InitStructure );
\r
138 USART_Init( USART1, &USART_InitStructure );
\r
139 USART_ITConfig( USART1, USART_IT_RXNE, ENABLE );
\r
141 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
\r
142 NVIC_Init( &NVIC_InitStructure );
\r
144 USART_DMACmd( USART1, ( USART_DMAReq_Tx | USART_DMAReq_Rx ), ENABLE );
\r
145 USART_Cmd( USART1, ENABLE );
\r
147 /* Everything is ok. */
\r
150 else if( ulPort == 1 )
\r
152 /* Create the queue of chars that are waiting to be sent to COM1. */
\r
153 xCharsForTx[ 1 ] = xQueueCreate( serTX_QUEUE_LEN, sizeof( char ) );
\r
155 /* Create the queue used to hold characters received from COM0. */
\r
156 xRxedChars[ 1 ] = xQueueCreate( serRX_QUEUE_LEN, sizeof( char ) );
\r
158 /* Enable COM0 clock - the ST libraries start numbering from 1. */
\r
159 RCC_APB2PeriphClockCmd( RCC_APB1Periph_USART2 | RCC_APB2Periph_GPIOA, ENABLE );
\r
161 /* Configure USART2 Rx (PA3) as input floating */
\r
162 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
\r
163 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
\r
164 GPIO_Init( GPIOA, &GPIO_InitStructure );
\r
166 /* Configure USART2 Tx (PA2) as alternate function push-pull */
\r
167 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
\r
168 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
\r
169 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
\r
170 GPIO_Init( GPIOA, &GPIO_InitStructure );
\r
172 USART_Init( USART2, &USART_InitStructure );
\r
173 USART_ITConfig( USART2, USART_IT_RXNE, ENABLE );
\r
175 NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel;
\r
176 NVIC_Init( &NVIC_InitStructure );
\r
178 USART_DMACmd( USART2, ( USART_DMAReq_Tx | USART_DMAReq_Rx ), ENABLE );
\r
179 USART_Cmd( USART2, ENABLE );
\r
181 /* Everything is ok. */
\r
186 /* Nothing to do unless more than two ports are supported. */
\r
192 /*-----------------------------------------------------------*/
\r
194 signed long xSerialGetChar( long lPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
196 long lReturn = pdFAIL;
\r
198 if( lPort < serNUM_COM_PORTS )
\r
200 if( xQueueReceive( xRxedChars[ lPort ], pcRxedChar, xBlockTime ) == pdPASS )
\r
208 /*-----------------------------------------------------------*/
\r
210 long lSerialPutString( long lPort, const char * const pcString, unsigned long ulStringLength )
\r
215 if( lPort < serNUM_COM_PORTS )
\r
219 for( ul = 0; ul < ulStringLength; ul++ )
\r
221 if( xQueueSend( xCharsForTx[ lPort ], &( pcString[ ul ] ), serPUT_STRING_CHAR_DELAY ) != pdPASS )
\r
223 /* Cannot fit any more in the queue. Try turning the Tx on to
\r
224 clear some space. */
\r
225 USART_ITConfig( xUARTS[ lPort ], USART_IT_TXE, ENABLE );
\r
226 vTaskDelay( serPUT_STRING_CHAR_DELAY );
\r
228 /* Go back and try again. */
\r
233 USART_ITConfig( xUARTS[ lPort ], USART_IT_TXE, ENABLE );
\r
242 /*-----------------------------------------------------------*/
\r
244 signed long xSerialPutChar( long lPort, signed char cOutChar, TickType_t xBlockTime )
\r
248 if( xQueueSend( xCharsForTx[ lPort ], &cOutChar, xBlockTime ) == pdPASS )
\r
251 USART_ITConfig( xUARTS[ lPort ], USART_IT_TXE, ENABLE );
\r
260 /*-----------------------------------------------------------*/
\r
262 void USART1_IRQHandler( void )
\r
264 long xHigherPriorityTaskWoken = pdFALSE;
\r
267 if( USART_GetITStatus( USART1, USART_IT_TXE ) == SET )
\r
269 /* The interrupt was caused by the THR becoming empty. Are there any
\r
270 more characters to transmit? */
\r
271 if( xQueueReceiveFromISR( xCharsForTx[ 0 ], &cChar, &xHigherPriorityTaskWoken ) )
\r
273 /* A character was retrieved from the buffer so can be sent to the
\r
275 USART_SendData( USART1, cChar );
\r
279 USART_ITConfig( USART1, USART_IT_TXE, DISABLE );
\r
283 if( USART_GetITStatus( USART1, USART_IT_RXNE ) == SET )
\r
285 cChar = USART_ReceiveData( USART1 );
\r
286 xQueueSendFromISR( xRxedChars[ 0 ], &cChar, &xHigherPriorityTaskWoken );
\r
289 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
291 /*-----------------------------------------------------------*/
\r
293 void USART2_IRQHandler( void )
\r