CodeVision code for MMC

Go To Last Post
23 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi
I am trying to write to MMC/SD card using ATmega128 in CodeVisionAVR

I need to create a text file, write to it, and when necessary open it again and append more data

I looked at CodeVision help but it seems to have very low level functions:

DSTATUS disk_initialize (unsigned char drv);
DSTATUS disk_status (unsigned char drv);
DRESULT disk_read (unsigned char drv, unsigned char* buff, unsigned long sector, unsigned char count);
DRESULT disk_write (unsigned char drv, const unsigned char* buff, unsigned long sector, unsigned char count);
DRESULT disk_ioctl (unsigned char drv, unsigned char ctrl, void* buff);
void disk_timerproc (void);

Do you know an easier solution for creating files (FAT file system) and writing to it?

Thanks in advance

Last Edited: Mon. Feb 4, 2013 - 12:52 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Check out FatFs...

http://elm-chan.org/fsw/ff/00ind...

It provides all the high level stuff, you provide a few low-level functions.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
I looked at CodeVision help but it seems to have very low level functions:

But Codevision has the full FatFs?! Sure the disk_*() functions are the low level interface but it has all the f_*() functions at the higher level too. Suggest you read the documentation at:

http://elm-chan.org/fsw/ff/00index_e.html

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I suggest that the OP simply read the CodeVision Help files.

You configure the pins for your MMC via Project->C_Compiler->Libraries->MMC

And use the FATfs library functions in the regular way.
e.g. f_mount(), f_open(), f_write() ...

You do not need to access the functions at all. i.e. f_mount() will look after the housekeeping for different card types.

Yes, you will probably exceed the 4kB code limit of the Evaluation CV when you use library.

OTOH, writing to disk with only primitive functions will be very hard work. (and most likely use the same amount of flash as the does)

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:

You do not need to access the functions at all.


Thanks all
in sdcard.h help it says we must call disk_timerproc every 10ms
If I use FF.h, can I ignore this?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No, I think that you still need some method of calling disk_timerproc().

Just look at the example programs. Ask the Wizard to create a 10ms Timer interrupt for you.

An avr-gcc user has to positively go looking for third party libraries, example programs, documentation etc.

The CV user simply does one download and install. Everything is there without you even needing to scratch your backside.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi
I wrote this code but it's not working
Could you please help me figure out the problem

void main(void)
{
// Declare your local variables here

FATFS *fs1;
unsigned char disk_status;
unsigned int sector_size;
unsigned long int sector_count;
FIL *fs;
unsigned char fg;
FRESULT res; 

// Global enable interrupts
#asm("sei")
 
 //f_mount(0,fs) ;
 disk_status=disk_initialize(0);
 sprintf( text,"init=%2d",disk_status); //returns 0 if OK, is OK
 glcd_outtextxy(50,27,text);
 
res=f_mount(0,fs1);   //returns 0 (OK)
sprintf( text,"mount=%2d",res);
glcd_outtextxy(50,36,text);
 
 

res = f_open(fs, "0:/smms.txt", FA_CREATE_ALWAYS );  //returns 0 ;  OK
sprintf( text,"open=%2d",res);
glcd_outtextxy(50,45,text);

res=f_close(fs);       //returns 3 
sprintf( text,"close=%2d",res);
glcd_outtextxy(50,54,text);

/******************************************  WHILE   ************************************/
while (1)
    { 
    }                     
}

I want to create a file, but No file is created

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So what debug messages you see on your LCD display?

And how are you calling disk_timerproc every 10ms?

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I had in my comments:
disk_initialize(0); //returns 0
res=f_mount(0,fs1); //returns 0
res = f_open(fs, "0:/smms.txt", FA_CREATE_ALWAYS );//returns0
res=f_close(fs); //returns 3

I call disk_timerproc in a timer interrupt:

// Timer3 output compare A interrupt service routine

interrupt [TIM3_COMPA] void timer3_compa_isr(void)
{
   TCNT3H=0x00;
    TCNT3L=0x00;
   disk_timerproc();
   PORTD.7=~PORTD.7;
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
FATFS *fs1; 

You are only declaring a pointer to FATFS structure. There is no memory reserved for the struct. All that happens is that the f_mount() function fills in FATFS details at the address pointed to by *fs1. (which will be NULL)

...
/* will hold the information for logical drive 0: */
FATFS fat;
    ...
    /* mount logical drive 0: */
    if ((res = f_mount(0, &fat)) == FR_OK)
        ...

Why not simply copy one of the CV example programs?
Then modify to suit your requirements.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks
it worked

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have another problem now
I can write to a file, but I cannot append data to it and data s overwritten
This is my code

res=f_mount(0,&fs1);
res = f_open(&fs,shmdate_date,FA_OPEN_ALWAYS  | FA_WRITE);
res=f_write(&fs,display_buffer,sizeof(display_buffer)-1,&bw);
res=f_close(&fs);

I know there is a f_lseek(&fs, OFFSET) command
but I don't know if this is the best solution because I don't know what to write instead of OFFSET

Last Edited: Mon. Feb 4, 2013 - 01:00 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You are creating a FATFS object called fs1
Then accessing some unrelated object called fs.

Note that every function call returns an error status value. You should always use it.

I would guess that your use of an uninitialised fs would ensure an FR_ERROR of some description!

Oh, using the key would make you much happier when you read your code.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks
I think the use of fs and fs1 is correct because because the code is working, also all the functions return 0 (NO ERROR)
just the data is overwritten each time instead of being appended to the end of previous content
I had made the code shorter to be readable

FATFS fs1;
FIL fs; 

res=f_mount(0,&fs1);

res = f_open(&fs,shmdate_date,FA_OPEN_ALWAYS  | FA_WRITE);

res=f_write(&fs,display_buffer,sizeof(display_buffer)-1,&bw);

res=f_close(&fs);
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I think the use of fs and fs1 is correct

Perhaps but those are extremely confusing names. One is a Fat structure and one is a file structure yet the "fs" in both names gives the impression of them likely being of the same type. Why not more obvious names like:

FATFS fat;
FIL file;

res=f_mount(0,&fat);

res = f_open(&file,shmdate_date,FA_OPEN_ALWAYS  | FA_WRITE);

res=f_write(&file,display_buffer,sizeof(display_buffer)-1,&bw);

res=f_close(&file); 

If it confused David (and me) then think about how it will be for you in 9 months tmie when you come back to this code and try to remember exactly what it was doing. You'll be in the same position as David and I. Always write code with the maintainer (which could be you) in mind.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes you are right
I'm trying to help my friend that has written his code for atmega128 and doesn't know English to ask in forums
I agree that his naming is not well enough

But do you know any solution for getting file size in codeVision so he can use f_lseek(&fs,F_SIZE(&fs)) to go to end of file or any other suggestions for writing data at the end of file?

(by F_SIZE I mean something that return size of a file, it doesn't exist in codevision libraries)

Last Edited: Mon. Feb 4, 2013 - 01:24 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am well aware that a machine could not care less about what names you use or whether you format your source code prettily.

I still reckon that it makes a MASSIVE difference to a human. For example, don't you find it confusing to use 'fs' and 'fs1' for two totally different types of object?

Incidentally, I was whingeing about the key and not the
key. I like to see attractive code that fits on a page. Blank lines are useful to separate blocks. Probably counter-productive to separate every line.

If you want to append to an existing file, you either open() the file for appending or you simply seek to the end.

David.

Last Edited: Mon. Feb 4, 2013 - 01:24 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

from the head...
there is a good example in the example code made by chan.
your friend should first go and download that and check out what the different functions do and how the can be set and report back.
If he first starts playing with the demo then he change things to see what happens.
It is the best way to learn how FatFS works.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

In real (POSIX) operating systems the API to get a file size is fstat(). I see Chan has f_stat() and it fills a FILINFO where that is defined as:

http://elm-chan.org/fsw/ff/en/sf...

typedef struct {
    DWORD fsize;      /* File size */
    WORD  fdate;      /* Last modified date */
    WORD  ftime;      /* Last modified time */
    BYTE  fattrib;    /* Attribute */
    TCHAR fname[13];  /* Short file name (8.3 format) */
#if _USE_LFN
    TCHAR* lfname;    /* Pointer to the LFN buffer */
    int   lfsize;     /* Size of the LFN buffer in unit of TCHAR */
#endif
} FILINFO;

SoI'd be surprised if "fsize" in that is not what you are looking for.

Personally I think it pays huge dividends to learn about filing on a PC using the POSIX functions (fopen(), fread(), fwrite(), fputc(), fgetc(), fstat(), etc.) then bring what you have learnt there to FatFs. It's not 100% compatible but the differences are very very small. (which is nice).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

meslomp wrote:

there is a good example in the example code made by chan.

There is also an example of appending in the Codevision example files.

Quite why the poster is not looking at the 'official' examples is beyond me.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

From the CV Help entry:

Quote:

FRESULT f_lseek(FIL* fp, unsigned long ofs)

moves the file read/write pointer of a file previously opened with f_open.
In write-mode, this function can be also used to extend the file size, by moving the file read/write pointer past the end of the file. On success the value of the fptr member of the FIL structure, pointed by fp, must be checked to see if the file read/write pointer effectively advanced to the correct position and the drive didn't get full.
In read-mode, trying to advance the file read/write pointer past the end, will limit its position to the end of the file. In this case the fptr member of the FIL structure, pointed by fp, will hold the size of the file.

Parameters:

fp points to the FIL type structure that contains the file parameters. This structure must have been previously initialized by calling the f_open function.
ofs represents the byte position where the file read/write pointer must be placed starting with the begining of the file.

Return values:

FR_OK - success.
FR_NOT_READY - no disk access was possible due to missing media or other reason.
FR_DISK_ERR - the function failed because of a physical disk access function failure.
FR_INT_ERR - the function failed due to a wrong FAT structure or an internal error.
FR_INVALID_OBJECT - the file was not opened with f_open.


Note that FATfs is used over the whole world. And not just by AVR users. So your friend can probably find documentation in her own language. Or use google-translate.

Regarding variable names. We often get code posted here in German or Spanish. I may not understand what the names mean, but at least I can notice very different spelling.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

But do you know any solution for getting file size in codeVision

Here is a fragment from one of my apps:

					// Put file pointer at end of file
					card_retval = f_lseek (&card_file_log, card_file_log.fsize);
					// Decide on actions to take if a failure
					if (card_retval != FR_OK)
						{
						}

You'll have to check the documentation and/or the code to see when .fsize gets updated. I don't remember.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

You'll have to check the documentation

As I said above fsize is an element of FILINFO and (like POSIX) you can get it updated with a call to f_stat(). Also available from f_readdir().