You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
6.0 KiB

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