twi.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. twi.c - Stripped-down TWI/I2C library
  3. Copyright (c) 2006 Nicholas Zambetti. All right reserved.
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library 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 GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
  16. */
  17. #include <math.h>
  18. #include "config.h"
  19. #include "fastio.h"
  20. #include "twi.h"
  21. void twi_init(void)
  22. {
  23. // activate internal pullups for twi.
  24. WRITE(SDA_PIN, 1);
  25. WRITE(SCL_PIN, 1);
  26. // initialize twi prescaler and bit rate
  27. TWSR &= ~(_BV(TWPS0) | _BV(TWPS1));
  28. TWBR = ((F_CPU / TWI_FREQ) - 16) / 2;
  29. /* twi bit rate formula from atmega128 manual pg 204
  30. SCL Frequency = CPU Clock Frequency / (16 + (2 * TWBR))
  31. note: TWBR should be 10 or higher for master mode
  32. It is 72 for a 16mhz Wiring board with 100kHz TWI */
  33. }
  34. void twi_disable(void)
  35. {
  36. // deactivate internal pullups for twi.
  37. WRITE(SDA_PIN, 0);
  38. WRITE(SCL_PIN, 0);
  39. }
  40. static void twi_stop()
  41. {
  42. TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWSTO);
  43. }
  44. static uint8_t twi_wait(uint8_t status)
  45. {
  46. while(!(TWCR & _BV(TWINT)));
  47. if(TW_STATUS != status)
  48. {
  49. twi_stop();
  50. return 1;
  51. }
  52. return 0;
  53. }
  54. static uint8_t twi_start(uint8_t address, uint8_t reg)
  55. {
  56. // send start condition
  57. TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWSTA);
  58. if(twi_wait(TW_START))
  59. return 1;
  60. // send address
  61. TWDR = TW_WRITE | (address << 1);
  62. TWCR = _BV(TWEN) | _BV(TWINT);
  63. if(twi_wait(TW_MT_SLA_ACK))
  64. return 2;
  65. // send register
  66. TWDR = reg;
  67. TWCR = _BV(TWEN) | _BV(TWINT);
  68. if(twi_wait(TW_MT_DATA_ACK))
  69. return 3;
  70. return 0;
  71. }
  72. uint8_t twi_r8(uint8_t address, uint8_t reg, uint8_t* data)
  73. {
  74. if(twi_start(address, reg))
  75. return 1;
  76. // repeat start
  77. TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWSTA);
  78. if(twi_wait(TW_REP_START))
  79. return 2;
  80. // start receiving
  81. TWDR = TW_READ | (address << 1);
  82. TWCR = _BV(TWEN) | _BV(TWINT);
  83. if(twi_wait(TW_MR_SLA_ACK))
  84. return 3;
  85. // receive data
  86. TWCR = _BV(TWEN) | _BV(TWINT);
  87. if(twi_wait(TW_MR_DATA_NACK))
  88. return 4;
  89. *data = TWDR;
  90. // send stop
  91. twi_stop();
  92. return 0;
  93. }
  94. uint8_t twi_w8(uint8_t address, uint8_t reg, uint8_t data)
  95. {
  96. if(twi_start(address, reg))
  97. return 1;
  98. // send data
  99. TWDR = data;
  100. TWCR = _BV(TWEN) | _BV(TWINT);
  101. if(twi_wait(TW_MT_DATA_ACK))
  102. return 2;
  103. // send stop
  104. twi_stop();
  105. return 0;
  106. }