Hi,
I have sometimes problem with init for the SD card.
When I run my code the I get sometimes the Error:
SD errorCode: 0X43,0X1
Then I run the sketch sdinfo:
SdFat version: 20160913
Assuming the SD is the only SPI device.
Edit DISABLE_CHIP_SELECT to disable another device.
Assuming the SD chip select pin is: 53
Edit SD_CHIP_SELECT to change the SD chip select pin.
type any character to start
error: cardBegin failed
SD errorCode: 0X43,0X1
type any character to start
next try:
type any character to start
error: cardBegin failed
SD errorCode: 0X43,0X5
type any character to start
then I eject the card and put it in the slot again:
init time: 813 ms
Card type: SDHC
Manufacturer ID: 0X74
OEM ID: J`
Product: USD
Version: 1.0
Serial number: 0X508FA33B
Manufacturing date: 8/2015
cardSize: 8068.79 MB (MB = 1,000,000 bytes)
flashEraseSize: 128 blocks
eraseSingleBlock: true
OCR: 0XC0FF8000
SD Partition Table
part,boot,type,start,length
1,0X0,0XC,8192,15751168
2,0X0,0X0,0,0
3,0X0,0X0,0,0
4,0X0,0X0,0,0
Volume is FAT32
blocksPerCluster: 64
clusterCount: 245984
freeClusters: 245903
freeSpace: 8057.75 MB (MB = 1,000,000 bytes)
fatStartBlock: 12540
fatCount: 2
blocksPerFat: 1922
rootDirStart: 2
dataStartBlock: 16384
type any character to start
Then all is fine.
When I add my Sketch again, it may happen that I get the Error 0x43, 0x5 again. But not always. When the Init on SD card is ok, the I can read/write/delete files.
What can I do that the initialization always works?
Here my code:
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <stdlib.h>
#include <mcp_can.h>
#include <Timer.h>
#include <EEPROM.h>
#include <WiFi101.h>
#include "SdFat.h"
// function declaration
void clearScreen();
/**
===================================================
WIFI
===================================================
*/
#define WINC_CS 34
#define WINC_IRQ 32
#define WINC_RST 35
#define WINC_EN 36
#define WINC_WAKE 33
const int8_t DISABLE_CHIP_SELECT_WIFI = 34;
WiFiServer server(80);
WiFiClient client;
/**
===================================================
CAN
===================================================
*/
#define CAN_CS 31
MCP_CAN CAN(CAN_CS);
const int8_t DISABLE_CHIP_SELECT_CAN = 31;
uint8_t deviceType;
uint8_t len = 0;
unsigned char buf[8];
uint16_t canId = 0;
uint8_t deviceNodeId = 0x12;
boolean deviceStatus = false;
/**
===================================================
TFT
===================================================
*/
#define TFT_CS 49
#define TFT_DC 48
#define TFT_RST 47
#define LITE 46
#define SCR_ROTATION 45
#define TEXT_WRAP true
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
/**
===================================================
SD-Card
===================================================
*/
#define SD_CS 53
SdFat sd;
File myFile;
File webFile;
char fileNameVersion[] = "version.txt";
char firmware[] = "firmware.bin";
void setup() {
Serial.begin(9600);
// Init section
initTFT();
tft.println();
initSD();
initWiFi();
initCAN();
}
void loop() {
// Do some CAN stuff
}
void initTFT() {
// Use this initializer if you're using a 1.8" TFT
// tft.initR(INITR_BLACKTAB);
// Use this initializer if you're using a 1.44" TFT
tft.initR(INITR_144GREENTAB);
tft.setTextWrap(TEXT_WRAP);
clearScreen();
tft.setRotation(SCR_ROTATION);
}
void initCAN() {
tft.setTextColor(ST7735_WHITE);
tft.setTextSize(2);
tft.print("CAN:");
uint8_t count = 0;
// Init can bus : baudrate = 500k
// CAN_OK (0)
// CAN_FAILINIT (1)
// CAN_FAILTX (2)
// CAN_MSGAVAIL (3)
// CAN_NOMSG (4)
// CAN_CTRLERROR (5)
// CAN_GETTXBFTIMEOUT (6)
// CAN_SENDMSGTIMEOUT (7)
// CAN_FAIL (0xff)
while (CAN_OK != CAN.begin(CAN_500KBPS)) {
}
tft.println(" OK");
}
void initSD() {
tft.setTextColor(ST7735_WHITE);
tft.setTextSize(2);
tft.print("SD:");
// Desable WIFI
pinMode(DISABLE_CHIP_SELECT_WIFI, OUTPUT);
digitalWrite(DISABLE_CHIP_SELECT_WIFI, HIGH);
// Desable CAN
pinMode(DISABLE_CHIP_SELECT_CAN, OUTPUT);
digitalWrite(DISABLE_CHIP_SELECT_CAN, HIGH);
// Initialize at the highest speed supported by the board that is
// not over 50 MHz. Try a lower speed if SPI errors occur.
if (!sd.begin(SD_CS, SD_SCK_MHZ(50))) {
tft.println(" NOK");
sd.initErrorHalt();
} else {
tft.println(" OK");
}
// Check if file "version.txt" exist.
if (!sd.exists(fileNameVersion)) {
tft.print("CF:");
File fileHandler = sd.open(fileNameVersion, FILE_WRITE);
if (fileHandler) {
fileHandler.close();
tft.println(" OK");
} else {
tft.println(" NOK");
}
}
}
void initWiFi() {
tft.setTextColor(ST7735_WHITE);
tft.setTextSize(2);
tft.print("WiFi:");
// check for the presence of the shield:
WiFi.setPins(WINC_CS, WINC_IRQ, WINC_RST, WINC_EN);
if (WiFi.status() == WL_NO_SHIELD) {
tft.println(" NOK");
} else {
tft.println(" OK");
}
}
/**
------------------------------------------
CLEAR SCREEN
------------------------------------------
*/
void clearScreen() {
tft.fillScreen(ST7735_BLACK);
}
Thanks for your help!