You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

171 line
3.7 KiB

  1. #include "define.h"
  2. #include "BoardCfg.h"
  3. #include "I2C.h"
  4. int mLastTransactionOK;
  5. int mI2CWaitCounter;
  6. int I2CInit()
  7. {
  8. //SPI and I2C BRG work the same way. So let's reuse some code!
  9. // int BaudRateGenerator = SPICalculateBRG(80000000,100000); //PBclk is 80MHz, I2C clk = 100KHz
  10. int BaudRateGenerator = 398;
  11. I2C3CON = 0;
  12. I2C3CONbits.DISSLW = 1; //disable slew rate control since we are only at 100KHz
  13. I2C3BRG = BaudRateGenerator;
  14. I2C3CONbits.ON = 1;
  15. mLastTransactionOK = 0;
  16. mI2CWaitCounter = 0;
  17. }
  18. int I2CWrite(unsigned char* OutBuf, unsigned char length)
  19. {
  20. int RET = RET_OK;
  21. int i;
  22. //Emit start event
  23. I2C3CONbits.SEN = 1;
  24. while(I2C3CONbits.SEN == 1)
  25. {
  26. }
  27. if(I2C3STATbits.BCL == 1)
  28. {
  29. mLastTransactionOK = false;
  30. return RET_ERROR;
  31. }
  32. for(i = 0; i < length; i++)
  33. {
  34. I2C3TRN = OutBuf[i];
  35. mI2CWaitCounter = 0;
  36. while(I2C3STATbits.TRSTAT == 1)
  37. {
  38. if(I2C3STATbits.BCL == 1 || mI2CWaitCounter++ > I2C_TRANSACTION_TIMEOUT_COUNT)
  39. {
  40. mLastTransactionOK = false;
  41. RET = RET_ERROR;
  42. break;
  43. }
  44. }
  45. // if(I2C3STATbits.ACKSTAT == 1)
  46. // {
  47. // RET = RET_ERROR;
  48. // }
  49. }
  50. //Emit stop event
  51. I2C3CONbits.PEN = 1;
  52. mI2CWaitCounter = 0;
  53. while(I2C3CONbits.PEN == 1)
  54. {
  55. if(I2C3STATbits.BCL == 1 || mI2CWaitCounter++ > I2C_TRANSACTION_TIMEOUT_COUNT)
  56. {
  57. mLastTransactionOK = false;
  58. RET = RET_ERROR;
  59. break;
  60. }
  61. }
  62. mLastTransactionOK = true;
  63. return RET;
  64. }
  65. int I2CTransmitByte(unsigned char Byte)
  66. {
  67. return RET_ERROR;
  68. }
  69. int I2CRead(unsigned char SlaveAddress,unsigned char* InputBuf,unsigned char length)
  70. {
  71. int RET = RET_OK;
  72. int i;
  73. //Emit start event
  74. I2C3CONbits.SEN = 1;
  75. mI2CWaitCounter = 0;
  76. while(I2C3CONbits.SEN == 1)
  77. if(I2C3STATbits.BCL == 1)
  78. {
  79. mLastTransactionOK = false;
  80. return RET_ERROR;
  81. }
  82. //Transmit slave address and write bit
  83. I2C3TRN = SlaveAddress;
  84. mI2CWaitCounter = 0;
  85. while(I2C3STATbits.TRSTAT == 1)
  86. {
  87. if(I2C3STATbits.BCL == 1 || mI2CWaitCounter++ > I2C_TRANSACTION_TIMEOUT_COUNT)
  88. {
  89. mLastTransactionOK = false;
  90. return RET_ERROR;
  91. }
  92. }
  93. for(i = 0; i < length; i++)
  94. {
  95. //
  96. I2C3CONbits.RCEN = 1;
  97. mI2CWaitCounter = 0;
  98. while(I2C3CONbits.RCEN == 1)
  99. {
  100. if(I2C3STATbits.BCL == 1 || mI2CWaitCounter++ > I2C_TRANSACTION_TIMEOUT_COUNT)
  101. {
  102. mLastTransactionOK = false;
  103. return RET_ERROR;
  104. }
  105. }
  106. InputBuf[i] = I2C3RCV;
  107. //Acknowledge reception
  108. I2C3CONbits.ACKDT = 1;
  109. I2C3CONbits.ACKEN = 1;
  110. mI2CWaitCounter = 0;
  111. while(I2C3CONbits.ACKEN == 1)
  112. {
  113. if(I2C3STATbits.BCL == 1 || mI2CWaitCounter++ > I2C_TRANSACTION_TIMEOUT_COUNT)
  114. {
  115. mLastTransactionOK = false;
  116. return RET_ERROR;
  117. }
  118. }
  119. }
  120. //Emit stop event
  121. I2C3CONbits.PEN = 1;
  122. mI2CWaitCounter = 0;
  123. while(I2C3CONbits.PEN == 1)
  124. {
  125. if(I2C3STATbits.BCL == 1 || mI2CWaitCounter++ > I2C_TRANSACTION_TIMEOUT_COUNT)
  126. {
  127. mLastTransactionOK = false;
  128. return RET_ERROR;
  129. }
  130. }
  131. mLastTransactionOK = true;
  132. return RET;
  133. }
  134. bool I2CWasLastTransactionOK()
  135. {
  136. return mLastTransactionOK;
  137. }