#use "default.h"

#if (BOARD_TYPE!=LittleG)
#fatal "This program only runs on the BL16xx/LittleG."
#endif

/*
	GENDMA.C

	This is a sample program that uses the /DREQ0 and /DREQ1 lines as high speed
	counters for detection of negative edges. The DMA counter will observe the
	number of pulses given by the argument of the DMA0Count() ( or DMA1Count() )
	function. By default this value is 100 pulses.

	Jumper /USER2 to /DREQ0 on H4.
	Jumper /USER3 to /DREQ1 on H4.

	Generate pulses by typing "pulse(50)" in the watch window dialog box. Repeat
	typing "pulse(arg)" until the DMA counter is finished. By default this is
	100 pulses.

	Reload the DMA counter from the watch window by typing "DMA0Count(arg0)"
	for channel 0 and "DMA1Count(arg1)" for channel 1. Try different values for
	arg0 and arg1 to make the DMA counter read different numbers of pulses
	(i.e. arg0=1000 and arg1=25.)
*/

void pulse(unsigned int count);

main()
{
unsigned count1, count0;
unsigned old1, old0;
    
    old1 = old0 = count1 = count0;
    DMA0Count(100);
    DMA1Count(100);

    while (1)
    {
        while (!_DMAFLAG0 && !_DMAFLAG1)
        {
            runwatch();
            DMASnapShot(0, &count0);
            DMASnapShot(1, &count1);
            if (old0 != count0)
            {
                printf("Counter 0 has %u counts\n", count0);
                old0 = count0;
            }
            if (old1 != count1)
            {
                printf("Counter 1 has %u counts\n", count1);
                old1 = count1;
            }
        }
        if (_DMAFLAG0)
        {
            printf("DMA0 is finished counting\n");
            _DMAFLAG0 = 0;
        }
        if (_DMAFLAG1)
        {
            printf("DMA1 is finished counting\n");
            _DMAFLAG1 = 0;
        }
    }
}

/*
	Generate negative edges.
*/

void pulse(unsigned int times)
{
    unsigned int i;
    for (i = 0; i < times; i++)
    {
        outport(USER2, 0xff);       // data does not matter since
                                    // /USER2 is a control line.
        outport(USER3, 0xff);       // data does not matter since
                                    // /USER3 is a control line.
    }
}

