i2c.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. ** Made by fabien le mentec <texane@gmail.com>
  3. **
  4. ** Started on Sat Jan 30 15:43:29 2010 texane
  5. ** Last update Sun Feb 7 18:18:43 2010 texane
  6. */
  7. #include <pic18fregs.h>
  8. #include "i2c.h"
  9. /* globals */
  10. enum i2c_state
  11. {
  12. I2C_STATE_IDLE = 0,
  13. I2C_STATE_READ,
  14. I2C_STATE_WRITE
  15. };
  16. static volatile enum i2c_state i2c_state = I2C_STATE_IDLE;
  17. static volatile unsigned char i2c_buffer[32];
  18. static volatile unsigned int i2c_pos = 0;
  19. static volatile i2c_addr_t i2c_addr = 0;
  20. /* locking */
  21. static void lock_i2c(void)
  22. {
  23. PIE1bits.SSPIE = 0;
  24. }
  25. static void unlock_i2c(void)
  26. {
  27. PIE1bits.SSPIE = 1;
  28. }
  29. /* exported */
  30. int i2c_setup(void)
  31. {
  32. /* disable interrupts */
  33. PIE1bits.SSPIE = 0;
  34. /* tris */
  35. #define I2C_SDA_TRIS TRISBbits.TRISB0
  36. #define I2C_SCL_TRIS TRISBbits.TRISB1
  37. I2C_SCL_TRIS = 1;
  38. I2C_SDA_TRIS = 1;
  39. /* addressing */
  40. #define I2C_SLAVE_ADDR 0x8
  41. SSPADD = I2C_SLAVE_ADDR;
  42. /* enable module, slave mode 7 bit addressing, start and stop bits enabled */
  43. /* SSPCON1 = 0x2e; */
  44. SSPCON1 = 0x26;
  45. SSPSTAT = 0x00;
  46. /* enable general call, clock stretching */
  47. SSPCON2 = 0x81;
  48. /* SSPCON2 = 0x01; */
  49. /* enable interrupts */
  50. IPR1bits.SSPIP = 0;
  51. PIR1bits.SSPIF = 0;
  52. PIE1bits.SSPIE = 1;
  53. return 0;
  54. }
  55. static void write_byte(unsigned char b)
  56. {
  57. PIE1bits.SSPIE = 1;
  58. PIR1bits.SSPIF = 0;
  59. SSPBUF = b;
  60. SSPCON1bits.CKP = 1;
  61. while (PIR1bits.SSPIF == 0)
  62. ;
  63. PIE1bits.SSPIE = 1;
  64. }
  65. unsigned int i2c_write(i2c_addr_t a, unsigned char* s, unsigned int n)
  66. {
  67. #if 0
  68. /* async mode. tx at the next master read request. */
  69. unsigned int i;
  70. if ((i2c_size + n) > sizeof(i2c_buffer))
  71. n = sizeof(i2c_buffer) - i2c_pos;
  72. if (n == 0)
  73. return 0;
  74. lock_i2c();
  75. for (i = 0; i < n; ++i, ++i2c_pos)
  76. i2c_buffer[i2c_pos] = s[i];
  77. unlock_i2c();
  78. return i;
  79. #else
  80. unsigned int i;
  81. write_byte((unsigned char)a);
  82. for (i = 0; i < n; ++i, ++s)
  83. write_byte(*s);
  84. return i;
  85. #endif
  86. }
  87. unsigned int i2c_read(unsigned char* s, unsigned int n)
  88. {
  89. /* non blocking mode. bugged testing version. */
  90. unsigned int i;
  91. /* not safe, but will do for testing */
  92. if (i2c_pos == 0)
  93. return 0;
  94. lock_i2c();
  95. if (n > i2c_pos)
  96. n = i2c_pos;
  97. i2c_pos = 0;
  98. for (i = 0; i < n; ++i)
  99. s[i] = i2c_buffer[i];
  100. unlock_i2c();
  101. return i;
  102. }
  103. void i2c_handle_interrupt(void)
  104. {
  105. static unsigned char buf;
  106. static int is_avail;
  107. if (!PIR1bits.SSPIF)
  108. return ;
  109. PIR1bits.SSPIF = 0;
  110. switch (i2c_state)
  111. {
  112. case I2C_STATE_IDLE:
  113. i2c_state = I2C_STATE_READ;
  114. /* fallback */
  115. case I2C_STATE_READ:
  116. is_avail = SSPSTATbits.BF | SSPCON1bits.SSPOV;
  117. buf = SSPBUF;
  118. if (is_avail)
  119. {
  120. if (SSPSTATbits.D_A == 1)
  121. {
  122. if (SSPSTATbits.R_W == 1)
  123. i2c_state = I2C_STATE_READ;
  124. else
  125. i2c_state = I2C_STATE_WRITE;
  126. if (i2c_pos < sizeof(i2c_buffer))
  127. i2c_buffer[i2c_pos++] = buf;
  128. }
  129. else
  130. {
  131. i2c_addr = (i2c_addr_t)buf;
  132. }
  133. SSPSTATbits.BF = 0;
  134. SSPCON1bits.SSPOV = 0;
  135. }
  136. /* stop bit */
  137. if (SSPSTATbits.P)
  138. i2c_state = I2C_STATE_IDLE;
  139. /* release the line on last bit */
  140. SSPCON1bits.CKP = 1;
  141. break;
  142. case I2C_STATE_WRITE:
  143. /* stop bit */
  144. if (SSPSTATbits.P)
  145. i2c_state = I2C_STATE_IDLE;
  146. /* release the line once data put in buffer */
  147. SSPBUF = i2c_buffer[i2c_pos++];
  148. SSPCON1bits.CKP = 1;
  149. i2c_state = I2C_STATE_IDLE;
  150. SSPCON1bits.WCOL = 0;
  151. break;
  152. default:
  153. i2c_state = I2C_STATE_IDLE;
  154. break;
  155. }
  156. }