SD-Card sporadic write access

Hello,

I'm working on a project on a Nano (328) board including lcd, sd-card, incremental encoder and one-wire-bus which should do some control stuff and log some values. In general everything works but a I've found out that my log file is only written sporadic.

I open the file before every logging and close it after it to be able to write additional files later on. The log is executed every second but the log file timestamps differs 2 seconds. If I uncomment some lcd access before logging the logging fails completely - except for the file access in the setup-section.

This brought me to this
post which suggest to reduce the memory usage.

I've optimized my free memory from 524 bytes to 750 bytes by introducing the F() makro to my string-prints. Now even the write access in the init fails. I've rechecked the hardware with an older software - still works.

The file read in the setup, which reads some configs, works always.

So what can cause such problems that the write access fails on minor code modifications or every second time?

When you open a file there is a dynamic memory allocation of a 512 byte buffer. It sounds like you're ending up with too little free memory.

I've checked the memory by this function. Just before opening the file and printed it to the lcd. Regarding to this it should be 546 bytes. The File variable is defined global and shouldn't need any additional space.

How much free memory should be sufficient for stable operation?

Try your memory check when the file is open

I tried this with the older version where the file open every second time successfully. In this case the free RAM is 277 bytes after opening the file successfully and 308 bytes before opening the file. So opening the file needs only 31 bytes of RAM.

Regarding to the facts that:

  • Reducing the RAM usage made the behavior worse.
  • Disabling the lcd.print command just before opening the file made the opening unsuccessful.

I can't believe that reducing the memory usage further will help me. Could you imagine any other reasons for e.g. :

  • timing of the SPI
  • anything in some sort of cache
  • general timing problems: is there any kind of background process involved?

Kind regards Axel

After I've removed everything except for the sd access from the code the problem was still existent.

But if I changed the file name from the variable "char log_file_name[8];" to an constant string the log is successfully every time.

The chararray is written only once during setup() to prevent overwriting an old log file - it should by constant during the loop() function. Is there any explanation for this behavior or did the error disappears randomly?

Kind regards Axel

Time to post your code I think

The project is splitted into different tabs, so I zipped the whole project folder.

I think the most interesting part ist in the main tab line 154 (setup) and 276 (loop) which leads to the functions in the "brau_log" tab.

brew-control_v11.zip (6.64 KB)