Real Time Clock Module with I2C Interface  0.3
⇒ back to Project Page
 All Data Structures Files Functions Variables Typedefs Macros Pages
i2c.h
Go to the documentation of this file.
1 /****************************************************************************/
40 #ifndef I2C_H_INCLUDED
41 #define I2C_H_INCLUDED
42 
43 
44 
45 /******************************************
46  \internal
47  General definitions
48 *******************************************/
49 
50 //operation mode
51 #define I2C_SLAVE_INTERRUPT_OPERATION 1
52 
53 //Bus I/O
54 #define I2C_WAIT_BEFORE_STA_US 4
55 
56 #define I2C_MAX_RETRYS 3
57 #define I2C_RETRY_WAIT_US 30
58 
59 #define I2C_MAX_STA_WAIT_CYCLES 250
60 #define I2C_STA_RETRY_WAIT_US 5
61 
62 #define I2C_MAX_TIMEOUT_WAIT_CYCLES 250
63 #define I2C_TIMEOUT_RETRY_WAIT_US 1
64 
65 #define I2C_RESET_BUS_ON_ERROR 1
66 
67 
68 
69 //buffers
70 #define I2C_MASTER_BUFFER_LENGTH 10
71 #define I2C_SLAVE_BUFFER_LENGTH 7
72 
73 
74 //SCL Frequency
75 #define I2C_SCL_FREQ 100000
76 
77 //Slave Address
78 #define I2C_SLAVE_ADDRESS 0xaa
79 
80 //maximum values for Prescaler
81 #define I2C_MAX_PRESCALER_1 255
82 #define I2C_MAX_PRESCALER_4 1020
83 #define I2C_MAX_PRESCALER_16 4080
84 #define I2C_MAX_PRESCALER_64 16320
85 
86 //return values
87 #define I2C_SCL_SET_SUCC 0
88 #define I2C_SCL_ERR 1
89 
90 
91 #define I2C_IO_SUCC 0xd0
92 #define I2C_IO_NUM_ERROR 0xd1
93 #define I2C_IO_BLOCK_ERROR 0xd2
94 #define I2C_STA_ERROR 0xd3
95 #define I2C_TIMEOUT_ERROR 0xd4
96 #define I2C_IO_BUS_ERROR 0xd5
97 #define I2C_IO_ERROR 0xd6
98 
99 //I2C Return Codes (Master Transmission/Receive Mode)
100 #define I2C_START_COND_TRANS 0x08
101 #define I2C_RSTART_COND_TRANS 0x10
102 #define I2C_SLAW_TRANS 0x18
103 #define I2C_SLAR_TRANS 0x40
104 #define I2C_M_NOACK_SLAW 0x20
105 #define I2C_M_NOACK_SLAR 0x48
106 #define I2C_DATA_TRANS 0x28
107 #define I2C_DATA_RCV_ACK 0x50
108 #define I2C_DATA_RCV_NACK 0x58
109 #define I2C_M_NOACK_DATA_SEND 0x30
110 #define I2C_M_ARB_LOST 0x38
111 
112 #define I2C_BUS_ERROR 0x00
113 
114 //I2C Return Codes (Slave Transmission/Receive Mode)
115 #define I2C_SLAW_ACK_RCVD 0x60
116 #define I2C_SLAW_ACK_ARB_LOST 0x68
117 #define I2C_GCA_ACK_RCVD 0x70
118 #define I2C_GCA_ACK_ARB_LOST 0x78
119 #define I2C_DATA_ACK_RCVD 0x80
120 #define I2C_DATA_NOACK_RCVD 0x88
121 #define I2C_GCA_DATA_ACK_RCVD 0x90
122 #define I2C_GCA_DATA_NOACK_RCVD 0x98
123 #define I2C_STO_REPSTART_COND 0xa0
124 
125 #define I2C_SLAR_ACK_RCVD 0xa8
126 #define I2C_SLAR_ACK_ARB_LOST 0xb0
127 #define I2C_DATA_ACK_TRANS 0xb8
128 #define I2C_DATA_NOACK_TRANS 0xc0
129 #define I2C_LAST_DATA_ACK_TRANS 0xc8
130 
131 #define I2C_REGISTER_ERROR_VALUE 0xff
132 
133  //commands for slave mode
134 #define I2C_SLAVE_MODE_CONTINUE 0
135 #define I2C_SLAVE_MODE_END 1
136 #define I2C_SLAVE_MODE_ABORT 2
137 
138 
139 
140 
141 /******************************************
142  \internal
143  Function prototypes
144 *******************************************/
145 
146 void i2c_pin_init(void); //initializes SDA and SCL pins
147 uint8_t i2c_set_scl(uint32_t, uint32_t); //sets SCL frequency
148 uint8_t i2c_master_data_io(uint8_t*, uint8_t, uint8_t); //sends or receives data master <--> slave
149 #if I2C_SLAVE_INTERRUPT_OPERATION == 1
150 uint8_t i2c_slave_data_io(volatile uint8_t*, volatile uint8_t*, volatile uint8_t); //sends or receives data slave <--> master
151 #else
152 uint8_t i2c_slave_data_io(uint8_t*, uint8_t*, uint8_t); //sends or receives data slave <--> master
153 #endif
154 
155 
156 #endif // I2C_H_INCLUDED