Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / i2c_charge_station / i2c_auto.c @ 1862

History | View | Annotate | Download (3.66 KB)

1 666 kwoo
/**
2
 * This is the API for i2c_autodetect used on the
3
 * charging station. This block explains the high
4
 * level implementation. The functions below explain
5
 * the low level.
6
 *
7
 ***** High Level *****
8
 * Since the charge stations are hooked up
9
 * via i2c lines, we must determine one master. We
10
 * decide that the one with the XBee is the master.
11
 * There should only be one Xbee in an i2c chain, though
12
 * in the case that there are two, the second one to detect
13
 * that they have an XBee must yeild the the older one.
14
 * All others are slaves. At startup, we check if we have an
15
 * XBee. If so, we poll for master, if there is none we declare
16
 * ourselves master. If you do not have an XBee, poll the line
17
 * asking for a master. If there is none, wait for a while and
18
 * then try again until the master assigns you an ID.
19
 * Once you have an ID, wait until the Master sends you commands.
20
 * Periodically check if you have an XBee, just in case.
21
 * The master must hand out IDs to the slaves. These IDs are
22
 * kept in a table that indicates the status of the station.
23
 * Occassionally poll the slaves to make sure they are not there and
24
 * update the table as needed. If a packet comes in over XBee,
25
 * forward to the correct charging station.
26
 * In the event that the master loses the XBee, broadcast a message
27
 * indicating that. Then poll for a new master. When one comes on,
28
 * pass it the table.
29
 **/
30
31
 /**
32
  * Sets up the needed states for i2c_autodetect
33
  */
34
 void i2c_startup (void) {
35
36
 }
37
38
 /**
39
  * Checks if an XBee is present.
40
  *
41
  * @return 0 if false, 1 if true
42
  */
43
 char i2c_has_xbee(void) {
44
45
 }
46
47
 /**
48
  * Acting as a temporary master, poll the master for an ID. If there
49
  * is a master, he should respond with an ID in which case you should
50
  * add that as your ID. Otherwise leave your ID blank.
51
  *
52
  * @return your ID, otherwise 0x00
53
  */
54
char i2c_request_id(void) {
55
56
}
57
58
/**
59
 * This should be called on the reception of an ID request. Since this
60
 * will be called during an interrupt, make it as fast as possible.
61
 * Check for the next available ID in the allocation table ans assign it.
62
 * Send the ID back on the bus.
63
 **/
64
void i2c_allocate_id(void) {
65
66
}
67
68
/**
69
 * This is your handler function to be called by the i2c interrupt. You
70
 * should get only 1 byte at a time and it will not be buffered. Handle it
71
 * fast. You should analyze if the packet is a ID request, a poll, a transfer
72
 * ID table, or data and then call the correct function. Basically this is a
73
 * giant switch.
74
 *
75
 * @param data Data byte received from master
76
 */
77
void i2c_receive_data(char data) {
78
79
}
80
81
/**
82
 * Called if a poll request has been received. Since this is in an interrupt make it
83
 * fast. Queues up a poll response message to the master.
84
 */
85
void i2c_poll_respond(void) {
86
87
}
88
89
/**
90
 * This should be done periodically. The allocation table should have a time since
91
 * last poll field which counts down along with a poll send bit.
92
 * If time reaches 0 after a  poll remove it. This function, when
93
 * called with a NULL should run through the table and send out request to all
94
 * those who are not currently being polled and check if those who have polls
95
 * have reached 0 and remove them. If called with an ID, set the poll status
96
 * bit for that ID to 0.
97
 *
98
 * @param ID ID of the slave who has responded. If NULL poll all those not being polled
99
 */
100
void i2c_poll_slaves_send(char ID) {
101
102
}
103
104
/**
105
 * Call this if you have lost the XBee and a new master has arrived. You should
106
 * transfer the entire table over to the new master.
107
 */
108
void i2c_transfer_table(void) {
109
110
}
111
112
/**
113
 * Call this if you receive a transfer table request. You should accept it and
114
 * merge it into your table.
115
 */
116
 void i2c_receive_table(void) {
117
118
 }
119