Dear forum members,

I have busted my butt while working on my CAN logger project. My main task is to capture all messages transmitted over CAN and not to lose one. This is crucial. More crucial is the need to save logged CAN frames into an SD card. I have tried few techniques of fulfilling this task, but this is the point where the lack of programming experience hits hard. The main problem is that I loose many CAN frames after a while of logging. I don't know how to implement writing data to SD card by 512 byte blocks. Could this increase speed? And what happens with CAN bus monitoring during data transferring to SD? Should I use multitasking here?
I would appreciate if my humble code would be overviewed by someone more experienced than I am.
Source code attached.