New I2C devices
[owSlave2.git] / common / I2C / USI_TWI_Master.h
1 /*****************************************************************************\r
2 *\r
3 * Atmel Corporation\r
4 *\r
5 * File              : USI_TWI_Master.h\r
6 * Compiler          : AVRGCC Toolchain version 3.4.2\r
7 * Revision          : $Revision: 992 $\r
8 * Date              : $Date: 2013-11-07 $\r
9 * Updated by        : $Author: Atmel $\r
10 *\r
11 * Support mail      : avr@atmel.com\r
12 *\r
13 * Supported devices : All device with USI module can be used.\r
14 *                     The example is written for the ATmega169, ATtiny26 and ATtiny2313\r
15 *\r
16 * AppNote           : AVR310 - Using the USI module as a TWI Master\r
17 *\r
18 * Description       : This is an implementation of an TWI master using\r
19 *                     the USI module as basis. The implementation assumes the AVR to\r
20 *                     be the only TWI master in the system and can therefore not be\r
21 *                     used in a multi-master system.\r
22 * Usage             : Initialize the USI module by calling the USI_TWI_Master_Initialise() \r
23 *                     function. Hence messages/data are transceived on the bus using\r
24 *                     the USI_TWI_Start_Transceiver_With_Data() function. If the transceiver\r
25 *                     returns with a fail, then use USI_TWI_Get_Status_Info to evaluate the \r
26 *                     couse of the failure.\r
27 *\r
28 ****************************************************************************/\r
29     #include<avr/io.h> \r
30 //********** Defines **********//\r
31 \r
32 // Defines controlling timing limits\r
33 #define TWI_FAST_MODE\r
34 #ifdef  __4MHZ__\r
35 #define SYS_CLK  4000.0  // [kHz]\r
36 #else\r
37 #define SYS_CLK   8000.0  // [kHz]\r
38 #endif\r
39 \r
40 \r
41 \r
42 #ifdef TWI_FAST_MODE               // TWI FAST mode timing limits. SCL = 100-400kHz\r
43   #define T2_TWI    ((SYS_CLK *1300) /1000000) +1 // >1,3us\r
44   #define T4_TWI    ((SYS_CLK * 600) /1000000) +1 // >0,6us\r
45   \r
46 #else                              // TWI STANDARD mode timing limits. SCL <= 100kHz\r
47   #define T2_TWI    ((SYS_CLK *4700) /1000000) +1 // >4,7us\r
48   #define T4_TWI    ((SYS_CLK *4000) /1000000) +1 // >4,0us\r
49 #endif\r
50 \r
51 // Defines controling code generating\r
52 //#define PARAM_VERIFICATION\r
53 //#define NOISE_TESTING\r
54 //#define SIGNAL_VERIFY\r
55 \r
56 //USI_TWI messages and flags and bit masks\r
57 //#define SUCCESS   7\r
58 //#define MSG       0\r
59 /****************************************************************************\r
60   Bit and byte definitions\r
61 ****************************************************************************/\r
62 #define TWI_READ_BIT  0       // Bit position for R/W bit in "address byte".\r
63 #define TWI_ADR_BITS  1       // Bit position for LSB of the slave address bits in the init byte.\r
64 #define TWI_NACK_BIT  0       // Bit position for (N)ACK bit.\r
65 \r
66 #define USI_TWI_NO_DATA             0x00  // Transmission buffer is empty\r
67 #define USI_TWI_DATA_OUT_OF_BOUND   0x01  // Transmission buffer is outside SRAM space\r
68 #define USI_TWI_UE_START_CON        0x02  // Unexpected Start Condition\r
69 #define USI_TWI_UE_STOP_CON         0x03  // Unexpected Stop Condition\r
70 #define USI_TWI_UE_DATA_COL         0x04  // Unexpected Data Collision (arbitration)\r
71 #define USI_TWI_NO_ACK_ON_DATA      0x05  // The slave did not acknowledge  all data\r
72 #define USI_TWI_NO_ACK_ON_ADDRESS   0x06  // The slave did not acknowledge  the address\r
73 #define USI_TWI_MISSING_START_CON   0x07  // Generated Start Condition not detected on bus\r
74 #define USI_TWI_MISSING_STOP_CON    0x08  // Generated Stop Condition not detected on bus\r
75 \r
76 // Device dependant defines\r
77 \r
78 #if defined(__AVR_AT90Mega169__) | defined(__AVR_ATmega169PA__) | \\r
79     defined(__AVR_AT90Mega165__) | defined(__AVR_ATmega165__) | \\r
80     defined(__AVR_ATmega325__) | defined(__AVR_ATmega3250__) | \\r
81     defined(__AVR_ATmega645__) | defined(__AVR_ATmega6450__) | \\r
82     defined(__AVR_ATmega329__) | defined(__AVR_ATmega3290__) | \\r
83     defined(__AVR_ATmega649__) | defined(__AVR_ATmega6490__)\r
84     #define DDR_USI             DDRE\r
85     #define PORT_USI            PORTE\r
86     #define PIN_USI             PINE\r
87     #define PORT_USI_SDA        PORTE5\r
88     #define PORT_USI_SCL        PORTE4\r
89     #define PIN_USI_SDA         PINE5\r
90     #define PIN_USI_SCL         PINE4\r
91 #endif\r
92 \r
93 #if defined(__AVR_ATtiny25__) | defined(__AVR_ATtiny45__) | defined(__AVR_ATtiny85__) | \\r
94     defined(__AVR_AT90Tiny26__) | defined(__AVR_ATtiny26__)\r
95     #define DDR_USI             DDRB\r
96     #define PORT_USI            PORTB\r
97     #define PIN_USI             PINB\r
98     #define PORT_USI_SDA        PORTB0\r
99     #define PORT_USI_SCL        PORTB2\r
100     #define PIN_USI_SDA         PINB0\r
101     #define PIN_USI_SCL         PINB2\r
102 #endif\r
103 \r
104 #if defined(__AVR_AT90Tiny2313__) | defined(__AVR_ATtiny2313__)\r
105     #define DDR_USI             DDRB\r
106     #define PORT_USI            PORTB\r
107     #define PIN_USI             PINB\r
108     #define PORT_USI_SDA        PORTB5\r
109     #define PORT_USI_SCL        PORTB7\r
110     #define PIN_USI_SDA         PINB5\r
111     #define PIN_USI_SCL         PINB7\r
112 #endif\r
113 \r
114 #if defined(__AVR_ATtiny84__) | defined(__AVR_ATtiny84A__)\r
115 #define DDR_USI             DDRA\r
116 #define PORT_USI            PORTA\r
117 #define PIN_USI             PINA\r
118 #define PORT_USI_SDA        PORTA6\r
119 #define PORT_USI_SCL        PORTA4\r
120 #define PIN_USI_SDA         PINA6\r
121 #define PIN_USI_SCL         PINA4\r
122 #endif\r
123 \r
124 // General defines\r
125 #define TRUE  1\r
126 #define FALSE 0\r
127 \r
128 #define ACK (1<<TWI_NACK_BIT )\r
129 #define NO_ACK 0\r
130 \r
131 \r
132 //********** Prototypes **********//\r
133 \r
134 void USI_TWI_Master_Initialise( void );\r
135 unsigned char USI_TWI_Start_Transceiver_With_Data( unsigned char * , unsigned char );\r
136 unsigned char USI_TWI_Get_State_Info( void );\r
137 \r
138 unsigned char I2c_WriteByte(unsigned char msg);\r
139 unsigned char I2c_ReadByte(unsigned char ack_mode);\r
140 void I2c_StartCondition(void);\r
141 void I2c_StopCondition(void);\r
142 \r