Revision 2011
Some fixes to forklift demo, and added most recent forklift code to warehouse project.
trunk/code/projects/forklift_demo/Forklift.cpp | ||
---|---|---|
1 |
#define USE_ALT_TWI // an alternative to libdragonfly's i2c code, in twi.cpp |
|
2 |
|
|
1 | 3 |
#include "Forklift.h" |
2 | 4 |
extern "C" |
3 | 5 |
{ |
4 |
#include "i2c.h" |
|
5 | 6 |
#include <util/delay.h> |
6 | 7 |
} |
7 | 8 |
|
9 |
#ifdef USE_ALT_TWI |
|
10 |
# include "twi.h" |
|
11 |
#else |
|
12 |
extern "C" |
|
13 |
{ |
|
14 |
# include "i2c.h" |
|
15 |
} |
|
16 |
#endif |
|
17 |
|
|
8 | 18 |
#define SCOUT_AVR_ADDR 0x01 |
9 | 19 |
|
10 | 20 |
#define FORKLIFT_ADDR 0x41 |
... | ... | |
20 | 30 |
|
21 | 31 |
#define FORKLIFT_DATA_LEN 7 |
22 | 32 |
|
23 |
using namespace Forklift; |
|
24 |
|
|
25 | 33 |
char volatile response; |
26 | 34 |
char volatile received; |
27 | 35 |
|
... | ... | |
37 | 45 |
|
38 | 46 |
static int read_addr(char addr) |
39 | 47 |
{ |
48 |
#ifdef USE_ALT_TWI |
|
49 |
return FORKLIFT_ERROR; |
|
50 |
#else |
|
40 | 51 |
if (i2c_send(FORKLIFT_ADDR, &addr, 1)) |
41 | 52 |
return FORKLIFT_ERROR; |
42 | 53 |
received = 0; |
... | ... | |
49 | 60 |
return response; |
50 | 61 |
} |
51 | 62 |
return FORKLIFT_ERROR; |
63 |
#endif |
|
52 | 64 |
} |
53 | 65 |
|
54 | 66 |
static int write_addr(char addr, char data) |
... | ... | |
56 | 68 |
char buf[2]; |
57 | 69 |
buf[0] = addr; |
58 | 70 |
buf[1] = data; |
71 |
#ifdef USE_ALT_TWI |
|
72 |
if (twi_writeTo(FORKLIFT_ADDR, (uint8_t*) buf, 2, 1)) |
|
73 |
return FORKLIFT_ERROR; |
|
74 |
#else |
|
59 | 75 |
if (i2c_send(FORKLIFT_ADDR, buf, 2)) |
60 | 76 |
return FORKLIFT_ERROR; |
61 |
else
|
|
62 |
return 0;
|
|
77 |
#endif
|
|
78 |
return 0; |
|
63 | 79 |
} |
64 | 80 |
|
65 | 81 |
void Forklift::forklift_init() |
66 | 82 |
{ |
83 |
#ifdef USE_ALT_TWI |
|
84 |
twi_init(); |
|
85 |
twi_setAddress(SCOUT_AVR_ADDR); |
|
86 |
#else |
|
67 | 87 |
i2c_init(SCOUT_AVR_ADDR, mrecv, srecv, ssend); |
88 |
#endif |
|
68 | 89 |
} |
69 | 90 |
|
70 | 91 |
int Forklift::get_tracking_id() |
trunk/code/projects/forklift_demo/twi.cpp | ||
---|---|---|
1 |
/* |
|
2 |
twi.c - TWI/I2C library for Wiring & Arduino |
|
3 |
Copyright (c) 2006 Nicholas Zambetti. All right reserved. |
|
4 |
|
|
5 |
This library is free software; you can redistribute it and/or |
|
6 |
modify it under the terms of the GNU Lesser General Public |
|
7 |
License as published by the Free Software Foundation; either |
|
8 |
version 2.1 of the License, or (at your option) any later version. |
|
9 |
|
|
10 |
This library is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 |
Lesser General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Lesser General Public |
|
16 |
License along with this library; if not, write to the Free Software |
|
17 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|
18 |
*/ |
|
19 |
|
|
20 |
#include <math.h> |
|
21 |
#include <stdlib.h> |
|
22 |
#include <inttypes.h> |
|
23 |
#include <avr/io.h> |
|
24 |
#include <avr/interrupt.h> |
|
25 |
#include <compat/twi.h> |
|
26 |
|
|
27 |
#ifndef cbi |
|
28 |
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) |
|
29 |
#endif |
|
30 |
|
|
31 |
#ifndef sbi |
|
32 |
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) |
|
33 |
#endif |
|
34 |
|
|
35 |
#include "twi.h" |
|
36 |
|
|
37 |
static volatile uint8_t twi_state; |
|
38 |
static uint8_t twi_slarw; |
|
39 |
|
|
40 |
static void (*twi_onSlaveTransmit)(void); |
|
41 |
static void (*twi_onSlaveReceive)(uint8_t*, int); |
|
42 |
|
|
43 |
static uint8_t twi_masterBuffer[TWI_BUFFER_LENGTH]; |
|
44 |
static volatile uint8_t twi_masterBufferIndex; |
|
45 |
static uint8_t twi_masterBufferLength; |
|
46 |
|
|
47 |
static uint8_t twi_txBuffer[TWI_BUFFER_LENGTH]; |
|
48 |
static volatile uint8_t twi_txBufferIndex; |
|
49 |
static volatile uint8_t twi_txBufferLength; |
|
50 |
|
|
51 |
static uint8_t twi_rxBuffer[TWI_BUFFER_LENGTH]; |
|
52 |
static volatile uint8_t twi_rxBufferIndex; |
|
53 |
|
|
54 |
static volatile uint8_t twi_error; |
|
55 |
|
|
56 |
/* |
|
57 |
* Function twi_init |
|
58 |
* Desc readys twi pins and sets twi bitrate |
|
59 |
* Input none |
|
60 |
* Output none |
|
61 |
*/ |
|
62 |
void twi_init(void) |
|
63 |
{ |
|
64 |
// initialize state |
|
65 |
twi_state = TWI_READY; |
|
66 |
|
|
67 |
// activate internal pullups for twi. |
|
68 |
//digitalWrite(SDA, 1); |
|
69 |
//digitalWrite(SCL, 1); |
|
70 |
|
|
71 |
// initialize twi prescaler and bit rate |
|
72 |
cbi(TWSR, TWPS0); |
|
73 |
cbi(TWSR, TWPS1); |
|
74 |
TWBR = ((F_CPU / TWI_FREQ) - 16) / 2; |
|
75 |
|
|
76 |
/* twi bit rate formula from atmega128 manual pg 204 |
|
77 |
SCL Frequency = CPU Clock Frequency / (16 + (2 * TWBR)) |
|
78 |
note: TWBR should be 10 or higher for master mode |
|
79 |
It is 72 for a 16mhz Wiring board with 100kHz TWI */ |
|
80 |
|
|
81 |
// enable twi module, acks, and twi interrupt |
|
82 |
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA); |
|
83 |
} |
|
84 |
|
|
85 |
/* |
|
86 |
* Function twi_slaveInit |
|
87 |
* Desc sets slave address and enables interrupt |
|
88 |
* Input none |
|
89 |
* Output none |
|
90 |
*/ |
|
91 |
void twi_setAddress(uint8_t address) |
|
92 |
{ |
|
93 |
// set twi slave address (skip over TWGCE bit) |
|
94 |
TWAR = address << 1; |
|
95 |
} |
|
96 |
|
|
97 |
/* |
|
98 |
* Function twi_readFrom |
|
99 |
* Desc attempts to become twi bus master and read a |
|
100 |
* series of bytes from a device on the bus |
|
101 |
* Input address: 7bit i2c device address |
|
102 |
* data: pointer to byte array |
|
103 |
* length: number of bytes to read into array |
|
104 |
* Output number of bytes read |
|
105 |
*/ |
|
106 |
uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length) |
|
107 |
{ |
|
108 |
uint8_t i; |
|
109 |
|
|
110 |
// ensure data will fit into buffer |
|
111 |
if(TWI_BUFFER_LENGTH < length){ |
|
112 |
return 0; |
|
113 |
} |
|
114 |
|
|
115 |
// wait until twi is ready, become master receiver |
|
116 |
while(TWI_READY != twi_state){ |
|
117 |
continue; |
|
118 |
} |
|
119 |
twi_state = TWI_MRX; |
|
120 |
// reset error state (0xFF.. no error occured) |
|
121 |
twi_error = 0xFF; |
|
122 |
|
|
123 |
// initialize buffer iteration vars |
|
124 |
twi_masterBufferIndex = 0; |
|
125 |
twi_masterBufferLength = length-1; // This is not intuitive, read on... |
|
126 |
// On receive, the previously configured ACK/NACK setting is transmitted in |
|
127 |
// response to the received byte before the interrupt is signalled. |
|
128 |
// Therefor we must actually set NACK when the _next_ to last byte is |
|
129 |
// received, causing that NACK to be sent in response to receiving the last |
|
130 |
// expected byte of data. |
|
131 |
|
|
132 |
// build sla+w, slave device address + w bit |
|
133 |
twi_slarw = TW_READ; |
|
134 |
twi_slarw |= address << 1; |
|
135 |
|
|
136 |
// send start condition |
|
137 |
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA); |
|
138 |
|
|
139 |
// wait for read operation to complete |
|
140 |
while(TWI_MRX == twi_state){ |
|
141 |
continue; |
|
142 |
} |
|
143 |
|
|
144 |
if (twi_masterBufferIndex < length) |
|
145 |
length = twi_masterBufferIndex; |
|
146 |
|
|
147 |
// copy twi buffer to data |
|
148 |
for(i = 0; i < length; ++i){ |
|
149 |
data[i] = twi_masterBuffer[i]; |
|
150 |
} |
|
151 |
|
|
152 |
return length; |
|
153 |
} |
|
154 |
|
|
155 |
/* |
|
156 |
* Function twi_writeTo |
|
157 |
* Desc attempts to become twi bus master and write a |
|
158 |
* series of bytes to a device on the bus |
|
159 |
* Input address: 7bit i2c device address |
|
160 |
* data: pointer to byte array |
|
161 |
* length: number of bytes in array |
|
162 |
* wait: boolean indicating to wait for write or not |
|
163 |
* Output 0 .. success |
|
164 |
* 1 .. length to long for buffer |
|
165 |
* 2 .. address send, NACK received |
|
166 |
* 3 .. data send, NACK received |
|
167 |
* 4 .. other twi error (lost bus arbitration, bus error, ..) |
|
168 |
*/ |
|
169 |
uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait) |
|
170 |
{ |
|
171 |
uint8_t i; |
|
172 |
|
|
173 |
// ensure data will fit into buffer |
|
174 |
if(TWI_BUFFER_LENGTH < length){ |
|
175 |
return 1; |
|
176 |
} |
|
177 |
|
|
178 |
// wait until twi is ready, become master transmitter |
|
179 |
while(TWI_READY != twi_state){ |
|
180 |
continue; |
|
181 |
} |
|
182 |
twi_state = TWI_MTX; |
|
183 |
// reset error state (0xFF.. no error occured) |
|
184 |
twi_error = 0xFF; |
|
185 |
|
|
186 |
// initialize buffer iteration vars |
|
187 |
twi_masterBufferIndex = 0; |
|
188 |
twi_masterBufferLength = length; |
|
189 |
|
|
190 |
// copy data to twi buffer |
|
191 |
for(i = 0; i < length; ++i){ |
|
192 |
twi_masterBuffer[i] = data[i]; |
|
193 |
} |
|
194 |
|
|
195 |
// build sla+w, slave device address + w bit |
|
196 |
twi_slarw = TW_WRITE; |
|
197 |
twi_slarw |= address << 1; |
|
198 |
|
|
199 |
// send start condition |
|
200 |
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA); |
|
201 |
|
|
202 |
// wait for write operation to complete |
|
203 |
while(wait && (TWI_MTX == twi_state)){ |
|
204 |
continue; |
|
205 |
} |
|
206 |
|
|
207 |
if (twi_error == 0xFF) |
|
208 |
return 0; // success |
|
209 |
else if (twi_error == TW_MT_SLA_NACK) |
|
210 |
return 2; // error: address send, nack received |
|
211 |
else if (twi_error == TW_MT_DATA_NACK) |
|
212 |
return 3; // error: data send, nack received |
|
213 |
else |
|
214 |
return 4; // other twi error |
|
215 |
} |
|
216 |
|
|
217 |
/* |
|
218 |
* Function twi_transmit |
|
219 |
* Desc fills slave tx buffer with data |
|
220 |
* must be called in slave tx event callback |
|
221 |
* Input data: pointer to byte array |
|
222 |
* length: number of bytes in array |
|
223 |
* Output 1 length too long for buffer |
|
224 |
* 2 not slave transmitter |
|
225 |
* 0 ok |
|
226 |
*/ |
|
227 |
uint8_t twi_transmit(const uint8_t* data, uint8_t length) |
|
228 |
{ |
|
229 |
uint8_t i; |
|
230 |
|
|
231 |
// ensure data will fit into buffer |
|
232 |
if(TWI_BUFFER_LENGTH < length){ |
|
233 |
return 1; |
|
234 |
} |
|
235 |
|
|
236 |
// ensure we are currently a slave transmitter |
|
237 |
if(TWI_STX != twi_state){ |
|
238 |
return 2; |
|
239 |
} |
|
240 |
|
|
241 |
// set length and copy data into tx buffer |
|
242 |
twi_txBufferLength = length; |
|
243 |
for(i = 0; i < length; ++i){ |
|
244 |
twi_txBuffer[i] = data[i]; |
|
245 |
} |
|
246 |
|
|
247 |
return 0; |
|
248 |
} |
|
249 |
|
|
250 |
/* |
|
251 |
* Function twi_attachSlaveRxEvent |
|
252 |
* Desc sets function called before a slave read operation |
|
253 |
* Input function: callback function to use |
|
254 |
* Output none |
|
255 |
*/ |
|
256 |
void twi_attachSlaveRxEvent( void (*function)(uint8_t*, int) ) |
|
257 |
{ |
|
258 |
twi_onSlaveReceive = function; |
|
259 |
} |
|
260 |
|
|
261 |
/* |
|
262 |
* Function twi_attachSlaveTxEvent |
|
263 |
* Desc sets function called before a slave write operation |
|
264 |
* Input function: callback function to use |
|
265 |
* Output none |
|
266 |
*/ |
|
267 |
void twi_attachSlaveTxEvent( void (*function)(void) ) |
|
268 |
{ |
|
269 |
twi_onSlaveTransmit = function; |
|
270 |
} |
|
271 |
|
|
272 |
/* |
|
273 |
* Function twi_reply |
|
274 |
* Desc sends byte or readys receive line |
|
275 |
* Input ack: byte indicating to ack or to nack |
|
276 |
* Output none |
|
277 |
*/ |
|
278 |
void twi_reply(uint8_t ack) |
|
279 |
{ |
|
280 |
// transmit master read ready signal, with or without ack |
|
281 |
if(ack){ |
|
282 |
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT) | _BV(TWEA); |
|
283 |
}else{ |
|
284 |
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT); |
|
285 |
} |
|
286 |
} |
|
287 |
|
|
288 |
/* |
|
289 |
* Function twi_stop |
|
290 |
* Desc relinquishes bus master status |
|
291 |
* Input none |
|
292 |
* Output none |
|
293 |
*/ |
|
294 |
void twi_stop(void) |
|
295 |
{ |
|
296 |
// send stop condition |
|
297 |
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTO); |
|
298 |
|
|
299 |
// wait for stop condition to be exectued on bus |
|
300 |
// TWINT is not set after a stop condition! |
|
301 |
while(TWCR & _BV(TWSTO)){ |
|
302 |
continue; |
|
303 |
} |
|
304 |
|
|
305 |
// update twi state |
|
306 |
twi_state = TWI_READY; |
|
307 |
} |
|
308 |
|
|
309 |
/* |
|
310 |
* Function twi_releaseBus |
|
311 |
* Desc releases bus control |
|
312 |
* Input none |
|
313 |
* Output none |
|
314 |
*/ |
|
315 |
void twi_releaseBus(void) |
|
316 |
{ |
|
317 |
// release bus |
|
318 |
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT); |
|
319 |
|
|
320 |
// update twi state |
|
321 |
twi_state = TWI_READY; |
|
322 |
} |
|
323 |
|
|
324 |
SIGNAL(TWI_vect) |
|
325 |
{ |
|
326 |
switch(TW_STATUS){ |
|
327 |
// All Master |
|
328 |
case TW_START: // sent start condition |
|
329 |
case TW_REP_START: // sent repeated start condition |
|
330 |
// copy device address and r/w bit to output register and ack |
|
331 |
TWDR = twi_slarw; |
|
332 |
twi_reply(1); |
|
333 |
break; |
|
334 |
|
|
335 |
// Master Transmitter |
|
336 |
case TW_MT_SLA_ACK: // slave receiver acked address |
|
337 |
case TW_MT_DATA_ACK: // slave receiver acked data |
|
338 |
// if there is data to send, send it, otherwise stop |
|
339 |
if(twi_masterBufferIndex < twi_masterBufferLength){ |
|
340 |
// copy data to output register and ack |
|
341 |
TWDR = twi_masterBuffer[twi_masterBufferIndex++]; |
|
342 |
twi_reply(1); |
|
343 |
}else{ |
|
344 |
twi_stop(); |
|
345 |
} |
|
346 |
break; |
|
347 |
case TW_MT_SLA_NACK: // address sent, nack received |
|
348 |
twi_error = TW_MT_SLA_NACK; |
|
349 |
twi_stop(); |
|
350 |
break; |
|
351 |
case TW_MT_DATA_NACK: // data sent, nack received |
|
352 |
twi_error = TW_MT_DATA_NACK; |
|
353 |
twi_stop(); |
|
354 |
break; |
|
355 |
case TW_MT_ARB_LOST: // lost bus arbitration |
|
356 |
twi_error = TW_MT_ARB_LOST; |
|
357 |
twi_releaseBus(); |
|
358 |
break; |
|
359 |
|
|
360 |
// Master Receiver |
|
361 |
case TW_MR_DATA_ACK: // data received, ack sent |
|
362 |
// put byte into buffer |
|
363 |
twi_masterBuffer[twi_masterBufferIndex++] = TWDR; |
|
364 |
case TW_MR_SLA_ACK: // address sent, ack received |
|
365 |
// ack if more bytes are expected, otherwise nack |
|
366 |
if(twi_masterBufferIndex < twi_masterBufferLength){ |
|
367 |
twi_reply(1); |
|
368 |
}else{ |
|
369 |
twi_reply(0); |
|
370 |
} |
|
371 |
break; |
|
372 |
case TW_MR_DATA_NACK: // data received, nack sent |
|
373 |
// put final byte into buffer |
|
374 |
twi_masterBuffer[twi_masterBufferIndex++] = TWDR; |
|
375 |
case TW_MR_SLA_NACK: // address sent, nack received |
|
376 |
twi_stop(); |
|
377 |
break; |
|
378 |
// TW_MR_ARB_LOST handled by TW_MT_ARB_LOST case |
|
379 |
|
|
380 |
// Slave Receiver |
|
381 |
case TW_SR_SLA_ACK: // addressed, returned ack |
|
382 |
case TW_SR_GCALL_ACK: // addressed generally, returned ack |
|
383 |
case TW_SR_ARB_LOST_SLA_ACK: // lost arbitration, returned ack |
|
384 |
case TW_SR_ARB_LOST_GCALL_ACK: // lost arbitration, returned ack |
|
385 |
// enter slave receiver mode |
|
386 |
twi_state = TWI_SRX; |
|
387 |
// indicate that rx buffer can be overwritten and ack |
|
388 |
twi_rxBufferIndex = 0; |
|
389 |
twi_reply(1); |
|
390 |
break; |
|
391 |
case TW_SR_DATA_ACK: // data received, returned ack |
|
392 |
case TW_SR_GCALL_DATA_ACK: // data received generally, returned ack |
|
393 |
// if there is still room in the rx buffer |
|
394 |
if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){ |
|
395 |
// put byte in buffer and ack |
|
396 |
twi_rxBuffer[twi_rxBufferIndex++] = TWDR; |
|
397 |
twi_reply(1); |
|
398 |
}else{ |
|
399 |
// otherwise nack |
|
400 |
twi_reply(0); |
|
401 |
} |
|
402 |
break; |
|
403 |
case TW_SR_STOP: // stop or repeated start condition received |
|
404 |
// put a null char after data if there's room |
|
405 |
if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){ |
|
406 |
twi_rxBuffer[twi_rxBufferIndex] = '\0'; |
|
407 |
} |
|
408 |
// sends ack and stops interface for clock stretching |
|
409 |
twi_stop(); |
|
410 |
// callback to user defined callback |
|
411 |
twi_onSlaveReceive(twi_rxBuffer, twi_rxBufferIndex); |
|
412 |
// since we submit rx buffer to "wire" library, we can reset it |
|
413 |
twi_rxBufferIndex = 0; |
|
414 |
// ack future responses and leave slave receiver state |
|
415 |
twi_releaseBus(); |
|
416 |
break; |
|
417 |
case TW_SR_DATA_NACK: // data received, returned nack |
|
418 |
case TW_SR_GCALL_DATA_NACK: // data received generally, returned nack |
|
419 |
// nack back at master |
|
420 |
twi_reply(0); |
|
421 |
break; |
|
422 |
|
|
423 |
// Slave Transmitter |
|
424 |
case TW_ST_SLA_ACK: // addressed, returned ack |
|
425 |
case TW_ST_ARB_LOST_SLA_ACK: // arbitration lost, returned ack |
|
426 |
// enter slave transmitter mode |
|
427 |
twi_state = TWI_STX; |
|
428 |
// ready the tx buffer index for iteration |
|
429 |
twi_txBufferIndex = 0; |
|
430 |
// set tx buffer length to be zero, to verify if user changes it |
|
431 |
twi_txBufferLength = 0; |
|
432 |
// request for txBuffer to be filled and length to be set |
|
433 |
// note: user must call twi_transmit(bytes, length) to do this |
|
434 |
twi_onSlaveTransmit(); |
|
435 |
// if they didn't change buffer & length, initialize it |
|
436 |
if(0 == twi_txBufferLength){ |
|
437 |
twi_txBufferLength = 1; |
|
438 |
twi_txBuffer[0] = 0x00; |
|
439 |
} |
|
440 |
// transmit first byte from buffer, fall |
|
441 |
case TW_ST_DATA_ACK: // byte sent, ack returned |
|
442 |
// copy data to output register |
|
443 |
TWDR = twi_txBuffer[twi_txBufferIndex++]; |
|
444 |
// if there is more to send, ack, otherwise nack |
|
445 |
if(twi_txBufferIndex < twi_txBufferLength){ |
|
446 |
twi_reply(1); |
|
447 |
}else{ |
|
448 |
twi_reply(0); |
|
449 |
} |
|
450 |
break; |
|
451 |
case TW_ST_DATA_NACK: // received nack, we are done |
|
452 |
case TW_ST_LAST_DATA: // received ack, but we are done already! |
|
453 |
// ack future responses |
|
454 |
twi_reply(1); |
|
455 |
// leave slave receiver state |
|
456 |
twi_state = TWI_READY; |
|
457 |
break; |
|
458 |
|
|
459 |
// All |
|
460 |
case TW_NO_INFO: // no state information |
|
461 |
break; |
|
462 |
case TW_BUS_ERROR: // bus error, illegal stop/start |
|
463 |
twi_error = TW_BUS_ERROR; |
|
464 |
twi_stop(); |
|
465 |
break; |
|
466 |
} |
|
467 |
} |
trunk/code/projects/forklift_demo/main.cpp | ||
---|---|---|
1 | 1 |
#include "Forklift.h" |
2 |
|
|
2 |
#include "twi.h" |
|
3 | 3 |
extern "C" |
4 | 4 |
{ |
5 | 5 |
#include <dragonfly_lib.h> |
6 | 6 |
} |
7 | 7 |
|
8 |
#define SPD 200 |
|
9 |
#define TURN_TIME 950 |
|
8 |
#define SPD 190 //200
|
|
9 |
#define TURN_TIME 1050 //950
|
|
10 | 10 |
|
11 | 11 |
|
12 | 12 |
using namespace Forklift; |
... | ... | |
14 | 14 |
|
15 | 15 |
int main() |
16 | 16 |
{ |
17 |
sei(); |
|
17 | 18 |
dragonfly_init(ALL_ON); |
18 | 19 |
orb_init(); |
19 | 20 |
forklift_init(); |
... | ... | |
22 | 23 |
set_height_setpoint(128); |
23 | 24 |
orb1_set_color(PURPLE); |
24 | 25 |
orb2_set_color(RED); |
26 |
/*int high = 1; |
|
27 |
while(true) |
|
28 |
{ |
|
29 |
set_height_setpoint(high? 100 : 150); |
|
30 |
high = !high; |
|
31 |
delay_ms(4000); |
|
32 |
}*/ |
|
25 | 33 |
while(true) |
26 | 34 |
{ |
27 | 35 |
while(!button2_read()); |
28 | 36 |
motor_l_set(BACKWARD, SPD); |
29 | 37 |
motor_r_set(BACKWARD, SPD); |
30 |
delay_ms(1500);
|
|
38 |
delay_ms(800);
|
|
31 | 39 |
motor_l_set(BACKWARD, 0); |
32 | 40 |
motor_r_set(BACKWARD, 0); |
33 | 41 |
set_height_setpoint(30); |
... | ... | |
36 | 44 |
motor_r_set(BACKWARD, SPD); |
37 | 45 |
delay_ms(200); |
38 | 46 |
set_height_setpoint(230); |
39 |
delay_ms(1000); |
|
47 |
delay_ms(600); |
|
48 |
motor_l_set(BACKWARD, 0); |
|
49 |
motor_r_set(BACKWARD, 0); |
|
50 |
delay_ms(1200); |
|
40 | 51 |
motor_l_set(BACKWARD, SPD); |
41 | 52 |
motor_r_set(FORWARD, SPD); |
42 | 53 |
delay_ms(TURN_TIME); |
43 | 54 |
motor_l_set(BACKWARD, SPD); |
44 | 55 |
motor_r_set(BACKWARD, SPD); |
45 |
delay_ms(1500);
|
|
56 |
delay_ms(800);
|
|
46 | 57 |
motor_l_set(BACKWARD, SPD); |
47 | 58 |
motor_r_set(FORWARD, SPD); |
48 | 59 |
delay_ms(TURN_TIME); |
49 | 60 |
motor_l_set(BACKWARD, 0); |
50 | 61 |
motor_r_set(BACKWARD, 0); |
51 |
set_height_setpoint(130);
|
|
62 |
set_height_setpoint(128);
|
|
52 | 63 |
} |
53 | 64 |
} |
trunk/code/projects/forklift_demo/twi.h | ||
---|---|---|
1 |
/* |
|
2 |
twi.h - TWI/I2C library for Wiring & Arduino |
|
3 |
Copyright (c) 2006 Nicholas Zambetti. All right reserved. |
|
4 |
|
|
5 |
This library is free software; you can redistribute it and/or |
|
6 |
modify it under the terms of the GNU Lesser General Public |
|
7 |
License as published by the Free Software Foundation; either |
|
8 |
version 2.1 of the License, or (at your option) any later version. |
|
9 |
|
|
10 |
This library is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 |
Lesser General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Lesser General Public |
|
16 |
License along with this library; if not, write to the Free Software |
|
17 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|
18 |
*/ |
|
19 |
|
|
20 |
#ifndef twi_h |
|
21 |
#define twi_h |
|
22 |
|
|
23 |
#include <inttypes.h> |
|
24 |
|
|
25 |
//#define ATMEGA8 |
|
26 |
|
|
27 |
#ifndef TWI_FREQ |
|
28 |
#define TWI_FREQ 20000L |
|
29 |
#endif |
|
30 |
|
|
31 |
#ifndef TWI_BUFFER_LENGTH |
|
32 |
#define TWI_BUFFER_LENGTH 32 |
|
33 |
#endif |
|
34 |
|
|
35 |
#define TWI_READY 0 |
|
36 |
#define TWI_MRX 1 |
|
37 |
#define TWI_MTX 2 |
|
38 |
#define TWI_SRX 3 |
|
39 |
#define TWI_STX 4 |
|
40 |
|
|
41 |
void twi_init(void); |
|
42 |
void twi_setAddress(uint8_t); |
|
43 |
uint8_t twi_readFrom(uint8_t, uint8_t*, uint8_t); |
|
44 |
uint8_t twi_writeTo(uint8_t, uint8_t*, uint8_t, uint8_t); |
|
45 |
uint8_t twi_transmit(const uint8_t*, uint8_t); |
|
46 |
void twi_attachSlaveRxEvent( void (*)(uint8_t*, int) ); |
|
47 |
void twi_attachSlaveTxEvent( void (*)(void) ); |
|
48 |
void twi_reply(uint8_t); |
|
49 |
void twi_stop(void); |
|
50 |
void twi_releaseBus(void); |
|
51 |
|
|
52 |
#endif |
|
53 |
|
trunk/code/projects/warehouse/Forklift.cpp | ||
---|---|---|
1 |
#define USE_ALT_TWI // an alternative to libdragonfly's i2c code, in twi.cpp |
|
2 |
|
|
1 | 3 |
#include "Forklift.h" |
2 |
#include "libdragonfly/i2c.h" |
|
4 |
extern "C" |
|
5 |
{ |
|
3 | 6 |
#include <util/delay.h> |
7 |
} |
|
4 | 8 |
|
9 |
#ifdef USE_ALT_TWI |
|
10 |
# include "twi.h" |
|
11 |
#else |
|
12 |
extern "C" |
|
13 |
{ |
|
14 |
# include "i2c.h" |
|
15 |
} |
|
16 |
#endif |
|
17 |
|
|
5 | 18 |
#define SCOUT_AVR_ADDR 0x01 |
6 | 19 |
|
7 | 20 |
#define FORKLIFT_ADDR 0x41 |
... | ... | |
17 | 30 |
|
18 | 31 |
#define FORKLIFT_DATA_LEN 7 |
19 | 32 |
|
20 |
using namespace Forklift; |
|
21 |
|
|
22 | 33 |
char volatile response; |
23 | 34 |
char volatile received; |
24 | 35 |
|
25 |
int mrecv(char data) |
|
36 |
static int mrecv(char data)
|
|
26 | 37 |
{ |
27 | 38 |
response = data; |
28 | 39 |
received = 1; |
40 |
return 0; |
|
29 | 41 |
} |
30 | 42 |
|
31 |
void srecv(char) {} |
|
32 |
char ssend() {}
|
|
43 |
static void srecv(char) {}
|
|
44 |
static char ssend() {return 0;}
|
|
33 | 45 |
|
34 |
int read_addr(char addr) |
|
46 |
static int read_addr(char addr)
|
|
35 | 47 |
{ |
48 |
#ifdef USE_ALT_TWI |
|
49 |
return FORKLIFT_ERROR; |
|
50 |
#else |
|
36 | 51 |
if (i2c_send(FORKLIFT_ADDR, &addr, 1)) |
37 | 52 |
return FORKLIFT_ERROR; |
38 | 53 |
received = 0; |
... | ... | |
45 | 60 |
return response; |
46 | 61 |
} |
47 | 62 |
return FORKLIFT_ERROR; |
63 |
#endif |
|
48 | 64 |
} |
49 | 65 |
|
50 |
int write_addr(char addr, char data) |
|
66 |
static int write_addr(char addr, char data)
|
|
51 | 67 |
{ |
52 | 68 |
char buf[2]; |
53 | 69 |
buf[0] = addr; |
54 | 70 |
buf[1] = data; |
71 |
#ifdef USE_ALT_TWI |
|
72 |
if (twi_writeTo(FORKLIFT_ADDR, (uint8_t*) buf, 2, 1)) |
|
73 |
return FORKLIFT_ERROR; |
|
74 |
#else |
|
55 | 75 |
if (i2c_send(FORKLIFT_ADDR, buf, 2)) |
56 | 76 |
return FORKLIFT_ERROR; |
57 |
else
|
|
58 |
return 0;
|
|
77 |
#endif
|
|
78 |
return 0; |
|
59 | 79 |
} |
60 | 80 |
|
61 |
void forklift_init() |
|
81 |
void Forklift::forklift_init()
|
|
62 | 82 |
{ |
83 |
#ifdef USE_ALT_TWI |
|
84 |
twi_init(); |
|
85 |
twi_setAddress(SCOUT_AVR_ADDR); |
|
86 |
#else |
|
63 | 87 |
i2c_init(SCOUT_AVR_ADDR, mrecv, srecv, ssend); |
88 |
#endif |
|
64 | 89 |
} |
65 | 90 |
|
66 |
int get_tracking_id() |
|
91 |
int Forklift::get_tracking_id()
|
|
67 | 92 |
{ |
68 | 93 |
return read_addr(FORKLIFT_TRACKING_ID); |
69 | 94 |
} |
70 | 95 |
|
71 |
int get_serial_number() |
|
96 |
int Forklift::get_serial_number()
|
|
72 | 97 |
{ |
73 | 98 |
return read_addr(FORKLIFT_SERIAL_NUMBER); |
74 | 99 |
} |
75 | 100 |
|
76 |
int get_height() |
|
101 |
int Forklift::get_height()
|
|
77 | 102 |
{ |
78 | 103 |
return read_addr(FORKLIFT_HEIGHT); |
79 | 104 |
} |
80 | 105 |
|
81 |
int get_height_setpoint() |
|
106 |
int Forklift::get_height_setpoint()
|
|
82 | 107 |
{ |
83 | 108 |
return read_addr(FORKLIFT_HEIGHT_SETPOINT); |
84 | 109 |
} |
85 | 110 |
|
86 |
int set_height_setpoint(char setpoint) |
|
111 |
int Forklift::set_height_setpoint(char setpoint)
|
|
87 | 112 |
{ |
88 | 113 |
return write_addr(FORKLIFT_HEIGHT_SETPOINT, setpoint); |
89 | 114 |
} |
trunk/code/projects/warehouse/twi.cpp | ||
---|---|---|
1 |
/* |
|
2 |
twi.c - TWI/I2C library for Wiring & Arduino |
|
3 |
Copyright (c) 2006 Nicholas Zambetti. All right reserved. |
|
4 |
|
|
5 |
This library is free software; you can redistribute it and/or |
|
6 |
modify it under the terms of the GNU Lesser General Public |
|
7 |
License as published by the Free Software Foundation; either |
|
8 |
version 2.1 of the License, or (at your option) any later version. |
|
9 |
|
|
10 |
This library is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 |
Lesser General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Lesser General Public |
|
16 |
License along with this library; if not, write to the Free Software |
|
17 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|
18 |
*/ |
|
19 |
|
|
20 |
#include <math.h> |
|
21 |
#include <stdlib.h> |
|
22 |
#include <inttypes.h> |
|
23 |
#include <avr/io.h> |
|
24 |
#include <avr/interrupt.h> |
|
25 |
#include <compat/twi.h> |
|
26 |
|
|
27 |
#ifndef cbi |
|
28 |
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit)) |
|
29 |
#endif |
|
30 |
|
|
31 |
#ifndef sbi |
|
32 |
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit)) |
|
33 |
#endif |
|
34 |
|
|
35 |
#include "twi.h" |
|
36 |
|
|
37 |
static volatile uint8_t twi_state; |
|
38 |
static uint8_t twi_slarw; |
|
39 |
|
|
40 |
static void (*twi_onSlaveTransmit)(void); |
|
41 |
static void (*twi_onSlaveReceive)(uint8_t*, int); |
|
42 |
|
|
43 |
static uint8_t twi_masterBuffer[TWI_BUFFER_LENGTH]; |
|
44 |
static volatile uint8_t twi_masterBufferIndex; |
|
45 |
static uint8_t twi_masterBufferLength; |
|
46 |
|
|
47 |
static uint8_t twi_txBuffer[TWI_BUFFER_LENGTH]; |
|
48 |
static volatile uint8_t twi_txBufferIndex; |
|
49 |
static volatile uint8_t twi_txBufferLength; |
|
50 |
|
|
51 |
static uint8_t twi_rxBuffer[TWI_BUFFER_LENGTH]; |
|
52 |
static volatile uint8_t twi_rxBufferIndex; |
|
53 |
|
|
54 |
static volatile uint8_t twi_error; |
|
55 |
|
|
56 |
/* |
|
57 |
* Function twi_init |
|
58 |
* Desc readys twi pins and sets twi bitrate |
|
59 |
* Input none |
|
60 |
* Output none |
|
61 |
*/ |
|
62 |
void twi_init(void) |
|
63 |
{ |
|
64 |
// initialize state |
|
65 |
twi_state = TWI_READY; |
|
66 |
|
|
67 |
// activate internal pullups for twi. |
|
68 |
//digitalWrite(SDA, 1); |
|
69 |
//digitalWrite(SCL, 1); |
|
70 |
|
|
71 |
// initialize twi prescaler and bit rate |
|
72 |
cbi(TWSR, TWPS0); |
|
73 |
cbi(TWSR, TWPS1); |
|
74 |
TWBR = ((F_CPU / TWI_FREQ) - 16) / 2; |
|
75 |
|
|
76 |
/* twi bit rate formula from atmega128 manual pg 204 |
|
77 |
SCL Frequency = CPU Clock Frequency / (16 + (2 * TWBR)) |
|
78 |
note: TWBR should be 10 or higher for master mode |
|
79 |
It is 72 for a 16mhz Wiring board with 100kHz TWI */ |
|
80 |
|
|
81 |
// enable twi module, acks, and twi interrupt |
|
82 |
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA); |
|
83 |
} |
|
84 |
|
|
85 |
/* |
|
86 |
* Function twi_slaveInit |
|
87 |
* Desc sets slave address and enables interrupt |
|
88 |
* Input none |
|
89 |
* Output none |
|
90 |
*/ |
|
91 |
void twi_setAddress(uint8_t address) |
|
92 |
{ |
|
93 |
// set twi slave address (skip over TWGCE bit) |
|
94 |
TWAR = address << 1; |
|
95 |
} |
|
96 |
|
|
97 |
/* |
|
98 |
* Function twi_readFrom |
|
99 |
* Desc attempts to become twi bus master and read a |
|
100 |
* series of bytes from a device on the bus |
|
101 |
* Input address: 7bit i2c device address |
|
102 |
* data: pointer to byte array |
|
103 |
* length: number of bytes to read into array |
|
104 |
* Output number of bytes read |
|
105 |
*/ |
|
106 |
uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length) |
|
107 |
{ |
|
108 |
uint8_t i; |
|
109 |
|
|
110 |
// ensure data will fit into buffer |
|
111 |
if(TWI_BUFFER_LENGTH < length){ |
|
112 |
return 0; |
|
113 |
} |
|
114 |
|
|
115 |
// wait until twi is ready, become master receiver |
|
116 |
while(TWI_READY != twi_state){ |
|
117 |
continue; |
|
118 |
} |
|
119 |
twi_state = TWI_MRX; |
|
120 |
// reset error state (0xFF.. no error occured) |
|
121 |
twi_error = 0xFF; |
|
122 |
|
|
123 |
// initialize buffer iteration vars |
|
124 |
twi_masterBufferIndex = 0; |
|
125 |
twi_masterBufferLength = length-1; // This is not intuitive, read on... |
|
126 |
// On receive, the previously configured ACK/NACK setting is transmitted in |
|
127 |
// response to the received byte before the interrupt is signalled. |
|
128 |
// Therefor we must actually set NACK when the _next_ to last byte is |
|
129 |
// received, causing that NACK to be sent in response to receiving the last |
|
130 |
// expected byte of data. |
|
131 |
|
|
132 |
// build sla+w, slave device address + w bit |
|
133 |
twi_slarw = TW_READ; |
|
134 |
twi_slarw |= address << 1; |
|
135 |
|
|
136 |
// send start condition |
|
137 |
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA); |
|
138 |
|
|
139 |
// wait for read operation to complete |
|
140 |
while(TWI_MRX == twi_state){ |
|
141 |
continue; |
|
142 |
} |
|
143 |
|
|
144 |
if (twi_masterBufferIndex < length) |
|
145 |
length = twi_masterBufferIndex; |
|
146 |
|
|
147 |
// copy twi buffer to data |
|
148 |
for(i = 0; i < length; ++i){ |
|
149 |
data[i] = twi_masterBuffer[i]; |
|
150 |
} |
|
151 |
|
|
152 |
return length; |
|
153 |
} |
|
154 |
|
|
155 |
/* |
|
156 |
* Function twi_writeTo |
|
157 |
* Desc attempts to become twi bus master and write a |
|
158 |
* series of bytes to a device on the bus |
|
159 |
* Input address: 7bit i2c device address |
|
160 |
* data: pointer to byte array |
|
161 |
* length: number of bytes in array |
|
162 |
* wait: boolean indicating to wait for write or not |
|
163 |
* Output 0 .. success |
|
164 |
* 1 .. length to long for buffer |
|
165 |
* 2 .. address send, NACK received |
|
166 |
* 3 .. data send, NACK received |
|
167 |
* 4 .. other twi error (lost bus arbitration, bus error, ..) |
|
168 |
*/ |
|
169 |
uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait) |
|
170 |
{ |
|
171 |
uint8_t i; |
|
172 |
|
|
173 |
// ensure data will fit into buffer |
|
174 |
if(TWI_BUFFER_LENGTH < length){ |
|
175 |
return 1; |
|
176 |
} |
|
177 |
|
|
178 |
// wait until twi is ready, become master transmitter |
|
179 |
while(TWI_READY != twi_state){ |
|
180 |
continue; |
|
181 |
} |
|
182 |
twi_state = TWI_MTX; |
|
183 |
// reset error state (0xFF.. no error occured) |
|
184 |
twi_error = 0xFF; |
|
185 |
|
|
186 |
// initialize buffer iteration vars |
|
187 |
twi_masterBufferIndex = 0; |
|
188 |
twi_masterBufferLength = length; |
|
189 |
|
|
190 |
// copy data to twi buffer |
|
191 |
for(i = 0; i < length; ++i){ |
|
192 |
twi_masterBuffer[i] = data[i]; |
|
193 |
} |
|
194 |
|
|
195 |
// build sla+w, slave device address + w bit |
|
196 |
twi_slarw = TW_WRITE; |
|
197 |
twi_slarw |= address << 1; |
|
198 |
|
|
199 |
// send start condition |
|
200 |
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA); |
|
201 |
|
|
202 |
// wait for write operation to complete |
|
203 |
while(wait && (TWI_MTX == twi_state)){ |
|
204 |
continue; |
|
205 |
} |
|
206 |
|
|
207 |
if (twi_error == 0xFF) |
|
208 |
return 0; // success |
|
209 |
else if (twi_error == TW_MT_SLA_NACK) |
|
210 |
return 2; // error: address send, nack received |
|
211 |
else if (twi_error == TW_MT_DATA_NACK) |
|
212 |
return 3; // error: data send, nack received |
|
213 |
else |
|
214 |
return 4; // other twi error |
|
215 |
} |
|
216 |
|
|
217 |
/* |
|
218 |
* Function twi_transmit |
|
219 |
* Desc fills slave tx buffer with data |
|
220 |
* must be called in slave tx event callback |
|
221 |
* Input data: pointer to byte array |
|
222 |
* length: number of bytes in array |
|
223 |
* Output 1 length too long for buffer |
|
224 |
* 2 not slave transmitter |
|
225 |
* 0 ok |
|
226 |
*/ |
|
227 |
uint8_t twi_transmit(const uint8_t* data, uint8_t length) |
|
228 |
{ |
|
229 |
uint8_t i; |
|
230 |
|
|
231 |
// ensure data will fit into buffer |
|
232 |
if(TWI_BUFFER_LENGTH < length){ |
|
233 |
return 1; |
|
234 |
} |
|
235 |
|
|
236 |
// ensure we are currently a slave transmitter |
|
237 |
if(TWI_STX != twi_state){ |
|
238 |
return 2; |
|
239 |
} |
|
240 |
|
|
241 |
// set length and copy data into tx buffer |
|
242 |
twi_txBufferLength = length; |
|
243 |
for(i = 0; i < length; ++i){ |
|
244 |
twi_txBuffer[i] = data[i]; |
|
245 |
} |
|
246 |
|
|
247 |
return 0; |
|
248 |
} |
|
249 |
|
|
250 |
/* |
|
251 |
* Function twi_attachSlaveRxEvent |
|
252 |
* Desc sets function called before a slave read operation |
|
253 |
* Input function: callback function to use |
|
254 |
* Output none |
|
255 |
*/ |
|
256 |
void twi_attachSlaveRxEvent( void (*function)(uint8_t*, int) ) |
|
257 |
{ |
|
258 |
twi_onSlaveReceive = function; |
|
259 |
} |
|
260 |
|
|
261 |
/* |
|
262 |
* Function twi_attachSlaveTxEvent |
|
263 |
* Desc sets function called before a slave write operation |
|
264 |
* Input function: callback function to use |
|
265 |
* Output none |
|
266 |
*/ |
|
267 |
void twi_attachSlaveTxEvent( void (*function)(void) ) |
|
268 |
{ |
|
269 |
twi_onSlaveTransmit = function; |
|
270 |
} |
|
271 |
|
|
272 |
/* |
|
273 |
* Function twi_reply |
|
274 |
* Desc sends byte or readys receive line |
|
275 |
* Input ack: byte indicating to ack or to nack |
|
276 |
* Output none |
|
277 |
*/ |
|
278 |
void twi_reply(uint8_t ack) |
|
279 |
{ |
|
280 |
// transmit master read ready signal, with or without ack |
|
281 |
if(ack){ |
|
282 |
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT) | _BV(TWEA); |
|
283 |
}else{ |
|
284 |
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT); |
|
285 |
} |
|
286 |
} |
|
287 |
|
|
288 |
/* |
|
289 |
* Function twi_stop |
|
290 |
* Desc relinquishes bus master status |
|
291 |
* Input none |
|
292 |
* Output none |
|
293 |
*/ |
|
294 |
void twi_stop(void) |
|
295 |
{ |
|
296 |
// send stop condition |
|
297 |
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTO); |
|
298 |
|
|
299 |
// wait for stop condition to be exectued on bus |
|
300 |
// TWINT is not set after a stop condition! |
|
301 |
while(TWCR & _BV(TWSTO)){ |
|
302 |
continue; |
|
303 |
} |
|
304 |
|
|
305 |
// update twi state |
|
306 |
twi_state = TWI_READY; |
|
307 |
} |
|
308 |
|
|
309 |
/* |
|
310 |
* Function twi_releaseBus |
|
311 |
* Desc releases bus control |
|
312 |
* Input none |
|
313 |
* Output none |
|
314 |
*/ |
|
315 |
void twi_releaseBus(void) |
|
316 |
{ |
|
317 |
// release bus |
|
318 |
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT); |
|
319 |
|
|
320 |
// update twi state |
|
321 |
twi_state = TWI_READY; |
|
322 |
} |
|
323 |
|
|
324 |
SIGNAL(TWI_vect) |
|
325 |
{ |
|
326 |
switch(TW_STATUS){ |
|
327 |
// All Master |
|
328 |
case TW_START: // sent start condition |
|
329 |
case TW_REP_START: // sent repeated start condition |
|
330 |
// copy device address and r/w bit to output register and ack |
|
331 |
TWDR = twi_slarw; |
|
332 |
twi_reply(1); |
|
333 |
break; |
|
334 |
|
|
335 |
// Master Transmitter |
|
336 |
case TW_MT_SLA_ACK: // slave receiver acked address |
|
337 |
case TW_MT_DATA_ACK: // slave receiver acked data |
|
338 |
// if there is data to send, send it, otherwise stop |
|
339 |
if(twi_masterBufferIndex < twi_masterBufferLength){ |
|
340 |
// copy data to output register and ack |
|
341 |
TWDR = twi_masterBuffer[twi_masterBufferIndex++]; |
|
342 |
twi_reply(1); |
|
343 |
}else{ |
|
344 |
twi_stop(); |
|
345 |
} |
|
346 |
break; |
|
347 |
case TW_MT_SLA_NACK: // address sent, nack received |
|
348 |
twi_error = TW_MT_SLA_NACK; |
|
349 |
twi_stop(); |
|
350 |
break; |
|
351 |
case TW_MT_DATA_NACK: // data sent, nack received |
|
352 |
twi_error = TW_MT_DATA_NACK; |
|
353 |
twi_stop(); |
|
354 |
break; |
|
355 |
case TW_MT_ARB_LOST: // lost bus arbitration |
|
356 |
twi_error = TW_MT_ARB_LOST; |
|
357 |
twi_releaseBus(); |
|
358 |
break; |
|
359 |
|
|
360 |
// Master Receiver |
|
361 |
case TW_MR_DATA_ACK: // data received, ack sent |
|
362 |
// put byte into buffer |
|
363 |
twi_masterBuffer[twi_masterBufferIndex++] = TWDR; |
|
364 |
case TW_MR_SLA_ACK: // address sent, ack received |
|
365 |
// ack if more bytes are expected, otherwise nack |
|
366 |
if(twi_masterBufferIndex < twi_masterBufferLength){ |
|
367 |
twi_reply(1); |
|
368 |
}else{ |
|
369 |
twi_reply(0); |
|
370 |
} |
|
371 |
break; |
|
372 |
case TW_MR_DATA_NACK: // data received, nack sent |
|
373 |
// put final byte into buffer |
|
374 |
twi_masterBuffer[twi_masterBufferIndex++] = TWDR; |
|
375 |
case TW_MR_SLA_NACK: // address sent, nack received |
|
376 |
twi_stop(); |
|
377 |
break; |
|
378 |
// TW_MR_ARB_LOST handled by TW_MT_ARB_LOST case |
|
379 |
|
|
380 |
// Slave Receiver |
|
381 |
case TW_SR_SLA_ACK: // addressed, returned ack |
|
382 |
case TW_SR_GCALL_ACK: // addressed generally, returned ack |
|
383 |
case TW_SR_ARB_LOST_SLA_ACK: // lost arbitration, returned ack |
|
384 |
case TW_SR_ARB_LOST_GCALL_ACK: // lost arbitration, returned ack |
|
385 |
// enter slave receiver mode |
|
386 |
twi_state = TWI_SRX; |
|
387 |
// indicate that rx buffer can be overwritten and ack |
|
388 |
twi_rxBufferIndex = 0; |
|
389 |
twi_reply(1); |
|
390 |
break; |
|
391 |
case TW_SR_DATA_ACK: // data received, returned ack |
|
392 |
case TW_SR_GCALL_DATA_ACK: // data received generally, returned ack |
|
393 |
// if there is still room in the rx buffer |
|
394 |
if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){ |
|
395 |
// put byte in buffer and ack |
|
396 |
twi_rxBuffer[twi_rxBufferIndex++] = TWDR; |
|
397 |
twi_reply(1); |
|
398 |
}else{ |
|
399 |
// otherwise nack |
|
400 |
twi_reply(0); |
|
401 |
} |
|
402 |
break; |
|
403 |
case TW_SR_STOP: // stop or repeated start condition received |
|
404 |
// put a null char after data if there's room |
|
405 |
if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){ |
|
406 |
twi_rxBuffer[twi_rxBufferIndex] = '\0'; |
|
407 |
} |
|
408 |
// sends ack and stops interface for clock stretching |
|
409 |
twi_stop(); |
|
410 |
// callback to user defined callback |
|
411 |
twi_onSlaveReceive(twi_rxBuffer, twi_rxBufferIndex); |
|
412 |
// since we submit rx buffer to "wire" library, we can reset it |
|
413 |
twi_rxBufferIndex = 0; |
|
414 |
// ack future responses and leave slave receiver state |
|
415 |
twi_releaseBus(); |
|
416 |
break; |
|
417 |
case TW_SR_DATA_NACK: // data received, returned nack |
|
418 |
case TW_SR_GCALL_DATA_NACK: // data received generally, returned nack |
|
419 |
// nack back at master |
|
420 |
twi_reply(0); |
|
421 |
break; |
|
422 |
|
|
423 |
// Slave Transmitter |
|
424 |
case TW_ST_SLA_ACK: // addressed, returned ack |
|
425 |
case TW_ST_ARB_LOST_SLA_ACK: // arbitration lost, returned ack |
|
426 |
// enter slave transmitter mode |
|
427 |
twi_state = TWI_STX; |
|
428 |
// ready the tx buffer index for iteration |
|
429 |
twi_txBufferIndex = 0; |
|
430 |
// set tx buffer length to be zero, to verify if user changes it |
|
431 |
twi_txBufferLength = 0; |
|
432 |
// request for txBuffer to be filled and length to be set |
|
433 |
// note: user must call twi_transmit(bytes, length) to do this |
|
434 |
twi_onSlaveTransmit(); |
|
435 |
// if they didn't change buffer & length, initialize it |
|
436 |
if(0 == twi_txBufferLength){ |
|
437 |
twi_txBufferLength = 1; |
|
438 |
twi_txBuffer[0] = 0x00; |
|
439 |
} |
|
440 |
// transmit first byte from buffer, fall |
|
441 |
case TW_ST_DATA_ACK: // byte sent, ack returned |
|
442 |
// copy data to output register |
|
443 |
TWDR = twi_txBuffer[twi_txBufferIndex++]; |
|
444 |
// if there is more to send, ack, otherwise nack |
|
445 |
if(twi_txBufferIndex < twi_txBufferLength){ |
|
446 |
twi_reply(1); |
|
447 |
}else{ |
|
448 |
twi_reply(0); |
|
449 |
} |
|
450 |
break; |
|
451 |
case TW_ST_DATA_NACK: // received nack, we are done |
|
452 |
case TW_ST_LAST_DATA: // received ack, but we are done already! |
|
453 |
// ack future responses |
|
454 |
twi_reply(1); |
|
455 |
// leave slave receiver state |
|
456 |
twi_state = TWI_READY; |
|
457 |
break; |
|
458 |
|
|
459 |
// All |
|
460 |
case TW_NO_INFO: // no state information |
|
461 |
break; |
|
462 |
case TW_BUS_ERROR: // bus error, illegal stop/start |
|
463 |
twi_error = TW_BUS_ERROR; |
|
464 |
twi_stop(); |
|
465 |
break; |
|
466 |
} |
|
467 |
} |
trunk/code/projects/warehouse/Forklift.h | ||
---|---|---|
10 | 10 |
int get_serial_number(); // unique to each forklift |
11 | 11 |
int get_height(); |
12 | 12 |
int get_height_setpoint(); |
13 |
int set_height_setpoint(int setpoint);
|
|
13 |
int set_height_setpoint(char setpoint);
|
|
14 | 14 |
}; |
15 | 15 |
|
16 | 16 |
#endif |
trunk/code/projects/warehouse/twi.h | ||
---|---|---|
1 |
/* |
|
2 |
twi.h - TWI/I2C library for Wiring & Arduino |
|
3 |
Copyright (c) 2006 Nicholas Zambetti. All right reserved. |
|
4 |
|
|
5 |
This library is free software; you can redistribute it and/or |
|
6 |
modify it under the terms of the GNU Lesser General Public |
|
7 |
License as published by the Free Software Foundation; either |
|
8 |
version 2.1 of the License, or (at your option) any later version. |
|
9 |
|
|
10 |
This library is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 |
Lesser General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Lesser General Public |
|
16 |
License along with this library; if not, write to the Free Software |
|
17 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|
18 |
*/ |
|
19 |
|
|
20 |
#ifndef twi_h |
|
21 |
#define twi_h |
|
22 |
|
|
23 |
#include <inttypes.h> |
|
24 |
|
|
25 |
//#define ATMEGA8 |
|
26 |
|
|
27 |
#ifndef TWI_FREQ |
|
28 |
#define TWI_FREQ 20000L |
|
29 |
#endif |
|
30 |
|
|
31 |
#ifndef TWI_BUFFER_LENGTH |
|
32 |
#define TWI_BUFFER_LENGTH 32 |
|
33 |
#endif |
|
34 |
|
|
35 |
#define TWI_READY 0 |
|
36 |
#define TWI_MRX 1 |
|
37 |
#define TWI_MTX 2 |
|
38 |
#define TWI_SRX 3 |
|
39 |
#define TWI_STX 4 |
|
40 |
|
|
41 |
void twi_init(void); |
|
42 |
void twi_setAddress(uint8_t); |
|
43 |
uint8_t twi_readFrom(uint8_t, uint8_t*, uint8_t); |
|
44 |
uint8_t twi_writeTo(uint8_t, uint8_t*, uint8_t, uint8_t); |
|
45 |
uint8_t twi_transmit(const uint8_t*, uint8_t); |
|
46 |
void twi_attachSlaveRxEvent( void (*)(uint8_t*, int) ); |
|
47 |
void twi_attachSlaveTxEvent( void (*)(void) ); |
|
48 |
void twi_reply(uint8_t); |
|
49 |
void twi_stop(void); |
|
50 |
void twi_releaseBus(void); |
|
51 |
|
|
52 |
#endif |
|
53 |
|
Also available in: Unified diff