Background:
I am developing a largish project using an Atmel AVR ATmega2560. This project contains a lot of hardware-based functions (seven SPI devices, two I²C, two RS-485 Modbus ports, and lots of analogue and digital I/O). I have developed "drivers" for all of these devices which provide the main application loop with an interface to access the required data.
The project I am developing will eventually have to meet SIL standards.
I would like to be able to test the code and provide a good level of code coverage. However, I am unable to find any information to get me started on how such a testing framework should be set up.
The idea is that I can have a suite of automated tests which will allow future bug fixes and feature additions to be tested to see if they break the code. The thing is I don't understand is how the code can be tested on chip.
Do I require hardware to monitor the I/O on the device and emulate externally-connected devices?
This is a very good question and is a common concern for embedded developers. Unfortunately, most embedded developers aren't as concerned as you are and only test the code on real hardware. But as another answer pointed out, this can basically test just the nominal functionality of the code and not the corner/error cases.
There isn't any single and simple solution to this problem. Some guidelines and techniques exist, however, to do a relatively good job.
First, separate your code into layers. One layer should be "hardware agnostic", i.e., function calls. Do not ask the user to write into hardware registers directly. The other (lower) layer deals with the hardware. This layer can be "mocked" in order to test the higher level. The lower level can not be really tested without the hardware, but it's not going to change often and needs deep hardware integration, so it's not an issue.
A "test harness" will be all your high-level hardware-agnostic code with a "fake" lower level specifically for testing. This can simulate the hardware devices for correct and incorrect functionality and thus allow you to run automated tests on the PC.