8000 Merge branch 'lacklustrlabs-WireSlave' · Batlapin/Arduino_STM32@5368d04 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5368d04

Browse files
Merge branch 'lacklustrlabs-WireSlave'
2 parents 8a18e09 + 9c61fbd commit 5368d04

File tree

21 files changed

+2648
-0
lines changed

21 files changed

+2648
-0
lines changed

STM32F1/libraries/Wire/Wire_slave.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#error "Something is trying to include Wire_slave.h when Wire.h is already included, they are mutually exclusive"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=Wire
2+
version=1.0
3+
author=Roger Clark
4+
maintainer=
5+
sentence=Allows the communication between devices or sensors connected via Two Wire Interface Bus.
6+
paragraph=
7+
category=Communication
8+
url=http://www.arduino.cc/en/Reference/Wire
9+
architectures=STM32F1
10+
include=Wire.h,SoftWire.h
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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_slave.h>
14+
15+
void setup()
16+
{
17+
Wire.begin(); // join i2c bus (address optional for master)
18+
Serial.begin(115200); // 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+
}
10000 86+
87+
*/
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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_slave.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+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* i2c_slave example.cpp
3+
*
4+
* You can use this sketch in combination with master_writer.pde
5+
*
6+
* Created on: 4 Sep 2012
7+
* Author: Barry Carter <barry.carter@gmail.com>
8+
*/
9+
#include <Wire_slave.h>
10+
#include <Arduino.h>
11+
12+
#define USE_BUFFERED_EXAMPLE 1
13+
14+
i2c_msg msg;
15+
uint8 buffer[255];
16+
char* const bufferAsChar = (char*)buffer; // ready type casted alias
17+
18+
volatile bool newMessage = false;
19+
20+
void funcrx(i2c_msg *msg __attribute__((unused))){
21+
// Received length will be in msg->length
22+
newMessage = true;
23+
}
24+
25+
#if USE_BUFFERED_EXAMPLE == 1
26+
/* We ARE using a buffer to transmit the data out.
27+
* Make sure you fill the buffer with the data AND you set the length correctly
28+
*/
29+
void functx(i2c_msg *msg){
30+
// Cheeky. We are using the received byte of the data which is currently in
31+
// byte 0 to echo it back to the master device
32+
//msg->data[0] = 0x01; // We are re-using the rx buffer here to echo the request back
33+
msg->data[1] = 0x02;
34+
msg->data[2] = 0x03;
35+
msg->data[3] = 0x04;
36+
msg->data[4] = 0x05;
37+
msg->length = 5;
38+
}
39+
40+
#else
41+
42+
/* We are NOT using the buffered data transmission
43+
* We will get this callback for each outgoing packet. Make sure to call i2c_write
44+
* Strickly speaking, we should be sending a NACk on the last byte we want to send
45+
* but for this test example I am going to assume the master will NACK it when it
46+
* wants to stop.
47+
*/
48+
void functx(i2c_msg *msg){
49+
i2c_write(I2C1, msg->data[0]);
50+
}
51+
52+
#endif
53+
54+
void setup() {
55+
Serial.begin(115200);
56+
Serial.println("libmaple I2C slave reader example");
57+
58+
// attach the buffer
59+
msg.data = buffer;
60+
61+
/* Init slave mode. Enables master too
62+
* We are going to configure the slave device to
63+
* - enable fast I2C (400khz)
64+
* - dual addresses (can have 2 addresses per module)
65+
* general call (accepts data writes to 0x00 on a broadcast basis)
66+
*
67+
* If the buffered example is enabled, then we also enable the
68+
* buffer for rx and tx.
69+
* Note you can independently enable/disable RX and TX buffers to
70+
* allow a buffered read and direct writes. Useful if you don't know how
71+
* much the master will read.
72+
*/
73+
#if USE_BUFFERED_EXAMPLE == 1
74+
i2c_slave_enable(I2C1, I2C_FAST_MODE | I2C_SLAVE_DUAL_ADDRESS | I2C_SLAVE_GENERAL_CALL | I2C_SLAVE_USE_RX_BUFFER | I2C_SLAVE_USE_TX_BUFFER);
75+
#else
76+
i2c_slave_enable(I2C1, I2C_FAST_MODE | I2C_SLAVE_DUAL_ADDRESS | I2C_SLAVE_GENERAL_CALL);
77+
#endif
78+
79+
// attach receive handler
80+
i2c_slave_attach_recv_handler(I2C1, &msg, funcrx);
81+
// attach transmit handler
82+
i2c_slave_attach_transmit_handler(I2C1, &msg, functx);
83+
84+
// set addresss to 4
85+
i2c_slave_set_own_address(I2C1, 4);
86+
}
87+
88+
void loop() {
89+
static uint32_t lastMessage = millis();
90+
91+
// This is potentially dangerous.
92+
// We're reading from the live buffer, the content can change
93+
// in the middle of Serial.println
94+
95+
if (newMessage && strlen(bufferAsChar)==6) {
96+
for (int i=0; i<5; i++) {
97+
// Print as char
98+
Serial.print(bufferAsChar[i]);
99+
}
100+
// Print as byte
101+
Serial.println(buffer[5]);
102+
lastMessage = millis();
103+
newMessage = false;
104+
} else {
105+
if(newMessage && strlen(bufferAsChar)!=6) {
106+
// this also happends on the line "x is 0"
107+
Serial.print("Bad data received:");
108+
Serial.println(bufferAsChar);
109+
Serial.print("strlen:");
110+
Serial.println(strlen(bufferAsChar));
111+
newMessage = false;
112+
} else
113+
if(millis() - lastMessage > 3000){
114+
Serial.println("Nothing received in 3 seconds.");
115+
lastMessage = millis();
116+
}
117+
}
118+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// --------------------------------------
2+
// i2c_scanner
3+
//
4+
// Version 1
5+
// This program (or code that looks like it)
6+
// can be found in many places.
7+
// For example on the Arduino.cc forum.
8+
// The original author is not know.
9+
// Version 2, Juni 2012, Using Arduino 1.0.1
10+
// Adapted to be as simple as possible by Arduino.cc user Krodal
11+
// Version 3, Feb 26 2013
12+
// V3 by louarnold
13+
// Version 4, March 3, 2013, Using Arduino 1.0.3
14+
// by Arduino.cc user Krodal.
15+
// Changes by louarnold removed.
16+
// Scanning addresses changed from 0...127 to 1...119,
17+
// according to the i2c scanner by Nick Gammon
18+
// http://www.gammon.com.au/forum/?id=10896
19+
// Version 5, March 28, 2013
20+
// As version 4, but address scans now to 127.
21+
// A sensor seems to use address 120.
22+
//
23+
// This sketch tests the standard 7-bit addresses
24+
// Devices with higher bit address might not be seen properly.
25+
//
26+
27+
#include <Wire_slave.h>
28+
29+
//use IIC2
30+
//TwoWire WIRE2 (2,I2C_FAST_MODE);
31+
//#define Wire WIRE2
32+
33+
34+
void setup() {
35+
36+
Serial.begin(115200);
37+
Wire.begin();
38+
Serial.println("\nI2C Scanner");
39+
}
40+
41+
42+
void loop() {
43+
byte error, address;
44+
int nDevices;
45+
46+
Serial.println("Scanning...");
47+
48+
nDevices = 0;
49+
for(address = 1; address < 127; address++) {
50+
// The i2c_scanner uses the return value of
51+
// the Write.endTransmisstion to see if
52+
// a device did acknowledge to the address.
53+
54+
Wire.beginTransmission(address);
55+
error = Wire.endTransmission();
56+
57+
if (error == 0) {
58+
Serial.print("I2C device found at address 0x");
59+
if (address < 16)
60+
Serial.print("0");
61+
Serial.println(address, HEX);
62+
63+
nDevices++;
64+
}
65+
else if (error == 4) {
66+
Serial.print("Unknown error at address 0x");
67+
if (address < 16)
68+
Serial.print("0");
69+
Serial.println(address, HEX);
70+
}
71+
}
72+
if (nDevices == 0)
73+
Serial.println("No I2C devices found");
74+
else
75+
Serial.println("done");
76+
77+
delay(5000); // wait 5 seconds for next scan
78+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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_slave.h>
14+
15+
void setup()
16+
{
17+
Wire.begin(); // join i2c bus (address optional for master)
18+
Serial.begin(115200); // start serial for output
19+
}
20+
21+
void loop()
22+
{
23+
Wire.requestFrom(8, 6); // request 6 bytes from slave device #8
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+
}

0 commit comments

Comments
 (0)
0