Longan-Labs / Arduino_CAN_BUS_MCP2515

Arduino CAN Bus library, MCP2515/MCP2551
https://www.longan-labs.cc/
MIT License
118 stars 371 forks source link

SdFat + MCP_CAN_lib #23

Open JJGR93 opened 3 years ago

JJGR93 commented 3 years ago

Hi, first of all thank you for your work!

I've been researching and trying to make both Sd and MCP2515 work fine. The issue is that I must call function sd.begin everytime before reading from CAN, or CAN won't work.

Using SHARED_SPI.

Simplified code:

#include "mcp_can.h"
#include "SdFat.h"

ExFile file;
SdExFat sd;

MCP_CAN CAN(10);

void setup()
{
  Serial.begin(115200);

  initSD();
  initCANBUS();
}

void loop()
{
  readCANBUS();
  saveSD();
  initSD(); //THIS MUST BE CALLED OR NEXT readCANBUS() wont work.
}

void initSD()
{
  sd.begin(53, SD_SCK_MHZ(16));
}

void saveSD()
{
  file = sd.open("test", FILE_WRITE);

  if (file)
  {
    file.print("test");
    file.close();
  }
}

void initCANBUS()
{
  while (CAN_OK != CAN.begin(MCP_ANY, CAN_250KBPS, MCP_8MHZ))
  {
    delay(100);
  }

  CAN.setMode(MCP_NORMAL);
}

void readCANBUS()
{
  //...
}