[{"element_type":"text","content":"\n        \u003cp\u003eBetter SD write performance is probably a common goal. My project uses the Adafruit SD Micro SDIO breakout, the Adafruit OV5640 Camera breakout and the Unexpected Maker ESP32S3 feather in a compact stack (with a custom PCB to connect them) that fits in a GoPro waterproof case. I want GoPro-like video recording but with BLE time synchronization and start/stop functions with multiple cameras. It's like a GoPro but it's not, it's a NoPro!\u003c/p\u003e\n\u003cp\u003eI'm programming with the Arduino IDE and using the SD_MMC library. The ESP32S3 has an SDIO interface with programmable pins, which the library supports. But for the longest time, I could only get sustainable write speeds of 150-200 kilobytes per second. I'm using a \u003cspan\u003eSandisk Extreme V30 32GB which should get 30MB/s. For the frame rates and resolution I want, I need about 3MB/s.\u003c/span\u003e\u003c/p\u003e\n\u003cp\u003e\u003cspan\u003e\u003c/span\u003e\u003cspan\u003eI initially played with the file system buffer size but never got consistent results. I might occasionally get a few frames written at target speeds but never a whole video. So I set my code aside and started from scratch to explore how to get better speed.\u003c/span\u003e\u003c/p\u003e\n\u003cp\u003e\u003cspan\u003e\u003c/span\u003e\u003cspan\u003eThere definitely seemed to be a dependence on file buffer size as well as on the size of data being written. So I tried lots of combinations and got the same results - usually about 150-200 kB/s but occasionally over 3MB/s.\u003c/span\u003e\u003c/p\u003e\n      ","metadata":{}},{"element_type":"user_image","content":"https://cdn-learn.adafruit.com/user_assets/assets/000/000/674/original/writerand.png?1706876886","metadata":{"caption":""}},{"element_type":"text","content":"\n  \n  \n  \n        \u003cp\u003e\u003cspan\u003eBut I had noticed a pattern of good performance when the write lengths were certain powers of 2 so I tried multiples of 512 bytes and got much better speeds. With a file buffer size of 4096 and data sizes over 75kB I could get consistent speed over 3MB/s, just what I need.\u0026nbsp; 4096 also gets consistent small writes at over 1MB/s.\u0026nbsp; Buffer sizes of 8192 and greater actually worked worse.\u003c/span\u003e\u003cspan\u003e\u003c/span\u003e\u003c/p\u003e\n      \n\n\n","metadata":{}},{"element_type":"user_image","content":"https://cdn-learn.adafruit.com/user_assets/assets/000/000/675/original/write512.png?1706876970","metadata":{"caption":""}},{"element_type":"text","content":"\n  \n        \u003cp\u003e\u003cspan\u003eI'm saving frames as JPEGs in an AVI file. I can pad extra bytes at the end of each JPEG to get to 512 byte multiples and always start a new frame on a 512 byte boundary. Problem solved!\u003c/span\u003e\u003c/p\u003e\n\u003cp\u003e\u003cspan\u003e\u003c/span\u003e\u003cspan\u003eAfter I did this experiment, I found a discussion at \u003c/span\u003ehttps://github.com/ZuluSCSI/ZuluSCSI-firmware/issues/269 which included the author of the SdFat library. He can get blazing fast writes on other microcontrollers (over 40MB/s in one case) but it seems to require tuning for each and I don't think he's done EPS32 yet.\u003c/p\u003e\n\u003cp\u003eThe github discussion includes an interesting explanation of modern SD operation but I'm not sure why 4096 buffer size and 512-padded writes works better.\u0026nbsp; Modern SD cards have much bigger block sizes.\u0026nbsp; I would expect the library to hide these details and, some day, it might.\u0026nbsp; For now, I'm happy.\u003c/p\u003e\n      \n","metadata":{}}]