Post by lukint on Mar 29, 2018 8:08:59 GMT
Hi, all. I'm still on my project to save data from CAN on SD card. I use core1 to save the received CAN data into one of my two buffers and core0 to save the data from the buffers on SD card. If I only use one buffer all is fine, but if I use both buffers, the Debugger hangs in file: crt0-tc2x.c at TRAP_ENTRY(trap_function); //Trap_busError. I tried to fix this by Locking and Unlocking the buffers with Htx_LockResource(buf1InUse), but that didn't effect anything. Now my code, so you can better understand what I'm doing.
#include <SPI.h>
#include "mcp_can.h"
#include <SdFat.h>
#include <avr/pgmspace.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
SdFatEX sd;
const int SPI_CS_CAN = 9;
#define SD_CS_PIN 4
#define BUF_NUM_PCKS 384 //Anzahl der pro Puffer speicherbaren Nachrichten;
MCP_CAN CAN(SPI_CS_CAN); // Set CS pin
File myFile;
StartOfUninitialised_LMURam_Variables
int lc1, lc2;
int bufSize;
EndOfUninitialised_LMURam_Variables
StartOfUninitialised_CPU0_Variables
/* Put your CPU1 fast access variables that have no initial values here e.g. uint32 CPU1_var; */
uint8_t buf1[BUF_NUM_PCKS*9*4], buf2[BUF_NUM_PCKS*9*4];
EndOfUninitialised_CPU0_Variables
void saveOnSd(uint8_t* data, int bufSel);
void fillBuffer(uint8_t* endm1, int bufSel);
void setup()
{
uint8_t buf[512];
SerialASC.begin(115200);
while (CAN_OK != CAN.begin(CAN_1000KBPS)) // init can bus : baudrate = 1000k
{
SerialASC.println(F("CAN BUS Shield init fail"));
delay(100);
}
SerialASC.println(F("CAN BUS Shield init ok!"));
if (!sd.begin(SD_CS_PIN,SD_SCK_MHZ(15))) {
SerialASC.println(F("initialization failed!"));
return;
}
SerialASC.println(F("initialization done."));
lc1=0;
lc2=0;
for (size_t i = 0; i < (512-2); i++) {
buf[i] = 'A' + (i % 26);
}
buf[512-2] = '\r';
buf[512-1] = '\n';
myFile = sd.open("CAN.csv", O_CREAT|O_APPEND|O_WRITE);
myFile.write(buf, sizeof(buf));
myFile.close();
pinMode(53, OUTPUT);
pinMode(51, OUTPUT);
digitalWrite(53, LOW);
bufSize = sizeof(buf1);
SerialASC.println(F("Initialization done"));
}
void loop(){
//fillBuffer();
if (lc1 == BUF_NUM_PCKS){
saveOnSd(buf1,1);
}
if (lc2 == BUF_NUM_PCKS){
saveOnSd(buf2,2);
}
}
void setup1(){
}
void loop1() {
if(lc1 < BUF_NUM_PCKS){
fillBuffer(buf1,1);
}
else if(lc2 < BUF_NUM_PCKS){
fillBuffer(buf2,2);
}
}
void setup2(){
}
void loop2(){
}
void fillBuffer(uint8_t* endm1, int bufSel){
int lc = 0;
while(lc<BUF_NUM_PCKS){
unsigned char len = 0;
unsigned char buf[8];
if(CAN_MSGAVAIL == CAN.checkReceive()) // check if data coming
{
//digitalWrite(51, HIGH);
CAN.readMsgBuf(&len, buf); // read data, len: data length, buf: data buf
unsigned int canId = CAN.getCanId();
if(canId>59){
*endm1=54;
canId=(int)canId-60;
}else if(canId>49){
*endm1=53;
canId=(int)canId-50;
}else if(canId>39){
*endm1=52;
canId=(int)canId-40;
}else if(canId>29){
*endm1=51;
canId=(int)canId-30;
}else if(canId>19){
*endm1=50;
canId=(int)canId-20;
}else if(canId>9){
*endm1=49;
canId=(int)canId-10;
}else{
*endm1=48;
}
endm1++;
switch (canId){
case 0: *endm1=48; break;
case 1: *endm1=49; break;
case 2: *endm1=50; break;
case 3: *endm1=51; break;
case 4: *endm1=52; break;
case 5: *endm1=53; break;
case 6: *endm1=54; break;
case 7: *endm1=55; break;
case 8: *endm1=56; break;
case 9: *endm1=57; break;
}
endm1++;
for (int g = 0; g<len; g++){
*endm1=',';
endm1++;
if(buf[g]>199){
*endm1=50;
buf[g]=(int)buf[g]-200;
}else if(buf[g]>99){
*endm1=49;
buf[g]=(int)buf[g]-100;
}else{
*endm1=48;
}
endm1++;
if(buf[g]>89){
*endm1=57;
buf[g]=(int)buf[g]-90;
}else if(buf[g]>79){
*endm1=56;
buf[g]=(int)buf[g]-80;
}else if(buf[g]>69){
*endm1=55;
buf[g]=(int)buf[g]-70;
}else if(buf[g]>59){
*endm1=54;
buf[g]=(int)buf[g]-60;
}else if(buf[g]>49){
*endm1=53;
buf[g]=(int)buf[g]-50;
}else if(buf[g]>39){
*endm1=52;
buf[g]=(int)buf[g]-40;
}else if(buf[g]>29){
*endm1=51;
buf[g]=(int)buf[g]-30;
}else if(buf[g]>19){
*endm1=50;
buf[g]=(int)buf[g]-20;
}else if(buf[g]>9){
*endm1=49;
buf[g]=(int)buf[g]-10;
}else{
*endm1=48;
}
endm1++;
switch (buf[g]){
case 0: *endm1=48; break;
case 1: *endm1=49; break;
case 2: *endm1=50; break;
case 3: *endm1=51; break;
case 4: *endm1=52; break;
case 5: *endm1=53; break;
case 6: *endm1=54; break;
case 7: *endm1=55; break;
case 8: *endm1=56; break;
case 9: *endm1=57; break;
}
endm1++;
}
*endm1='\n';
endm1++;
lc++;
}
}
switch(bufSel)
{
case 1:
lc1=lc;
break;
case 2:
lc2=lc;
break;
}
//digitalWrite(51, LOW);
}
void saveOnSd(uint8_t* data, int bufSel){
myFile = sd.open("CAN.csv", O_APPEND|O_WRITE);
digitalWrite(53, HIGH);
if (myFile) {
myFile.write(data, bufSize);
digitalWrite(53, LOW);
} else {
SerialASC.println(F("error"));
}
myFile.close();
switch(bufSel)
{
case 1:
lc1=0;
break;
case 2:
lc2=0;
break;
}
}