NetBurner 3.1
i2cfuncs.h
1 /* Revision: 2.8.7 */
2 
3 /******************************************************************************
4 * Copyright 1998-2018 NetBurner, Inc. ALL RIGHTS RESERVED
5 *
6 * Permission is hereby granted to purchasers of NetBurner Hardware to use or
7 * modify this computer program for any use as long as the resultant program
8 * is only executed on NetBurner provided hardware.
9 *
10 * No other rights to use this program or its derivatives in part or in
11 * whole are granted.
12 *
13 * It may be possible to license this or other NetBurner software for use on
14 * non-NetBurner Hardware. Contact sales@Netburner.com for more information.
15 *
16 * NetBurner makes no representation or warranties with respect to the
17 * performance of this computer program, and specifically disclaims any
18 * responsibility for any damages, special or consequential, connected with
19 * the use of this program.
20 *
21 * NetBurner
22 * 5405 Morehouse Dr.
23 * San Diego, CA 92121
24 * www.netburner.com
25 ******************************************************************************/
26 
27 #ifndef _I2CFUNCS_H_
28 #define _I2CFUNCS_H_
29 
30 
31 #if (defined SB70LC)
32 #include <i2cmulti.h> //Used for Multi-Master I2C
33 //#include <i2cmaster.h> //Used for Master Only I2C
34 #elif( defined NANO54415 )
35 #include <multichanneli2c.h>
36 //#include <i2cmaster.h> //Used for Master Only I2C
37 #else
38 #warning I2C features not supported for this platform
39 #endif
40 
41 #ifdef __cplusplus
42 extern "C"
43 {
44 #endif
45 
46 #define I2C_LOW_ADDR_LIMIT (0x07)
47 #define I2C_HIGH_ADDR_LIMIT (0x78)
48 
49 #define I2C_MAX_BUF_SIZE (64) // Size allocated to input and output buffers in slave mode I2C
50 #define I2C_SLAVE_TX_TERM_CHAR (0) // Terminating char to be sent when Slave TX buffer is empty
51 #define NB_FACTORY_I2C_ERR_VALUE (0) // Error value if there is a failure to extract webpage form data
52 
53 /* defined I2C Timeout values will be used if user does not include the 'ticks_to_wait'
54  parameter when calling I2C functions */
55 #define I2C_RX_TX_TIMEOUT (5) // Ticks allowed before timeout of a single byte transmission
56 #define I2C_START_TIMEOUT (20) // Ticks allowed before timeout when attempting start on I2C bus
57 
58 #define MAX_WRITE_LEN (128)
59 #define MAX_READ_LEN (128)
60 // *2 to account for inputting values in a 2-digit hex format
61 #define MAX_BUFFER_LEN ((MAX_WRITE_LEN > MAX_READ_LEN)? MAX_WRITE_LEN*2 : MAX_READ_LEN*2)
62 #define MAX_I2C_DEVICES (128)
63 
64 // These are the values the NetBurner I2C functions will return
65 #define I2C_OK ( 0 ) // Last instruction terminated correctly
66 #define I2C_NEXT_WRITE_OK ( 1 ) // I2C bus is OK for a write
67 #define I2C_NEXT_READ_OK ( 2 ) // I2C bus is OK for a read
68 #define I2C_MASTER_OK ( 3 ) // I2C finished transmission but still owns but (need to stop or restart)
69 #define I2C_TIMEOUT ( 4 ) // A timeout occurred while trying communicate on I2C bus
70 #define I2C_BUS_NOT_AVAIL ( 5 ) // A timeout occurred while trying gain I2C bus control
71 #define I2C_NOT_READY ( 6 ) // A read or write was attempted before I2C ready or during a slave transmission
72 #define I2C_LOST_ARB ( 7 ) // Lost arbitration during start
73 #define I2C_LOST_ARB_ADD ( 8 ) // Lost arbitration and then winner addressed our slave address
74 #define I2C_NO_LINK_RX_ACK ( 9 ) // We are in Master TX mode and received no ACK from slave device, possibly during start
75 
76 /* Refer to BaudRate SB70LC_I2CSpeeds[] deceleration in main.cpp for
77  * details on how to enable higher I2C bus speeds.
78  */
79 #define SB70LC_DIVIDER_COUNT (35)
80 //#define SB70LC_DIVIDER_COUNT (50)
81 
82 struct ModuleI2CAddress_Struct
83 {
84  unsigned char byteAddress;
85  unsigned char asciiAddress[3];
86 };
87 
88 // This structure stores the I2C address retrieved from Non-volatile memory
89 //typedef struct NVSettings_Struct
90 //{
91 // char slaveI2CAddress[3];
92 //};
93 
94 // Stores ascii and byte versions of I2C parameters
95 struct I2CParam_Struct
96 {
97  char asciiAddress[3];
98  char asciiData[MAX_BUFFER_LEN];
99  char asciiLength[3];
100  unsigned char byteAddress;
101  unsigned char byteData;
102  unsigned char byteBuffer[MAX_BUFFER_LEN];
103  int length;
104 };
105 
106 // Used for handling the web I2C functions
107 struct I2CWebFuncs_Struct
108 {
109  I2CParam_Struct WriteReadByteParams;
110  I2CParam_Struct WriteReadBufferParams;
111  I2CParam_Struct WriteByteParams;
112  I2CParam_Struct ReadByteParams;
113  I2CParam_Struct WriteBufferParams;
114  I2CParam_Struct ReadBufferParams;
115 };
116 
117 // Used for handling the serial I2C functions
118 struct SerialParams_Struct
119 {
120  //int strLen;
121  unsigned char buf[3];
122  unsigned char byteData;
123  unsigned char length;
124  unsigned char addr;
125  unsigned char byteToWrite;
126  unsigned char commBuffer[MAX_BUFFER_LEN];
127 };
128 
129 struct I2CBusStatus_Struct
130 {
131  volatile int status;
132  const char* statusMsgs[12];
133 
134 };
135 
136 
137 
138 
139 #if ( defined MCF5270 || defined MCF5234 || defined MCF5208 )
140 void InitI2C( unsigned char slave_Addr = 0x08, unsigned char freqdiv = 0x16 );
141 #elif ( defined MCF5213 || defined MCF5282 )
142 void InitI2C( unsigned char slave_Addr = 0x08, unsigned char freqdiv = 0x15 );
143 #elif ( defined MCF52234 )
144 void InitI2C( unsigned char slave_Addr = 0x08, unsigned char freqdiv = 0x37 );
145 #elif (defined MCF5441X)
146 void InitI2C (unsigned char slave_Addr = 0x08, unsigned char freqdiv = 0x3C );
147 #else
148 #error Multi_I2CInit declaration missing for defined platform
149 #endif
150 
151 
152 unsigned char Ascii2Byte( char* buf );
153 unsigned char I2CReadBuffer(unsigned char addr, unsigned char *buf, int num, bool stop = true);
154 unsigned char I2CReadByte( unsigned char address, unsigned char *val);
155 unsigned char I2CStopBus();
156 int I2CScan(bool* discovered, unsigned char slaveAddress, unsigned char ic = 0x16);
157 void I2CStatus();
158 unsigned char I2CWriteBuffer(unsigned char address, unsigned char *buf, int num, bool stop);
159 unsigned char I2CWriteByte(unsigned char address, unsigned char val, unsigned long ticksToWait = 5);
160 unsigned char I2CWriteReadBuffer(unsigned char address, unsigned char writeVal, unsigned char *readBuffer, int length);
161 unsigned char I2CWriteReadByte(unsigned char address, unsigned char &data, unsigned char *buffer);
162 
163 
164 #ifdef __cplusplus
165 }
166 
167 #endif // __cplusplus
168 #endif // _I2CFUNCS_H_