]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/WizNET_DEMO_GCC_ARM7/i2c.c
dfebdbed7ecbbee899097222d82901a8a07c1700
[freertos] / FreeRTOS / Demo / WizNET_DEMO_GCC_ARM7 / i2c.c
1 /*\r
2     FreeRTOS V8.0.0:rc1 - Copyright (C) 2014 Real Time Engineers Ltd. \r
3     All rights reserved\r
4 \r
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>! NOTE: The modification to the GPL is included to allow you to distribute\r
28     >>! a combined work that includes FreeRTOS without being obliged to provide\r
29     >>! the source code for proprietary components outside of the FreeRTOS\r
30     >>! kernel.\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 \r
67 /* Standard includes. */\r
68 #include <stdlib.h>\r
69 \r
70 /* Scheduler include files. */\r
71 #include "FreeRTOS.h"\r
72 #include "queue.h"\r
73 #include "semphr.h"\r
74 \r
75 /* Application includes. */\r
76 #include "i2c.h"\r
77 \r
78 /*-----------------------------------------------------------*/\r
79 \r
80 /* Constants to setup the microcontroller IO. */\r
81 #define mainSDA_ENABLE                  ( ( unsigned long ) 0x0040 )\r
82 #define mainSCL_ENABLE                  ( ( unsigned long ) 0x0010 )\r
83 \r
84 /* Bit definitions within the I2CONCLR register. */\r
85 #define i2cSTA_BIT                              ( ( unsigned char ) 0x20 )\r
86 #define i2cSI_BIT                               ( ( unsigned char ) 0x08 )\r
87 #define i2cSTO_BIT                              ( ( unsigned char ) 0x10 )\r
88 \r
89 /* Constants required to setup the VIC. */\r
90 #define i2cI2C_VIC_CHANNEL              ( ( unsigned long ) 0x0009 )\r
91 #define i2cI2C_VIC_CHANNEL_BIT  ( ( unsigned long ) 0x0200 )\r
92 #define i2cI2C_VIC_ENABLE               ( ( unsigned long ) 0x0020 )\r
93 \r
94 /* Misc constants. */\r
95 #define i2cNO_BLOCK                             ( ( TickType_t ) 0 )\r
96 #define i2cQUEUE_LENGTH                 ( ( unsigned char ) 5 )\r
97 #define i2cEXTRA_MESSAGES               ( ( unsigned char ) 2 )\r
98 #define i2cREAD_TX_LEN                  ( ( unsigned long ) 2 )\r
99 #define i2cACTIVE_MASTER_MODE   ( ( unsigned char ) 0x40 )\r
100 #define i2cTIMERL                               ( 200 )\r
101 #define i2cTIMERH                               ( 200 )\r
102 \r
103 /* Array of message definitions.  See the header file for more information\r
104 on the structure members.  There are two more places in the queue than as\r
105 defined by i2cQUEUE_LENGTH.  This is to ensure that there is always a free\r
106 message available - one can be in the process of being transmitted and one\r
107 can be left free. */\r
108 static xI2CMessage xTxMessages[ i2cQUEUE_LENGTH + i2cEXTRA_MESSAGES ];\r
109 \r
110 /* Function in the ARM part of the code used to create the queues. */\r
111 extern void vI2CISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, QueueHandle_t *pxTxMessages, unsigned long **ppulBusFree );\r
112 \r
113 /* Index to the next free message in the xTxMessages array. */\r
114 unsigned long ulNextFreeMessage = ( unsigned long ) 0;\r
115 \r
116 /* Queue of messages that are waiting transmission. */\r
117 static QueueHandle_t xMessagesForTx;\r
118 \r
119 /* Flag to indicate the state of the I2C ISR state machine. */\r
120 static unsigned long *pulBusFree;\r
121 \r
122 /*-----------------------------------------------------------*/\r
123 void i2cMessage( const unsigned char * const pucMessage, long lMessageLength, unsigned char ucSlaveAddress, unsigned short usBufferAddress, unsigned long ulDirection, SemaphoreHandle_t xMessageCompleteSemaphore, TickType_t xBlockTime )\r
124 {\r
125 extern volatile xI2CMessage *pxCurrentMessage;\r
126 xI2CMessage *pxNextFreeMessage;\r
127 signed portBASE_TYPE xReturn;\r
128 \r
129         portENTER_CRITICAL();\r
130         {\r
131                 /* This message is guaranteed to be free as there are two more messages\r
132                 than spaces in the queue allowing for one message to be in process of\r
133                 being transmitted and one to be left free. */\r
134                 pxNextFreeMessage = &( xTxMessages[ ulNextFreeMessage ] );\r
135 \r
136                 /* Fill the message with the data to be sent. */\r
137 \r
138                 /* Pointer to the actual data.  Only a pointer is stored (i.e. the \r
139                 actual data is not copied, so the data being pointed to must still\r
140                 be valid when the message eventually gets sent (it may be queued for\r
141                 a while. */\r
142                 pxNextFreeMessage->pucBuffer = ( unsigned char * ) pucMessage;          \r
143 \r
144                 /* This is the address of the I2C device we are going to transmit this\r
145                 message to. */\r
146                 pxNextFreeMessage->ucSlaveAddress = ucSlaveAddress | ( unsigned char ) ulDirection;\r
147 \r
148                 /* A semaphore can be used to allow the I2C ISR to indicate that the\r
149                 message has been sent.  This can be NULL if you don't want to wait for\r
150                 the message transmission to complete. */\r
151                 pxNextFreeMessage->xMessageCompleteSemaphore = xMessageCompleteSemaphore;\r
152 \r
153                 /* How many bytes are to be sent? */\r
154                 pxNextFreeMessage->lMessageLength = lMessageLength;\r
155 \r
156                 /* The address within the WIZnet device to which the data will be \r
157                 written.  This could be the address of a register, or alternatively\r
158                 a location within the WIZnet Tx buffer. */\r
159                 pxNextFreeMessage->ucBufferAddressLowByte = ( unsigned char ) ( usBufferAddress & 0xff );\r
160 \r
161                 /* Second byte of the address. */\r
162                 usBufferAddress >>= 8;\r
163                 pxNextFreeMessage->ucBufferAddressHighByte = ( unsigned char ) ( usBufferAddress & 0xff );\r
164 \r
165                 /* Increment to the next message in the array - with a wrap around check. */\r
166                 ulNextFreeMessage++;\r
167                 if( ulNextFreeMessage >= ( i2cQUEUE_LENGTH + i2cEXTRA_MESSAGES ) )\r
168                 {\r
169                         ulNextFreeMessage = ( unsigned long ) 0;\r
170                 }\r
171 \r
172                 /* Is the I2C interrupt in the middle of transmitting a message? */\r
173                 if( *pulBusFree == ( unsigned long ) pdTRUE )\r
174                 {\r
175                         /* No message is currently being sent or queued to be sent.  We\r
176                         can start the ISR sending this message immediately. */\r
177                         pxCurrentMessage = pxNextFreeMessage;\r
178 \r
179                         I2C_I2CONCLR = i2cSI_BIT;       \r
180                         I2C_I2CONSET = i2cSTA_BIT;\r
181                         \r
182                         *pulBusFree = ( unsigned long ) pdFALSE;\r
183                 }\r
184                 else\r
185                 {\r
186                         /* The I2C interrupt routine is mid sending a message.  Queue\r
187                         this message ready to be sent. */\r
188                         xReturn = xQueueSend( xMessagesForTx, &pxNextFreeMessage, xBlockTime );\r
189 \r
190                         /* We may have blocked while trying to queue the message.  If this\r
191                         was the case then the interrupt would have been enabled and we may\r
192                         now find that the I2C interrupt routine is no longer sending a\r
193                         message. */\r
194                         if( ( *pulBusFree == ( unsigned long ) pdTRUE ) && ( xReturn == pdPASS ) )\r
195                         {\r
196                                 /* Get the next message in the queue (this should be the \r
197                                 message we just posted) and start off the transmission\r
198                                 again. */\r
199                                 xQueueReceive( xMessagesForTx, &pxNextFreeMessage, i2cNO_BLOCK );\r
200                                 pxCurrentMessage = pxNextFreeMessage;\r
201 \r
202                                 I2C_I2CONCLR = i2cSI_BIT;       \r
203                                 I2C_I2CONSET = i2cSTA_BIT;\r
204                                 \r
205                                 *pulBusFree = ( unsigned long ) pdFALSE;\r
206                         }\r
207                 }\r
208         }\r
209         portEXIT_CRITICAL();\r
210 }\r
211 /*-----------------------------------------------------------*/\r
212 \r
213 void i2cInit( void )\r
214 {\r
215 extern void ( vI2C_ISR_Wrapper )( void );\r
216 \r
217         /* Create the queue used to send messages to the ISR. */\r
218         vI2CISRCreateQueues( i2cQUEUE_LENGTH, &xMessagesForTx, &pulBusFree );\r
219 \r
220         /* Configure the I2C hardware. */\r
221 \r
222         I2C_I2CONCLR = 0xff; \r
223 \r
224         PCB_PINSEL0 |= mainSDA_ENABLE;\r
225         PCB_PINSEL0 |= mainSCL_ENABLE;\r
226 \r
227         I2C_I2SCLL = i2cTIMERL;\r
228         I2C_I2SCLH = i2cTIMERH;\r
229         I2C_I2CONSET = i2cACTIVE_MASTER_MODE;\r
230 \r
231         portENTER_CRITICAL();\r
232         {\r
233                 /* Setup the VIC for the i2c interrupt. */\r
234                 VICIntSelect &= ~( i2cI2C_VIC_CHANNEL_BIT );\r
235                 VICIntEnable |= i2cI2C_VIC_CHANNEL_BIT;\r
236                 VICVectAddr2 = ( long ) vI2C_ISR_Wrapper;\r
237 \r
238                 VICVectCntl2 = i2cI2C_VIC_CHANNEL | i2cI2C_VIC_ENABLE;\r
239         }\r
240         portEXIT_CRITICAL();\r
241 }\r
242 \r