twimaster.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*************************************************************************
  2. * Title: I2C master library using hardware TWI interface
  3. * Author: Peter Fleury <pfleury@gmx.ch> http://jump.to/fleury
  4. * File: $Id: twimaster.c,v 1.3 2005/07/02 11:14:21 Peter Exp $
  5. * Software: AVR-GCC 3.4.3 / avr-libc 1.2.3
  6. * Target: any AVR device with hardware TWI
  7. * Usage: API compatible with I2C Software Library i2cmaster.h
  8. **************************************************************************/
  9. #include <inttypes.h>
  10. #include <compat/twi.h>
  11. #include <i2cmaster.h>
  12. /* define CPU frequency in Hz here if not defined in Makefile */
  13. #ifndef F_CPU
  14. #define F_CPU 16000000UL
  15. #endif
  16. /* I2C clock in Hz */
  17. #define SCL_CLOCK 400000L
  18. /*************************************************************************
  19. Initialization of the I2C bus interface. Need to be called only once
  20. *************************************************************************/
  21. void i2c_init(void)
  22. {
  23. /* initialize TWI clock
  24. * minimal values in Bit Rate Register (TWBR) and minimal Prescaler
  25. * bits in the TWI Status Register should give us maximal possible
  26. * I2C bus speed - about 444 kHz
  27. *
  28. * for more details, see 20.5.2 in ATmega16/32 secification
  29. */
  30. TWSR = 0; /* no prescaler */
  31. TWBR = 10; /* must be >= 10 for stable operation */
  32. }/* i2c_init */
  33. /*************************************************************************
  34. Issues a start condition and sends address and transfer direction.
  35. return 0 = device accessible, 1= failed to access device
  36. *************************************************************************/
  37. unsigned char i2c_start(unsigned char address)
  38. {
  39. uint8_t twst;
  40. /* send START condition */
  41. TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
  42. /* wait until transmission completed */
  43. while(!(TWCR & (1<<TWINT)));
  44. /* check value of TWI Status Register. Mask prescaler bits. */
  45. twst = TW_STATUS & 0xF8;
  46. if ( (twst != TW_START) && (twst != TW_REP_START)) return 1;
  47. /* send device address */
  48. TWDR = address;
  49. TWCR = (1<<TWINT) | (1<<TWEN);
  50. /* wail until transmission completed and ACK/NACK has been received */
  51. while(!(TWCR & (1<<TWINT)));
  52. /* check value of TWI Status Register. Mask prescaler bits. */
  53. twst = TW_STATUS & 0xF8;
  54. if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) return 1;
  55. return 0;
  56. }/* i2c_start */
  57. /*************************************************************************
  58. Issues a start condition and sends address and transfer direction.
  59. If device is busy, use ack polling to wait until device is ready
  60. Input: address and transfer direction of I2C device
  61. *************************************************************************/
  62. void i2c_start_wait(unsigned char address)
  63. {
  64. uint8_t twst;
  65. while ( 1 )
  66. {
  67. /* send START condition */
  68. TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
  69. /* wait until transmission completed */
  70. while(!(TWCR & (1<<TWINT)));
  71. /* check value of TWI Status Register. Mask prescaler bits. */
  72. twst = TW_STATUS & 0xF8;
  73. if ( (twst != TW_START) && (twst != TW_REP_START)) continue;
  74. /* send device address */
  75. TWDR = address;
  76. TWCR = (1<<TWINT) | (1<<TWEN);
  77. /* wail until transmission completed */
  78. while(!(TWCR & (1<<TWINT)));
  79. /* check value of TWI Status Register. Mask prescaler bits. */
  80. twst = TW_STATUS & 0xF8;
  81. if ( (twst == TW_MT_SLA_NACK )||(twst ==TW_MR_DATA_NACK) )
  82. {
  83. /* device busy, send stop condition to terminate write operation */
  84. TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
  85. /* wait until stop condition is executed and bus released */
  86. while(TWCR & (1<<TWSTO));
  87. continue;
  88. }
  89. break;
  90. }
  91. }/* i2c_start_wait */
  92. /*************************************************************************
  93. Issues a repeated start condition and sends address and transfer direction
  94. Input: address and transfer direction of I2C device
  95. Return: 0 device accessible
  96. 1 failed to access device
  97. *************************************************************************/
  98. unsigned char i2c_rep_start(unsigned char address)
  99. {
  100. return i2c_start( address );
  101. }/* i2c_rep_start */
  102. /*************************************************************************
  103. Terminates the data transfer and releases the I2C bus
  104. *************************************************************************/
  105. void i2c_stop(void)
  106. {
  107. /* send stop condition */
  108. TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
  109. /* wait until stop condition is executed and bus released */
  110. while(TWCR & (1<<TWSTO));
  111. }/* i2c_stop */
  112. /*************************************************************************
  113. Send one byte to I2C device
  114. Input: byte to be transfered
  115. Return: 0 write successful
  116. 1 write failed
  117. *************************************************************************/
  118. unsigned char i2c_write( unsigned char data )
  119. {
  120. uint8_t twst;
  121. /* send data to the previously addressed device */
  122. TWDR = data;
  123. TWCR = (1<<TWINT) | (1<<TWEN);
  124. /* wait until transmission completed */
  125. while(!(TWCR & (1<<TWINT)));
  126. /* check value of TWI Status Register. Mask prescaler bits */
  127. twst = TW_STATUS & 0xF8;
  128. if( twst != TW_MT_DATA_ACK) return 1;
  129. return 0;
  130. }/* i2c_write */
  131. /*************************************************************************
  132. Read one byte from the I2C device, request more data from device
  133. Return: byte read from I2C device
  134. *************************************************************************/
  135. unsigned char i2c_readAck(void)
  136. {
  137. TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
  138. while(!(TWCR & (1<<TWINT)));
  139. return TWDR;
  140. }/* i2c_readAck */
  141. /*************************************************************************
  142. Read one byte from the I2C device, read is followed by a stop condition
  143. Return: byte read from I2C device
  144. *************************************************************************/
  145. unsigned char i2c_readNak(void)
  146. {
  147. TWCR = (1<<TWINT) | (1<<TWEN);
  148. while(!(TWCR & (1<<TWINT)));
  149. return TWDR;
  150. }/* i2c_readNak */