/*
	BAREZNM.C
	
	Bare znet master controller program.
	See barezn1.c for usage notes.
*/

#define ZNMASTER 1		//	I am the master
#define RUNKERNEL 1		//	need background task
#use eziolp31.lib
#use vdriver.lib
#use aasczn.lib
//#use eziopk23.lib		// needed only if you are using a pk23xx controller.
#use srtk.lib

#funcchain _srtk_hightask znPollMaster

#define BUFSIZ 0x100

main() {
char InFrom2[BUFSIZ], OutTo2[BUFSIZ];
CHANNEL Slave2;
char locBuf[30];
char *ptr;
int cnt, moved;
int rxCnt;
char ch;
unsigned long pakCnt;

	rtcInit();
	VdInit();
	_GLOBAL_INIT();
	znInit(ZN1200*16,0,NULL,0);
//	znSetMap(1,ZNMUSTPOLL);
	znSetMap(2,ZNMUSTPOLL);
	Slave2 = aascOpen(DEV_ZNET,0,ZN1200*16|ZNSLAVE*2,NULL);
	aascSetReadBuf(Slave2,InFrom2,sizeof(InFrom2));
	aascSetWriteBuf(Slave2,OutTo2,sizeof(OutTo2));
	aascTxSwitch(Slave2,1);
	aascRxSwitch(Slave2,1);
	aascFlush(Slave2);
	pakCnt = 0;

	init_srtkernel();
	while (1) {
		hitwd();

		costate {
			waitfor(rxCnt = aascReadBlk(Slave2,locBuf,sizeof(locBuf)-1,0));
			locBuf[rxCnt] = 0;
			printf("%lu:%s[%d]\n",++pakCnt,locBuf,rxCnt);
			ch = '\x01';
			waitfor(aascWriteChar(Slave2,ch));
		}
	}
}