1
0

I2C.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * I2C.c
  3. * k4be 2022
  4. * License: BSD
  5. */
  6. #include <avr/io.h>
  7. #include <util/delay.h>
  8. #include "I2C.h"
  9. #include "main.h"
  10. #define I2C_FREQ 400000
  11. #define PRESCALER 1 /* possible values: 1, 4, 16, 64 */
  12. void I2C_init(void){
  13. TWBR = (F_CPU/I2C_FREQ - 16.0)/2.0/PRESCALER;
  14. #if PRESCALER == 1
  15. TWSR |= 0;
  16. #elif PRESCALER == 4
  17. TWSR |= _BV(TWPS0);
  18. #elif PRESCALER == 16
  19. TWSR |= _BV(TWPS1);
  20. #elif PRESCALER == 64
  21. TWSR |= _BV(TWPS1) | _BV(TWPS0);
  22. #else
  23. #error Invalid prescaler
  24. #endif
  25. }
  26. /* two byte command (or command + value) */
  27. unsigned int I2C_SendCommand(unsigned char address, unsigned char command, unsigned char data_byte)
  28. {
  29. unsigned int error = ERROR_I2C;
  30. for(int index = 0 ; (index < MAX_REPEAT_I2C) && (error != ERROR_NO); index++)
  31. {
  32. error = 0;
  33. TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN); // Send START condition
  34. error |= I2C_WaitForTWInt();
  35. error |= I2C_SendOneCommandByte(address << 1);
  36. error |= I2C_SendOneCommandByte(command);
  37. error |= I2C_SendOneCommandByte(data_byte);
  38. TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
  39. }
  40. return error;
  41. }
  42. /* three byte command (or command + 2 values) */
  43. unsigned int I2C_SendCommand3byte(unsigned char address,unsigned char command,unsigned char data0, unsigned char data1)
  44. {
  45. unsigned int error = ERROR_I2C;
  46. for(int index = 0 ; (index < MAX_REPEAT_I2C) && (error != ERROR_NO); index++)
  47. {
  48. error = 0;
  49. TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN); // Send START condition
  50. error |= I2C_WaitForTWInt();
  51. error |= I2C_SendOneCommandByte(address << 1);
  52. error |= I2C_SendOneCommandByte(command);
  53. error |= I2C_SendOneCommandByte(data0);
  54. error |= I2C_SendOneCommandByte(data1);
  55. TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
  56. }
  57. return error;
  58. }
  59. /* read single byte */
  60. unsigned int I2C_ReceiveCommand(unsigned char address, unsigned char command, unsigned char *data_byte)
  61. {
  62. unsigned int error = ERROR_I2C;
  63. unsigned char _unused;
  64. for(int index = 0 ; (index < MAX_REPEAT_I2C) && (error != ERROR_NO); index++)
  65. {
  66. error = 0;
  67. TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN); // Send START condition
  68. error |= I2C_WaitForTWInt();
  69. error |= I2C_SendOneCommandByte(address << 1);
  70. error |= I2C_SendOneCommandByte(command);
  71. error |= I2C_SendOneCommandByte((address << 1) | 1);
  72. TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
  73. error |= I2C_WaitForTWInt();
  74. error |= I2C_ReceiveOneCommandByte(&_unused, 0);
  75. error |= I2C_ReceiveOneCommandByte(data_byte, 0);
  76. TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
  77. }
  78. return error;
  79. }
  80. /* send command + any byte count */
  81. unsigned int I2C_Send_n_bytes(unsigned char address,unsigned char command, const unsigned char *data, unsigned int length)
  82. {
  83. unsigned int error = ERROR_I2C;
  84. unsigned int i;
  85. for(int index = 0 ; (index < MAX_REPEAT_I2C) && (error != ERROR_NO); index++)
  86. {
  87. error = 0;
  88. TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN); // Send START condition
  89. error |= I2C_WaitForTWInt();
  90. error |= I2C_SendOneCommandByte(address << 1);
  91. error |= I2C_SendOneCommandByte(command);
  92. for(i=0;i<length;i++){
  93. error |= I2C_SendOneCommandByte(data[i]);
  94. }
  95. TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
  96. }
  97. return error;
  98. }
  99. /* read any byte count */
  100. unsigned int I2C_Receive_n_bytes(unsigned char address, unsigned char command, unsigned char *data, unsigned int length)
  101. {
  102. unsigned int i;
  103. unsigned int error = ERROR_I2C;
  104. for(int index = 0 ; (index < MAX_REPEAT_I2C) && (error != ERROR_NO); index++)
  105. {
  106. error = 0;
  107. TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN); // Send START condition
  108. error |= I2C_WaitForTWInt();
  109. error |= I2C_SendOneCommandByte(address << 1);
  110. error |= I2C_SendOneCommandByte(command);
  111. TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
  112. _delay_us(1);
  113. TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
  114. error |= I2C_WaitForTWInt();
  115. error |= I2C_SendOneCommandByte((address << 1) | 1);
  116. for(i=0; i<length; i++){
  117. error |= I2C_ReceiveOneCommandByte(&data[i], i != length-1);
  118. }
  119. TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
  120. }
  121. return error;
  122. }
  123. /* read 2 bytes */
  124. unsigned int I2C_ReceiveCommand3byte(unsigned char address, unsigned char command, unsigned char *data0, unsigned char *data1)
  125. {
  126. unsigned int error = ERROR_I2C;
  127. unsigned char _unused;
  128. for(int index = 0 ; (index < MAX_REPEAT_I2C) && (error != ERROR_NO); index++)
  129. {
  130. error = 0;
  131. TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);//Send START condition
  132. error |= I2C_WaitForTWInt();
  133. error |= I2C_SendOneCommandByte(address << 1);
  134. error |= I2C_SendOneCommandByte(command);
  135. error |= I2C_SendOneCommandByte((address << 1) | 1);
  136. TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
  137. error |= I2C_WaitForTWInt();
  138. error |= I2C_ReceiveOneCommandByte(&_unused, 0);
  139. error |= I2C_ReceiveOneCommandByte(data0, 0);
  140. error |= I2C_ReceiveOneCommandByte(data1, 0);
  141. TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
  142. }
  143. return error;
  144. }
  145. unsigned int I2C_SendOneCommandByte(unsigned char command)
  146. {
  147. TWDR = command;
  148. TWCR = (1<<TWINT) | (1<<TWEN);
  149. return I2C_WaitForTWInt();
  150. }
  151. unsigned int I2C_ReceiveOneCommandByte(unsigned char *command, unsigned char ack)
  152. {
  153. TWCR = (1<<TWINT) | (1<<TWEN) | (ack?_BV(TWEA):0);
  154. unsigned int error = I2C_WaitForTWInt();
  155. *command = TWDR;
  156. return error;
  157. }
  158. unsigned int I2C_WaitForTWInt(void){
  159. unsigned char count = 10;
  160. while (count && !(TWCR & (1<<TWINT))){
  161. count--;
  162. _delay_us(1000000.0/I2C_FREQ*10); /* wait for 10 bit periods; the transaction never succeeds quicker */
  163. }
  164. if(count == 0) return ERROR_I2C_TIMEOUT;
  165. return ERROR_NO;
  166. }