i2cmaster.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #pragma once
  2. /*************************************************************************
  3. * Title: C include file for the I2C master interface
  4. * (i2cmaster.S or twimaster.c)
  5. * Author: Peter Fleury <pfleury@gmx.ch> http://jump.to/fleury
  6. * File: $Id: i2cmaster.h,v 1.10 2005/03/06 22:39:57 Peter Exp $
  7. * Software: AVR-GCC 3.4.3 / avr-libc 1.2.3
  8. * Target: any AVR device
  9. * Usage: see Doxygen manual
  10. **************************************************************************/
  11. #ifdef DOXYGEN
  12. /**
  13. @defgroup pfleury_ic2master I2C Master library
  14. @code #include <i2cmaster.h> @endcode
  15. @brief I2C (TWI) Master Software Library
  16. Basic routines for communicating with I2C slave devices. This single master
  17. implementation is limited to one bus master on the I2C bus.
  18. This I2c library is implemented as a compact assembler software implementation of the I2C protocol
  19. which runs on any AVR (i2cmaster.S) and as a TWI hardware interface for all AVR with built-in TWI hardware (twimaster.c).
  20. Since the API for these two implementations is exactly the same, an application can be linked either against the
  21. software I2C implementation or the hardware I2C implementation.
  22. Use 4.7k pull-up resistor on the SDA and SCL pin.
  23. Adapt the SCL and SDA port and pin definitions and eventually the delay routine in the module
  24. i2cmaster.S to your target when using the software I2C implementation !
  25. Adjust the CPU clock frequence F_CPU in twimaster.c or in the Makfile when using the TWI hardware implementaion.
  26. @note
  27. The module i2cmaster.S is based on the Atmel Application Note AVR300, corrected and adapted
  28. to GNU assembler and AVR-GCC C call interface.
  29. Replaced the incorrect quarter period delays found in AVR300 with
  30. half period delays.
  31. @author Peter Fleury pfleury@gmx.ch http://jump.to/fleury
  32. @par API Usage Example
  33. The following code shows typical usage of this library, see example test_i2cmaster.c
  34. @code
  35. #include <i2cmaster.h>
  36. #define Dev24C02 0xA2 // device address of EEPROM 24C02, see datasheet
  37. int main(void)
  38. {
  39. unsigned char ret;
  40. i2c_init(); // initialize I2C library
  41. // write 0x75 to EEPROM address 5 (Byte Write)
  42. i2c_start_wait(Dev24C02+I2C_WRITE); // set device address and write mode
  43. i2c_write(0x05); // write address = 5
  44. i2c_write(0x75); // write value 0x75 to EEPROM
  45. i2c_stop(); // set stop conditon = release bus
  46. // read previously written value back from EEPROM address 5
  47. i2c_start_wait(Dev24C02+I2C_WRITE); // set device address and write mode
  48. i2c_write(0x05); // write address = 5
  49. i2c_rep_start(Dev24C02+I2C_READ); // set device address and read mode
  50. ret = i2c_readNak(); // read one byte from EEPROM
  51. i2c_stop();
  52. for(;;);
  53. }
  54. @endcode
  55. */
  56. #endif /* DOXYGEN */
  57. /**@{*/
  58. #if (__GNUC__ * 100 + __GNUC_MINOR__) < 304
  59. #error "This library requires AVR-GCC 3.4 or later, update to newer AVR-GCC compiler !"
  60. #endif
  61. #include <avr/io.h>
  62. /** defines the data direction (reading from I2C device) in i2c_start(),i2c_rep_start() */
  63. #define I2C_READ 1
  64. /** defines the data direction (writing to I2C device) in i2c_start(),i2c_rep_start() */
  65. #define I2C_WRITE 0
  66. /**
  67. @brief initialize the I2C master interace. Need to be called only once
  68. @param void
  69. @return none
  70. */
  71. extern void i2c_init(void);
  72. /**
  73. @brief Terminates the data transfer and releases the I2C bus
  74. @param void
  75. @return none
  76. */
  77. extern void i2c_stop(void);
  78. /**
  79. @brief Issues a start condition and sends address and transfer direction
  80. @param addr address and transfer direction of I2C device
  81. @retval 0 device accessible
  82. @retval 1 failed to access device
  83. */
  84. extern unsigned char i2c_start(unsigned char addr);
  85. /**
  86. @brief Issues a repeated start condition and sends address and transfer direction
  87. @param addr address and transfer direction of I2C device
  88. @retval 0 device accessible
  89. @retval 1 failed to access device
  90. */
  91. extern unsigned char i2c_rep_start(unsigned char addr);
  92. /**
  93. @brief Issues a start condition and sends address and transfer direction
  94. If device is busy, use ack polling to wait until device ready
  95. @param addr address and transfer direction of I2C device
  96. @return none
  97. */
  98. extern void i2c_start_wait(unsigned char addr);
  99. /**
  100. @brief Send one byte to I2C device
  101. @param data byte to be transfered
  102. @retval 0 write successful
  103. @retval 1 write failed
  104. */
  105. extern unsigned char i2c_write(unsigned char data);
  106. /**
  107. @brief read one byte from the I2C device, request more data from device
  108. @return byte read from I2C device
  109. */
  110. extern unsigned char i2c_readAck(void);
  111. /**
  112. @brief read one byte from the I2C device, read is followed by a stop condition
  113. @return byte read from I2C device
  114. */
  115. extern unsigned char i2c_readNak(void);
  116. /**
  117. @brief read one byte from the I2C device
  118. Implemented as a macro, which calls either i2c_readAck or i2c_readNak
  119. @param ack 1 send ack, request more data from device<br>
  120. 0 send nak, read is followed by a stop condition
  121. @return byte read from I2C device
  122. */
  123. extern unsigned char i2c_read(unsigned char ack);
  124. #define i2c_read(ack) (ack) ? i2c_readAck() : i2c_readNak();
  125. /**@}*/