-
Notifications
You must be signed in to change notification settings - Fork 456
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add PIO-based SoftwareSPI enabling SPI on any pins (#2778)
* Add PIO-based SoftwareSPI enabling SPI on any pins The Raspberry Pi team has a working PIO-based SPI interface. Wrap it to work like a hardware SPI interface, allowing SPI on any pin combination. Tested reading and writing an SD card using unmodified SD library. * Add W5500 example Good for testing, shows non-contiguous pin outs.
1 parent
a426fbf
commit acf81f4
Showing
11 changed files
with
1,110 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
libraries/SoftwareSPI/examples/FilesSoftwareSPI/FilesSoftwareSPI.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
SD card basic file example with Software SPI | ||
This example shows how to create and destroy an SD card file | ||
The circuit: | ||
SD card attached to Pico as follows: | ||
** SCK - GPIO0 | ||
** CS - GPIO1 | ||
** MISO (AKA RX) - GPIO2 | ||
** MOSI (AKA TX) - GPIO3 | ||
created Nov 2010 | ||
by David A. Mellis | ||
modified 9 Apr 2012 | ||
by Tom Igoe | ||
This example code is in the public domain. | ||
*/ | ||
|
||
#include <SoftwareSPI.h> | ||
|
||
const int _SCK = 0; | ||
const int _CS = 1; // Must be SCK+1 for HW CS support | ||
const int _MISO = 2; | ||
const int _MOSI = 3; | ||
SoftwareSPI softSPI(_SCK, _MISO, _MOSI, _CS); | ||
|
||
#include <SD.h> | ||
|
||
File myFile; | ||
|
||
void setup() { | ||
// Open serial communications and wait for port to open: | ||
Serial.begin(115200); | ||
|
||
do { | ||
delay(100); // wait for serial port to connect. Needed for native USB port only | ||
} while (!Serial); | ||
|
||
Serial.print("Initializing SD card..."); | ||
|
||
bool sdInitialized = false; | ||
sdInitialized = SD.begin(_CS, softSPI); | ||
if (!sdInitialized) { | ||
Serial.println("initialization failed!"); | ||
return; | ||
} | ||
Serial.println("initialization done."); | ||
|
||
if (SD.exists("example.txt")) { | ||
Serial.println("example.txt exists."); | ||
} else { | ||
Serial.println("example.txt doesn't exist."); | ||
} | ||
|
||
// open a new file and immediately close it: | ||
Serial.println("Creating example.txt..."); | ||
myFile = SD.open("example.txt", FILE_WRITE); | ||
myFile.close(); | ||
|
||
// Check to see if the file exists: | ||
if (SD.exists("example.txt")) { | ||
Serial.println("example.txt exists."); | ||
} else { | ||
Serial.println("example.txt doesn't exist."); | ||
} | ||
|
||
// delete the file: | ||
Serial.println("Removing example.txt..."); | ||
SD.remove("example.txt"); | ||
|
||
if (SD.exists("example.txt")) { | ||
Serial.println("example.txt exists."); | ||
} else { | ||
Serial.println("example.txt doesn't exist."); | ||
} | ||
} | ||
|
||
void loop() { | ||
// nothing happens after setup finishes. | ||
} | ||
|
||
|
||
|
98 changes: 98 additions & 0 deletions
98
libraries/SoftwareSPI/examples/W5500SoftwareSPI/W5500SoftwareSPI.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
This sketch establishes a TCP connection to a "quote of the day" service. | ||
It sends a "hello" message, and then prints received data. | ||
*/ | ||
|
||
#include <W5500lwIP.h> | ||
|
||
const char* host = "djxmmx.net"; | ||
const uint16_t port = 17; | ||
|
||
#include <SoftwareSPI.h> | ||
const int _SCK = 0; // Any pin allowed | ||
const int _CS = 1; // Must be SCK+1 for HW CS support | ||
const int _MISO = 28; // Note that MOSI and MISO don't need to be contiguous. Any pins allowed | ||
const int _MOSI = 3; // Any pin not used elsewhere | ||
const int _INT = 4; // W5500 IRQ line | ||
|
||
SoftwareSPI softSPI(_SCK, _MISO, _MOSI, _CS); | ||
|
||
Wiznet5500lwIP eth(_CS, softSPI, _INT); | ||
|
||
void setup() { | ||
Serial.begin(115200); | ||
delay(5000); | ||
Serial.println(); | ||
Serial.println(); | ||
Serial.println("Starting Ethernet port"); | ||
|
||
// Start the Ethernet port | ||
if (!eth.begin()) { | ||
Serial.println("No wired Ethernet hardware detected. Check pinouts, wiring."); | ||
while (1) { | ||
delay(1000); | ||
} | ||
} | ||
|
||
while (!eth.connected()) { | ||
Serial.print("."); | ||
delay(500); | ||
} | ||
|
||
Serial.println(""); | ||
Serial.println("Ethernet connected"); | ||
Serial.println("IP address: "); | ||
Serial.println(eth.localIP()); | ||
} | ||
|
||
void loop() { | ||
static bool wait = false; | ||
|
||
Serial.print("connecting to "); | ||
Serial.print(host); | ||
Serial.print(':'); | ||
Serial.println(port); | ||
|
||
// Use WiFiClient class to create TCP connections | ||
WiFiClient client; | ||
if (!client.connect(host, port)) { | ||
Serial.println("connection failed"); | ||
delay(5000); | ||
return; | ||
} | ||
|
||
// This will send a string to the server | ||
Serial.println("sending data to server"); | ||
if (client.connected()) { | ||
client.println("hello from RP2040"); | ||
} | ||
|
||
// wait for data to be available | ||
unsigned long timeout = millis(); | ||
while (client.available() == 0) { | ||
if (millis() - timeout > 5000) { | ||
Serial.println(">>> Client Timeout !"); | ||
client.stop(); | ||
delay(60000); | ||
return; | ||
} | ||
} | ||
|
||
// Read all the lines of the reply from server and print them to Serial | ||
Serial.println("receiving from remote server"); | ||
// not testing 'client.connected()' since we do not need to send data here | ||
while (client.available()) { | ||
char ch = static_cast<char>(client.read()); | ||
Serial.print(ch); | ||
} | ||
|
||
// Close the connection | ||
Serial.println(); | ||
Serial.println("closing connection"); | ||
client.stop(); | ||
|
||
if (wait) { | ||
delay(300000); // execute once every 5 minutes, don't flood remote service | ||
} | ||
wait = true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
####################################### | ||
# Syntax Coloring Map SPI | ||
####################################### | ||
|
||
####################################### | ||
# Instances (KEYWORD2) | ||
####################################### | ||
|
||
SoftwareSPI KEYWORD1 | ||
|
||
####################################### | ||
# Methods and Functions (KEYWORD2) | ||
####################################### | ||
begin KEYWORD2 | ||
end KEYWORD2 | ||
beginTransaction KEYWORD2 | ||
endTransaction KEYWORD2 | ||
SPISettings KEYWORD2 | ||
transfer KEYWORD2 | ||
transfer16 KEYWORD2 | ||
setBitOrder KEYWORD2 | ||
setDataMode KEYWORD2 | ||
setClockDivider KEYWORD2 | ||
setSCK KEYWORD2 | ||
setMOSI KEYWORD2 | ||
setMISO KEYWORD2 | ||
setCS KEYWORD2 | ||
|
||
####################################### | ||
# Constants (LITERAL1) | ||
####################################### | ||
SPI_CLOCK_DIV4 LITERAL1 | ||
SPI_CLOCK_DIV16 LITERAL1 | ||
SPI_CLOCK_DIV64 LITERAL1 | ||
SPI_CLOCK_DIV128 LITERAL1 | ||
SPI_CLOCK_DIV2 LITERAL1 | ||
SPI_CLOCK_DIV8 LITERAL1 | ||
SPI_CLOCK_DIV32 LITERAL1 | ||
SPI_CLOCK_DIV64 LITERAL1 | ||
SPI_MODE0 LITERAL1 | ||
SPI_MODE1 LITERAL1 | ||
SPI_MODE2 LITERAL1 | ||
SPI_MODE3 LITERAL1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
name=SoftwareSPI | ||
version=1.0 | ||
author=Earle F. Philhower, III <earlephilhower@yahoo.com> | ||
maintainer=Earle F. Philhower, III <earlephilhower@yahoo.com> | ||
sentence=Uses the PIO to provide an SPI interface on any pin. | ||
paragraph= | ||
category=Signal Input/Output | ||
url=http://arduino.cc/en/Reference/SPI | ||
architectures=rp2040 | ||
dot_a_linkage=true |
Oops, something went wrong.