[En-Nut-Discussion] How to use AT45 DataFlash with webserver

Ole Reinhardt ole.reinhardt at embedded-it.de
Tue Feb 21 18:17:08 CET 2017


Hi Thiago,

sorry for the late reply. My mail client failed to sort your message
into the right folder :(

There are two drivers with different API.

spi_blkio_at45d.c:

This one provides the devSpiBlkAt45d0

The driver uses the NutBlockDevice API, where you can access the flash
as other block devices as well using a file system driver.

For example you could use it together with the "rawfs" driver to access
the complete flash as one large file.

Unfortunately I do not have ready to use code available as I personally
always used the second api...

spi_at45dib.c:

This one provides flashAt25df0.

This driver uses the NUTSERIALFLASH API, that is used together with the
uflash filesystem. The uflashfs is a simple journaling flash file system
with simple wear levelling. But due to implementation details of the
uflashfs, it needs a flash API that is different to the blockdevice API.
That's the reason for the two flash device drivers.

A completely erased flash is equivalent to an empty uflashfs. So no
special "formatting" of the device is needed.

The uflashfs itself can be accessed like a "normal" filesystem. You can
create files and directories and access them randomly.

See also the hints on bottom of the following page:

http://www.ethernut.de/api/uflashfs_8c.html


To initialise the flash and mount the volume:

if (UFlashAttach(&devUFlash0, &flashAt45dib0, &spiBus0At91)) {
    puts("Attaching serial flash failed");
}
if (NutRegisterDevice(&devUFlash0, 0, 0)) {
    puts("Mounting UFLASH0 failed");
} else {
    puts("Mounting UFLASH0: OK!");
}


To unmount it again:

UFlashDetach(&devUFlash0);


To "format" a filesystem (erase the flash)

UFlashFormat(&devUFlash0, &flashAt45dib0, &spiBus0At91);




A simple function that accesses a file on the uflash filesystem:

uint32_t selftest_dataflash(FILE *uart0)
{
    uint32_t error = ERROR_AT45;
    FILE    *file;
    char     input[64];

    file = fopen("UFLASH0:/selftest.txt", "w");
    if (file == NULL) {
        fprintf(uart0, "Dataflash: Opening file for writing failed!!!\r\n");
        goto out;
    }

    if (fputs("Hello world\r\n", file) == EOF) {
        fprintf(uart0, "Dataflash: Writing file failed!!!\r\n");
        fclose(file);
        goto out;
    }

    if (fclose(file) != 0) {
        goto out;
    }

    file = fopen("UFLASH0:/selftest.txt", "r");
    if (file == NULL) {
        fprintf(uart0, "Dataflash: Re-Opening file for reading failed:
%s!!!\r\n", strerror(errno));
        goto out;
    }

    if (fgets(input, sizeof(input), file) != NULL) {
        if (strcmp(input, "Hello world\r\n") != 0) {
            fprintf(uart0, "Dataflash: File contents does not
match!!!\r\n");
            fclose(file);
            goto out;
        }
    } else {
        fprintf(uart0, "Dataflash: Reading file failed!!!\r\n");
        fclose(file);
        goto out;
    }
    if (fclose(file) != 0) {
        fprintf(uart0, "Dataflash: Closing file failed!!!\r\n");
        goto out;
    }
    error = 0;

    fprintf(uart0, "Dataflash: File test: Success...\r\n");
out:

    return error;
}


I hope this helps a little. The code is cut&past from different projects.

Best regards,

Ole



Am 14.02.2017 um 21:00 schrieb Thiago A. Corrêa:
> Hi Ole,
> 
>     Thanks for your reply, I was attempting to use a bit differently,
> with devSpiBlkAt45d0. Which is recommended? Using UFlash0 like your
> example how do you read/write the memory? (assuming fopen)
> 
> Kind Regards,
>     Thiago A. Correa
> 
> 
> On Mon, Feb 13, 2017 at 3:03 PM, Ole Reinhardt
> <ole.reinhardt at embedded-it.de> wrote:
>> Hi thiago,
>>
>> For an at25 I have a short example:
>>
>>     if (UFlashAttach(&devUFlash0, &flashAt25df0, &spiBus0Gpio)) {
>>         LogMsg(LOG_STARTUP, "Attaching serial flash failed\n");
>>     } else {
>>         LogMsg(LOG_STARTUP, "Mount serial flash.\n"); fflush(stdout);
>>         if (NutRegisterDevice(&devUFlash0, 0, 0)) {
>>             LogMsg(LOG_STARTUP, "Mounting UFLASH0: failed\n");
>>         } else {
>>             LogMsg(LOG_STARTUP, "Mounting UFLASH0: OK!\n");
>>         }
>>     }
>>
>>
>> and for at45:
>>
>>     if (UFlashAttach(&devUFlash0, &flashAt45dib0, &spiBus0At91)) {
>>         puts("Attaching serial flash failed");
>>     }
>>     if (NutRegisterDevice(&devUFlash0, 0, 0)) {
>>         puts("Mounting UFLASH0 failed");
>>     } else {
>>         puts("Mounting UFLASH0: OK!");
>>     }
>>
>>
>>
>> It's more or less the same :)
>>
>> Best regards,
>>
>> Ole Reinhardt
>>
>>
>>
>>
>>
>> Am 13.02.2017 um 17:38 schrieb Thiago A. Corrêa:
>>> Hi,
>>>
>>>
>>>      Michael's website isn't available anymore. Does anyone has the
>>> xflash example or other examples using Nut/OS AT45 dataflash?
>>>
>>> Kind Regards,
>>>     Thiago A. Correa
>>>
>>>
>>>
>>> On Fri, May 13, 2011 at 1:59 PM, Michael Fischer <fischermi at t-online.de> wrote:
>>>> Hello Oliver,
>>>>
>>>> take a look here:
>>>> http://www.usbdip.de/en/xflash/index.html
>>>>
>>>> It is an old example, perhaps you get it working on
>>>> new NutOS version. But you have an idea how to solve
>>>> your problem. Later you can upload the image with
>>>> TFTP like:
>>>>
>>>> tftp -i <ip-address> PUT xflash.bin
>>>>
>>>> Best regards,
>>>> Michael
>>>> _______________________________________________
>>>> http://lists.egnite.de/mailman/listinfo/en-nut-discussion
>>> _______________________________________________
>>> http://lists.egnite.de/mailman/listinfo/en-nut-discussion
>>>
>>
>> --
>> Embedded-IT
>> Alter Weg 3
>> 57223 Kreuztal
>> http://www.embedded-it.de
>>
>> Tel.: +49-177-7420433
>> _______________________________________________
>> http://lists.egnite.de/mailman/listinfo/en-nut-discussion
> _______________________________________________
> http://lists.egnite.de/mailman/listinfo/en-nut-discussion
> 

-- 
Embedded-IT
Alter Weg 3
57223 Kreuztal
http://www.embedded-it.de

Tel.: +49-177-7420433


More information about the En-Nut-Discussion mailing list