Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Strings in C - help
11-05-2012, 01:38 PM, (This post was last modified: 11-05-2012, 01:39 PM by KiloOne.)
#12
RE: Strings in C - help
Hi Guys,

Again I've spent too much time on this one.

To me there are more than one problems here.

I can solve the CDC buffer issue by just CDC printing 64 bytes at a time off my long string.

And, strcat() seems to be causing me issues so using sprintf to join strings as a kludge may have to be my solution for now (see // add timestamp below).

I do not like the inefficiency of the code I came up with but at PIC32 speeds it probably is OK.

Maybe someone could show me the elegant way to print 64 bytes at a time from a string (in the #ifdef section below), here is my working attempt in my SD card writing section:
Code:
// SD write process takes about 25-30 msec to write 200 char to file (tested up to 4mb file.)
// 8 ms of this is to mount, most of rest of time is the flush
// need global filename char variable with 8.3 filename in it
void storeit(char* intxt)  {
    if (strcmp(filename,"empty")==0) return;
    SPI_close();                                    // disables other SPI devices
    FIL     fil;                                    // File object
    FRESULT res, rc;
    UINT    btw, bw;
    char storetxt[400] = "";                        // set max write size to user needs!!!!!!                        
    sprintf(storetxt, "%4lu,%s", millis()/1000,intxt); // add timestamp
    #ifdef DEBUGMODE                                // adds CDC for debug (costs some overhead)
        char tmp[66] = "";
        char CDCline[400] = "";                        // set max write size to user needs!!!!!!
        strcpy(CDCline,storetxt);
        while (1==1){                                
            if (strlen(CDCline)<=64){
                CDC.println(CDCline);
                break;
            } else {
                strncpy(tmp,CDCline,64);
                CDC.print(tmp,strlen(tmp));
                strcpy(tmp,CDCline+64);
                strcpy(CDCline,tmp);
            }
        }
    #endif
    btw = strlen(storetxt);                            // number of Bytes To be Written
    SD.mount(40);                          
    res = SD.open(&fil, filename, FA_OPEN_ALWAYS | FA_READ | FA_WRITE);    // appends if file exists
    if (!res) {
        digitalWrite(YELLOWLED, HIGH);          
        SD.seek(&fil,fil.fsize);                    // Set R/W pointer to end of file
        rc = SD.write(&fil, storetxt, btw, &bw);    // write new data to file
        SD.flush(&fil);                             // flush cache to avoid data loss from loss of power etc
        SD.close(&fil);                             // Included for safety to ensure file properly closed
        SD.unmount();                                // in case user removes card instead of proceeding.
        digitalWrite(YELLOWLED, LOW);          
    }
    SPI_6803();                                     //reenables other SPI devices with user
}

void SPI_6803() {
    SPI_close();
    SPI_init();                         // left clock at 250khz, not much gain at 1mhz since reading all 18 volt regs takes 1ms while conversion takes 13ms
    SPI2CONCLR = 0x8000;                // turn off SPI mode before changing an SPI register!!!!!!!!!!!
    SPI2CONCLR = 0x100;                 // clock edge bit(8) to 0    for PHA POL needs                        
    SPI2CONSET = 0x40;                    // clock polarity bit(6) to 1     for PHA POL needs                        
    SPI2CONSET = 0x200;                 // SMP bit(9) to 1    hex of 2^9
    SPI2CONSET = 0x8000;                // turn SPI mode back ON after changing an SPI register!!!!!!!!!!!
    pinMode(39, OUTPUT);                // #SS CSBI or whatever is PIC pin RF0 at pin 39 in digitalw.c    
}

Thanks,
Dale
PIC32-Pinguino-OTG Rev C and PIC32-PINGUINO-MICRO rev.B
Win XP SP3
r381 x.3 Big Grin
AND spi.c {} error fixed
AND sdmmc.c pin error fixed
AND diskio.c fixed, MICRO can't use the RTCC
AND analog.c fixed for MICRO
Reply


Messages In This Thread
Strings in C - help - by KiloOne - 10-05-2012, 09:24 PM
RE: Strings in C - help - by mf01 - 10-05-2012, 10:00 PM
RE: Strings in C - help - by KiloOne - 10-05-2012, 10:30 PM
RE: Strings in C - help - by KiloOne - 10-05-2012, 10:06 PM
RE: Strings in C - help - by jpmandon - 10-05-2012, 10:18 PM
RE: Strings in C - help - by mf01 - 10-05-2012, 10:43 PM
RE: Strings in C - help - by KiloOne - 10-05-2012, 10:45 PM
RE: Strings in C - help - by jpmandon - 10-05-2012, 11:30 PM
RE: Strings in C - help - by mf01 - 10-05-2012, 11:32 PM
RE: Strings in C - help - by moreno - 11-05-2012, 06:59 AM
RE: Strings in C - help - by mf01 - 11-05-2012, 09:10 AM
RE: Strings in C - help - by KiloOne - 11-05-2012, 01:38 PM
RE: Strings in C - help - by pingotg - 11-05-2012, 02:46 PM
RE: Strings in C - help - by KiloOne - 11-05-2012, 03:08 PM
RE: Strings in C - help - by mf01 - 11-05-2012, 03:08 PM
RE: Strings in C - help - by pingotg - 11-05-2012, 03:27 PM
RE: Strings in C - help - by KiloOne - 11-05-2012, 03:53 PM
RE: Strings in C - help - by pingotg - 11-05-2012, 07:19 PM
RE: Strings in C - help - by mf01 - 11-05-2012, 03:47 PM
RE: Strings in C - help - by mf01 - 11-05-2012, 03:57 PM
RE: Strings in C - help - by KiloOne - 11-05-2012, 04:23 PM
RE: Strings in C - help - by KiloOne - 11-05-2012, 08:33 PM
RE: Strings in C - help - by moreno - 11-05-2012, 08:53 PM
RE: Strings in C - help - by KiloOne - 13-05-2012, 12:34 AM
RE: Strings in C - help - by pingotg - 11-05-2012, 08:58 PM
RE: Strings in C - help - by KiloOne - 11-05-2012, 09:50 PM
RE: Strings in C - help - by pingotg - 12-05-2012, 09:55 AM
RE: Strings in C - help - by moreno - 12-05-2012, 07:45 AM
RE: Strings in C - help - by KiloOne - 12-05-2012, 03:31 PM
RE: Strings in C - help - by pingotg - 12-05-2012, 05:22 PM
RE: Strings in C - help - by KiloOne - 12-05-2012, 05:40 PM
RE: Strings in C - help - by pingotg - 12-05-2012, 06:11 PM
RE: Strings in C - help - by KiloOne - 12-05-2012, 07:27 PM
RE: Strings in C - help - by pingotg - 13-05-2012, 04:48 PM
RE: Strings in C - help - by bjoernp - 15-05-2012, 05:42 PM
RE: Strings in C - help - by KiloOne - 17-05-2012, 12:09 AM

Forum Jump:


Users browsing this thread: 2 Guest(s)