⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pcmaster.c

📁 基于FREESCALE MC56F802 BLDC的源程序
💻 C
📖 第 1 页 / 共 4 页
字号:
	move y1,length				/* store length */
	rts
}

/*------------------------------------------------------------------------------*/

static asm void cmdWriteMem(UWord16 *Addr, UWord16 *Data)
/* write data in data memory
 Addr - address of address
 Data - address of data to be written
 using length (number of bytes to be written) */
{
	move x:(r2)+,y1				/* low byte */
	move x:(r2)+,y0				/* high byte */
	move #8,x0
	lsll y0,x0,y0				/* high byte shifted in Y0 */
	add y0,y1					/* complete address */
	move y1,r2					/* use R2 */

	move length,y0
	lsr y0

	do y0,EndDo
	move x:(r3)+,y0				/* low byte of data */
	move x:(r3)+,y1				/* high byte of data */
	lsll y1,x0,y1				/* shift high byte */
	add y1,y0					/* complete data */
	move y0,x:(r2)+				/* write data to destination */
EndDo:
	rts
}

/*------------------------------------------------------------------------------*/

static asm void cmdWriteMemMask(UWord16 *AddrData, UWord16 *AddrMask)
/* write data in data memory using mask
 Addr - address of address
 Data - address of data to be written
 using length (number of bytes to be written) */
{
	move x:(r2)+,y1				/* low byte */
	move x:(r2)+,y0				/* high byte */
	move #8,x0
	lsll y0,x0,y0				/* high byte shifted in Y0 */
	add y0,y1					/* complete address */
	move y1,r1					/* use R1 */

	move length,y0
	lsr y0

	do y0,EndDo
	move x:(r2)+,y0				/* low byte of data */
	move x:(r2)+,y1				/* high byte of data */
	lsll y1,x0,y1				/* shift upper byte */
	add y1,y0					/* complete data -> Y0 */
	move y0,a1					/* store data -> A1 */
	move x:(r3)+,y0				/* low byte of mask */
	move x:(r3)+,y1				/* high byte of mask */
	lsll y1,x0,y1				/* shift high byte */
	add y1,y0					/* complete mask -> Y0 */
	move y0,b1					/* store mask -> B1 */
	and a1,y0					/* data & mask -> Y0 */
	neg b						/* negate mask */
	move b1,y1
	
	move x:(r1),a1				/* read memory -> A1 */
	and a1,y1					/* origdata & neg(mask) -> Y1 */
	or y1,y0					/* (data & mask) or (origdata & neg(mask)) -> Y0 */
	move y0,x:(r1)+				/* write back */

EndDo:
	rts
}

/*------------------------------------------------------------------------------*/

static asm void cmdCallAppCmd(UWord16 *Addr, UWord16 *Data)
/* write data in application command buffer
 Addr - address of beginning of Application Command data in the message (dataBuff)
 Data - address of Application Command buffer */
{
	move x:(r2)+,a				/* length */
	lsr a

	jeq NoData					/* jump if command contains no data */

	move x:(r2)+,x0
	move x0,x:(r3)+				/* write application command code */
	
	move #8,x0					/* shift value */
	
	do a,EndDo
	move x:(r2)+,y0				/* low byte of data */
	move x:(r2)+,y1				/* high byte of data */
	lsll y1,x0,y1				/* shift high byte */
	add y1,y0					/* complete data */
	move y0,x:(r3)+				/* write data to destination */
EndDo:
	rts
	
NoData:
	move x:(r2)+,x0
	move x0,x:(r3)+				/* write application command code */
	rts
}

/*------------------------------------------------------------------------------*/

static asm void cmdRecInit(UWord16 *Addr, UWord16 *DestAddr)
/* initialize recorder (copy data from dataBuff to Recorder structure)
 Addr - address of Recorder settings data in the message (dataBuff)
 DestAddr - address of Recorder structure with settings (Recorder) */
{
	/* trigger mode */
	move x:(r2)+,a
	move a,x:(r3)+			
	move #8,x0

	/*  total samples
	 	post trigger
	 	timediv
	 	trigger variable address */
	do #4,EndDo
	move x:(r2)+,y1				/* low byte */
	move x:(r2)+,y0				/* high byte */
	lsll y0,x0,y0
	add y1,y0					/* complete word */
	move y0,x:(r3)+				/* write word to memory */
	EndDo:
	
	/* trigger variable size */
	move x:(r2)+,a				/* read */
	move a,x:(r3)+				/* write to memory */

	/* trigger variable signed */
	move x:(r2)+,a				/* read */
	move a,x:(r3)+				/* write to memory */
	
	/* trigger treshold */
	do #2,EndSize
	move x:(r2)+,y1				/* low byte */
	move x:(r2)+,y0				/* high byte */
	lsll y0,x0,y0
	add y1,y0					/* complete word */
	move y0,x:(r3)+				/* write word to memory */
EndSize:
	rts
}

/*------------------------------------------------------------------------------*/

static asm void inline readSample(UWord16 *Addr, UWord16 *DestAddr)
/* read recorder data (data will be copied to dataBuff)
 Addr - address of recorder settings buffer
 DestAddr - address of recorder data buffer */
{
	move x:(r2)+,lc				/* number of channels */

	do lc,EndChannel			
	move x:(r2)+,x0
	lsr x0						/* number of words */
	move x:(r2)+,r1				/* address of scope variable */
	move #8,y0
	push x0						/* loop counter */
	Word:
	move x:(r1)+,a1
	move a1,x:(r3)+				/* write value to buffer */
	
	pop a
	dec a
	tst a
	push a
	bgt Word
	pop a
	nop
	EndChannel:

	rts
}

#define add
/*------------------------------------------------------------------------------*/

static void cmdGetInfo(void)
/* get information about hardware 
	(initialization sequence before any other communication)
 data are transfered to dataBuff */
{
	int i;				/* temporary variable for string copy */
	
	dataBuff[1] = 1;                                /* protocol version	*/
	dataBuff[2] = PCMASTER_CFG_FLAFGS;	            /* CFG_FLAFGS */
	dataBuff[3] = 2;                                /* dataBusWdt */
	dataBuff[4] = PCMASTER_GLOB_VERSION_MAJOR;      /* version */
	dataBuff[5] = PCMASTER_GLOB_VERSION_MINOR;      /* version */
	dataBuff[6] = PCMASTER_BUFFER_SIZE - 1;     /* size of input buffer 
	                                                   (without CMD, LENGTH) */
    /* size in bytes is transfered to PC */
	dataBuff[7] = (PCMASTER_REC_BUFF_LEN*2) & 0x00ff; /* size of 
	                                                     recorder buffer */
	dataBuff[8] = (PCMASTER_REC_BUFF_LEN*2) >> 8;
	dataBuff[9] = PCMASTER_RECORDER_TIME_BASE & 0x00ff; /* period of Recorder 
                                                           routine launch */
	dataBuff[10]= PCMASTER_RECORDER_TIME_BASE >> 8;
	
	strcpy((char*)&(dataBuff[11]),PCMASTER_IDT_STRING);
}

/*------------------------------------------------------------------------------*/

static void cmdGetInfoBrief(void)
/* get brief information about hardware
   (initialization sequence before any other communication)
   data are transfered to dataBuff */
{
	dataBuff[1] = PCMASTER_PROT_VER;					/* protocol version	*/
	dataBuff[2] = PCMASTER_CFG_FLAFGS;				/* CFG_FLAFGS */
	dataBuff[3] = PCMASTER_DATABUSWDT;				/* dataBusWdt */
	dataBuff[4] = PCMASTER_GLOB_VERSION_MAJOR; 		/* version */
	dataBuff[5] = PCMASTER_GLOB_VERSION_MINOR;		/* version */
	
	/* size of input buffer	(without CMD, LENGTH) */
	dataBuff[6] = PCMASTER_BUFFER_SIZE - 1;	
}

/*------------------------------------------------------------------------------*/

static void sendBuffer(void)
/* sends '+', data of message (dataBuff) and checksum (dataBuff)
 using length */
{
	if (pos <= length)						/* is it end of message ? */
	{
		inChar = ioctl(SCI_0, SCI_GET_STATUS_REG, NULL);
		ioctl(SCI_0, SCI_WRITE_DATA, dataBuff[pos]);
			
		if (dataBuff[pos] != '+') 		/* current character is not '+' */
		{
			checkSum += dataBuff[pos];	/* accumulate checksum */
			pos++;
		}
		else 								/* current character is '+' */
		{
			if (status & ST_ST_SENT)		/* the last sent char was '+' */
			{
				periphBitClear(ST_ST_SENT,&status);
				checkSum += dataBuff[pos];	/* accumulate checksum */
				pos++;
			}
			else							/* the last sent byte was not '+' */
			{
				periphBitSet(ST_ST_SENT,&status);	
			}
		}
		
		if ((pos == length) && !(status & ST_CS_ADDED))		/* the last byte 
								before cs was sent, now add the checksum */
		{
			checkSum = (-checkSum) & 0x00FF;	/* compute checksum */
			dataBuff[pos] = checkSum;
			periphBitSet(ST_CS_ADDED,&status);			/* set flag */
		}
	}
	else		/* end of transmitting */
	{
		ioctl(SCI_0, SCI_TX_EMPTY_INT, SCI_DISABLE); 	/* disable Tx Empty Interrupt */
		ioctl(SCI_0, SCI_RX_FULL_INT, SCI_ENABLE);  	/* enable Rx Full Interrupt */

		periphBitClear(ST_SENDING | ST_CS_ADDED,&status);		/* reset transmitter 
												(switch to receiving mode) */
	}
}

/*------------------------------------------------------------------------------*/

static void sendResponse(sResponse *resp) 
/* put all data to dataBuff, computes a checksum and transmit '+' as 
	beginning of message */
{
	periphBitSet(ST_SENDING,&status);			/* set flag */
	dataBuff[0]=resp->status;		/* status of trasmitted message */
	length=resp->length;				/* length of message */
	pos=0;								/* position in the message */
	
	/* send start of message */
	inChar = ioctl(SCI_0, SCI_GET_STATUS_REG, NULL);
	
	ioctl(SCI_0, SCI_WRITE_DATA, '+');          /* write SOB character */
	ioctl(SCI_0, SCI_TX_EMPTY_INT, SCI_ENABLE); /* enable Tx Empty Interrupt */

	checkSum = 0;						/* reset checksum */
}

/*------------------------------------------------------------------------------*/

static void messageDecode(void)
/* decoding of received message
 proper command will be executed and response sent to PC */
{
int *varaddr;
unsigned int i;

	switch(dataBuff[0])
	{
		/* -------------------------
		    special format commands
		   ------------------------- */
		case PCMASTER_CMD_READVAR16:					/* read 16-bit variable */
		{
			length=2;
			/* execute command */
			cmdReadMem(&dataBuff[1],&dataBuff[1]);	
			respPrepare(PCMASTER_STC_OK,3);			/* OK */
		}break;
		case PCMASTER_CMD_READVAR32:					/* read 32-bit variable */
		{
			length=4;
			/* execute command */
			cmdReadMem(&dataBuff[1],&dataBuff[1]);	
			respPrepare(PCMASTER_STC_OK,5);			/* OK */
		}break;
		#ifdef PCMASTER_INCLUDE_CMD_SCOPE
		case PCMASTER_CMD_READSCOPE:					/* read scope variables */
		{
			/* scope not configured */
			if (scope.varCnt==0)			
			{
				respPrepare(PCMASTER_STC_NOTINIT,1);	/* scope not initialized */
			}		
			else									/* scope configured */
			{
				/* execute command */
				cmdReadScope(&(scope.varCnt),	&dataBuff[1]); 
				respPrepare(PCMASTER_STC_OK,length);	/* OK */
			}
		}break;
		#endif
		#ifdef PCMASTER_INCLUDE_CMD_RECORDER
		case PCMASTER_CMD_GETRECSTS:					/* get recorder status */
		{
			if (recorder.varCnt)		/* recorder is initialized */
			{
				if (!(status & ST_REC_ACTIVATED))
				{
					respPrepare(PCMASTER_STC_RECDONE,1);	/* recorder finished */
				}
				else
				{
					respPrepare(PCMASTER_STC_RECRUN,1);	/* recorder running */
				}
			}
			else								/* recorder is not initialized */
			{
				respPrepare(PCMASTER_STC_NOTINIT,1);	/* recorder not initialized */
			}
		}break;
		case PCMASTER_CMD_STARTREC:					/* start recorder */
		{
			if (recorder.varCnt)			/* recorder initialized */
			{
				if (!(status & ST_REC_RUNNING))		/* recorder not running */
				{
					periphBitClear(ST_REC_RUNNING,&status);		/* stop recorder if it 
																	is running */
					/* initialize time div */
					(recorder.recTime) = (unsigned)(recorder.timeDiv);	
					/* address of triggering variable */
					varaddr=(int *)(recorder.trgVarAddr);	
					switch (status & 0x000f)			
					/* initialize last rcorder value 
						with current value of triggering variable */
					{
						case REC_WORD_UNSIGNED: recorder.recLastVal.uw=(unsigned int)*varaddr; break;
						case REC_WORD_SIGNED: recorder.recLastVal.sw=(int)*varaddr; break;
						case REC_DWORD_UNSIGNED: recorder.recLastVal.ud=(unsigned long)*varaddr; break;
						case REC_DWORD_SIGNED: recorder.recLastVal.sd=(long)*varaddr; break;
					}
					/* activate recorder to wait for trigger */
					periphBitSet(ST_REC_ACTIVATED, &status);
					respPrepare(PCMASTER_STC_OK,1);	/* OK */
				}
				else
				{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -