8
\$\begingroup\$

I want to know if bare metal code, especially things like device/peripheral initialization code has any testing methods since there is little to nothing that can go wrong when writing to registers (once you know that all the addresses are mapped correctly). Also this kind of code usually has very few branches/paths when the device is being configured for one single function only, so what kinds of testing would be necessary or applicable here?

\$\endgroup\$
11
\$\begingroup\$

The first thing I verify on a new board, whether it is using an internal oscillator or an external crystal, is that I have the clock frequency set up correctly. This is important because many of the peripherals, such as UART, SPI, I2C and timers depend on it.

The way I verify it is to write a program with a short loop, either in assembly language where I can count the cycles manually, or C as long as you can get a disassembly listing and do the same thing -- and turn an LED on and off. I set up a loop so it executes once a second. I run the code, and check that the LED blinks 60 times in a minute.

As far as peripherals go, the best way to check them is to use an oscilloscope if you have one, and look at the RX line for UART, the CLK, MOSI, and chip select lines for SPI, and the SDA and SCL lines for I2C, and check that the lines are toggling and the timing looks correct.

If you don't have an oscilloscope, you can put LEDs on these lines, and then enable or disable the peripherals, When disabled, most of the lines will be low (LED off), but some will be high, like the RX lead of the UART (LED on). When the peripheral is enabled, most the LEDs should dim, since the lines will be toggling. By running in a loop (disabled/enabled) it is easier to see the difference between on or dim.

For the UART, you can connect the TX line to the RX line as a loop around. You can also connect then to a UART to USB cable, and on the PC real a terminal a program like RealTerm. Besides testing out the interface, this will come in handy for other debugging later.

For other pieces of code, I use multiple LEDs as necessary to show that various paths in the code are being executed. If you have the UART working and connected to a PC, you can sprinkle your code with calls to a subroutine to output a message to show what points the program has reached (or use printf if you have the standard C libraries available). But as Vladimir Cravero points out in a comment below, this can slow your code down some (at 115,200 baud, not too much, since one character time is < 10 µs). But in ISRs and other time critical code, just use LEDs.

As Al Bundy points out in a comment below, in-circuit debuggers can be useful also, particularly if one can set multiple breakpoints, and even more useful if you can breakpoint on a memory location being changed. Not all debuggers have that feature.

However I don't use debuggers a lot unless I have to, for example to look at bits in a peripheral register; or to track down a bug which I can't find by inspection; or to rudimentary code coverage analysis. But in general I like to run programs at their "normal" speed since a lot of issues will usually show up which may not when the program is single-stepped. Most of my programs use interrupts a lot, which interferes with using a debugger.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ You said pretty much everything, I'd just add that usually sprinkling your code with fprintf will slow it down a lot, it's a thing you should use if necessary and if you know what you're doing. I've seen some questions (and had some problems) about an fprintf in an ISR or such. \$\endgroup\$ – Vladimir Cravero May 6 '15 at 7:47
  • \$\begingroup\$ @VladimirCravero Agree -- that's why like using LEDs. \$\endgroup\$ – tcrosley May 6 '15 at 7:58
  • \$\begingroup\$ Thanks for the answer. Any pointers on what tools/setup you would use for code coverage in case you have only a simulator but not the actual hardware? \$\endgroup\$ – caveman May 6 '15 at 8:31
  • \$\begingroup\$ What about debugguer? Isn't it worth to mention ? \$\endgroup\$ – Al Bundy May 6 '15 at 8:37
  • 2
    \$\begingroup\$ @AlBundy I don't use debuggers a lot unless I have to, for example to look at bits in a peripheral register; or to track down a bug which I can't find by inspection; or to perform rudimentary code coverage analysis like I mentioned in my comment to the OP. But in general I like to run programs at their "normal" speed since a lot of issues will usually show up which would not when the program is single-stepped. Most of my programs use interrupts a lot, which interferes with using a debugger. I'll add some of these comments into my answer. \$\endgroup\$ – tcrosley May 6 '15 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.