Source code of Windows XP (NT5)
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.

235 lines
3.8 KiB

  1. //
  2. // MODULE : I2C.C
  3. // PURPOSE : I2C Interface
  4. // AUTHOR : JBS Yadawa
  5. // CREATED : 7/20/96
  6. //
  7. //
  8. // Copyright (C) 1996 SGS-THOMSON Microelectronics
  9. //
  10. //
  11. // REVISION HISTORY :
  12. //
  13. // DATE :
  14. //
  15. // COMMENTS :
  16. //
  17. #include "common.h"
  18. #include "stdefs.h"
  19. #include "memio.h"
  20. #include "i2c.h"
  21. #include "board.h"
  22. #define I2CREG 0x44
  23. #define SCL 0x01
  24. #define SDA 0x02
  25. BYTE i2cShadow;
  26. void NEARAPI I2CSendBit(BOOL Data);
  27. void NEARAPI I2CStart(void);
  28. void NEARAPI I2CStop(void);
  29. void NEARAPI I2CSendByte(BYTE data);
  30. void NEARAPI I2CSendDataByte(BYTE data);
  31. void NEARAPI I2CGetBit(BOOL * data);
  32. void NEARAPI I2CGetByte(BYTE *data);
  33. void NEARAPI I2CGetDataByte(BYTE *data);
  34. void FARAPI I2CInitBus(void)
  35. {
  36. i2cShadow = 0;
  37. i2cShadow = SDA;
  38. memOutByte(I2CREG, i2cShadow);
  39. i2cShadow |= SCL;
  40. memOutByte(I2CREG, i2cShadow);
  41. }
  42. void FARAPI I2CSettleBus(void)
  43. {
  44. int i;
  45. BOOL b;
  46. i2cShadow &= (~SDA);
  47. memOutByte(I2CREG, i2cShadow);
  48. Delay(50);
  49. i2cShadow &= (~SCL);
  50. i2cShadow = 0x00;
  51. memOutByte(I2CREG, i2cShadow);
  52. Delay(50);
  53. i2cShadow |= SCL;
  54. for(i=0; i<100; i++)
  55. {
  56. memOutByte(I2CREG, i2cShadow);
  57. Delay(50);
  58. memOutByte(I2CREG, i2cShadow);
  59. Delay(50);
  60. }
  61. i2cShadow |= SDA;
  62. memOutByte(I2CREG, i2cShadow);
  63. i2cShadow |= SCL;
  64. memOutByte(I2CREG, i2cShadow);
  65. Delay(50);
  66. b = memInByte(I2CREG);
  67. b = b >> 1;
  68. i = 0;
  69. while ((i < 2000) && (!b))
  70. {
  71. i2cShadow &= (!SCL);
  72. memOutByte(I2CREG, i2cShadow);
  73. i2cShadow |= SDA;
  74. memOutByte(I2CREG, i2cShadow);
  75. Delay(50);
  76. i2cShadow |= SCL;
  77. memOutByte(I2CREG, i2cShadow);
  78. i2cShadow |= SDA;
  79. memOutByte(I2CREG, i2cShadow);
  80. Delay(50);
  81. i++;
  82. b = memInByte(I2CREG) >> 1;
  83. }
  84. if(i >= 2000)
  85. {
  86. // MessageBox(GetFocus(), "I2C Bus is unstable!!","STHal", MB_OK);
  87. }
  88. }
  89. void NEARAPI I2CSendBit(BOOL Data)
  90. {
  91. if(Data)
  92. {
  93. i2cShadow |= SDA;
  94. memOutByte(I2CREG, i2cShadow);
  95. }
  96. else
  97. {
  98. i2cShadow &= SCL;
  99. memOutByte(I2CREG, i2cShadow);
  100. }
  101. Delay(50);
  102. i2cShadow |= SCL;
  103. memOutByte(I2CREG, i2cShadow);
  104. Delay(50);
  105. i2cShadow &= SDA;
  106. memOutByte(I2CREG, i2cShadow);
  107. Delay(50);
  108. }
  109. extern volatile BYTE *lpBase;
  110. volatile BYTE *i2cptr;
  111. void NEARAPI I2CStart(void)
  112. {
  113. i2cShadow = 0;
  114. i2cShadow |= SDA;
  115. memOutByte(I2CREG, i2cShadow);
  116. i2cShadow |= SCL;
  117. memOutByte(I2CREG, i2cShadow);
  118. Delay(50);
  119. i2cShadow = memInByte(I2CREG);
  120. if(!(i2cShadow&(SDA|SCL)))
  121. {
  122. // MessageBox(GetFocus(), "I2C Bus Busy", "STHal", MB_OK);
  123. }
  124. i2cShadow &= SCL;
  125. memOutByte(I2CREG, i2cShadow);
  126. Delay(50);
  127. i2cShadow = 0x00;
  128. memOutByte(I2CREG, i2cShadow);
  129. Delay(50);
  130. }
  131. void NEARAPI I2CStop(void)
  132. {
  133. i2cShadow &= SCL;
  134. memOutByte(I2CREG, i2cShadow);
  135. Delay(50);
  136. i2cShadow |= SCL;
  137. memOutByte(I2CREG, i2cShadow);
  138. Delay(50);
  139. i2cShadow |= SDA;
  140. memOutByte(I2CREG, i2cShadow);
  141. Delay(50);
  142. }
  143. void NEARAPI I2CSendByte(BYTE data)
  144. {
  145. int i;
  146. for(i=7; i>=0; i--)
  147. {
  148. if(data & (1 << i))
  149. I2CSendBit(TRUE);
  150. else
  151. I2CSendBit(FALSE);
  152. }
  153. }
  154. void NEARAPI I2CSendDataByte(BYTE data)
  155. {
  156. BOOL ack;
  157. I2CSendByte(data);
  158. I2CGetBit(&ack);
  159. if(ack)
  160. {
  161. // MessageBox(GetFocus(), "No Ack after send byte", "sthal", MB_OK);
  162. }
  163. }
  164. void NEARAPI I2CGetBit(BOOL * data)
  165. {
  166. i2cShadow |= SDA;
  167. memOutByte(I2CREG, i2cShadow);
  168. Delay(50);
  169. i2cShadow |= SCL;
  170. memOutByte(I2CREG, i2cShadow);
  171. Delay(50);
  172. i2cShadow = memInByte(I2CREG);
  173. Delay(50);
  174. i2cShadow &= SDA;
  175. memOutByte(I2CREG, i2cShadow);
  176. Delay(50);
  177. *data = (i2cShadow >> 1);
  178. }
  179. void NEARAPI I2CGetByte(BYTE *data)
  180. {
  181. int i;
  182. BOOL b;
  183. *data = 0x00;
  184. for (i = 7; i >= 0; i--)
  185. {
  186. I2CGetBit(&b);
  187. if(b)
  188. *data |= (1 << i);
  189. }
  190. }
  191. void NEARAPI I2CGetDataByte(BYTE *data)
  192. {
  193. I2CGetByte(data);
  194. I2CSendBit(TRUE);
  195. }
  196. void FARAPI I2CSendSeq(WORD unit, WORD num, BYTE *data)
  197. {
  198. WORD i;
  199. I2CStart();
  200. I2CSendDataByte((BYTE)(unit));
  201. for(i=0; i < num; i++)
  202. {
  203. I2CSendDataByte((BYTE)data[i]);
  204. }
  205. I2CStop();
  206. }
  207.