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