i2c_master.c 4.8 KB

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