twimaster.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 Mhz 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. //if( twst != TW_MT_SLA_ACK) return 1;
  90. break;
  91. }
  92. }/* i2c_start_wait */
  93. /*************************************************************************
  94. Issues a repeated start condition and sends address and transfer direction
  95. Input: address and transfer direction of I2C device
  96. Return: 0 device accessible
  97. 1 failed to access device
  98. *************************************************************************/
  99. unsigned char i2c_rep_start(unsigned char address)
  100. {
  101. return i2c_start( address );
  102. }/* i2c_rep_start */
  103. /*************************************************************************
  104. Terminates the data transfer and releases the I2C bus
  105. *************************************************************************/
  106. void i2c_stop(void)
  107. {
  108. /* send stop condition */
  109. TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
  110. // wait until stop condition is executed and bus released
  111. while(TWCR & (1<<TWSTO));
  112. }/* i2c_stop */
  113. /*************************************************************************
  114. Send one byte to I2C device
  115. Input: byte to be transfered
  116. Return: 0 write successful
  117. 1 write failed
  118. *************************************************************************/
  119. unsigned char i2c_write( unsigned char data )
  120. {
  121. uint8_t twst;
  122. // send data to the previously addressed device
  123. TWDR = data;
  124. TWCR = (1<<TWINT) | (1<<TWEN);
  125. // wait until transmission completed
  126. while(!(TWCR & (1<<TWINT)));
  127. // check value of TWI Status Register. Mask prescaler bits
  128. twst = TW_STATUS & 0xF8;
  129. if( twst != TW_MT_DATA_ACK) return 1;
  130. return 0;
  131. }/* i2c_write */
  132. /*************************************************************************
  133. Read one byte from the I2C device, request more data from device
  134. Return: byte read from I2C device
  135. *************************************************************************/
  136. unsigned char i2c_readAck(void)
  137. {
  138. TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
  139. while(!(TWCR & (1<<TWINT)));
  140. return TWDR;
  141. }/* i2c_readAck */
  142. /*************************************************************************
  143. Read one byte from the I2C device, read is followed by a stop condition
  144. Return: byte read from I2C device
  145. *************************************************************************/
  146. unsigned char i2c_readNak(void)
  147. {
  148. TWCR = (1<<TWINT) | (1<<TWEN);
  149. while(!(TWCR & (1<<TWINT)));
  150. return TWDR;
  151. }/* i2c_readNak */