1
0
Fork 0

better sd

sd2
radex 2024-05-17 16:50:30 +02:00
parent 790fe673ef
commit 111700b0e9
Signed by: radex
SSH Key Fingerprint: SHA256:hvqRXAGG1h89yqnS+cyFTLKQbzjWD4uXIqw7Y+0ws30
3 changed files with 164 additions and 168 deletions

View File

@ -4,10 +4,8 @@
#include "pico/multicore.h"
#include "hardware/gpio.h"
#include "mbed_wait_api.h"
#include <SPI.h>
// #include <SD.h>
#include <RP2040_SD.h>
// #include "audio.h"
#include "audio.h"
#include "sd.h"
// #define Serial Serial1
@ -60,139 +58,22 @@ void life_setup();
void life_step();
extern bool cells[ROW_COUNT * COL_COUNT];
void printDirectory(File dir, int numTabs);
void setup() {
pinMode(16, INPUT);
pinMode(17, OUTPUT);
digitalWrite(17, HIGH);
pinMode(18, OUTPUT);
pinMode(19, OUTPUT);
pinMode(28, INPUT_PULLUP);
// pinMode(23, OUTPUT);
setupSDPins();
delay(2000);
Serial.begin(115200);
Serial.println("Hello worldd!");
init_audio();
SPI.begin();
while (digitalRead(28) == HIGH) {
while (!isSDCardInserted()) {
Serial.println("SD card not connected, waiting...");
delay(1000);
}
delay(100);
Serial.println(BOARD_NAME);
Serial.println(RP2040_SD_VERSION);
Serial.print("Initializing SD card with SS = ");
Serial.println(PIN_SPI_SS);
Serial.print("SCK = ");
Serial.println(PIN_SPI_SCK);
Serial.print("MOSI = ");
Serial.println(PIN_SPI_MOSI);
Serial.print("MISO = ");
Serial.println(PIN_SPI_MISO);
if (!SD.begin(PIN_SPI_SS))
{
Serial.println("Initialization failed!");
Serial.print("Error code: ");
// Serial.println(SD.card.errorCode(), HEX);
return;
}
Serial.println("initialization done.");
// delay(2000);
// return;
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File myFile = SD.open("test2.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
// re-open the file for reading:
myFile = SD.open("test2.txt");
if (myFile) {
Serial.println("test.txt:");
// read from the file until there's nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
/*
Serial.print("Clusters: ");
Serial.println(SD.volume.clusterCount());
Serial.print("Blocks x Cluster: ");
Serial.println(SD.volume.blocksPerCluster());
Serial.print("Total Blocks: ");
Serial.println(SD.volume.blocksPerCluster() * SD.volume.clusterCount());
Serial.println();
// print the type and size of the first FAT-type volume
uint32_t volumesize;
Serial.print("Volume type is: FAT");
Serial.println(SD.volume.fatType(), DEC);
volumesize = SD.volume.blocksPerCluster(); // clusters are collections of blocks
volumesize *= SD.volume.clusterCount(); // we'll have a lot of clusters
volumesize /= 2; // SD card blocks are always 512 bytes (2 blocks are 1 KB)
Serial.print("Volume size (KB): ");
Serial.println(volumesize);
Serial.print("Volume size (MB): ");
volumesize /= 1024;
Serial.println(volumesize);
Serial.print("Volume size (GB): ");
Serial.println((float)volumesize / 1024.0);*/
File root = SD.open("/");
printDirectory(root, 0);
// Serial.println("\nFiles found on the card (name, date and size in bytes): ");
// SD.root.openRoot(SD.volume);
// list all files in the card with date and size
// SD.root.ls(LS_R | LS_DATE | LS_SIZE);
// SD.root.close();
/*
Serial.print("Initializing SD card...");
if (!SD.begin(17)) {
Serial.println("initialization failed!");
Serial.print("Error code: ");
Serial.println(SD.card.errorCode(), HEX);
while (1);
}
*/
// if (!SD.begin(17)) {
// Serial.println("initialization failed!");
// Serial.print("Error code: ");
// Serial.println(SD.card.errorCode(), HEX);
// while (1);
// }
setupSD();
return;
@ -247,48 +128,6 @@ void setup() {
// }
}
void printDirectory(File dir, int numTabs) {
while (true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
break;
}
for (uint8_t i = 0; i < numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs + 1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}
void loop2();
void main2() {

153
firmware/src/sd.cpp Normal file
View File

@ -0,0 +1,153 @@
#include <Arduino.h>
#include <RP2040_SD.h>
#include <SPI.h>
#define SD_DET_PIN 28
#if PIN_SPI_SS != 17
#error "PIN_SPI_SS must be 17"
#endif
#if PIN_SPI_SCK != 18
#error "PIN_SPI_SCK must be 18"
#endif
#if PIN_SPI_MOSI != 19
#error "PIN_SPI_MOSI must be 19"
#endif
#if PIN_SPI_MISO != 16
#error "PIN_SPI_MISO must be 16"
#endif
void setupSDPins() {
// TODO: Is that even needed if we use built-in SPI?
pinMode(PIN_SPI_MISO, INPUT);
pinMode(PIN_SPI_SS, OUTPUT);
digitalWrite(PIN_SPI_SS, HIGH);
pinMode(PIN_SPI_SCK, OUTPUT);
pinMode(PIN_SPI_MOSI, OUTPUT);
pinMode(SD_DET_PIN, INPUT_PULLUP);
}
bool isSDCardInserted() {
return digitalRead(SD_DET_PIN) == LOW;
}
void printSDConfig();
void testSDCard();
void printSDStats();
void printDirectory(File dir, int numTabs);
void setupSD() {
SPI.begin();
printSDConfig();
if (!SD.begin(PIN_SPI_SS)) {
Serial.println("SD Initialization failed!");
// Serial.print("Error code: ");
// Serial.println(SD.card.errorCode(), HEX);
return;
}
Serial.println("SD Initialization done");
testSDCard();
// printSDStats(SD.volume);
File root = SD.open("/");
printDirectory(root, 0);
}
void printSDConfig() {
Serial.println(BOARD_NAME);
Serial.println(RP2040_SD_VERSION);
Serial.print("Initializing SD card with SS = ");
Serial.println(PIN_SPI_SS);
Serial.print("SCK = ");
Serial.println(PIN_SPI_SCK);
Serial.print("MOSI = ");
Serial.println(PIN_SPI_MOSI);
Serial.print("MISO = ");
Serial.println(PIN_SPI_MISO);
}
void testSDCard() {
File myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
myFile.println("testing 1, 2, 3.");
myFile.close();
Serial.println("done.");
} else {
Serial.println("error opening test.txt");
}
// re-open the file for reading:
myFile = SD.open("test.txt");
if (myFile) {
Serial.println("test.txt:");
while (myFile.available()) {
Serial.write(myFile.read());
}
myFile.close();
} else {
Serial.println("error opening test.txt");
}
}
void printDirectory(File dir, int numTabs) {
while (true) {
File entry = dir.openNextFile();
if (!entry) {
// no more files
break;
}
for (uint8_t i = 0; i < numTabs; i++) {
Serial.print('\t');
}
Serial.print(entry.name());
if (entry.isDirectory()) {
Serial.println("/");
printDirectory(entry, numTabs + 1);
} else {
// files have sizes, directories do not
Serial.print("\t\t");
Serial.println(entry.size(), DEC);
}
entry.close();
}
}
void printSDStats(RP2040_SdVolume volume) {
Serial.print("Clusters: ");
Serial.println(volume.clusterCount());
Serial.print("Blocks x Cluster: ");
Serial.println(volume.blocksPerCluster());
Serial.print("Total Blocks: ");
Serial.println(volume.blocksPerCluster() * volume.clusterCount());
Serial.println();
// print the type and size of the first FAT-type volume
uint32_t volumesize;
Serial.print("Volume type is: FAT");
Serial.println(volume.fatType(), DEC);
volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
volumesize *= volume.clusterCount(); // we'll have a lot of clusters
volumesize /= 2; // SD card blocks are always 512 bytes (2 blocks are 1 KB)
Serial.print("Volume size (KB): ");
Serial.println(volumesize);
Serial.print("Volume size (MB): ");
volumesize /= 1024;
Serial.println(volumesize);
Serial.print("Volume size (GB): ");
Serial.println((float)volumesize / 1024.0);
}

4
firmware/src/sd.h Normal file
View File

@ -0,0 +1,4 @@
void setupSDPins();
void setupSD();
bool isSDCardInserted();