NetBurner 3.5.6
PDF Version
multichanneli2c.h
1/*NB_REVISION*/
2
3/*NB_COPYRIGHT*/
4
5/*-----------------------------------------------------------------------------
6 NetBurner I2C Multi-Master I2C Driver.
7 Multi-Master I2C will enable operation as a master device on the I2C bus when
8 activly seeking to communicate. When idle, the NetBurner will act as a slave
9 device so other I2C masters can use the bus.
10
11 Note: If your NetBurner device is the only master on the I2C bus, you may
12 choose to use the Master-Only I2C driver to conserve system resources
13-------------------------------------------------------------------------------*/
14
50#ifndef _MULTICHANNELI2C_H
51#define _MULTICHANNELI2C_H
52#include <basictypes.h>
53#include <nbrtos.h>
54#include <i2c_class.h>
55
56#ifdef __cplusplus
57extern "C"
58{
59#endif
60
61#define I2C_MAX_BUF_SIZE (64) // Size allocated to input and output buffers in slave mode I2C
62
63#define DEFAULT_I2C_MODULE (0)
64
65/* defined I2C Timeout values will be used if user does not include the 'ticks_to_wait'
66 parameter when calling I2C functions */
67#define I2C_RX_TX_TIMEOUT (5) // Ticks allowed before timeout of a single byte transmission
68#define I2C_START_TIMEOUT (20) // Ticks allowed before timeout when attempting start on I2C bus
69
70#define I2C_SLAVE_TX_TERM_CHAR (0) // Terminating char to be sent when Slave TX buffer is empty
71
78#define I2C_OK (0)
79#define I2C_NEXT_WRITE_OK (1)
80#define I2C_NEXT_READ_OK (2)
81#define I2C_MASTER_OK (3)
82#define I2C_TIMEOUT (4)
83#define I2C_BUS_NOT_AVAIL (5)
84#define I2C_NOT_READY (6)
85#define I2C_LOST_ARB (7)
86#define I2C_LOST_ARB_ADD (8)
87#define I2C_NO_LINK_RX_ACK (9)
90// The following defines were created to allow the multi and master-only drivers to
91// have the same function calls. i.e. Multi_I2CInit(); is the same as calling I2CInit();
92//#define I2CInit MultiChannel_I2CInit
93//#define I2CSendBuf MultiChannel_I2CSendBuf
94//#define I2CReadBuf MultiChannel_I2CReadBuf
95//#define I2CRestart MultiChannel_I2CRestart
96//#define I2CStart MultiChannel_I2CStart
97//#define I2CStop MultiChannel_I2CStop
98//#define I2CSend MultiChannel_I2CSend
99//#define I2CRead MultiChannel_I2CRead
100
101// Setup sim references to i2c struct
102#define I2C_SR i2cModule->i2sr
103#define I2C_CR i2cModule->i2cr
104#define I2C_DR i2cModule->i2dr
105#define I2C_FDR i2cModule->i2fdr
106#define I2C_ADR i2cModule->i2adr
107
121 void MultiChannel_I2CInit(int moduleNum = DEFAULT_I2C_MODULE, uint8_t slave_Addr = 0x08, uint8_t freqdiv = 0x3C);
122
139 uint8_t MultiChannel_I2CSendBuf(int moduleNum, uint8_t addr, puint8_t buf, int num, bool stop = true);
140
157 uint8_t MultiChannel_I2CReadBuf(int moduleNum, uint8_t addr, puint8_t buf, int num, bool stop = true);
158
167
168 /*----------------------------------------------------------------------------
169 bool I2CRXAvail();
170
171 returns true if data is available in the Slave RX buffer
172 */
173 bool I2CRXAvail();
174
175 /*----------------------------------------------------------------------------
176 uint32_t I2CTXAvail();
177
178 returns amount of free space available in Slave TX buffer
179 */
180 uint32_t I2CTXAvail();
181
182 /*----------------------------------------------------------------------------
183 uint8_t I2CGetByte();
184
185 This function will pend on a slave recieve I2C semaphore
186
187 returns last unread uint8_t received as a I2C Slave
188 */
189 uint8_t I2CGetByte();
190
191 /*----------------------------------------------------------------------------
192 bool I2CFillSlaveTXBuf(puint8_t buf, uint32_t num, bool restart = true);
193
194 This will put the first 'num' BYTES from the buffer 'buf' into the
195 I2C Slave TX buffer and clear the previously-read contents of the buffer.
196 The 'restart' parameter tells the slave transmitter how to terminate
197 if 'restart' = true then restart next tx from begining TX buffer (A new slave fill replaces buffer);
198 if 'false' continue next tx from last slave tx,(A new slave fill adds to buffer at last read byte);
199 In I2C multi.h there is a I2C_SLAVE_TX_TERM_CHAR defined that will decide the terminating char (if any) to send
200 when the slave tx buffer is empty
201
202 returns false if failed to copy data
203 */
204 uint8_t I2CFillSlaveTXBuf(puint8_t buf, uint32_t num, bool restart = true);
205
206 /*----------------------------------------------------------------------------
207 uint8_t ( *I2C_SlaveTX_Callback )( );
208
209 If this callback is pointed to a function then it will be called when
210 retreiving the data to be sent to the master. This will bypass the circular
211 buffer functions "I2CTXAvail" and "I2CFillSlaveTXBuf" used when transmitting
212 data as a slave.
213
214 returns the data to be sent to the master
215 */
216 extern uint8_t (*I2C_SlaveTX_Callback)();
217
218 /*----------------------------------------------------------------------------
219 void ( *I2C_SlaveTX_NAK_Callback )( );
220
221 If this callback is pointed to a function then it will be called when
222 the recieving master device sends a NAK. This indicates that the master
223 device will not be reading any more data from the slave transmitter.
224
225 */
226 extern void (*I2C_SlaveTX_NAK_Callback)();
227
228 /*----------------------------------------------------------------------------
229 void ( *I2C_SlaveRX_Callback )( uint8_t RX_Data);
230
231 If this callback is pointed to a function then it will be called when
232 storing the data received from the master. This will bypass the circular
233 buffer functions "I2CRXAvail" and "I2CGetByte" used when receiving
234 data as a slave.
235
236 uint8_t RX_Data - Passes the received data to the callback function
237
238 returns nothing
239 */
240 extern void (*I2C_SlaveRX_Callback)(uint8_t RX_Data);
241
242#define I2C_START_READ (1) // defines to be used for bRead_Not_Write
243#define I2C_START_WRITE (0)
261 uint8_t MultiChannel_I2CRestart(int moduleNum, uint8_t addr, bool Read_Not_Write, uint32_t ticks_to_wait = I2C_RX_TX_TIMEOUT);
262
263 /*------------------------------------------------------------------------------------------
264 Advanced NetBurner I2C Functions
265
266 The following functions are used for advanced communications on using the I2C bus. These
267 functions are useful if user wishes to talk to a device that does not follow the Phillips
268 I2C standard. One example is an eeprom that first must be addressed and then read from
269 with no restart in between.
270 Data must be sent or received a byte at a time. Also user must check function returns
271 to verify the status of the bus before performing the next option.
272 -------------------------------------------------------------------------------------------
273 ===========================================================================================*/
274
292 uint8_t MultiChannel_I2CStart(int moduleNum, uint8_t addr, bool Read_Not_Write, uint32_t ticks_to_wait = I2C_START_TIMEOUT);
293
307 uint8_t MultiChannel_I2CStop(int moduleNum = DEFAULT_I2C_MODULE, uint32_t ticks_to_wait = I2C_RX_TX_TIMEOUT);
308
325 uint8_t MultiChannel_I2CSend(int moduleNum, uint8_t val, uint32_t ticks_to_wait = I2C_RX_TX_TIMEOUT);
326
347 uint8_t MultiChannel_I2CRead(int moduleNum, puint8_t val, uint32_t ticks_to_wait = I2C_RX_TX_TIMEOUT);
348
349 // Struct that contains I2C Slave I/O Buffers
350 struct I2C_Slave_Record
351 {
352 OS_SEM I2C_Slave_RX_Semaphore; // semaphore used to determine when a slave RX interrupt occurs
353
354 volatile uint8_t *pI2CRxbuf;
355 volatile uint8_t *pI2CTxbuf;
356
357 volatile uint32_t I2Crx_put;
358 volatile uint32_t I2Crx_get;
359
360 volatile uint32_t I2Ctx_put;
361 volatile uint32_t I2Ctx_get;
362 };
363
364/*---------------------------------------------------------------------------
365 Useful I2C macros
366----------------------------------------------------------------------------*/
372#define I2C_SR_BUSY (((0x20 & I2C_SR) == 0x20))
373#define I2C_CR_SLAVE (((0x20 & I2C_CR) == 0x00))
374#define I2C_SR_ARB_LOST (((0x10 & I2C_SR) == 0x10))
375#define I2C_SR_ADRES_AS_SLAVE (((0x40 & I2C_SR) == 0x40))
376#define I2C_SR_SLAVE_TX (((0x04 & I2C_SR) == 0x04))
377
378#define I2C_CR_TX (((0x10 & I2C_CR) == 0x10))
379#define I2C_SR_RX_ACK (((0x01 & I2C_SR) == 0x00))
380#define I2C_CR_RX_ACK (((0x08 & I2C_CR) == 0x00))
381
382#define I2C_SET_NO_ACK ((I2C_CR |= 0x08))
383#define I2C_SET_ACK ((I2C_CR &= 0xF7))
384#define I2C_SET_TX ((I2C_CR |= 0x10))
385#define I2C_SET_RX ((I2C_CR &= 0xEF))
386#define I2C_SET_REPEAT_START ((I2C_CR |= 0x04))
387#define I2C_CLR_ARB_LOST ((I2C_SR &= 0xEF))
390 //----------------------------------------------------------------------------
391
392 inline void I2CInit(uint8_t slave_Addr = 0x08, uint8_t freqdiv = 0x3C)
393 {
394 MultiChannel_I2CInit(DEFAULT_I2C_MODULE, slave_Addr, freqdiv);
395 }
396
397 inline void Mulit_I2CInit(uint8_t slave_Addr = 0x08, uint8_t freqdiv = 0x3C)
398 {
399 MultiChannel_I2CInit(DEFAULT_I2C_MODULE, slave_Addr, freqdiv);
400 }
401
402 inline uint8_t I2CSendBuf(uint8_t addr, puint8_t buf, int num, bool stop = true)
403 {
404 return MultiChannel_I2CSendBuf(DEFAULT_I2C_MODULE, addr, buf, num, stop);
405 }
406
407 inline uint8_t Multi_I2CSendBuf(uint8_t addr, puint8_t buf, int num, bool stop = true)
408 {
409 return MultiChannel_I2CSendBuf(DEFAULT_I2C_MODULE, addr, buf, num, stop);
410 }
411
412 inline uint8_t I2CReadBuf(uint8_t addr, puint8_t buf, int num, bool stop = true)
413 {
414 return MultiChannel_I2CReadBuf(DEFAULT_I2C_MODULE, addr, buf, num, stop);
415 }
416
417 inline uint8_t Multi_I2CReadBuf(uint8_t addr, puint8_t buf, int num, bool stop = true)
418 {
419 return MultiChannel_I2CReadBuf(DEFAULT_I2C_MODULE, addr, buf, num, stop);
420 }
421
422 inline uint8_t I2CRestart(uint8_t addr, bool Read_Not_Write, uint32_t ticks_to_wait = I2C_RX_TX_TIMEOUT)
423 {
424 return MultiChannel_I2CRestart(DEFAULT_I2C_MODULE, addr, Read_Not_Write, ticks_to_wait);
425 }
426
427 inline uint8_t Multi_I2CRestart(uint8_t addr, bool Read_Not_Write, uint32_t ticks_to_wait = I2C_RX_TX_TIMEOUT)
428 {
429 return MultiChannel_I2CRestart(DEFAULT_I2C_MODULE, addr, Read_Not_Write, ticks_to_wait);
430 }
431
432 inline uint8_t I2CStart(uint8_t addr, bool Read_Not_Write, uint32_t ticks_to_wait = I2C_RX_TX_TIMEOUT)
433 {
434 return MultiChannel_I2CStart(DEFAULT_I2C_MODULE, addr, Read_Not_Write, ticks_to_wait);
435 }
436
437 inline uint8_t Multi_I2CStart(uint8_t addr, bool Read_Not_Write, uint32_t ticks_to_wait = I2C_RX_TX_TIMEOUT)
438 {
439 return MultiChannel_I2CStart(DEFAULT_I2C_MODULE, addr, Read_Not_Write, ticks_to_wait);
440 }
441
442 inline uint8_t I2CStop(uint32_t ticks_to_wait = I2C_RX_TX_TIMEOUT) { return MultiChannel_I2CStop(DEFAULT_I2C_MODULE, ticks_to_wait); }
443
444 inline uint8_t Multi_I2CStop(uint32_t ticks_to_wait = I2C_RX_TX_TIMEOUT)
445 {
446 return MultiChannel_I2CStop(DEFAULT_I2C_MODULE, ticks_to_wait);
447 }
448
449 inline uint8_t I2CSend(uint8_t val, uint32_t ticks_to_wait = I2C_RX_TX_TIMEOUT)
450 {
451 return MultiChannel_I2CSend(DEFAULT_I2C_MODULE, val, ticks_to_wait);
452 }
453
454 inline uint8_t Multi_I2CSend(uint8_t val, uint32_t ticks_to_wait = I2C_RX_TX_TIMEOUT)
455 {
456 return MultiChannel_I2CSend(DEFAULT_I2C_MODULE, val, ticks_to_wait);
457 }
458
459 inline uint8_t I2CRead(puint8_t val, uint32_t ticks_to_wait = I2C_RX_TX_TIMEOUT)
460 {
461 return MultiChannel_I2CRead(DEFAULT_I2C_MODULE, val, ticks_to_wait);
462 }
463
464 inline uint8_t Multi_I2CRead(puint8_t val, uint32_t ticks_to_wait = I2C_RX_TX_TIMEOUT)
465 {
466 return MultiChannel_I2CRead(DEFAULT_I2C_MODULE, val, ticks_to_wait);
467 }
468
469#ifdef __cplusplus
470}
471#endif
472
473#endif
474
uint8_t MultiChannel_I2CRead(int moduleNum, puint8_t val, uint32_t ticks_to_wait=I2C_RX_TX_TIMEOUT)
Read a single byte from the I2C bus.
void MultiChannel_I2CInit(int moduleNum=DEFAULT_I2C_MODULE, uint8_t slave_Addr=0x08, uint8_t freqdiv=0x3C)
Initialize the I2C peripheral module.
uint8_t MultiChannel_I2CStop(int moduleNum=DEFAULT_I2C_MODULE, uint32_t ticks_to_wait=I2C_RX_TX_TIMEOUT)
Issue an I2C stop terminate communication with an I2C device and release the bus.
uint8_t MultiChannel_I2CSend(int moduleNum, uint8_t val, uint32_t ticks_to_wait=I2C_RX_TX_TIMEOUT)
Send a single byte on the I2C bus.
uint8_t MultiChannel_I2CSendBuf(int moduleNum, uint8_t addr, puint8_t buf, int num, bool stop=true)
Send a buffer of bytes to an I2C device.
void I2CMultiChannelResetPeripheral(int moduleNum)
Reset the specified I2C peripheral module.
uint8_t MultiChannel_I2CReadBuf(int moduleNum, uint8_t addr, puint8_t buf, int num, bool stop=true)
Read a number of bytes from an I2C device and store in the specified buffer.
uint8_t MultiChannel_I2CStart(int moduleNum, uint8_t addr, bool Read_Not_Write, uint32_t ticks_to_wait=I2C_START_TIMEOUT)
Send an I2C start to an I2C device to begin communication.
uint8_t MultiChannel_I2CRestart(int moduleNum, uint8_t addr, bool Read_Not_Write, uint32_t ticks_to_wait=I2C_RX_TX_TIMEOUT)
Restart communication with a I2C device.