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 "Configuration_prusa.h"
  7. #include "pins.h"
  8. #include "io_atmega2560.h"
  9. #define SWI2C_RMSK 0x01 //read mask (bit0 = 1)
  10. #define SWI2C_WMSK 0x00 //write mask (bit0 = 0)
  11. #define SWI2C_ASHF 0x01 //address shift (<< 1)
  12. #define SWI2C_DMSK 0x7f //device address mask
  13. void __delay(void)
  14. {
  15. _delay_us(1.5);
  16. }
  17. void swi2c_init(void)
  18. {
  19. PIN_OUT(SWI2C_SDA);
  20. PIN_OUT(SWI2C_SCL);
  21. PIN_SET(SWI2C_SDA);
  22. PIN_SET(SWI2C_SCL);
  23. uint8_t i; for (i = 0; i < 100; i++)
  24. __delay();
  25. }
  26. void swi2c_start(void)
  27. {
  28. PIN_CLR(SWI2C_SDA);
  29. __delay();
  30. PIN_CLR(SWI2C_SCL);
  31. __delay();
  32. }
  33. void swi2c_stop(void)
  34. {
  35. PIN_SET(SWI2C_SCL);
  36. __delay();
  37. PIN_SET(SWI2C_SDA);
  38. __delay();
  39. }
  40. void swi2c_ack(void)
  41. {
  42. PIN_CLR(SWI2C_SDA);
  43. __delay();
  44. PIN_SET(SWI2C_SCL);
  45. __delay();
  46. PIN_CLR(SWI2C_SCL);
  47. __delay();
  48. }
  49. uint8_t swi2c_wait_ack()
  50. {
  51. PIN_INP(SWI2C_SDA);
  52. __delay();
  53. // PIN_SET(SWI2C_SDA);
  54. __delay();
  55. PIN_SET(SWI2C_SCL);
  56. // __delay();
  57. uint8_t ack = 0;
  58. uint16_t ackto = SWI2C_TMO;
  59. while (!(ack = (PIN_GET(SWI2C_SDA)?0:1)) && ackto--) __delay();
  60. PIN_CLR(SWI2C_SCL);
  61. __delay();
  62. PIN_OUT(SWI2C_SDA);
  63. __delay();
  64. PIN_CLR(SWI2C_SDA);
  65. __delay();
  66. return ack;
  67. }
  68. uint8_t swi2c_read(void)
  69. {
  70. PIN_SET(SWI2C_SDA);
  71. __delay();
  72. PIN_INP(SWI2C_SDA);
  73. uint8_t data = 0;
  74. int8_t bit; for (bit = 7; bit >= 0; bit--)
  75. {
  76. PIN_SET(SWI2C_SCL);
  77. __delay();
  78. data |= (PIN_GET(SWI2C_SDA)?1:0) << bit;
  79. PIN_CLR(SWI2C_SCL);
  80. __delay();
  81. }
  82. PIN_OUT(SWI2C_SDA);
  83. return data;
  84. }
  85. void swi2c_write(uint8_t data)
  86. {
  87. int8_t bit; for (bit = 7; bit >= 0; bit--)
  88. {
  89. if (data & (1 << bit)) PIN_SET(SWI2C_SDA);
  90. else PIN_CLR(SWI2C_SDA);
  91. __delay();
  92. PIN_SET(SWI2C_SCL);
  93. __delay();
  94. PIN_CLR(SWI2C_SCL);
  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