i2c_master.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* Library made by: g4lvanix
  2. * Github repository: https://github.com/g4lvanix/I2C-master-lib
  3. */
  4. #include <avr/io.h>
  5. #include <util/twi.h>
  6. #include "i2c_master.h"
  7. #include "timer.h"
  8. #define F_SCL 400000UL // SCL frequency
  9. #define Prescaler 1
  10. #define TWBR_val ((((F_CPU / F_SCL) / Prescaler) - 16 ) / 2)
  11. void i2c_init(void)
  12. {
  13. TWSR = 0; /* no prescaler */
  14. TWBR = (uint8_t)TWBR_val;
  15. //TWBR = 10;
  16. }
  17. uint8_t i2c_start(uint8_t address)
  18. {
  19. // reset TWI control register
  20. TWCR = 0;
  21. // transmit START condition
  22. TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
  23. #ifdef I2C_TIMEOUT
  24. uint16_t timeout_timer = timer_read();
  25. while( !(TWCR & (1<<TWINT)) ) {
  26. if ((timer_read() - timeout_timer) > I2C_TIMEOUT) {
  27. return 2; // should make these codes standard
  28. }
  29. }
  30. #else
  31. // wait for end of transmission
  32. while( !(TWCR & (1<<TWINT)) );
  33. #endif
  34. // check if the start condition was successfully transmitted
  35. if(((TW_STATUS & 0xF8) != TW_START) && ((TW_STATUS & 0xF8) != TW_REP_START)){ return 1; }
  36. // load slave address into data register
  37. TWDR = address;
  38. // start transmission of address
  39. TWCR = (1<<TWINT) | (1<<TWEN);
  40. #ifdef I2C_TIMEOUT
  41. timeout_timer = timer_read();
  42. while( !(TWCR & (1<<TWINT)) ) {
  43. if ((timer_read() - timeout_timer) > I2C_TIMEOUT) {
  44. return 2; // should make these codes standard
  45. }
  46. }
  47. #else
  48. // wait for end of transmission
  49. while( !(TWCR & (1<<TWINT)) );
  50. #endif
  51. // check if the device has acknowledged the READ / WRITE mode
  52. uint8_t twst = TW_STATUS & 0xF8;
  53. if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) return 1;
  54. return 0;
  55. }
  56. uint8_t i2c_write(uint8_t data)
  57. {
  58. // load data into data register
  59. TWDR = data;
  60. // start transmission of data
  61. TWCR = (1<<TWINT) | (1<<TWEN);
  62. #ifdef I2C_TIMEOUT
  63. uint16_t timeout_timer = timer_read();
  64. while( !(TWCR & (1<<TWINT)) ) {
  65. if ((timer_read() - timeout_timer) > I2C_TIMEOUT) {
  66. return 2; // should make these codes standard
  67. }
  68. }
  69. #else
  70. // wait for end of transmission
  71. while( !(TWCR & (1<<TWINT)) );
  72. #endif
  73. if( (TW_STATUS & 0xF8) != TW_MT_DATA_ACK ){ return 1; }
  74. return 0;
  75. }
  76. uint8_t i2c_read_ack(void)
  77. {
  78. // start TWI module and acknowledge data after reception
  79. TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
  80. #ifdef I2C_TIMEOUT
  81. uint16_t timeout_timer = timer_read();
  82. while( !(TWCR & (1<<TWINT)) ) {
  83. if ((timer_read() - timeout_timer) > I2C_TIMEOUT) {
  84. return 2; // should make these codes standard
  85. }
  86. }
  87. #else
  88. // wait for end of transmission
  89. while( !(TWCR & (1<<TWINT)) );
  90. #endif
  91. // return received data from TWDR
  92. return TWDR;
  93. }
  94. uint8_t i2c_read_nack(void)
  95. {
  96. // start receiving without acknowledging reception
  97. TWCR = (1<<TWINT) | (1<<TWEN);
  98. #ifdef I2C_TIMEOUT
  99. uint16_t timeout_timer = timer_read();
  100. while( !(TWCR & (1<<TWINT)) ) {
  101. if ((timer_read() - timeout_timer) > I2C_TIMEOUT) {
  102. return 2; // should make these codes standard
  103. }
  104. }
  105. #else
  106. // wait for end of transmission
  107. while( !(TWCR & (1<<TWINT)) );
  108. #endif
  109. // return received data from TWDR
  110. return TWDR;
  111. }
  112. uint8_t i2c_transmit(uint8_t address, uint8_t* data, uint16_t length)
  113. {
  114. if (i2c_start(address | I2C_WRITE)) return 1;
  115. for (uint16_t i = 0; i < length; i++)
  116. {
  117. if (i2c_write(data[i])) return 1;
  118. }
  119. i2c_stop();
  120. return 0;
  121. }
  122. uint8_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length)
  123. {
  124. if (i2c_start(address | I2C_READ)) return 1;
  125. for (uint16_t i = 0; i < (length-1); i++)
  126. {
  127. data[i] = i2c_read_ack();
  128. }
  129. data[(length-1)] = i2c_read_nack();
  130. i2c_stop();
  131. return 0;
  132. }
  133. uint8_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length)
  134. {
  135. if (i2c_start(devaddr | 0x00)) return 1;
  136. i2c_write(regaddr);
  137. for (uint16_t i = 0; i < length; i++)
  138. {
  139. if (i2c_write(data[i])) return 1;
  140. }
  141. i2c_stop();
  142. return 0;
  143. }
  144. uint8_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length)
  145. {
  146. if (i2c_start(devaddr)) return 1;
  147. i2c_write(regaddr);
  148. if (i2c_start(devaddr | 0x01)) return 1;
  149. for (uint16_t i = 0; i < (length-1); i++)
  150. {
  151. data[i] = i2c_read_ack();
  152. }
  153. data[(length-1)] = i2c_read_nack();
  154. i2c_stop();
  155. return 0;
  156. }
  157. uint8_t i2c_stop(void)
  158. {
  159. // transmit STOP condition
  160. TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
  161. #ifdef I2C_TIMEOUT
  162. uint16_t timeout_timer = timer_read();
  163. while(TWCR & (1<<TWSTO)) {
  164. if ((timer_read() - timeout_timer) > I2C_TIMEOUT) {
  165. return 2; // should make these codes standard
  166. }
  167. }
  168. #else
  169. // wait for end of transmission
  170. while(TWCR & (1<<TWSTO));
  171. #endif
  172. return 0;
  173. }