i2c_master.c 5.0 KB

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