i2c_master.c 5.2 KB

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