CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

sd_mmc & PCD
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
kmp84



Joined: 02 Feb 2010
Posts: 354

View user's profile Send private message

sd_mmc & PCD
PostPosted: Mon Nov 25, 2024 9:32 am     Reply with quote

Hello all,

Any one successful run CCS's "ex_mmcsd.c" or "ex_fat.c" with PCD devicess (dsPic33EP512MU810)?

I have no success with those examples and find this one https://simple-circuit.com/pic18f4550-sd-card-read-write-files/ which init fat lib and return card type, but can't create dir. and files and open them.

My test code:

CCS v.5.115

Code:

#include <33EP512MU810.h>
#device PASS_STRINGS = IN_RAM
#device ICSP=1
#use delay(crystal=8MHz, clock=32MHz)

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES CKSFSM                   //Clock Switching is enabled, fail Safe clock monitor is enabled
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOJTAG                   //JTAG disabled

#define PER_EN PIN_D5
#define PER_ON() output_low(PER_EN)
#define PER_OFF() output_float(PER_EN)

/* HW UART1 */
#pin_select U1RX=PIN_E1         // Rx Com1.
#pin_select U1TX=PIN_E0         // Tx Com1.   
#define EN1_485 PIN_D1          // RE/DE Com1.

#use rs232(UART1,baud=115200,errors,enable=EN1_485)

#include <stdlib.h> // for atoi32

// Use HW_SPI1 [ok]
#define MMCSD_SPI_HW SPI1
#define MMCSD_PIN_SELECT  PIN_D6    //PIN_C2 //o
#pin_select SCK1OUT=PIN_F12
#pin_select SDO1=PIN_F13
#pin_select SDI1=PIN_A1

 
// Include MMC/SD card driver source file
#include <mmcsd_m.c>
 
// Include FAT library source file
#include <fat_m.c>
 
void main(void) {

  delay_ms(100);
  PER_ON();
  delay_ms(100);

  int8 i;
 
  FILE myfile;
 
  printf("\r\n\nInitialize FAT library ... ");
  delay_ms(2000);
 
  // Initializing the FAT library as well as the SD card ---> returns 0 if OK
  i = fat_init();
 
  if(i != 0)
    printf("Error initializing FAT library!");
 
  else {
 
    printf("FAT Library initialized");
    delay_ms(2000);
 
    // Display SD card type ---> MMC, SDSC or SDHC
    printf("\r\n\r\nCard Type: ");
 
    switch(g_card_type) {
      case MMC: printf("MMC"); break;
      case SDSC: printf("SDSC"); break;
      case SDHC: printf("SDHC");
    }
 
    delay_ms(2000);
    // Create folder 'Test Dir'
    printf("\r\n\r\nCreate 'Test Dir' folder ... ");
    if(mk_dir("/Test Dir/") == 0)
      printf("OK");
    else
      printf("error creating folder");
 
    delay_ms(2000);
    // Create a text file 'log.txt'
    printf("\r\n\r\nCreate 'log.txt' file ... ");
    if(mk_file("/log.txt") == 0)
      printf("OK");
    else
      printf("error creating file");
 
    delay_ms(2000);
    // Open the last created file 'log.txt' with write permission ('w')
    printf("\r\nOpen 'log.txt' file ... ");
    if(fatopen("/log.txt", "w", &myfile) != 0)
      printf("error opening file");
    else {
      printf("OK");
 
      delay_ms(2000);
      // Write some thing to the text file
      printf("\r\nWriting to the text file 'log.txt' ... ");
      if(fatputs("Hello,\r\nThis is a text file created using PIC18F4550 microcontroller and CCS C compiler\r\nHave a nice day ...", &myfile) == 0)
        printf("OK");
      else
        printf("writing error");
      delay_ms(2000);
      // Now close the file
      printf("\r\nClosing the file 'log.txt' ... ");
      if(fatclose(&myfile) == 0)
        printf("OK");
      else
        printf("closing error");
    }
 
    delay_ms(2000);
    // Reading 'log.txt' file
    printf("\r\n\r\nReading 'log.txt' file:");
    delay_ms(2000);
    // Open 'log.txt' file with read permission ('r')
    printf("\r\nOpen 'log.txt' file ... ");
    if(fatopen("/log.txt", "r", &myfile) != 0)
      printf("error opening file");
 
    else {
      printf("OK");
      delay_ms(2000);
      // Print the whole file
      printf("\r\nPrint 'log.txt' file:\r\n\r");
      delay_ms(2000);
      fatprintf(&myfile);
      delay_ms(2000);
      // Now close the file
      printf("\r\n\r\nClosing the file 'log.txt' ... ");
      if(fatclose(&myfile) == 0)
        printf("OK");
      else
        printf("closing error");
 
    }
 
  }
 
  delay_ms(2000);
  printf("\r\n\r\n***** END *****");
 
  while(TRUE) ;
 
}
// End of code


Best Regards,
Ttelmah



Joined: 11 Mar 2010
Posts: 19534

View user's profile Send private message

PostPosted: Mon Nov 25, 2024 11:36 am     Reply with quote

How big is the card?????
Assuming it is a reasonable size, you will need an mmdhc driver, not the
standard driver. Look here:
[url]
http://www.ccsinfo.com/forum/viewtopic.php?t=52490
[/url]
The supplied driver has some of the fixes needed, but not all.

There is also an issue regarding how to program the SPI clock speed.
The method used by the standard driver is not supported properly with the
PIC24/30. Do a search here. Some time ago, I posted how to to this instead
by setting up two streams for the different rates, and using these for the
speeds needed. This works for the 16bit PIC's.

Most of the people here wanting a reliable driver, have bought the
Brush Electronics driver.

Also as a general comment, 90% of problems with SD cards, is with
either not having the correct pull-ups on the lines (these are required not
optional), or with how well the supply is decoupled close to the card. Again
a very large low ESR capacitor is required very close to the SD itsellf. The
spikes in current during some operations are awful.....

The circuits you point to are not suitable. They are for a 5v PIC. You
are using a 3.3v chip. Look at the sticky at the top of the forum about this.
kmp84



Joined: 02 Feb 2010
Posts: 354

View user's profile Send private message

PostPosted: Tue Nov 26, 2024 6:30 am     Reply with quote

Hello mr.Ttelmah

First thanks for replay!

The card size = SD-1GB. I didn't found your topic for spi clock. Here is attached schematics of sd card connection to cpu. https://easyupload.io/odp8kx

Some of the CCS's driver are specific designed for some part of PIC micro etc. PCM.PCH,PCD devices, but sd,fat are not marked as such?


Best Regards,
Ttelmah



Joined: 11 Mar 2010
Posts: 19534

View user's profile Send private message

PostPosted: Tue Nov 26, 2024 10:23 am     Reply with quote

The pull up resistors should be 10K. There should be one on each of
DAT0, DAT1, DAT2 & CS.

The article about how to change speed is:
[url]
https://www.ccsinfo.com/forum/viewtopic.php?t=50645
[/url]

1GB should be OK with the standard drivers.
The FAT driver has no processor specific code, hence provided the hardware
driver is done right, works with any PIC.
The SD is correct for PIC16's and 18's, but the speed change used to
change the speed for the initialisation is not correct for the PIC24/30.
kmp84



Joined: 02 Feb 2010
Posts: 354

View user's profile Send private message

PostPosted: Wed Nov 27, 2024 7:26 am     Reply with quote

Hello,

Still no success with this two spi stream configuration:

Code:


#include <33EP512MU810.h>
#device ICSP=1
#use delay(crystal=8MHz, clock=32MHz)

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES CKSFSM                   //Clock Switching is enabled, fail Safe clock monitor is enabled
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOJTAG                   //JTAG disabled



// HW_SPI1
#define MMCSD_SPI_HW SPI1
#define MMCSD_PIN_SELECT  PIN_D6   
#pin_select SCK1OUT=PIN_F12
#pin_select SDO1=PIN_F13
#pin_select SDI1=PIN_A1



//Use SW_SPI
#define MMCSD_PIN_SCL     PIN_F12     //o
#define MMCSD_PIN_SDI     PIN_A1      //i
#define MMCSD_PIN_SDO     PIN_F13     //o


//In your configuration
#use spi(MASTER, FORCE_SW, DI=MMCSD_PIN_SDI, DO=MMCSD_PIN_SDO, CLK=MMCSD_PIN_SCL, BITS=8, MSB_FIRST, MODE=0, baud=100000, stream=SLOW)
#use spi(MASTER, MMCSD_SPI_HW, BITS=8, MSB_FIRST, MODE=0, baud=16000000, stream=FAST)

#word SPI1STAT=getenv("SFR:SPI1STAT")
#bit SSP1EN=SPI1STAT.15

#define MMCSD_SPI_XFER_SLOW(x)  spi_xfer(SLOW, x)
#define MMCSD_SPI_XFER(x)  spi_xfer(FAST, x)

....


and all funcion in "mmcsd_init()" use "MMCSD_SPI_XFER_SLOW" and
SSP1EN = FALSE;
...
...
Slow init funcions..

SSP1EN = TRUE;

and after that use "MMCSD_SPI_XFER".

Best Regards,
Ttelmah



Joined: 11 Mar 2010
Posts: 19534

View user's profile Send private message

PostPosted: Wed Nov 27, 2024 7:57 am     Reply with quote

You can't have a software and hardware stream both on the same pins.
You need to use two hardware streams, set for the different rates. The
pins are not available for use by the software driver, once the hardware
is enabled on them.
You can use a software stream for the slow rate, if you disable the pin
selection (ensure the fuse is set to allow multiple changes to the pin
selection, and use the inline pin_select function to deselect the pins
before using the software SPI, and then reselect them afterwards).
kmp84



Joined: 02 Feb 2010
Posts: 354

View user's profile Send private message

PostPosted: Wed Nov 27, 2024 8:11 am     Reply with quote

With this setup still not working..
Code:

#use spi(MASTER, MMCSD_SPI_HW, BITS=8, MSB_FIRST, MODE=0, baud=100000, stream=SLOW)
#use spi(MASTER, MMCSD_SPI_HW, BITS=8, MSB_FIRST, MODE=0, baud=16000000, stream=FAST)
Ttelmah



Joined: 11 Mar 2010
Posts: 19534

View user's profile Send private message

PostPosted: Wed Nov 27, 2024 8:56 am     Reply with quote

Several things:

First, with spi_xfer, and #use_spi, just use the spa_speed option. Where
the code changes speed, just change:
Code:

   #if (getenv("CLOCK") <= 80000000)
      #define MMC_SPI_CLK_DIV SPI_CLK_DIV_4
   #else
      #if defined(SPI_CLK_DIV_8)
         #define MMC_SPI_CLK_DIV SPI_CLK_DIV_8
      #else
         #define MMC_SPI_CLK_DIV SPI_CLK_DIV_16
      #endif
   #endif
   #error/warning the next line will only work if using SPI1
   setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_XMIT_L_TO_H | MMC_SPI_CLK_DIV);


To:

spi_speed(mmcsd_sp, 8000000);

This will take it as fast as possible.

However I have to say don't get stuck on this, you need to prove your
hardware is working and talking to the card _first_. Forget about FAT,
just try something really simple like leaving the clock set at the slow
boot rate, and reading the first physical sector from the disk. If this doesn't
work you have a hardware issue to sort first.
kmp84



Joined: 02 Feb 2010
Posts: 354

View user's profile Send private message

PostPosted: Wed Nov 27, 2024 4:22 pm     Reply with quote

Hello,

With those spi setup:
Code:

#use spi(MASTER, MMCSD_SPI_HW, BITS=8, MSB_FIRST, MODE=0, baud=100000, stream=mmcsd_spi)



and after init sd card at slow rate (no more 200000) and switch to high speed :

Code:

 spi_speed(mmcsd_spi, 20000000);  //8000000,  16000000, 20000000


ex_sdmmc.c work good! Smile, but ex_fat.c doesn't work (FAT16, FAT32) Sad
kmp84



Joined: 02 Feb 2010
Posts: 354

View user's profile Send private message

PostPosted: Thu Nov 28, 2024 2:48 am     Reply with quote

The main idea for use sdcard is to write some log in .txt file together with using TCP/IP communication and two serial port comm. Is it needed to include dspic DMA transfer modules for sdcard SPI or not ?

Best Regards,
Ttelmah



Joined: 11 Mar 2010
Posts: 19534

View user's profile Send private message

PostPosted: Thu Nov 28, 2024 8:49 am     Reply with quote

OK. Hurrah.
My suspicion is that the card is actually formatted with an MBR. The CCS
driver does not support this. There have been fixed drivers posted.

[url]
http://www.ccsinfo.com/forum/viewtopic.php?t=43402&highlight=sdhc
[/url]
kmp84



Joined: 02 Feb 2010
Posts: 354

View user's profile Send private message

PostPosted: Thu Nov 28, 2024 1:13 pm     Reply with quote

OK, Ehaa..

This corrections also not enough to use ex_fat.c
[url]
http://www.ccsinfo.com/forum/viewtopic.php?t=43402&highlight=sdhc
[/url]


I had forgotten what a pain it is to use a CCS compiler and their drivers. Maybe I should forget for logging to SD card. (Just returned to it because has a ready hardware for may current project:)). This CCS's drivers come in the begin with compiler and still not ready Shocked .

Many thanks mr.'Ttelmah' for your help Smile

Best Wishes
newguy



Joined: 24 Jun 2004
Posts: 1908

View user's profile Send private message

PostPosted: Thu Nov 28, 2024 2:03 pm     Reply with quote

Buy this: https://brushelectronics.com/software/fat-file-system-for-microchip-microcontrollers/

I have no affiliation with Brush Electronics; I am a customer. I can tell you that the driver works and works well. Given the price, it's well worth the time you'll save and the aggravation.
kmp84



Joined: 02 Feb 2010
Posts: 354

View user's profile Send private message

PostPosted: Thu Nov 28, 2024 3:20 pm     Reply with quote

Hi,

I know about Brush Electronics for a long time. (No need for advertising:). There is also an even cheaper solution Espressif 8266+SD(~5$)

Best Regards,
temtronic



Joined: 01 Jul 2010
Posts: 9236
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Nov 28, 2024 5:18 pm     Reply with quote

No need to advertise for the ESPxxx unit...

Cut the code yourself and it's 100% FREE ! Smile
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group