]> git.sur5r.net Git - freertos/blob - Demo/WizNET_DEMO_GCC_ARM7/i2c.c
Prepare for V5.3.0 release.
[freertos] / Demo / WizNET_DEMO_GCC_ARM7 / i2c.c
1 /*\r
2         FreeRTOS.org V5.3.0 - Copyright (C) 2003-2009 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify it\r
7         under the terms of the GNU General Public License (version 2) as published\r
8         by the Free Software Foundation and modified by the FreeRTOS exception.\r
9         **NOTE** The exception to the GPL is included to allow you to distribute a\r
10         combined work that includes FreeRTOS.org without being obliged to provide\r
11         the source code for any proprietary components.  Alternative commercial\r
12         license and support terms are also available upon request.  See the \r
13         licensing section of http://www.FreeRTOS.org for full details.\r
14 \r
15         FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
16         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
17         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
18         more details.\r
19 \r
20         You should have received a copy of the GNU General Public License along\r
21         with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59\r
22         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
23 \r
24 \r
25         ***************************************************************************\r
26         *                                                                         *\r
27         * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
28         *                                                                         *\r
29         * This is a concise, step by step, 'hands on' guide that describes both   *\r
30         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
31         * explains numerous examples that are written using the FreeRTOS API.     *\r
32         * Full source code for all the examples is provided in an accompanying    *\r
33         * .zip file.                                                              *\r
34         *                                                                         *\r
35         ***************************************************************************\r
36 \r
37         1 tab == 4 spaces!\r
38 \r
39         Please ensure to read the configuration and relevant port sections of the\r
40         online documentation.\r
41 \r
42         http://www.FreeRTOS.org - Documentation, latest information, license and\r
43         contact details.\r
44 \r
45         http://www.SafeRTOS.com - A version that is certified for use in safety\r
46         critical systems.\r
47 \r
48         http://www.OpenRTOS.com - Commercial support, development, porting,\r
49         licensing and training services.\r
50 */\r
51 \r
52 \r
53 /* Standard includes. */\r
54 #include <stdlib.h>\r
55 \r
56 /* Scheduler include files. */\r
57 #include "FreeRTOS.h"\r
58 #include "queue.h"\r
59 #include "semphr.h"\r
60 \r
61 /* Application includes. */\r
62 #include "i2c.h"\r
63 \r
64 /*-----------------------------------------------------------*/\r
65 \r
66 /* Constants to setup the microcontroller IO. */\r
67 #define mainSDA_ENABLE                  ( ( unsigned portLONG ) 0x0040 )\r
68 #define mainSCL_ENABLE                  ( ( unsigned portLONG ) 0x0010 )\r
69 \r
70 /* Bit definitions within the I2CONCLR register. */\r
71 #define i2cSTA_BIT                              ( ( unsigned portCHAR ) 0x20 )\r
72 #define i2cSI_BIT                               ( ( unsigned portCHAR ) 0x08 )\r
73 #define i2cSTO_BIT                              ( ( unsigned portCHAR ) 0x10 )\r
74 \r
75 /* Constants required to setup the VIC. */\r
76 #define i2cI2C_VIC_CHANNEL              ( ( unsigned portLONG ) 0x0009 )\r
77 #define i2cI2C_VIC_CHANNEL_BIT  ( ( unsigned portLONG ) 0x0200 )\r
78 #define i2cI2C_VIC_ENABLE               ( ( unsigned portLONG ) 0x0020 )\r
79 \r
80 /* Misc constants. */\r
81 #define i2cNO_BLOCK                             ( ( portTickType ) 0 )\r
82 #define i2cQUEUE_LENGTH                 ( ( unsigned portCHAR ) 5 )\r
83 #define i2cEXTRA_MESSAGES               ( ( unsigned portCHAR ) 2 )\r
84 #define i2cREAD_TX_LEN                  ( ( unsigned portLONG ) 2 )\r
85 #define i2cACTIVE_MASTER_MODE   ( ( unsigned portCHAR ) 0x40 )\r
86 #define i2cTIMERL                               ( 200 )\r
87 #define i2cTIMERH                               ( 200 )\r
88 \r
89 /* Array of message definitions.  See the header file for more information\r
90 on the structure members.  There are two more places in the queue than as\r
91 defined by i2cQUEUE_LENGTH.  This is to ensure that there is always a free\r
92 message available - one can be in the process of being transmitted and one\r
93 can be left free. */\r
94 static xI2CMessage xTxMessages[ i2cQUEUE_LENGTH + i2cEXTRA_MESSAGES ];\r
95 \r
96 /* Function in the ARM part of the code used to create the queues. */\r
97 extern void vI2CISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxTxMessages, unsigned portLONG **ppulBusFree );\r
98 \r
99 /* Index to the next free message in the xTxMessages array. */\r
100 unsigned portLONG ulNextFreeMessage = ( unsigned portLONG ) 0;\r
101 \r
102 /* Queue of messages that are waiting transmission. */\r
103 static xQueueHandle xMessagesForTx;\r
104 \r
105 /* Flag to indicate the state of the I2C ISR state machine. */\r
106 static unsigned portLONG *pulBusFree;\r
107 \r
108 /*-----------------------------------------------------------*/\r
109 void i2cMessage( const unsigned portCHAR * const pucMessage, portLONG lMessageLength, unsigned portCHAR ucSlaveAddress, unsigned portSHORT usBufferAddress, unsigned portLONG ulDirection, xSemaphoreHandle xMessageCompleteSemaphore, portTickType xBlockTime )\r
110 {\r
111 extern volatile xI2CMessage *pxCurrentMessage;\r
112 xI2CMessage *pxNextFreeMessage;\r
113 signed portBASE_TYPE xReturn;\r
114 \r
115         portENTER_CRITICAL();\r
116         {\r
117                 /* This message is guaranteed to be free as there are two more messages\r
118                 than spaces in the queue allowing for one message to be in process of\r
119                 being transmitted and one to be left free. */\r
120                 pxNextFreeMessage = &( xTxMessages[ ulNextFreeMessage ] );\r
121 \r
122                 /* Fill the message with the data to be sent. */\r
123 \r
124                 /* Pointer to the actual data.  Only a pointer is stored (i.e. the \r
125                 actual data is not copied, so the data being pointed to must still\r
126                 be valid when the message eventually gets sent (it may be queued for\r
127                 a while. */\r
128                 pxNextFreeMessage->pucBuffer = ( unsigned portCHAR * ) pucMessage;              \r
129 \r
130                 /* This is the address of the I2C device we are going to transmit this\r
131                 message to. */\r
132                 pxNextFreeMessage->ucSlaveAddress = ucSlaveAddress | ( unsigned portCHAR ) ulDirection;\r
133 \r
134                 /* A semaphore can be used to allow the I2C ISR to indicate that the\r
135                 message has been sent.  This can be NULL if you don't want to wait for\r
136                 the message transmission to complete. */\r
137                 pxNextFreeMessage->xMessageCompleteSemaphore = xMessageCompleteSemaphore;\r
138 \r
139                 /* How many bytes are to be sent? */\r
140                 pxNextFreeMessage->lMessageLength = lMessageLength;\r
141 \r
142                 /* The address within the WIZnet device to which the data will be \r
143                 written.  This could be the address of a register, or alternatively\r
144                 a location within the WIZnet Tx buffer. */\r
145                 pxNextFreeMessage->ucBufferAddressLowByte = ( unsigned portCHAR ) ( usBufferAddress & 0xff );\r
146 \r
147                 /* Second byte of the address. */\r
148                 usBufferAddress >>= 8;\r
149                 pxNextFreeMessage->ucBufferAddressHighByte = ( unsigned portCHAR ) ( usBufferAddress & 0xff );\r
150 \r
151                 /* Increment to the next message in the array - with a wrap around check. */\r
152                 ulNextFreeMessage++;\r
153                 if( ulNextFreeMessage >= ( i2cQUEUE_LENGTH + i2cEXTRA_MESSAGES ) )\r
154                 {\r
155                         ulNextFreeMessage = ( unsigned portLONG ) 0;\r
156                 }\r
157 \r
158                 /* Is the I2C interrupt in the middle of transmitting a message? */\r
159                 if( *pulBusFree == ( unsigned portLONG ) pdTRUE )\r
160                 {\r
161                         /* No message is currently being sent or queued to be sent.  We\r
162                         can start the ISR sending this message immediately. */\r
163                         pxCurrentMessage = pxNextFreeMessage;\r
164 \r
165                         I2C_I2CONCLR = i2cSI_BIT;       \r
166                         I2C_I2CONSET = i2cSTA_BIT;\r
167                         \r
168                         *pulBusFree = ( unsigned portLONG ) pdFALSE;\r
169                 }\r
170                 else\r
171                 {\r
172                         /* The I2C interrupt routine is mid sending a message.  Queue\r
173                         this message ready to be sent. */\r
174                         xReturn = xQueueSend( xMessagesForTx, &pxNextFreeMessage, xBlockTime );\r
175 \r
176                         /* We may have blocked while trying to queue the message.  If this\r
177                         was the case then the interrupt would have been enabled and we may\r
178                         now find that the I2C interrupt routine is no longer sending a\r
179                         message. */\r
180                         if( ( *pulBusFree == ( unsigned portLONG ) pdTRUE ) && ( xReturn == pdPASS ) )\r
181                         {\r
182                                 /* Get the next message in the queue (this should be the \r
183                                 message we just posted) and start off the transmission\r
184                                 again. */\r
185                                 xQueueReceive( xMessagesForTx, &pxNextFreeMessage, i2cNO_BLOCK );\r
186                                 pxCurrentMessage = pxNextFreeMessage;\r
187 \r
188                                 I2C_I2CONCLR = i2cSI_BIT;       \r
189                                 I2C_I2CONSET = i2cSTA_BIT;\r
190                                 \r
191                                 *pulBusFree = ( unsigned portLONG ) pdFALSE;\r
192                         }\r
193                 }\r
194         }\r
195         portEXIT_CRITICAL();\r
196 }\r
197 /*-----------------------------------------------------------*/\r
198 \r
199 void i2cInit( void )\r
200 {\r
201 extern void ( vI2C_ISR_Wrapper )( void );\r
202 \r
203         /* Create the queue used to send messages to the ISR. */\r
204         vI2CISRCreateQueues( i2cQUEUE_LENGTH, &xMessagesForTx, &pulBusFree );\r
205 \r
206         /* Configure the I2C hardware. */\r
207 \r
208         I2C_I2CONCLR = 0xff; \r
209 \r
210         PCB_PINSEL0 |= mainSDA_ENABLE;\r
211         PCB_PINSEL0 |= mainSCL_ENABLE;\r
212 \r
213         I2C_I2SCLL = i2cTIMERL;\r
214         I2C_I2SCLH = i2cTIMERH;\r
215         I2C_I2CONSET = i2cACTIVE_MASTER_MODE;\r
216 \r
217         portENTER_CRITICAL();\r
218         {\r
219                 /* Setup the VIC for the i2c interrupt. */\r
220                 VICIntSelect &= ~( i2cI2C_VIC_CHANNEL_BIT );\r
221                 VICIntEnable |= i2cI2C_VIC_CHANNEL_BIT;\r
222                 VICVectAddr2 = ( portLONG ) vI2C_ISR_Wrapper;\r
223 \r
224                 VICVectCntl2 = i2cI2C_VIC_CHANNEL | i2cI2C_VIC_ENABLE;\r
225         }\r
226         portEXIT_CRITICAL();\r
227 }\r
228 \r