Hi folks,
I tested a basic firmware that writes to SD card a default value and also records logs to SD card. For logging, I am using SdCardLogHandler.
This issue with the ChipSelect line jumping high mid-write persisted through changing to SPI1 and changing the ChipSelect pin for A5. The screenshots below were taken when the Boron was isolated on a breakout board and was not connected to any peripherals.
However, when I removed the logging library, the behavior went away.
Has anyone have ideas on how/why this could be happening? The basic SD writing firmware I am using is below and I am using SdFat=1.0.16 and SdCardLogHandlerRK=0.1.0 library versions.
/*
* Project modulair-pm
* Description: Firmware for the QuantAQ MODULAIR(TM)-PM
* Author: David H Hagan
* Date: July 2020
*
* Update the DeviceOS: $ particle update
*
* Enter DFU-Util Mode: $ stty -f /dev/tty.usbmodem1421 14400
* Enter Listening Mode: $ stty -f /dev/tty.usbmodem1421 28800
*
* We have extra EEPROM available to us as seen here:
* https://github.com/rickkas7/MCP79410RK
*/
#include "Particle.h"
#include "SdFat.h"
//#include "SdCardLogHandlerRK.h"
#define SD_CS A5
#define SD_PWR D9
#define WRITE_RATE (800)
unsigned long millisLastWrite, millisNow;
String filename = "TEST-FILE.csv";
bool sdCardEnabled;
// Define the different states
enum DeviceState {
STARTUP,
IDLE,
WRITE_TO_SD,
};
// Set up the µSD Card
SdFat sd(&SPI);
SdFile LF;
// Set the initial state
DeviceState STATE = STARTUP;
// Setup logging functionality
static Logger appLog("app.modulair-pm");
// SdCardLogHandler<2048> logHandlerSD(sd, SD_CS, SPI_FULL_SPEED, LOG_LEVEL_WARN, {
// { "app.modulair-pm", LOG_LEVEL_INFO },
// { "app.data", LOG_LEVEL_INFO },
// { "app.devicekeys", LOG_LEVEL_INFO },
// { "app.rtc", LOG_LEVEL_INFO },
// { "app.pubq", LOG_LEVEL_WARN },
// { "comm.protocol", LOG_LEVEL_WARN },
// { "app.pms5003", LOG_LEVEL_WARN },
// { "app.opcn3", LOG_LEVEL_WARN },
// { "system", LOG_LEVEL_WARN },
// { "app", LOG_LEVEL_WARN },
// });
// INFO default
SerialLogHandler logHandler(LOG_LEVEL_INFO, {
{ "app.modulair-pm", LOG_LEVEL_INFO },
{ "app.data", LOG_LEVEL_TRACE },
{ "app.devicekeys", LOG_LEVEL_INFO },
{ "app.rtc", LOG_LEVEL_INFO },
{ "app.pubq", LOG_LEVEL_INFO },
{ "app.pms5003", LOG_LEVEL_INFO },
{ "app.opcn3", LOG_LEVEL_INFO }, // INFO
{ "system", LOG_LEVEL_INFO },
{ "app", LOG_LEVEL_INFO },
{ "comm.protocol", LOG_LEVEL_INFO },
{ "app.help", LOG_LEVEL_INFO },
{ "ncp.at", LOG_LEVEL_TRACE },
{ "net.pppncp", LOG_LEVEL_INFO },
{ "net.ppp.client", LOG_LEVEL_INFO },
{ "mux", LOG_LEVEL_INFO },
{ "system.nm", LOG_LEVEL_INFO },
{ "comm.dtls", LOG_LEVEL_INFO },
});
void startup() {
RGB.mirrorTo(D7, D8, D6, false, true);
System.enableFeature(FEATURE_RETAINED_MEMORY);
System.enableFeature(FEATURE_RESET_INFO);
//logHandlerSD.withDesiredFileSize(5000000).withMaxFilesToKeep(10).withNoSerialLogging();
}
STARTUP(startup());
SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);
void handler();
void setup()
{
Serial.begin(9600);
// Set the GPIO pins
pinMode(SD_PWR, OUTPUT);
// Turn on the µSD card
digitalWrite(SD_PWR, HIGH);
// Setup the log handler
//logHandlerSD.setup();
// Set the time format
Time.setFormat(TIME_FORMAT_ISO8601_FULL);
// If using an external SIM, set the keepAlive
waitFor(Serial.available, 3000);
// Turn on the cellular connection
Cellular.on();
Particle.connect();
}
// loop() runs over and over again, as quickly as it can execute.
void loop()
{
switch(STATE)
{
case STARTUP:
{
millisLastWrite = millis();
STATE = IDLE;
break;
}
case IDLE:
{
millisNow = millis();
if ((int)(millisNow - millisLastWrite) >= WRITE_RATE)
{
STATE = WRITE_TO_SD;
}
break;
}
case WRITE_TO_SD:
{
millisLastWrite = millis();
// Get the current time
time_t time = Time.now();
// Create new file if one doesn't already exist
if (!sd.exists(filename)) {
sdCardEnabled = makeFile(filename);
if (!sdCardEnabled)
appLog.error("Issue encountered while creating a new file.");
}
if (!LF.open(filename, O_WRONLY | O_AT_END)) {
appLog.error("Could not open file for writing.");
appLog.trace("Trying to restart µSD card.");
// try starting the µSD card again
sdCardEnabled = startSD();
} else {
LF.write(String::format("%d,%.1f,%.1f,%.1f\n", millis(), 1.0, 2.0, 3.0));
LF.close();
appLog.info("Wrote a successful string and turned SD true.");
}
STATE = IDLE;
break;
}
} // end switch statement
// // Update the log handler
//logHandlerSD.loop();
}
/**
* Start the µSD card and start the publishQueue
*/
bool startSD() {
// Power Cycle the pins
if (!sd.begin(SD_CS)) {
appLog.error("Could not open the µSD card for writing");
return false;
}
return true;
}
/**
* Create a new data file in the DATA directory
*
* @param fname Filename as a string
*/
bool makeFile(String fname) {
// Make a new file if one doesn't already exist with this filename
SdFile _LF;
String s;
// If the file already exists, return true
if (!_LF.open(fname, O_WRONLY | O_CREAT | O_AT_END)) {
return false;
}
// Build up a file and write
s.concat("millis,a,b,c");
s.concat("\n");
_LF.write(s);
_LF.close();
return true;
}