There’s a joke here somewhere, and I think it’s the code

I’m working on systems software for a new ARM Cortex-M3 board, and right now, I’m trying to get the SDRAM interface working. I’ve never done SDRAM interfacing before, and it’s pretty complex, compared to most processor subsystems I’ve worked with before. There’s a lot of detail you have to handle. The library that came with the compiler includes initialization routines for most, if not all, of the processor subsystems, and the external memory controller (EMC) is one of them.

I’ve taken the configuration for an existing board (one that I don’t have) as my starting point, and modified it for my hardware. It hasn’t worked.

I’ve starting taking a “deeper dive” into the code, and I have a lot less trust in it than before. I’m not certain the configuration for the existing board actually works, now that I’ve looked at things in more detail. Part of the problem is that some comments are inconsistent with the code, and others are just plain wrong.

For inconsistency, there are three places where the comment says that a delay is needed. They’re implemented with busy-wait loops, with a comment that it would be better to use a system timer. For a 100 microsecond delay, an empty loop is executed 1000 times. For the first 200 microsecond delay, an empty loop is executed 1000 times. For the second 200 microsecond delay, an empty loop is executed 80 times. I’ve got some calculations ahead of me, it seems.

For being out-and-out wrong, another section of the code has a comment that if the external device has a 32-bit bus, use a burst length of 4. If the external device has a 16-bit bus, use a burst length of 2. According to the processor reference manual, the EMC will use a burst length of 8 for a device with a 16-bit bus (which my hardware has).

Finally, some of the values in the configuration structure are populated using predefined constants combined using shifts and ORs. When I work through two of the key values manually based on the predefined constants I’m combining, I come up with values of 0x27 and 0x1380. When I look at the structure in memory, it contains 0x23 and 0x280. I haven’t figured out yet how that’s happening.

I’ve only just found these problems; I haven’t determined how they’re occurring. I just needed to rant about it.

Comments are closed.