This code works fine writing and reading back uint16_t data but it stops writing after 2^16 records. Arduino accepts the pre_allocate only after the file is opened which suggests a problem that I can't correct. It also issues a warning " 448 | #warning File not defined because __has_include(FS.h)". Using Pico RP2040 on Philhower core with Win11 Arduino IDE and terabyte card. Any suggestions?
// sample2uSD.ino
// Demonstrates two buffers and two processors alternately sampling and storing to uSD
// 12 bit sampling A0 at 210,000 samples/sec, storing to uSD much faster.
// Larry Gorham
// This code is public domain
//
#include <SPI.h>
#define BUFFSIZE 512 // Size of single buffer.
#define UPDATES 129 //5
#define invert(r) (r+1)%2
int buff0[BUFFSIZE] = {0};
int buff1[BUFFSIZE] = {0};
uint16_t flip = 0;
uint16_t flop = 0;
uint32_t last;
uint32_t tim;
uint32_t fl = 0;
float approxBuffRate = (float) BUFFSIZE/.22; // sampling rate microseconds
uint16_t printBuff[UPDATES*BUFFSIZE] = {0};
uint16_t writeBuff[BUFFSIZE] = {0};
//---------- SD Configs --------------------------
#include "SdFat.h"
#include "sdios.h"
#include "FreeStack.h"
#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
// SD_FAT_TYPE = 0 for SdFat/File as defined in SdFatConfig.h,
// 1 for FAT16/FAT32, 2 for exFAT, 3 for FAT16/FAT32 and exFAT.
#define SD_FAT_TYPE 2
const uint8_t SD_CS_PIN = SS;
#define SPI_CLOCK SD_SCK_MHZ(25) // 50
#define SD_CONFIG SdSpiConfig(SD_CS_PIN, DEDICATED_SPI, SPI_CLOCK)
#define error(dum) Serial.printf("%s\n", dum);
#define GPIO16pad ((uint32_t*)0x4001c044) // pin 21
#define GPIO19pad ((uint32_t*)0x4001c050) // pin 25
// Set PRE_ALLOCATE true to pre-allocate file clusters.
const bool PRE_ALLOCATE = true;
const uint64_t MAX_FILE_SIZE = (pow(2,32) - 1);
//======================== End SD Configs =================================================
SdExFat sd;
ExFile file;
//**********************************************************************
void setup() {
Serial.begin(115200);
// Wait for USB Serial
while (!Serial) {
yield();
}
delay(1);
if (!ENABLE_DEDICATED_SPI) {
Serial.printf("\nSet ENABLE_DEDICATED_SPI nonzero\n");
}
if (!sd.begin(SD_CONFIG)) {
sd.initErrorHalt(&Serial);
}
// open or create file - truncate existing file.
if (!file.open("bench.dat", O_RDWR | O_CREAT | O_TRUNC)) {
error("open failed");
}
if (PRE_ALLOCATE) {
if (!file.preAllocate((uint64_t)MAX_FILE_SIZE)) {
error("preAllocate failed");
}
}
*GPIO16pad = 0x46; // sets drive strength to 2mA
*GPIO19pad = 0x46;
uint16_t temp = 0;
for (uint32_t iUp = 0 ; iUp < UPDATES ; iUp++){
while (flip == flop){
asm volatile (" nop\n"); // smallest delay
}
for (uint32_t j=0;j<BUFFSIZE;j++){
temp = iUp*BUFFSIZE + j;
if (flip == 1){
//printBuff1[temp] = (uint16_t) buff0[j];
writeBuff[j] = (uint16_t) buff0[j];
}else{
//printBuff1[temp] = (uint16_t) buff1[j];
writeBuff[j] = (uint16_t) buff1[j];
}
}
// ******************** writing to uSD ***************************
if (file.write(writeBuff, BUFFSIZE*2) != BUFFSIZE*2) { // writes in bytes
error("write buff failed");
}
flop = invert(flop);
}
// ********************* read what was written to uSD **********************
file.rewind();
for (uint32_t iUp = 0 ; iUp < UPDATES ; iUp++){
int32_t nr = file.read(writeBuff, BUFFSIZE*2); // reads in bytes
if (nr != BUFFSIZE*2) {
error("read failed");
}
for (uint32_t iOut = 0; iOut < BUFFSIZE ; iOut++){
temp = iUp*BUFFSIZE + iOut;
printBuff[temp] = writeBuff[iOut];
}
}
Serial.printf("Read it back\n");
for (uint32_t i = 0; i < UPDATES*BUFFSIZE ; i++){
//Serial.printf("%d , %d\n",i,printBuff[i]);
Serial.printf("%8d %4d\n\r",i,printBuff[i]);
}
}
void loop() {}
// ***************************Running on Core1*****************************************************
void setup1() {
Serial.begin(115200);
delay(3000);
flip = 0;
flop = 0;
//**************** Sampling ***********************
analogReadResolution(12);
for (uint32_t iUp = 0 ; iUp < UPDATES; iUp++){
if (flip == 0){
for (uint32_t iTo = 0; iTo < BUFFSIZE; iTo++) {
buff0[iTo] = analogRead(A0);
}
}else{
for (uint32_t iTo = 0; iTo < BUFFSIZE; iTo++) {
buff1[iTo] = analogRead(A0);
}
}
delay(2);
//flag = flip + 1;
flip = invert(flip);
}
}
void loop1() {}