Change only one byte in the SD card sector

I am implementing FAT16 on an SD card using Atmega328.

I often have to change only one or two bytes in a sector (512B area).

I know how Flash works, and that he needs to rewrite the entire sector at once, but I was wondering if there could be any special command that would make it process it herself?

The fact is that atmega has only 2k RAM, and allocating 512 only for the SD card buffer for read-modify-write is very unfavorable. There is no other way?

(PS. AFAIK atmega cannot initially have external RAM)

+6
source share
2 answers

You cannot change only a few bytes in a sector without reading / writing only 512 bytes. So you really need RAM for the buffer.

However, there are ways to pre-assign the file space or update the FAT to anyone who writes. This will even save write cycles, but can lead to data loss when the power is turned off before the FAT update (however, this may not ruin the file system if it is executed correctly).

As you state that these are cheap ArduinoNano (clones - presumably), you can just use two of them related to UART / SPI / i2C - everything is available, even 8 bit bit-bang will work.

One does what is needed, and the other only for processing SD card / FAT.

Reminds me of the Comodore 64 and its 1541 floppy drive, which also included a small processor (well, "MCU", but not one chip).

+1
source

The ELM Petit FAT file system module states:

It can be included in tiny memory-limited microcontrollers, even if the size of the RAM is less than the size of the sector.

Actually:

  • Very low RAM consumption (44 bytes + specific stack).

It is open source; so you can take a look at how he does it (or just use it as it is).

The write function has some significant limitations, for example, while it can change the file, it cannot create or resize one — which is probably the compromise necessary to prevent sector buffers.

There is a fully functional version of ELM FatFS , but this requires significantly more code space and RAM . The page also has links to the FAT32 specification and some technical notes on how SD cards work, which may be useful.

EDIT:

This is actually not particularly useful. The Petit FAT application applies only to the file level itself, it does not include any device hardware driver, and for SD / MMC there is no way out of the 512 byte read-modify-write byte cycle.

Although AVR may not support external memory with memory mapping, you can use an external serial memory device such as Cypress FRAM or nvSRAM to store sector data; although it is non-volatile, it can completely eliminate the need for SD unless a large removable media is specifically required.

+1
source

Source: https://habr.com/ru/post/988834/


All Articles