Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
20 years ago

How to use Compact Flash in eCos?

Hi. I'm having problems accessing the compact flash card in eCos. I tried a program that mount the filesystem to / and write a file to it. But the program hangs when I try to open a file. I have run the cf_ide_test that comes with the compact flash ip from this forum. The card is recognised properly. I have also tried to run the fileio1.c test program that is in eCos. I get a -1 error when it tries to mount the FAT fs that's on the CF card. I can read and write to and from it in Windows. It's a brand new card, Kingston Elite Pro High Speed 512 MB.

I have read that I need to remove and insert the card again in some cases. I have tried this, but it does not work. Does anyone have some tip to what I might do?

Ole

17 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    It doesn't look like the elf file is too big, dec is 301176. But I wonder what kind of performance you have gotten out of writing to the compact flash.

    What is the function that in theory should be fastest? And is it faster to write for example 100 at a time than 1 byte? Is it so much overhead on the fprintf, fwrite and similar functions that it's not possible to even get close to 1 MB/s writing to the compact flash? I need to get some samples from a ADC via SPI and then write it to disk. 32000 samples from the ADC takes about 0.4 s and these are 16 bit samples. When I combine taking the samples and writing to disk, it uses about 1.4 seconds. That is 1 second to write 64000 byte to the compact flash. That's a little slow I think. I use the Nios II/f processor with hardware multiply in logic elements on an Cyclone chip at 50 MHz.

    Ole
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    The compact flash driver uses polled IDE accesses rather than the faster DMA modes (since these are unsupported in the hardware). This means that you won't be able to achieve the advertised fastest access rates for a given compact flash cards, however I have seen rates in the order of 600 Kbytes/s for a similar system to the one you're describing.

    Obvious things to watch out for are:

    1. turn on optimisisation in the compiler (this is on by default).

    2. not being starved out by other activities, i.e. higher priority threads, ISR's and DSR's. In particular make sure that your system clock tick rate is not too high.

    There is an overhead to each call to an access function, so the bigger the blocks you transfer - the better.

    Best performance is obtained using the lowest level function that you can get away with, i.e. it is better to use read() rather than fprintf().
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Is it possible for you to disclose how that 600 kB/s was achieved? What kind of packages was used (if any relevant but the standard ones), what kind of function was used and how large blocks was transfered? Was the data written to disk in a seperate thread?

    When you say read(), you mean write()? I didn't get it to work, but then I've never used it before.

    Ole
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    About the DMA. DMA is not supported in the eCos port? Will it be supported? I will probably need to make a prototype of this project later and then I can connect to the Compact Flash via DMA. Do you know what kind of performance I can get then? Maybe 2 MB/s to take a wild guess?

    And do one have to make very large changes to the eCos port to support DMA? Not so large on the Compact Flash driver I believe if DMA was supported by the eCos port. I might get a couple of weeks working full time on this later this summer if that would be a priority for my employer.

    Ole
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Sorry, my previously hasty post may have misled you. The transfer rates that we measured were for read access not write. Write may well be slower (because of the nature of flash), but I have no data points there.

    In our case the compact flash accesses were run in a seperate thread and data was read out in blocks of 4K using fread(). The packages used to manage the compact flash/FAT file system were those listed in the Nios II eCos documentation.

    Currently there are no plans to provide generic DMA drivers for eCos. However if a community minded person was willing to submit a driver, I&#39;d be happy to include it in the next release http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/smile.gif

    Modifying the compact flash driver to use DMA should be a relatively straightforward task. The altera_avalon_dma component is not too difficult to drive. You can find details of the register interface etc. in the Nios II processor handbook.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Ok. Then I believe DMA might be the only answer for now. Because what I would have to do with this setup would go something like this: I have to read data into a locked array in one thread and then unlock it, copy it and then write it to disk in another thread. The original array would be filled up again right after it was copied. I&#39;m not that knowledgeable about how many cycles taskswitching and arraycopying takes, but I&#39;m afraid that I wouldn&#39;t gain anything by doing it that way. What I&#39;m doing now is filling up a smaller array, about 200 bytes and then writing it to disk. I have tried with larger arrays, but it was slower. So I have to settle for this until a DMA might be used.

    Ole
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    I tried write() again today, and it works as fast as it needed to. It seems to be about 2-4 times faster than fwrite. So now my application&#39;s fast enough.

    Ole