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