i2c_master.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* Copyright (C) 2019 Elia Ritterbusch
  2. +
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. */
  16. /* Library made by: g4lvanix
  17. * GitHub repository: https://github.com/g4lvanix/I2C-master-lib
  18. */
  19. #include <avr/io.h>
  20. #include <util/twi.h>
  21. #include "i2c_master.h"
  22. #include "timer.h"
  23. #include "wait.h"
  24. #ifndef F_SCL
  25. # define F_SCL 400000UL // SCL frequency
  26. #endif
  27. #ifndef I2C_START_RETRY_COUNT
  28. # define I2C_START_RETRY_COUNT 20
  29. #endif // I2C_START_RETRY_COUNT
  30. #define TWBR_val (((F_CPU / F_SCL) - 16) / 2)
  31. #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
  32. void i2c_init(void) {
  33. TWSR = 0; /* no prescaler */
  34. TWBR = (uint8_t)TWBR_val;
  35. #ifdef __AVR_ATmega32A__
  36. // set pull-up resistors on I2C bus pins
  37. PORTC |= 0b11;
  38. // enable TWI (two-wire interface)
  39. TWCR |= (1 << TWEN);
  40. // enable TWI interrupt and slave address ACK
  41. TWCR |= (1 << TWIE);
  42. TWCR |= (1 << TWEA);
  43. #endif
  44. }
  45. static i2c_status_t i2c_start_impl(uint8_t address, uint16_t timeout) {
  46. // reset TWI control register
  47. TWCR = 0;
  48. // transmit START condition
  49. TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);
  50. uint16_t timeout_timer = timer_read();
  51. while (!(TWCR & (1 << TWINT))) {
  52. if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
  53. return I2C_STATUS_TIMEOUT;
  54. }
  55. }
  56. // check if the start condition was successfully transmitted
  57. if (((TW_STATUS & 0xF8) != TW_START) && ((TW_STATUS & 0xF8) != TW_REP_START)) {
  58. return I2C_STATUS_ERROR;
  59. }
  60. // load slave address into data register
  61. TWDR = address;
  62. // start transmission of address
  63. TWCR = (1 << TWINT) | (1 << TWEN);
  64. 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. // check if the device has acknowledged the READ / WRITE mode
  71. uint8_t twst = TW_STATUS & 0xF8;
  72. if ((twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK)) {
  73. return I2C_STATUS_ERROR;
  74. }
  75. return I2C_STATUS_SUCCESS;
  76. }
  77. i2c_status_t i2c_start(uint8_t address, uint16_t timeout) {
  78. // Retry i2c_start_impl a bunch times in case the remote side has interrupts disabled.
  79. uint16_t timeout_timer = timer_read();
  80. uint16_t time_slice = MAX(1, (timeout == (I2C_TIMEOUT_INFINITE)) ? 5 : (timeout / (I2C_START_RETRY_COUNT))); // if it's infinite, wait 1ms between attempts, otherwise split up the entire timeout into the number of retries
  81. i2c_status_t status;
  82. do {
  83. status = i2c_start_impl(address, time_slice);
  84. } while ((status < 0) && ((timeout == I2C_TIMEOUT_INFINITE) || (timer_elapsed(timeout_timer) < timeout)));
  85. return status;
  86. }
  87. i2c_status_t i2c_write(uint8_t data, uint16_t timeout) {
  88. // load data into data register
  89. TWDR = data;
  90. // start transmission of data
  91. TWCR = (1 << TWINT) | (1 << TWEN);
  92. uint16_t timeout_timer = timer_read();
  93. while (!(TWCR & (1 << TWINT))) {
  94. if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
  95. return I2C_STATUS_TIMEOUT;
  96. }
  97. }
  98. if ((TW_STATUS & 0xF8) != TW_MT_DATA_ACK) {
  99. return I2C_STATUS_ERROR;
  100. }
  101. return I2C_STATUS_SUCCESS;
  102. }
  103. int16_t i2c_read_ack(uint16_t timeout) {
  104. // start TWI module and acknowledge data after reception
  105. TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
  106. uint16_t timeout_timer = timer_read();
  107. while (!(TWCR & (1 << TWINT))) {
  108. if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
  109. return I2C_STATUS_TIMEOUT;
  110. }
  111. }
  112. // return received data from TWDR
  113. return TWDR;
  114. }
  115. int16_t i2c_read_nack(uint16_t timeout) {
  116. // start receiving without acknowledging reception
  117. TWCR = (1 << TWINT) | (1 << TWEN);
  118. uint16_t timeout_timer = timer_read();
  119. while (!(TWCR & (1 << TWINT))) {
  120. if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
  121. return I2C_STATUS_TIMEOUT;
  122. }
  123. }
  124. // return received data from TWDR
  125. return TWDR;
  126. }
  127. i2c_status_t i2c_transmit(uint8_t address, const uint8_t* data, uint16_t length, uint16_t timeout) {
  128. i2c_status_t status = i2c_start(address | I2C_WRITE, timeout);
  129. for (uint16_t i = 0; i < length && status >= 0; i++) {
  130. status = i2c_write(data[i], timeout);
  131. }
  132. i2c_stop();
  133. return status;
  134. }
  135. i2c_status_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout) {
  136. i2c_status_t status = i2c_start(address | I2C_READ, timeout);
  137. for (uint16_t i = 0; i < (length - 1) && status >= 0; i++) {
  138. status = i2c_read_ack(timeout);
  139. if (status >= 0) {
  140. data[i] = status;
  141. }
  142. }
  143. if (status >= 0) {
  144. status = i2c_read_nack(timeout);
  145. if (status >= 0) {
  146. data[(length - 1)] = status;
  147. }
  148. }
  149. i2c_stop();
  150. return (status < 0) ? status : I2C_STATUS_SUCCESS;
  151. }
  152. i2c_status_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, const uint8_t* data, uint16_t length, uint16_t timeout) {
  153. i2c_status_t status = i2c_start(devaddr | 0x00, timeout);
  154. if (status >= 0) {
  155. status = i2c_write(regaddr, timeout);
  156. for (uint16_t i = 0; i < length && status >= 0; i++) {
  157. status = i2c_write(data[i], timeout);
  158. }
  159. }
  160. i2c_stop();
  161. return status;
  162. }
  163. i2c_status_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout) {
  164. i2c_status_t status = i2c_start(devaddr, timeout);
  165. if (status < 0) {
  166. goto error;
  167. }
  168. status = i2c_write(regaddr, timeout);
  169. if (status < 0) {
  170. goto error;
  171. }
  172. status = i2c_start(devaddr | 0x01, timeout);
  173. for (uint16_t i = 0; i < (length - 1) && status >= 0; i++) {
  174. status = i2c_read_ack(timeout);
  175. if (status >= 0) {
  176. data[i] = status;
  177. }
  178. }
  179. if (status >= 0) {
  180. status = i2c_read_nack(timeout);
  181. if (status >= 0) {
  182. data[(length - 1)] = status;
  183. }
  184. }
  185. error:
  186. i2c_stop();
  187. return (status < 0) ? status : I2C_STATUS_SUCCESS;
  188. }
  189. void i2c_stop(void) {
  190. // transmit STOP condition
  191. TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
  192. }