1 |
582 |
jeremybenn |
/*
|
2 |
|
|
FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
|
3 |
|
|
|
4 |
|
|
***************************************************************************
|
5 |
|
|
* *
|
6 |
|
|
* If you are: *
|
7 |
|
|
* *
|
8 |
|
|
* + New to FreeRTOS, *
|
9 |
|
|
* + Wanting to learn FreeRTOS or multitasking in general quickly *
|
10 |
|
|
* + Looking for basic training, *
|
11 |
|
|
* + Wanting to improve your FreeRTOS skills and productivity *
|
12 |
|
|
* *
|
13 |
|
|
* then take a look at the FreeRTOS books - available as PDF or paperback *
|
14 |
|
|
* *
|
15 |
|
|
* "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
|
16 |
|
|
* http://www.FreeRTOS.org/Documentation *
|
17 |
|
|
* *
|
18 |
|
|
* A pdf reference manual is also available. Both are usually delivered *
|
19 |
|
|
* to your inbox within 20 minutes to two hours when purchased between 8am *
|
20 |
|
|
* and 8pm GMT (although please allow up to 24 hours in case of *
|
21 |
|
|
* exceptional circumstances). Thank you for your support! *
|
22 |
|
|
* *
|
23 |
|
|
***************************************************************************
|
24 |
|
|
|
25 |
|
|
This file is part of the FreeRTOS distribution.
|
26 |
|
|
|
27 |
|
|
FreeRTOS is free software; you can redistribute it and/or modify it under
|
28 |
|
|
the terms of the GNU General Public License (version 2) as published by the
|
29 |
|
|
Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
|
30 |
|
|
***NOTE*** The exception to the GPL is included to allow you to distribute
|
31 |
|
|
a combined work that includes FreeRTOS without being obliged to provide the
|
32 |
|
|
source code for proprietary components outside of the FreeRTOS kernel.
|
33 |
|
|
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
|
34 |
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
35 |
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
36 |
|
|
more details. You should have received a copy of the GNU General Public
|
37 |
|
|
License and the FreeRTOS license exception along with FreeRTOS; if not it
|
38 |
|
|
can be viewed here: http://www.freertos.org/a00114.html and also obtained
|
39 |
|
|
by writing to Richard Barry, contact details for whom are available on the
|
40 |
|
|
FreeRTOS WEB site.
|
41 |
|
|
|
42 |
|
|
1 tab == 4 spaces!
|
43 |
|
|
|
44 |
|
|
http://www.FreeRTOS.org - Documentation, latest information, license and
|
45 |
|
|
contact details.
|
46 |
|
|
|
47 |
|
|
http://www.SafeRTOS.com - A version that is certified for use in safety
|
48 |
|
|
critical systems.
|
49 |
|
|
|
50 |
|
|
http://www.OpenRTOS.com - Commercial support, development, porting,
|
51 |
|
|
licensing and training services.
|
52 |
|
|
*/
|
53 |
|
|
|
54 |
|
|
/*
|
55 |
|
|
BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.
|
56 |
|
|
*/
|
57 |
|
|
|
58 |
|
|
/* Scheduler includes. */
|
59 |
|
|
#include "FreeRTOS.h"
|
60 |
|
|
#include "queue.h"
|
61 |
|
|
#include "semphr.h"
|
62 |
|
|
|
63 |
|
|
/* Library includes. */
|
64 |
|
|
#include "stm32f10x_lib.h"
|
65 |
|
|
|
66 |
|
|
/* Demo application includes. */
|
67 |
|
|
#include "serial.h"
|
68 |
|
|
/*-----------------------------------------------------------*/
|
69 |
|
|
|
70 |
|
|
/* Misc defines. */
|
71 |
|
|
#define serINVALID_QUEUE ( ( xQueueHandle ) 0 )
|
72 |
|
|
#define serNO_BLOCK ( ( portTickType ) 0 )
|
73 |
|
|
#define serTX_BLOCK_TIME ( 40 / portTICK_RATE_MS )
|
74 |
|
|
|
75 |
|
|
/*-----------------------------------------------------------*/
|
76 |
|
|
|
77 |
|
|
/* The queue used to hold received characters. */
|
78 |
|
|
static xQueueHandle xRxedChars;
|
79 |
|
|
static xQueueHandle xCharsForTx;
|
80 |
|
|
|
81 |
|
|
/*-----------------------------------------------------------*/
|
82 |
|
|
|
83 |
|
|
/* UART interrupt handler. */
|
84 |
|
|
void vUARTInterruptHandler( void );
|
85 |
|
|
|
86 |
|
|
/*-----------------------------------------------------------*/
|
87 |
|
|
|
88 |
|
|
/*
|
89 |
|
|
* See the serial2.h header file.
|
90 |
|
|
*/
|
91 |
|
|
xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
|
92 |
|
|
{
|
93 |
|
|
xComPortHandle xReturn;
|
94 |
|
|
USART_InitTypeDef USART_InitStructure;
|
95 |
|
|
NVIC_InitTypeDef NVIC_InitStructure;
|
96 |
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
97 |
|
|
|
98 |
|
|
/* Create the queues used to hold Rx/Tx characters. */
|
99 |
|
|
xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
|
100 |
|
|
xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
|
101 |
|
|
|
102 |
|
|
/* If the queue/semaphore was created correctly then setup the serial port
|
103 |
|
|
hardware. */
|
104 |
|
|
if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )
|
105 |
|
|
{
|
106 |
|
|
/* Enable USART1 clock */
|
107 |
|
|
RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE );
|
108 |
|
|
|
109 |
|
|
/* Configure USART1 Rx (PA10) as input floating */
|
110 |
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
|
111 |
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
112 |
|
|
GPIO_Init( GPIOA, &GPIO_InitStructure );
|
113 |
|
|
|
114 |
|
|
/* Configure USART1 Tx (PA9) as alternate function push-pull */
|
115 |
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
|
116 |
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
117 |
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
118 |
|
|
GPIO_Init( GPIOA, &GPIO_InitStructure );
|
119 |
|
|
|
120 |
|
|
USART_InitStructure.USART_BaudRate = ulWantedBaud;
|
121 |
|
|
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
|
122 |
|
|
USART_InitStructure.USART_StopBits = USART_StopBits_1;
|
123 |
|
|
USART_InitStructure.USART_Parity = USART_Parity_No ;
|
124 |
|
|
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
125 |
|
|
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
|
126 |
|
|
USART_InitStructure.USART_Clock = USART_Clock_Disable;
|
127 |
|
|
USART_InitStructure.USART_CPOL = USART_CPOL_Low;
|
128 |
|
|
USART_InitStructure.USART_CPHA = USART_CPHA_2Edge;
|
129 |
|
|
USART_InitStructure.USART_LastBit = USART_LastBit_Disable;
|
130 |
|
|
|
131 |
|
|
USART_Init( USART1, &USART_InitStructure );
|
132 |
|
|
|
133 |
|
|
USART_ITConfig( USART1, USART_IT_RXNE, ENABLE );
|
134 |
|
|
|
135 |
|
|
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
|
136 |
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = configLIBRARY_KERNEL_INTERRUPT_PRIORITY;
|
137 |
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
138 |
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
139 |
|
|
NVIC_Init( &NVIC_InitStructure );
|
140 |
|
|
|
141 |
|
|
USART_Cmd( USART1, ENABLE );
|
142 |
|
|
}
|
143 |
|
|
else
|
144 |
|
|
{
|
145 |
|
|
xReturn = ( xComPortHandle ) 0;
|
146 |
|
|
}
|
147 |
|
|
|
148 |
|
|
/* This demo file only supports a single port but we have to return
|
149 |
|
|
something to comply with the standard demo header file. */
|
150 |
|
|
return xReturn;
|
151 |
|
|
}
|
152 |
|
|
/*-----------------------------------------------------------*/
|
153 |
|
|
|
154 |
|
|
signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
|
155 |
|
|
{
|
156 |
|
|
/* The port handle is not required as this driver only supports one port. */
|
157 |
|
|
( void ) pxPort;
|
158 |
|
|
|
159 |
|
|
/* Get the next character from the buffer. Return false if no characters
|
160 |
|
|
are available, or arrive before xBlockTime expires. */
|
161 |
|
|
if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
|
162 |
|
|
{
|
163 |
|
|
return pdTRUE;
|
164 |
|
|
}
|
165 |
|
|
else
|
166 |
|
|
{
|
167 |
|
|
return pdFALSE;
|
168 |
|
|
}
|
169 |
|
|
}
|
170 |
|
|
/*-----------------------------------------------------------*/
|
171 |
|
|
|
172 |
|
|
void vSerialPutString( xComPortHandle pxPort, const signed portCHAR * const pcString, unsigned portSHORT usStringLength )
|
173 |
|
|
{
|
174 |
|
|
signed portCHAR *pxNext;
|
175 |
|
|
|
176 |
|
|
/* A couple of parameters that this port does not use. */
|
177 |
|
|
( void ) usStringLength;
|
178 |
|
|
( void ) pxPort;
|
179 |
|
|
|
180 |
|
|
/* NOTE: This implementation does not handle the queue being full as no
|
181 |
|
|
block time is used! */
|
182 |
|
|
|
183 |
|
|
/* The port handle is not required as this driver only supports UART1. */
|
184 |
|
|
( void ) pxPort;
|
185 |
|
|
|
186 |
|
|
/* Send each character in the string, one at a time. */
|
187 |
|
|
pxNext = ( signed portCHAR * ) pcString;
|
188 |
|
|
while( *pxNext )
|
189 |
|
|
{
|
190 |
|
|
xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
|
191 |
|
|
pxNext++;
|
192 |
|
|
}
|
193 |
|
|
}
|
194 |
|
|
/*-----------------------------------------------------------*/
|
195 |
|
|
|
196 |
|
|
signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
|
197 |
|
|
{
|
198 |
|
|
signed portBASE_TYPE xReturn;
|
199 |
|
|
|
200 |
|
|
if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) == pdPASS )
|
201 |
|
|
{
|
202 |
|
|
xReturn = pdPASS;
|
203 |
|
|
USART_ITConfig( USART1, USART_IT_TXE, ENABLE );
|
204 |
|
|
}
|
205 |
|
|
else
|
206 |
|
|
{
|
207 |
|
|
xReturn = pdFAIL;
|
208 |
|
|
}
|
209 |
|
|
|
210 |
|
|
return xReturn;
|
211 |
|
|
}
|
212 |
|
|
/*-----------------------------------------------------------*/
|
213 |
|
|
|
214 |
|
|
void vSerialClose( xComPortHandle xPort )
|
215 |
|
|
{
|
216 |
|
|
/* Not supported as not required by the demo application. */
|
217 |
|
|
}
|
218 |
|
|
/*-----------------------------------------------------------*/
|
219 |
|
|
|
220 |
|
|
void vUARTInterruptHandler( void )
|
221 |
|
|
{
|
222 |
|
|
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
|
223 |
|
|
portCHAR cChar;
|
224 |
|
|
|
225 |
|
|
if( USART_GetITStatus( USART1, USART_IT_TXE ) == SET )
|
226 |
|
|
{
|
227 |
|
|
/* The interrupt was caused by the THR becoming empty. Are there any
|
228 |
|
|
more characters to transmit? */
|
229 |
|
|
if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
|
230 |
|
|
{
|
231 |
|
|
/* A character was retrieved from the queue so can be sent to the
|
232 |
|
|
THR now. */
|
233 |
|
|
USART_SendData( USART1, cChar );
|
234 |
|
|
}
|
235 |
|
|
else
|
236 |
|
|
{
|
237 |
|
|
USART_ITConfig( USART1, USART_IT_TXE, DISABLE );
|
238 |
|
|
}
|
239 |
|
|
}
|
240 |
|
|
|
241 |
|
|
if( USART_GetITStatus( USART1, USART_IT_RXNE ) == SET )
|
242 |
|
|
{
|
243 |
|
|
cChar = USART_ReceiveData( USART1 );
|
244 |
|
|
xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
|
245 |
|
|
}
|
246 |
|
|
|
247 |
|
|
portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
|
248 |
|
|
}
|
249 |
|
|
|
250 |
|
|
|
251 |
|
|
|
252 |
|
|
|
253 |
|
|
|
254 |
|
|
|