swi2c.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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_var.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. static void __delay(void);
  16. static void swi2c_start(void);
  17. static void swi2c_stop(void);
  18. // static void swi2c_ack(void);
  19. static void swi2c_nack(void);
  20. static uint8_t swi2c_wait_ack();
  21. static uint8_t swi2c_read(void);
  22. static void swi2c_write(uint8_t data);
  23. void swi2c_init(void)
  24. {
  25. SET_INPUT(SWI2C_SDA);
  26. WRITE(SWI2C_SDA, 1); //SDA must be input with pullups while we are not sure if the slave is outputing or not
  27. WRITE(SWI2C_SCL, 0);
  28. SET_OUTPUT(SWI2C_SCL); //SCL can be an output at all times. The bus is not in a multi-master configuration.
  29. for (uint8_t i = 0; i < 100; i++) //wait. Not sure what for, but wait anyway.
  30. __delay();
  31. for (uint8_t i = 0; i < 10; i++) { //send nack 10 times. This makes sure that the slave gets a nack regardless of it's state when we init the bus.
  32. swi2c_nack();
  33. }
  34. swi2c_stop(); //"release" the bus by sending a stop condition.
  35. SET_OUTPUT(SWI2C_SDA); //finally make the SDA line an output since the bus is idle for sure.
  36. }
  37. void swi2c_disable(void)
  38. {
  39. SET_INPUT(SWI2C_SDA);
  40. WRITE(SWI2C_SDA, 0);
  41. SET_INPUT(SWI2C_SCL);
  42. WRITE(SWI2C_SCL, 0);
  43. }
  44. static void __delay(void)
  45. {
  46. _delay_us(1.5);
  47. }
  48. static void swi2c_start(void)
  49. {
  50. WRITE(SWI2C_SDA, 0);
  51. __delay();
  52. WRITE(SWI2C_SCL, 0);
  53. __delay();
  54. }
  55. static void swi2c_stop(void)
  56. {
  57. WRITE(SWI2C_SCL, 1);
  58. __delay();
  59. WRITE(SWI2C_SDA, 1);
  60. __delay();
  61. }
  62. /*
  63. static void swi2c_ack(void)
  64. {
  65. WRITE(SWI2C_SDA, 0);
  66. __delay();
  67. WRITE(SWI2C_SCL, 1);
  68. __delay();
  69. WRITE(SWI2C_SCL, 0);
  70. __delay();
  71. }
  72. */
  73. static void swi2c_nack(void)
  74. {
  75. WRITE(SWI2C_SDA, 1);
  76. __delay();
  77. WRITE(SWI2C_SCL, 1);
  78. __delay();
  79. WRITE(SWI2C_SCL, 0);
  80. __delay();
  81. }
  82. static uint8_t swi2c_wait_ack()
  83. {
  84. SET_INPUT(SWI2C_SDA);
  85. __delay();
  86. // WRITE(SWI2C_SDA, 1);
  87. __delay();
  88. WRITE(SWI2C_SCL, 1);
  89. // __delay();
  90. uint8_t ack = 0;
  91. uint16_t ackto = SWI2C_TMO;
  92. while (!(ack = (!READ(SWI2C_SDA))) && ackto--) __delay();
  93. WRITE(SWI2C_SCL, 0);
  94. __delay();
  95. SET_OUTPUT(SWI2C_SDA);
  96. __delay();
  97. WRITE(SWI2C_SDA, 0);
  98. __delay();
  99. return ack;
  100. }
  101. static uint8_t swi2c_read(void)
  102. {
  103. WRITE(SWI2C_SDA, 1);
  104. __delay();
  105. SET_INPUT(SWI2C_SDA);
  106. uint8_t data = 0;
  107. for (uint8_t bit = 8; bit-- > 0;)
  108. {
  109. WRITE(SWI2C_SCL, 1);
  110. __delay();
  111. data |= (READ(SWI2C_SDA)) << bit;
  112. WRITE(SWI2C_SCL, 0);
  113. __delay();
  114. }
  115. SET_OUTPUT(SWI2C_SDA);
  116. return data;
  117. }
  118. static void swi2c_write(uint8_t data)
  119. {
  120. for (uint8_t bit = 8; bit-- > 0;)
  121. {
  122. WRITE(SWI2C_SDA, data & _BV(bit));
  123. __delay();
  124. WRITE(SWI2C_SCL, 1);
  125. __delay();
  126. WRITE(SWI2C_SCL, 0);
  127. __delay();
  128. }
  129. }
  130. uint8_t swi2c_check(uint8_t dev_addr)
  131. {
  132. swi2c_start();
  133. swi2c_write((dev_addr & SWI2C_DMSK) << SWI2C_ASHF);
  134. if (!swi2c_wait_ack()) { swi2c_stop(); return 1; }
  135. swi2c_stop();
  136. return 0;
  137. }
  138. #ifdef SWI2C_A8 //8bit address
  139. uint8_t swi2c_readByte_A8(uint8_t dev_addr, uint8_t addr, uint8_t* pbyte)
  140. {
  141. swi2c_start();
  142. swi2c_write(SWI2C_WMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
  143. if (!swi2c_wait_ack()) { swi2c_stop(); 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_A8(uint8_t dev_addr, uint8_t 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 & 0xff);
  161. if (!swi2c_wait_ack()) return 0;
  162. swi2c_write(*pbyte);
  163. if (!swi2c_wait_ack()) return 0;
  164. swi2c_stop();
  165. return 1;
  166. }
  167. #endif //SWI2C_A8
  168. #ifdef SWI2C_A16 //16bit address
  169. uint8_t swi2c_readByte_A16(uint8_t dev_addr, unsigned short addr, uint8_t* pbyte)
  170. {
  171. swi2c_start();
  172. swi2c_write(SWI2C_WMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
  173. if (!swi2c_wait_ack()) { swi2c_stop(); return 0; }
  174. swi2c_write(addr >> 8);
  175. if (!swi2c_wait_ack()) return 0;
  176. swi2c_write(addr & 0xff);
  177. if (!swi2c_wait_ack()) return 0;
  178. swi2c_stop();
  179. swi2c_start();
  180. swi2c_write(SWI2C_RMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
  181. if (!swi2c_wait_ack()) return 0;
  182. uint8_t byte = swi2c_read();
  183. swi2c_stop();
  184. if (pbyte) *pbyte = byte;
  185. return 1;
  186. }
  187. uint8_t swi2c_writeByte_A16(uint8_t dev_addr, unsigned short addr, uint8_t* pbyte)
  188. {
  189. swi2c_start();
  190. swi2c_write(SWI2C_WMSK | ((dev_addr & SWI2C_DMSK) << SWI2C_ASHF));
  191. if (!swi2c_wait_ack()) { swi2c_stop(); return 0; }
  192. swi2c_write(addr >> 8);
  193. if (!swi2c_wait_ack()) return 0;
  194. swi2c_write(addr & 0xff);
  195. if (!swi2c_wait_ack()) return 0;
  196. swi2c_write(*pbyte);
  197. if (!swi2c_wait_ack()) return 0;
  198. swi2c_stop();
  199. return 1;
  200. }
  201. #endif //SWI2C_A16
  202. #endif //SWI2C_SCL