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 support@ccsinfo.com

Another MMC Over SPI FAT32 code!
Goto page Previous  1, 2, 3, 4, 5, 6  Next
 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
wielen



Joined: 15 Aug 2007
Posts: 3

View user's profile Send private message

problem solved with append
PostPosted: Thu Aug 23, 2007 3:14 pm     Reply with quote

I used a fixed time and he dit not like that, it works now verry when I increment the time. It works now verry wel. I did a number of write and read tests with large files up to 1Mbyte.
Andrei



Joined: 26 Aug 2007
Posts: 3

View user's profile Send private message

PostPosted: Sun Aug 26, 2007 6:02 am     Reply with quote

Hi miniman!
I'm having some problems with initializing mmc. The only thing I changed in MMC_SPI_FAT32.c is mmc port and pins, i'm using port d:
Code:

// Change the port to whatever port you are going to use
#use FAST_IO(D)
// Change this to set the right tris for your pins
#define SET_MMC_TRIS() set_tris_d(0b00001000)
// Change these pins to whatever pins you are using
#define ChipSel      pin_D0 // Chip-Select pin
#define ChipDout    pin_D1 // SPI-Data out pin
#define ChipClk      pin_D2 // SPI-Clock pin
#define ChipDin      pin_D3 // SPI-Data in pin


I used 1 kohm resistors instead of 820 ohm, so level conversion leaves about 2.8V. Is that enought for mmc? I used ATX power supply for powering the mmc, it gives me ~3.4V but I think it's ok? I don't really know is my problem in hardware or somewhere else...
Here's my main.c
Code:

#include <18F4550.h>

#fuses INTRC_IO,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN, NOPBADEN, ICPRT, NOMCLR
#use delay (clock=8000000)
#use rs232(baud=9600, XMIT=PIN_D7, BITS=8, STOP=1, parity=N, INVERT)

#include <STRING.H>
#include <MMC_SPI_FAT32.h> // As usual you also need to      ////
#include <MMC_SPI_FAT32.c> // include your device .h file... ////

void main(void)                                              ////
{                                                           ////
   char f,filename[20],res,i,c;                              ////
   while(TRUE)                                               ////
   {                                                         ////
      if(MMCInit() == MMC_OK)                                ////
      {                                                      ////
      printf("MMC initialized\r\n");                      ////
         InitFAT(); 
         strcpy(filename,"");                            ////
         f = InitList(filename);                             ////
        if(f & MMC_ERROR)                                   ////
            printf("Error");                                 ////
         else                                                ////
         {                                                   ////
            do                                               ////
            {                                                ////
               res = ListFiles(f);                           ////
               for(i=0;i<res;i++)                            ////
               {                                             ////
                  printf(FileList[i].name);                  ////
                  if(FileList[i].isDir)                      ////
                     printf("\\\r\n");//a "\" with row break ////
                  else                                       ////
                     printf("\r\n");                         ////
               }                                             ////
            } while(NextPage(f) == MMC_OK);                  ////
            CloseList(f); // frees the file, but the list is ////
         }                // still there...                  ////
         strcpy(filename,FileList[0].shortName);             ////
         f = fopen(filename,'r'); // open file for reading    ////
         if((f & MMC_ERROR) == 0) // No error, same as       ////
         {                        // if(f < MAXFILES)        ////
            while(fgetch(&c,f) == MMC_OK)                    ////
               printf(c);                                    ////
            fclose(f);                                       ////
         }                                                   ////
         else if(f == MMC_NOT_FOUND)                         ////
            printf("1st file in list was probebly a dir");   ////
         else                                                ////
            printf("Other error\r\n");                       ////
         while(TRUE);                                      ////
               // Loop forever, program is finished           ////
      }                                                      ////
      else                                                   ////
         printf("MMC init failed!\r\n");                     ////
      delay_ms(1000);                                        ////
      printf("Trying once more..\r\n");                      ////
   }                                                         ////

}   


The only thing I get is "MMC init failed!". The card is SanDisk 64MB and I have formated it to FAT32, 512byte/sector.
Miniman



Joined: 30 Apr 2007
Posts: 44

View user's profile Send private message

PostPosted: Sun Aug 26, 2007 7:41 am     Reply with quote

Hi
It sounds strange.. 2.8V should be enough. Have you forgotten the 100K ohm resistor on data out from card to your 3.3V source?
Well, otherwise I must say that I can not see what could be the problem. It would not hurt trying to increase the voltage on the clock and data pins so that it is over 3V, but it should not be the problem. Maby the SPI mode is diffrent in diffrent cards? you could try to change the #use SPI, then try to change the SAMPLE_RISE to SAMPLE_FALL and IDLE=1 to IDLE=0 try some diffrent combinations..
But as I have said, I do not know would could be that problem, I'm not an xpert on this. But it won't hurt trying diffrent settings.
Maby someone else know what it could be?

Best regards
Miniman
Milhaus



Joined: 20 Oct 2007
Posts: 9

View user's profile Send private message

PostPosted: Mon Nov 12, 2007 2:18 am     Reply with quote

Thanks Miniman,
your library work perfectly. But i have one question. I am using interrupts in my PIC18F2610 ( 22050 times/s ) for playing wav file from MMC card.It is really needed to disable/enable interrupts in function MMCResponse ReadSector(int32 sector, char *hova) ?

thanks
Milos
incubus22



Joined: 15 Nov 2007
Posts: 14

View user's profile Send private message

not enough RAM
PostPosted: Thu Nov 15, 2007 10:40 am     Reply with quote

Hello sir Miniman!

I'm trying to make your code run but my CCS told me that it has not enough RAM for all variables. I am trying out your example main.c where you will list all the files within the main directory and then open the first one. I used PIC18F4550 in my CCS project file.

Please help me sir!
Miniman



Joined: 30 Apr 2007
Posts: 44

View user's profile Send private message

PostPosted: Thu Nov 15, 2007 12:20 pm     Reply with quote

Milhaus wrote:

It is really needed to disable/enable interrupts in function MMCResponse ReadSector(int32 sector, char *hova) ?

Well, I do not think so. But I'm not an expert on this. Try without disable it (in both ReadSector and WriteSector) it should work just as good

incubus22 wrote:
I'm trying to make your code run but my CCS told me that it has not enough RAM for all variables. I am trying out your example main.c where you will list all the files within the main directory and then open the first one. I used PIC18F4550 in my CCS project file.

It really should work if you have not changed anything. Make sure the #define MAXFILES 1 (in the .h) file defines just 1 file.
you could also try to reduce the value "#define MAX_FILE_LIST 10" in the .c file. This sould reduce the RAM needed. Also make sure that you do not take much RAM outside the FAT lib..

/Miniman
incubus22



Joined: 15 Nov 2007
Posts: 14

View user's profile Send private message

PostPosted: Thu Nov 15, 2007 8:43 pm     Reply with quote

incubus22 wrote:
I'm trying to make your code run but my CCS told me that it has not enough RAM for all variables. I am trying out your example main.c where you will list all the files within the main directory and then open the first one. I used PIC18F4550 in my CCS project file.

It really should work if you have not changed anything. Make sure the #define MAXFILES 1 (in the .h) file defines just 1 file.
you could also try to reduce the value "#define MAX_FILE_LIST 10" in the .c file. This sould reduce the RAM needed. Also make sure that you do not take much RAM outside the FAT lib..

/Miniman[/quote]
It's still not working sir. I tried your suggestions. Still, CCS tells me it has not enough RAM for all variables. I also tried making "#define MAX_FILE_LIST 1". I also don't have any other function within my void main(void). I only copied the example (which is commented out). Here is my void main(void):
Code:

char f,filename[20],res,i,c;

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF|ADC_TAD_MUL_0);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
//Setup_Oscillator parameter not selected from Intr Oscillotar Config tab

   // TODO: USER CODE!!

                                                             
                                   
      while(TRUE)                                               
      {                                                         
         if(MMCInit() == MMC_OK)                               
         {                                                     
            printf("MMC initialized\r\n");                     
            InitFAT();                                         
            strcpy(filename,"DIR/");                           
            f = InitList(filename);                             
            if(f & MMC_ERROR)                                   
               printf("Error");                                 
           
       
            else
            {                                                   
               do                                               
               {                                               
                  res = ListFiles(f);                           
                  for(i=0;i<res;i++)                           
                  {                                             
                     printf(FileList[i].name);                 
                     if(FileList[i].isDir)                     
                        printf("\\\r\n");//a "\" with row break
                     else                                       
                        printf("\r\n");                         
                  }                                             
               } while(NextPage(f) == MMC_OK);                 
               CloseList(f); // frees the file, but the list is
            }                // still there...                 
            strcpy(filename,FileList[0].shortName);             
            f = fopen(filename,'r'); // open file for reading   
            if((f & MMC_ERROR) == 0) // No error, same as       
            {                        // if(f < MAXFILES)       
               while(fgetch(&c,f) == MMC_OK)                   
                  printf(c);                                   
               fclose(f);                                       
            }                                                   
            else if(f == MMC_NOT_FOUND)                         
               printf("1st file in list was probebly a dir");   
            else                                               
               printf("Other error\r\n");                       
            while(TRUE)                                         
               ; // Loop forever, program is finished           
         }                                                   
         else                                               
            printf("MMC init failed!\r\n");                     
         delay_ms(1000);                                       
         printf("Trying once more..\r\n");                   
      }         


I am using the internal 8MHz clock by the way. Here's my project header file.
Code:

#include <18F4450.h>
#device adc=10

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES NOPROTECT                //Code not protected from reading
#FUSES BROWNOUT                 //Reset when brownout detected
#FUSES BORV20                   //Brownout reset at 2.0V
#FUSES PUT                      //Power Up Timer
#FUSES VREGEN                   //USB voltage regulator enabled
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT                    //Program memory not write protected
#FUSES LPT1OSC                  //Timer1 configured for low-power operation

#use delay(clock=8000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
Miniman



Joined: 30 Apr 2007
Posts: 44

View user's profile Send private message

PostPosted: Fri Nov 16, 2007 8:16 am     Reply with quote

Hi
I do not know what the problem could be. I also have a PIC18F4550 and there are enough RAM for me. Sorry can't help you on this one..
incubus22



Joined: 15 Nov 2007
Posts: 14

View user's profile Send private message

PostPosted: Sat Nov 17, 2007 1:09 am     Reply with quote

Miniman wrote:
Hi
I do not know what the problem could be. I also have a PIC18F4550 and there are enough RAM for me. Sorry can't help you on this one..


Hello Miniman! I found the bug in CCS. The device file for my PIC18F4550 was incorrect. I already compiled it successfully.
However, a new problem occurs, I can't simulate it in Proteus. Have you tried to do this before? The MMC card can't be initialized. What's wrong?
Miniman



Joined: 30 Apr 2007
Posts: 44

View user's profile Send private message

PostPosted: Sat Nov 17, 2007 2:02 am     Reply with quote

Have never used Proteus, all I can say is that I have got it to work irl and that it seems like some other peoples have to. Maby the SPI mode is wrong, try to change that
incubus22



Joined: 15 Nov 2007
Posts: 14

View user's profile Send private message

PostPosted: Sat Nov 17, 2007 2:54 am     Reply with quote

Miniman wrote:
Have never used Proteus, all I can say is that I have got it to work irl and that it seems like some other peoples have to. Maby the SPI mode is wrong, try to change that


Hello again! I've managed to make the MMC initialize in real world. However, I can't append to a file. My code to append is as follows:
Code:

lcd_init();
   printf(lcd_putc, "START!!!\n");
   
   if(MMCInit() == MMC_OK)                               
   {                                                     
      printf(lcd_putc,"MMC initialized\n");                     
      InitFAT();
                           
      { 
         strcpy(gfilename,"EVENTS.LOG");     
         f = fopen(gfilename,'a'); // open EVENTS.LOG for append   
         if (f & MMC_ERROR)                                           
         {                                                             
            printf(lcd_putc,"Couldn't open file!\n");                         
            if(f == MMC_NO_CARD_INSERTED)                             
               printf(lcd_putc,"Please, insert MMC!");                         
            else if(f == MMC_MAX_FILES_REACHED)                       
               printf(lcd_putc,"ops.. =)"); 
            while(1);
         }                                                               
         else                                                         
         {                                                             
            strcpy(msg,"System started\n");   
            printf(lcd_putc,"written\n");
            fputstring(msg,f);                                         
            fclose(f);   
            while(1);
         }       
     }
   }
   else
   {
      printf(lcd_putc,"MMC init failed!\n");
      while(1);
   }

I have formatted my MMC using the cmd and the following command:
format F: /A:512 /V:mymmc /FS:FAT32

Nothing is happening. The file EVENTS.LOG does not change at all. Any clue?

The only lines printed on my LCD are:
START!!!
MMC initialized

no more else.
incubus22



Joined: 15 Nov 2007
Posts: 14

View user's profile Send private message

PostPosted: Wed Nov 21, 2007 8:23 am     Reply with quote

Hello Miniman!

I made your code run! Very Happy Thank you very much!

I have follow up questions. I am writing around 160 Kbytes of characters to each file in the MMC. I've noticed that it takes around 15 minutes. Is there a way to speed things up? Or is this at its maximum speed? I am using a 20MHz crystal on the PIC18F4550.

Also, can I use the USB capability of the PIC18F4550 while using your libraries for MMC?
Milhaus



Joined: 20 Oct 2007
Posts: 9

View user's profile Send private message

PostPosted: Wed Nov 21, 2007 8:41 am     Reply with quote

incubus22>
1. use HW SPI ( if you use SW SPI now )
2. speed up the clock to 40MHz
3. optimalize your code / do not use slow MMC/FAT32 routines from Miniman library. The problem could be in your code, not in the writing speed. I think at 40MHz is the real maximum writing speed around 0.5MB/s ( i guess )
incubus22



Joined: 15 Nov 2007
Posts: 14

View user's profile Send private message

PostPosted: Wed Nov 21, 2007 11:05 am     Reply with quote

Milhaus wrote:
incubus22>
1. use HW SPI ( if you use SW SPI now )
2. speed up the clock to 40MHz
3. optimalize your code / do not use slow MMC/FAT32 routines from Miniman library. The problem could be in your code, not in the writing speed. I think at 40MHz is the real maximum writing speed around 0.5MB/s ( i guess )


Hello Milhaus!

1. I've tried the HW SPI of Miniman and I can't get it to work. Have you done this?
2. Do you mean that I should use a 40MHz crystal?
3. Do you have an implementation aside from Miniman's? Please teach me how or send some working files on FAT32. I'm using PIC18F4550.
incubus22



Joined: 15 Nov 2007
Posts: 14

View user's profile Send private message

PostPosted: Fri Nov 23, 2007 9:26 am     Reply with quote

Hello Miniman! Very Happy

Can you help me with the hardware SPI with your FAT32 code?

Please!

I managed to make it work via the software SPI but no luck with the hardware SPI.

Please teach me how! Embarassed Crying or Very sad
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Goto page Previous  1, 2, 3, 4, 5, 6  Next
Page 2 of 6

 
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