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.

300 lines
8.3 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. #ifndef I2C_START_RETRY_COUNT
  28. # define I2C_START_RETRY_COUNT 20
  29. #endif // I2C_START_RETRY_COUNT
  30. #define I2C_ACTION_READ 0x01
  31. #define I2C_ACTION_WRITE 0x00
  32. #define TWBR_val (((F_CPU / F_SCL) - 16) / 2)
  33. #define MAX(X, Y) ((X) > (Y) ? (X) : (Y))
  34. void i2c_init(void) {
  35. TWSR = 0; /* no prescaler */
  36. TWBR = (uint8_t)TWBR_val;
  37. #ifdef __AVR_ATmega32A__
  38. // set pull-up resistors on I2C bus pins
  39. PORTC |= 0b11;
  40. // enable TWI (two-wire interface)
  41. TWCR |= (1 << TWEN);
  42. // enable TWI interrupt and slave address ACK
  43. TWCR |= (1 << TWIE);
  44. TWCR |= (1 << TWEA);
  45. #endif
  46. }
  47. static i2c_status_t i2c_start_impl(uint8_t address, uint16_t timeout) {
  48. // reset TWI control register
  49. TWCR = 0;
  50. // transmit START condition
  51. TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);
  52. uint16_t timeout_timer = timer_read();
  53. while (!(TWCR & (1 << TWINT))) {
  54. if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
  55. return I2C_STATUS_TIMEOUT;
  56. }
  57. }
  58. // check if the start condition was successfully transmitted
  59. if (((TW_STATUS & 0xF8) != TW_START) && ((TW_STATUS & 0xF8) != TW_REP_START)) {
  60. return I2C_STATUS_ERROR;
  61. }
  62. // load slave address into data register
  63. TWDR = address;
  64. // start transmission of address
  65. TWCR = (1 << TWINT) | (1 << TWEN);
  66. timeout_timer = timer_read();
  67. while (!(TWCR & (1 << TWINT))) {
  68. if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
  69. return I2C_STATUS_TIMEOUT;
  70. }
  71. }
  72. // check if the device has acknowledged the READ / WRITE mode
  73. uint8_t twst = TW_STATUS & 0xF8;
  74. if ((twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK)) {
  75. return I2C_STATUS_ERROR;
  76. }
  77. return I2C_STATUS_SUCCESS;
  78. }
  79. i2c_status_t i2c_start(uint8_t address, uint16_t timeout) {
  80. // Retry i2c_start_impl a bunch times in case the remote side has interrupts disabled.
  81. uint16_t timeout_timer = timer_read();
  82. 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
  83. i2c_status_t status;
  84. do {
  85. status = i2c_start_impl(address, time_slice);
  86. } while ((status < 0) && ((timeout == I2C_TIMEOUT_INFINITE) || (timer_elapsed(timeout_timer) < timeout)));
  87. return status;
  88. }
  89. i2c_status_t i2c_write(uint8_t data, uint16_t timeout) {
  90. // load data into data register
  91. TWDR = data;
  92. // start transmission of data
  93. TWCR = (1 << TWINT) | (1 << TWEN);
  94. uint16_t timeout_timer = timer_read();
  95. while (!(TWCR & (1 << TWINT))) {
  96. if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
  97. return I2C_STATUS_TIMEOUT;
  98. }
  99. }
  100. if ((TW_STATUS & 0xF8) != TW_MT_DATA_ACK) {
  101. return I2C_STATUS_ERROR;
  102. }
  103. return I2C_STATUS_SUCCESS;
  104. }
  105. int16_t i2c_read_ack(uint16_t timeout) {
  106. // start TWI module and acknowledge data after reception
  107. TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWEA);
  108. uint16_t timeout_timer = timer_read();
  109. while (!(TWCR & (1 << TWINT))) {
  110. if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
  111. return I2C_STATUS_TIMEOUT;
  112. }
  113. }
  114. // return received data from TWDR
  115. return TWDR;
  116. }
  117. int16_t i2c_read_nack(uint16_t timeout) {
  118. // start receiving without acknowledging reception
  119. TWCR = (1 << TWINT) | (1 << TWEN);
  120. uint16_t timeout_timer = timer_read();
  121. while (!(TWCR & (1 << TWINT))) {
  122. if ((timeout != I2C_TIMEOUT_INFINITE) && ((timer_read() - timeout_timer) >= timeout)) {
  123. return I2C_STATUS_TIMEOUT;
  124. }
  125. }
  126. // return received data from TWDR
  127. return TWDR;
  128. }
  129. i2c_status_t i2c_transmit(uint8_t address, const uint8_t* data, uint16_t length, uint16_t timeout) {
  130. i2c_status_t status = i2c_start(address | I2C_ACTION_WRITE, timeout);
  131. for (uint16_t i = 0; i < length && status >= 0; i++) {
  132. status = i2c_write(data[i], timeout);
  133. }
  134. i2c_stop();
  135. return status;
  136. }
  137. i2c_status_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16_t timeout) {
  138. i2c_status_t status = i2c_start(address | I2C_ACTION_READ, timeout);
  139. for (uint16_t i = 0; i < (length - 1) && status >= 0; i++) {
  140. status = i2c_read_ack(timeout);
  141. if (status >= 0) {
  142. data[i] = status;
  143. }
  144. }
  145. if (status >= 0) {
  146. status = i2c_read_nack(timeout);
  147. if (status >= 0) {
  148. data[(length - 1)] = status;
  149. }
  150. }
  151. i2c_stop();
  152. return (status < 0) ? status : I2C_STATUS_SUCCESS;
  153. }
  154. i2c_status_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, const uint8_t* data, uint16_t length, uint16_t timeout) {
  155. i2c_status_t status = i2c_start(devaddr | 0x00, timeout);
  156. if (status >= 0) {
  157. status = i2c_write(regaddr, timeout);
  158. for (uint16_t i = 0; i < length && status >= 0; i++) {
  159. status = i2c_write(data[i], timeout);
  160. }
  161. }
  162. i2c_stop();
  163. return status;
  164. }
  165. i2c_status_t i2c_writeReg16(uint8_t devaddr, uint16_t regaddr, const uint8_t* data, uint16_t length, uint16_t timeout) {
  166. i2c_status_t status = i2c_start(devaddr | 0x00, timeout);
  167. if (status >= 0) {
  168. status = i2c_write(regaddr >> 8, timeout);
  169. if (status >= 0) {
  170. status = i2c_write(regaddr & 0xFF, timeout);
  171. for (uint16_t i = 0; i < length && status >= 0; i++) {
  172. status = i2c_write(data[i], timeout);
  173. }
  174. }
  175. }
  176. i2c_stop();
  177. return status;
  178. }
  179. i2c_status_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout) {
  180. i2c_status_t status = i2c_start(devaddr, timeout);
  181. if (status < 0) {
  182. goto error;
  183. }
  184. status = i2c_write(regaddr, timeout);
  185. if (status < 0) {
  186. goto error;
  187. }
  188. status = i2c_start(devaddr | 0x01, timeout);
  189. for (uint16_t i = 0; i < (length - 1) && status >= 0; i++) {
  190. status = i2c_read_ack(timeout);
  191. if (status >= 0) {
  192. data[i] = status;
  193. }
  194. }
  195. if (status >= 0) {
  196. status = i2c_read_nack(timeout);
  197. if (status >= 0) {
  198. data[(length - 1)] = status;
  199. }
  200. }
  201. error:
  202. i2c_stop();
  203. return (status < 0) ? status : I2C_STATUS_SUCCESS;
  204. }
  205. i2c_status_t i2c_readReg16(uint8_t devaddr, uint16_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout) {
  206. i2c_status_t status = i2c_start(devaddr, timeout);
  207. if (status < 0) {
  208. goto error;
  209. }
  210. status = i2c_write(regaddr >> 8, timeout);
  211. if (status < 0) {
  212. goto error;
  213. }
  214. status = i2c_write(regaddr & 0xFF, timeout);
  215. if (status < 0) {
  216. goto error;
  217. }
  218. status = i2c_start(devaddr | 0x01, timeout);
  219. for (uint16_t i = 0; i < (length - 1) && status >= 0; i++) {
  220. status = i2c_read_ack(timeout);
  221. if (status >= 0) {
  222. data[i] = status;
  223. }
  224. }
  225. if (status >= 0) {
  226. status = i2c_read_nack(timeout);
  227. if (status >= 0) {
  228. data[(length - 1)] = status;
  229. }
  230. }
  231. error:
  232. i2c_stop();
  233. return (status < 0) ? status : I2C_STATUS_SUCCESS;
  234. }
  235. void i2c_stop(void) {
  236. // transmit STOP condition
  237. TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
  238. }