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