OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [CORTEX_STM32F103_GCC_Rowley/] [Drivers/] [STM32_USART.c] - Blame information for rev 582

Details | Compare with Previous | View Log

Line No. Rev Author Line
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
        INTERRUPT DRIVEN SERIAL PORT DRIVER.
56
*/
57
 
58
 
59
/******************************************************************************
60
*** NOTE:  COM0 == USART1, COM1 == USART2
61
******************************************************************************/
62
 
63
 
64
/* Scheduler includes. */
65
#include "FreeRTOS.h"
66
#include "task.h"
67
#include "queue.h"
68
#include "semphr.h"
69
 
70
/* Library includes. */
71
#include "stm32f10x_lib.h"
72
 
73
/* Driver includes. */
74
#include "STM32_USART.h"
75
/*-----------------------------------------------------------*/
76
 
77
/* The number of COM ports that can be controlled at the same time. */
78
#define serNUM_COM_PORTS                                ( 2 )
79
 
80
/* Queues are used to hold characters that are waiting to be transmitted.  This
81
constant sets the maximum number of characters that can be contained in such a
82
queue at any one time. */
83
#define serTX_QUEUE_LEN                                 ( 100 )
84
 
85
/* Queues are used to hold characters that have been received but not yet
86
processed.  This constant sets the maximum number of characters that can be
87
contained in such a queue. */
88
#define serRX_QUEUE_LEN                                 ( 100 )
89
 
90
/* The maximum amount of time that calls to lSerialPutString() should wait for
91
there to be space to post each character to the queue of characters waiting
92
transmission.  NOTE!  This is the time to wait per character - not the time to
93
wait for the entire string. */
94
#define serPUT_STRING_CHAR_DELAY                ( 5 / portTICK_RATE_MS )
95
 
96
/*-----------------------------------------------------------*/
97
 
98
/* References to the USART peripheral addresses themselves. */
99
static USART_TypeDef * const xUARTS[ serNUM_COM_PORTS ] = { ( ( USART_TypeDef * ) USART1_BASE ), ( ( USART_TypeDef * ) USART2_BASE ) };
100
 
101
/* Queues used to hold characters waiting to be transmitted - one queue per port. */
102
static xQueueHandle xCharsForTx[ serNUM_COM_PORTS ] = { 0 };
103
 
104
/* Queues holding received characters - one queue per port. */
105
static xQueueHandle xRxedChars[ serNUM_COM_PORTS ] = { 0 };
106
 
107
/*-----------------------------------------------------------*/
108
 
109
/* UART interrupt handlers, as named in the vector table. */
110
void USART1_IRQHandler( void );
111
void USART2_IRQHandler( void );
112
 
113
/*-----------------------------------------------------------*/
114
 
115
/*
116
 * See header file for parameter descriptions.
117
 */
118
long lCOMPortInit( unsigned long ulPort, unsigned long ulWantedBaud )
119
{
120
long lReturn = pdFAIL;
121
USART_InitTypeDef USART_InitStructure;
122
NVIC_InitTypeDef NVIC_InitStructure;
123
GPIO_InitTypeDef GPIO_InitStructure;
124
 
125
        if( ulPort < serNUM_COM_PORTS )
126
        {
127
                /* The common (not port dependent) part of the initialisation. */
128
                USART_InitStructure.USART_BaudRate = ulWantedBaud;
129
                USART_InitStructure.USART_WordLength = USART_WordLength_8b;
130
                USART_InitStructure.USART_StopBits = USART_StopBits_1;
131
                USART_InitStructure.USART_Parity = USART_Parity_No;
132
                USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
133
                USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
134
                NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = configLIBRARY_KERNEL_INTERRUPT_PRIORITY;
135
                NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
136
                NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
137
 
138
 
139
                /* Init the buffer structures with the buffer for the COM port being
140
                initialised, and perform any non-common initialisation necessary.  This
141
                does not check to see if the COM port has already been initialised. */
142
                if( ulPort == 0 )
143
                {
144
                        /* Create the queue of chars that are waiting to be sent to COM0. */
145
                        xCharsForTx[ 0 ] = xQueueCreate( serTX_QUEUE_LEN, sizeof( char ) );
146
 
147
                        /* Create the queue used to hold characters received from COM0. */
148
                        xRxedChars[ 0 ] = xQueueCreate( serRX_QUEUE_LEN, sizeof( char ) );
149
 
150
                        /* Enable COM0 clock - the ST libraries start numbering from UART1. */
151
                        RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE );
152
 
153
                        /* Configure USART1 Rx (PA10) as input floating */
154
                        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
155
                        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
156
                        GPIO_Init( GPIOA, &GPIO_InitStructure );
157
 
158
                        /* Configure USART1 Tx (PA9) as alternate function push-pull */
159
                        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
160
                        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
161
                        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
162
                        GPIO_Init( GPIOA, &GPIO_InitStructure );
163
 
164
                        USART_Init( USART1, &USART_InitStructure );
165
                        USART_ITConfig( USART1, USART_IT_RXNE, ENABLE );
166
 
167
                        NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;
168
                        NVIC_Init( &NVIC_InitStructure );
169
 
170
            USART_DMACmd( USART1, ( USART_DMAReq_Tx | USART_DMAReq_Rx ), ENABLE );
171
                        USART_Cmd( USART1, ENABLE );
172
 
173
                        /* Everything is ok. */
174
                        lReturn = pdPASS;
175
                }
176
                else if( ulPort == 1 )
177
                {
178
                        /* Create the queue of chars that are waiting to be sent to COM1. */
179
                        xCharsForTx[ 1 ] = xQueueCreate( serTX_QUEUE_LEN, sizeof( char ) );
180
 
181
                        /* Create the queue used to hold characters received from COM0. */
182
                        xRxedChars[ 1 ] = xQueueCreate( serRX_QUEUE_LEN, sizeof( char ) );
183
 
184
                        /* Enable COM0 clock - the ST libraries start numbering from 1. */
185
                        RCC_APB2PeriphClockCmd( RCC_APB1Periph_USART2 | RCC_APB2Periph_GPIOA, ENABLE );
186
 
187
                        /* Configure USART2 Rx (PA3) as input floating */
188
                        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
189
                        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
190
                        GPIO_Init( GPIOA, &GPIO_InitStructure );
191
 
192
                        /* Configure USART2 Tx (PA2) as alternate function push-pull */
193
                        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
194
                        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
195
                        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
196
                        GPIO_Init( GPIOA, &GPIO_InitStructure );
197
 
198
                        USART_Init( USART2, &USART_InitStructure );
199
                        USART_ITConfig( USART2, USART_IT_RXNE, ENABLE );
200
 
201
                        NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel;
202
                        NVIC_Init( &NVIC_InitStructure );
203
 
204
            USART_DMACmd( USART2, ( USART_DMAReq_Tx | USART_DMAReq_Rx ), ENABLE );
205
                        USART_Cmd( USART2, ENABLE );
206
 
207
                        /* Everything is ok. */
208
                        lReturn = pdPASS;
209
                }
210
                else
211
                {
212
                        /* Nothing to do unless more than two ports are supported. */
213
                }
214
        }
215
 
216
        return lReturn;
217
}
218
/*-----------------------------------------------------------*/
219
 
220
signed long xSerialGetChar( long lPort, signed char *pcRxedChar, portTickType xBlockTime )
221
{
222
long lReturn = pdFAIL;
223
 
224
        if( lPort < serNUM_COM_PORTS )
225
        {
226
                if( xQueueReceive( xRxedChars[ lPort ], pcRxedChar, xBlockTime ) == pdPASS )
227
                {
228
                        lReturn = pdPASS;
229
                }
230
        }
231
 
232
        return lReturn;
233
}
234
/*-----------------------------------------------------------*/
235
 
236
long lSerialPutString( long lPort, const char * const pcString, unsigned long ulStringLength )
237
{
238
long lReturn;
239
unsigned long ul;
240
 
241
        if( lPort < serNUM_COM_PORTS )
242
        {
243
                lReturn = pdPASS;
244
 
245
                for( ul = 0; ul < ulStringLength; ul++ )
246
                {
247
                        if( xQueueSend( xCharsForTx[ lPort ], &( pcString[ ul ] ), serPUT_STRING_CHAR_DELAY ) != pdPASS )
248
                        {
249
                                /* Cannot fit any more in the queue.  Try turning the Tx on to
250
                                clear some space. */
251
                                USART_ITConfig( xUARTS[ lPort ], USART_IT_TXE, ENABLE );
252
                                vTaskDelay( serPUT_STRING_CHAR_DELAY );
253
 
254
                                /* Go back and try again. */
255
                                continue;
256
                        }
257
                }
258
 
259
        USART_ITConfig( xUARTS[ lPort ], USART_IT_TXE, ENABLE );
260
        }
261
        else
262
        {
263
                lReturn = pdFAIL;
264
        }
265
 
266
        return lReturn;
267
}
268
/*-----------------------------------------------------------*/
269
 
270
signed long xSerialPutChar( long lPort, signed char cOutChar, portTickType xBlockTime )
271
{
272
long lReturn;
273
 
274
        if( xQueueSend( xCharsForTx[ lPort ], &cOutChar, xBlockTime ) == pdPASS )
275
        {
276
                lReturn = pdPASS;
277
                USART_ITConfig( xUARTS[ lPort ], USART_IT_TXE, ENABLE );
278
        }
279
        else
280
        {
281
                lReturn = pdFAIL;
282
        }
283
 
284
        return lReturn;
285
}
286
/*-----------------------------------------------------------*/
287
 
288
void USART1_IRQHandler( void )
289
{
290
long xHigherPriorityTaskWoken = pdFALSE;
291
char cChar;
292
 
293
        if( USART_GetITStatus( USART1, USART_IT_TXE ) == SET )
294
        {
295
                /* The interrupt was caused by the THR becoming empty.  Are there any
296
                more characters to transmit? */
297
                if( xQueueReceiveFromISR( xCharsForTx[ 0 ], &cChar, &xHigherPriorityTaskWoken ) )
298
                {
299
                        /* A character was retrieved from the buffer so can be sent to the
300
                        THR now. */
301
                        USART_SendData( USART1, cChar );
302
                }
303
                else
304
                {
305
                        USART_ITConfig( USART1, USART_IT_TXE, DISABLE );
306
                }
307
        }
308
 
309
        if( USART_GetITStatus( USART1, USART_IT_RXNE ) == SET )
310
        {
311
                cChar = USART_ReceiveData( USART1 );
312
                xQueueSendFromISR( xRxedChars[ 0 ], &cChar, &xHigherPriorityTaskWoken );
313
        }
314
 
315
        portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
316
}
317
/*-----------------------------------------------------------*/
318
 
319
void USART2_IRQHandler( void )
320
{
321
}
322
 
323
 
324
 
325
 
326
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.