Project

General

Profile

Statistics
| Revision:

root / branches / simulator / lib / include / libdragonfly / i2c.h @ 1073

History | View | Annotate | Download (4.67 KB)

1 241 bcoltin
/**
2
 * Copyright (c) 2007 Colony Project
3
 *
4
 * Permission is hereby granted, free of charge, to any person
5
 * obtaining a copy of this software and associated documentation
6
 * files (the "Software"), to deal in the Software without
7
 * restriction, including without limitation the rights to use,
8
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following
11
 * conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be
14
 * included in all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
 * OTHER DEALINGS IN THE SOFTWARE.
24
 **/
25
26
27 87 bcoltin
/** @file i2c.h
28
 *  @brief Header file for I2C
29
 *
30 241 bcoltin
 *  Contains functions for I2C.
31
 *
32
 *  @author Kevin Woo and Suresh Nidhiry, Colony Project, CMU Robotics Club
33
 **/
34 87 bcoltin
35 893 bcoltin
/**
36
 * @defgroup i2c I2C
37
 *
38
 * @brief Provides Inter-Interconnected-Communications (I2C)
39
 *
40
 * Initiates I2C functions on an ATMega128 which has a fully hardware Two Wire
41
 * Interface (TWI) module. Any Atmel chip with this hardware should be able to
42
 * use the software.
43
 *
44
 * This code will operate in a multi-master enviornment and can be either a
45
 * slave or a master at any time (as long as they are not one or the other at
46
 * the moment. You can queue up multiple transmission modes in the buffer up to
47
 * the buffer size. The buffer is implemented as a ring buffer.
48
 *
49
 * It is implemented using callback functions. Whenever you want to send a packet
50
 * you can call the built in send function (as a master) and it will send an array
51
 * of bytes. Master recieve and slave send/receive are all handled by the call back
52
 * functions. It is up to the end user to create functions that will handle the
53
 * receiving of packets. Their functions will be called with every byte recieved
54
 * so you must either buffer the inputs or handle each one separately.
55
 *
56
 * On errors we will simply flush the entire buffer.
57
 *
58
 * For information on how I2C operates, read the wikipedia article
59
 * http://en.wikipedia.org/wiki/I2c
60
 * for a good explanation of how it works.
61
 * @{
62
 */
63 87 bcoltin
64
#ifndef _I2C_H_
65
#define _I2C_H_
66
67
#include <stddef.h>
68
69 241 bcoltin
/** @brief Address of slave receive handler function **/
70 87 bcoltin
typedef void (*fun_srecv_t)(char);
71
72 241 bcoltin
/** @brief Address of master receive handler function**/
73 87 bcoltin
typedef int (*fun_mrecv_t)(char);
74
75 241 bcoltin
/** @brief Address of slave send handler function**/
76 87 bcoltin
typedef char (*fun_send_t)(void);
77
78 893 bcoltin
/**
79
 * @brief Initializes the i2c module.
80
 *
81
 * Initializes the I2C module to start listening on the i2c lines. If the callback functions
82
 * are not set to null they will be called when that transmission mode is called. The address
83
 * is your address that you will listen to when you are not the master.
84
 *
85
 * @param addr                         Your address on the I2C bus.
86
 * @param master_recv         The address of the function to call when you receive a byte when you are a
87
 *                            master.
88
 * @param slave_recv         The address of the function to call when you are a slave you receive data
89
 *                                                                from the master
90
 * @param slave_send                The address of the function to call when you are a slave and the master
91
 *                                                                requests data from you.
92
 *
93
 * @return 0 for success, nonzero for failure
94
 **/
95 87 bcoltin
int i2c_init(char addr, fun_mrecv_t master_recv, fun_srecv_t slave_recv, fun_send_t slave_send);
96 893 bcoltin
97
/**
98
 * @brief Sends a byte array over I2C as a master
99
 *
100
 * Will perform a send over I2C to the destination from data for the ammount of
101
 * bytes that bytes is.
102
 *
103
 * @param dest                Destination address of the data on the I2C bus.
104
 * @param data         The pointer to the byte array of data
105
 * @param bytes        The amount of bytes long that the byte array is. This is how
106
 *                                                many bytes from the array that the function will send.
107
 *
108
 * @return zero for success, nonzero for failure
109
 **/
110 87 bcoltin
int i2c_send(char dest, char* data, size_t bytes);
111 893 bcoltin
112
/**
113
 * @brief Send a master request to the destination
114
 *
115
 * Sends a request of data from the target address and calls
116
 * the callback function to handle data as it comes in. This function will
117
 * not work if the slave has not informationt to send or has nothing implemented
118
 * to send it.
119
 *
120
 * @param dest                The destination that we want to receive information from.
121
 *
122
 * @return 0 for success, nonzero for failure
123
 **/
124 87 bcoltin
int i2c_request(char dest);
125
126
#endif