jamz-az/PIC18F-Dev — Issue #4: PIC18F46J50 ring buffer SD card problem
Author: alimary15
Created: 2012-01-26 21:13:55+00:00
State: open

Good evening,
aside using the product OpenLog, i am trying to implement a personal code for writing on SD. i have my own library in CCS for writing on sd that seems to work greatly but i am facing today this problem:

I would like to have a interrupt of timer 2 where i add a data (int16) into the ring buffer.

Then in the main loop i check if the tails and head of the ring are same and in case i write the datas on the sd.

I have typed a code but what i see its that the file its being created into the SD card but nothing is written to it!
The functions of writing are all working since i tested them with another simple program. I suppose that there is a problem in the way i am using the interrupts! Its all day I'm trying to figure out what is that but i don't seem to come up with a conclusion. Anyone has any suggestion?

here is my code:

INT_timer2

void timer2_isr()
{
RxBaseADC[RxHeadADC++]=data;

// test if ring buffer wrap is required
if (RxHeadADC >= RxADCQsize)
RxHeadADC = 0;

}

void main (){

FIL fsrc; // file structures
FRESULT result; // FatFs function common result code
WORD btw, bw; // File R/W count

FRESULT FS_Status;

char pressure[64];

disable_interrupts(GLOBAL);

restart_wdt();

init_pic();

setup_timer_2 (T2_DIV_BY_16, 255, 6);

RxHeadADC=0;
RxTailADC=0;

enable_interrupts(GLOBAL);

//enable_interrupts(int_timer2);

do
{
FS_Status = f_mountdrv();
}
while (FS_Status);

strcpy(pressure,"prova1.txt");

result = f_open(&fsrc,&pressure, FA_OPEN_ALWAYS | FA_WRITE);

result = f_lseek(&fsrc, fsrc.fsize);

//write a string delimiter to each append data log

strcpy(pressure, "\r\nNewTrial \r\n");

btw=strlen(pressure);

result = f_write(&fsrc,pressure, btw, &bw);

//checking the buffer tail and head

disable_interrupts(int_timer2);

if (RxHeadADC != RxTailADC)
{
enable_interrupts(int_timer2);

sprintf(pressure,"Valore=%lu\r\n",RxBaseADC[RxTailADC++]);

// test if ring buffer wrap is required
if (RxTailADC >= RxADCQsize)
RxTailADC = 0;
f_write(&fsrc,pressure,strlen(pressure),&bw);

}
else
enable_interrupts(int_timer2);

f_close(&fsrc);

}