Netcruzer Library API  V2.03
 All Data Structures Files Functions Variables Typedefs Enumerations Macros Groups Pages
nz_serI2C.h
Go to the documentation of this file.
1 
106 // I2C - documentation module
109 
472 #ifndef NZ_SERI2C_H
473 #define NZ_SERI2C_H
474 
475 #if defined(HAS_SERPORT_I2C)
476 
477 #include "nz_circularBuffer.h"
478 
479 
481 
483 #ifndef I2C_BAUD_62K
484 #define I2C_BAUD_62K 255 /* This is the slowest possible BAUD rate! */
485 #endif
486 
488 #ifndef I2C_BAUD_100K
489 #define I2C_BAUD_100K 157
490 #endif
491 
493 #ifndef I2C_BAUD_200K
494 #define I2C_BAUD_200K 77
495 #endif
496 
498 #ifndef I2C_BAUD_300K
499 #define I2C_BAUD_300K 51
500 #endif
501 
503 #ifndef I2C_BAUD_400K
504 #define I2C_BAUD_400K 37
505 #endif
506 
508 #ifndef I2C_BAUD_506K
509 #define I2C_BAUD_506K 24
510 #endif
511 
513 #ifndef I2C_BAUD_600K
514 #define I2C_BAUD_600K 24
515 #endif
516 
518 #ifndef I2C_BAUD_708K
519 #define I2C_BAUD_708K 20
520 #endif
521 
523 #ifndef I2C_BAUD_909K
524 #define I2C_BAUD_909K 15
525 #endif
526 
528 #ifndef I2C_BAUD_970K
529 #define I2C_BAUD_970K 12
530 #endif
531 
532 //1.03 mbits/sec Baud rate */
533 #ifndef I2C_BAUD_1M
534 #define I2C_BAUD_1M 13
535 #endif
536 
537 #ifndef I2C_ALWAYS_ADD_WHOLE_MESSAGE
538 #define I2C_ALWAYS_ADD_WHOLE_MESSAGE 0
539 #endif
540 #ifndef I2C2_ALWAYS_ADD_WHOLE_MESSAGE
541 #define I2C2_ALWAYS_ADD_WHOLE_MESSAGE 0
542 #endif
543 #ifndef I2C3_ALWAYS_ADD_WHOLE_MESSAGE
544 #define I2C3_ALWAYS_ADD_WHOLE_MESSAGE 0
545 #endif
546 
547 //Transmit and Receive buffer sizes
548 #if !defined(I2C1_RXBUF_SIZE)
549 #define I2C1_RXBUF_SIZE 64
550 #endif
551 #if !defined(I2C1_TXBUF_SIZE)
552 #define I2C1_TXBUF_SIZE 256
553 #endif
554 #if !defined(I2C2_RXBUF_SIZE)
555 #define I2C2_RXBUF_SIZE 32
556 #endif
557 #if !defined(I2C2_TXBUF_SIZE)
558 #define I2C2_TXBUF_SIZE 128
559 #endif
560 #if !defined(I2C3_RXBUF_SIZE)
561 #define I2C3_RXBUF_SIZE 32
562 #endif
563 #if !defined(I2C3_TXBUF_SIZE)
564 #define I2C3_TXBUF_SIZE 128
565 #endif
566 
567 
569 
570 //I2C_TXION_STATUS_xx defines, 0=OK, 1=Added to Buffer, 0x80-0x8F=Error, 0xFF=Status Not Available
571 #define I2C_TXION_STATUS_OK 0 //Currently OK, have not finished adding whole message to buffer
572 #define I2C_TXION_STATUS_ADDED_TO_BUF 1 //Successfully added whole message to the buffer (from start to stop)
573 #define I2C_TXION_STATUS_ERROR 4 //No space in buffer to add message
574 #define I2C_TXION_STATUS_ERR_NOSPACE 5 //No space in buffer to add message
575 #define I2C_TXION_STATUS_ERR_ADR_NOACK 6 //Slave did not ACK address we sent
576 #define I2C_TXION_STATUS_ERR_DAT_NOACK 7 //Slave did not ACK data we sent
577 #define I2C_TXION_STATUS_ERR_BUF_FRMT 8 //Buffer format error
578 #define I2C_TXION_STATUS_ERR_TIMEOUT 9 //Timeout
579 #define I2C_TXION_STATUS_NOT_AVAILABLE 0xFF //Status not available
580 
581 //I2C "Serial Data Port"
582 #if (defined(HAS_SERPORT_I2C1) || defined(HAS_SERPORT_I2C2) || defined(HAS_SERPORT_I2C3))
583  #if !defined(HAS_A_SERPORT)
584  #define HAS_A_SERPORT
585  #endif
586 #endif
587 
588 
590 
591 //If "Power of 2" buffer used, size must by a power of 2
592 #if defined(CIRBUF_USE_CIRCULAR_BUFFER_PWR2)
593 #if defined(HAS_SERPORT_I2C1)
594  //Ensure size is power of 2
595  #if ((I2C1_TXBUF_SIZE & ~(I2C1_TXBUF_SIZE-1))!=I2C1_TXBUF_SIZE)
596  #error "I2C1 I2C1_TXBUF_SIZE is not power of 2! Either change size defined in projdefs.h --OR-- use nz_circularBufferStd.c in stead of nz_circularBufferPwr2.c for project!"
597  #endif
598 
599  //Ensure size is power of 2
600  #if ((I2C1_RXBUF_SIZE & ~(I2C1_RXBUF_SIZE-1))!=I2C1_RXBUF_SIZE)
601  #error "I2C1 I2C1_RXBUF_SIZE is not power of 2! Either change size defined in projdefs.h --OR-- use nz_circularBufferStd.c in stead of nz_circularBufferPwr2.c for project!"
602  #endif
603 #endif
604 #if defined(HAS_SERPORT_I2C2)
605  //Ensure size is power of 2
606  #if ((I2C2_TXBUF_SIZE & ~(I2C2_TXBUF_SIZE-1))!=I2C2_TXBUF_SIZE)
607  #error "I2C2 I2C2_TXBUF_SIZE is not power of 2! Either change size defined in projdefs.h --OR-- use nz_circularBufferStd.c in stead of nz_circularBufferPwr2.c for project!"
608  #endif
609 
610  //Ensure size is power of 2
611  #if ((I2C2_RXBUF_SIZE & ~(I2C2_RXBUF_SIZE-1))!=I2C2_RXBUF_SIZE)
612  #error "I2C2 I2C2_RXBUF_SIZE is not power of 2! Either change size defined in projdefs.h --OR-- use nz_circularBufferStd.c in stead of nz_circularBufferPwr2.c for project!"
613  #endif
614 #endif
615 #if defined(HAS_SERPORT_I2C3)
616  //Ensure size is power of 2
617  #if ((I2C3_TXBUF_SIZE & ~(I2C3_TXBUF_SIZE-1))!=I2C3_TXBUF_SIZE)
618  #error "I2C3 I2C3_TXBUF_SIZE is not power of 2! Either change size defined in projdefs.h --OR-- use nz_circularBufferStd.c in stead of nz_circularBufferPwr2.c for project!"
619  #endif
620 
621  //Ensure size is power of 2
622  #if ((I2C3_RXBUF_SIZE & ~(I2C3_RXBUF_SIZE-1))!=I2C3_RXBUF_SIZE)
623  #error "I2C3 I2C3_RXBUF_SIZE is not power of 2! Either change size defined in projdefs.h --OR-- use nz_circularBufferStd.c in stead of nz_circularBufferPwr2.c for project!"
624  #endif
625 #endif
626 #endif
627 
628 
629 
633 typedef struct __attribute__((aligned(2), packed))
634 {
635  union {
636  struct
637  {
638  unsigned int bFill : 16; //Fill to ensure flags take up a 16-bit WORD
639  } flags;
640  WORD flagsVal;
641  };
642 
644  // Not all values for I2C1CON are possible, only following can be set:
664  WORD config1;
665 
667  //for RTC. This is the value for I2C1BRG register of PIC, see PIC datasheet for details. Some values are:
668  //157=100kHz, 75=200kHz, 37=400kHz, 12=970kHz, 11=1.03Mhz, 8=1.28Mhz
669  WORD baud;
670 
671 } SERPORT_I2C_CONFIG;
672 
673 //DON'T change! Code that checks wrDstType.bit0 and wrDstType.bit1 assumes there values!
674 #define RD_DEST_TYPE_LISTENERS 0
675 #define RD_DEST_TYPE_POINTER 2
676 #define RD_DEST_TYPE_CIRBUF 3
677 
678 typedef struct __attribute__((__packed__)) I2C_ISR_SMFLAGS_
679 {
680  union {
681  struct
682  {
683  unsigned int sm : 4;
684  unsigned int sAddress : 1;
685  unsigned int b : 1;
686  unsigned int wrDstType : 2;
687  unsigned int isrTxionStatus: 4;
688  unsigned int fill12 : 4;
689  } bits;
690  struct
691  {
692  unsigned int fill0to5 : 6;
693  unsigned int bit0 : 1;
694  unsigned int bit1 : 1;
695  unsigned int fill8to15 : 8;
696  } wrDstType;
697  WORD val;
698  };
699 } I2C_ISR_SMFLAGS;
700 
701 //Must be a power of two value: 4,8,16,32,64
702 #define I2C_STATUS_BUF_SIZE 8
703 
704 typedef struct I2C_INFO_
705 //typedef struct __attribute__((__packed__)) I2C_INFO_
706 //typedef struct __attribute__((aligned(2), packed)) I2C_INFO_
707 {
708  //Buffer containing status. Two bytes are used for each entry, the LSB=Address (0=empty), and MSB=Status.
709  BYTE bufStat[I2C_STATUS_BUF_SIZE];
710  //BYTE putStat; //Put pointer to bufStat. Points to last entry added.
711  BYTE adr; //Used by non-ISR functions to remember current slave address
712  BYTE taskSm; //ISR State machine. 0 is idle.
713  BYTE isrAdr; //Used in ISR - address of slave
714  BYTE isrCnt; //Incremented in ISR each time a STOP condition is put on the bus
715  BYTE isrRead; //Used in ISR - number of bytes to to read
716  BYTE fillByte;
717 
718  WORD isrRdDest; //Destination for read, meaning depends on value of i2c1.isr.bits.wrDstType
719  // - When 0, bits 0-15 indicates which "Circular Buffer" listeners are destinations (0=default)
720  // - When 1, contains pointer to destination BYTE array
721  WORD timeout; //Used in task to check for a timeout
722  CIRBUF* pCbufTx;
723  CIRBUF* pCbufRx;
724  I2C_ISR_SMFLAGS isr; //ISR State machine AND flags.
725  union {
726  struct
727  {
728  BYTE ch;
729  BYTE currTxionStatus;
730 // unsigned int ch : 3; //Channel Offset = number - 1: 0=I2C1, 1=I2C2, 2=I2C3
731 // unsigned int bFill3to7 : 5; //Fill bytes
732 // unsigned int currTxionStatus : 4; //Status of current Transmission
733 // unsigned int bFill12to15 : 4; //Fill bytes
734  } flags;
735  WORD flagsVal;
736  };
737 } I2C_INFO;
738 
739 
740 
742 //I2C Functions for all ports
743 
747 void serI2CInit(void);
748 
754 void serI2CTask(I2C_INFO* objI2C);
755 
756 
770 BYTE serI2CRegisterListener(CIRBUF* pBuf);
771 
772 
782 I2C_INFO* i2cGetInfoForBus(BYTE i2cBus);
783 
816 BYTE i2cGetStatus(I2C_INFO* objI2C, BYTE adr);
817 
818 
850 BYTE i2cGetStatusWait(I2C_INFO* objI2C, BYTE adr);
851 
862 BYTE i2cIsBusy(I2C_INFO* objI2C);
863 
864 
886 BYTE i2cWriteMessage(I2C_INFO* objI2C, BYTE adr, BYTE* msg, WORD size);
887 
888 
906 BYTE i2cBeginTransmission(I2C_INFO* objI2C, BYTE adr);
907 
908 
927 BYTE i2cEndTransmission(I2C_INFO* objI2C);
928 
929 
938 BYTE i2cWrite(I2C_INFO* objI2C, BYTE b);
939 
951 BYTE i2cWriteArray(I2C_INFO* objI2C, BYTE* arr, BYTE size);
952 
953 
964 BYTE i2cWriteString(I2C_INFO* objI2C, const char* str);
965 
966 
983 BYTE i2cWriteAsciiEscString(I2C_INFO* objI2C, const char* str);
984 
985 
999 BYTE i2cRead(I2C_INFO* objI2C, BYTE size);
1000 
1001 
1013 BYTE i2cReadToArray(I2C_INFO* objI2C, BYTE* buf, BYTE size);
1014 
1015 
1055 BYTE i2cReadSlaveReg(I2C_INFO* objI2C, BYTE adr, BYTE reg, BYTE* buf, BYTE size);
1056 
1069 #define i2cGetByte(objI2C) cbufGetByte(objI2C->pCbufRx)
1070 
1071 
1087 #define i2cGetArray(objI2C, pDstArr, size) cbufGetArray(objI2C->pCbufRx, pDstArr, size)
1088 
1089 
1091 //I2C 1
1092 #if defined(HAS_SERPORT_I2C1)
1093 
1094 #if !defined(SERPORT_I2C1_CREATE_OWN_CIRBUFS)
1095 #include "nz_serDataPorts.h"
1096 #endif
1097 
1098 //Modes
1099 #define SERPORT_I2C1_MODE_ASCII 0
1100 
1101 //This is used by others
1102 #ifndef THIS_IS_NZ_SERI2C1_C
1103 extern I2C_INFO i2c1Info;
1104 #endif
1105 #define I2C1_INFO &i2c1Info
1106 
1107 //TX and RX "Circular Buffers". Use SERPORT_I2C1_CREATE_OWN_CIRBUFS define to use this
1108 //module without SERPORTs. If NOT defined, this modules requires nz_serDataPorts.c
1109 #if defined(SERPORT_I2C1_CREATE_OWN_CIRBUFS)
1110  //This is used by others
1111  #ifndef THIS_IS_NZ_SERI2C1_C
1112  extern CIRBUF cbufTxI2C1;
1113  extern CIRBUF cbufRxI2C1;
1114  #endif
1115 
1116  #define CIRBUF_TX_I2C1 &cbufTxI2C1
1117  #define CIRBUF_RX_I2C1 &cbufRxI2C1
1118 #endif
1119 
1120 
1124 void serI2C1Init(void);
1125 
1129 //void serI2C1Task(void);
1130 
1160 BYTE i2c1GetStatus(BYTE adr);
1161 
1162 
1208 BYTE i2c1GetStatusWait(BYTE adr);
1209 
1227 BYTE i2c1IsBusy(void);
1228 
1232 #define i2c1GetTxionCnt() (i2c1Info.isrCnt)
1233 
1237 #define i2c1GetTxionAddress() (i2c1Info.adr)
1238 
1239 
1268 BYTE i2c1WriteMessage(BYTE adr, BYTE* msg, WORD size);
1269 
1270 
1313 BYTE i2c1BeginTransmission(BYTE adr);
1314 
1315 
1358 BYTE i2c1EndTransmission(void);
1359 
1360 
1368 BYTE i2c1Write(BYTE b);
1369 
1370 
1379 BYTE i2c1WriteArray(BYTE* arr, BYTE size);
1380 
1381 
1390 BYTE i2c1WriteString(const char* str);
1391 
1392 
1407 BYTE i2c1WriteAsciiEscString(const char* str);
1408 
1409 
1435 BYTE i2c1Read(BYTE size);
1436 
1437 
1461 BYTE i2c1ReadToArray(BYTE* buf, BYTE size);
1462 
1463 
1501 BYTE i2c1ReadSlaveReg(BYTE adr, BYTE reg, BYTE* buf, BYTE size);
1502 
1503 
1504 /*
1505 #define i2c1EmptyTxBuf() cbufEmpty(CIRBUF_TX_I2C1)
1506 #define i2c1IsTxBufEmpty() cbufIsEmpty(CIRBUF_TX_I2C1)
1507 #define i2c1IsTxBufFull() cbufIsFull(CIRBUF_TX_I2C1)
1508 #define i2c1GetTxBufCount() cbufGetCount(CIRBUF_TX_I2C1)
1509 #define i2c1PeekTxBuf() cbufPeekByte(CIRBUF_TX_I2C1)
1510 #define i2c1PeekTxBufAt(offset) cbufPeekByteAt(CIRBUF_TX_I2C1, offset)
1511 #define i2c1TxBufRemoveByte() cbufRemoveByte(CIRBUF_TX_I2C1)
1512 #define i2c1TxBufRemoveBytes(n) cbufRemoveBytes(CIRBUF_TX_I2C1, n)
1513 */
1514 
1518 #define i2c1GetTxBufFree() cbufGetFree(CIRBUF_TX_I2C1)
1519 
1520 
1524 #define i2c1EmptyTxBuf() cbufEmpty(CIRBUF_TX_I2C1)
1525 
1526 
1532 #define i2c1IsTxBufEmpty() cbufIsEmpty(CIRBUF_TX_I2C1)
1533 
1534 
1540 #define i2c1IsTxBufFull() cbufIsFull(CIRBUF_TX_I2C1)
1541 
1542 
1550 #define i2c1GetRxBufCount() cbufGetCount(CIRBUF_RX_I2C1)
1551 
1552 
1559 #define i2c1HasData() cbufHasData(CIRBUF_RX_I2C1)
1560 
1561 
1568 #define i2c1IsRxBufEmpty() cbufIsEmpty(CIRBUF_RX_I2C1)
1569 
1570 
1580 #define i2c1GetByte() cbufGetByte(CIRBUF_RX_I2C1)
1581 
1582 
1593 #define i2c1GetArray(pDstArr, size) cbufGetArray(CIRBUF_RX_I2C1, pDstArr, size)
1594 
1595 #endif //#if defined(HAS_SERPORT_I2C1)
1596 
1597 #endif //#if defined(HAS_SERPORT_I2C)
1598 #endif //#ifndef NZ_SERI2C_H