twimastertimeout.c 6.8 KB

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