11

How do you unit test your assembly code?

I'm working on a serial servo controller as part of a hexapod robot project and the code has got to the point where it's becoming complicated ;) Anyway, I'm used to using unit tests in my day job as a C++ server developer and so have been trying to apply the same kinds of testing to my AVR assembly code. I've worked out a way that works OK for me (see here) but I'm interested if there are any standard tools or techniques that I'm missing.

Updated: For those of you that are interested the full source to the servo controller and the unit tests are now available here.

Len Holgate
  • 1,078
  • 1
  • 10
  • 19

2 Answers2

3

I would also describe this as elegant, but would like to add the the problem, if you will forgive my intrusion.

I know there are very pricey software packages to work through situations like this, but at the company I work at we cannot afford the cost unless we are sure it does what we need.

Test Driven Development(TDD) is one of the better systems I have heard of for development, and I enjoy it, but the problems that take up my time are normally caused by complex interrupt and hardware events that many would call glitches. It seems like a minor thing to have a problem every 2 hours when the stars align, but if your phone just froze once a week,you would curse the engineers name. In our case, we have to trek into a feed lot when things really break, which, as you can imagine, I like to avoid.

I have seen very intelligent solutions for checking functionality of subsystems, which, if implemented properly, would probably save me 3 hours out of a 50 hour work week, but if there was an intelligent way to find glitch situations it would save me weeks of work looking for the "bug" that happens in the field occasionally under heavy load.

This post probably does not help a large amount, but I find bringing everything into the light makes everything easier to resolve. If there was a TDD method for finding glitch situations, I could get 10s of thousands allocated to pay for it. -Max

Kortuk
  • 13,362
  • 8
  • 60
  • 85
  • 1
    I haven't really thought about testing the interaction between the interrupt code and the non interrupt code. It's a good point. I was planning on testing my PWM generation timer interrupt code outside of an interrupting situation in a similar way to the way I'm testing my main line serial code. I guess even once I have coverage for all of that I'm still missing the interaction. I suppose that I could trigger interrupts from within the tests (it's easy with the timer interrupt but all interrupt code could be set to run on a timer interrupt within the test harness). Non trivial though. – Len Holgate Nov 13 '09 at 09:49
  • 1
    I may misunderstand you, but I try to be quite careful of non-interrupt and interrupt code interactions. I probably should be more lax in my use of atomic operations, but it has never been shown to cause harm at the level I have our optimizer. The issues arise when one interrupt delays another, a common one I help students with would be interrupt driven PWM generation. If you need an extremly precise speed, for example using a Compare module in your chip, and another interrupt is busy spending time on something else and delays you by 50uS that could be world ending. – Kortuk Nov 13 '09 at 10:45
  • 1
    In some cases you can have priority, or you can even have an interrupt disable itself and re-enable global interrupts internally, platform dependent, but the easiest path for my own development is limiting time in interrupt to things absolutely necessary and have normal code do the rest of the work. – Kortuk Nov 13 '09 at 10:47
  • 1
    My current design is explained on my blog, as is a proposed new design. Right now I have 64 channels of PWM generated by a timer interrupt and no other interrupts; serial is done by polling. This means the PWM is rock solid but the serial may have issues. My new design would use interrupts for UART as well as the timer for PWM generation and careful reenabling and blocking of interrupts to ensure glitch free PWM and glitch free UART processing... – Len Holgate Nov 13 '09 at 15:24
  • 2
    I was using the PWM as an example, the system I have had this problem on has 3 SPI interfaces with 1 of the SPI connections having 3 chips we use on it. There are 4 different port interrupts informing of change in state of external chips and a few other things going on. The problem becomes worse the more interfaces you have. – Kortuk Nov 14 '09 at 19:40
  • Understand. I haven't yet worked out how it might be possible to test such interactions. The main reason for the question was to find out if others had any better ideas :) – Len Holgate Nov 14 '09 at 22:39
  • Yes, I understood your question, and considered it an excellent one. I could not add any to the answer, so why not expand the questions space. – Kortuk Nov 15 '09 at 08:15
2

Interesting. After Christmas I was planning on looking into doing some Assembler with Pics, when I've got a bit more time I'll have a good look at your system.

One way I could see would be to script some sort of framework in a different language to create and tear down the mock objects etc, but how you would interface this to the chip/simulation would be a problem.

If it becomes too onerous though then that will outweigh the benefits of unit testing and also make you less keen to use it.

Amos
  • 2,893
  • 3
  • 26
  • 26
  • Getting it to work inside the simulator was my initial hurdle until I worked out how to break the code apart into separate files and simply "mock" out some of the labels that I'd jump to in the real code. I'll post the whole thing once I'm done. Retrofitting the unit tests to the code is taking a while but it has been well worth it. – Len Holgate Nov 12 '09 at 13:06
  • Now that I've had a proper chance to look through your methodology, I think it's quite elegant. Maybe I'll have a look at avrs after Christmas, as there seems to be a lot more community based stuff around for them than for the Pics. Any ideas what a decent Linux IDE for AVR Assembler programming might be. – Amos Nov 12 '09 at 22:50
  • 1
    There's a GNU tool chain that you can use in place of AVR Studio (which is Atmel's free toolset). It's probably possible to run that on Linux, but I haven't needed to. – Len Holgate Nov 13 '09 at 09:44
  • Just found this link to a Linux Journal article on developing for AVR under Linux: http://www.linuxjournal.com/article/7289 – Len Holgate Nov 14 '09 at 16:49
  • My main concern with that article is that it's from 2005 and so may be out of date. – Amos Nov 20 '09 at 14:46
  • I do AVR-GCC on Linux all the time. It works like a champ. (and if you've used Arduino on Linux, you've used it too) – todbot Nov 30 '09 at 18:56