Revision 891
Moved comments to headers. That was painful.
i2c.c | ||
---|---|---|
43 | 43 |
#include "i2c.h" |
44 | 44 |
#include "ring_buffer.h" |
45 | 45 |
|
46 |
/** |
|
47 |
* @defgroup i2c I2C |
|
48 |
* |
|
49 |
* @brief Provides Inter-Interconnected-Communications (I2C) |
|
50 |
* |
|
51 |
* Initiates I2C functions on an ATMega128 which has a fully hardware Two Wire |
|
52 |
* Interface (TWI) module. Any Atmel chip with this hardware should be able to |
|
53 |
* use the software. |
|
54 |
* |
|
55 |
* This code will operate in a multi-master enviornment and can be either a |
|
56 |
* slave or a master at any time (as long as they are not one or the other at |
|
57 |
* the moment. You can queue up multiple transmission modes in the buffer up to |
|
58 |
* the buffer size. The buffer is implemented as a ring buffer. |
|
59 |
* |
|
60 |
* It is implemented using callback functions. Whenever you want to send a packet |
|
61 |
* you can call the built in send function (as a master) and it will send an array |
|
62 |
* of bytes. Master recieve and slave send/receive are all handled by the call back |
|
63 |
* functions. It is up to the end user to create functions that will handle the |
|
64 |
* receiving of packets. Their functions will be called with every byte recieved |
|
65 |
* so you must either buffer the inputs or handle each one separately. |
|
66 |
* |
|
67 |
* On errors we will simply flush the entire buffer. |
|
68 |
* |
|
69 |
* For information on how I2C operates, read the wikipedia article |
|
70 |
* http://en.wikipedia.org/wiki/I2c |
|
71 |
* for a good explanation of how it works. |
|
72 |
* @{ |
|
73 |
*/ |
|
74 | 46 |
|
75 | 47 |
/** |
76 | 48 |
* @brief Set bit rate 12 = 100kbit/s (max speed setting is 10 for an |
... | ... | |
87 | 59 |
RING_BUFFER_NEW(i2c_buffer, 128, char, i2c_write_buff, i2c_addr_buff); |
88 | 60 |
|
89 | 61 |
|
90 |
/** |
|
91 |
* @brief Initializes the i2c module. |
|
92 |
* |
|
93 |
* Initializes the I2C module to start listening on the i2c lines. If the callback functions |
|
94 |
* are not set to null they will be called when that transmission mode is called. The address |
|
95 |
* is your address that you will listen to when you are not the master. |
|
96 |
* |
|
97 |
* @param addr Your address on the I2C bus. |
|
98 |
* @param master_recv The address of the function to call when you receive a byte when you are a |
|
99 |
* master. |
|
100 |
* @param slave_recv The address of the function to call when you are a slave you receive data |
|
101 |
* from the master |
|
102 |
* @param slave_send The address of the function to call when you are a slave and the master |
|
103 |
* requests data from you. |
|
104 |
* |
|
105 |
* @return 0 for success, nonzero for failure |
|
106 |
**/ |
|
107 | 62 |
int i2c_init(char addr, fun_mrecv_t master_recv, fun_srecv_t slave_recv, fun_send_t slave_send) { |
108 | 63 |
master_recv_function = master_recv; |
109 | 64 |
slave_recv_function = slave_recv; |
... | ... | |
125 | 80 |
return 0; |
126 | 81 |
} |
127 | 82 |
|
128 |
/** |
|
129 |
* @brief Sends a byte array over I2C as a master |
|
130 |
* |
|
131 |
* Will perform a send over I2C to the destination from data for the ammount of |
|
132 |
* bytes that bytes is. |
|
133 |
* |
|
134 |
* @param dest Destination address of the data on the I2C bus. |
|
135 |
* @param data The pointer to the byte array of data |
|
136 |
* @param bytes The amount of bytes long that the byte array is. This is how |
|
137 |
* many bytes from the array that the function will send. |
|
138 |
* |
|
139 |
* @return zero for success, nonzero for failure |
|
140 |
**/ |
|
141 | 83 |
int i2c_send(char dest, char *data, size_t bytes) { |
142 | 84 |
int i; |
143 | 85 |
|
... | ... | |
167 | 109 |
return 0; |
168 | 110 |
} |
169 | 111 |
|
170 |
/** |
|
171 |
* @brief Send a master request to the destination |
|
172 |
* |
|
173 |
* Sends a request of data from the target address and calls |
|
174 |
* the callback function to handle data as it comes in. This function will |
|
175 |
* not work if the slave has not informationt to send or has nothing implemented |
|
176 |
* to send it. |
|
177 |
* |
|
178 |
* @param dest The destination that we want to receive information from. |
|
179 |
* |
|
180 |
* @return 0 for success, nonzero for failure |
|
181 |
**/ |
|
182 | 112 |
int i2c_request(char dest) { |
183 | 113 |
if(RING_BUFFER_FULL(i2c_write_buff)) |
184 | 114 |
return -1; |
Also available in: Unified diff