You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

twimaster.c 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*************************************************************************
  2. * Title: I2C master library using hardware TWI interface
  3. * Author: Peter Fleury <[email protected]> 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 <util/delay.h>
  12. #include <i2cmaster.h>
  13. #include "timer.h"
  14. #include "debug.h"
  15. /* define CPU frequency in Mhz here if not defined in Makefile */
  16. #ifndef F_CPU
  17. #define F_CPU 4000000UL
  18. #endif
  19. /* I2C clock in Hz */
  20. #define SCL_CLOCK 400000L
  21. #define SCL_DURATION (1000000L/SCL_CLOCK)/2
  22. volatile uint8_t i2c_force_stop = 0;
  23. #define TIMEOUT 3000
  24. #define CHECK_FORCE_STOP() if(i2c_force_stop){i2c_force_stop=0;break;}
  25. #define CHECK_TIMEOUT_PRE() \
  26. uint16_t start; \
  27. uint8_t once = 1;
  28. #define CHECK_TIMEOUT_PRE2() \
  29. once = 1;
  30. #define CHECK_TIMEOUT(retval) { \
  31. if (once) { \
  32. start = timer_read(); \
  33. once = 0; \
  34. } \
  35. else { \
  36. if (timer_elapsed(start) >= TIMEOUT) { \
  37. i2c_forceStop(); \
  38. return retval; \
  39. } \
  40. } \
  41. }
  42. static void i2c_forceStop(void);
  43. /*************************************************************************
  44. Initialization of the I2C bus interface. Need to be called only once
  45. *************************************************************************/
  46. void i2c_init(void)
  47. {
  48. /* initialize TWI clock: 100 kHz clock, TWPS = 0 => prescaler = 1 */
  49. TWSR = 0; /* no prescaler */
  50. TWBR = ((F_CPU/SCL_CLOCK)-16)/2; /* must be > 10 for stable operation */
  51. }/* i2c_init */
  52. /*************************************************************************
  53. Issues a start condition and sends address and transfer direction.
  54. return 0 = device accessible, 1= failed to access device
  55. *************************************************************************/
  56. unsigned char i2c_start(unsigned char address)
  57. {
  58. uint8_t twst;
  59. // send START condition
  60. TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
  61. // wait until transmission completed
  62. CHECK_TIMEOUT_PRE();
  63. while(!(TWCR & (1<<TWINT))) { CHECK_TIMEOUT(2); };
  64. // check value of TWI Status Register. Mask prescaler bits.
  65. twst = TW_STATUS & 0xF8;
  66. if ( (twst != TW_START) && (twst != TW_REP_START)) return 1;
  67. // send device address
  68. TWDR = address;
  69. TWCR = (1<<TWINT) | (1<<TWEN);
  70. // wail until transmission completed and ACK/NACK has been received
  71. CHECK_TIMEOUT_PRE2();
  72. while(!(TWCR & (1<<TWINT))) { CHECK_TIMEOUT(2); };
  73. // check value of TWI Status Register. Mask prescaler bits.
  74. twst = TW_STATUS & 0xF8;
  75. if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) ) return 1;
  76. return 0;
  77. }/* i2c_start */
  78. /*************************************************************************
  79. Issues a start condition and sends address and transfer direction.
  80. If device is busy, use ack polling to wait until device is ready
  81. Input: address and transfer direction of I2C device
  82. *************************************************************************/
  83. void i2c_start_wait(unsigned char address)
  84. {
  85. uint8_t twst;
  86. while ( 1 )
  87. {
  88. // send START condition
  89. TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
  90. // wait until transmission completed
  91. while(!(TWCR & (1<<TWINT)));
  92. // check value of TWI Status Register. Mask prescaler bits.
  93. twst = TW_STATUS & 0xF8;
  94. if ( (twst != TW_START) && (twst != TW_REP_START)) continue;
  95. // send device address
  96. TWDR = address;
  97. TWCR = (1<<TWINT) | (1<<TWEN);
  98. // wail until transmission completed
  99. while(!(TWCR & (1<<TWINT)));
  100. // check value of TWI Status Register. Mask prescaler bits.
  101. twst = TW_STATUS & 0xF8;
  102. if ( (twst == TW_MT_SLA_NACK )||(twst ==TW_MR_DATA_NACK) )
  103. {
  104. /* device busy, send stop condition to terminate write operation */
  105. TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
  106. // wait until stop condition is executed and bus released
  107. CHECK_TIMEOUT_PRE();
  108. while(TWCR & (1<<TWSTO)) { CHECK_TIMEOUT(); };
  109. continue;
  110. }
  111. //if( twst != TW_MT_SLA_ACK) return 1;
  112. break;
  113. }
  114. }/* i2c_start_wait */
  115. /*************************************************************************
  116. Issues a repeated start condition and sends address and transfer direction
  117. Input: address and transfer direction of I2C device
  118. Return: 0 device accessible
  119. 1 failed to access device
  120. *************************************************************************/
  121. unsigned char i2c_rep_start(unsigned char address)
  122. {
  123. return i2c_start( address );
  124. }/* i2c_rep_start */
  125. /*************************************************************************
  126. Terminates the data transfer and releases the I2C bus
  127. *************************************************************************/
  128. void i2c_stop(void)
  129. {
  130. /* send stop condition */
  131. TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
  132. // wait until stop condition is executed and bus released
  133. CHECK_TIMEOUT_PRE();
  134. while(TWCR & (1<<TWSTO)) { CHECK_TIMEOUT(); };
  135. }/* i2c_stop */
  136. /*************************************************************************
  137. Send one byte to I2C device
  138. Input: byte to be transfered
  139. Return: 0 write successful
  140. 1 write failed
  141. *************************************************************************/
  142. unsigned char i2c_write( unsigned char data )
  143. {
  144. uint8_t twst;
  145. // send data to the previously addressed device
  146. TWDR = data;
  147. TWCR = (1<<TWINT) | (1<<TWEN);
  148. // wait until transmission completed
  149. CHECK_TIMEOUT_PRE();
  150. while(!(TWCR & (1<<TWINT))) { CHECK_TIMEOUT(2) };
  151. // check value of TWI Status Register. Mask prescaler bits
  152. twst = TW_STATUS & 0xF8;
  153. if( twst != TW_MT_DATA_ACK) return 1;
  154. return 0;
  155. }/* i2c_write */
  156. /*************************************************************************
  157. Read one byte from the I2C device, request more data from device
  158. Return: byte read from I2C device
  159. *************************************************************************/
  160. unsigned char i2c_readAck(void)
  161. {
  162. TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
  163. CHECK_TIMEOUT_PRE();
  164. while(!(TWCR & (1<<TWINT))) { CHECK_TIMEOUT(2); };
  165. return TWDR;
  166. }/* i2c_readAck */
  167. /*************************************************************************
  168. Read one byte from the I2C device, read is followed by a stop condition
  169. Return: byte read from I2C device
  170. *************************************************************************/
  171. unsigned char i2c_readNak(void)
  172. {
  173. TWCR = (1<<TWINT) | (1<<TWEN);
  174. CHECK_TIMEOUT_PRE();
  175. while(!(TWCR & (1<<TWINT))) { CHECK_TIMEOUT(2); };
  176. return TWDR;
  177. }/* i2c_readNak */
  178. void i2c_forceStop(void)
  179. {
  180. xprintf("i2c timeout\n");
  181. /* let slave to release SDA */
  182. TWCR = 0;
  183. DDRD |= (1<<PD0);
  184. DDRD &= ~(1<<PD1);
  185. _delay_us(30);
  186. if ((PIND & (1<<PD1)) == 0) {
  187. for (uint8_t i = 0; i < 9; i++) {
  188. PORTD &= ~(1<<PD0);
  189. _delay_us(SCL_DURATION);
  190. PORTD |= (1<<PD0);
  191. _delay_us(SCL_DURATION);
  192. }
  193. }
  194. DDRD &= ~(1<<PD0);
  195. /* send stop condition */
  196. TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
  197. }