USI_TWI_Master.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*****************************************************************************
  2. *
  3. *
  4. * File USI_TWI_Master.h compiled with gcc
  5. * Date Friday, 10/31/08 Boo!
  6. * Updated by jkl
  7. *
  8. *
  9. * Supported devices : All device with USI module can be used.
  10. * The example is written for the ATtiny2313
  11. *
  12. * AppNote : AVR310 - Using the USI module as a TWI Master
  13. *
  14. * This is modified to just do I2C communication on ATtiny2313 running at
  15. * 1MHz. Fast mode is probably possible, but would need a faster clock
  16. * and has not been tested.
  17. *
  18. * 12/15/08 Added declaration of USI_TWI_Start_Memory_Read -jkl
  19. ****************************************************************************/
  20. //********** Defines **********//
  21. // Defines controlling timing limits - SCL <= 100KHz.
  22. // For use with _delay_us()
  23. #define T2_TWI 5 // >4,7us
  24. #define T4_TWI 4 // >4,0us
  25. // Defines error code generating
  26. //#define PARAM_VERIFICATION
  27. //#define NOISE_TESTING
  28. #define SIGNAL_VERIFY // This should probably be on always.
  29. /****************************************************************************
  30. Bit and byte definitions
  31. ****************************************************************************/
  32. #define TWI_READ_BIT 0 // Bit position for R/W bit in "address byte".
  33. #define TWI_ADR_BITS 1 // Bit position for LSB of the slave address bits in the init byte.
  34. #define TWI_NACK_BIT 0 // Bit position for (N)ACK bit.
  35. // Note these have been renumbered from the Atmel Apps Note. Most likely errors are now
  36. // lowest numbers so they're easily recognized as LED flashes.
  37. #define USI_TWI_NO_DATA 0x08 // Transmission buffer is empty
  38. #define USI_TWI_DATA_OUT_OF_BOUND 0x09 // Transmission buffer is outside SRAM space
  39. #define USI_TWI_UE_START_CON 0x07 // Unexpected Start Condition
  40. #define USI_TWI_UE_STOP_CON 0x06 // Unexpected Stop Condition
  41. #define USI_TWI_UE_DATA_COL 0x05 // Unexpected Data Collision (arbitration)
  42. #define USI_TWI_NO_ACK_ON_DATA 0x02 // The slave did not acknowledge all data
  43. #define USI_TWI_NO_ACK_ON_ADDRESS 0x01 // The slave did not acknowledge the address
  44. #define USI_TWI_MISSING_START_CON 0x03 // Generated Start Condition not detected on bus
  45. #define USI_TWI_MISSING_STOP_CON 0x04 // Generated Stop Condition not detected on bus
  46. #define USI_TWI_BAD_MEM_READ 0x0A // Error during external memory read
  47. // Device dependant defines ADDED BACK IN FROM ORIGINAL ATMEL .H
  48. #if defined(__AVR_AT90Mega169__) | defined(__AVR_ATmega169__) | \
  49. defined(__AVR_AT90Mega165__) | defined(__AVR_ATmega165__) | \
  50. defined(__AVR_ATmega325__) | defined(__AVR_ATmega3250__) | \
  51. defined(__AVR_ATmega645__) | defined(__AVR_ATmega6450__) | \
  52. defined(__AVR_ATmega329__) | defined(__AVR_ATmega3290__) | \
  53. defined(__AVR_ATmega649__) | defined(__AVR_ATmega6490__)
  54. #define DDR_USI DDRE
  55. #define PORT_USI PORTE
  56. #define PIN_USI PINE
  57. #define PORT_USI_SDA PORTE5
  58. #define PORT_USI_SCL PORTE4
  59. #define PIN_USI_SDA PINE5
  60. #define PIN_USI_SCL PINE4
  61. #endif
  62. #if defined(__AVR_ATtiny25__) | defined(__AVR_ATtiny45__) | defined(__AVR_ATtiny85__) | \
  63. defined(__AVR_AT90Tiny26__) | defined(__AVR_ATtiny26__)
  64. #define DDR_USI DDRB
  65. #define PORT_USI PORTB
  66. #define PIN_USI PINB
  67. #define PORT_USI_SDA PORTB0
  68. #define PORT_USI_SCL PORTB2
  69. #define PIN_USI_SDA PINB0
  70. #define PIN_USI_SCL PINB2
  71. #endif
  72. #if defined(__AVR_ATtiny84__) | defined(__AVR_ATtiny44__)
  73. # define DDR_USI DDRA
  74. # define PORT_USI PORTA
  75. # define PIN_USI PINA
  76. # define PORT_USI_SDA PORTA6
  77. # define PORT_USI_SCL PORTA4
  78. # define PIN_USI_SDA PINA6
  79. # define PIN_USI_SCL PINA4
  80. #endif
  81. #if defined(__AVR_AT90Tiny2313__) | defined(__AVR_ATtiny2313__)
  82. #define DDR_USI DDRB
  83. #define PORT_USI PORTB
  84. #define PIN_USI PINB
  85. #define PORT_USI_SDA PORTB5
  86. #define PORT_USI_SCL PORTB7
  87. #define PIN_USI_SDA PINB5
  88. #define PIN_USI_SCL PINB7
  89. #endif
  90. /* From the original .h
  91. // Device dependant defines - These for ATtiny2313. // CHANGED FOR ATtiny85
  92. #define DDR_USI DDRB
  93. #define PORT_USI PORTB
  94. #define PIN_USI PINB
  95. #define PORT_USI_SDA PORTB0 // was PORTB5 - N/U
  96. #define PORT_USI_SCL PORTB2 // was PORTB7 - N/U
  97. #define PIN_USI_SDA PINB0 // was PINB5
  98. #define PIN_USI_SCL PINB2 // was PINB7
  99. */
  100. // General defines
  101. #define TRUE 1
  102. #define FALSE 0
  103. //********** Prototypes **********//
  104. void USI_TWI_Master_Initialise( void );
  105. unsigned char USI_TWI_Start_Random_Read( unsigned char * , unsigned char );
  106. unsigned char USI_TWI_Start_Read_Write( unsigned char * , unsigned char );
  107. unsigned char USI_TWI_Master_Stop( void );
  108. unsigned char USI_TWI_Get_State_Info( void );