twi_master_sync.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "twi_master_sync.h"
  12. /* define CPU frequency in Mhz here if not defined in Makefile */
  13. //#ifndef F_CPU
  14. //#define F_CPU 4000000UL
  15. //#endif
  16. /* I2C clock in Hz */
  17. //#define SCL_CLOCK 100000L
  18. //#define SCL_CLOCK 50000L
  19. #define SCL_CLOCK TWI_SCL_HZ
  20. /*************************************************************************
  21. Initialization of the I2C bus interface. Need to be called only once
  22. *************************************************************************/
  23. void i2c_init(void)
  24. {
  25. /* initialize TWI clock: 100 kHz clock, TWPS = 0 => prescaler = 1 */
  26. TWSR = 0; /* no prescaler */
  27. TWBR = ((F_CPU/SCL_CLOCK)-16)/2; /* must be > 10 for stable operation */
  28. }/* i2c_init */
  29. /*************************************************************************
  30. Issues a start condition and sends address and transfer direction.
  31. return 0 = device accessible, 1= failed to access device
  32. *************************************************************************/
  33. unsigned char i2c_start(unsigned char address)
  34. {
  35. uint8_t twst;
  36. // send START condition
  37. TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
  38. // wait until transmission completed
  39. while(!(TWCR & (1<<TWINT)));
  40. // check value of TWI Status Register. Mask prescaler bits.
  41. twst = TW_STATUS & 0xF8;
  42. if ( (twst != TW_START) && (twst != TW_REP_START)) return 1;
  43. // send device address
  44. TWDR = address;
  45. TWCR = (1<<TWINT) | (1<<TWEN);
  46. // wail until transmission completed and ACK/NACK has been received
  47. while(!(TWCR & (1<<TWINT)));
  48. // check value of TWI Status Register. Mask prescaler bits.
  49. twst = TW_STATUS & 0xF8;
  50. if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) return 1;
  51. return 0;
  52. }/* i2c_start */
  53. /*************************************************************************
  54. Issues a start condition and sends address and transfer direction.
  55. If device is busy, use ack polling to wait until device is ready
  56. Input: address and transfer direction of I2C device
  57. *************************************************************************/
  58. void i2c_start_wait(unsigned char address)
  59. {
  60. uint8_t twst;
  61. while ( 1 )
  62. {
  63. // send START condition
  64. TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
  65. // wait until transmission completed
  66. while(!(TWCR & (1<<TWINT)));
  67. // check value of TWI Status Register. Mask prescaler bits.
  68. twst = TW_STATUS & 0xF8;
  69. if ( (twst != TW_START) && (twst != TW_REP_START)) continue;
  70. // send device address
  71. TWDR = address;
  72. TWCR = (1<<TWINT) | (1<<TWEN);
  73. // wail until transmission completed
  74. while(!(TWCR & (1<<TWINT)));
  75. // check value of TWI Status Register. Mask prescaler bits.
  76. twst = TW_STATUS & 0xF8;
  77. if ( (twst == TW_MT_SLA_NACK )||(twst ==TW_MR_DATA_NACK) )
  78. {
  79. /* device busy, send stop condition to terminate write operation */
  80. TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
  81. // wait until stop condition is executed and bus released
  82. while(TWCR & (1<<TWSTO));
  83. continue;
  84. }
  85. //if( twst != TW_MT_SLA_ACK) return 1;
  86. break;
  87. }
  88. }/* i2c_start_wait */
  89. /*************************************************************************
  90. Issues a repeated start condition and sends address and transfer direction
  91. Input: address and transfer direction of I2C device
  92. Return: 0 device accessible
  93. 1 failed to access device
  94. *************************************************************************/
  95. unsigned char i2c_rep_start(unsigned char address)
  96. {
  97. return i2c_start( address );
  98. }/* i2c_rep_start */
  99. /*************************************************************************
  100. Terminates the data transfer and releases the I2C bus
  101. *************************************************************************/
  102. void i2c_stop(void)
  103. {
  104. /* send stop condition */
  105. TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
  106. // wait until stop condition is executed and bus released
  107. while(TWCR & (1<<TWSTO));
  108. }/* i2c_stop */
  109. /*************************************************************************
  110. Send one byte to I2C device
  111. Input: byte to be transfered
  112. Return: 0 write successful
  113. 1 write failed
  114. *************************************************************************/
  115. unsigned char i2c_write( unsigned char data )
  116. {
  117. uint8_t twst;
  118. // send data to the previously addressed device
  119. TWDR = data;
  120. TWCR = (1<<TWINT) | (1<<TWEN);
  121. // wait until transmission completed
  122. while(!(TWCR & (1<<TWINT)));
  123. // check value of TWI Status Register. Mask prescaler bits
  124. twst = TW_STATUS & 0xF8;
  125. if( twst != TW_MT_DATA_ACK) return 1;
  126. return 0;
  127. }/* i2c_write */
  128. /*************************************************************************
  129. Read one byte from the I2C device, request more data from device
  130. Return: byte read from I2C device
  131. *************************************************************************/
  132. unsigned char i2c_readAck(void)
  133. {
  134. TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
  135. while(!(TWCR & (1<<TWINT)));
  136. return TWDR;
  137. }/* i2c_readAck */
  138. /*************************************************************************
  139. Read one byte from the I2C device, read is followed by a stop condition
  140. Return: byte read from I2C device
  141. *************************************************************************/
  142. unsigned char i2c_readNak(void)
  143. {
  144. TWCR = (1<<TWINT) | (1<<TWEN);
  145. while(!(TWCR & (1<<TWINT)));
  146. return TWDR;
  147. }/* i2c_readNak */