Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

solved a bug, provided 2 example

asicnI2C.ino does not work properly, use asicnI2Ctest2.ino for reference
(it use ADXL345)
  • Loading branch information...
commit 35456dbcb99724bbebb7755aca4b953d33f82252 1 parent af9c143
authored August 08, 2012
12  asincI2C/Wire.cpp
@@ -54,6 +54,11 @@ TwoWire::TwoWire()
54 54
 
55 55
 // Public Methods //////////////////////////////////////////////////////////////
56 56
 
  57
+void TwoWire::begin(void)
  58
+{
  59
+  begin(true, false);
  60
+}
  61
+
57 62
 void TwoWire::begin(boolean pullUp, boolean fast)
58 63
 {
59 64
   rxBufferIndex = 0;
@@ -197,14 +202,17 @@ uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop
197 202
   if(quantity > BUFFER_LENGTH){
198 203
     quantity = BUFFER_LENGTH;
199 204
   }
  205
+//  Serial.println("tw_r1");
200 206
   // perform blocking read into buffer
201 207
   uint8_t read = twi_askReadFrom(address, quantity, sendStop);
202  
-  
  208
+//  Serial.println("tw_r2");  
203 209
   /* ADDED BY LESTO FOR RETROCOMPATIBILITY */
204 210
   while (twi_AsincReadNotReady(quantity) != 0){
  211
+//    Serial.print("tw_err: ");
  212
+//    Serial.println( twi_AsincReadNotReady(quantity) );
205 213
     continue;
206 214
   }
207  
-  
  215
+//  Serial.println("tw_r3");
208 216
   read = twi_AsincReadFrom(rxBuffer, quantity);
209 217
   /* END ADD */
210 218
   
1  asincI2C/Wire.h
@@ -55,6 +55,7 @@ class TwoWire : public Stream
55 55
     void begin(int, boolean, boolean);
56 56
     void begin(uint8_t);
57 57
     void begin(int);
  58
+    void begin(void);
58 59
     void beginTransmission(uint8_t);
59 60
     void beginTransmission(int);
60 61
     uint8_t endTransmission(void);
172  asincI2C/asincI2C.ino
... ...
@@ -1,7 +1,175 @@
  1
+//fast test copyed from http://www.sparkfun.com/tutorials/265
  2
+//thank you sparkfun!
1 3
 
  4
+//The Wire library is used for I2C communication
  5
+#include <Wire.h>
2 6
 
3  
-void setup(){
  7
+//This is a list of registers in the ITG-3200. Registers are parameters that determine how the sensor will behave, or they can hold data that represent the
  8
+//sensors current status.
  9
+//To learn more about the registers on the ITG-3200, download and read the datasheet.
  10
+char WHO_AM_I = 0x00;
  11
+char SMPLRT_DIV= 0x15;
  12
+char DLPF_FS = 0x16;
  13
+char GYRO_XOUT_H = 0x1D;
  14
+char GYRO_XOUT_L = 0x1E;
  15
+char GYRO_YOUT_H = 0x1F;
  16
+char GYRO_YOUT_L = 0x20;
  17
+char GYRO_ZOUT_H = 0x21;
  18
+char GYRO_ZOUT_L = 0x22;
  19
+
  20
+//This is a list of settings that can be loaded into the registers.
  21
+//DLPF, Full Scale Register Bits
  22
+//FS_SEL must be set to 3 for proper operation
  23
+//Set DLPF_CFG to 3 for 1kHz Fint and 42 Hz Low Pass Filter
  24
+char DLPF_CFG_0 = 1<<0;
  25
+char DLPF_CFG_1 = 1<<1;
  26
+char DLPF_CFG_2 = 1<<2;
  27
+char DLPF_FS_SEL_0 = 1<<3;
  28
+char DLPF_FS_SEL_1 = 1<<4;
  29
+
  30
+//I2C devices each have an address. The address is defined in the datasheet for the device. The ITG-3200 breakout board can have different address depending on how
  31
+//the jumper on top of the board is configured. By default, the jumper is connected to the VDD pin. When the jumper is connected to the VDD pin the I2C address
  32
+//is 0x69.
  33
+char itgAddress = 0x69;
  34
+
  35
+
  36
+		//In the setup section of the sketch the serial port will be configured, the i2c communication will be initialized, and the itg-3200 will be configured.
  37
+void setup()
  38
+{
  39
+  //Create a serial connection using a 9600bps baud rate.
  40
+  Serial.begin(9600);
  41
+  delay(1000);
  42
+  //Initialize the I2C communication. This will set the Arduino up as the 'Master' device.
  43
+    Serial.println('1');
  44
+  Wire.begin();
  45
+    Serial.println('2');
  46
+  //Read the WHO_AM_I register and print the result
  47
+  char id=0; 
  48
+  id = itgRead(itgAddress, 0x00);  
  49
+  Serial.print("ID: ");
  50
+  Serial.println(id, HEX);
  51
+  //Configure the gyroscope
  52
+  //Set the gyroscope scale for the outputs to +/-2000 degrees per second
  53
+  itgWrite(itgAddress, DLPF_FS, (DLPF_FS_SEL_0|DLPF_FS_SEL_1|DLPF_CFG_0));
  54
+    Serial.println('3');
  55
+  //Set the sample rate to 100 hz
  56
+  itgWrite(itgAddress, SMPLRT_DIV, 9);
  57
+    Serial.println('4');
  58
+}
  59
+
  60
+		//The loop section of the sketch will read the X,Y and Z output rates from the gyroscope and output them in the Serial Terminal
  61
+void loop()
  62
+{
  63
+  //Create variables to hold the output rates.
  64
+//  Serial.println('a');
  65
+  int xRate, yRate, zRate;
  66
+
  67
+  //Read the x,y and z output rates from the gyroscope.
  68
+//  Serial.println('b');
  69
+  xRate = readX();
  70
+  yRate = readY();
  71
+  zRate = readZ();
  72
+//  Serial.println('c');
  73
+
  74
+  //Print the output rates to the terminal, seperated by a TAB character.
  75
+  Serial.print(xRate);
  76
+  Serial.print('\t');
  77
+  Serial.print(yRate);
  78
+  Serial.print('\t');
  79
+  Serial.println(zRate);  
  80
+
  81
+  //Wait 10ms before reading the values again. (Remember, the output rate was set to 100hz and 1reading per 10ms = 100hz.)
  82
+  delay(10);
  83
+}
  84
+
  85
+		//This function will write a value to a register on the itg-3200.
  86
+//Parameters:
  87
+//  char address: The I2C address of the sensor. For the ITG-3200 breakout the address is 0x69.
  88
+//  char registerAddress: The address of the register on the sensor that should be written to.
  89
+//  char data: The value to be written to the specified register.
  90
+void itgWrite(char address, char registerAddress, char data)
  91
+{
  92
+  //Initiate a communication sequence with the desired i2c device
  93
+  Wire.beginTransmission(address);
  94
+  //Tell the I2C address which register we are writing to
  95
+  Wire.write(registerAddress);
  96
+  //Send the value to write to the specified register
  97
+  Wire.write(data);
  98
+  //End the communication sequence
  99
+  Wire.endTransmission();
  100
+}
  101
+
  102
+//This function will read the data from a specified register on the ITG-3200 and return the value.
  103
+//Parameters:
  104
+//  char address: The I2C address of the sensor. For the ITG-3200 breakout the address is 0x69.
  105
+//  char registerAddress: The address of the register on the sensor that should be read
  106
+//Return:
  107
+//  unsigned char: The value currently residing in the specified register
  108
+unsigned char itgRead(char address, char registerAddress)
  109
+{
  110
+  //This variable will hold the contents read from the i2c device.
  111
+  unsigned char data=0;
  112
+//  Serial.println("r1");
  113
+  //Send the register address to be read.
  114
+  Wire.beginTransmission(address);
  115
+  //Send the Register Address
  116
+//  Serial.println("r2");
  117
+  Wire.write(registerAddress);
  118
+  //End the communication sequence.
  119
+//  Serial.println("r3");
  120
+  Wire.endTransmission();
  121
+//  Serial.println("r4");
  122
+  //Ask the I2C device for data
  123
+  Wire.beginTransmission(address);
  124
+ // Serial.println("r4.1");
  125
+  Wire.requestFrom(address, 1);
  126
+//  Serial.println("r5");
  127
+  //Wait for a response from the I2C device
  128
+  if(Wire.available()){
  129
+ //   Serial.println("r6");
  130
+    //Save the data sent from the I2C device
  131
+    data = Wire.read();
  132
+  }
  133
+  
  134
+  //End the communication sequence.
  135
+  Wire.endTransmission();
  136
+//  Serial.println("r7");
  137
+  //Return the data read during the operation
  138
+  return data;
  139
+}
  140
+
  141
+//This function is used to read the X-Axis rate of the gyroscope. The function returns the ADC value from the Gyroscope
  142
+//NOTE: This value is NOT in degrees per second. 
  143
+//Usage: int xRate = readX();
  144
+int readX(void)
  145
+{
  146
+  int data=0;
  147
+  data = itgRead(itgAddress, GYRO_XOUT_H)<<8;
  148
+  data |= itgRead(itgAddress, GYRO_XOUT_L);  
  149
+  
  150
+  return data;
  151
+}
  152
+
  153
+//This function is used to read the Y-Axis rate of the gyroscope. The function returns the ADC value from the Gyroscope
  154
+//NOTE: This value is NOT in degrees per second. 
  155
+//Usage: int yRate = readY();
  156
+int readY(void)
  157
+{
  158
+  int data=0;
  159
+  data = itgRead(itgAddress, GYRO_YOUT_H)<<8;
  160
+  data |= itgRead(itgAddress, GYRO_YOUT_L);  
  161
+  
  162
+  return data;
4 163
 }
5 164
 
6  
-void loop(){
  165
+//This function is used to read the Z-Axis rate of the gyroscope. The function returns the ADC value from the Gyroscope
  166
+//NOTE: This value is NOT in degrees per second. 
  167
+//Usage: int zRate = readZ();
  168
+int readZ(void)
  169
+{
  170
+  int data=0;
  171
+  data = itgRead(itgAddress, GYRO_ZOUT_H)<<8;
  172
+  data |= itgRead(itgAddress, GYRO_ZOUT_L);  
  173
+  
  174
+  return data;
7 175
 }
8  asincI2C/twi.c
@@ -72,11 +72,9 @@ static boolean twi_receiving, twi_writing;
72 72
 void twi_init(boolean pullUp, boolean fastI2C)
73 73
 {
74 74
   unsigned long frequency = 100000;
75  
-  if (fastI2C){
  75
+
  76
+  if (fastI2C)
76 77
     frequency = 400000;
77  
-  }else{
78  
-    frequency = 100000;
79  
-  }
80 78
   
81 79
   // initialize state
82 80
   twi_state = TWI_READY;
@@ -215,7 +213,7 @@ uint8_t twi_AsincReadNotReady(uint8_t length){
215 213
     return 2; //error, waiting for right status
216 214
 
217 215
   //wait until all data arrive
218  
-  if (twi_masterBufferIndex < length){
  216
+  if (twi_masterBufferIndex < length-1){
219 217
     return 3; //error, missing some data
220 218
   }
221 219
   
437  asincI2Ctest2/Wire.cpp
... ...
@@ -0,0 +1,437 @@
  1
+/*
  2
+  TwoWire.cpp - TWI/I2C library for Wiring & Arduino
  3
+  Copyright (c) 2006 Nicholas Zambetti.  All right reserved.
  4
+  
  5
+  Modifierd by Mauro Mombelli (aka lesto) - 2012
  6
+    now the library should has asincronus read/write.
  7
+    
  8
+  This library is free software; you can redistribute it and/or
  9
+  modify it under the terms of the GNU Lesser General Public
  10
+  License as published by the Free Software Foundation; either
  11
+  version 2.1 of the License, or (at your option) any later version.
  12
+
  13
+  This library is distributed in the hope that it will be useful,
  14
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16
+  Lesser General Public License for more details.
  17
+
  18
+  You should have received a copy of the GNU Lesser General Public
  19
+  License along with this library; if not, write to the Free Software
  20
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  21
+ 
  22
+  Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
  23
+*/
  24
+
  25
+extern "C" {
  26
+  #include <stdlib.h>
  27
+  #include <string.h>
  28
+  #include <inttypes.h>
  29
+  #include "twi.h"
  30
+}
  31
+
  32
+#include "Wire.h"
  33
+
  34
+// Initialize Class Variables //////////////////////////////////////////////////
  35
+
  36
+uint8_t TwoWire::rxBuffer[BUFFER_LENGTH];
  37
+uint8_t TwoWire::rxBufferIndex = 0;
  38
+uint8_t TwoWire::rxBufferLength = 0;
  39
+
  40
+uint8_t TwoWire::txAddress = 0;
  41
+uint8_t TwoWire::txBuffer[BUFFER_LENGTH];
  42
+uint8_t TwoWire::txBufferIndex = 0;
  43
+uint8_t TwoWire::txBufferLength = 0;
  44
+
  45
+uint8_t TwoWire::transmitting = 0;
  46
+void (*TwoWire::user_onRequest)(void);
  47
+void (*TwoWire::user_onReceive)(int);
  48
+
  49
+// Constructors ////////////////////////////////////////////////////////////////
  50
+
  51
+TwoWire::TwoWire()
  52
+{
  53
+}
  54
+
  55
+// Public Methods //////////////////////////////////////////////////////////////
  56
+
  57
+void TwoWire::begin(void)
  58
+{
  59
+  begin(true, false);
  60
+}
  61
+
  62
+void TwoWire::begin(boolean pullUp, boolean fast)
  63
+{
  64
+  rxBufferIndex = 0;
  65
+  rxBufferLength = 0;
  66
+
  67
+  txBufferIndex = 0;
  68
+  txBufferLength = 0;
  69
+
  70
+  twi_init(pullUp, fast);
  71
+}
  72
+
  73
+void TwoWire::begin(uint8_t address, boolean pullUp, boolean fast)
  74
+{
  75
+  twi_setAddress(address);
  76
+  twi_attachSlaveTxEvent(onRequestService);
  77
+  twi_attachSlaveRxEvent(onReceiveService);
  78
+  begin(pullUp, fast);
  79
+}
  80
+
  81
+void TwoWire::begin(int address, boolean pullUp, boolean fast)
  82
+{
  83
+  begin((uint8_t)address, pullUp, fast);
  84
+}
  85
+
  86
+void TwoWire::begin(uint8_t address)
  87
+{
  88
+  twi_setAddress(address);
  89
+  twi_attachSlaveTxEvent(onRequestService);
  90
+  twi_attachSlaveRxEvent(onReceiveService);
  91
+  begin(true, false); //modded by lesto for retrocompatibility
  92
+}
  93
+
  94
+void TwoWire::begin(int address)
  95
+{
  96
+  begin((uint8_t)address, true, false); //modded by lesto for retrocompatibility
  97
+}
  98
+
  99
+/*ADDED BY LESTO*/
  100
+
  101
+uint8_t TwoWire::asincRequestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop)
  102
+{
  103
+  // clamp to buffer length
  104
+  if(quantity > BUFFER_LENGTH){
  105
+    quantity = BUFFER_LENGTH;
  106
+  }
  107
+  // perform unblocking read into buffer
  108
+  uint8_t read = twi_askReadFrom(address, quantity, sendStop);
  109
+  // set rx buffer iterator vars
  110
+  rxBufferIndex = 0;
  111
+  rxBufferLength = 0;
  112
+
  113
+  return read;
  114
+}
  115
+
  116
+// can be called after asincRequestFrom(address, numBytes)
  117
+// not retrocompatible
  118
+uint8_t TwoWire::asincReady(uint8_t quantity)
  119
+{
  120
+  if ( twi_AsincReadNotReady(quantity) )
  121
+    return 0;
  122
+  return quantity;
  123
+}
  124
+
  125
+// must be called after asincRequestFrom(address, numBytes)
  126
+int TwoWire::asincRead(uint8_t *data, uint8_t quantity)
  127
+{
  128
+  int value = -1;
  129
+  
  130
+  if ( asincReady(quantity) ){
  131
+    value = twi_AsincReadFrom(data, quantity);
  132
+  }
  133
+  
  134
+  return value;
  135
+}
  136
+
  137
+uint8_t TwoWire::asincRequestResponse(uint8_t address, uint8_t quantity, uint8_t sendStop)
  138
+{
  139
+  // clamp to buffer length
  140
+  if(quantity > BUFFER_LENGTH){
  141
+    quantity = BUFFER_LENGTH;
  142
+  }
  143
+  // perform unbloking read into buffer
  144
+  uint8_t read = twi_askReadFrom(address, quantity, sendStop);
  145
+  // set rx buffer iterator vars
  146
+  rxBufferIndex = 0;
  147
+  rxBufferLength = 0;
  148
+
  149
+  return read;
  150
+}
  151
+
  152
+uint8_t TwoWire::asincRequestFrom(uint8_t address, uint8_t quantity)
  153
+{
  154
+  return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
  155
+}
  156
+
  157
+uint8_t TwoWire::asincRequestFrom(int address, int quantity)
  158
+{
  159
+  return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
  160
+}
  161
+
  162
+uint8_t TwoWire::asincRequestFrom(int address, int quantity, int sendStop)
  163
+{
  164
+  return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)sendStop);
  165
+}
  166
+
  167
+uint8_t TwoWire::asincEndTransmission(uint8_t sendStop)
  168
+{
  169
+  // transmit buffer (blocking)
  170
+  int8_t ret = twi_AskWriteTo(txAddress, txBuffer, txBufferLength, 1, sendStop);
  171
+  
  172
+  // reset tx buffer iterator vars
  173
+  txBufferIndex = 0;
  174
+  txBufferLength = 0;
  175
+  // indicate that we are done transmitting
  176
+  transmitting = 0;
  177
+  return ret;
  178
+}
  179
+
  180
+uint8_t TwoWire::asincBeginTransmission(uint8_t address)
  181
+{
  182
+  if ( twi_AsincWriteNotReady() == 6 ){
  183
+    return 0; //error!
  184
+  }
  185
+  
  186
+  // indicate that we are transmitting
  187
+  transmitting = 1;
  188
+  // set address of targeted slave
  189
+  txAddress = address;
  190
+  // reset tx buffer iterator vars
  191
+  txBufferIndex = 0;
  192
+  txBufferLength = 0;
  193
+  
  194
+  return 1; //ok
  195
+}
  196
+
  197
+/* END ADDED BY LESTO */
  198
+
  199
+uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop)
  200
+{
  201
+  // clamp to buffer length
  202
+  if(quantity > BUFFER_LENGTH){
  203
+    quantity = BUFFER_LENGTH;
  204
+  }
  205
+//  Serial.println("tw_r1");
  206
+  // perform blocking read into buffer
  207
+  uint8_t read = twi_askReadFrom(address, quantity, sendStop);
  208
+//  Serial.println("tw_r2");  
  209
+  /* ADDED BY LESTO FOR RETROCOMPATIBILITY */
  210
+  while (twi_AsincReadNotReady(quantity) != 0){
  211
+//    Serial.print("tw_err: ");
  212
+//    Serial.println( twi_AsincReadNotReady(quantity) );
  213
+    continue;
  214
+  }
  215
+//  Serial.println("tw_r3");
  216
+  read = twi_AsincReadFrom(rxBuffer, quantity);
  217
+  /* END ADD */
  218
+  
  219
+  // set rx buffer iterator vars
  220
+  rxBufferIndex = 0;
  221
+  rxBufferLength = read;
  222
+
  223
+  return read;
  224
+}
  225
+
  226
+uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity)
  227
+{
  228
+  return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
  229
+}
  230
+
  231
+uint8_t TwoWire::requestFrom(int address, int quantity)
  232
+{
  233
+  return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
  234
+}
  235
+
  236
+uint8_t TwoWire::requestFrom(int address, int quantity, int sendStop)
  237
+{
  238
+  return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)sendStop);
  239
+}
  240
+
  241
+void TwoWire::beginTransmission(uint8_t address)
  242
+{
  243
+  // indicate that we are transmitting
  244
+  transmitting = 1;
  245
+  // set address of targeted slave
  246
+  txAddress = address;
  247
+  // reset tx buffer iterator vars
  248
+  txBufferIndex = 0;
  249
+  txBufferLength = 0;
  250
+}
  251
+
  252
+void TwoWire::beginTransmission(int address)
  253
+{
  254
+  beginTransmission((uint8_t)address);
  255
+}
  256
+
  257
+//
  258
+//	Originally, 'endTransmission' was an f(void) function.
  259
+//	It has been modified to take one parameter indicating
  260
+//	whether or not a STOP should be performed on the bus.
  261
+//	Calling endTransmission(false) allows a sketch to 
  262
+//	perform a repeated start. 
  263
+//
  264
+//	WARNING: Nothing in the library keeps track of whether
  265
+//	the bus tenure has been properly ended with a STOP. It
  266
+//	is very possible to leave the bus in a hung state if
  267
+//	no call to endTransmission(true) is made. Some I2C
  268
+//	devices will behave oddly if they do not see a STOP.
  269
+//
  270
+uint8_t TwoWire::endTransmission(uint8_t sendStop)
  271
+{
  272
+  // transmit buffer (blocking)
  273
+  int8_t ret = twi_AskWriteTo(txAddress, txBuffer, txBufferLength, 1, sendStop);
  274
+  
  275
+  /* ADDED BY LESTO FOR RETROCOMPATIBILITY */
  276
+  while ( twi_AsincWriteNotReady() == 6 ){
  277
+    continue;
  278
+  }
  279
+  /* END ADD */
  280
+  
  281
+  // reset tx buffer iterator vars
  282
+  txBufferIndex = 0;
  283
+  txBufferLength = 0;
  284
+  // indicate that we are done transmitting
  285
+  transmitting = 0;
  286
+  return ret;
  287
+}
  288
+
  289
+//	This provides backwards compatibility with the original
  290
+//	definition, and expected behaviour, of endTransmission
  291
+//
  292
+uint8_t TwoWire::endTransmission(void)
  293
+{
  294
+  return endTransmission(true);
  295
+}
  296
+
  297
+// must be called in:
  298
+// slave tx event callback
  299
+// or after beginTransmission(address)
  300
+size_t TwoWire::write(uint8_t data)
  301
+{
  302
+  if(transmitting){
  303
+  // in master transmitter mode
  304
+    // don't bother if buffer is full
  305
+    if(txBufferLength >= BUFFER_LENGTH){
  306
+      setWriteError();
  307
+      return 0;
  308
+    }
  309
+    // put byte in tx buffer
  310
+    txBuffer[txBufferIndex] = data;
  311
+    ++txBufferIndex;
  312
+    // update amount in buffer   
  313
+    txBufferLength = txBufferIndex;
  314
+  }else{
  315
+  // in slave send mode
  316
+    // reply to master
  317
+    twi_transmit(&data, 1);
  318
+  }
  319
+  return 1;
  320
+}
  321
+
  322
+// must be called in:
  323
+// slave tx event callback
  324
+// or after beginTransmission(address)
  325
+size_t TwoWire::write(const uint8_t *data, size_t quantity)
  326
+{
  327
+  if(transmitting){
  328
+  // in master transmitter mode
  329
+    for(size_t i = 0; i < quantity; ++i){
  330
+      write(data[i]);
  331
+    }
  332
+  }else{
  333
+  // in slave send mode
  334
+    // reply to master
  335
+    twi_transmit(data, quantity);
  336
+  }
  337
+  return quantity;
  338
+}
  339
+
  340
+// must be called in:
  341
+// slave rx event callback
  342
+// or after requestFrom(address, numBytes)
  343
+int TwoWire::available(void)
  344
+{
  345
+  return rxBufferLength - rxBufferIndex;
  346
+}
  347
+
  348
+// must be called in:
  349
+// slave rx event callback
  350
+// or after requestFrom(address, numBytes)
  351
+int TwoWire::read(void)
  352
+{
  353
+  int value = -1;
  354
+  
  355
+  // get each successive byte on each call
  356
+  if(rxBufferIndex < rxBufferLength){
  357
+    value = rxBuffer[rxBufferIndex];
  358
+    ++rxBufferIndex;
  359
+  }
  360
+
  361
+  return value;
  362
+}
  363
+
  364
+// must be called in:
  365
+// slave rx event callback
  366
+// or after requestFrom(address, numBytes)
  367
+int TwoWire::peek(void)
  368
+{
  369
+  int value = -1;
  370
+  
  371
+  if(rxBufferIndex < rxBufferLength){
  372
+    value = rxBuffer[rxBufferIndex];
  373
+  }
  374
+
  375
+  return value;
  376
+}
  377
+
  378
+void TwoWire::flush(void)
  379
+{
  380
+  // XXX: to be implemented.
  381
+}
  382
+
  383
+// behind the scenes function that is called when data is received
  384
+void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes)
  385
+{
  386
+  // don't bother if user hasn't registered a callback
  387
+  if(!user_onReceive){
  388
+    return;
  389
+  }
  390
+  // don't bother if rx buffer is in use by a master requestFrom() op
  391
+  // i know this drops data, but it allows for slight stupidity
  392
+  // meaning, they may not have read all the master requestFrom() data yet
  393
+  if(rxBufferIndex < rxBufferLength){
  394
+    return;
  395
+  }
  396
+  // copy twi rx buffer into local read buffer
  397
+  // this enables new reads to happen in parallel
  398
+  for(uint8_t i = 0; i < numBytes; ++i){
  399
+    rxBuffer[i] = inBytes[i];    
  400
+  }
  401
+  // set rx iterator vars
  402
+  rxBufferIndex = 0;
  403
+  rxBufferLength = numBytes;
  404
+  // alert user program
  405
+  user_onReceive(numBytes);
  406
+}
  407
+
  408
+// behind the scenes function that is called when data is requested
  409
+void TwoWire::onRequestService(void)
  410
+{
  411
+  // don't bother if user hasn't registered a callback
  412
+  if(!user_onRequest){
  413
+    return;
  414
+  }
  415
+  // reset tx buffer iterator vars
  416
+  // !!! this will kill any pending pre-master sendTo() activity
  417
+  txBufferIndex = 0;
  418
+  txBufferLength = 0;
  419
+  // alert user program
  420
+  user_onRequest();
  421
+}
  422
+
  423
+// sets function called on slave write
  424
+void TwoWire::onReceive( void (*function)(int) )
  425
+{
  426
+  user_onReceive = function;
  427
+}
  428
+
  429
+// sets function called on slave read
  430
+void TwoWire::onRequest( void (*function)(void) )
  431
+{
  432
+  user_onRequest = function;
  433
+}
  434
+
  435
+// Preinstantiate Objects //////////////////////////////////////////////////////
  436
+
  437
+TwoWire Wire = TwoWire();
97  asincI2Ctest2/Wire.h
... ...
@@ -0,0 +1,97 @@
  1
+/*
  2
+  TwoWire.h - TWI/I2C library for Arduino & Wiring
  3
+  Copyright (c) 2006 Nicholas Zambetti.  All right reserved.
  4
+  
  5
+  Modifierd by Mauro Mombelli (aka lesto) - 2012
  6
+    now the library should has asincronus read/write.
  7
+    
  8
+  This library is free software; you can redistribute it and/or
  9
+  modify it under the terms of the GNU Lesser General Public
  10
+  License as published by the Free Software Foundation; either
  11
+  version 2.1 of the License, or (at your option) any later version.
  12
+
  13
+  This library is distributed in the hope that it will be useful,
  14
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16
+  Lesser General Public License for more details.
  17
+
  18
+  You should have received a copy of the GNU Lesser General Public
  19
+  License along with this library; if not, write to the Free Software
  20
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  21
+
  22
+  Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
  23
+*/
  24
+
  25
+#ifndef TwoWire_h
  26
+#define TwoWire_h
  27
+
  28
+#include <inttypes.h>
  29
+#include "Stream.h"
  30
+#include "Arduino.h"
  31
+
  32
+#define BUFFER_LENGTH 32
  33
+
  34
+class TwoWire : public Stream
  35
+{
  36
+  private:
  37
+    static uint8_t rxBuffer[];
  38
+    static uint8_t rxBufferIndex;
  39
+    static uint8_t rxBufferLength;
  40
+
  41
+    static uint8_t txAddress;
  42
+    static uint8_t txBuffer[];
  43
+    static uint8_t txBufferIndex;
  44
+    static uint8_t txBufferLength;
  45
+
  46
+    static uint8_t transmitting;
  47
+    static void (*user_onRequest)(void);
  48
+    static void (*user_onReceive)(int);
  49
+    static void onRequestService(void);
  50
+    static void onReceiveService(uint8_t*, int);
  51
+  public:
  52
+    TwoWire();
  53
+    void begin(boolean, boolean);
  54
+    void begin(uint8_t, boolean, boolean);
  55
+    void begin(int, boolean, boolean);
  56
+    void begin(uint8_t);
  57
+    void begin(int);
  58
+    void begin(void);
  59
+    void beginTransmission(uint8_t);
  60
+    void beginTransmission(int);
  61
+    uint8_t endTransmission(void);
  62
+    uint8_t endTransmission(uint8_t);
  63
+    uint8_t requestFrom(uint8_t, uint8_t);
  64
+    uint8_t requestFrom(uint8_t, uint8_t, uint8_t);
  65
+    uint8_t requestFrom(int, int);
  66
+    uint8_t requestFrom(int, int, int);
  67
+    virtual size_t write(uint8_t);
  68
+    virtual size_t write(const uint8_t *, size_t);
  69
+    virtual int available(void);
  70
+    virtual int read(void);
  71
+    virtual int peek(void);
  72
+	virtual void flush(void);
  73
+    void onReceive( void (*)(int) );
  74
+    void onRequest( void (*)(void) );
  75
+  
  76
+    inline size_t write(unsigned long n) { return write((uint8_t)n); }
  77
+    inline size_t write(long n) { return write((uint8_t)n); }
  78
+    inline size_t write(unsigned int n) { return write((uint8_t)n); }
  79
+    inline size_t write(int n) { return write((uint8_t)n); }
  80
+    using Print::write;
  81
+    
  82
+    uint8_t asincRequestFrom(uint8_t, uint8_t, uint8_t);
  83
+    uint8_t asincRequestFrom(uint8_t address, uint8_t quantity);
  84
+    uint8_t asincRequestFrom(int address, int quantity);
  85
+    uint8_t asincRequestFrom(int address, int quantity, int sendStop);
  86
+    uint8_t asincEndTransmission(uint8_t sendStop);
  87
+    uint8_t asincBeginTransmission(uint8_t address);
  88
+    
  89
+    uint8_t asincReady(uint8_t);
  90
+    int asincRead(uint8_t *, uint8_t);
  91
+    uint8_t asincRequestResponse(uint8_t, uint8_t, uint8_t);
  92
+
  93
+};
  94
+
  95
+extern TwoWire Wire;
  96
+
  97
+#endif
103  asincI2Ctest2/asincI2Ctest2.ino
... ...
@@ -0,0 +1,103 @@
  1
+// Bare bones ADXL345 i2c example for Arduino 1.0
  2
+// by Jens C Brynildsen <http://www.flashgamer.com>
  3
+// This version is not reliant of any external lib
  4
+
  5
+// Demonstrates use of ADXL345 (using the Sparkfun ADXL345 breakout) with i2c communication
  6
+// Datasheet http://www.sparkfun.com/datasheets/Sensors/Accelerometer/ADXL345.pdf
  7
+// If you need more advanced features such as freefall and tap detection, check out
  8
+// https://github.com/jenschr/Arduino-libraries
  9
+// (Adapted for Arduino 1.0 from http://code.google.com/p/adxl345driver)
  10
+
  11
+// Cabling for i2c using Sparkfun breakout with an Arduino Uno / Duemilanove:
  12
+// Arduino <-> Breakout board
  13
+// Gnd      -  GND
  14
+// 3.3v     -  VCC
  15
+// 3.3v     -  CS
  16
+// Analog 4 -  SDA
  17
+// Analog 5 -  SLC
  18
+
  19
+// Cabling for i2c using Sparkfun breakout with an Arduino Mega / Mega ADK:
  20
+// Arduino <-> Breakout board
  21
+// Gnd      -  GND
  22
+// 3.3v     -  VCC
  23
+// 3.3v     -  CS
  24
+// 20       -  SDA
  25
+// 21       -  SLC
  26
+
  27
+// This example code is in the public domain.
  28
+
  29
+#include "Wire.h"
  30
+
  31
+#define DEVICE (0x53) // Device address as specified in data sheet 
  32
+
  33
+byte _buff[6];
  34
+
  35
+char POWER_CTL = 0x2D;	//Power Control Register
  36
+char DATA_FORMAT = 0x31;
  37
+char DATAX0 = 0x32;	//X-Axis Data 0
  38
+char DATAX1 = 0x33;	//X-Axis Data 1
  39
+char DATAY0 = 0x34;	//Y-Axis Data 0
  40
+char DATAY1 = 0x35;	//Y-Axis Data 1
  41
+char DATAZ0 = 0x36;	//Z-Axis Data 0
  42
+char DATAZ1 = 0x37;	//Z-Axis Data 1
  43
+
  44
+void setup()
  45
+{
  46
+  Wire.begin();        // join i2c bus (address optional for master)
  47
+  Serial.begin(9600);  // start serial for output. Make sure you set your Serial Monitor to the same!
  48
+  delay(1000);
  49
+  Serial.print("init");
  50
+  
  51
+  //Put the ADXL345 into +/- 4G range by writing the value 0x01 to the DATA_FORMAT register.
  52
+  writeTo(DATA_FORMAT, 0x01);
  53
+  //Put the ADXL345 into Measurement Mode by writing 0x08 to the POWER_CTL register.
  54
+  writeTo(POWER_CTL, 0x08);
  55
+}
  56
+
  57
+void loop()
  58
+{
  59
+  readAccel(); // read the x/y/z tilt
  60
+  delay(500); // only read every 0,5 seconds
  61
+}
  62
+
  63
+void readAccel() {
  64
+  uint8_t howManyBytesToRead = 6;
  65
+  readFrom( DATAX0, howManyBytesToRead, _buff); //read the acceleration data from the ADXL345
  66
+
  67
+  // each axis reading comes in 10 bit resolution, ie 2 bytes.  Least Significat Byte first!!
  68
+  // thus we are converting both bytes in to one int
  69
+  int x = (((int)_buff[1]) << 8) | _buff[0];   
  70
+  int y = (((int)_buff[3]) << 8) | _buff[2];
  71
+  int z = (((int)_buff[5]) << 8) | _buff[4];
  72
+  Serial.print("x: ");
  73
+  Serial.print( x );
  74
+  Serial.print(" y: ");
  75
+  Serial.print( y );
  76
+  Serial.print(" z: ");
  77
+  Serial.println( z );
  78
+}
  79
+
  80
+void writeTo(byte address, byte val) {
  81
+  Wire.beginTransmission(DEVICE); // start transmission to device 
  82
+  Wire.write(address);             // send register address
  83
+  Wire.write(val);                 // send value to write
  84
+  Wire.endTransmission();         // end transmission
  85
+}
  86
+
  87
+// Reads num bytes starting from address register on device in to _buff array
  88
+void readFrom(byte address, int num, byte _buff[]) {
  89
+  Wire.beginTransmission(DEVICE); // start transmission to device 
  90
+  Wire.write(address);             // sends address to read from
  91
+  Wire.endTransmission();         // end transmission
  92
+
  93
+  Wire.beginTransmission(DEVICE); // start transmission to device
  94
+  Wire.requestFrom(DEVICE, num);    // request 6 bytes from device
  95
+
  96
+  int i = 0;
  97
+  while(Wire.available())         // device may send less than requested (abnormal)
  98
+  { 
  99
+    _buff[i] = Wire.read();    // receive a byte
  100
+    i++;
  101
+  }
  102
+  Wire.endTransmission();         // end transmission
  103
+}
647  asincI2Ctest2/twi.c
... ...
@@ -0,0 +1,647 @@
  1
+/*
  2
+  twi.c - TWI/I2C library for Wiring & Arduino
  3
+  Copyright (c) 2006 Nicholas Zambetti.  All right reserved.
  4
+  
  5
+  Modifierd by Mauro Mombelli (aka lesto) - 2012
  6
+    now the library should has asincronus read/write.
  7
+  
  8
+  This library is free software; you can redistribute it and/or
  9
+  modify it under the terms of the GNU Lesser General Public
  10
+  License as published by the Free Software Foundation; either
  11
+  version 2.1 of the License, or (at your option) any later version.
  12
+
  13
+  This library is distributed in the hope that it will be useful,
  14
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16
+  Lesser General Public License for more details.
  17
+
  18
+  You should have received a copy of the GNU Lesser General Public
  19
+  License along with this library; if not, write to the Free Software
  20
+  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  21
+
  22
+  Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
  23
+*/
  24
+
  25
+#include <math.h>
  26
+#include <stdlib.h>
  27
+#include <inttypes.h>
  28
+#include <avr/io.h>
  29
+#include <avr/interrupt.h>
  30
+#include <compat/twi.h>
  31
+
  32
+#ifndef cbi
  33
+#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
  34
+#endif
  35
+
  36
+#ifndef sbi
  37
+#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
  38
+#endif
  39
+
  40
+#include "pins_arduino.h"
  41
+#include "twi.h"
  42
+
  43
+static volatile uint8_t twi_state;
  44
+static volatile uint8_t twi_slarw;
  45
+static volatile uint8_t twi_sendStop;			// should the transaction end with a stop
  46
+static volatile uint8_t twi_inRepStart;			// in the middle of a repeated start
  47
+
  48
+static void (*twi_onSlaveTransmit)(void);
  49
+static void (*twi_onSlaveReceive)(uint8_t*, int);
  50
+
  51
+static uint8_t twi_masterBuffer[TWI_BUFFER_LENGTH];
  52
+static volatile uint8_t twi_masterBufferIndex;
  53
+static volatile uint8_t twi_masterBufferLength;
  54
+
  55
+static uint8_t twi_txBuffer[TWI_BUFFER_LENGTH];
  56
+static volatile uint8_t twi_txBufferIndex;
  57
+static volatile uint8_t twi_txBufferLength;
  58
+
  59
+static uint8_t twi_rxBuffer[TWI_BUFFER_LENGTH];
  60
+static volatile uint8_t twi_rxBufferIndex;
  61
+
  62
+static volatile uint8_t twi_error;
  63
+static boolean twi_receiving, twi_writing;
  64
+
  65
+/* 
  66
+ * Function twi_init
  67
+ * Desc     readys twi pins and sets twi bitrate
  68
+ * Input    pullUp:  true if you want use internal pull-up (added by lesto)
  69
+ *          fastI2C: true if you want use fast I2C (400.000Hz instead 100.000Hz, sensor have to support it) (added by lesto)
  70
+ * Output   none
  71
+ */
  72
+void twi_init(boolean pullUp, boolean fastI2C)
  73
+{
  74
+  unsigned long frequency = 100000;
  75
+  if (fastI2C){
  76
+    frequency = 400000;
  77
+  }else{
  78
+    frequency = 100000;
  79
+  }
  80
+  
  81
+  // initialize state
  82
+  twi_state = TWI_READY;
  83
+  twi_sendStop = true;		// default value
  84
+  twi_inRepStart = false;
  85
+  
  86
+  // activate internal pullups for twi.
  87
+  if (pullUp){
  88
+    sbi(SDA, 1);
  89
+    sbi(SCL, 1);
  90
+  }
  91
+
  92
+  // initialize twi prescaler and bit rate
  93
+  cbi(TWSR, TWPS0);
  94
+  cbi(TWSR, TWPS1);
  95
+  TWBR = ((F_CPU / frequency) - 16) / 2;
  96
+
  97
+  /* twi bit rate formula from atmega128 manual pg 204
  98
+  SCL Frequency = CPU Clock Frequency / (16 + (2 * TWBR))
  99
+  note: TWBR should be 10 or higher for master mode
  100
+  It is 72 for a 16mhz Wiring board with 100kHz TWI */
  101
+
  102
+  // enable twi module, acks, and twi interrupt
  103
+  TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA);
  104
+}
  105
+
  106
+/* 
  107
+ * Function twi_slaveInit
  108
+ * Desc     sets slave address and enables interrupt
  109
+ * Input    none
  110
+ * Output   none
  111
+ */
  112
+void twi_setAddress(uint8_t address)
  113
+{
  114
+  // set twi slave address (skip over TWGCE bit)
  115
+  TWAR = address << 1;
  116
+}
  117
+
  118
+/* 
  119
+ * Function twi_askReadFrom (modded by twi_ReadFrom)
  120
+ * Desc     attempts to become twi bus master and begin the transaction to read a series of bytes from a device on the bus
  121
+ * Input    address: 7bit i2c device address
  122
+ *          data: pointer to byte array (removed by lesto, use twi_AsincReadFrom)
  123
+ *          length: number of bytes to read into array
  124
+ *          sendStop: Boolean indicating whether to send a stop at the end
  125
+ * Output   0 .. success
  126
+ *          1 .. length to long for buffer
  127
+ *          2 .. status is not READY
  128
+ */
  129
+uint8_t twi_askReadFrom(uint8_t address, uint8_t length, uint8_t sendStop)
  130
+{
  131
+  uint8_t i;
  132
+
  133
+  // ensure data will fit into buffer
  134
+  if(TWI_BUFFER_LENGTH < length){
  135
+    return 1;
  136
+  }
  137
+
  138
+  // wait until twi is ready, become master receiver
  139
+  /* WAS
  140
+  while(TWI_READY != twi_state){
  141
+    continue;
  142
+  }
  143
+  NOW IS (by Lesto)*/
  144
+  if(TWI_READY != twi_state){
  145
+    return 2;
  146
+  }
  147
+  twi_receiving = true;
  148
+  /*END */
  149
+  
  150
+  twi_state = TWI_MRX;
  151
+  twi_sendStop = sendStop;
  152
+  // reset error state (0xFF.. no error occured)
  153
+  twi_error = 0xFF;
  154
+
  155
+  // initialize buffer iteration vars
  156
+  twi_masterBufferIndex = 0;
  157
+  twi_masterBufferLength = length-1;  // This is not intuitive, read on...
  158
+  // On receive, the previously configured ACK/NACK setting is transmitted in
  159
+  // response to the received byte before the interrupt is signalled. 
  160
+  // Therefor we must actually set NACK when the _next_ to last byte is
  161
+  // received, causing that NACK to be sent in response to receiving the last
  162
+  // expected byte of data.
  163
+
  164
+  // build sla+w, slave device address + w bit
  165
+  twi_slarw = TW_READ;
  166
+  twi_slarw |= address << 1;
  167
+
  168
+  if (true == twi_inRepStart) {
  169
+    // if we're in the repeated start state, then we've already sent the start,
  170
+    // (@@@ we hope), and the TWI statemachine is just waiting for the address byte.
  171
+    // We need to remove ourselves from the repeated start state before we enable interrupts,
  172
+    // since the ISR is ASYNC, and we could get confused if we hit the ISR before cleaning
  173
+    // up. Also, don't enable the START interrupt. There may be one pending from the 
  174
+    // repeated start that we sent outselves, and that would really confuse things.
  175
+    twi_inRepStart = false;			// remember, we're dealing with an ASYNC ISR
  176
+    TWDR = twi_slarw;
  177
+    TWCR = _BV(TWINT) | _BV(TWEA) | _BV(TWEN) | _BV(TWIE);	// enable INTs, but not START
  178
+  }
  179
+  else
  180
+    // send start condition
  181
+    TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA);
  182
+
  183
+  // wait for read operation to complete
  184
+  /* WAS
  185
+  while(TWI_MRX == twi_state){
  186
+    continue;
  187
+  }
  188
+
  189
+  if (twi_masterBufferIndex < length)
  190
+    length = twi_masterBufferIndex;
  191
+
  192
+  // copy twi buffer to data
  193
+  for(i = 0; i < length; ++i){
  194
+    data[i] = twi_masterBuffer[i];
  195
+  }
  196
+  
  197
+  return length;
  198
+  NOW IS (by Lesto)*/
  199
+  return 0;
  200
+  /*END */
  201
+
  202
+}
  203
+
  204
+/* 
  205
+ * Function twi_AsincReadNotReady (added by lesto)
  206
+ * Desc     return the status of the read asinc transaction
  207
+ * Input    length: number of bytes expected to be read
  208
+ * Output   number of error, 0 if ok
  209
+ */
  210
+uint8_t twi_AsincReadNotReady(uint8_t length){
  211
+  if (!twi_receiving)
  212
+    return 1;//error, no reading asked
  213
+    
  214
+  if (TWI_MRX == twi_state)
  215
+    return 2; //error, waiting for right status
  216
+
  217
+  //wait until all data arrive
  218
+  if (twi_masterBufferIndex < length-1){
  219
+    return 3; //error, missing some data
  220
+  }
  221
+  
  222
+  return 0; //data is ready
  223
+}
  224
+
  225
+/* 
  226
+ * Function twi_AsincReadFrom (added by lesto)
  227
+ * Desc     complete the read transaction, if possible
  228
+ * Input    data: pointer to byte array 
  229
+ *          length: number of bytes expected to be read
  230
+ * Output   number of byte read, 0 if error or data not ready yet
  231
+ */
  232
+uint8_t twi_AsincReadFrom(uint8_t* data, uint8_t length)
  233
+{
  234
+  if ( twi_AsincReadNotReady(length) )
  235
+    return 0;
  236
+
  237
+  twi_receiving = false;
  238
+
  239
+  // copy twi buffer to data
  240
+  int i;
  241
+  for(i = 0; i < length; ++i){
  242
+    data[i] = twi_masterBuffer[i];
  243
+  }
  244
+  
  245
+  return length;
  246
+}
  247
+
  248
+/* 
  249
+ * Function twi_AskWriteTo (modded by twi_writeTo by Lesto)
  250
+ * Desc     attempts to become twi bus master and beging to write a
  251
+ *          series of bytes to a device on the bus
  252
+ * Input    address: 7bit i2c device address
  253
+ *          data: pointer to byte array
  254
+ *          length: number of bytes in array
  255
+ *          wait: boolean indicating to wait for write or not
  256
+ *          sendStop: boolean indicating whether or not to send a stop at the end
  257
+ * Output   0 .. success
  258
+ *          1 .. length to long for buffer
  259
+ *          2 .. status is not READY
  260
+ */
  261
+uint8_t twi_AskWriteTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait, uint8_t sendStop)
  262
+{
  263
+  uint8_t i;
  264
+
  265
+  // ensure data will fit into buffer
  266
+  if(TWI_BUFFER_LENGTH < length){
  267
+    return 1;
  268
+  }
  269
+
  270
+  // wait until twi is ready, become master transmitter
  271
+  /* WAS
  272
+  while(TWI_READY != twi_state){
  273
+    continue;
  274
+  }
  275
+  NOW IS (by Lesto)*/
  276
+  if(TWI_READY != twi_state){
  277
+    return 2;
  278
+  }
  279
+  twi_writing = true;
  280
+  /*END */
  281
+  twi_state = TWI_MTX;
  282
+  twi_sendStop = sendStop;
  283
+  // reset error state (0xFF.. no error occured)
  284
+  twi_error = 0xFF;
  285
+
  286
+  // initialize buffer iteration vars
  287
+  twi_masterBufferIndex = 0;
  288
+  twi_masterBufferLength = length;
  289
+  
  290
+  // copy data to twi buffer