I've been (slowly) working on a data logger for a project. The idea is to read in data from the onboard ADC, convert to comma separated values, and then write these to a file on an SD card. All of this is working until a signal is attached to the ADC. This can be an arbitrary sinusoid, attached to ground directly, or attached to ground through a pull down resistor and the issue occurs.
Running it without anything attached produces the following: "Power on, begin new logging. 85,9,0,0,0,0,12,..." and so on -- basically sitting at 0 with some slight noise. Running anything on the input produces reads correctly (as far as I can tell), converts to CSV correctly, but then writes out "潐敷湯敢楧敮⁷潬杧湩 㜳〰㌬㠶ⰴ..." Interestingly, the initial "Power on" message does write out correctly (if I pause and remove the SD card), but is overwritten by the garbage data. Additionally, for approximately the same amount of time running, the garbage data files are orders of magnitude larger (1KB vs ~100KB).
I've also disabled interrupts before the attempt to convert/write to hopefully prevent any issues with re-entrancy that might be occurring (doesn't seem to be the case).
Relevant portions of code:
while(1){
if(buff1_full_flag || buff2_full_flag){
DINT;
DataConversion();
EINT;
}
}
void DataConversion(void){
static Uint16 buffer_writes = 0;
char write_buffer[WRITE_BUFFER_SIZE];
char convert_buffer[TEMP_BUFFER_SIZE];
Uint16 i = 0;
Uint16 char_printed = 0;
size_t string_length = 0;
if(buff1_full_flag){ //buffer 1 full, needs to write
buff2_ready_flag = 1; // write to buffer 2
buff1_ready_flag = 0; // do not write to buffer one until buffer 2 is full
memset(write_buffer, 0,WRITE_BUFFER_SIZE); //clear write buffer
for(; i < DATA_BUFFER_SIZE;i++){
memset(convert_buffer, 0, TEMP_BUFFER_SIZE);
char_printed = snprintf(convert_buffer, TEMP_BUFFER_SIZE, "%u,",data_buff_1[i]);
if(char_printed <= 0){
while(1);
}
strcat(write_buffer,convert_buffer);
}
fresult = f_open(&g_sFileObject, "data.txt", OPEN_MODE);
if(fresult != FR_OK){
while(1); //hold for debugging
}
string_length = strlen(write_buffer);
fresult = f_lseek(&g_sFileObject, f_size(&g_sFileObject));
if(fresult != FR_OK){
while(1); //hold for debugging
}
fresult = f_write(&g_sFileObject,write_buffer,string_length,&bytes_written);
if(fresult != FR_OK){
while(1); //hold for debugging
}
fresult = f_close(&g_sFileObject);
if(fresult != FR_OK){
while(1); //hold for debugging
}
memset(data_buff_1,0,DATA_BUFFER_SIZE); //clear buffer 1
buff1_full_flag = 0; //buffer 1 empty
buffer_writes++;
}
//other buffer convert code, same as above. Will be rewritten eventually to make it move concise.
}
What am I missing that would cause this?