[ECOS] Unaligned flash writes

Simon Kallweit simon.kallweit@intefo.ch
Tue Apr 7 09:10:00 GMT 2009


In my current project I have a few code spots which write to NOR flash. 
Some of them may write unaligned data (unaligned base address and/or 
length of buffer). Most flash drivers don't handle unaligned writes. So 
I currently have a little helper function to align the flash writes. I 
wonder if it would make sense to add something like that into the flash API.

I attached the code fragment I currently use. When the memory buffer is 
kept static, we would have to add a mutex to protect it.

What do you think about this?


static cyg_uint8 tmp[32] 

int cyg_flash_program_unaligned(cyg_flashaddr_t addr, const void *buf,
                                size_t len, cyg_flashaddr_t *err_address)
    int ret = CYG_FLASH_ERR_OK;
    int ofs;
    int chunk = 0;
    const cyg_uint8 *src = buf;
    if (len <= 0)
        goto out;

    // Align flash write address
    ofs = addr & (CYGNUM_APP_TRIPBOOT_FLASH_ALIGN - 1);
    addr -= ofs;
    while (chunk < ofs)
        tmp[chunk++] = 0xff;
    // Write aligned chunks
    while (len--) {
        tmp[chunk++] = *src++;
        if (chunk >= sizeof(tmp)) {
            ret = cyg_flash_program(addr, tmp, chunk, err_address);
            if (ret != CYG_FLASH_ERR_OK)
                goto out;
            addr += chunk;
            chunk = 0;
    // Write remainder
    if (chunk > 0) {
        while ((chunk & (CYGNUM_APP_TRIPBOOT_FLASH_ALIGN - 1)) != 0)
            tmp[chunk++] = 0xff;
        ret = cyg_flash_program(addr, tmp, chunk, err_address);

    return ret;

Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

More information about the Ecos-discuss mailing list