i2c_master.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* Library made by: g4lvanix
  2. * Github repository: https://github.com/g4lvanix/I2C-master-lib
  3. */
  4. #include <avr/io.h>
  5. #include <util/twi.h>
  6. #include "i2c_master.h"
  7. #include "timer.h"
  8. #define F_SCL 400000UL // SCL frequency
  9. #define Prescaler 1
  10. #define TWBR_val ((((F_CPU / F_SCL) / Prescaler) - 16 ) / 2)
  11. void i2c_init(void)
  12. {
  13. TWSR = 0; /* no prescaler */
  14. TWBR = (uint8_t)TWBR_val;
  15. //TWBR = 10;
  16. }
  17. i2c_status_t i2c_start(uint8_t address, uint8_t timeout)
  18. {
  19. // reset TWI control register
  20. TWCR = 0;
  21. // transmit START condition
  22. TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
  23. uint16_t timeout_timer = timer_read();
  24. while( !(TWCR & (1<<TWINT)) ) {
  25. if (timeout && (timer_read() - timeout_timer) > timeout) {
  26. return I2C_STATUS_TIMEOUT;
  27. }
  28. }
  29. // check if the start condition was successfully transmitted
  30. if(((TW_STATUS & 0xF8) != TW_START) && ((TW_STATUS & 0xF8) != TW_REP_START)){ return 1; }
  31. // load slave address into data register
  32. TWDR = address;
  33. // start transmission of address
  34. TWCR = (1<<TWINT) | (1<<TWEN);
  35. timeout_timer = timer_read();
  36. while( !(TWCR & (1<<TWINT)) ) {
  37. if (timeout && (timer_read() - timeout_timer) > I2C_TIMEOUT) {
  38. return I2C_STATUS_TIMEOUT;
  39. }
  40. }
  41. // check if the device has acknowledged the READ / WRITE mode
  42. uint8_t twst = TW_STATUS & 0xF8;
  43. if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) return 1;
  44. return 0;
  45. }
  46. i2c_status_t i2c_write(uint8_t data, uint8_t timeout)
  47. {
  48. // load data into data register
  49. TWDR = data;
  50. // start transmission of data
  51. TWCR = (1<<TWINT) | (1<<TWEN);
  52. uint16_t timeout_timer = timer_read();
  53. while( !(TWCR & (1<<TWINT)) ) {
  54. if (timeout && (timer_read() - timeout_timer) > I2C_TIMEOUT) {
  55. return I2C_STATUS_TIMEOUT;
  56. }
  57. }
  58. if( (TW_STATUS & 0xF8) != TW_MT_DATA_ACK ){ return 1; }
  59. return 0;
  60. }
  61. i2c_status_t i2c_read_ack(uint8_t timeout)
  62. {
  63. // start TWI module and acknowledge data after reception
  64. TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
  65. uint16_t timeout_timer = timer_read();
  66. while( !(TWCR & (1<<TWINT)) ) {
  67. if (timeout && (timer_read() - timeout_timer) > I2C_TIMEOUT) {
  68. return I2C_STATUS_TIMEOUT;
  69. }
  70. }
  71. // return received data from TWDR
  72. return TWDR;
  73. }
  74. i2c_status_t i2c_read_nack(uint8_t timeout)
  75. {
  76. // start receiving without acknowledging reception
  77. TWCR = (1<<TWINT) | (1<<TWEN);
  78. uint16_t timeout_timer = timer_read();
  79. while( !(TWCR & (1<<TWINT)) ) {
  80. if (timeout && (timer_read() - timeout_timer) > I2C_TIMEOUT) {
  81. return I2C_STATUS_TIMEOUT;
  82. }
  83. }
  84. // return received data from TWDR
  85. return TWDR;
  86. }
  87. i2c_status_t i2c_transmit(uint8_t address, uint8_t* data, uint16_t length)
  88. {
  89. if (i2c_start(address | I2C_WRITE)) return 1;
  90. for (uint16_t i = 0; i < length; i++)
  91. {
  92. if (i2c_write(data[i])) return 1;
  93. }
  94. i2c_stop();
  95. return 0;
  96. }
  97. uint8_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length)
  98. {
  99. if (i2c_start(address | I2C_READ)) return 1;
  100. for (uint16_t i = 0; i < (length-1); i++)
  101. {
  102. data[i] = i2c_read_ack();
  103. }
  104. data[(length-1)] = i2c_read_nack();
  105. i2c_stop();
  106. return 0;
  107. }
  108. uint8_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length)
  109. {
  110. if (i2c_start(devaddr | 0x00)) return 1;
  111. i2c_write(regaddr);
  112. for (uint16_t i = 0; i < length; i++)
  113. {
  114. if (i2c_write(data[i])) return 1;
  115. }
  116. i2c_stop();
  117. return 0;
  118. }
  119. uint8_t i2c_readReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length)
  120. {
  121. if (i2c_start(devaddr)) return 1;
  122. i2c_write(regaddr);
  123. if (i2c_start(devaddr | 0x01)) return 1;
  124. for (uint16_t i = 0; i < (length-1); i++)
  125. {
  126. data[i] = i2c_read_ack();
  127. }
  128. data[(length-1)] = i2c_read_nack();
  129. i2c_stop();
  130. return 0;
  131. }
  132. i2c_status_t i2c_stop(uint8_t timeout)
  133. {
  134. // transmit STOP condition
  135. TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
  136. uint16_t timeout_timer = timer_read();
  137. while(TWCR & (1<<TWSTO)) {
  138. if (timeout && (timer_read() - timeout_timer) > I2C_TIMEOUT) {
  139. return I2C_STATUS_TIMEOUT;
  140. }
  141. }
  142. return 0;
  143. }