mirror of
https://github.com/arduino/Arduino.git
synced 2025-02-18 12:54:25 +01:00
Wire library patch to provide better error handling.
This commit is contained in:
parent
3233d7939b
commit
0bdc02cea5
@ -30,7 +30,7 @@
|
|||||||
|
|
||||||
void Print::print(uint8_t b)
|
void Print::print(uint8_t b)
|
||||||
{
|
{
|
||||||
write(b);
|
this->write(b);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Print::print(char c)
|
void Print::print(char c)
|
||||||
|
@ -77,22 +77,24 @@ void TwoWire::begin(int address)
|
|||||||
begin((uint8_t)address);
|
begin((uint8_t)address);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TwoWire::requestFrom(uint8_t address, uint8_t quantity)
|
uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity)
|
||||||
{
|
{
|
||||||
// clamp to buffer length
|
// clamp to buffer length
|
||||||
if(quantity > BUFFER_LENGTH){
|
if(quantity > BUFFER_LENGTH){
|
||||||
quantity = BUFFER_LENGTH;
|
quantity = BUFFER_LENGTH;
|
||||||
}
|
}
|
||||||
// perform blocking read into buffer
|
// perform blocking read into buffer
|
||||||
twi_readFrom(address, rxBuffer, quantity);
|
uint8_t read = twi_readFrom(address, rxBuffer, quantity);
|
||||||
// set rx buffer iterator vars
|
// set rx buffer iterator vars
|
||||||
rxBufferIndex = 0;
|
rxBufferIndex = 0;
|
||||||
rxBufferLength = quantity;
|
rxBufferLength = read;
|
||||||
|
|
||||||
|
return read;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TwoWire::requestFrom(int address, int quantity)
|
uint8_t TwoWire::requestFrom(int address, int quantity)
|
||||||
{
|
{
|
||||||
requestFrom((uint8_t)address, (uint8_t)quantity);
|
return requestFrom((uint8_t)address, (uint8_t)quantity);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TwoWire::beginTransmission(uint8_t address)
|
void TwoWire::beginTransmission(uint8_t address)
|
||||||
@ -111,15 +113,16 @@ void TwoWire::beginTransmission(int address)
|
|||||||
beginTransmission((uint8_t)address);
|
beginTransmission((uint8_t)address);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TwoWire::endTransmission(void)
|
uint8_t TwoWire::endTransmission(void)
|
||||||
{
|
{
|
||||||
// transmit buffer (blocking)
|
// transmit buffer (blocking)
|
||||||
twi_writeTo(txAddress, txBuffer, txBufferLength, 1);
|
int8_t ret = twi_writeTo(txAddress, txBuffer, txBufferLength, 1);
|
||||||
// reset tx buffer iterator vars
|
// reset tx buffer iterator vars
|
||||||
txBufferIndex = 0;
|
txBufferIndex = 0;
|
||||||
txBufferLength = 0;
|
txBufferLength = 0;
|
||||||
// indicate that we are done transmitting
|
// indicate that we are done transmitting
|
||||||
transmitting = 0;
|
transmitting = 0;
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
// must be called in:
|
// must be called in:
|
||||||
|
@ -48,9 +48,9 @@ class TwoWire
|
|||||||
void begin(int);
|
void begin(int);
|
||||||
void beginTransmission(uint8_t);
|
void beginTransmission(uint8_t);
|
||||||
void beginTransmission(int);
|
void beginTransmission(int);
|
||||||
void endTransmission(void);
|
uint8_t endTransmission(void);
|
||||||
void requestFrom(uint8_t, uint8_t);
|
uint8_t requestFrom(uint8_t, uint8_t);
|
||||||
void requestFrom(int, int);
|
uint8_t requestFrom(int, int);
|
||||||
void send(uint8_t);
|
void send(uint8_t);
|
||||||
void send(uint8_t*, uint8_t);
|
void send(uint8_t*, uint8_t);
|
||||||
void send(int);
|
void send(int);
|
||||||
|
@ -51,6 +51,8 @@ static volatile uint8_t twi_txBufferLength;
|
|||||||
static uint8_t* twi_rxBuffer;
|
static uint8_t* twi_rxBuffer;
|
||||||
static volatile uint8_t twi_rxBufferIndex;
|
static volatile uint8_t twi_rxBufferIndex;
|
||||||
|
|
||||||
|
static volatile uint8_t twi_error;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Function twi_init
|
* Function twi_init
|
||||||
* Desc readys twi pins and sets twi bitrate
|
* Desc readys twi pins and sets twi bitrate
|
||||||
@ -112,7 +114,7 @@ void twi_setAddress(uint8_t address)
|
|||||||
* Input address: 7bit i2c device address
|
* Input address: 7bit i2c device address
|
||||||
* data: pointer to byte array
|
* data: pointer to byte array
|
||||||
* length: number of bytes to read into array
|
* length: number of bytes to read into array
|
||||||
* Output byte: 0 ok, 1 length too long for buffer
|
* Output number of bytes read
|
||||||
*/
|
*/
|
||||||
uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length)
|
uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length)
|
||||||
{
|
{
|
||||||
@ -120,7 +122,7 @@ uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length)
|
|||||||
|
|
||||||
// ensure data will fit into buffer
|
// ensure data will fit into buffer
|
||||||
if(TWI_BUFFER_LENGTH < length){
|
if(TWI_BUFFER_LENGTH < length){
|
||||||
return 1;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// wait until twi is ready, become master receiver
|
// wait until twi is ready, become master receiver
|
||||||
@ -128,6 +130,8 @@ uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
twi_state = TWI_MRX;
|
twi_state = TWI_MRX;
|
||||||
|
// reset error state (0xFF.. no error occured)
|
||||||
|
twi_error = 0xFF;
|
||||||
|
|
||||||
// initialize buffer iteration vars
|
// initialize buffer iteration vars
|
||||||
twi_masterBufferIndex = 0;
|
twi_masterBufferIndex = 0;
|
||||||
@ -145,12 +149,15 @@ uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (twi_masterBufferIndex < length)
|
||||||
|
length = twi_masterBufferIndex;
|
||||||
|
|
||||||
// copy twi buffer to data
|
// copy twi buffer to data
|
||||||
for(i = 0; i < length; ++i){
|
for(i = 0; i < length; ++i){
|
||||||
data[i] = twi_masterBuffer[i];
|
data[i] = twi_masterBuffer[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -161,7 +168,11 @@ uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length)
|
|||||||
* data: pointer to byte array
|
* data: pointer to byte array
|
||||||
* length: number of bytes in array
|
* length: number of bytes in array
|
||||||
* wait: boolean indicating to wait for write or not
|
* wait: boolean indicating to wait for write or not
|
||||||
* Output byte: 0 ok, 1 length too long for buffer
|
* Output 0 .. success
|
||||||
|
* 1 .. length to long for buffer
|
||||||
|
* 2 .. address send, NACK received
|
||||||
|
* 3 .. data send, NACK received
|
||||||
|
* 4 .. other twi error (lost bus arbitration, bus error, ..)
|
||||||
*/
|
*/
|
||||||
uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait)
|
uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait)
|
||||||
{
|
{
|
||||||
@ -177,6 +188,8 @@ uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
twi_state = TWI_MTX;
|
twi_state = TWI_MTX;
|
||||||
|
// reset error state (0xFF.. no error occured)
|
||||||
|
twi_error = 0xFF;
|
||||||
|
|
||||||
// initialize buffer iteration vars
|
// initialize buffer iteration vars
|
||||||
twi_masterBufferIndex = 0;
|
twi_masterBufferIndex = 0;
|
||||||
@ -199,7 +212,14 @@ uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
if (twi_error == 0xFF)
|
||||||
|
return 0; // success
|
||||||
|
else if (twi_error == TW_MT_SLA_NACK)
|
||||||
|
return 2; // error: address send, nack received
|
||||||
|
else if (twi_error == TW_MT_DATA_NACK)
|
||||||
|
return 3; // error: data send, nack received
|
||||||
|
else
|
||||||
|
return 4; // other twi error
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -333,10 +353,15 @@ SIGNAL(SIG_2WIRE_SERIAL)
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case TW_MT_SLA_NACK: // address sent, nack received
|
case TW_MT_SLA_NACK: // address sent, nack received
|
||||||
|
twi_error = TW_MT_SLA_NACK;
|
||||||
|
twi_stop();
|
||||||
|
break;
|
||||||
case TW_MT_DATA_NACK: // data sent, nack received
|
case TW_MT_DATA_NACK: // data sent, nack received
|
||||||
|
twi_error = TW_MT_DATA_NACK;
|
||||||
twi_stop();
|
twi_stop();
|
||||||
break;
|
break;
|
||||||
case TW_MT_ARB_LOST: // lost bus arbitration
|
case TW_MT_ARB_LOST: // lost bus arbitration
|
||||||
|
twi_error = TW_MT_ARB_LOST;
|
||||||
twi_releaseBus();
|
twi_releaseBus();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@ -441,6 +466,7 @@ SIGNAL(SIG_2WIRE_SERIAL)
|
|||||||
case TW_NO_INFO: // no state information
|
case TW_NO_INFO: // no state information
|
||||||
break;
|
break;
|
||||||
case TW_BUS_ERROR: // bus error, illegal stop/start
|
case TW_BUS_ERROR: // bus error, illegal stop/start
|
||||||
|
twi_error = TW_BUS_ERROR;
|
||||||
twi_stop();
|
twi_stop();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -17,18 +17,18 @@
|
|||||||
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifndef twi_h
|
#ifndef twi_h
|
||||||
#define twi_h
|
#define twi_h
|
||||||
|
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
|
|
||||||
//#define ATMEGA8
|
//#define ATMEGA8
|
||||||
|
|
||||||
#ifndef CPU_FREQ
|
#ifndef CPU_FREQ
|
||||||
#define CPU_FREQ 16000000L
|
#define CPU_FREQ 16000000L
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef TWI_FREQ
|
#ifndef TWI_FREQ
|
||||||
#define TWI_FREQ 100000L
|
#define TWI_FREQ 100000L
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -51,7 +51,7 @@
|
|||||||
void twi_attachSlaveTxEvent( void (*)(void) );
|
void twi_attachSlaveTxEvent( void (*)(void) );
|
||||||
void twi_reply(uint8_t);
|
void twi_reply(uint8_t);
|
||||||
void twi_stop(void);
|
void twi_stop(void);
|
||||||
void twi_releaseBus(void);
|
void twi_releaseBus(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
21
todo.txt
21
todo.txt
@ -9,7 +9,6 @@ Add String library.
|
|||||||
Add Servo library.
|
Add Servo library.
|
||||||
Comment LiquidCrystal examples.
|
Comment LiquidCrystal examples.
|
||||||
Write LiquidCrystal documentation.
|
Write LiquidCrystal documentation.
|
||||||
Wire library patch: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1206621511
|
|
||||||
[done] Move #include <WProgram.h> after other #include's? (prevent it from interfering with standard libraries)
|
[done] Move #include <WProgram.h> after other #include's? (prevent it from interfering with standard libraries)
|
||||||
[done] Add LiquidCrystal library.
|
[done] Add LiquidCrystal library.
|
||||||
[done] Fix millis() so it overflows on a nice variable-size boundary; see: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1205949448
|
[done] Fix millis() so it overflows on a nice variable-size boundary; see: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1205949448
|
||||||
@ -18,7 +17,7 @@ Wire library patch: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1206621511
|
|||||||
AVR
|
AVR
|
||||||
|
|
||||||
Support pin change interrupts.
|
Support pin change interrupts.
|
||||||
Fix interference between millis() / timer 0 overflow interrupt and pwm output on pins 5 and 6.
|
Switch pwm output on pins 5 and 6 to phase-correct mode, if possible.
|
||||||
Problems including WProgram.h twice?
|
Problems including WProgram.h twice?
|
||||||
Add #defines for the analog input pins.
|
Add #defines for the analog input pins.
|
||||||
Add parameter to shiftOut() for specifying a number of bits.
|
Add parameter to shiftOut() for specifying a number of bits.
|
||||||
@ -32,6 +31,7 @@ Way to print floats.
|
|||||||
Fix delayMicroseconds(0).
|
Fix delayMicroseconds(0).
|
||||||
Add sleep function(s).
|
Add sleep function(s).
|
||||||
Add SPI library.
|
Add SPI library.
|
||||||
|
Add OneWire library.
|
||||||
Add pulseOut(), etc. functions from Wiring.
|
Add pulseOut(), etc. functions from Wiring.
|
||||||
Add Ping example.
|
Add Ping example.
|
||||||
Supporting EEMEM directive by changing compiler command line: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1202157667
|
Supporting EEMEM directive by changing compiler command line: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1202157667
|
||||||
@ -84,7 +84,6 @@ Create form for submitting projects.
|
|||||||
|
|
||||||
DOCUMENTATION / META
|
DOCUMENTATION / META
|
||||||
|
|
||||||
Create community to do list.
|
|
||||||
Create community section of site.
|
Create community section of site.
|
||||||
List of examples we'd like to have.
|
List of examples we'd like to have.
|
||||||
Style guide for examples, references, and foundations.
|
Style guide for examples, references, and foundations.
|
||||||
@ -99,18 +98,22 @@ Move Environment into the Reference section (which should be renamed Programming
|
|||||||
DOCUMENTATION / CONTENTS
|
DOCUMENTATION / CONTENTS
|
||||||
|
|
||||||
Document Matrix, Sprite, and Wire libraries on the Arduino site.
|
Document Matrix, Sprite, and Wire libraries on the Arduino site.
|
||||||
Add examples using specific hardware (simple analog sensors, optocouplers, etc.)
|
|
||||||
Get good top-down, well-lit, plain-white-background photos of the Arduino boards.
|
|
||||||
Documentation for moving from Arduino to custom PCBs.
|
Documentation for moving from Arduino to custom PCBs.
|
||||||
Examples should demonstrate use of functions.
|
|
||||||
Arduino feature list (in Getting Started > Introduction).
|
Arduino feature list (in Getting Started > Introduction).
|
||||||
Programming tutorial for Arduino.
|
Programming tutorial for Arduino.
|
||||||
Write advanced library tutorial.
|
Write advanced library tutorial.
|
||||||
Better documentation of the Arduino BT.
|
Better documentation of the Arduino BT.
|
||||||
Tutorial about serial communication.
|
Tutorial about serial communication.
|
||||||
|
|
||||||
|
DOCUMENTATION / EXAMPLES
|
||||||
|
|
||||||
|
Add examples using specific hardware (simple analog sensors, optocouplers, etc.)
|
||||||
|
Examples should demonstrate use of functions.
|
||||||
|
Add I2C EEPROM example using Wire library.
|
||||||
|
|
||||||
DOCUMENTATION / IMAGES
|
DOCUMENTATION / IMAGES
|
||||||
|
|
||||||
|
Get good top-down, well-lit, plain-white-background photos of the Arduino boards.
|
||||||
Replace rainbow diagram on the guide to the board page.
|
Replace rainbow diagram on the guide to the board page.
|
||||||
Update pictures to use Arduino Diecimila: Guide/Board, tutorials,
|
Update pictures to use Arduino Diecimila: Guide/Board, tutorials,
|
||||||
Create diagrams and schematics for the examples.
|
Create diagrams and schematics for the examples.
|
||||||
@ -121,4 +124,8 @@ Consider deleting many of the pictures in the howto's as they just make it harde
|
|||||||
Tell people not to put the board on a Powerbook.
|
Tell people not to put the board on a Powerbook.
|
||||||
People don't know what a jumper is.
|
People don't know what a jumper is.
|
||||||
Add picture of the RX/TX LEDs flashing.
|
Add picture of the RX/TX LEDs flashing.
|
||||||
Show a picture of the LED flashing.
|
Show a picture of the LED flashing.
|
||||||
|
|
||||||
|
DOCUMENTATION / TROUBLESHOOTING
|
||||||
|
|
||||||
|
Add explanation of how to work around auto-reset.
|
Loading…
x
Reference in New Issue
Block a user