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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [AVR_ATMega323_WinAVR/] [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 589 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
Changes from V1.2.3
56
 
57
        + The function xPortInitMinimal() has been renamed to
58
          xSerialPortInitMinimal() and the function xPortInit() has been renamed
59
          to xSerialPortInit().
60
 
61
Changes from V2.0.0
62
 
63
        + Delay periods are now specified using variables and constants of
64
          portTickType rather than unsigned long.
65
        + xQueueReceiveFromISR() used in place of xQueueReceive() within the ISR.
66
 
67
Changes from V2.6.0
68
 
69
        + Replaced the inb() and outb() functions with direct memory
70
          access.  This allows the port to be built with the 20050414 build of
71
          WinAVR.
72
*/
73
 
74
/* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER. */
75
 
76
 
77
#include <stdlib.h>
78
#include <avr/interrupt.h>
79
#include "FreeRTOS.h"
80
#include "queue.h"
81
#include "task.h"
82
#include "serial.h"
83
 
84
#define serBAUD_DIV_CONSTANT                    ( ( unsigned long ) 16 )
85
 
86
/* Constants for writing to UCSRB. */
87
#define serRX_INT_ENABLE                                ( ( unsigned char ) 0x80 )
88
#define serRX_ENABLE                                    ( ( unsigned char ) 0x10 )
89
#define serTX_ENABLE                                    ( ( unsigned char ) 0x08 )
90
#define serTX_INT_ENABLE                                ( ( unsigned char ) 0x20 )
91
 
92
/* Constants for writing to UCSRC. */
93
#define serUCSRC_SELECT                                 ( ( unsigned char ) 0x80 )
94
#define serEIGHT_DATA_BITS                              ( ( unsigned char ) 0x06 )
95
 
96
static xQueueHandle xRxedChars;
97
static xQueueHandle xCharsForTx;
98
 
99
#define vInterruptOn()                                                                          \
100
{                                                                                                                       \
101
        unsigned char ucByte;                                                           \
102
                                                                                                                        \
103
        ucByte = UCSRB;                                                                                 \
104
        ucByte |= serTX_INT_ENABLE;                                                             \
105
        UCSRB = ucByte;                                                                                 \
106
}
107
/*-----------------------------------------------------------*/
108
 
109
#define vInterruptOff()                                                                         \
110
{                                                                                                                       \
111
        unsigned char ucInByte;                                                         \
112
                                                                                                                        \
113
        ucInByte = UCSRB;                                                                               \
114
        ucInByte &= ~serTX_INT_ENABLE;                                                  \
115
        UCSRB = ucInByte;                                                                               \
116
}
117
/*-----------------------------------------------------------*/
118
 
119
xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
120
{
121
unsigned long ulBaudRateCounter;
122
unsigned char ucByte;
123
 
124
        portENTER_CRITICAL();
125
        {
126
                /* Create the queues used by the com test task. */
127
                xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
128
                xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
129
 
130
                /* Calculate the baud rate register value from the equation in the
131
                data sheet. */
132
                ulBaudRateCounter = ( configCPU_CLOCK_HZ / ( serBAUD_DIV_CONSTANT * ulWantedBaud ) ) - ( unsigned long ) 1;
133
 
134
                /* Set the baud rate. */
135
                ucByte = ( unsigned char ) ( ulBaudRateCounter & ( unsigned long ) 0xff );
136
                UBRRL = ucByte;
137
 
138
                ulBaudRateCounter >>= ( unsigned long ) 8;
139
                ucByte = ( unsigned char ) ( ulBaudRateCounter & ( unsigned long ) 0xff );
140
                UBRRH = ucByte;
141
 
142
                /* Enable the Rx interrupt.  The Tx interrupt will get enabled
143
                later. Also enable the Rx and Tx. */
144
                UCSRB = ( serRX_INT_ENABLE | serRX_ENABLE | serTX_ENABLE );
145
 
146
                /* Set the data bits to 8. */
147
                UCSRC = ( serUCSRC_SELECT | serEIGHT_DATA_BITS );
148
        }
149
        portEXIT_CRITICAL();
150
 
151
        /* Unlike other ports, this serial code does not allow for more than one
152
        com port.  We therefore don't return a pointer to a port structure and can
153
        instead just return NULL. */
154
        return NULL;
155
}
156
/*-----------------------------------------------------------*/
157
 
158
signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
159
{
160
        /* Only one port is supported. */
161
        ( void ) pxPort;
162
 
163
        /* Get the next character from the buffer.  Return false if no characters
164
        are available, or arrive before xBlockTime expires. */
165
        if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
166
        {
167
                return pdTRUE;
168
        }
169
        else
170
        {
171
                return pdFALSE;
172
        }
173
}
174
/*-----------------------------------------------------------*/
175
 
176
signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
177
{
178
        /* Only one port is supported. */
179
        ( void ) pxPort;
180
 
181
        /* Return false if after the block time there is no room on the Tx queue. */
182
        if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
183
        {
184
                return pdFAIL;
185
        }
186
 
187
        vInterruptOn();
188
 
189
        return pdPASS;
190
}
191
/*-----------------------------------------------------------*/
192
 
193
void vSerialClose( xComPortHandle xPort )
194
{
195
unsigned char ucByte;
196
 
197
        /* The parameter is not used. */
198
        ( void ) xPort;
199
 
200
        /* Turn off the interrupts.  We may also want to delete the queues and/or
201
        re-install the original ISR. */
202
 
203
        portENTER_CRITICAL();
204
        {
205
                vInterruptOff();
206
                ucByte = UCSRB;
207
                ucByte &= ~serRX_INT_ENABLE;
208
                UCSRB = ucByte;
209
        }
210
        portEXIT_CRITICAL();
211
}
212
/*-----------------------------------------------------------*/
213
 
214
SIGNAL( SIG_UART_RECV )
215
{
216
signed char cChar;
217
signed portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
218
 
219
        /* Get the character and post it on the queue of Rxed characters.
220
        If the post causes a task to wake force a context switch as the woken task
221
        may have a higher priority than the task we have interrupted. */
222
        cChar = UDR;
223
 
224
        xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
225
 
226
        if( xHigherPriorityTaskWoken != pdFALSE )
227
        {
228
                taskYIELD();
229
        }
230
}
231
/*-----------------------------------------------------------*/
232
 
233
SIGNAL( SIG_UART_DATA )
234
{
235
signed char cChar, cTaskWoken;
236
 
237
        if( xQueueReceiveFromISR( xCharsForTx, &cChar, &cTaskWoken ) == pdTRUE )
238
        {
239
                /* Send the next character queued for Tx. */
240
                UDR = cChar;
241
        }
242
        else
243
        {
244
                /* Queue empty, nothing to send. */
245
                vInterruptOff();
246
        }
247
}
248
 

powered by: WebSVN 2.1.0

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