i2c_master.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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_transmit_and_receive(uint8_t address, const uint8_t* tx_data, uint16_t tx_length, uint8_t* rx_data, uint16_t rx_length, uint16_t timeout) {
  168. i2c_status_t status = i2c_start(address | I2C_ACTION_WRITE, timeout);
  169. for (uint16_t i = 0; i < tx_length && status >= 0; i++) {
  170. status = i2c_write(tx_data[i], timeout);
  171. }
  172. for (uint16_t i = 0; i < (rx_length - 1) && status >= 0; i++) {
  173. status = i2c_read_ack(timeout);
  174. if (status >= 0) {
  175. rx_data[i] = status;
  176. }
  177. }
  178. if (status >= 0) {
  179. status = i2c_read_nack(timeout);
  180. if (status >= 0) {
  181. rx_data[(rx_length - 1)] = status;
  182. }
  183. }
  184. i2c_stop();
  185. return status;
  186. }
  187. i2c_status_t i2c_write_register(uint8_t devaddr, uint8_t regaddr, const uint8_t* data, uint16_t length, uint16_t timeout) {
  188. i2c_status_t status = i2c_start(devaddr | 0x00, timeout);
  189. if (status >= 0) {
  190. status = i2c_write(regaddr, timeout);
  191. for (uint16_t i = 0; i < length && status >= 0; i++) {
  192. status = i2c_write(data[i], timeout);
  193. }
  194. }
  195. i2c_stop();
  196. return status;
  197. }
  198. i2c_status_t i2c_write_register16(uint8_t devaddr, uint16_t regaddr, const uint8_t* data, uint16_t length, uint16_t timeout) {
  199. i2c_status_t status = i2c_start(devaddr | 0x00, timeout);
  200. if (status >= 0) {
  201. status = i2c_write(regaddr >> 8, timeout);
  202. if (status >= 0) {
  203. status = i2c_write(regaddr & 0xFF, timeout);
  204. for (uint16_t i = 0; i < length && status >= 0; i++) {
  205. status = i2c_write(data[i], timeout);
  206. }
  207. }
  208. }
  209. i2c_stop();
  210. return status;
  211. }
  212. i2c_status_t i2c_read_register(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout) {
  213. i2c_status_t status = i2c_start(devaddr, timeout);
  214. if (status < 0) {
  215. goto error;
  216. }
  217. status = i2c_write(regaddr, timeout);
  218. if (status < 0) {
  219. goto error;
  220. }
  221. status = i2c_start(devaddr | 0x01, timeout);
  222. for (uint16_t i = 0; i < (length - 1) && status >= 0; i++) {
  223. status = i2c_read_ack(timeout);
  224. if (status >= 0) {
  225. data[i] = status;
  226. }
  227. }
  228. if (status >= 0) {
  229. status = i2c_read_nack(timeout);
  230. if (status >= 0) {
  231. data[(length - 1)] = status;
  232. }
  233. }
  234. error:
  235. i2c_stop();
  236. return (status < 0) ? status : I2C_STATUS_SUCCESS;
  237. }
  238. i2c_status_t i2c_read_register16(uint8_t devaddr, uint16_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout) {
  239. i2c_status_t status = i2c_start(devaddr, timeout);
  240. if (status < 0) {
  241. goto error;
  242. }
  243. status = i2c_write(regaddr >> 8, timeout);
  244. if (status < 0) {
  245. goto error;
  246. }
  247. status = i2c_write(regaddr & 0xFF, timeout);
  248. if (status < 0) {
  249. goto error;
  250. }
  251. status = i2c_start(devaddr | 0x01, timeout);
  252. for (uint16_t i = 0; i < (length - 1) && status >= 0; i++) {
  253. status = i2c_read_ack(timeout);
  254. if (status >= 0) {
  255. data[i] = status;
  256. }
  257. }
  258. if (status >= 0) {
  259. status = i2c_read_nack(timeout);
  260. if (status >= 0) {
  261. data[(length - 1)] = status;
  262. }
  263. }
  264. error:
  265. i2c_stop();
  266. return (status < 0) ? status : I2C_STATUS_SUCCESS;
  267. }
  268. __attribute__((weak)) i2c_status_t i2c_ping_address(uint8_t address, uint16_t timeout) {
  269. i2c_status_t status = i2c_start(address, timeout);
  270. i2c_stop();
  271. return status;
  272. }