i2c_master.c 5.2 KB

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