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 "Configuration_prusa.h"
  7. #include "pins.h"
  8. #include "io_atmega2560.h"
  9. #ifdef SWI2C_SCL
  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. PIN_OUT(SWI2C_SDA);
  21. PIN_OUT(SWI2C_SCL);
  22. PIN_SET(SWI2C_SDA);
  23. PIN_SET(SWI2C_SCL);
  24. uint8_t i; for (i = 0; i < 100; i++)
  25. __delay();
  26. }
  27. void swi2c_start(void)
  28. {
  29. PIN_CLR(SWI2C_SDA);
  30. __delay();
  31. PIN_CLR(SWI2C_SCL);
  32. __delay();
  33. }
  34. void swi2c_stop(void)
  35. {
  36. PIN_SET(SWI2C_SCL);
  37. __delay();
  38. PIN_SET(SWI2C_SDA);
  39. __delay();
  40. }
  41. void swi2c_ack(void)
  42. {
  43. PIN_CLR(SWI2C_SDA);
  44. __delay();
  45. PIN_SET(SWI2C_SCL);
  46. __delay();
  47. PIN_CLR(SWI2C_SCL);
  48. __delay();
  49. }
  50. uint8_t swi2c_wait_ack()
  51. {
  52. PIN_INP(SWI2C_SDA);
  53. __delay();
  54. // PIN_SET(SWI2C_SDA);
  55. __delay();
  56. PIN_SET(SWI2C_SCL);
  57. // __delay();
  58. uint8_t ack = 0;
  59. uint16_t ackto = SWI2C_TMO;
  60. while (!(ack = (PIN_GET(SWI2C_SDA)?0:1)) && ackto--) __delay();
  61. PIN_CLR(SWI2C_SCL);
  62. __delay();
  63. PIN_OUT(SWI2C_SDA);
  64. __delay();
  65. PIN_CLR(SWI2C_SDA);
  66. __delay();
  67. return ack;
  68. }
  69. uint8_t swi2c_read(void)
  70. {
  71. PIN_SET(SWI2C_SDA);
  72. __delay();
  73. PIN_INP(SWI2C_SDA);
  74. uint8_t data = 0;
  75. int8_t bit; for (bit = 7; bit >= 0; bit--)
  76. {
  77. PIN_SET(SWI2C_SCL);
  78. __delay();
  79. data |= (PIN_GET(SWI2C_SDA)?1:0) << bit;
  80. PIN_CLR(SWI2C_SCL);
  81. __delay();
  82. }
  83. PIN_OUT(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. if (data & (1 << bit)) PIN_SET(SWI2C_SDA);
  91. else PIN_CLR(SWI2C_SDA);
  92. __delay();
  93. PIN_SET(SWI2C_SCL);
  94. __delay();
  95. PIN_CLR(SWI2C_SCL);
  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