• AVR Freaks

FatFs on dspic33ep not working

Author
Reykor
New Member
  • Total Posts : 25
  • Reward points : 0
  • Joined: 2016/09/14 08:50:30
  • Location: 0
  • Status: offline
2018/05/26 10:54:42 (permalink)
0

FatFs on dspic33ep not working

I have a dspic33ep256mu806 and I'm trying to write a file to an sd card using the FatFs library. I've already got microchip's mla library working but the write speed wasn't enough so I'm changing to fatfs. 
When I try to do f_mount, it returns FR_NOT_READY, meaning that something in the hardware layer is poorly configured.
 
I'm using FCY = 16MHz and I've checked that the SPI clock is 333KHz using an oscilloscope.
Both configurations below are being called in the beginning of my main.
 
Here is my SPI configuration:

SPI1STATbits.SPIEN = 0; //Assure module is disabled

    /* SPI1CON1 */
    SPI1CON1bits.DISSCK = 0;
    SPI1CON1bits.DISSDO = 0;
    SPI1CON1bits.MODE16 = 0;
    SPI1CON1bits.SSEN = 0;
    SPI1CON1bits.MSTEN = 1;
    /* Timing configuration */
    SPI1CON1bits.CKP = 0;
    SPI1CON1bits.CKE = 1;
    SPI1CON1bits.SMP = 0;

    /* Chose baudrate prescalers */
    SPI1CON1bits.PPRE = 1;
    SPI1CON1bits.SPRE = 5;

    /* SPI1CON2 */
    SPI1CON2bits.FRMEN = 0; // Disable all framed SPI support
    SPI1CON2bits.SPIBEN = 1; // Enhanced mode is enabled

    /* SPI1STAT */
    SPI1STATbits.SPISIDL = 0; // continue operation in Idle
    SPI1STATbits.SPIROV = 0; // Clear overflow
    SPI1STATbits.SISEL = 0b011; // Set interrupt to SPI receive buffer full is set (Not used)

    SPI1STATbits.SPIEN = 1;

 
Here is my PPS configuration:
 
/* Configure SPI Pins */
/* SDI - E6(RP86)
* SDO - E4(RP84)
* SCK - E5(RP85)
* CS - E7(RP86)
*/

LATEbits.LATE7 = 1;
TRISEbits.TRISE7 = 0;

TRISEbits.TRISE6 = 1;
TRISEbits.TRISE4 = 0;
TRISEbits.TRISE5 = 0;
 
PPSUnLock;
PPSInput(IN_FN_PPS_SDI1, IN_PIN_PPS_RP86);
PPSOutput(OUT_FN_PPS_SDO1, OUT_PIN_PPS_RP84);
PPSInput(IN_FN_PPS_SCK1, IN_PIN_PPS_RP85);
PPSOutput(OUT_FN_PPS_SCK1, OUT_PIN_PPS_RP85);
PPSLock;


 
I've redefined the tx and rx functions to use the MCC generated function:


void SPI1_Exchange( uint8_t *pTransmitData, uint8_t *pReceiveData )
{

    while( SPI1STATbits.SPITBF == true )
    {

    }

    if (SPI1_TransferModeGet() == SPI_TRANSFER_MODE_16BIT)
        SPI1BUF = *((uint16_t*)pTransmitData);
    else
        SPI1BUF = *((uint8_t*)pTransmitData);

    while ( SPI1STATbits.SRXMPT == true);

    if (SPI1_TransferModeGet() == SPI_TRANSFER_MODE_16BIT)
        *((uint16_t*)pReceiveData) = SPI1BUF;
    else
        *((uint8_t*)pReceiveData) = SPI1BUF;

    return;
}
uint8_t sd_rx(){

    uint8_t receive;
    SPI1_Exchange(NULL, &receive);
    
    return receive;
}

void sd_tx(uint8_t data){

    SPI1_Exchange(&data, NULL);
}

 
Anyone's got an idea of what I'm doing wrong?
#1
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
Re: FatFs on dspic33ep not working 2018/05/26 10:59:56 (permalink)
0
you're passing null pointers, and not checking for them.
 
(I'd help more but the forum is unusable, just like this whole planet)

GENOVA :D :D ! GODO
#2
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11430
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: FatFs on dspic33ep not working 2018/05/26 11:10:44 (permalink)
0
Does the initialization work as expected?
#3
Reykor
New Member
  • Total Posts : 25
  • Reward points : 0
  • Joined: 2016/09/14 08:50:30
  • Location: 0
  • Status: offline
Re: FatFs on dspic33ep not working 2018/05/26 11:19:43 (permalink)
0
DarioG
you're passing null pointers, and not checking for them.
 
(I'd help more but the forum is unusable, just like this whole planet)


Are you talking about the sd_tx and sd_rx functions?


The documentation for the SPI1_Exchange(..) is:

 
* @brief Exchange one element of data. Transmit or receive
*
* @param pTransmitData Data element to send. NULL when receiving
* only.
* @param pReceiveData Data element to receive. NULL when
* transmitting only.
 
So I send NULL in the transmit pointer when I want to read and NULL in the read pointer when I want to write.
 
#4
Reykor
New Member
  • Total Posts : 25
  • Reward points : 0
  • Joined: 2016/09/14 08:50:30
  • Location: 0
  • Status: offline
Re: FatFs on dspic33ep not working 2018/05/26 11:23:23 (permalink)
0
jtemples
Does the initialization work as expected?




I checked that my SPI clock is indeed 333KHz and I can see data on the bus with an oscilloscope, so I think so.
#5
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
Re: FatFs on dspic33ep not working 2018/05/26 11:32:23 (permalink)
0
It's strange then: from the code above, when a NULL is passed (in both places) the function tries to use it (them) anyway. This should cause an exception on that PIC

GENOVA :D :D ! GODO
#6
Reykor
New Member
  • Total Posts : 25
  • Reward points : 0
  • Joined: 2016/09/14 08:50:30
  • Location: 0
  • Status: offline
Re: FatFs on dspic33ep not working 2018/05/26 11:49:56 (permalink)
0
DarioG
It's strange then: from the code above, when a NULL is passed (in both places) the function tries to use it (them) anyway. This should cause an exception on that PIC


That should never happen. However, I've added a check. Still not working.
#7
Antipodean
Super Member
  • Total Posts : 1778
  • Reward points : 0
  • Joined: 2008/12/09 10:19:08
  • Location: Didcot, United Kingdom
  • Status: offline
Re: FatFs on dspic33ep not working 2018/05/26 12:44:37 (permalink)
0
Why do you say the code from the MLA is not fast enough?
 
What speed are you wanting?
 
The SPI interface on an SD card is not a particularly fast interface anyway.
 
 

Do not use my alias in your message body when replying, your message will disappear ...

Alan
#8
RISC
Super Member
  • Total Posts : 5379
  • Reward points : 0
  • Status: offline
Re: FatFs on dspic33ep not working 2018/05/26 13:04:31 (permalink)
0
Hi,
You mentioned that MLA is not fast enough and you only run SPI @ 333kHz ???
Regards
 
#9
Gort2015
Klaatu Barada Nikto
  • Total Posts : 3348
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: FatFs on dspic33ep not working 2018/05/26 13:49:43 (permalink)
0
Plus slow clock fcy.

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#10
DarioG
Allmächtig.
  • Total Posts : 54081
  • Reward points : 0
  • Joined: 2006/02/25 08:58:22
  • Location: Oesterreich
  • Status: offline
Re: FatFs on dspic33ep not working 2018/05/27 02:42:26 (permalink)
0
333KHz could be the "startup phase" of the SDcard, if I recall correctly. Then it should increase

GENOVA :D :D ! GODO
#11
Gort2015
Klaatu Barada Nikto
  • Total Posts : 3348
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: FatFs on dspic33ep not working 2018/05/27 03:50:38 (permalink)
0
Initialize slow then set SPI to 10MHz.
 
That's what I did in my sdcard library and run the mpu at full speed.
post edited by Gort2015 - 2018/05/27 03:51:44

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#12
Jump to:
© 2019 APG vNext Commercial Version 4.5