Adafruit TinyWireM Library
USI_TWI_Master.h
Go to the documentation of this file.
1 
4 /*****************************************************************************
5  *
6  *
7  * File USI_TWI_Master.h compiled with gcc
8  * Date Friday, 10/31/08 Boo!
9  * Updated by jkl
10  *
11  *
12  * Supported devices : All device with USI module can be used.
13  * The example is written for the ATtiny2313
14  *
15  * AppNote : AVR310 - Using the USI module as a TWI Master
16  *
17  * This is modified to just do I2C communication on ATtiny2313 running at
18  * 1MHz. Fast mode is probably possible, but would need a faster clock
19  * and has not been tested.
20  *
21  * 12/15/08 Added declaration of USI_TWI_Start_Memory_Read -jkl
22  ****************************************************************************/
23 
24 //********** Defines **********//
25 
26 // Defines controlling timing limits - SCL <= 100KHz.
27 
28 // For use with _delay_us()
29 #define T2_TWI 5
30 #define T4_TWI 4
31 
32 // Defines error code generating
33 //#define PARAM_VERIFICATION
34 //#define NOISE_TESTING
35 #define SIGNAL_VERIFY
36 
38 /****************************************************************************
39  Bit and byte definitions
40 ****************************************************************************/
41 #define TWI_READ_BIT 0
42 #define TWI_ADR_BITS \
43  1
44 #define TWI_NACK_BIT 0
45 
46 // Note these have been renumbered from the Atmel Apps Note. Most likely errors
47 // are now
48 // lowest numbers so they're easily recognized as LED flashes.
49 #define USI_TWI_NO_DATA 0x08
50 #define USI_TWI_DATA_OUT_OF_BOUND \
51  0x09
52 #define USI_TWI_UE_START_CON 0x07
53 #define USI_TWI_UE_STOP_CON 0x06
54 #define USI_TWI_UE_DATA_COL 0x05
55 #define USI_TWI_NO_ACK_ON_DATA 0x02
56 #define USI_TWI_NO_ACK_ON_ADDRESS \
57  0x01
58 #define USI_TWI_MISSING_START_CON \
59  0x03
60 #define USI_TWI_MISSING_STOP_CON \
61  0x04
62 #define USI_TWI_BAD_MEM_READ 0x0A
63 
64 // Device dependant defines ADDED BACK IN FROM ORIGINAL ATMEL .H
65 
66 #if defined(__AVR_AT90Mega169__) | defined(__AVR_ATmega169__) | \
67  defined(__AVR_AT90Mega165__) | defined(__AVR_ATmega165__) | \
68  defined(__AVR_ATmega325__) | defined(__AVR_ATmega3250__) | \
69  defined(__AVR_ATmega645__) | defined(__AVR_ATmega6450__) | \
70  defined(__AVR_ATmega329__) | defined(__AVR_ATmega3290__) | \
71  defined(__AVR_ATmega649__) | defined(__AVR_ATmega6490__)
72 #define DDR_USI DDRE
73 #define PORT_USI PORTE
74 #define PIN_USI PINE
75 #define PORT_USI_SDA PORTE5
76 #define PORT_USI_SCL PORTE4
77 #define PIN_USI_SDA PINE5
78 #define PIN_USI_SCL PINE4
79 #endif
80 
81 #if defined(__AVR_ATtiny25__) | defined(__AVR_ATtiny45__) | \
82  defined(__AVR_ATtiny85__) | defined(__AVR_AT90Tiny26__) | \
83  defined(__AVR_ATtiny26__)
84 #define DDR_USI DDRB
85 #define PORT_USI PORTB
86 #define PIN_USI PINB
87 #define PORT_USI_SDA PORTB0
88 #define PORT_USI_SCL PORTB2
89 #define PIN_USI_SDA PINB0
90 #define PIN_USI_SCL PINB2
91 #endif
92 
93 #if defined(__AVR_ATtiny84__) | defined(__AVR_ATtiny44__)
94 #define DDR_USI DDRA
95 #define PORT_USI PORTA
96 #define PIN_USI PINA
97 #define PORT_USI_SDA PORTA6
98 #define PORT_USI_SCL PORTA4
99 #define PIN_USI_SDA PINA6
100 #define PIN_USI_SCL PINA4
101 #endif
102 
103 #if defined(__AVR_AT90Tiny2313__) | defined(__AVR_ATtiny2313__)
104 #define DDR_USI DDRB
105 #define PORT_USI PORTB
106 #define PIN_USI PINB
107 #define PORT_USI_SDA PORTB5
108 #define PORT_USI_SCL PORTB7
109 #define PIN_USI_SDA PINB5
110 #define PIN_USI_SCL PINB7
111 #endif
112 
113 /* From the original .h
114 // Device dependant defines - These for ATtiny2313. // CHANGED FOR ATtiny85
115 
116  #define DDR_USI DDRB
117  #define PORT_USI PORTB
118  #define PIN_USI PINB
119  #define PORT_USI_SDA PORTB0 // was PORTB5 - N/U
120  #define PORT_USI_SCL PORTB2 // was PORTB7 - N/U
121  #define PIN_USI_SDA PINB0 // was PINB5
122  #define PIN_USI_SCL PINB2 // was PINB7
123 */
124 
125 // General defines
126 #define TRUE 1
127 #define FALSE 0
128 
129 //********** Prototypes **********//
130 
131 void USI_TWI_Master_Initialise(void);
132 unsigned char USI_TWI_Start_Random_Read(unsigned char *, unsigned char);
133 unsigned char USI_TWI_Start_Read_Write(unsigned char *, unsigned char);
134 unsigned char USI_TWI_Master_Stop(void);
135 unsigned char USI_TWI_Get_State_Info(void);
unsigned char USI_TWI_Start_Random_Read(unsigned char *, unsigned char)
USI Random (memory) Read function. This function sets up for call to USI_TWI_Start_Transceiver_With_D...
Definition: USI_TWI_Master.cpp:107
unsigned char USI_TWI_Get_State_Info(void)
Use this function to get hold of the error message from the last transmission.
Definition: USI_TWI_Master.cpp:90
void USI_TWI_Master_Initialise(void)
USI TWI single master initialization function.
Definition: USI_TWI_Master.cpp:63
unsigned char USI_TWI_Master_Stop(void)
Function for generating a TWI Stop Condition. Used to release the TWI bus.
Definition: USI_TWI_Master.cpp:348
unsigned char USI_TWI_Start_Read_Write(unsigned char *, unsigned char)
USI Normal Read / Write Function Transmit and receive function. LSB of first byte in buffer indicates...
Definition: USI_TWI_Master.cpp:130