swi2c.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //swi2c.c
  2. #include "swi2c.h"
  3. #include <avr/io.h>
  4. #include <util/delay.h>
  5. #include <avr/pgmspace.h>
  6. #include "stdbool.h"
  7. #include "Configuration_prusa.h"
  8. #include "pins.h"
  9. #include "fastio.h"
  10. #ifdef SWI2C_SCL
  11. #define SWI2C_RMSK 0x01 //read mask (bit0 = 1)
  12. #define SWI2C_WMSK 0x00 //write mask (bit0 = 0)
  13. #define SWI2C_ASHF 0x01 //address shift (<< 1)
  14. #define SWI2C_DMSK 0x7f //device address mask
  15. void __delay(void)
  16. {
  17. _delay_us(1.5);
  18. }
  19. void swi2c_init(void)
  20. {
  21. WRITE(SWI2C_SDA, 1);
  22. WRITE(SWI2C_SCL, 1);
  23. SET_OUTPUT(SWI2C_SDA);
  24. SET_OUTPUT(SWI2C_SCL);
  25. uint8_t i; for (i = 0; i < 100; i++)
  26. __delay();
  27. }
  28. void swi2c_start(void)
  29. {
  30. WRITE(SWI2C_SDA, 0);
  31. __delay();
  32. WRITE(SWI2C_SCL, 0);
  33. __delay();
  34. }
  35. void swi2c_stop(void)
  36. {
  37. WRITE(SWI2C_SCL, 1);
  38. __delay();
  39. WRITE(SWI2C_SDA, 1);
  40. __delay();
  41. }
  42. void swi2c_ack(void)
  43. {
  44. WRITE(SWI2C_SDA, 0);
  45. __delay();
  46. WRITE(SWI2C_SCL, 1);
  47. __delay();
  48. WRITE(SWI2C_SCL, 0);
  49. __delay();
  50. }
  51. uint8_t swi2c_wait_ack()
  52. {
  53. SET_INPUT(SWI2C_SDA);
  54. __delay();
  55. // WRITE(SWI2C_SDA, 1);
  56. __delay();
  57. WRITE(SWI2C_SCL, 1);
  58. // __delay();
  59. uint8_t ack = 0;
  60. uint16_t ackto = SWI2C_TMO;
  61. while (!(ack = (!READ(SWI2C_SDA))) && ackto--) __delay();
  62. WRITE(SWI2C_SCL, 0);
  63. __delay();
  64. SET_OUTPUT(SWI2C_SDA);
  65. __delay();
  66. WRITE(SWI2C_SDA, 0);
  67. __delay();
  68. return ack;
  69. }
  70. uint8_t swi2c_read(void)
  71. {
  72. WRITE(SWI2C_SDA, 1);
  73. __delay();
  74. SET_INPUT(SWI2C_SDA);
  75. uint8_t data = 0;
  76. int8_t bit; for (bit = 7; bit >= 0; bit--)
  77. {
  78. WRITE(SWI2C_SCL, 1);
  79. __delay();
  80. data |= (READ(SWI2C_SDA)) << bit;
  81. WRITE(SWI2C_SCL, 0);
  82. __delay();
  83. }
  84. SET_OUTPUT(SWI2C_SDA);
  85. return data;
  86. }
  87. void swi2c_write(uint8_t data)
  88. {
  89. int8_t bit; for (bit = 7; bit >= 0; bit--)
  90. {
  91. WRITE(SWI2C_SDA, data & _BV(bit));
  92. __delay();
  93. WRITE(SWI2C_SCL, 1);
  94. __delay();
  95. WRITE(SWI2C_SCL, 0);
  96. __delay();
  97. }
  98. }
  99. uint8_t swi2c_check(uint8_t dev_addr)
  100. {
  101. swi2c_start();
  102. swi2c_write((dev_addr & SWI2C_DMSK) << SWI2C_ASHF);
  103. if (!swi2c_wait_ack()) { swi2c_stop(); return 0; }
  104. swi2c_stop();
  105. return 1;
  106. }
  107. #ifdef SWI2C_A8 //8bit address
  108. uint8_t swi2c_readByte_A8(uint8_t dev_addr, uint8_t addr, uint8_t* pbyte)
  109. {
  110. swi2c_start();
  111. swi2c_write(SWI2C_WMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
  112. if (!swi2c_wait_ack()) { swi2c_stop(); return 0; }
  113. swi2c_write(addr & 0xff);
  114. if (!swi2c_wait_ack()) return 0;
  115. swi2c_stop();
  116. swi2c_start();
  117. swi2c_write(SWI2C_RMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
  118. if (!swi2c_wait_ack()) return 0;
  119. uint8_t byte = swi2c_read();
  120. swi2c_stop();
  121. if (pbyte) *pbyte = byte;
  122. return 1;
  123. }
  124. uint8_t swi2c_writeByte_A8(uint8_t dev_addr, uint8_t addr, uint8_t* pbyte)
  125. {
  126. swi2c_start();
  127. swi2c_write(SWI2C_WMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
  128. if (!swi2c_wait_ack()) { swi2c_stop(); return 0; }
  129. swi2c_write(addr & 0xff);
  130. if (!swi2c_wait_ack()) return 0;
  131. swi2c_write(*pbyte);
  132. if (!swi2c_wait_ack()) return 0;
  133. swi2c_stop();
  134. return 1;
  135. }
  136. #endif //SWI2C_A8
  137. #ifdef SWI2C_A16 //16bit address
  138. uint8_t swi2c_readByte_A16(uint8_t dev_addr, unsigned short addr, uint8_t* pbyte)
  139. {
  140. swi2c_start();
  141. swi2c_write(SWI2C_WMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
  142. if (!swi2c_wait_ack()) { swi2c_stop(); return 0; }
  143. swi2c_write(addr >> 8);
  144. if (!swi2c_wait_ack()) return 0;
  145. swi2c_write(addr & 0xff);
  146. if (!swi2c_wait_ack()) return 0;
  147. swi2c_stop();
  148. swi2c_start();
  149. swi2c_write(SWI2C_RMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
  150. if (!swi2c_wait_ack()) return 0;
  151. uint8_t byte = swi2c_read();
  152. swi2c_stop();
  153. if (pbyte) *pbyte = byte;
  154. return 1;
  155. }
  156. uint8_t swi2c_writeByte_A16(uint8_t dev_addr, unsigned short addr, uint8_t* pbyte)
  157. {
  158. swi2c_start();
  159. swi2c_write(SWI2C_WMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
  160. if (!swi2c_wait_ack()) { swi2c_stop(); return 0; }
  161. swi2c_write(addr >> 8);
  162. if (!swi2c_wait_ack()) return 0;
  163. swi2c_write(addr & 0xff);
  164. if (!swi2c_wait_ack()) return 0;
  165. swi2c_write(*pbyte);
  166. if (!swi2c_wait_ack()) return 0;
  167. swi2c_stop();
  168. return 1;
  169. }
  170. #endif //SWI2C_A16
  171. #endif //SWI2C_SCL