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