twi.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #include "Timer.h"
  22. void twi_init(void)
  23. {
  24. // activate internal pullups for twi.
  25. WRITE(SDA_PIN, 1);
  26. WRITE(SCL_PIN, 1);
  27. // initialize twi prescaler and bit rate
  28. TWSR &= ~(_BV(TWPS0) | _BV(TWPS1));
  29. TWBR = ((F_CPU / TWI_FREQ) - 16) / 2;
  30. /* twi bit rate formula from atmega128 manual pg 204
  31. SCL Frequency = CPU Clock Frequency / (16 + (2 * TWBR))
  32. note: TWBR should be 10 or higher for master mode
  33. It is 72 for a 16mhz Wiring board with 100kHz TWI */
  34. }
  35. void twi_disable(void)
  36. {
  37. // disable TWI hardware.
  38. TWCR = 0;
  39. // deactivate internal pullups for twi.
  40. WRITE(SDA_PIN, 0);
  41. WRITE(SCL_PIN, 0);
  42. }
  43. static void twi_stop()
  44. {
  45. TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWSTO);
  46. }
  47. static uint8_t twi_wait(uint8_t status)
  48. {
  49. ShortTimer timmy;
  50. timmy.start();
  51. while(!(TWCR & _BV(TWINT))) {
  52. if (timmy.expired(TWI_TIMEOUT_MS)) {
  53. return 2;
  54. }
  55. }
  56. if(TW_STATUS != status)
  57. {
  58. twi_stop();
  59. return 1;
  60. }
  61. return 0;
  62. }
  63. static uint8_t twi_start(uint8_t address, uint8_t reg)
  64. {
  65. // send start condition
  66. TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWSTA);
  67. if(twi_wait(TW_START))
  68. return 1;
  69. // send address
  70. TWDR = TW_WRITE | (address << 1);
  71. TWCR = _BV(TWEN) | _BV(TWINT);
  72. if(twi_wait(TW_MT_SLA_ACK))
  73. return 2;
  74. // send register
  75. TWDR = reg;
  76. TWCR = _BV(TWEN) | _BV(TWINT);
  77. if(twi_wait(TW_MT_DATA_ACK))
  78. return 3;
  79. return 0;
  80. }
  81. uint8_t twi_r8(uint8_t address, uint8_t reg, uint8_t* data)
  82. {
  83. if(twi_start(address, reg))
  84. return 1;
  85. // repeat start
  86. TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWSTA);
  87. if(twi_wait(TW_REP_START))
  88. return 2;
  89. // start receiving
  90. TWDR = TW_READ | (address << 1);
  91. TWCR = _BV(TWEN) | _BV(TWINT);
  92. if(twi_wait(TW_MR_SLA_ACK))
  93. return 3;
  94. // receive data
  95. TWCR = _BV(TWEN) | _BV(TWINT);
  96. if(twi_wait(TW_MR_DATA_NACK))
  97. return 4;
  98. *data = TWDR;
  99. // send stop
  100. twi_stop();
  101. return 0;
  102. }
  103. uint8_t twi_w8(uint8_t address, uint8_t reg, uint8_t data)
  104. {
  105. if(twi_start(address, reg))
  106. return 1;
  107. // send data
  108. TWDR = data;
  109. TWCR = _BV(TWEN) | _BV(TWINT);
  110. if(twi_wait(TW_MT_DATA_ACK))
  111. return 2;
  112. // send stop
  113. twi_stop();
  114. return 0;
  115. }