/*
	MGPIOM0.C

	BL14xx/MicroG or BL15xx/MicroG2

	Write parallel data.
	
	PIOA is set to output mode 0 for high speed parallel data transfer to
	another device. When a byte is send out of PIODA, ARDY1 goes high to
	alert the peripheral device that data is ready on the PIOA port. A
	positive pulse to ASTB1 generates an interrupt for PIODA and the ARDY1
	line is also cleared.

	PIOB bit 7 is used to monitor ARDY1.
	PIOB bit 6 is used to strobe ASTB1.

	Jumper PB7 (H3-13) to ARDY1 (H3-11).
   Jumper PB6 (H3-14) to ASTB1 (H3-12).
   
   The interrupt service routine can then send additional data as directed.
   Use function loaddata(char *buf, int count) to load the pointer to the
   start of data and the number of bytes to send.
*/

char dataout;
char *piobuf;
int datacount;
int datasndcnt;

char test[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
					0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA,
					0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA,
					0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA,
					0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA,
					0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA,
					0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA,
					0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA,
					0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA,
					0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA,
					0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA,
					0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA,
					0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA,
					0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA,
					0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA,
					0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA,
					0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA};

void strobe();
void loaddata(char *, int count);

main()
{
	resPIOCB(0x40);					//	PIOB bit 6 is set as output
	resPIODB(0x40);					// PIOB bit 6 is set to low
	setPIOCB(0x80);					// PIOB bit 7 is set as input
	
   loaddata(test, 100);				// prepare to send 100 bytes of data
   while (1)
   {
   	hitwd();
      runwatch();
      if(inport(PIODB) & 0x80)
      {
         printf("%d, dataout >> %x\n",datasndcnt, dataout);  	// demo that data is advancing
         strobe();
      }
   }
}

// Generate a positive pulse on PIOB bit 6 to talk to PIOA ASTB1.
void strobe()
{
   setPIODB(0x40);			// set PIODB bit 6
   resPIODB(0x40);			// reset PIODB bit 6
}


// Prepare data for transfer out of PIODA. First data byte is also
// send out.
void loaddata(char *buf, int count)
{
	piobuf = buf+1;
   datacount = count-1;
	dataout = *(buf);
	outport(PIOCA, 0x0f);         // PIOA in mode0
   outport(PIOCA, PIOA_VEC);     // set vector to PIOA
   outport(PIOCA,0x87);          // interrupts on, d7 = 1, (0) off
   outport(ITC, inport(ITC) | 0x01);          	// enable INT0
	outport(PIODA, dataout);
	datasndcnt = 1;
}

#INT_VEC PIOA_VEC myint

interrupt reti myint()
{
   if(datacount--)
   {
   	dataout = *(piobuf++);
      outport( PIODA, dataout);		// send the next byte
		datasndcnt++;
   }
   else
   {
		outport(PIOCA, 0x0f);         // PIOA in mode0
   	outport(PIOCA,0x07);          // interrupts on, d7 = 1, (0) off
   	outport(ITC, inport(ITC) & 0xfe);  	// disable INT0
   }
}
