swi2c.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #include "uni_avr_rpi.h"
  2. #ifdef SWI2C
  3. #include "swi2c.h"
  4. #ifdef __AVR
  5. unsigned char swi2c_sda = 20; // SDA pin
  6. unsigned char swi2c_scl = 21; // SCL pin
  7. #endif //__AVR
  8. #ifdef __RPI
  9. unsigned char swi2c_sda = 2; // SDA pin
  10. unsigned char swi2c_scl = 3; // SCL pin
  11. #endif //__RPI
  12. unsigned char swi2c_cfg = 0xb1; // config
  13. // bit0..3 = clock delay factor = 1 << 1 = 2 [us]
  14. // bit4..7 = ack timeout factor = 1 << 11 = 2048 [cycles]
  15. #define SWI2C_SDA swi2c_sda
  16. #define SWI2C_SCL swi2c_scl
  17. #define SWI2C_RMSK 0x01 //read mask (bit0 = 1)
  18. #define SWI2C_WMSK 0x00 //write mask (bit0 = 0)
  19. #define SWI2C_ASHF 0x01 //address shift (<< 1)
  20. #define SWI2C_DMSK 0x7f //device address mask
  21. void swi2c_init(unsigned char sda, unsigned char scl, unsigned char cfg)
  22. {
  23. swi2c_sda = sda;
  24. swi2c_scl = scl;
  25. swi2c_cfg = cfg;
  26. GPIO_OUT(SWI2C_SDA);
  27. GPIO_OUT(SWI2C_SCL);
  28. GPIO_SET(SWI2C_SDA);
  29. GPIO_SET(SWI2C_SCL);
  30. DELAY(1000);
  31. }
  32. void swi2c_start(int delay)
  33. {
  34. GPIO_CLR(SWI2C_SDA);
  35. DELAY(delay);
  36. GPIO_CLR(SWI2C_SCL);
  37. DELAY(delay);
  38. }
  39. void swi2c_stop(int delay)
  40. {
  41. GPIO_SET(SWI2C_SCL);
  42. DELAY(delay);
  43. GPIO_SET(SWI2C_SDA);
  44. DELAY(delay);
  45. }
  46. void swi2c_ack(int delay)
  47. {
  48. GPIO_CLR(SWI2C_SDA);
  49. DELAY(delay);
  50. GPIO_SET(SWI2C_SCL);
  51. DELAY(delay);
  52. GPIO_CLR(SWI2C_SCL);
  53. DELAY(delay);
  54. }
  55. int swi2c_wait_ack(int delay, int ackto)
  56. {
  57. GPIO_INP(SWI2C_SDA);
  58. DELAY(delay);
  59. // GPIO_SET(SWI2C_SDA);
  60. DELAY(delay);
  61. GPIO_SET(SWI2C_SCL);
  62. // DELAY(delay);
  63. int ack = 0;
  64. while (!(ack = !GPIO_GET(SWI2C_SDA)) && ackto--) DELAY(delay);
  65. GPIO_CLR(SWI2C_SCL);
  66. DELAY(delay);
  67. GPIO_OUT(SWI2C_SDA);
  68. DELAY(delay);
  69. GPIO_CLR(SWI2C_SDA);
  70. DELAY(delay);
  71. return ack;
  72. }
  73. unsigned char swi2c_read(int delay)
  74. {
  75. GPIO_SET(SWI2C_SDA);
  76. DELAY(delay);
  77. GPIO_INP(SWI2C_SDA);
  78. unsigned char data = 0;
  79. int bit; for (bit = 7; bit >= 0; bit--)
  80. {
  81. GPIO_SET(SWI2C_SCL);
  82. DELAY(delay);
  83. data |= GPIO_GET(SWI2C_SDA) << bit;
  84. GPIO_CLR(SWI2C_SCL);
  85. DELAY(delay);
  86. }
  87. GPIO_OUT(SWI2C_SDA);
  88. return data;
  89. }
  90. void swi2c_write(int delay, unsigned char data)
  91. {
  92. int bit; for (bit = 7; bit >= 0; bit--)
  93. {
  94. if (data & (1 << bit)) GPIO_SET(SWI2C_SDA);
  95. else GPIO_CLR(SWI2C_SDA);
  96. DELAY(delay);
  97. GPIO_SET(SWI2C_SCL);
  98. DELAY(delay);
  99. GPIO_CLR(SWI2C_SCL);
  100. DELAY(delay);
  101. }
  102. }
  103. int swi2c_check(unsigned char dev_addr)
  104. {
  105. int delay = 1 << (swi2c_cfg & 0xf);
  106. int tmout = 1 << (swi2c_cfg >> 4);
  107. swi2c_start(delay);
  108. swi2c_write(delay, (dev_addr & SWI2C_DMSK) << SWI2C_ASHF);
  109. if (!swi2c_wait_ack(delay, tmout)) { swi2c_stop(delay); return 0; }
  110. swi2c_stop(delay);
  111. return 1;
  112. }
  113. #ifdef SWI2C_A8 //8bit address
  114. int swi2c_readByte_A8(unsigned char dev_addr, unsigned char addr, unsigned char* pbyte)
  115. {
  116. int delay = 1 << (swi2c_cfg & 0xf);
  117. int tmout = 1 << (swi2c_cfg >> 4);
  118. swi2c_start(delay);
  119. swi2c_write(delay, SWI2C_WMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
  120. if (!swi2c_wait_ack(delay, tmout)) { swi2c_stop(delay); return 0; }
  121. swi2c_write(delay, addr & 0xff);
  122. if (!swi2c_wait_ack(delay, tmout)) return 0;
  123. swi2c_stop(delay);
  124. swi2c_start(delay);
  125. swi2c_write(delay, SWI2C_RMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
  126. if (!swi2c_wait_ack(delay, tmout)) return 0;
  127. unsigned char byte = swi2c_read(delay);
  128. swi2c_stop(delay);
  129. if (pbyte) *pbyte = byte;
  130. return 1;
  131. }
  132. int swi2c_writeByte_A8(unsigned char dev_addr, unsigned char addr, unsigned char* pbyte)
  133. {
  134. int delay = 1 << (swi2c_cfg & 0xf);
  135. int tmout = 1 << (swi2c_cfg >> 4);
  136. swi2c_start(delay);
  137. swi2c_write(delay, SWI2C_WMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
  138. if (!swi2c_wait_ack(delay, tmout)) { swi2c_stop(delay); return 0; }
  139. swi2c_write(delay, addr & 0xff);
  140. if (!swi2c_wait_ack(delay, tmout)) return 0;
  141. swi2c_write(delay, *pbyte);
  142. if (!swi2c_wait_ack(delay, tmout)) return 0;
  143. swi2c_stop(delay);
  144. return 1;
  145. }
  146. #endif //SWI2C_A8
  147. #ifdef SWI2C_A16 //16bit address
  148. int swi2c_readByte_A16(unsigned char dev_addr, unsigned short addr, unsigned char* pbyte)
  149. {
  150. int delay = 1 << (swi2c_cfg & 0xf);
  151. int tmout = 1 << (swi2c_cfg >> 4);
  152. swi2c_start(delay);
  153. swi2c_write(delay, SWI2C_WMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
  154. if (!swi2c_wait_ack(delay, tmout)) { swi2c_stop(delay); return 0; }
  155. swi2c_write(delay, addr >> 8);
  156. if (!swi2c_wait_ack(delay, tmout)) return 0;
  157. swi2c_write(delay, addr & 0xff);
  158. if (!swi2c_wait_ack(delay, tmout)) return 0;
  159. swi2c_stop(delay);
  160. swi2c_start(delay);
  161. swi2c_write(delay, SWI2C_RMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
  162. if (!swi2c_wait_ack(delay, tmout)) return 0;
  163. unsigned char byte = swi2c_read(delay);
  164. swi2c_stop(delay);
  165. if (pbyte) *pbyte = byte;
  166. return 1;
  167. }
  168. int swi2c_writeByte_A16(unsigned char dev_addr, unsigned short addr, unsigned char* pbyte)
  169. {
  170. int delay = 1 << (swi2c_cfg & 0xf);
  171. int tmout = 1 << (swi2c_cfg >> 4);
  172. swi2c_start(delay);
  173. swi2c_write(delay, SWI2C_WMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
  174. if (!swi2c_wait_ack(delay, tmout)) { swi2c_stop(delay); return 0; }
  175. swi2c_write(delay, addr >> 8);
  176. if (!swi2c_wait_ack(delay, tmout)) return 0;
  177. swi2c_write(delay, addr & 0xff);
  178. if (!swi2c_wait_ack(delay, tmout)) return 0;
  179. swi2c_write(delay, *pbyte);
  180. if (!swi2c_wait_ack(delay, tmout)) return 0;
  181. swi2c_stop(delay);
  182. return 1;
  183. }
  184. #endif //SWI2C_A16
  185. #endif //SWI2C