Pulse Generator

This program is a simple pulse generator. It uses an output compare timer to generate sequences of pulses of various periods.

The OC4 compare is set to generate an interrupt and toggle the PA4/OC4 output pin. In the interrupt handler, a new compare value is set according to a static table which represents the pattern to generate. The compare value is always computed from the previous compare value to ensure there is no drift due to the program itself. Consider the following chart:

     ______          ___________         ___
PA4 |      |        |           |       |
    +------+--------+-----------+-------+---> time
      ^ ^   ^ ^
      |_|   |_| Interrupts 
    <->    <>
     Interrupt latency

The PA4 pin is changed when the compare register OC4 matches the free running counter. This is changed in hardware and thus is synchronous with the free running counter. The interrupt is handled immediately but there will be a delay to finish the current instruction (assuming that interrupts are enabled). The next compare value is set by adding a constant to the previous value, thus providing an exact and reproducible pulse.

The interrupt latency is the time between when it is raised by the hardware and when it is first handled by the processor (ie, when it starts pushing the interrupt frame). The interrupt latency can be checked in the gdb simulator by using the command:

(gdb) sim info

and the output looks like:

N  Interrupt     Cycle Taken         Latency   Delta between interrupts
 0 OUT4                18715               3
 1 OUT4                17713               1      1002 (501.0 us)
 2 OUT4                17514               2       199 (99.5 us)
 3 OUT4                 7513               1     10001 (5.0 ms)
 4 OUT4                 5513               1      2000 (1.0 ms)
 5 OUT4                 3513               1      2000 (1.0 ms)
 6 OUT4                 2514               2       999 (499.5 us)
 7 OUT4                 1515               3       999 (499.5 us)
 8 OUT4                  516               4       999 (499.5 us)

If you connect an oscilloscope on PA4 you should see the pulses with the timing indicated in `cycle_table'.

Source file: pulse.c