madrona-rad 0.2.5 → 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +14 -0
- data/Manifest.txt +41 -18
- data/Rakefile +5 -0
- data/bin/rad +16 -6
- data/lib/examples/blink_m_address_assignment.rb +104 -0
- data/lib/examples/blink_m_multi.rb +61 -0
- data/lib/examples/configure_pa_lcd_boot.rb +91 -0
- data/lib/examples/external_variables.rb +5 -1
- data/lib/examples/hello_array.rb +48 -0
- data/lib/examples/hello_array2.rb +79 -0
- data/lib/examples/hello_array_eeprom.rb +61 -0
- data/lib/examples/hello_eeprom.rb +4 -7
- data/lib/examples/hello_eeprom_lcdpa.rb +81 -0
- data/lib/examples/hello_lcd_charset.rb +75 -0
- data/lib/examples/hello_pa_lcd.rb +59 -0
- data/lib/examples/hysteresis_duel.rb +39 -0
- data/lib/examples/motor_knob.rb +30 -0
- data/lib/examples/orig_servo_throttle.rb +1 -1
- data/lib/examples/servo_calibrate_continuous.rb +92 -0
- data/lib/examples/servo_throttle.rb +1 -1
- data/lib/examples/sparkfun_lcd.rb +2 -2
- data/lib/examples/spectra_soft_pot.rb +34 -0
- data/lib/libraries/SWSerLCDpa/SWSerLCDpa.cpp +60 -25
- data/lib/libraries/SWSerLCDpa/SWSerLCDpa.h +8 -2
- data/lib/libraries/SWSerLCDsf/SWSerLCDsf.cpp +46 -27
- data/lib/libraries/SWSerLCDsf/SWSerLCDsf.h +5 -2
- data/lib/libraries/Stepper/Stepper.cpp +220 -0
- data/lib/libraries/Stepper/Stepper.h +86 -0
- data/lib/libraries/Stepper/keywords.txt +28 -0
- data/lib/libraries/Wire/utility/twi.c +449 -0
- data/lib/libraries/Wire/utility/twi.h +57 -0
- data/lib/plugins/blink_m.rb +79 -46
- data/lib/plugins/hysteresis.rb +52 -0
- data/lib/plugins/lcd_padding.rb +39 -0
- data/lib/plugins/spectra_symbol.rb +79 -0
- data/lib/rad/arduino_plugin.rb +21 -0
- data/lib/rad/arduino_sketch.rb +231 -53
- data/lib/rad/init.rb +2 -2
- data/lib/rad/rad_processor.rb +42 -1
- data/lib/rad/rad_type_checker.rb +26 -0
- data/lib/rad/sim/arduino_sketch.rb +57 -0
- data/lib/rad/tasks/build_and_make.rake +4 -4
- data/lib/rad/variable_processing.rb +49 -12
- data/lib/rad/version.rb +1 -1
- data/test/test_array_processing.rb +179 -0
- data/test/test_plugin_loading.rb +151 -0
- data/test/test_translation_post_processing.rb +185 -0
- data/{lib/test → test}/test_variable_processing.rb +63 -13
- data/website/index.html +8 -7
- metadata +66 -30
- data/lib/test/test_array_processing.rb +0 -78
@@ -46,10 +46,13 @@ class SWSerLCDsf
|
|
46
46
|
void print(unsigned long);
|
47
47
|
void print(long, int);
|
48
48
|
void clearscr(void);
|
49
|
-
void
|
49
|
+
void setxy(byte, byte);
|
50
|
+
void clearscr(const char[]);
|
51
|
+
void setxy(byte, byte, const char[]);
|
52
|
+
void clearscr(int);
|
53
|
+
void setxy(byte, byte, int);
|
50
54
|
void setgeo(int);
|
51
55
|
void setintensity(int);
|
52
|
-
void setxy(byte, byte);
|
53
56
|
void setcmd(byte, byte);
|
54
57
|
};
|
55
58
|
|
@@ -0,0 +1,220 @@
|
|
1
|
+
/*
|
2
|
+
Stepper.cpp - - Stepper library for Wiring/Arduino - Version 0.4
|
3
|
+
|
4
|
+
Original library (0.1) by Tom Igoe.
|
5
|
+
Two-wire modifications (0.2) by Sebastian Gassner
|
6
|
+
Combination version (0.3) by Tom Igoe and David Mellis
|
7
|
+
Bug fix for four-wire (0.4) by Tom Igoe, bug fix from Noah Shibley
|
8
|
+
|
9
|
+
Drives a unipolar or bipolar stepper motor using 2 wires or 4 wires
|
10
|
+
|
11
|
+
When wiring multiple stepper motors to a microcontroller,
|
12
|
+
you quickly run out of output pins, with each motor requiring 4 connections.
|
13
|
+
|
14
|
+
By making use of the fact that at any time two of the four motor
|
15
|
+
coils are the inverse of the other two, the number of
|
16
|
+
control connections can be reduced from 4 to 2.
|
17
|
+
|
18
|
+
A slightly modified circuit around a Darlington transistor array or an L293 H-bridge
|
19
|
+
connects to only 2 microcontroler pins, inverts the signals received,
|
20
|
+
and delivers the 4 (2 plus 2 inverted ones) output signals required
|
21
|
+
for driving a stepper motor.
|
22
|
+
|
23
|
+
The sequence of control signals for 4 control wires is as follows:
|
24
|
+
|
25
|
+
Step C0 C1 C2 C3
|
26
|
+
1 1 0 1 0
|
27
|
+
2 0 1 1 0
|
28
|
+
3 0 1 0 1
|
29
|
+
4 1 0 0 1
|
30
|
+
|
31
|
+
The sequence of controls signals for 2 control wires is as follows
|
32
|
+
(columns C1 and C2 from above):
|
33
|
+
|
34
|
+
Step C0 C1
|
35
|
+
1 0 1
|
36
|
+
2 1 1
|
37
|
+
3 1 0
|
38
|
+
4 0 0
|
39
|
+
|
40
|
+
The circuits can be found at
|
41
|
+
|
42
|
+
http://www.arduino.cc/en/Tutorial/Stepper
|
43
|
+
|
44
|
+
|
45
|
+
*/
|
46
|
+
|
47
|
+
|
48
|
+
#include "WProgram.h"
|
49
|
+
#include "Stepper.h"
|
50
|
+
|
51
|
+
/*
|
52
|
+
* two-wire constructor.
|
53
|
+
* Sets which wires should control the motor.
|
54
|
+
*/
|
55
|
+
Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2)
|
56
|
+
{
|
57
|
+
this->step_number = 0; // which step the motor is on
|
58
|
+
this->speed = 0; // the motor speed, in revolutions per minute
|
59
|
+
this->direction = 0; // motor direction
|
60
|
+
this->last_step_time = 0; // time stamp in ms of the last step taken
|
61
|
+
this->number_of_steps = number_of_steps; // total number of steps for this motor
|
62
|
+
|
63
|
+
// Arduino pins for the motor control connection:
|
64
|
+
this->motor_pin_1 = motor_pin_1;
|
65
|
+
this->motor_pin_2 = motor_pin_2;
|
66
|
+
|
67
|
+
// setup the pins on the microcontroller:
|
68
|
+
pinMode(this->motor_pin_1, OUTPUT);
|
69
|
+
pinMode(this->motor_pin_2, OUTPUT);
|
70
|
+
|
71
|
+
// When there are only 2 pins, set the other two to 0:
|
72
|
+
this->motor_pin_3 = 0;
|
73
|
+
this->motor_pin_4 = 0;
|
74
|
+
|
75
|
+
// pin_count is used by the stepMotor() method:
|
76
|
+
this->pin_count = 2;
|
77
|
+
}
|
78
|
+
|
79
|
+
|
80
|
+
/*
|
81
|
+
* constructor for four-pin version
|
82
|
+
* Sets which wires should control the motor.
|
83
|
+
*/
|
84
|
+
|
85
|
+
Stepper::Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, int motor_pin_3, int motor_pin_4)
|
86
|
+
{
|
87
|
+
this->step_number = 0; // which step the motor is on
|
88
|
+
this->speed = 0; // the motor speed, in revolutions per minute
|
89
|
+
this->direction = 0; // motor direction
|
90
|
+
this->last_step_time = 0; // time stamp in ms of the last step taken
|
91
|
+
this->number_of_steps = number_of_steps; // total number of steps for this motor
|
92
|
+
|
93
|
+
// Arduino pins for the motor control connection:
|
94
|
+
this->motor_pin_1 = motor_pin_1;
|
95
|
+
this->motor_pin_2 = motor_pin_2;
|
96
|
+
this->motor_pin_3 = motor_pin_3;
|
97
|
+
this->motor_pin_4 = motor_pin_4;
|
98
|
+
|
99
|
+
// setup the pins on the microcontroller:
|
100
|
+
pinMode(this->motor_pin_1, OUTPUT);
|
101
|
+
pinMode(this->motor_pin_2, OUTPUT);
|
102
|
+
pinMode(this->motor_pin_3, OUTPUT);
|
103
|
+
pinMode(this->motor_pin_4, OUTPUT);
|
104
|
+
|
105
|
+
// pin_count is used by the stepMotor() method:
|
106
|
+
this->pin_count = 4;
|
107
|
+
}
|
108
|
+
|
109
|
+
/*
|
110
|
+
Sets the speed in revs per minute
|
111
|
+
|
112
|
+
*/
|
113
|
+
void Stepper::set_speed(long whatSpeed)
|
114
|
+
{
|
115
|
+
this->step_delay = 60L * 1000L / this->number_of_steps / whatSpeed;
|
116
|
+
}
|
117
|
+
|
118
|
+
/*
|
119
|
+
Moves the motor steps_to_move steps. If the number is negative,
|
120
|
+
the motor moves in the reverse direction.
|
121
|
+
*/
|
122
|
+
void Stepper::set_steps(int steps_to_move)
|
123
|
+
{
|
124
|
+
int steps_left = abs(steps_to_move); // how many steps to take
|
125
|
+
|
126
|
+
// determine direction based on whether steps_to_mode is + or -:
|
127
|
+
if (steps_to_move > 0) {this->direction = 1;}
|
128
|
+
if (steps_to_move < 0) {this->direction = 0;}
|
129
|
+
|
130
|
+
|
131
|
+
// decrement the number of steps, moving one step each time:
|
132
|
+
while(steps_left > 0) {
|
133
|
+
// move only if the appropriate delay has passed:
|
134
|
+
if (millis() - this->last_step_time >= this->step_delay) {
|
135
|
+
// step the motor to step number 0, 1, 2, or 3:
|
136
|
+
stepMotor(this->step_number % 4);
|
137
|
+
// get the timeStamp of when you stepped:
|
138
|
+
this->last_step_time = millis();
|
139
|
+
// increment or decrement the step number,
|
140
|
+
// depending on direction:
|
141
|
+
if (this->direction == 1) {
|
142
|
+
this->step_number++;
|
143
|
+
if (this->step_number == this->number_of_steps) {
|
144
|
+
this->step_number = 0;
|
145
|
+
}
|
146
|
+
}
|
147
|
+
else {
|
148
|
+
if (this->step_number == 0) {
|
149
|
+
this->step_number = this->number_of_steps;
|
150
|
+
}
|
151
|
+
this->step_number--;
|
152
|
+
}
|
153
|
+
// decrement the steps left:
|
154
|
+
steps_left--;
|
155
|
+
}
|
156
|
+
}
|
157
|
+
}
|
158
|
+
|
159
|
+
/*
|
160
|
+
* Moves the motor forward or backwards.
|
161
|
+
*/
|
162
|
+
void Stepper::stepMotor(int thisStep)
|
163
|
+
{
|
164
|
+
if (this->pin_count == 2) {
|
165
|
+
switch (thisStep) {
|
166
|
+
case 0: /* 01 */
|
167
|
+
digitalWrite(motor_pin_1, LOW);
|
168
|
+
digitalWrite(motor_pin_2, HIGH);
|
169
|
+
break;
|
170
|
+
case 1: /* 11 */
|
171
|
+
digitalWrite(motor_pin_1, HIGH);
|
172
|
+
digitalWrite(motor_pin_2, HIGH);
|
173
|
+
break;
|
174
|
+
case 2: /* 10 */
|
175
|
+
digitalWrite(motor_pin_1, HIGH);
|
176
|
+
digitalWrite(motor_pin_2, LOW);
|
177
|
+
break;
|
178
|
+
case 3: /* 00 */
|
179
|
+
digitalWrite(motor_pin_1, LOW);
|
180
|
+
digitalWrite(motor_pin_2, LOW);
|
181
|
+
break;
|
182
|
+
}
|
183
|
+
}
|
184
|
+
if (this->pin_count == 4) {
|
185
|
+
switch (thisStep) {
|
186
|
+
case 0: // 1010
|
187
|
+
digitalWrite(motor_pin_1, HIGH);
|
188
|
+
digitalWrite(motor_pin_2, LOW);
|
189
|
+
digitalWrite(motor_pin_3, HIGH);
|
190
|
+
digitalWrite(motor_pin_4, LOW);
|
191
|
+
break;
|
192
|
+
case 1: // 0110
|
193
|
+
digitalWrite(motor_pin_1, LOW);
|
194
|
+
digitalWrite(motor_pin_2, HIGH);
|
195
|
+
digitalWrite(motor_pin_3, HIGH);
|
196
|
+
digitalWrite(motor_pin_4, LOW);
|
197
|
+
break;
|
198
|
+
case 2: //0101
|
199
|
+
digitalWrite(motor_pin_1, LOW);
|
200
|
+
digitalWrite(motor_pin_2, HIGH);
|
201
|
+
digitalWrite(motor_pin_3, LOW);
|
202
|
+
digitalWrite(motor_pin_4, HIGH);
|
203
|
+
break;
|
204
|
+
case 3: //1001
|
205
|
+
digitalWrite(motor_pin_1, HIGH);
|
206
|
+
digitalWrite(motor_pin_2, LOW);
|
207
|
+
digitalWrite(motor_pin_3, LOW);
|
208
|
+
digitalWrite(motor_pin_4, HIGH);
|
209
|
+
break;
|
210
|
+
}
|
211
|
+
}
|
212
|
+
}
|
213
|
+
|
214
|
+
/*
|
215
|
+
version() returns the version of the library:
|
216
|
+
*/
|
217
|
+
int Stepper::version(void)
|
218
|
+
{
|
219
|
+
return 4;
|
220
|
+
}
|
@@ -0,0 +1,86 @@
|
|
1
|
+
/*
|
2
|
+
Stepper.h - - Stepper library for Wiring/Arduino - Version 0.4
|
3
|
+
|
4
|
+
Original library (0.1) by Tom Igoe.
|
5
|
+
Two-wire modifications (0.2) by Sebastian Gassner
|
6
|
+
Combination version (0.3) by Tom Igoe and David Mellis
|
7
|
+
Bug fix for four-wire (0.4) by Tom Igoe, bug fix from Noah Shibley
|
8
|
+
|
9
|
+
Drives a unipolar or bipolar stepper motor using 2 wires or 4 wires
|
10
|
+
|
11
|
+
When wiring multiple stepper motors to a microcontroller,
|
12
|
+
you quickly run out of output pins, with each motor requiring 4 connections.
|
13
|
+
|
14
|
+
By making use of the fact that at any time two of the four motor
|
15
|
+
coils are the inverse of the other two, the number of
|
16
|
+
control connections can be reduced from 4 to 2.
|
17
|
+
|
18
|
+
A slightly modified circuit around a Darlington transistor array or an L293 H-bridge
|
19
|
+
connects to only 2 microcontroler pins, inverts the signals received,
|
20
|
+
and delivers the 4 (2 plus 2 inverted ones) output signals required
|
21
|
+
for driving a stepper motor.
|
22
|
+
|
23
|
+
The sequence of control signals for 4 control wires is as follows:
|
24
|
+
|
25
|
+
Step C0 C1 C2 C3
|
26
|
+
1 1 0 1 0
|
27
|
+
2 0 1 1 0
|
28
|
+
3 0 1 0 1
|
29
|
+
4 1 0 0 1
|
30
|
+
|
31
|
+
The sequence of controls signals for 2 control wires is as follows
|
32
|
+
(columns C1 and C2 from above):
|
33
|
+
|
34
|
+
Step C0 C1
|
35
|
+
1 0 1
|
36
|
+
2 1 1
|
37
|
+
3 1 0
|
38
|
+
4 0 0
|
39
|
+
|
40
|
+
The circuits can be found at
|
41
|
+
http://www.arduino.cc/en/Tutorial/Stepper
|
42
|
+
*/
|
43
|
+
|
44
|
+
// ensure this library description is only included once
|
45
|
+
#ifndef Stepper_h
|
46
|
+
#define Stepper_h
|
47
|
+
|
48
|
+
// include types & constants of Wiring core API
|
49
|
+
#include "WConstants.h"
|
50
|
+
|
51
|
+
// library interface description
|
52
|
+
class Stepper {
|
53
|
+
public:
|
54
|
+
// constructors:
|
55
|
+
Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2);
|
56
|
+
Stepper(int number_of_steps, int motor_pin_1, int motor_pin_2, int motor_pin_3, int motor_pin_4);
|
57
|
+
|
58
|
+
// speed setter method:
|
59
|
+
void set_speed(long whatSpeed);
|
60
|
+
|
61
|
+
// mover method:
|
62
|
+
void set_steps(int number_of_steps);
|
63
|
+
|
64
|
+
int version(void);
|
65
|
+
|
66
|
+
private:
|
67
|
+
void stepMotor(int this_step);
|
68
|
+
|
69
|
+
int direction; // Direction of rotation
|
70
|
+
int speed; // Speed in RPMs
|
71
|
+
unsigned long step_delay; // delay between steps, in ms, based on speed
|
72
|
+
int number_of_steps; // total number of steps this motor can take
|
73
|
+
int pin_count; // whether you're driving the motor with 2 or 4 pins
|
74
|
+
int step_number; // which step the motor is on
|
75
|
+
|
76
|
+
// motor pin numbers:
|
77
|
+
int motor_pin_1;
|
78
|
+
int motor_pin_2;
|
79
|
+
int motor_pin_3;
|
80
|
+
int motor_pin_4;
|
81
|
+
|
82
|
+
long last_step_time; // time stamp in ms of when the last step was taken
|
83
|
+
};
|
84
|
+
|
85
|
+
#endif
|
86
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
#######################################
|
2
|
+
# Syntax Coloring Map For Test
|
3
|
+
#######################################
|
4
|
+
|
5
|
+
#######################################
|
6
|
+
# Datatypes (KEYWORD1)
|
7
|
+
#######################################
|
8
|
+
|
9
|
+
Stepper KEYWORD1
|
10
|
+
|
11
|
+
#######################################
|
12
|
+
# Methods and Functions (KEYWORD2)
|
13
|
+
#######################################
|
14
|
+
|
15
|
+
step KEYWORD2
|
16
|
+
setSpeed KEYWORD2
|
17
|
+
version KEYWORD2
|
18
|
+
|
19
|
+
######################################
|
20
|
+
# Instances (KEYWORD2)
|
21
|
+
#######################################
|
22
|
+
direction KEYWORD2
|
23
|
+
speed KEYWORD2
|
24
|
+
|
25
|
+
|
26
|
+
#######################################
|
27
|
+
# Constants (LITERAL1)
|
28
|
+
#######################################
|
@@ -0,0 +1,449 @@
|
|
1
|
+
/*
|
2
|
+
twi.c - TWI/I2C library for Wiring & Arduino
|
3
|
+
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
|
4
|
+
|
5
|
+
This library is free software; you can redistribute it and/or
|
6
|
+
modify it under the terms of the GNU Lesser General Public
|
7
|
+
License as published by the Free Software Foundation; either
|
8
|
+
version 2.1 of the License, or (at your option) any later version.
|
9
|
+
|
10
|
+
This library is distributed in the hope that it will be useful,
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
13
|
+
Lesser General Public License for more details.
|
14
|
+
|
15
|
+
You should have received a copy of the GNU Lesser General Public
|
16
|
+
License along with this library; if not, write to the Free Software
|
17
|
+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
18
|
+
*/
|
19
|
+
|
20
|
+
#include <math.h>
|
21
|
+
#include <stdlib.h>
|
22
|
+
#include <inttypes.h>
|
23
|
+
#include <avr/io.h>
|
24
|
+
#include <avr/interrupt.h>
|
25
|
+
#include <avr/signal.h>
|
26
|
+
#include <compat/twi.h>
|
27
|
+
|
28
|
+
#ifndef cbi
|
29
|
+
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
|
30
|
+
#endif
|
31
|
+
|
32
|
+
#ifndef sbi
|
33
|
+
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
|
34
|
+
#endif
|
35
|
+
|
36
|
+
#include "twi.h"
|
37
|
+
|
38
|
+
static volatile uint8_t twi_state;
|
39
|
+
static uint8_t twi_slarw;
|
40
|
+
|
41
|
+
static void (*twi_onSlaveTransmit)(void);
|
42
|
+
static void (*twi_onSlaveReceive)(uint8_t*, int);
|
43
|
+
|
44
|
+
static uint8_t* twi_masterBuffer;
|
45
|
+
static volatile uint8_t twi_masterBufferIndex;
|
46
|
+
static uint8_t twi_masterBufferLength;
|
47
|
+
|
48
|
+
static uint8_t* twi_txBuffer;
|
49
|
+
static volatile uint8_t twi_txBufferIndex;
|
50
|
+
static volatile uint8_t twi_txBufferLength;
|
51
|
+
|
52
|
+
static uint8_t* twi_rxBuffer;
|
53
|
+
static volatile uint8_t twi_rxBufferIndex;
|
54
|
+
|
55
|
+
/*
|
56
|
+
* Function twi_init
|
57
|
+
* Desc readys twi pins and sets twi bitrate
|
58
|
+
* Input none
|
59
|
+
* Output none
|
60
|
+
*/
|
61
|
+
void twi_init(void)
|
62
|
+
{
|
63
|
+
// initialize state
|
64
|
+
twi_state = TWI_READY;
|
65
|
+
|
66
|
+
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega8__)
|
67
|
+
// activate internal pull-ups for twi
|
68
|
+
// as per note from atmega8 manual pg167
|
69
|
+
sbi(PORTC, 4);
|
70
|
+
sbi(PORTC, 5);
|
71
|
+
#else
|
72
|
+
// activate internal pull-ups for twi
|
73
|
+
// as per note from atmega128 manual pg204
|
74
|
+
sbi(PORTD, 0);
|
75
|
+
sbi(PORTD, 1);
|
76
|
+
#endif
|
77
|
+
|
78
|
+
// initialize twi prescaler and bit rate
|
79
|
+
cbi(TWSR, TWPS0);
|
80
|
+
cbi(TWSR, TWPS1);
|
81
|
+
TWBR = ((CPU_FREQ / TWI_FREQ) - 16) / 2;
|
82
|
+
|
83
|
+
/* twi bit rate formula from atmega128 manual pg 204
|
84
|
+
SCL Frequency = CPU Clock Frequency / (16 + (2 * TWBR))
|
85
|
+
note: TWBR should be 10 or higher for master mode
|
86
|
+
It is 72 for a 16mhz Wiring board with 100kHz TWI */
|
87
|
+
|
88
|
+
// enable twi module, acks, and twi interrupt
|
89
|
+
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA);
|
90
|
+
|
91
|
+
// allocate buffers
|
92
|
+
twi_masterBuffer = (uint8_t*) calloc(TWI_BUFFER_LENGTH, sizeof(uint8_t));
|
93
|
+
twi_txBuffer = (uint8_t*) calloc(TWI_BUFFER_LENGTH, sizeof(uint8_t));
|
94
|
+
twi_rxBuffer = (uint8_t*) calloc(TWI_BUFFER_LENGTH, sizeof(uint8_t));
|
95
|
+
}
|
96
|
+
|
97
|
+
/*
|
98
|
+
* Function twi_slaveInit
|
99
|
+
* Desc sets slave address and enables interrupt
|
100
|
+
* Input none
|
101
|
+
* Output none
|
102
|
+
*/
|
103
|
+
void twi_setAddress(uint8_t address)
|
104
|
+
{
|
105
|
+
// set twi slave address (skip over TWGCE bit)
|
106
|
+
TWAR = address << 1;
|
107
|
+
}
|
108
|
+
|
109
|
+
/*
|
110
|
+
* Function twi_readFrom
|
111
|
+
* Desc attempts to become twi bus master and read a
|
112
|
+
* series of bytes from a device on the bus
|
113
|
+
* Input address: 7bit i2c device address
|
114
|
+
* data: pointer to byte array
|
115
|
+
* length: number of bytes to read into array
|
116
|
+
* Output byte: 0 ok, 1 length too long for buffer
|
117
|
+
*/
|
118
|
+
uint8_t twi_readFrom(uint8_t address, uint8_t* data, uint8_t length)
|
119
|
+
{
|
120
|
+
uint8_t i;
|
121
|
+
|
122
|
+
// ensure data will fit into buffer
|
123
|
+
if(TWI_BUFFER_LENGTH < length){
|
124
|
+
return 1;
|
125
|
+
}
|
126
|
+
|
127
|
+
// wait until twi is ready, become master receiver
|
128
|
+
while(TWI_READY != twi_state){
|
129
|
+
continue;
|
130
|
+
}
|
131
|
+
twi_state = TWI_MRX;
|
132
|
+
|
133
|
+
// initialize buffer iteration vars
|
134
|
+
twi_masterBufferIndex = 0;
|
135
|
+
twi_masterBufferLength = length;
|
136
|
+
|
137
|
+
// build sla+w, slave device address + w bit
|
138
|
+
twi_slarw = TW_READ;
|
139
|
+
twi_slarw |= address << 1;
|
140
|
+
|
141
|
+
// send start condition
|
142
|
+
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA);
|
143
|
+
|
144
|
+
// wait for read operation to complete
|
145
|
+
while(TWI_MRX == twi_state){
|
146
|
+
continue;
|
147
|
+
}
|
148
|
+
|
149
|
+
// copy twi buffer to data
|
150
|
+
for(i = 0; i < length; ++i){
|
151
|
+
data[i] = twi_masterBuffer[i];
|
152
|
+
}
|
153
|
+
|
154
|
+
return 0;
|
155
|
+
}
|
156
|
+
|
157
|
+
/*
|
158
|
+
* Function twi_writeTo
|
159
|
+
* Desc attempts to become twi bus master and write a
|
160
|
+
* series of bytes to a device on the bus
|
161
|
+
* Input address: 7bit i2c device address
|
162
|
+
* data: pointer to byte array
|
163
|
+
* length: number of bytes in array
|
164
|
+
* wait: boolean indicating to wait for write or not
|
165
|
+
* Output byte: 0 ok, 1 length too long for buffer
|
166
|
+
*/
|
167
|
+
uint8_t twi_writeTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t wait)
|
168
|
+
{
|
169
|
+
uint8_t i;
|
170
|
+
|
171
|
+
// ensure data will fit into buffer
|
172
|
+
if(TWI_BUFFER_LENGTH < length){
|
173
|
+
return 1;
|
174
|
+
}
|
175
|
+
|
176
|
+
// wait until twi is ready, become master transmitter
|
177
|
+
while(TWI_READY != twi_state){
|
178
|
+
continue;
|
179
|
+
}
|
180
|
+
twi_state = TWI_MTX;
|
181
|
+
|
182
|
+
// initialize buffer iteration vars
|
183
|
+
twi_masterBufferIndex = 0;
|
184
|
+
twi_masterBufferLength = length;
|
185
|
+
|
186
|
+
// copy data to twi buffer
|
187
|
+
for(i = 0; i < length; ++i){
|
188
|
+
twi_masterBuffer[i] = data[i];
|
189
|
+
}
|
190
|
+
|
191
|
+
// build sla+w, slave device address + w bit
|
192
|
+
twi_slarw = TW_WRITE;
|
193
|
+
twi_slarw |= address << 1;
|
194
|
+
|
195
|
+
// send start condition
|
196
|
+
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTA);
|
197
|
+
|
198
|
+
// wait for write operation to complete
|
199
|
+
while(wait && (TWI_MTX == twi_state)){
|
200
|
+
continue;
|
201
|
+
}
|
202
|
+
|
203
|
+
return 0;
|
204
|
+
}
|
205
|
+
|
206
|
+
/*
|
207
|
+
* Function twi_transmit
|
208
|
+
* Desc fills slave tx buffer with data
|
209
|
+
* must be called in slave tx event callback
|
210
|
+
* Input data: pointer to byte array
|
211
|
+
* length: number of bytes in array
|
212
|
+
* Output 1 length too long for buffer
|
213
|
+
* 2 not slave transmitter
|
214
|
+
* 0 ok
|
215
|
+
*/
|
216
|
+
uint8_t twi_transmit(uint8_t* data, uint8_t length)
|
217
|
+
{
|
218
|
+
uint8_t i;
|
219
|
+
|
220
|
+
// ensure data will fit into buffer
|
221
|
+
if(TWI_BUFFER_LENGTH < length){
|
222
|
+
return 1;
|
223
|
+
}
|
224
|
+
|
225
|
+
// ensure we are currently a slave transmitter
|
226
|
+
if(TWI_STX != twi_state){
|
227
|
+
return 2;
|
228
|
+
}
|
229
|
+
|
230
|
+
// set length and copy data into tx buffer
|
231
|
+
twi_txBufferLength = length;
|
232
|
+
for(i = 0; i < length; ++i){
|
233
|
+
twi_txBuffer[i] = data[i];
|
234
|
+
}
|
235
|
+
|
236
|
+
return 0;
|
237
|
+
}
|
238
|
+
|
239
|
+
/*
|
240
|
+
* Function twi_attachSlaveRxEvent
|
241
|
+
* Desc sets function called before a slave read operation
|
242
|
+
* Input function: callback function to use
|
243
|
+
* Output none
|
244
|
+
*/
|
245
|
+
void twi_attachSlaveRxEvent( void (*function)(uint8_t*, int) )
|
246
|
+
{
|
247
|
+
twi_onSlaveReceive = function;
|
248
|
+
}
|
249
|
+
|
250
|
+
/*
|
251
|
+
* Function twi_attachSlaveTxEvent
|
252
|
+
* Desc sets function called before a slave write operation
|
253
|
+
* Input function: callback function to use
|
254
|
+
* Output none
|
255
|
+
*/
|
256
|
+
void twi_attachSlaveTxEvent( void (*function)(void) )
|
257
|
+
{
|
258
|
+
twi_onSlaveTransmit = function;
|
259
|
+
}
|
260
|
+
|
261
|
+
/*
|
262
|
+
* Function twi_reply
|
263
|
+
* Desc sends byte or readys receive line
|
264
|
+
* Input ack: byte indicating to ack or to nack
|
265
|
+
* Output none
|
266
|
+
*/
|
267
|
+
void twi_reply(uint8_t ack)
|
268
|
+
{
|
269
|
+
// transmit master read ready signal, with or without ack
|
270
|
+
if(ack){
|
271
|
+
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT) | _BV(TWEA);
|
272
|
+
}else{
|
273
|
+
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWINT);
|
274
|
+
}
|
275
|
+
}
|
276
|
+
|
277
|
+
/*
|
278
|
+
* Function twi_stop
|
279
|
+
* Desc relinquishes bus master status
|
280
|
+
* Input none
|
281
|
+
* Output none
|
282
|
+
*/
|
283
|
+
void twi_stop(void)
|
284
|
+
{
|
285
|
+
// send stop condition
|
286
|
+
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT) | _BV(TWSTO);
|
287
|
+
|
288
|
+
// wait for stop condition to be exectued on bus
|
289
|
+
// TWINT is not set after a stop condition!
|
290
|
+
while(TWCR & _BV(TWSTO)){
|
291
|
+
continue;
|
292
|
+
}
|
293
|
+
|
294
|
+
// update twi state
|
295
|
+
twi_state = TWI_READY;
|
296
|
+
}
|
297
|
+
|
298
|
+
/*
|
299
|
+
* Function twi_releaseBus
|
300
|
+
* Desc releases bus control
|
301
|
+
* Input none
|
302
|
+
* Output none
|
303
|
+
*/
|
304
|
+
void twi_releaseBus(void)
|
305
|
+
{
|
306
|
+
// release bus
|
307
|
+
TWCR = _BV(TWEN) | _BV(TWIE) | _BV(TWEA) | _BV(TWINT);
|
308
|
+
|
309
|
+
// update twi state
|
310
|
+
twi_state = TWI_READY;
|
311
|
+
}
|
312
|
+
|
313
|
+
SIGNAL(SIG_2WIRE_SERIAL)
|
314
|
+
{
|
315
|
+
switch(TW_STATUS){
|
316
|
+
// All Master
|
317
|
+
case TW_START: // sent start condition
|
318
|
+
case TW_REP_START: // sent repeated start condition
|
319
|
+
// copy device address and r/w bit to output register and ack
|
320
|
+
TWDR = twi_slarw;
|
321
|
+
twi_reply(1);
|
322
|
+
break;
|
323
|
+
|
324
|
+
// Master Transmitter
|
325
|
+
case TW_MT_SLA_ACK: // slave receiver acked address
|
326
|
+
case TW_MT_DATA_ACK: // slave receiver acked data
|
327
|
+
// if there is data to send, send it, otherwise stop
|
328
|
+
if(twi_masterBufferIndex < twi_masterBufferLength){
|
329
|
+
// copy data to output register and ack
|
330
|
+
TWDR = twi_masterBuffer[twi_masterBufferIndex++];
|
331
|
+
twi_reply(1);
|
332
|
+
}else{
|
333
|
+
twi_stop();
|
334
|
+
}
|
335
|
+
break;
|
336
|
+
case TW_MT_SLA_NACK: // address sent, nack received
|
337
|
+
case TW_MT_DATA_NACK: // data sent, nack received
|
338
|
+
twi_stop();
|
339
|
+
break;
|
340
|
+
case TW_MT_ARB_LOST: // lost bus arbitration
|
341
|
+
twi_releaseBus();
|
342
|
+
break;
|
343
|
+
|
344
|
+
// Master Receiver
|
345
|
+
case TW_MR_DATA_ACK: // data received, ack sent
|
346
|
+
// put byte into buffer
|
347
|
+
twi_masterBuffer[twi_masterBufferIndex++] = TWDR;
|
348
|
+
case TW_MR_SLA_ACK: // address sent, ack received
|
349
|
+
// ack if more bytes are expected, otherwise nack
|
350
|
+
if(twi_masterBufferIndex < twi_masterBufferLength){
|
351
|
+
twi_reply(1);
|
352
|
+
}else{
|
353
|
+
twi_reply(0);
|
354
|
+
}
|
355
|
+
break;
|
356
|
+
case TW_MR_DATA_NACK: // data received, nack sent
|
357
|
+
// put final byte into buffer
|
358
|
+
twi_masterBuffer[twi_masterBufferIndex++] = TWDR;
|
359
|
+
case TW_MR_SLA_NACK: // address sent, nack received
|
360
|
+
twi_stop();
|
361
|
+
break;
|
362
|
+
// TW_MR_ARB_LOST handled by TW_MT_ARB_LOST case
|
363
|
+
|
364
|
+
// Slave Receiver
|
365
|
+
case TW_SR_SLA_ACK: // addressed, returned ack
|
366
|
+
case TW_SR_GCALL_ACK: // addressed generally, returned ack
|
367
|
+
case TW_SR_ARB_LOST_SLA_ACK: // lost arbitration, returned ack
|
368
|
+
case TW_SR_ARB_LOST_GCALL_ACK: // lost arbitration, returned ack
|
369
|
+
// enter slave receiver mode
|
370
|
+
twi_state = TWI_SRX;
|
371
|
+
// indicate that rx buffer can be overwritten and ack
|
372
|
+
twi_rxBufferIndex = 0;
|
373
|
+
twi_reply(1);
|
374
|
+
break;
|
375
|
+
case TW_SR_DATA_ACK: // data received, returned ack
|
376
|
+
case TW_SR_GCALL_DATA_ACK: // data received generally, returned ack
|
377
|
+
// if there is still room in the rx buffer
|
378
|
+
if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){
|
379
|
+
// put byte in buffer and ack
|
380
|
+
twi_rxBuffer[twi_rxBufferIndex++] = TWDR;
|
381
|
+
twi_reply(1);
|
382
|
+
}else{
|
383
|
+
// otherwise nack
|
384
|
+
twi_reply(0);
|
385
|
+
}
|
386
|
+
break;
|
387
|
+
case TW_SR_STOP: // stop or repeated start condition received
|
388
|
+
// put a null char after data if there's room
|
389
|
+
if(twi_rxBufferIndex < TWI_BUFFER_LENGTH){
|
390
|
+
twi_rxBuffer[twi_rxBufferIndex] = '\0';
|
391
|
+
}
|
392
|
+
// callback to user defined callback
|
393
|
+
twi_onSlaveReceive(twi_rxBuffer, twi_rxBufferIndex);
|
394
|
+
// ack future responses
|
395
|
+
twi_reply(1);
|
396
|
+
// leave slave receiver state
|
397
|
+
twi_state = TWI_READY;
|
398
|
+
break;
|
399
|
+
case TW_SR_DATA_NACK: // data received, returned nack
|
400
|
+
case TW_SR_GCALL_DATA_NACK: // data received generally, returned nack
|
401
|
+
// nack back at master
|
402
|
+
twi_reply(0);
|
403
|
+
break;
|
404
|
+
|
405
|
+
// Slave Transmitter
|
406
|
+
case TW_ST_SLA_ACK: // addressed, returned ack
|
407
|
+
case TW_ST_ARB_LOST_SLA_ACK: // arbitration lost, returned ack
|
408
|
+
// enter slave transmitter mode
|
409
|
+
twi_state = TWI_STX;
|
410
|
+
// ready the tx buffer index for iteration
|
411
|
+
twi_txBufferIndex = 0;
|
412
|
+
// set tx buffer length to be zero, to verify if user changes it
|
413
|
+
twi_txBufferLength = 0;
|
414
|
+
// request for txBuffer to be filled and length to be set
|
415
|
+
// note: user must call twi_transmit(bytes, length) to do this
|
416
|
+
twi_onSlaveTransmit();
|
417
|
+
// if they didn't change buffer & length, initialize it
|
418
|
+
if(0 == twi_txBufferLength){
|
419
|
+
twi_txBufferLength = 1;
|
420
|
+
twi_txBuffer[0] = 0x00;
|
421
|
+
}
|
422
|
+
// transmit first byte from buffer, fall
|
423
|
+
case TW_ST_DATA_ACK: // byte sent, ack returned
|
424
|
+
// copy data to output register
|
425
|
+
TWDR = twi_txBuffer[twi_txBufferIndex++];
|
426
|
+
// if there is more to send, ack, otherwise nack
|
427
|
+
if(twi_txBufferIndex < twi_txBufferLength){
|
428
|
+
twi_reply(1);
|
429
|
+
}else{
|
430
|
+
twi_reply(0);
|
431
|
+
}
|
432
|
+
break;
|
433
|
+
case TW_ST_DATA_NACK: // received nack, we are done
|
434
|
+
case TW_ST_LAST_DATA: // received ack, but we are done already!
|
435
|
+
// ack future responses
|
436
|
+
twi_reply(1);
|
437
|
+
// leave slave receiver state
|
438
|
+
twi_state = TWI_READY;
|
439
|
+
break;
|
440
|
+
|
441
|
+
// All
|
442
|
+
case TW_NO_INFO: // no state information
|
443
|
+
break;
|
444
|
+
case TW_BUS_ERROR: // bus error, illegal stop/start
|
445
|
+
twi_stop();
|
446
|
+
break;
|
447
|
+
}
|
448
|
+
}
|
449
|
+
|