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.

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