Browse Source

Libraries supporting Marlin platform

Richard Wackerbarth 9 years ago
parent
commit
3004359191
19 changed files with 1993 additions and 0 deletions
  1. 106
    0
      ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/firmwares/Brainwave.inf
  2. 310
    0
      ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/LiquidCrystal/LiquidCrystal.cpp
  3. 106
    0
      ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/LiquidCrystal/LiquidCrystal.h
  4. 37
    0
      ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/LiquidCrystal/keywords.txt
  5. 66
    0
      ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/SPI/SPI.cpp
  6. 70
    0
      ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/SPI/SPI.h
  7. 36
    0
      ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/SPI/keywords.txt
  8. 303
    0
      ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/Wire/Wire.cpp
  9. 80
    0
      ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/Wire/Wire.h
  10. 87
    0
      ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/Wire/examples/SFRRanger_reader/SFRRanger_reader.ino
  11. 39
    0
      ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/Wire/examples/digital_potentiometer/digital_potentiometer.ino
  12. 32
    0
      ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/Wire/examples/master_reader/master_reader.ino
  13. 31
    0
      ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/Wire/examples/master_writer/master_writer.ino
  14. 38
    0
      ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/Wire/examples/slave_receiver/slave_receiver.ino
  15. 32
    0
      ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/Wire/examples/slave_sender/slave_sender.ino
  16. 32
    0
      ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/Wire/keywords.txt
  17. 8
    0
      ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/Wire/library.properties
  18. 527
    0
      ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/Wire/utility/twi.c
  19. 53
    0
      ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/Wire/utility/twi.h

+ 106
- 0
ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/firmwares/Brainwave.inf View File

@@ -0,0 +1,106 @@
1
+;************************************************************
2
+; Windows USB CDC ACM Setup File
3
+; Copyright (c) 2000 Microsoft Corporation
4
+
5
+
6
+[Version]
7
+Signature="$Windows NT$"
8
+Class=Ports
9
+ClassGuid={4D36E978-E325-11CE-BFC1-08002BE10318}
10
+Provider=%MFGNAME%
11
+LayoutFile=layout.inf
12
+CatalogFile=%MFGFILENAME%.cat
13
+DriverVer=11/15/2007,5.1.2600.0
14
+
15
+[Manufacturer]
16
+%MFGNAME%=DeviceList, NTamd64
17
+
18
+[DestinationDirs]
19
+DefaultDestDir=12
20
+
21
+
22
+;------------------------------------------------------------------------------
23
+;  Windows 2000/XP/Vista-32bit Sections
24
+;------------------------------------------------------------------------------
25
+
26
+[DriverInstall.nt]
27
+include=mdmcpq.inf
28
+CopyFiles=DriverCopyFiles.nt
29
+AddReg=DriverInstall.nt.AddReg
30
+
31
+[DriverCopyFiles.nt]
32
+usbser.sys,,,0x20
33
+
34
+[DriverInstall.nt.AddReg]
35
+HKR,,DevLoader,,*ntkern
36
+HKR,,NTMPDriver,,%DRIVERFILENAME%.sys
37
+HKR,,EnumPropPages32,,"MsPorts.dll,SerialPortPropPageProvider"
38
+
39
+[DriverInstall.nt.Services]
40
+AddService=usbser, 0x00000002, DriverService.nt
41
+
42
+[DriverService.nt]
43
+DisplayName=%SERVICE%
44
+ServiceType=1
45
+StartType=3
46
+ErrorControl=1
47
+ServiceBinary=%12%\%DRIVERFILENAME%.sys
48
+
49
+;------------------------------------------------------------------------------
50
+;  Vista-64bit Sections
51
+;------------------------------------------------------------------------------
52
+
53
+[DriverInstall.NTamd64]
54
+include=mdmcpq.inf
55
+CopyFiles=DriverCopyFiles.NTamd64
56
+AddReg=DriverInstall.NTamd64.AddReg
57
+
58
+[DriverCopyFiles.NTamd64]
59
+%DRIVERFILENAME%.sys,,,0x20
60
+
61
+[DriverInstall.NTamd64.AddReg]
62
+HKR,,DevLoader,,*ntkern
63
+HKR,,NTMPDriver,,%DRIVERFILENAME%.sys
64
+HKR,,EnumPropPages32,,"MsPorts.dll,SerialPortPropPageProvider"
65
+
66
+[DriverInstall.NTamd64.Services]
67
+AddService=usbser, 0x00000002, DriverService.NTamd64
68
+
69
+[DriverService.NTamd64]
70
+DisplayName=%SERVICE%
71
+ServiceType=1
72
+StartType=3
73
+ErrorControl=1
74
+ServiceBinary=%12%\%DRIVERFILENAME%.sys
75
+
76
+
77
+;------------------------------------------------------------------------------
78
+;  Vendor and Product ID Definitions
79
+;------------------------------------------------------------------------------
80
+; When developing your USB device, the VID and PID used in the PC side
81
+; application program and the firmware on the microcontroller must match.
82
+; Modify the below line to use your VID and PID.  Use the format as shown below.
83
+; Note: One INF file can be used for multiple devices with different VID and PIDs.
84
+; For each supported device, append ",USB\VID_xxxx&PID_yyyy" to the end of the line.
85
+;------------------------------------------------------------------------------
86
+[SourceDisksFiles]
87
+[SourceDisksNames]
88
+[DeviceList]
89
+%DESCRIPTION%=DriverInstall, USB\VID_16D0&PID_076B
90
+
91
+[DeviceList.NTamd64]
92
+%DESCRIPTION%=DriverInstall, USB\VID_16D0&PID_204A
93
+
94
+
95
+;------------------------------------------------------------------------------
96
+;  String Definitions
97
+;------------------------------------------------------------------------------
98
+;Modify these strings to customize your device
99
+;------------------------------------------------------------------------------
100
+[Strings]
101
+MFGFILENAME="CDC_vista"
102
+DRIVERFILENAME ="usbser"
103
+MFGNAME="Metrix Create Space"
104
+INSTDISK="Brainwave Driver Installer"
105
+DESCRIPTION="Communications Port"
106
+SERVICE="USB RS-232 Emulation Driver"

+ 310
- 0
ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/LiquidCrystal/LiquidCrystal.cpp View File

@@ -0,0 +1,310 @@
1
+#include "LiquidCrystal.h"
2
+
3
+#include <stdio.h>
4
+#include <string.h>
5
+#include <inttypes.h>
6
+#include "Arduino.h"
7
+
8
+// When the display powers up, it is configured as follows:
9
+//
10
+// 1. Display clear
11
+// 2. Function set: 
12
+//    DL = 1; 8-bit interface data 
13
+//    N = 0; 1-line display 
14
+//    F = 0; 5x8 dot character font 
15
+// 3. Display on/off control: 
16
+//    D = 0; Display off 
17
+//    C = 0; Cursor off 
18
+//    B = 0; Blinking off 
19
+// 4. Entry mode set: 
20
+//    I/D = 1; Increment by 1 
21
+//    S = 0; No shift 
22
+//
23
+// Note, however, that resetting the Arduino doesn't reset the LCD, so we
24
+// can't assume that it's in that state when a sketch starts (and the
25
+// LiquidCrystal constructor is called).
26
+
27
+LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
28
+			     uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
29
+			     uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
30
+{
31
+  init(0, rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7);
32
+}
33
+
34
+LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t enable,
35
+			     uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
36
+			     uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
37
+{
38
+  init(0, rs, 255, enable, d0, d1, d2, d3, d4, d5, d6, d7);
39
+}
40
+
41
+LiquidCrystal::LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
42
+			     uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
43
+{
44
+  init(1, rs, rw, enable, d0, d1, d2, d3, 0, 0, 0, 0);
45
+}
46
+
47
+LiquidCrystal::LiquidCrystal(uint8_t rs,  uint8_t enable,
48
+			     uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3)
49
+{
50
+  init(1, rs, 255, enable, d0, d1, d2, d3, 0, 0, 0, 0);
51
+}
52
+
53
+void LiquidCrystal::init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t enable,
54
+			 uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
55
+			 uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
56
+{
57
+  _rs_pin = rs;
58
+  _rw_pin = rw;
59
+  _enable_pin = enable;
60
+  
61
+  _data_pins[0] = d0;
62
+  _data_pins[1] = d1;
63
+  _data_pins[2] = d2;
64
+  _data_pins[3] = d3; 
65
+  _data_pins[4] = d4;
66
+  _data_pins[5] = d5;
67
+  _data_pins[6] = d6;
68
+  _data_pins[7] = d7; 
69
+
70
+  pinMode(_rs_pin, OUTPUT);
71
+  // we can save 1 pin by not using RW. Indicate by passing 255 instead of pin#
72
+  if (_rw_pin != 255) { 
73
+    pinMode(_rw_pin, OUTPUT);
74
+  }
75
+  pinMode(_enable_pin, OUTPUT);
76
+  
77
+  if (fourbitmode)
78
+    _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
79
+  else 
80
+    _displayfunction = LCD_8BITMODE | LCD_1LINE | LCD_5x8DOTS;
81
+  
82
+  begin(16, 1);  
83
+}
84
+
85
+void LiquidCrystal::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) {
86
+  if (lines > 1) {
87
+    _displayfunction |= LCD_2LINE;
88
+  }
89
+  _numlines = lines;
90
+  _currline = 0;
91
+
92
+  // for some 1 line displays you can select a 10 pixel high font
93
+  if ((dotsize != 0) && (lines == 1)) {
94
+    _displayfunction |= LCD_5x10DOTS;
95
+  }
96
+
97
+  // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
98
+  // according to datasheet, we need at least 40ms after power rises above 2.7V
99
+  // before sending commands. Arduino can turn on way befer 4.5V so we'll wait 50
100
+  delayMicroseconds(50000); 
101
+  // Now we pull both RS and R/W low to begin commands
102
+  digitalWrite(_rs_pin, LOW);
103
+  digitalWrite(_enable_pin, LOW);
104
+  if (_rw_pin != 255) { 
105
+    digitalWrite(_rw_pin, LOW);
106
+  }
107
+  
108
+  //put the LCD into 4 bit or 8 bit mode
109
+  if (! (_displayfunction & LCD_8BITMODE)) {
110
+    // this is according to the hitachi HD44780 datasheet
111
+    // figure 24, pg 46
112
+
113
+    // we start in 8bit mode, try to set 4 bit mode
114
+    write4bits(0x03);
115
+    delayMicroseconds(4500); // wait min 4.1ms
116
+
117
+    // second try
118
+    write4bits(0x03);
119
+    delayMicroseconds(4500); // wait min 4.1ms
120
+    
121
+    // third go!
122
+    write4bits(0x03); 
123
+    delayMicroseconds(150);
124
+
125
+    // finally, set to 4-bit interface
126
+    write4bits(0x02); 
127
+  } else {
128
+    // this is according to the hitachi HD44780 datasheet
129
+    // page 45 figure 23
130
+
131
+    // Send function set command sequence
132
+    command(LCD_FUNCTIONSET | _displayfunction);
133
+    delayMicroseconds(4500);  // wait more than 4.1ms
134
+
135
+    // second try
136
+    command(LCD_FUNCTIONSET | _displayfunction);
137
+    delayMicroseconds(150);
138
+
139
+    // third go
140
+    command(LCD_FUNCTIONSET | _displayfunction);
141
+  }
142
+
143
+  // finally, set # lines, font size, etc.
144
+  command(LCD_FUNCTIONSET | _displayfunction);  
145
+
146
+  // turn the display on with no cursor or blinking default
147
+  _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF;  
148
+  display();
149
+
150
+  // clear it off
151
+  clear();
152
+
153
+  // Initialize to default text direction (for romance languages)
154
+  _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT;
155
+  // set the entry mode
156
+  command(LCD_ENTRYMODESET | _displaymode);
157
+
158
+}
159
+
160
+/********** high level commands, for the user! */
161
+void LiquidCrystal::clear()
162
+{
163
+  command(LCD_CLEARDISPLAY);  // clear display, set cursor position to zero
164
+  delayMicroseconds(2000);  // this command takes a long time!
165
+}
166
+
167
+void LiquidCrystal::home()
168
+{
169
+  command(LCD_RETURNHOME);  // set cursor position to zero
170
+  delayMicroseconds(2000);  // this command takes a long time!
171
+}
172
+
173
+void LiquidCrystal::setCursor(uint8_t col, uint8_t row)
174
+{
175
+  int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 };
176
+  if ( row >= _numlines ) {
177
+    row = _numlines-1;    // we count rows starting w/0
178
+  }
179
+  
180
+  command(LCD_SETDDRAMADDR | (col + row_offsets[row]));
181
+}
182
+
183
+// Turn the display on/off (quickly)
184
+void LiquidCrystal::noDisplay() {
185
+  _displaycontrol &= ~LCD_DISPLAYON;
186
+  command(LCD_DISPLAYCONTROL | _displaycontrol);
187
+}
188
+void LiquidCrystal::display() {
189
+  _displaycontrol |= LCD_DISPLAYON;
190
+  command(LCD_DISPLAYCONTROL | _displaycontrol);
191
+}
192
+
193
+// Turns the underline cursor on/off
194
+void LiquidCrystal::noCursor() {
195
+  _displaycontrol &= ~LCD_CURSORON;
196
+  command(LCD_DISPLAYCONTROL | _displaycontrol);
197
+}
198
+void LiquidCrystal::cursor() {
199
+  _displaycontrol |= LCD_CURSORON;
200
+  command(LCD_DISPLAYCONTROL | _displaycontrol);
201
+}
202
+
203
+// Turn on and off the blinking cursor
204
+void LiquidCrystal::noBlink() {
205
+  _displaycontrol &= ~LCD_BLINKON;
206
+  command(LCD_DISPLAYCONTROL | _displaycontrol);
207
+}
208
+void LiquidCrystal::blink() {
209
+  _displaycontrol |= LCD_BLINKON;
210
+  command(LCD_DISPLAYCONTROL | _displaycontrol);
211
+}
212
+
213
+// These commands scroll the display without changing the RAM
214
+void LiquidCrystal::scrollDisplayLeft(void) {
215
+  command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT);
216
+}
217
+void LiquidCrystal::scrollDisplayRight(void) {
218
+  command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT);
219
+}
220
+
221
+// This is for text that flows Left to Right
222
+void LiquidCrystal::leftToRight(void) {
223
+  _displaymode |= LCD_ENTRYLEFT;
224
+  command(LCD_ENTRYMODESET | _displaymode);
225
+}
226
+
227
+// This is for text that flows Right to Left
228
+void LiquidCrystal::rightToLeft(void) {
229
+  _displaymode &= ~LCD_ENTRYLEFT;
230
+  command(LCD_ENTRYMODESET | _displaymode);
231
+}
232
+
233
+// This will 'right justify' text from the cursor
234
+void LiquidCrystal::autoscroll(void) {
235
+  _displaymode |= LCD_ENTRYSHIFTINCREMENT;
236
+  command(LCD_ENTRYMODESET | _displaymode);
237
+}
238
+
239
+// This will 'left justify' text from the cursor
240
+void LiquidCrystal::noAutoscroll(void) {
241
+  _displaymode &= ~LCD_ENTRYSHIFTINCREMENT;
242
+  command(LCD_ENTRYMODESET | _displaymode);
243
+}
244
+
245
+// Allows us to fill the first 8 CGRAM locations
246
+// with custom characters
247
+void LiquidCrystal::createChar(uint8_t location, uint8_t charmap[]) {
248
+  location &= 0x7; // we only have 8 locations 0-7
249
+  command(LCD_SETCGRAMADDR | (location << 3));
250
+  for (int i=0; i<8; i++) {
251
+    write(charmap[i]);
252
+  }
253
+}
254
+
255
+/*********** mid level commands, for sending data/cmds */
256
+
257
+inline void LiquidCrystal::command(uint8_t value) {
258
+  send(value, LOW);
259
+}
260
+
261
+inline size_t LiquidCrystal::write(uint8_t value) {
262
+  send(value, HIGH);
263
+  return 1; // assume sucess
264
+}
265
+
266
+/************ low level data pushing commands **********/
267
+
268
+// write either command or data, with automatic 4/8-bit selection
269
+void LiquidCrystal::send(uint8_t value, uint8_t mode) {
270
+  digitalWrite(_rs_pin, mode);
271
+
272
+  // if there is a RW pin indicated, set it low to Write
273
+  if (_rw_pin != 255) { 
274
+    digitalWrite(_rw_pin, LOW);
275
+  }
276
+  
277
+  if (_displayfunction & LCD_8BITMODE) {
278
+    write8bits(value); 
279
+  } else {
280
+    write4bits(value>>4);
281
+    write4bits(value);
282
+  }
283
+}
284
+
285
+void LiquidCrystal::pulseEnable(void) {
286
+  digitalWrite(_enable_pin, LOW);
287
+  delayMicroseconds(1);    
288
+  digitalWrite(_enable_pin, HIGH);
289
+  delayMicroseconds(1);    // enable pulse must be >450ns
290
+  digitalWrite(_enable_pin, LOW);
291
+  delayMicroseconds(100);   // commands need > 37us to settle
292
+}
293
+
294
+void LiquidCrystal::write4bits(uint8_t value) {
295
+  for (int i = 0; i < 4; i++) {
296
+    pinMode(_data_pins[i], OUTPUT);
297
+    digitalWrite(_data_pins[i], (value >> i) & 0x01);
298
+  }
299
+
300
+  pulseEnable();
301
+}
302
+
303
+void LiquidCrystal::write8bits(uint8_t value) {
304
+  for (int i = 0; i < 8; i++) {
305
+    pinMode(_data_pins[i], OUTPUT);
306
+    digitalWrite(_data_pins[i], (value >> i) & 0x01);
307
+  }
308
+  
309
+  pulseEnable();
310
+}

+ 106
- 0
ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/LiquidCrystal/LiquidCrystal.h View File

@@ -0,0 +1,106 @@
1
+#ifndef LiquidCrystal_h
2
+#define LiquidCrystal_h
3
+
4
+#include <inttypes.h>
5
+#include "Print.h"
6
+
7
+// commands
8
+#define LCD_CLEARDISPLAY 0x01
9
+#define LCD_RETURNHOME 0x02
10
+#define LCD_ENTRYMODESET 0x04
11
+#define LCD_DISPLAYCONTROL 0x08
12
+#define LCD_CURSORSHIFT 0x10
13
+#define LCD_FUNCTIONSET 0x20
14
+#define LCD_SETCGRAMADDR 0x40
15
+#define LCD_SETDDRAMADDR 0x80
16
+
17
+// flags for display entry mode
18
+#define LCD_ENTRYRIGHT 0x00
19
+#define LCD_ENTRYLEFT 0x02
20
+#define LCD_ENTRYSHIFTINCREMENT 0x01
21
+#define LCD_ENTRYSHIFTDECREMENT 0x00
22
+
23
+// flags for display on/off control
24
+#define LCD_DISPLAYON 0x04
25
+#define LCD_DISPLAYOFF 0x00
26
+#define LCD_CURSORON 0x02
27
+#define LCD_CURSOROFF 0x00
28
+#define LCD_BLINKON 0x01
29
+#define LCD_BLINKOFF 0x00
30
+
31
+// flags for display/cursor shift
32
+#define LCD_DISPLAYMOVE 0x08
33
+#define LCD_CURSORMOVE 0x00
34
+#define LCD_MOVERIGHT 0x04
35
+#define LCD_MOVELEFT 0x00
36
+
37
+// flags for function set
38
+#define LCD_8BITMODE 0x10
39
+#define LCD_4BITMODE 0x00
40
+#define LCD_2LINE 0x08
41
+#define LCD_1LINE 0x00
42
+#define LCD_5x10DOTS 0x04
43
+#define LCD_5x8DOTS 0x00
44
+
45
+class LiquidCrystal : public Print {
46
+public:
47
+  LiquidCrystal(uint8_t rs, uint8_t enable,
48
+		uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
49
+		uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7);
50
+  LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
51
+		uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
52
+		uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7);
53
+  LiquidCrystal(uint8_t rs, uint8_t rw, uint8_t enable,
54
+		uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3);
55
+  LiquidCrystal(uint8_t rs, uint8_t enable,
56
+		uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3);
57
+
58
+  void init(uint8_t fourbitmode, uint8_t rs, uint8_t rw, uint8_t enable,
59
+	    uint8_t d0, uint8_t d1, uint8_t d2, uint8_t d3,
60
+	    uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7);
61
+    
62
+  void begin(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS);
63
+
64
+  void clear();
65
+  void home();
66
+
67
+  void noDisplay();
68
+  void display();
69
+  void noBlink();
70
+  void blink();
71
+  void noCursor();
72
+  void cursor();
73
+  void scrollDisplayLeft();
74
+  void scrollDisplayRight();
75
+  void leftToRight();
76
+  void rightToLeft();
77
+  void autoscroll();
78
+  void noAutoscroll();
79
+
80
+  void createChar(uint8_t, uint8_t[]);
81
+  void setCursor(uint8_t, uint8_t); 
82
+  virtual size_t write(uint8_t);
83
+  void command(uint8_t);
84
+  
85
+  using Print::write;
86
+private:
87
+  void send(uint8_t, uint8_t);
88
+  void write4bits(uint8_t);
89
+  void write8bits(uint8_t);
90
+  void pulseEnable();
91
+
92
+  uint8_t _rs_pin; // LOW: command.  HIGH: character.
93
+  uint8_t _rw_pin; // LOW: write to LCD.  HIGH: read from LCD.
94
+  uint8_t _enable_pin; // activated by a HIGH pulse.
95
+  uint8_t _data_pins[8];
96
+
97
+  uint8_t _displayfunction;
98
+  uint8_t _displaycontrol;
99
+  uint8_t _displaymode;
100
+
101
+  uint8_t _initialized;
102
+
103
+  uint8_t _numlines,_currline;
104
+};
105
+
106
+#endif

+ 37
- 0
ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/LiquidCrystal/keywords.txt View File

@@ -0,0 +1,37 @@
1
+#######################################
2
+# Syntax Coloring Map For LiquidCrystal
3
+#######################################
4
+
5
+#######################################
6
+# Datatypes (KEYWORD1)
7
+#######################################
8
+
9
+LiquidCrystal	KEYWORD1
10
+
11
+#######################################
12
+# Methods and Functions (KEYWORD2)
13
+#######################################
14
+
15
+begin	KEYWORD2
16
+clear	KEYWORD2
17
+home	KEYWORD2
18
+print	KEYWORD2
19
+setCursor	KEYWORD2
20
+cursor	KEYWORD2
21
+noCursor	KEYWORD2
22
+blink	KEYWORD2
23
+noBlink	KEYWORD2
24
+display	KEYWORD2
25
+noDisplay	KEYWORD2
26
+autoscroll	KEYWORD2
27
+noAutoscroll	KEYWORD2
28
+leftToRight	KEYWORD2
29
+rightToLeft	KEYWORD2
30
+scrollDisplayLeft	KEYWORD2
31
+scrollDisplayRight	KEYWORD2
32
+createChar	KEYWORD2
33
+
34
+#######################################
35
+# Constants (LITERAL1)
36
+#######################################
37
+

+ 66
- 0
ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/SPI/SPI.cpp View File

@@ -0,0 +1,66 @@
1
+/*
2
+ * Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st>
3
+ * SPI Master library for arduino.
4
+ *
5
+ * This file is free software; you can redistribute it and/or modify
6
+ * it under the terms of either the GNU General Public License version 2
7
+ * or the GNU Lesser General Public License version 2.1, both as
8
+ * published by the Free Software Foundation.
9
+ */
10
+
11
+#include "pins_arduino.h"
12
+#include "SPI.h"
13
+
14
+SPIClass SPI;
15
+
16
+void SPIClass::begin() {
17
+
18
+  // Set SS to high so a connected chip will be "deselected" by default
19
+  digitalWrite(SS, HIGH);
20
+
21
+  // When the SS pin is set as OUTPUT, it can be used as
22
+  // a general purpose output port (it doesn't influence
23
+  // SPI operations).
24
+  pinMode(SS, OUTPUT);
25
+
26
+  // Warning: if the SS pin ever becomes a LOW INPUT then SPI
27
+  // automatically switches to Slave, so the data direction of
28
+  // the SS pin MUST be kept as OUTPUT.
29
+  SPCR |= _BV(MSTR);
30
+  SPCR |= _BV(SPE);
31
+
32
+  // Set direction register for SCK and MOSI pin.
33
+  // MISO pin automatically overrides to INPUT.
34
+  // By doing this AFTER enabling SPI, we avoid accidentally
35
+  // clocking in a single bit since the lines go directly
36
+  // from "input" to SPI control.  
37
+  // http://code.google.com/p/arduino/issues/detail?id=888
38
+  pinMode(SCK, OUTPUT);
39
+  pinMode(MOSI, OUTPUT);
40
+}
41
+
42
+
43
+void SPIClass::end() {
44
+  SPCR &= ~_BV(SPE);
45
+}
46
+
47
+void SPIClass::setBitOrder(uint8_t bitOrder)
48
+{
49
+  if(bitOrder == LSBFIRST) {
50
+    SPCR |= _BV(DORD);
51
+  } else {
52
+    SPCR &= ~(_BV(DORD));
53
+  }
54
+}
55
+
56
+void SPIClass::setDataMode(uint8_t mode)
57
+{
58
+  SPCR = (SPCR & ~SPI_MODE_MASK) | mode;
59
+}
60
+
61
+void SPIClass::setClockDivider(uint8_t rate)
62
+{
63
+  SPCR = (SPCR & ~SPI_CLOCK_MASK) | (rate & SPI_CLOCK_MASK);
64
+  SPSR = (SPSR & ~SPI_2XCLOCK_MASK) | ((rate >> 2) & SPI_2XCLOCK_MASK);
65
+}
66
+

+ 70
- 0
ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/SPI/SPI.h View File

@@ -0,0 +1,70 @@
1
+/*
2
+ * Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st>
3
+ * SPI Master library for arduino.
4
+ *
5
+ * This file is free software; you can redistribute it and/or modify
6
+ * it under the terms of either the GNU General Public License version 2
7
+ * or the GNU Lesser General Public License version 2.1, both as
8
+ * published by the Free Software Foundation.
9
+ */
10
+
11
+#ifndef _SPI_H_INCLUDED
12
+#define _SPI_H_INCLUDED
13
+
14
+#include <stdio.h>
15
+#include <Arduino.h>
16
+#include <avr/pgmspace.h>
17
+
18
+#define SPI_CLOCK_DIV4 0x00
19
+#define SPI_CLOCK_DIV16 0x01
20
+#define SPI_CLOCK_DIV64 0x02
21
+#define SPI_CLOCK_DIV128 0x03
22
+#define SPI_CLOCK_DIV2 0x04
23
+#define SPI_CLOCK_DIV8 0x05
24
+#define SPI_CLOCK_DIV32 0x06
25
+//#define SPI_CLOCK_DIV64 0x07
26
+
27
+#define SPI_MODE0 0x00
28
+#define SPI_MODE1 0x04
29
+#define SPI_MODE2 0x08
30
+#define SPI_MODE3 0x0C
31
+
32
+#define SPI_MODE_MASK 0x0C  // CPOL = bit 3, CPHA = bit 2 on SPCR
33
+#define SPI_CLOCK_MASK 0x03  // SPR1 = bit 1, SPR0 = bit 0 on SPCR
34
+#define SPI_2XCLOCK_MASK 0x01  // SPI2X = bit 0 on SPSR
35
+
36
+class SPIClass {
37
+public:
38
+  inline static byte transfer(byte _data);
39
+
40
+  // SPI Configuration methods
41
+
42
+  inline static void attachInterrupt();
43
+  inline static void detachInterrupt(); // Default
44
+
45
+  static void begin(); // Default
46
+  static void end();
47
+
48
+  static void setBitOrder(uint8_t);
49
+  static void setDataMode(uint8_t);
50
+  static void setClockDivider(uint8_t);
51
+};
52
+
53
+extern SPIClass SPI;
54
+
55
+byte SPIClass::transfer(byte _data) {
56
+  SPDR = _data;
57
+  while (!(SPSR & _BV(SPIF)))
58
+    ;
59
+  return SPDR;
60
+}
61
+
62
+void SPIClass::attachInterrupt() {
63
+  SPCR |= _BV(SPIE);
64
+}
65
+
66
+void SPIClass::detachInterrupt() {
67
+  SPCR &= ~_BV(SPIE);
68
+}
69
+
70
+#endif

+ 36
- 0
ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/SPI/keywords.txt View File

@@ -0,0 +1,36 @@
1
+#######################################
2
+# Syntax Coloring Map SPI
3
+#######################################
4
+
5
+#######################################
6
+# Datatypes (KEYWORD1)
7
+#######################################
8
+
9
+SPI	KEYWORD1
10
+
11
+#######################################
12
+# Methods and Functions (KEYWORD2)
13
+#######################################
14
+begin	KEYWORD2
15
+end	KEYWORD2
16
+transfer	KEYWORD2
17
+setBitOrder	KEYWORD2
18
+setDataMode	KEYWORD2
19
+setClockDivider	KEYWORD2
20
+
21
+
22
+#######################################
23
+# Constants (LITERAL1)
24
+#######################################
25
+SPI_CLOCK_DIV4	LITERAL1
26
+SPI_CLOCK_DIV16	LITERAL1
27
+SPI_CLOCK_DIV64	LITERAL1
28
+SPI_CLOCK_DIV128	LITERAL1
29
+SPI_CLOCK_DIV2	LITERAL1
30
+SPI_CLOCK_DIV8	LITERAL1
31
+SPI_CLOCK_DIV32	LITERAL1
32
+SPI_CLOCK_DIV64	LITERAL1
33
+SPI_MODE0	LITERAL1
34
+SPI_MODE1	LITERAL1
35
+SPI_MODE2	LITERAL1
36
+SPI_MODE3	LITERAL1

+ 303
- 0
ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/Wire/Wire.cpp View File

@@ -0,0 +1,303 @@
1
+/*
2
+  TwoWire.cpp - 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
+  Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
20
+*/
21
+
22
+extern "C" {
23
+  #include <stdlib.h>
24
+  #include <string.h>
25
+  #include <inttypes.h>
26
+  #include "twi.h"
27
+}
28
+
29
+#include "Wire.h"
30
+
31
+// Initialize Class Variables //////////////////////////////////////////////////
32
+
33
+uint8_t TwoWire::rxBuffer[BUFFER_LENGTH];
34
+uint8_t TwoWire::rxBufferIndex = 0;
35
+uint8_t TwoWire::rxBufferLength = 0;
36
+
37
+uint8_t TwoWire::txAddress = 0;
38
+uint8_t TwoWire::txBuffer[BUFFER_LENGTH];
39
+uint8_t TwoWire::txBufferIndex = 0;
40
+uint8_t TwoWire::txBufferLength = 0;
41
+
42
+uint8_t TwoWire::transmitting = 0;
43
+void (*TwoWire::user_onRequest)(void);
44
+void (*TwoWire::user_onReceive)(int);
45
+
46
+// Constructors ////////////////////////////////////////////////////////////////
47
+
48
+TwoWire::TwoWire()
49
+{
50
+}
51
+
52
+// Public Methods //////////////////////////////////////////////////////////////
53
+
54
+void TwoWire::begin(void)
55
+{
56
+  rxBufferIndex = 0;
57
+  rxBufferLength = 0;
58
+
59
+  txBufferIndex = 0;
60
+  txBufferLength = 0;
61
+
62
+  twi_init();
63
+}
64
+
65
+void TwoWire::begin(uint8_t address)
66
+{
67
+  twi_setAddress(address);
68
+  twi_attachSlaveTxEvent(onRequestService);
69
+  twi_attachSlaveRxEvent(onReceiveService);
70
+  begin();
71
+}
72
+
73
+void TwoWire::begin(int address)
74
+{
75
+  begin((uint8_t)address);
76
+}
77
+
78
+void TwoWire::setClock(uint32_t frequency)
79
+{
80
+  TWBR = ((F_CPU / frequency) - 16) / 2;
81
+}
82
+
83
+uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop)
84
+{
85
+  // clamp to buffer length
86
+  if(quantity > BUFFER_LENGTH){
87
+    quantity = BUFFER_LENGTH;
88
+  }
89
+  // perform blocking read into buffer
90
+  uint8_t read = twi_readFrom(address, rxBuffer, quantity, sendStop);
91
+  // set rx buffer iterator vars
92
+  rxBufferIndex = 0;
93
+  rxBufferLength = read;
94
+
95
+  return read;
96
+}
97
+
98
+uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity)
99
+{
100
+  return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
101
+}
102
+
103
+uint8_t TwoWire::requestFrom(int address, int quantity)
104
+{
105
+  return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
106
+}
107
+
108
+uint8_t TwoWire::requestFrom(int address, int quantity, int sendStop)
109
+{
110
+  return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)sendStop);
111
+}
112
+
113
+void TwoWire::beginTransmission(uint8_t address)
114
+{
115
+  // indicate that we are transmitting
116
+  transmitting = 1;
117
+  // set address of targeted slave
118
+  txAddress = address;
119
+  // reset tx buffer iterator vars
120
+  txBufferIndex = 0;
121
+  txBufferLength = 0;
122
+}
123
+
124
+void TwoWire::beginTransmission(int address)
125
+{
126
+  beginTransmission((uint8_t)address);
127
+}
128
+
129
+//
130
+//	Originally, 'endTransmission' was an f(void) function.
131
+//	It has been modified to take one parameter indicating
132
+//	whether or not a STOP should be performed on the bus.
133
+//	Calling endTransmission(false) allows a sketch to 
134
+//	perform a repeated start. 
135
+//
136
+//	WARNING: Nothing in the library keeps track of whether
137
+//	the bus tenure has been properly ended with a STOP. It
138
+//	is very possible to leave the bus in a hung state if
139
+//	no call to endTransmission(true) is made. Some I2C
140
+//	devices will behave oddly if they do not see a STOP.
141
+//
142
+uint8_t TwoWire::endTransmission(uint8_t sendStop)
143
+{
144
+  // transmit buffer (blocking)
145
+  int8_t ret = twi_writeTo(txAddress, txBuffer, txBufferLength, 1, sendStop);
146
+  // reset tx buffer iterator vars
147
+  txBufferIndex = 0;
148
+  txBufferLength = 0;
149
+  // indicate that we are done transmitting
150
+  transmitting = 0;
151
+  return ret;
152
+}
153
+
154
+//	This provides backwards compatibility with the original
155
+//	definition, and expected behaviour, of endTransmission
156
+//
157
+uint8_t TwoWire::endTransmission(void)
158
+{
159
+  return endTransmission(true);
160
+}
161
+
162
+// must be called in:
163
+// slave tx event callback
164
+// or after beginTransmission(address)
165
+size_t TwoWire::write(uint8_t data)
166
+{
167
+  if(transmitting){
168
+  // in master transmitter mode
169
+    // don't bother if buffer is full
170
+    if(txBufferLength >= BUFFER_LENGTH){
171
+      setWriteError();
172
+      return 0;
173
+    }
174
+    // put byte in tx buffer
175
+    txBuffer[txBufferIndex] = data;
176
+    ++txBufferIndex;
177
+    // update amount in buffer   
178
+    txBufferLength = txBufferIndex;
179
+  }else{
180
+  // in slave send mode
181
+    // reply to master
182
+    twi_transmit(&data, 1);
183
+  }
184
+  return 1;
185
+}
186
+
187
+// must be called in:
188
+// slave tx event callback
189
+// or after beginTransmission(address)
190
+size_t TwoWire::write(const uint8_t *data, size_t quantity)
191
+{
192
+  if(transmitting){
193
+  // in master transmitter mode
194
+    for(size_t i = 0; i < quantity; ++i){
195
+      write(data[i]);
196
+    }
197
+  }else{
198
+  // in slave send mode
199
+    // reply to master
200
+    twi_transmit(data, quantity);
201
+  }
202
+  return quantity;
203
+}
204
+
205
+// must be called in:
206
+// slave rx event callback
207
+// or after requestFrom(address, numBytes)
208
+int TwoWire::available(void)
209
+{
210
+  return rxBufferLength - rxBufferIndex;
211
+}
212
+
213
+// must be called in:
214
+// slave rx event callback
215
+// or after requestFrom(address, numBytes)
216
+int TwoWire::read(void)
217
+{
218
+  int value = -1;
219
+  
220
+  // get each successive byte on each call
221
+  if(rxBufferIndex < rxBufferLength){
222
+    value = rxBuffer[rxBufferIndex];
223
+    ++rxBufferIndex;
224
+  }
225
+
226
+  return value;
227
+}
228
+
229
+// must be called in:
230
+// slave rx event callback
231
+// or after requestFrom(address, numBytes)
232
+int TwoWire::peek(void)
233
+{
234
+  int value = -1;
235
+  
236
+  if(rxBufferIndex < rxBufferLength){
237
+    value = rxBuffer[rxBufferIndex];
238
+  }
239
+
240
+  return value;
241
+}
242
+
243
+void TwoWire::flush(void)
244
+{
245
+  // XXX: to be implemented.
246
+}
247
+
248
+// behind the scenes function that is called when data is received
249
+void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes)
250
+{
251
+  // don't bother if user hasn't registered a callback
252
+  if(!user_onReceive){
253
+    return;
254
+  }
255
+  // don't bother if rx buffer is in use by a master requestFrom() op
256
+  // i know this drops data, but it allows for slight stupidity
257
+  // meaning, they may not have read all the master requestFrom() data yet
258
+  if(rxBufferIndex < rxBufferLength){
259
+    return;
260
+  }
261
+  // copy twi rx buffer into local read buffer
262
+  // this enables new reads to happen in parallel
263
+  for(uint8_t i = 0; i < numBytes; ++i){
264
+    rxBuffer[i] = inBytes[i];    
265
+  }
266
+  // set rx iterator vars
267
+  rxBufferIndex = 0;
268
+  rxBufferLength = numBytes;
269
+  // alert user program
270
+  user_onReceive(numBytes);
271
+}
272
+
273
+// behind the scenes function that is called when data is requested
274
+void TwoWire::onRequestService(void)
275
+{
276
+  // don't bother if user hasn't registered a callback
277
+  if(!user_onRequest){
278
+    return;
279
+  }
280
+  // reset tx buffer iterator vars
281
+  // !!! this will kill any pending pre-master sendTo() activity
282
+  txBufferIndex = 0;
283
+  txBufferLength = 0;
284
+  // alert user program
285
+  user_onRequest();
286
+}
287
+
288
+// sets function called on slave write
289
+void TwoWire::onReceive( void (*function)(int) )
290
+{
291
+  user_onReceive = function;
292
+}
293
+
294
+// sets function called on slave read
295
+void TwoWire::onRequest( void (*function)(void) )
296
+{
297
+  user_onRequest = function;
298
+}
299
+
300
+// Preinstantiate Objects //////////////////////////////////////////////////////
301
+
302
+TwoWire Wire = TwoWire();
303
+

+ 80
- 0
ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/Wire/Wire.h View File

@@ -0,0 +1,80 @@
1
+/*
2
+  TwoWire.h - TWI/I2C library for Arduino & Wiring
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
+  Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
20
+*/
21
+
22
+#ifndef TwoWire_h
23
+#define TwoWire_h
24
+
25
+#include <inttypes.h>
26
+#include "Stream.h"
27
+
28
+#define BUFFER_LENGTH 32
29
+
30
+class TwoWire : public Stream
31
+{
32
+  private:
33
+    static uint8_t rxBuffer[];
34
+    static uint8_t rxBufferIndex;
35
+    static uint8_t rxBufferLength;
36
+
37
+    static uint8_t txAddress;
38
+    static uint8_t txBuffer[];
39
+    static uint8_t txBufferIndex;
40
+    static uint8_t txBufferLength;
41
+
42
+    static uint8_t transmitting;
43
+    static void (*user_onRequest)(void);
44
+    static void (*user_onReceive)(int);
45
+    static void onRequestService(void);
46
+    static void onReceiveService(uint8_t*, int);
47
+  public:
48
+    TwoWire();
49
+    void begin();
50
+    void begin(uint8_t);
51
+    void begin(int);
52
+    void setClock(uint32_t);
53
+    void beginTransmission(uint8_t);
54
+    void beginTransmission(int);
55
+    uint8_t endTransmission(void);
56
+    uint8_t endTransmission(uint8_t);
57
+    uint8_t requestFrom(uint8_t, uint8_t);
58
+    uint8_t requestFrom(uint8_t, uint8_t, uint8_t);
59
+    uint8_t requestFrom(int, int);
60
+    uint8_t requestFrom(int, int, int);
61
+    virtual size_t write(uint8_t);
62
+    virtual size_t write(const uint8_t *, size_t);
63
+    virtual int available(void);
64
+    virtual int read(void);
65
+    virtual int peek(void);
66
+    virtual void flush(void);
67
+    void onReceive( void (*)(int) );
68
+    void onRequest( void (*)(void) );
69
+
70
+    inline size_t write(unsigned long n) { return write((uint8_t)n); }
71
+    inline size_t write(long n) { return write((uint8_t)n); }
72
+    inline size_t write(unsigned int n) { return write((uint8_t)n); }
73
+    inline size_t write(int n) { return write((uint8_t)n); }
74
+    using Print::write;
75
+};
76
+
77
+extern TwoWire Wire;
78
+
79
+#endif
80
+

+ 87
- 0
ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/Wire/examples/SFRRanger_reader/SFRRanger_reader.ino View File

@@ -0,0 +1,87 @@
1
+// I2C SRF10 or SRF08 Devantech Ultrasonic Ranger Finder
2
+// by Nicholas Zambetti <http://www.zambetti.com>
3
+// and James Tichenor <http://www.jamestichenor.net>
4
+
5
+// Demonstrates use of the Wire library reading data from the
6
+// Devantech Utrasonic Rangers SFR08 and SFR10
7
+
8
+// Created 29 April 2006
9
+
10
+// This example code is in the public domain.
11
+
12
+
13
+#include <Wire.h>
14
+
15
+void setup()
16
+{
17
+  Wire.begin();                // join i2c bus (address optional for master)
18
+  Serial.begin(9600);          // start serial communication at 9600bps
19
+}
20
+
21
+int reading = 0;
22
+
23
+void loop()
24
+{
25
+  // step 1: instruct sensor to read echoes
26
+  Wire.beginTransmission(112); // transmit to device #112 (0x70)
27
+  // the address specified in the datasheet is 224 (0xE0)
28
+  // but i2c adressing uses the high 7 bits so it's 112
29
+  Wire.write(byte(0x00));      // sets register pointer to the command register (0x00)
30
+  Wire.write(byte(0x50));      // command sensor to measure in "inches" (0x50)
31
+  // use 0x51 for centimeters
32
+  // use 0x52 for ping microseconds
33
+  Wire.endTransmission();      // stop transmitting
34
+
35
+  // step 2: wait for readings to happen
36
+  delay(70);                   // datasheet suggests at least 65 milliseconds
37
+
38
+  // step 3: instruct sensor to return a particular echo reading
39
+  Wire.beginTransmission(112); // transmit to device #112
40
+  Wire.write(byte(0x02));      // sets register pointer to echo #1 register (0x02)
41
+  Wire.endTransmission();      // stop transmitting
42
+
43
+  // step 4: request reading from sensor
44
+  Wire.requestFrom(112, 2);    // request 2 bytes from slave device #112
45
+
46
+  // step 5: receive reading from sensor
47
+  if (2 <= Wire.available())   // if two bytes were received
48
+  {
49
+    reading = Wire.read();  // receive high byte (overwrites previous reading)
50
+    reading = reading << 8;    // shift high byte to be high 8 bits
51
+    reading |= Wire.read(); // receive low byte as lower 8 bits
52
+    Serial.println(reading);   // print the reading
53
+  }
54
+
55
+  delay(250);                  // wait a bit since people have to read the output :)
56
+}
57
+
58
+
59
+/*
60
+
61
+// The following code changes the address of a Devantech Ultrasonic Range Finder (SRF10 or SRF08)
62
+// usage: changeAddress(0x70, 0xE6);
63
+
64
+void changeAddress(byte oldAddress, byte newAddress)
65
+{
66
+  Wire.beginTransmission(oldAddress);
67
+  Wire.write(byte(0x00));
68
+  Wire.write(byte(0xA0));
69
+  Wire.endTransmission();
70
+
71
+  Wire.beginTransmission(oldAddress);
72
+  Wire.write(byte(0x00));
73
+  Wire.write(byte(0xAA));
74
+  Wire.endTransmission();
75
+
76
+  Wire.beginTransmission(oldAddress);
77
+  Wire.write(byte(0x00));
78
+  Wire.write(byte(0xA5));
79
+  Wire.endTransmission();
80
+
81
+  Wire.beginTransmission(oldAddress);
82
+  Wire.write(byte(0x00));
83
+  Wire.write(newAddress);
84
+  Wire.endTransmission();
85
+}
86
+
87
+*/

+ 39
- 0
ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/Wire/examples/digital_potentiometer/digital_potentiometer.ino View File

@@ -0,0 +1,39 @@
1
+// I2C Digital Potentiometer
2
+// by Nicholas Zambetti <http://www.zambetti.com>
3
+// and Shawn Bonkowski <http://people.interaction-ivrea.it/s.bonkowski/>
4
+
5
+// Demonstrates use of the Wire library
6
+// Controls AD5171 digital potentiometer via I2C/TWI
7
+
8
+// Created 31 March 2006
9
+
10
+// This example code is in the public domain.
11
+
12
+// This example code is in the public domain.
13
+
14
+
15
+#include <Wire.h>
16
+
17
+void setup()
18
+{
19
+  Wire.begin(); // join i2c bus (address optional for master)
20
+}
21
+
22
+byte val = 0;
23
+
24
+void loop()
25
+{
26
+  Wire.beginTransmission(44); // transmit to device #44 (0x2c)
27
+  // device address is specified in datasheet
28
+  Wire.write(byte(0x00));            // sends instruction byte
29
+  Wire.write(val);             // sends potentiometer value byte
30
+  Wire.endTransmission();     // stop transmitting
31
+
32
+  val++;        // increment value
33
+  if (val == 64) // if reached 64th position (max)
34
+  {
35
+    val = 0;    // start over from lowest value
36
+  }
37
+  delay(500);
38
+}
39
+

+ 32
- 0
ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/Wire/examples/master_reader/master_reader.ino View File

@@ -0,0 +1,32 @@
1
+// Wire Master Reader
2
+// by Nicholas Zambetti <http://www.zambetti.com>
3
+
4
+// Demonstrates use of the Wire library
5
+// Reads data from an I2C/TWI slave device
6
+// Refer to the "Wire Slave Sender" example for use with this
7
+
8
+// Created 29 March 2006
9
+
10
+// This example code is in the public domain.
11
+
12
+
13
+#include <Wire.h>
14
+
15
+void setup()
16
+{
17
+  Wire.begin();        // join i2c bus (address optional for master)
18
+  Serial.begin(9600);  // start serial for output
19
+}
20
+
21
+void loop()
22
+{
23
+  Wire.requestFrom(2, 6);    // request 6 bytes from slave device #2
24
+
25
+  while (Wire.available())   // slave may send less than requested
26
+  {
27
+    char c = Wire.read(); // receive a byte as character
28
+    Serial.print(c);         // print the character
29
+  }
30
+
31
+  delay(500);
32
+}

+ 31
- 0
ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/Wire/examples/master_writer/master_writer.ino View File

@@ -0,0 +1,31 @@
1
+// Wire Master Writer
2
+// by Nicholas Zambetti <http://www.zambetti.com>
3
+
4
+// Demonstrates use of the Wire library
5
+// Writes data to an I2C/TWI slave device
6
+// Refer to the "Wire Slave Receiver" example for use with this
7
+
8
+// Created 29 March 2006
9
+
10
+// This example code is in the public domain.
11
+
12
+
13
+#include <Wire.h>
14
+
15
+void setup()
16
+{
17
+  Wire.begin(); // join i2c bus (address optional for master)
18
+}
19
+
20
+byte x = 0;
21
+
22
+void loop()
23
+{
24
+  Wire.beginTransmission(4); // transmit to device #4
25
+  Wire.write("x is ");        // sends five bytes
26
+  Wire.write(x);              // sends one byte
27
+  Wire.endTransmission();    // stop transmitting
28
+
29
+  x++;
30
+  delay(500);
31
+}

+ 38
- 0
ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/Wire/examples/slave_receiver/slave_receiver.ino View File

@@ -0,0 +1,38 @@
1
+// Wire Slave Receiver
2
+// by Nicholas Zambetti <http://www.zambetti.com>
3
+
4
+// Demonstrates use of the Wire library
5
+// Receives data as an I2C/TWI slave device
6
+// Refer to the "Wire Master Writer" example for use with this
7
+
8
+// Created 29 March 2006
9
+
10
+// This example code is in the public domain.
11
+
12
+
13
+#include <Wire.h>
14
+
15
+void setup()
16
+{
17
+  Wire.begin(4);                // join i2c bus with address #4
18
+  Wire.onReceive(receiveEvent); // register event
19
+  Serial.begin(9600);           // start serial for output
20
+}
21
+
22
+void loop()
23
+{
24
+  delay(100);
25
+}
26
+
27
+// function that executes whenever data is received from master
28
+// this function is registered as an event, see setup()
29
+void receiveEvent(int howMany)
30
+{
31
+  while (1 < Wire.available()) // loop through all but the last
32
+  {
33
+    char c = Wire.read(); // receive byte as a character
34
+    Serial.print(c);         // print the character
35
+  }
36
+  int x = Wire.read();    // receive byte as an integer
37
+  Serial.println(x);         // print the integer
38
+}

+ 32
- 0
ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/Wire/examples/slave_sender/slave_sender.ino View File

@@ -0,0 +1,32 @@
1
+// Wire Slave Sender
2
+// by Nicholas Zambetti <http://www.zambetti.com>
3
+
4
+// Demonstrates use of the Wire library
5
+// Sends data as an I2C/TWI slave device
6
+// Refer to the "Wire Master Reader" example for use with this
7
+
8
+// Created 29 March 2006
9
+
10
+// This example code is in the public domain.
11
+
12
+
13
+#include <Wire.h>
14
+
15
+void setup()
16
+{
17
+  Wire.begin(2);                // join i2c bus with address #2
18
+  Wire.onRequest(requestEvent); // register event
19
+}
20
+
21
+void loop()
22
+{
23
+  delay(100);
24
+}
25
+
26
+// function that executes whenever data is requested by master
27
+// this function is registered as an event, see setup()
28
+void requestEvent()
29
+{
30
+  Wire.write("hello "); // respond with message of 6 bytes
31
+  // as expected by master
32
+}

+ 32
- 0
ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/Wire/keywords.txt View File

@@ -0,0 +1,32 @@
1
+#######################################
2
+# Syntax Coloring Map For Wire
3
+#######################################
4
+
5
+#######################################
6
+# Datatypes (KEYWORD1)
7
+#######################################
8
+
9
+#######################################
10
+# Methods and Functions (KEYWORD2)
11
+#######################################
12
+
13
+begin	KEYWORD2
14
+setClock	KEYWORD2
15
+beginTransmission	KEYWORD2
16
+endTransmission	KEYWORD2
17
+requestFrom	KEYWORD2
18
+send	KEYWORD2
19
+receive	KEYWORD2
20
+onReceive	KEYWORD2
21
+onRequest	KEYWORD2
22
+
23
+#######################################
24
+# Instances (KEYWORD2)
25
+#######################################
26
+
27
+Wire	KEYWORD2
28
+
29
+#######################################
30
+# Constants (LITERAL1)
31
+#######################################
32
+

+ 8
- 0
ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/Wire/library.properties View File

@@ -0,0 +1,8 @@
1
+name=Wire
2
+version=1.0
3
+author=Arduino
4
+maintainer=Arduino <info@arduino.cc>
5
+sentence=Allows the communication between devices or sensors connected via Two Wire Interface Bus. For all Arduino boards, BUT Arduino DUE. 
6
+paragraph=
7
+url=http://arduino.cc/en/Reference/Wire
8
+architectures=avr

+ 527
- 0
ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/Wire/utility/twi.c View File

@@ -0,0 +1,527 @@
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
+  Modified 2012 by Todd Krein (todd@krein.org) to implement repeated starts
20
+*/
21
+
22
+#include <math.h>
23
+#include <stdlib.h>
24
+#include <inttypes.h>
25
+#include <avr/io.h>
26
+#include <avr/interrupt.h>
27
+#include <compat/twi.h>
28
+#include "Arduino.h" // for digitalWrite
29
+
30
+#ifndef cbi
31
+#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
32
+#endif
33
+
34
+#ifndef sbi
35
+#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
36
+#endif
37
+
38
+#include "pins_arduino.h"
39
+#include "twi.h"
40
+
41
+static volatile uint8_t twi_state;
42
+static volatile uint8_t twi_slarw;
43
+static volatile uint8_t twi_sendStop;			// should the transaction end with a stop
44
+static volatile uint8_t twi_inRepStart;			// in the middle of a repeated start
45
+
46
+static void (*twi_onSlaveTransmit)(void);
47
+static void (*twi_onSlaveReceive)(uint8_t*, int);
48
+
49
+static uint8_t twi_masterBuffer[TWI_BUFFER_LENGTH];
50
+static volatile uint8_t twi_masterBufferIndex;
51
+static volatile uint8_t twi_masterBufferLength;
52
+
53
+static uint8_t twi_txBuffer[TWI_BUFFER_LENGTH];
54
+static volatile uint8_t twi_txBufferIndex;
55
+static volatile uint8_t twi_txBufferLength;
56
+
57
+static uint8_t twi_rxBuffer[TWI_BUFFER_LENGTH];
58
+static volatile uint8_t twi_rxBufferIndex;
59
+
60
+static volatile uint8_t twi_error;
61
+
62
+/* 
63
+ * Function twi_init
64
+ * Desc     readys twi pins and sets twi bitrate
65
+ * Input    none
66
+ * Output   none
67
+ */
68
+void twi_init(void)
69
+{
70
+  // initialize state
71
+  twi_state = TWI_READY;
72
+  twi_sendStop = true;		// default value
73
+  twi_inRepStart = false;
74
+  
75
+  // activate internal pullups for twi.
76
+  digitalWrite(SDA, 1);
77
+  digitalWrite(SCL, 1);
78
+
79
+  // initialize twi prescaler and bit rate
80
+  cbi(TWSR, TWPS0);
81
+  cbi(TWSR, TWPS1);
82
+  TWBR = ((F_CPU / TWI_FREQ) - 16) / 2;
83
+
84
+  /* twi bit rate formula from atmega128 manual pg 204
85
+  SCL Frequency = CPU Clock Frequency / (16 + (2 * TWBR))
86
+  note: TWBR should be 10 or higher for master mode
87
+  It is 72 for a 16mhz Wiring board with 100kHz TWI */
88
+
89
+  // enable twi module, acks, and twi interrupt
90
+  TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA);
91
+}
92
+
93
+/* 
94
+ * Function twi_slaveInit
95
+ * Desc     sets slave address and enables interrupt
96
+ * Input    none
97
+ * Output   none
98
+ */
99
+void twi_setAddress(uint8_t address)
100
+{
101
+  // set twi slave address (skip over TWGCE bit)
102
+  TWAR = address << 1;
103
+}
104
+
105
+/* 
106
+ * Function twi_readFrom
107
+ * Desc     attempts to become twi bus master and read a
108
+ *          series of bytes from a device on the bus
109
+ * Input    address: 7bit i2c device address
110
+ *          data: pointer to byte array
111
+ *          length: number of bytes to read into array
112
+ *          sendStop: Boolean indicating whether to send a stop at the end
113
+ * Output   number of bytes read
114
+ */
115
+uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sendStop)
116
+{
117
+  uint8_t i;
118
+
119
+  // ensure data will fit into buffer
120
+  if(TWI_BUFFER_LENGTH < length){
121
+    return 0;
122
+  }
123
+
124
+  // wait until twi is ready, become master receiver
125
+  while(TWI_READY != twi_state){
126
+    continue;
127
+  }
128
+  twi_state = TWI_MRX;
129
+  twi_sendStop = sendStop;
130
+  // reset error state (0xFF.. no error occured)
131
+  twi_error = 0xFF;
132
+
133
+  // initialize buffer iteration vars
134
+  twi_masterBufferIndex = 0;
135
+  twi_masterBufferLength = length-1;  // This is not intuitive, read on...
136
+  // On receive, the previously configured ACK/NACK setting is transmitted in
137
+  // response to the received byte before the interrupt is signalled. 
138
+  // Therefor we must actually set NACK when the _next_ to last byte is
139
+  // received, causing that NACK to be sent in response to receiving the last
140
+  // expected byte of data.
141
+
142
+  // build sla+w, slave device address + w bit
143
+  twi_slarw = TW_READ;
144
+  twi_slarw |= address << 1;
145
+
146
+  if (true == twi_inRepStart) {
147
+    // if we're in the repeated start state, then we've already sent the start,
148
+    // (@@@ we hope), and the TWI statemachine is just waiting for the address byte.
149
+    // We need to remove ourselves from the repeated start state before we enable interrupts,
150
+    // since the ISR is ASYNC, and we could get confused if we hit the ISR before cleaning
151
+    // up. Also, don't enable the START interrupt. There may be one pending from the 
152
+    // repeated start that we sent outselves, and that would really confuse things.
153
+    twi_inRepStart = false;			// remember, we're dealing with an ASYNC ISR
154
+    TWDR = twi_slarw;
155
+    TWCR = _BV(TWINT) | _BV(TWEA) | _BV(TWEN) | _BV(TWIE);	// enable INTs, but not START
156
+  }
157
+  else
158
+    // send start condition
159
+    TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA);
160
+
161
+  // wait for read operation to complete
162
+  while(TWI_MRX == twi_state){
163
+    continue;
164
+  }
165
+
166
+  if (twi_masterBufferIndex < length)
167
+    length = twi_masterBufferIndex;
168
+
169
+  // copy twi buffer to data
170
+  for(i = 0; i < length; ++i){
171
+    data[i] = twi_masterBuffer[i];
172
+  }
173
+	
174
+  return length;
175
+}
176
+
177
+/* 
178
+ * Function twi_writeTo
179
+ * Desc     attempts to become twi bus master and write a
180
+ *          series of bytes to a device on the bus
181
+ * Input    address: 7bit i2c device address
182
+ *          data: pointer to byte array
183
+ *          length: number of bytes in array
184
+ *          wait: boolean indicating to wait for write or not
185
+ *          sendStop: boolean indicating whether or not to send a stop at the end
186
+ * Output   0 .. success
187
+ *          1 .. length to long for buffer
188
+ *          2 .. address send, NACK received
189
+ *          3 .. data send, NACK received
190
+ *          4 .. other twi error (lost bus arbitration, bus error, ..)
191
+ */
192
+uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait, uint8_t sendStop)
193
+{
194
+  uint8_t i;
195
+
196
+  // ensure data will fit into buffer
197
+  if(TWI_BUFFER_LENGTH < length){
198
+    return 1;
199
+  }
200
+
201
+  // wait until twi is ready, become master transmitter
202
+  while(TWI_READY != twi_state){
203
+    continue;
204
+  }
205
+  twi_state = TWI_MTX;
206
+  twi_sendStop = sendStop;
207
+  // reset error state (0xFF.. no error occured)
208
+  twi_error = 0xFF;
209
+
210
+  // initialize buffer iteration vars
211
+  twi_masterBufferIndex = 0;
212
+  twi_masterBufferLength = length;
213
+  
214
+  // copy data to twi buffer
215
+  for(i = 0; i < length; ++i){
216
+    twi_masterBuffer[i] = data[i];
217
+  }
218
+  
219
+  // build sla+w, slave device address + w bit
220
+  twi_slarw = TW_WRITE;
221
+  twi_slarw |= address << 1;
222
+  
223
+  // if we're in a repeated start, then we've already sent the START
224
+  // in the ISR. Don't do it again.
225
+  //
226
+  if (true == twi_inRepStart) {
227
+    // if we're in the repeated start state, then we've already sent the start,
228
+    // (@@@ we hope), and the TWI statemachine is just waiting for the address byte.
229
+    // We need to remove ourselves from the repeated start state before we enable interrupts,
230
+    // since the ISR is ASYNC, and we could get confused if we hit the ISR before cleaning
231
+    // up. Also, don't enable the START interrupt. There may be one pending from the 
232
+    // repeated start that we sent outselves, and that would really confuse things.
233
+    twi_inRepStart = false;			// remember, we're dealing with an ASYNC ISR
234
+    TWDR = twi_slarw;				
235
+    TWCR = _BV(TWINT) | _BV(TWEA) | _BV(TWEN) | _BV(TWIE);	// enable INTs, but not START
236
+  }
237
+  else
238
+    // send start condition
239
+    TWCR = _BV(TWINT) | _BV(TWEA) | _BV(TWEN) | _BV(TWIE) | _BV(TWSTA);	// enable INTs
240
+
241
+  // wait for write operation to complete
242
+  while(wait && (TWI_MTX == twi_state)){
243
+    continue;
244
+  }
245
+  
246
+  if (twi_error == 0xFF)
247
+    return 0;	// success
248
+  else if (twi_error == TW_MT_SLA_NACK)
249
+    return 2;	// error: address send, nack received
250
+  else if (twi_error == TW_MT_DATA_NACK)
251
+    return 3;	// error: data send, nack received
252
+  else
253
+    return 4;	// other twi error
254
+}
255
+
256
+/* 
257
+ * Function twi_transmit
258
+ * Desc     fills slave tx buffer with data
259
+ *          must be called in slave tx event callback
260
+ * Input    data: pointer to byte array
261
+ *          length: number of bytes in array
262
+ * Output   1 length too long for buffer
263
+ *          2 not slave transmitter
264
+ *          0 ok
265
+ */
266
+uint8_t twi_transmit(const uint8_t* data, uint8_t length)
267
+{
268
+  uint8_t i;
269
+
270
+  // ensure data will fit into buffer
271
+  if(TWI_BUFFER_LENGTH < length){
272
+    return 1;
273
+  }
274
+  
275
+  // ensure we are currently a slave transmitter
276
+  if(TWI_STX != twi_state){
277
+    return 2;
278
+  }
279
+  
280
+  // set length and copy data into tx buffer
281
+  twi_txBufferLength = length;
282
+  for(i = 0; i < length; ++i){
283
+    twi_txBuffer[i] = data[i];
284
+  }
285
+  
286
+  return 0;
287
+}
288
+
289
+/* 
290
+ * Function twi_attachSlaveRxEvent
291
+ * Desc     sets function called before a slave read operation
292
+ * Input    function: callback function to use
293
+ * Output   none
294
+ */
295
+void twi_attachSlaveRxEvent( void (*function)(uint8_t*, int) )
296
+{
297
+  twi_onSlaveReceive = function;
298
+}
299
+
300
+/* 
301
+ * Function twi_attachSlaveTxEvent
302
+ * Desc     sets function called before a slave write operation
303
+ * Input    function: callback function to use
304
+ * Output   none
305
+ */
306
+void twi_attachSlaveTxEvent( void (*function)(void) )
307
+{
308
+  twi_onSlaveTransmit = function;
309
+}
310
+
311
+/* 
312
+ * Function twi_reply
313
+ * Desc     sends byte or readys receive line
314
+ * Input    ack: byte indicating to ack or to nack
315
+ * Output   none
316
+ */
317
+void twi_reply(uint8_t ack)
318
+{
319
+  // transmit master read ready signal, with or without ack
320
+  if(ack){
321
+    TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT) | _BV(TWEA);
322
+  }else{
323
+	  TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT);
324
+  }
325
+}
326
+
327
+/* 
328
+ * Function twi_stop
329
+ * Desc     relinquishes bus master status
330
+ * Input    none
331
+ * Output   none
332
+ */
333
+void twi_stop(void)
334
+{
335
+  // send stop condition
336
+  TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTO);
337
+
338
+  // wait for stop condition to be exectued on bus
339
+  // TWINT is not set after a stop condition!
340
+  while(TWCR & _BV(TWSTO)){
341
+    continue;
342
+  }
343
+
344
+  // update twi state
345
+  twi_state = TWI_READY;
346
+}
347
+
348
+/* 
349
+ * Function twi_releaseBus
350
+ * Desc     releases bus control
351
+ * Input    none
352
+ * Output   none
353
+ */
354
+void twi_releaseBus(void)
355
+{
356
+  // release bus
357
+  TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT);
358
+
359
+  // update twi state
360
+  twi_state = TWI_READY;
361
+}
362
+
363
+ISR(TWI_vect)
364
+{
365
+  switch(TW_STATUS){
366
+    // All Master
367
+    case TW_START:     // sent start condition
368
+    case TW_REP_START: // sent repeated start condition
369
+      // copy device address and r/w bit to output register and ack
370
+      TWDR = twi_slarw;
371
+      twi_reply(1);
372
+      break;
373
+
374
+    // Master Transmitter
375
+    case TW_MT_SLA_ACK:  // slave receiver acked address
376
+    case TW_MT_DATA_ACK: // slave receiver acked data
377
+      // if there is data to send, send it, otherwise stop 
378
+      if(twi_masterBufferIndex < twi_masterBufferLength){
379
+        // copy data to output register and ack
380
+        TWDR = twi_masterBuffer[twi_masterBufferIndex++];
381
+        twi_reply(1);
382
+      }else{
383
+	if (twi_sendStop)
384
+          twi_stop();
385
+	else {
386
+	  twi_inRepStart = true;	// we're gonna send the START
387
+	  // don't enable the interrupt. We'll generate the start, but we 
388
+	  // avoid handling the interrupt until we're in the next transaction,
389
+	  // at the point where we would normally issue the start.
390
+	  TWCR = _BV(TWINT) | _BV(TWSTA)| _BV(TWEN) ;
391
+	  twi_state = TWI_READY;
392
+	}
393
+      }
394
+      break;
395
+    case TW_MT_SLA_NACK:  // address sent, nack received
396
+      twi_error = TW_MT_SLA_NACK;
397
+      twi_stop();
398
+      break;
399
+    case TW_MT_DATA_NACK: // data sent, nack received
400
+      twi_error = TW_MT_DATA_NACK;
401
+      twi_stop();
402
+      break;
403
+    case TW_MT_ARB_LOST: // lost bus arbitration
404
+      twi_error = TW_MT_ARB_LOST;
405
+      twi_releaseBus();
406
+      break;
407
+
408
+    // Master Receiver
409
+    case TW_MR_DATA_ACK: // data received, ack sent
410
+      // put byte into buffer
411
+      twi_masterBuffer[twi_masterBufferIndex++] = TWDR;
412
+    case TW_MR_SLA_ACK:  // address sent, ack received
413
+      // ack if more bytes are expected, otherwise nack
414
+      if(twi_masterBufferIndex < twi_masterBufferLength){
415
+        twi_reply(1);
416
+      }else{
417
+        twi_reply(0);
418
+      }
419
+      break;
420
+    case TW_MR_DATA_NACK: // data received, nack sent
421
+      // put final byte into buffer
422
+      twi_masterBuffer[twi_masterBufferIndex++] = TWDR;
423
+	if (twi_sendStop)
424
+          twi_stop();
425
+	else {
426
+	  twi_inRepStart = true;	// we're gonna send the START
427
+	  // don't enable the interrupt. We'll generate the start, but we 
428
+	  // avoid handling the interrupt until we're in the next transaction,
429
+	  // at the point where we would normally issue the start.
430
+	  TWCR = _BV(TWINT) | _BV(TWSTA)| _BV(TWEN) ;
431
+	  twi_state = TWI_READY;
432
+	}    
433
+	break;
434
+    case TW_MR_SLA_NACK: // address sent, nack received
435
+      twi_stop();
436
+      break;
437
+    // TW_MR_ARB_LOST handled by TW_MT_ARB_LOST case
438
+
439
+    // Slave Receiver
440
+    case TW_SR_SLA_ACK:   // addressed, returned ack
441
+    case TW_SR_GCALL_ACK: // addressed generally, returned ack
442
+    case TW_SR_ARB_LOST_SLA_ACK:   // lost arbitration, returned ack
443
+    case TW_SR_ARB_LOST_GCALL_ACK: // lost arbitration, returned ack
444
+      // enter slave receiver mode
445
+      twi_state = TWI_SRX;
446
+      // indicate that rx buffer can be overwritten and ack
447
+      twi_rxBufferIndex = 0;
448
+      twi_reply(1);
449
+      break;
450
+    case TW_SR_DATA_ACK:       // data received, returned ack
451
+    case TW_SR_GCALL_DATA_ACK: // data received generally, returned ack
452
+      // if there is still room in the rx buffer
453
+      if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){
454
+        // put byte in buffer and ack
455
+        twi_rxBuffer[twi_rxBufferIndex++] = TWDR;
456
+        twi_reply(1);
457
+      }else{
458
+        // otherwise nack
459
+        twi_reply(0);
460
+      }
461
+      break;
462
+    case TW_SR_STOP: // stop or repeated start condition received
463
+      // put a null char after data if there's room
464
+      if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){
465
+        twi_rxBuffer[twi_rxBufferIndex] = '\0';
466
+      }
467
+      // sends ack and stops interface for clock stretching
468
+      twi_stop();
469
+      // callback to user defined callback
470
+      twi_onSlaveReceive(twi_rxBuffer, twi_rxBufferIndex);
471
+      // since we submit rx buffer to "wire" library, we can reset it
472
+      twi_rxBufferIndex = 0;
473
+      // ack future responses and leave slave receiver state
474
+      twi_releaseBus();
475
+      break;
476
+    case TW_SR_DATA_NACK:       // data received, returned nack
477
+    case TW_SR_GCALL_DATA_NACK: // data received generally, returned nack
478
+      // nack back at master
479
+      twi_reply(0);
480
+      break;
481
+    
482
+    // Slave Transmitter
483
+    case TW_ST_SLA_ACK:          // addressed, returned ack
484
+    case TW_ST_ARB_LOST_SLA_ACK: // arbitration lost, returned ack
485
+      // enter slave transmitter mode
486
+      twi_state = TWI_STX;
487
+      // ready the tx buffer index for iteration
488
+      twi_txBufferIndex = 0;
489
+      // set tx buffer length to be zero, to verify if user changes it
490
+      twi_txBufferLength = 0;
491
+      // request for txBuffer to be filled and length to be set
492
+      // note: user must call twi_transmit(bytes, length) to do this
493
+      twi_onSlaveTransmit();
494
+      // if they didn't change buffer & length, initialize it
495
+      if(0 == twi_txBufferLength){
496
+        twi_txBufferLength = 1;
497
+        twi_txBuffer[0] = 0x00;
498
+      }
499
+      // transmit first byte from buffer, fall
500
+    case TW_ST_DATA_ACK: // byte sent, ack returned
501
+      // copy data to output register
502
+      TWDR = twi_txBuffer[twi_txBufferIndex++];
503
+      // if there is more to send, ack, otherwise nack
504
+      if(twi_txBufferIndex < twi_txBufferLength){
505
+        twi_reply(1);
506
+      }else{
507
+        twi_reply(0);
508
+      }
509
+      break;
510
+    case TW_ST_DATA_NACK: // received nack, we are done 
511
+    case TW_ST_LAST_DATA: // received ack, but we are done already!
512
+      // ack future responses
513
+      twi_reply(1);
514
+      // leave slave receiver state
515
+      twi_state = TWI_READY;
516
+      break;
517
+
518
+    // All
519
+    case TW_NO_INFO:   // no state information
520
+      break;
521
+    case TW_BUS_ERROR: // bus error, illegal stop/start
522
+      twi_error = TW_BUS_ERROR;
523
+      twi_stop();
524
+      break;
525
+  }
526
+}
527
+

+ 53
- 0
ArduinoAddons/Arduino_1.5.x/hardware/marlin/avr/libraries/Wire/utility/twi.h View File

@@ -0,0 +1,53 @@
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 100000L
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, uint8_t);
44
+  uint8_t twi_writeTo(uint8_t, 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
+

Loading…
Cancel
Save