twi.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 "Arduino.h" // for digitalWrite
  19. #ifndef cbi
  20. #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
  21. #endif
  22. #ifndef sbi
  23. #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
  24. #endif
  25. #include "twi.h"
  26. void twi_init(void)
  27. {
  28. // activate internal pullups for twi.
  29. digitalWrite(SDA, 1);
  30. digitalWrite(SCL, 1);
  31. // initialize twi prescaler and bit rate
  32. cbi(TWSR, TWPS0);
  33. cbi(TWSR, TWPS1);
  34. TWBR = ((F_CPU / TWI_FREQ) - 16) / 2;
  35. /* twi bit rate formula from atmega128 manual pg 204
  36. SCL Frequency = CPU Clock Frequency / (16 + (2 * TWBR))
  37. note: TWBR should be 10 or higher for master mode
  38. It is 72 for a 16mhz Wiring board with 100kHz TWI */
  39. }
  40. void twi_disable(void)
  41. {
  42. // deactivate internal pullups for twi.
  43. digitalWrite(SDA, 0);
  44. digitalWrite(SCL, 0);
  45. }
  46. uint8_t twi_waitfor(uint8_t status)
  47. {
  48. while(!(TWCR & _BV(TWINT)));
  49. return (TW_STATUS != status);
  50. }
  51. uint8_t twi_rw8(uint8_t address, uint8_t mode, uint8_t* data)
  52. {
  53. // send start condition
  54. TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWSTA);
  55. if(twi_waitfor(TW_START))
  56. return 1;
  57. // send address
  58. TWDR = mode;
  59. TWDR |= (address << 1);
  60. TWCR = _BV(TWEN) | _BV(TWINT);
  61. if(twi_waitfor(mode == TW_READ? TW_MR_SLA_ACK: TW_MT_SLA_ACK))
  62. return 2;
  63. // send or receive data
  64. if(mode == TW_WRITE)
  65. TWDR = *data;
  66. TWCR = _BV(TWEN) | _BV(TWINT);
  67. if(twi_waitfor(mode == TW_READ? TW_MR_DATA_NACK: TW_MT_DATA_ACK))
  68. return 3;
  69. if(mode == TW_READ)
  70. *data = TWDR;
  71. // send stop
  72. TWCR = _BV(TWEN) | _BV(TWINT) | _BV(TWSTO);
  73. return 0;
  74. }