Hallo,
ich habe hier einen Yun, bei dem ich gerne die SPI-Schnittstelle benutzen möchte.
Dazu verwende ich die Standard-SPI Lib.
Im Moment habe ich das Problem, dass der Yun gar nicht sendet und aus der Transfer direkt rausspringt. Der slaveSelectPin wird direkt wieder auf High gesetzt (mit Oszilloskop 10-15us gemessen).
Der Code für die Transfer-Funktion sieht so aus:
byte SPIClass::transfer(byte _data) {
SPDR = _data;
while (!(SPSR & _BV(SPIF)))
;
return SPDR;
}
SPIF ist auch sofort immer 1. Auf MOSI kann ich mit dem Oszilloskop nix messen.
Was habe ich vergessen? Was mache ich Falsch?
So sieht mein Programm aus, soll eigentlich nur ein Adapter für Rest<->SPI sein:
#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>
#include <SPI.h>
/*
* Read: "http://192.168.240.1/arduino/spi/26"
* Write: "http://192.168.240.1/arduino/spi/26/2"
// Listen on default port 5555, the webserver on the Yún
// will forward there all the HTTP requests for us.
http://192.168.240.1/arduino/digital/13
*/
YunServer server;
const int slaveSelectPin = 4;
const int triggerPin = 3;
void setup() {
// Bridge startup
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
Bridge.begin();
Console.begin();
while (!Console) {
; // wait for Console port to connect.
}
Console.println("Verbindung hergestellt");
// Listen for incoming connection only from localhost
// (no one from the external network could connect)
server.listenOnLocalhost();
server.begin();
// initialize SPI:
pinMode (slaveSelectPin, OUTPUT);
SPI.begin();
SPI.setDataMode(SPI_MODE1);
SPI.setBitOrder(MSBFIRST);
SPI.setClockDivider(SPI_CLOCK_DIV4);
digitalWrite(slaveSelectPin, HIGH);
digitalWrite(13, HIGH);
pinMode(triggerPin, OUTPUT);
digitalWrite(triggerPin, HIGH);
}
void loop() {
// Get clients coming from server
YunClient client = server.accept();
// There is a new client?
if (client) {
Console.println("----------------------------------");
Console.println("Client Verbindungsaufbau");
// Process request
process(client);
// Close connection and free resources.
client.stop();
}
delay(50); // Poll every 50ms
}
void process(YunClient client) {
// read the command
String command = client.readStringUntil('/');
Console.print("In process, command:");
Console.println(command);
// is "mode" command?
if (command == "spi") {
digitalWrite(triggerPin, LOW);
digitalWrite(13, LOW);
SPICommand(client);
digitalWrite(triggerPin, HIGH);
digitalWrite(13, HIGH);
}
else {
client.print(F("No SPI-command found"));
}
}
void SPICommand(YunClient client) {
int address, value;
// Read pin number
address = client.parseInt();
Console.print("In SPICommand, address:");
Console.println(address);
// If the next character is a '/' it means we have an URL
// with a value like: "/spi/13/1"
if (client.read() == '/') {
value = client.parseInt();
Console.print("Schreibe:");
Console.println(value);
digitalWrite(slaveSelectPin, LOW);
// send in the address and value via SPI:
SPI.transfer(address);
SPI.transfer(value);
// take the SS pin high to de-select the chip:
digitalWrite(slaveSelectPin, HIGH);
// Send feedback to client
client.print(F("Address "));
client.print(address);
client.print(F(" set to "));
client.println(value);
}
else {
digitalWrite(slaveSelectPin, LOW);
// send in the address and value via SPI:
SPI.transfer(address);
value = SPI.transfer(0x00);
// take the SS pin high to de-select the chip:
digitalWrite(slaveSelectPin, HIGH);
Console.print("Lese:");
Console.println(value);
// Send feedback to client
client.print(F("Address "));
client.print(address);
client.print(F(" value is "));
client.println(value);
}
// Update datastore key with the current pin value
String key = "Address";
key += address;
Bridge.put(key, String(value));
}