i2c_master.c 5.7 KB

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