swi2c.c 3.8 KB

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