Pinguino Forum

Full Version: PIC32 PINGUIONO's and SD
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5 6 7 8
Hi Pingotg,

sorry to disagree but I think you will find that the PIC32 starts with the secondary oscillator (SOSC) switched off and that a bit in one of the control registers needs to be set to enable it (see Section 6. Oscillators DS61112G in the MicroChip documentation). I do recall whilst doing some debugging the RTCC library a while ago coming across the functions that start the RTCC which specifically enabled the SOSC when starting the real time clock. (See function RTCC_SOSCenable( ) in RTCC.c).

It may be a specific leakage/board layout problem which these pins are particularly sensitive to.


Hi Dale,

I am not sure whether a software only fix is possible. If you think of the logic of what was happening to you. The problem was stopping you turning on an output - which is a similar fix to what you have suggested implementing, therefore will the problem stop the code that is implementing the fix working?

Hope you have a good lunch Wink

Report 2

Well I misreported which pins are the two oscillator pins on the connector.

They are not CON1 2 & 3, they are CON1 1 & 2. But that is not a factor yet. I tried replacing my skin on 2-3 with 51k resistance, this failed.

Skin works on 1-2 also but again 51k across 1-2 no work either.

Then I took the pins to 1 and 2 to GND individually thru 10k resistors, no work.

Then 1 & 2 thru 10k each to 5v, no work either.

Same for pins 2 & 3, thru 10k to GND then 5v, none worked.

Only thing work was each time I removed a jumper, I got lights.

This means code is hanging until state change on one or more of these pins.

I suspect that whenever I used the skin method my clunky big fingers and fingerprints were moving slightly and creating clock pulses so to say.

Back to software...... LOL

Looks to me like it is possible that not only is the secondary oscillator enabled but that there is a bit of software that is waiting for a state change/s.

It is also possible that the PIC is in two speed startup mode.

Where can I see where the OSCCON and DEVCFG1 resisters are set?

Hi Mark (& Dale),

Looks like somewhere there's some software involved. Some init (enabling SOSC or whatever) would still be where I'd be looking if I had time.

So, I ran the code without SD.mount(40);

I put this as the loop function:
void loop()  {
sprintf(buff, "%10X", OSCCON);
CDC.printf(buff );
sprintf(buff, "%10X\l\r\n", DEVCFG1);
CDC.printf(buff );

and found out the value of OSCCON is 14D3360 and DEVCFG1 is FF60CADBl

That means that secondary oscillator is off and two speed startup is off, without SD functions.

That means that unless mounting the SD turns either one on, this is a dead-end.

Wow, so hard.

On a long shot I decided to just get rid of all reference to RTCC in the SD functions.

The only place was in diskio.c.



Now I am pretty sure we don't want to do this but it worked to let the program execute properly.
Maybe print them again after the mount, when it's running slow and you're using your pinky to get it to run?


Sorry but I can not see any CDC data from setup function as Hyperteminal has no feature that scans for the CDC serial port to become active and then open it and capture first data. Unless you know of a way I can do it.

Can't you use CDC in setup, if needs be with a 10 second delay and then use it again to print what you really wanted to see? I'm guessing 10 secs is long enough to get HyperT to open the port. I suppose you lose the first set of output but just print anything.

Dah, yes I will try

(07-05-2012, 12:09 AM)pingotg Wrote: [ -> ]Maybe print them again after the mount, when it's running slow and you're using your pinky to get it to run?


Pretty sure it was never running slow now, it was waiting for me to give it some input somehow on the SOSC lines.

I will put the RTCC refs back in and print it after my pinky does its magic.

Ok with finger trigger, OSCCON = 14D3362 DEVCFG1 = FF60CADBl

So, here is bits 0-2 of OSCCON def:
bit 2-0 FNOSC<2:0>: Oscillator Selection bits
111 = Fast RC Oscillator with divide-by-N (FRCDIV)
110 = FRCDIV16 Fast RC Oscillator with fixed divide-by-16 postscaler
101 = Low-Power RC Oscillator (LPRC)
100 = Secondary Oscillator (SOSC)
011 = Primary Oscillator (POSC) with PLL module (XT+PLL, HS+PLL, EC+PLL)
010 = Primary Oscillator (XT, HS, EC)(1)
001 = Fast RC Oscillator with divide-by-N with PLL module (FRCDIV+PLL)
000 = Fast RC Oscillator (FRC)

Not really sure what this means except that in both cases secondary oscillator was off.
So, for now I am using a diskio.c file that has all reference to RTCC taken out of it.

That includes taking out the #include <rtcc.c>. If I left even that include in, I would have to use the pinky trigger method to start the program.

I have confirmed that using this 'no RTCC' diskio.c for OTG compilation results in code that does read and write files to the SD. The file date is 1-1-1980 12:00 am but that is OK.

And I have to believe that I am the only person who is currently:


The program is:
#define B_SIZE 32            // Size of read buffer - arbitrary length,
                                    // try changing it to improve efficiency
UINT            i, l, btw, bw, br;
u32            b;
char            filename[]    = "Testing.txt";    
FIL            fil;                            // File object
FRESULT        res,    rc;
DWORD        file_size;
char            read_buff[B_SIZE];
char    buff[80];

void setup() {
    pinMode( YELLOWLED ,OUTPUT );
    digitalWrite(YELLOWLED, HIGH);         // Turn YELLOWLED On
    digitalWrite(YELLOWLED, LOW);         // Turn YELLOWLED Off

void loop()  {
    char    buffer[40]    = "1234,";        // arbitary value of 40 for length of character array
    btw = strlen(buffer);                //    number of Bytes To be Written
    SD.mount(40);                         //    Allocate file structure
    /*    Open existing file or new file if file does not already exist */
    res =, filename, FA_OPEN_ALWAYS | FA_READ | FA_WRITE);
    sprintf(buff , "%s opened to append returned %10X\r\n", filename , res);
    if (!res){
        digitalWrite(YELLOWLED, HIGH);         // Turn YELLOWLED On
        digitalWrite(YELLOWLED, LOW);         // Turn YELLOWLED Off
        file_size = fil.fsize;                        // Get existing file size,file_size);                    // Set R/W pointer to end of file
        /*    Write data to file */
        rc = SD.write(&fil, buffer, btw, &bw);        // write new data to file
        SD.flush(&fil);                                // flush any data in cache to file
                                                    // 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, HIGH);         // Turn YELLOWLED On
        digitalWrite(YELLOWLED, LOW);         // Turn YELLOWLED Off

    /*    Re-open existing data file - effectively resets R/W pointer to start of file */
    SD.mount(40);                             //    Allocate file structure
    res =, filename, FA_OPEN_EXISTING | FA_READ | FA_WRITE);
    sprintf(buff , "%s opened to read returned %10X\r\n", filename , res);
    if (!res){

        /*    Display data file on terminal including new data added above */
        CDC.println("\n\r*** File contents ***");
        b = 0;                                 // byte counter
        l = 0;                                 // line counter
        do {
            rc =, read_buff, B_SIZE, &br);    
                                                    // Read a chunk of file
            b = b + br;                        // update byte counter
            for(i = 0; i < br; i++){
                CDC.printf("%c", read_buff[i]);
                if (read_buff[i] == '\n'){
                    l++;                        // increment line counter
        } while(br == B_SIZE);            // while buffer is full
        SD.close(&fil);        //    Included for safety to ensure file properly closed
        SD.unmount();            //    in case user removes card instead of proceeding.
        CDC.println("*** End of file ***  ");
        CDC.printf("*** %u lines, %u bytes read.\r\n", l, b);
        digitalWrite(YELLOWLED, HIGH);         // Turn YELLOWLED On
        digitalWrite(YELLOWLED, LOW);         // Turn YELLOWLED Off


The CDC output is:

And the diskio.c for the \p32\include\pinguino\libraries\sd directory is attached.

This is a temporary diskio.c fix until someone figures out why I had to remove all RTCC references from it, that won't be me for a while.

Thanks again everyone and now I am off the bed after being at this for 17 hours today.

Dale Big Grin
*** Congratulations ***

Dale, taking your good work and intuition I have further modified the diskio.c file we were using earlier yesterday so that it should deal automatically with this - I have put a #if defined ( ) - #end if around the #include <rtcc.c>. This is consistent with what we did in the get_fattime( ) function within diskio.c earlier and should mean that the #include <rtcc> and the code that actually accesses the RTCC is only included for boards that are known to support the RTCC. This hopefully will mean that if someone uses the library with a new board it should work all be it without using the RTCC even if it is there.

I have tried it this version with a PIC32 Pinguino OTG and it works as expected. When you have the time could you try the attached version of fileio.c and see if it works for you. If it does we can see about getting the modified/updated versions of the various SD library files included in the repository so that it is available to all.

Pages: 1 2 3 4 5 6 7 8