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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [msp430_GCC/] [serial/] [serial.c] - Blame information for rev 773

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 583 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.
56
 *
57
 * This file only supports UART 1
58
 */
59
 
60
/* Standard includes. */
61
#include <stdlib.h>
62
#include <signal.h>
63
 
64
/* Scheduler includes. */
65
#include "FreeRTOS.h"
66
#include "queue.h"
67
#include "task.h"
68
 
69
/* Demo application includes. */
70
#include "serial.h"
71
 
72
/* Constants required to setup the hardware. */
73
#define serTX_AND_RX                    ( ( unsigned char ) 0x03 )
74
 
75
/* Misc. constants. */
76
#define serNO_BLOCK                             ( ( portTickType ) 0 )
77
 
78
/* Enable the UART Tx interrupt. */
79
#define vInterruptOn() IFG2 |= UTXIFG1
80
 
81
/* The queue used to hold received characters. */
82
static xQueueHandle xRxedChars;
83
 
84
/* The queue used to hold characters waiting transmission. */
85
static xQueueHandle xCharsForTx;
86
 
87
static volatile short sTHREEmpty;
88
 
89
/* Interrupt service routines. */
90
interrupt (UART1RX_VECTOR) wakeup vRxISR( void );
91
interrupt (UART1TX_VECTOR) wakeup vTxISR( void );
92
 
93
/*-----------------------------------------------------------*/
94
 
95
xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
96
{
97
unsigned long ulBaudRateCount;
98
 
99
        /* Initialise the hardware. */
100
 
101
        /* Generate the baud rate constants for the wanted baud rate. */
102
        ulBaudRateCount = configCPU_CLOCK_HZ / ulWantedBaud;
103
 
104
        portENTER_CRITICAL();
105
        {
106
                /* Create the queues used by the com test task. */
107
                xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
108
                xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
109
 
110
                /* Reset UART. */
111
                UCTL1 |= SWRST;
112
 
113
                /* Set pin function. */
114
                P4SEL |= serTX_AND_RX;
115
 
116
                /* All other bits remain at zero for n, 8, 1 interrupt driven operation.
117
                LOOPBACK MODE!*/
118
                U1CTL |= CHAR + LISTEN;
119
                U1TCTL |= SSEL1;
120
 
121
                /* Setup baud rate low byte. */
122
                U1BR0 = ( unsigned char ) ( ulBaudRateCount & ( unsigned long ) 0xff );
123
 
124
                /* Setup baud rate high byte. */
125
                ulBaudRateCount >>= 8UL;
126
                U1BR1 = ( unsigned char ) ( ulBaudRateCount & ( unsigned long ) 0xff );
127
 
128
                /* Enable ports. */
129
                ME2 |= UTXE1 + URXE1;
130
 
131
                /* Set. */
132
                UCTL1 &= ~SWRST;
133
 
134
                /* Nothing in the buffer yet. */
135
                sTHREEmpty = pdTRUE;
136
 
137
                /* Enable interrupts. */
138
                IE2 |= URXIE1 + UTXIE1;
139
        }
140
        portEXIT_CRITICAL();
141
 
142
        /* Unlike other ports, this serial code does not allow for more than one
143
        com port.  We therefore don't return a pointer to a port structure and can
144
        instead just return NULL. */
145
        return NULL;
146
}
147
/*-----------------------------------------------------------*/
148
 
149
signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
150
{
151
        /* Get the next character from the buffer.  Return false if no characters
152
        are available, or arrive before xBlockTime expires. */
153
        if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
154
        {
155
                return pdTRUE;
156
        }
157
        else
158
        {
159
                return pdFALSE;
160
        }
161
}
162
/*-----------------------------------------------------------*/
163
 
164
signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
165
{
166
signed portBASE_TYPE xReturn;
167
 
168
        /* Transmit a character. */
169
 
170
        portENTER_CRITICAL();
171
        {
172
                if( sTHREEmpty == pdTRUE )
173
                {
174
                        /* If sTHREEmpty is true then the UART Tx ISR has indicated that
175
                        there are no characters queued to be transmitted - so we can
176
                        write the character directly to the shift Tx register. */
177
                        sTHREEmpty = pdFALSE;
178
                        U1TXBUF = cOutChar;
179
                        xReturn = pdPASS;
180
                }
181
                else
182
                {
183
                        /* sTHREEmpty is false, so there are still characters waiting to be
184
                        transmitted.  We have to queue this character so it gets
185
                        transmitted     in turn. */
186
 
187
                        /* Return false if after the block time there is no room on the Tx
188
                        queue.  It is ok to block inside a critical section as each task
189
                        maintains it's own critical section status. */
190
                        xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );
191
 
192
                        /* Depending on queue sizing and task prioritisation:  While we
193
                        were blocked waiting to post on the queue interrupts were not
194
                        disabled.  It is possible that the serial ISR has emptied the
195
                        Tx queue, in which case we need to start the Tx off again
196
                        writing directly to the Tx register. */
197
                        if( ( sTHREEmpty == pdTRUE ) && ( xReturn == pdPASS ) )
198
                        {
199
                                /* Get back the character we just posted. */
200
                                xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );
201
                                sTHREEmpty = pdFALSE;
202
                                U1TXBUF = cOutChar;
203
                        }
204
                }
205
        }
206
        portEXIT_CRITICAL();
207
 
208
        return pdPASS;
209
}
210
/*-----------------------------------------------------------*/
211
 
212
/*
213
 * UART RX interrupt service routine.
214
 */
215
interrupt (UART1RX_VECTOR) wakeup vRxISR( void )
216
{
217
signed char cChar;
218
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
219
 
220
        /* Get the character from the UART and post it on the queue of Rxed
221
        characters. */
222
        cChar = U1RXBUF;
223
 
224
        xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
225
 
226
        if( xHigherPriorityTaskWoken )
227
        {
228
                /*If the post causes a task to wake force a context switch
229
                as the woken task may have a higher priority than the task we have
230
                interrupted. */
231
                taskYIELD();
232
        }
233
}
234
/*-----------------------------------------------------------*/
235
 
236
/*
237
 * UART Tx interrupt service routine.
238
 */
239
interrupt (UART1TX_VECTOR) wakeup vTxISR( void )
240
{
241
signed char cChar;
242
portBASE_TYPE xTaskWoken = pdFALSE;
243
 
244
        /* The previous character has been transmitted.  See if there are any
245
        further characters waiting transmission. */
246
 
247
        if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )
248
        {
249
                /* There was another character queued - transmit it now. */
250
                U1TXBUF = cChar;
251
        }
252
        else
253
        {
254
                /* There were no other characters to transmit. */
255
                sTHREEmpty = pdTRUE;
256
        }
257
}
258
 

powered by: WebSVN 2.1.0

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