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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [WizNET_DEMO_GCC_ARM7/] [i2c.c] - Blame information for rev 827

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

Line No. Rev Author Line
1 585 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
/* Standard includes. */
56
#include <stdlib.h>
57
 
58
/* Scheduler include files. */
59
#include "FreeRTOS.h"
60
#include "queue.h"
61
#include "semphr.h"
62
 
63
/* Application includes. */
64
#include "i2c.h"
65
 
66
/*-----------------------------------------------------------*/
67
 
68
/* Constants to setup the microcontroller IO. */
69
#define mainSDA_ENABLE                  ( ( unsigned long ) 0x0040 )
70
#define mainSCL_ENABLE                  ( ( unsigned long ) 0x0010 )
71
 
72
/* Bit definitions within the I2CONCLR register. */
73
#define i2cSTA_BIT                              ( ( unsigned char ) 0x20 )
74
#define i2cSI_BIT                               ( ( unsigned char ) 0x08 )
75
#define i2cSTO_BIT                              ( ( unsigned char ) 0x10 )
76
 
77
/* Constants required to setup the VIC. */
78
#define i2cI2C_VIC_CHANNEL              ( ( unsigned long ) 0x0009 )
79
#define i2cI2C_VIC_CHANNEL_BIT  ( ( unsigned long ) 0x0200 )
80
#define i2cI2C_VIC_ENABLE               ( ( unsigned long ) 0x0020 )
81
 
82
/* Misc constants. */
83
#define i2cNO_BLOCK                             ( ( portTickType ) 0 )
84
#define i2cQUEUE_LENGTH                 ( ( unsigned char ) 5 )
85
#define i2cEXTRA_MESSAGES               ( ( unsigned char ) 2 )
86
#define i2cREAD_TX_LEN                  ( ( unsigned long ) 2 )
87
#define i2cACTIVE_MASTER_MODE   ( ( unsigned char ) 0x40 )
88
#define i2cTIMERL                               ( 200 )
89
#define i2cTIMERH                               ( 200 )
90
 
91
/* Array of message definitions.  See the header file for more information
92
on the structure members.  There are two more places in the queue than as
93
defined by i2cQUEUE_LENGTH.  This is to ensure that there is always a free
94
message available - one can be in the process of being transmitted and one
95
can be left free. */
96
static xI2CMessage xTxMessages[ i2cQUEUE_LENGTH + i2cEXTRA_MESSAGES ];
97
 
98
/* Function in the ARM part of the code used to create the queues. */
99
extern void vI2CISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxTxMessages, unsigned long **ppulBusFree );
100
 
101
/* Index to the next free message in the xTxMessages array. */
102
unsigned long ulNextFreeMessage = ( unsigned long ) 0;
103
 
104
/* Queue of messages that are waiting transmission. */
105
static xQueueHandle xMessagesForTx;
106
 
107
/* Flag to indicate the state of the I2C ISR state machine. */
108
static unsigned long *pulBusFree;
109
 
110
/*-----------------------------------------------------------*/
111
void i2cMessage( const unsigned char * const pucMessage, long lMessageLength, unsigned char ucSlaveAddress, unsigned short usBufferAddress, unsigned long ulDirection, xSemaphoreHandle xMessageCompleteSemaphore, portTickType xBlockTime )
112
{
113
extern volatile xI2CMessage *pxCurrentMessage;
114
xI2CMessage *pxNextFreeMessage;
115
signed portBASE_TYPE xReturn;
116
 
117
        portENTER_CRITICAL();
118
        {
119
                /* This message is guaranteed to be free as there are two more messages
120
                than spaces in the queue allowing for one message to be in process of
121
                being transmitted and one to be left free. */
122
                pxNextFreeMessage = &( xTxMessages[ ulNextFreeMessage ] );
123
 
124
                /* Fill the message with the data to be sent. */
125
 
126
                /* Pointer to the actual data.  Only a pointer is stored (i.e. the
127
                actual data is not copied, so the data being pointed to must still
128
                be valid when the message eventually gets sent (it may be queued for
129
                a while. */
130
                pxNextFreeMessage->pucBuffer = ( unsigned char * ) pucMessage;
131
 
132
                /* This is the address of the I2C device we are going to transmit this
133
                message to. */
134
                pxNextFreeMessage->ucSlaveAddress = ucSlaveAddress | ( unsigned char ) ulDirection;
135
 
136
                /* A semaphore can be used to allow the I2C ISR to indicate that the
137
                message has been sent.  This can be NULL if you don't want to wait for
138
                the message transmission to complete. */
139
                pxNextFreeMessage->xMessageCompleteSemaphore = xMessageCompleteSemaphore;
140
 
141
                /* How many bytes are to be sent? */
142
                pxNextFreeMessage->lMessageLength = lMessageLength;
143
 
144
                /* The address within the WIZnet device to which the data will be
145
                written.  This could be the address of a register, or alternatively
146
                a location within the WIZnet Tx buffer. */
147
                pxNextFreeMessage->ucBufferAddressLowByte = ( unsigned char ) ( usBufferAddress & 0xff );
148
 
149
                /* Second byte of the address. */
150
                usBufferAddress >>= 8;
151
                pxNextFreeMessage->ucBufferAddressHighByte = ( unsigned char ) ( usBufferAddress & 0xff );
152
 
153
                /* Increment to the next message in the array - with a wrap around check. */
154
                ulNextFreeMessage++;
155
                if( ulNextFreeMessage >= ( i2cQUEUE_LENGTH + i2cEXTRA_MESSAGES ) )
156
                {
157
                        ulNextFreeMessage = ( unsigned long ) 0;
158
                }
159
 
160
                /* Is the I2C interrupt in the middle of transmitting a message? */
161
                if( *pulBusFree == ( unsigned long ) pdTRUE )
162
                {
163
                        /* No message is currently being sent or queued to be sent.  We
164
                        can start the ISR sending this message immediately. */
165
                        pxCurrentMessage = pxNextFreeMessage;
166
 
167
                        I2C_I2CONCLR = i2cSI_BIT;
168
                        I2C_I2CONSET = i2cSTA_BIT;
169
 
170
                        *pulBusFree = ( unsigned long ) pdFALSE;
171
                }
172
                else
173
                {
174
                        /* The I2C interrupt routine is mid sending a message.  Queue
175
                        this message ready to be sent. */
176
                        xReturn = xQueueSend( xMessagesForTx, &pxNextFreeMessage, xBlockTime );
177
 
178
                        /* We may have blocked while trying to queue the message.  If this
179
                        was the case then the interrupt would have been enabled and we may
180
                        now find that the I2C interrupt routine is no longer sending a
181
                        message. */
182
                        if( ( *pulBusFree == ( unsigned long ) pdTRUE ) && ( xReturn == pdPASS ) )
183
                        {
184
                                /* Get the next message in the queue (this should be the
185
                                message we just posted) and start off the transmission
186
                                again. */
187
                                xQueueReceive( xMessagesForTx, &pxNextFreeMessage, i2cNO_BLOCK );
188
                                pxCurrentMessage = pxNextFreeMessage;
189
 
190
                                I2C_I2CONCLR = i2cSI_BIT;
191
                                I2C_I2CONSET = i2cSTA_BIT;
192
 
193
                                *pulBusFree = ( unsigned long ) pdFALSE;
194
                        }
195
                }
196
        }
197
        portEXIT_CRITICAL();
198
}
199
/*-----------------------------------------------------------*/
200
 
201
void i2cInit( void )
202
{
203
extern void ( vI2C_ISR_Wrapper )( void );
204
 
205
        /* Create the queue used to send messages to the ISR. */
206
        vI2CISRCreateQueues( i2cQUEUE_LENGTH, &xMessagesForTx, &pulBusFree );
207
 
208
        /* Configure the I2C hardware. */
209
 
210
        I2C_I2CONCLR = 0xff;
211
 
212
        PCB_PINSEL0 |= mainSDA_ENABLE;
213
        PCB_PINSEL0 |= mainSCL_ENABLE;
214
 
215
        I2C_I2SCLL = i2cTIMERL;
216
        I2C_I2SCLH = i2cTIMERH;
217
        I2C_I2CONSET = i2cACTIVE_MASTER_MODE;
218
 
219
        portENTER_CRITICAL();
220
        {
221
                /* Setup the VIC for the i2c interrupt. */
222
                VICIntSelect &= ~( i2cI2C_VIC_CHANNEL_BIT );
223
                VICIntEnable |= i2cI2C_VIC_CHANNEL_BIT;
224
                VICVectAddr2 = ( long ) vI2C_ISR_Wrapper;
225
 
226
                VICVectCntl2 = i2cI2C_VIC_CHANNEL | i2cI2C_VIC_ENABLE;
227
        }
228
        portEXIT_CRITICAL();
229
}
230
 

powered by: WebSVN 2.1.0

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