2 * FreeRTOS Kernel V10.0.0
\r
3 * Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy of
\r
6 * this software and associated documentation files (the "Software"), to deal in
\r
7 * the Software without restriction, including without limitation the rights to
\r
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
\r
9 * the Software, and to permit persons to whom the Software is furnished to do so,
\r
10 * subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in all
\r
13 * copies or substantial portions of the Software. If you wish to use our Amazon
\r
14 * FreeRTOS name, please do so in a fair use way that does not cause confusion.
\r
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
\r
18 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
\r
19 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
20 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 * http://www.FreeRTOS.org
\r
24 * http://aws.amazon.com/freertos
\r
26 * 1 tab == 4 spaces!
\r
30 /* Standard includes. */
\r
33 /* Scheduler include files. */
\r
34 #include "FreeRTOS.h"
\r
38 /* Application includes. */
\r
41 /*-----------------------------------------------------------*/
\r
43 /* Constants to setup the microcontroller IO. */
\r
44 #define mainSDA_ENABLE ( ( unsigned long ) 0x0040 )
\r
45 #define mainSCL_ENABLE ( ( unsigned long ) 0x0010 )
\r
47 /* Bit definitions within the I2CONCLR register. */
\r
48 #define i2cSTA_BIT ( ( unsigned char ) 0x20 )
\r
49 #define i2cSI_BIT ( ( unsigned char ) 0x08 )
\r
50 #define i2cSTO_BIT ( ( unsigned char ) 0x10 )
\r
52 /* Constants required to setup the VIC. */
\r
53 #define i2cI2C_VIC_CHANNEL ( ( unsigned long ) 0x0009 )
\r
54 #define i2cI2C_VIC_CHANNEL_BIT ( ( unsigned long ) 0x0200 )
\r
55 #define i2cI2C_VIC_ENABLE ( ( unsigned long ) 0x0020 )
\r
57 /* Misc constants. */
\r
58 #define i2cNO_BLOCK ( ( TickType_t ) 0 )
\r
59 #define i2cQUEUE_LENGTH ( ( unsigned char ) 5 )
\r
60 #define i2cEXTRA_MESSAGES ( ( unsigned char ) 2 )
\r
61 #define i2cREAD_TX_LEN ( ( unsigned long ) 2 )
\r
62 #define i2cACTIVE_MASTER_MODE ( ( unsigned char ) 0x40 )
\r
63 #define i2cTIMERL ( 200 )
\r
64 #define i2cTIMERH ( 200 )
\r
66 /* Array of message definitions. See the header file for more information
\r
67 on the structure members. There are two more places in the queue than as
\r
68 defined by i2cQUEUE_LENGTH. This is to ensure that there is always a free
\r
69 message available - one can be in the process of being transmitted and one
\r
70 can be left free. */
\r
71 static xI2CMessage xTxMessages[ i2cQUEUE_LENGTH + i2cEXTRA_MESSAGES ];
\r
73 /* Function in the ARM part of the code used to create the queues. */
\r
74 extern void vI2CISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, QueueHandle_t *pxTxMessages, unsigned long **ppulBusFree );
\r
76 /* Index to the next free message in the xTxMessages array. */
\r
77 unsigned long ulNextFreeMessage = ( unsigned long ) 0;
\r
79 /* Queue of messages that are waiting transmission. */
\r
80 static QueueHandle_t xMessagesForTx;
\r
82 /* Flag to indicate the state of the I2C ISR state machine. */
\r
83 static unsigned long *pulBusFree;
\r
85 /*-----------------------------------------------------------*/
\r
86 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
88 extern volatile xI2CMessage *pxCurrentMessage;
\r
89 xI2CMessage *pxNextFreeMessage;
\r
90 signed portBASE_TYPE xReturn;
\r
92 portENTER_CRITICAL();
\r
94 /* This message is guaranteed to be free as there are two more messages
\r
95 than spaces in the queue allowing for one message to be in process of
\r
96 being transmitted and one to be left free. */
\r
97 pxNextFreeMessage = &( xTxMessages[ ulNextFreeMessage ] );
\r
99 /* Fill the message with the data to be sent. */
\r
101 /* Pointer to the actual data. Only a pointer is stored (i.e. the
\r
102 actual data is not copied, so the data being pointed to must still
\r
103 be valid when the message eventually gets sent (it may be queued for
\r
105 pxNextFreeMessage->pucBuffer = ( unsigned char * ) pucMessage;
\r
107 /* This is the address of the I2C device we are going to transmit this
\r
109 pxNextFreeMessage->ucSlaveAddress = ucSlaveAddress | ( unsigned char ) ulDirection;
\r
111 /* A semaphore can be used to allow the I2C ISR to indicate that the
\r
112 message has been sent. This can be NULL if you don't want to wait for
\r
113 the message transmission to complete. */
\r
114 pxNextFreeMessage->xMessageCompleteSemaphore = xMessageCompleteSemaphore;
\r
116 /* How many bytes are to be sent? */
\r
117 pxNextFreeMessage->lMessageLength = lMessageLength;
\r
119 /* The address within the WIZnet device to which the data will be
\r
120 written. This could be the address of a register, or alternatively
\r
121 a location within the WIZnet Tx buffer. */
\r
122 pxNextFreeMessage->ucBufferAddressLowByte = ( unsigned char ) ( usBufferAddress & 0xff );
\r
124 /* Second byte of the address. */
\r
125 usBufferAddress >>= 8;
\r
126 pxNextFreeMessage->ucBufferAddressHighByte = ( unsigned char ) ( usBufferAddress & 0xff );
\r
128 /* Increment to the next message in the array - with a wrap around check. */
\r
129 ulNextFreeMessage++;
\r
130 if( ulNextFreeMessage >= ( i2cQUEUE_LENGTH + i2cEXTRA_MESSAGES ) )
\r
132 ulNextFreeMessage = ( unsigned long ) 0;
\r
135 /* Is the I2C interrupt in the middle of transmitting a message? */
\r
136 if( *pulBusFree == ( unsigned long ) pdTRUE )
\r
138 /* No message is currently being sent or queued to be sent. We
\r
139 can start the ISR sending this message immediately. */
\r
140 pxCurrentMessage = pxNextFreeMessage;
\r
142 I2C_I2CONCLR = i2cSI_BIT;
\r
143 I2C_I2CONSET = i2cSTA_BIT;
\r
145 *pulBusFree = ( unsigned long ) pdFALSE;
\r
149 /* The I2C interrupt routine is mid sending a message. Queue
\r
150 this message ready to be sent. */
\r
151 xReturn = xQueueSend( xMessagesForTx, &pxNextFreeMessage, xBlockTime );
\r
153 /* We may have blocked while trying to queue the message. If this
\r
154 was the case then the interrupt would have been enabled and we may
\r
155 now find that the I2C interrupt routine is no longer sending a
\r
157 if( ( *pulBusFree == ( unsigned long ) pdTRUE ) && ( xReturn == pdPASS ) )
\r
159 /* Get the next message in the queue (this should be the
\r
160 message we just posted) and start off the transmission
\r
162 xQueueReceive( xMessagesForTx, &pxNextFreeMessage, i2cNO_BLOCK );
\r
163 pxCurrentMessage = pxNextFreeMessage;
\r
165 I2C_I2CONCLR = i2cSI_BIT;
\r
166 I2C_I2CONSET = i2cSTA_BIT;
\r
168 *pulBusFree = ( unsigned long ) pdFALSE;
\r
172 portEXIT_CRITICAL();
\r
174 /*-----------------------------------------------------------*/
\r
176 void i2cInit( void )
\r
178 extern void ( vI2C_ISR_Wrapper )( void );
\r
180 /* Create the queue used to send messages to the ISR. */
\r
181 vI2CISRCreateQueues( i2cQUEUE_LENGTH, &xMessagesForTx, &pulBusFree );
\r
183 /* Configure the I2C hardware. */
\r
185 I2C_I2CONCLR = 0xff;
\r
187 PCB_PINSEL0 |= mainSDA_ENABLE;
\r
188 PCB_PINSEL0 |= mainSCL_ENABLE;
\r
190 I2C_I2SCLL = i2cTIMERL;
\r
191 I2C_I2SCLH = i2cTIMERH;
\r
192 I2C_I2CONSET = i2cACTIVE_MASTER_MODE;
\r
194 portENTER_CRITICAL();
\r
196 /* Setup the VIC for the i2c interrupt. */
\r
197 VICIntSelect &= ~( i2cI2C_VIC_CHANNEL_BIT );
\r
198 VICIntEnable |= i2cI2C_VIC_CHANNEL_BIT;
\r
199 VICVectAddr2 = ( long ) vI2C_ISR_Wrapper;
\r
201 VICVectCntl2 = i2cI2C_VIC_CHANNEL | i2cI2C_VIC_ENABLE;
\r
203 portEXIT_CRITICAL();
\r