Esempio n. 1
0
uint8_t fsmanMountSD(uint8_t * result_code){
    
    uint8_t ret=0;
    
    MEDIA_INFORMATION  *mediainfo = MDD_MediaInitialize();
    
    if(mediainfo->errorCode != MEDIA_NO_ERROR){
        __debug("MDD media init failed");
    }
    
    if(MDD_MediaDetect() == 0){
        __debug("MDD media detect failed");
        return 0;
    }
    
    if(FSInit() == 0){
        __debug("FSInit failed");
        return 0;
    }

    return 1;
       
}
Esempio n. 2
0
int main(void)
{
    // Watchdog Timer Enabled/disabled by user software
    // (LPRC can be disabled by clearing SWDTEN bit in RCON registe

    // Configure Oscillator to operate the device at 40Mhz
    // Fosc= Fin*M/(N1*N2), Fcy=Fosc/2
    // Fosc= 8M*40/(2*2)=80Mhz for 8M input clock
    PLLFBD = 38; // M=40
    CLKDIVbits.PLLPOST = 0; // N1=2
    CLKDIVbits.PLLPRE = 0; // N2=2
    OSCTUN = 0; // Tune FRC oscillator, if FRC is used

    RCONbits.SWDTEN = 0; /* Disable Watch Dog Timer*/

    // Clock switch to incorporate PLL
    __builtin_write_OSCCONH(0x03); // Initiate Clock Switch to Primary
    // Oscillator with PLL (NOSC=0b011)
    __builtin_write_OSCCONL(0x01); // Start clock switching
    while (OSCCONbits.COSC != 0b011); // Wait for Clock switch to occur

    while (OSCCONbits.LOCK != 1) {
    }; /* Wait for PLL to lock*/

//    bufferPointer = NULL;
    Uart2Init(InterruptRoutine);

    if (!CB_Init(&circBuf, cbData, CB_SIZE)) FATAL_ERROR();

    // Generate a fake input to record to the circular buffer
    // give beginning and end special characters
    unsigned char goodData[512];
    int i;
    for (i=0; i<512; i++) {
        goodData[i] = (char)i;
    }
    goodSum = checksum(goodData, 512);
    
    Uart2PrintStr("Begin.\n");
    file = NewSDInit("newfile.txt");

    int SDConnected = 0;
    while(1)
    {
//        if (bufferPointer != NULL) { TODO implement this?
//            CB_WriteMany(&circBuf, bufferPointer, UART2_BUFFER_SIZE, 1); // the 1 is arbitrary
//            bufferPointer = NULL;
//        }
        if (SD_IN)
        {
            // if the board was just plugged in try to reinitialize
            if(!SDConnected) {
                MEDIA_INFORMATION * Minfo;
                do {
                    Minfo = MDD_MediaInitialize();
                } while(Minfo->errorCode == MEDIA_CANNOT_INITIALIZE);
                SDConnected = 1;
            }
            // When we are connected and initialized, poll the buffer, if there
            // is data, write it.
            unsigned char outData[SD_SECTOR_SIZE];
            if (CB_PeekMany(&circBuf, outData, SD_SECTOR_SIZE)){
                if(NewSDWriteSector(file, outData)){
                    // Remove the data we just written.
                    if(checksum(outData, 512) != goodSum) {
                        FATAL_ERROR();
                    }
                    CB_Remove(&circBuf, SD_SECTOR_SIZE);
                }
            }
        } else {
            SDConnected = 0;
        }
    }
}