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

📄 pcmaster.c

📁 基于FREESCALE MC56F802 BLDC的源程序
💻 C
📖 第 1 页 / 共 4 页
字号:
				if ( (!(status & ST_REC_RUNNING)) && (status & ST_REC_READ_PRETRIG) )
				{
					if ((recorder.trgMode == PCMASTER_REC_TRIGRIS) && \
						(actual.sw >= recorder.trgTreshold.sw) && \
						(recorder.recLastVal.sw < recorder.trgTreshold.sw))
					/* recorder is configured to rising edge
					   actual value greater than treshold
					   last value smaller than treshold */
					{
   						periphBitSet(ST_REC_RUNNING, &status);				
   						recorder.recToEnd = recorder.postTrigger-1;
					}
					else if ((recorder.trgMode == PCMASTER_REC_TRIGFAL) && \
							(actual.sw <= recorder.trgTreshold.sw) && \
							(recorder.recLastVal.sw > recorder.trgTreshold.sw))
					/* recorder is configured to falling edge
					   actual value smaller than treshold
					   last value greater than treshold */
					{
   						periphBitSet(ST_REC_RUNNING, &status);				
   						recorder.recToEnd = recorder.postTrigger-1;
					}
				}
				else if(status & ST_REC_READ_PRETRIG) recorder.recToEnd--;
			
				if (recorder.recToEnd==0)
				/* buffer is full */
				{
					periphBitClear(ST_REC_RUNNING | ST_REC_ACTIVATED | \
					                ST_REC_READ_PRETRIG, &status);			
					recorder.recToEnd=1;
				}
				/* remember last value */
				recorder.recLastVal.sw=actual.sw;	
				/* reset recorder time */
				recorder.recTime=0;		
			}break;
			
			case REC_DWORD_UNSIGNED:
			/* size=4, unsigned	*/
			{
				addr=(int *)recorder.trgVarAddr;
				actual.ud=(unsigned long)*addr;			/* read actual value */
				
				/* read new samples */
				readSample(&recorder.varCnt, &recorderBuff[recorder.recPos]);		

				recorder.recPos += recorder.recSetLen;
				/* wrap around */
				if (recorder.recPos >= recorder.totalSmps) recorder.recPos=0;		
									
				if ( (!(status & ST_REC_RUNNING)) && (status & ST_REC_READ_PRETRIG) )
				{
					if ((recorder.trgMode == PCMASTER_REC_TRIGRIS) && \
						(actual.ud >= recorder.trgTreshold.ud) && \
						 (recorder.recLastVal.ud < recorder.trgTreshold.ud))
					/* recorder is configured to rising edge
					   actual value greater than treshold
					   last value smaller than treshold */
					{
   						periphBitSet(ST_REC_RUNNING, &status);				
   						recorder.recToEnd = recorder.postTrigger-1;
					}
					else if ((recorder.trgMode == PCMASTER_REC_TRIGFAL) && \
							(actual.ud <= recorder.trgTreshold.ud) && \
							(recorder.recLastVal.ud > recorder.trgTreshold.ud))
					/* recorder is configured to falling edge
					   actual value smaller than treshold
					   last value greater than treshold */
					{
   						periphBitSet(ST_REC_RUNNING, &status);				
   						recorder.recToEnd = recorder.postTrigger-1;
					}
				}
				else recorder.recToEnd--;
			
				if (recorder.recToEnd==0)
				/* buffer is full */
				{
					periphBitClear(ST_REC_RUNNING | ST_REC_ACTIVATED |\
					                ST_REC_READ_PRETRIG, &status);			
					recorder.recToEnd=1;
				}
				/* remember last value */
				recorder.recLastVal.ud=actual.ud;	
				/* reset recorder time */
				recorder.recTime=0;					
			}break;
			
			case REC_DWORD_SIGNED:
			/* size=4, signed */
			{
				addr=(int *)recorder.trgVarAddr;
				actual.sd=(long)*addr;					/* read actual value */
				
				/* read new samples */
				readSample(&recorder.varCnt, &recorderBuff[recorder.recPos]);	

				recorder.recPos += recorder.recSetLen;
				/* wrap around */
				if (recorder.recPos >= recorder.totalSmps) recorder.recPos=0;		
									
				if ( (!(status & ST_REC_RUNNING)) && (status & ST_REC_READ_PRETRIG) )
				{
					if ((recorder.trgMode == PCMASTER_REC_TRIGRIS) && \
						(actual.sd >= recorder.trgTreshold.sd) && \
						(recorder.recLastVal.sd < recorder.trgTreshold.sd))
					/* recorder is configured to rising edge
					   actual value greater than treshold
					   last value smaller than treshold */
					{
   						periphBitSet(ST_REC_RUNNING, &status);				
   						recorder.recToEnd = recorder.postTrigger-1;
					}
					else if ((recorder.trgMode == PCMASTER_REC_TRIGFAL) && \
							(actual.sd <= recorder.trgTreshold.sd) && \
							(recorder.recLastVal.sd > recorder.trgTreshold.sd))
					/* recorder is configured to falling edge
					   actual value smaller than treshold
					   last value greater than treshold */
					{
   						periphBitSet(ST_REC_RUNNING, &status);				
   						recorder.recToEnd = recorder.postTrigger-1;
					}
				}
				else recorder.recToEnd--;
			
				if (recorder.recToEnd==0)
				/* buffer is full */
				{
					periphBitClear(ST_REC_RUNNING | ST_REC_ACTIVATED | \
					                ST_REC_READ_PRETRIG, &status);			
					recorder.recToEnd=1;
				}
				/* remember last value */
				recorder.recLastVal.sd=actual.sd;	
				/* reset recorder time */
				recorder.recTime=0;		
			}break;
			case 0: /* no trigger variable specified */
			{
				/* read new samples */
				readSample(&recorder.varCnt, &recorderBuff[recorder.recPos]);	

				recorder.recPos += recorder.recSetLen;
				/* wrap around */
				if (recorder.recPos >= recorder.totalSmps) recorder.recPos=0;		
									
				if (status & ST_REC_RUNNING)
				{
				    recorder.recToEnd--;
				}
			
				if (recorder.recToEnd==0)   /* buffer is full */
				{
					periphBitClear(ST_REC_RUNNING | ST_REC_ACTIVATED | \
					                ST_REC_READ_PRETRIG, &status);			
					recorder.recToEnd = 1;
				}
				/* reset recorder time */
				recorder.recTime=0;		
			}break;
			}
		}
		/* wait for the right time */
		else recorder.recTime++;			
    }
    else /* recorder not activated */
    {
        recPretrigCount = 0;
        periphBitClear(ST_REC_READ_PRETRIG, &status);
    }
}
#endif

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

static void messageData(UWord16 startOfMessage)
{
	if (startOfMessage == 0)	/* no start of message */
	{
		if (status & ST_STARTED)	/* start of message already detected */
		{
			if (pos != length)		
			{
				/* read byte, accumulate checksum */
				dataBuff[pos] = inChar;		
				checkSum += inChar;			/* checksum accumulation */
				pos++;						/* next position in buffer */

				if (status & ST_STD_CMD)	
				/* inChar contains length of standard format message */
				{
					length = inChar + 2;	/* read length of message */
					periphBitClear(ST_STD_CMD,&status);	/* clear flag */
			
					if (length > (PCMASTER_BUFFER_SIZE))
					/* command is greater than input buffer */
					{
						/* clear flag */
						periphBitClear(ST_STARTED | ST_ST_CHAR_REC,&status);	
						/* input buffer overflow */
						respPrepare(PCMASTER_STC_CMDBUFFOVF,1);	
						sendResponse(&response);	/* send response to PC */
						/* disable Rx Full Interrupt during transmitting*/
						ioctl(SCI_0, SCI_RX_FULL_INT, SCI_DISABLE);  
					}
				}			
			}
			else	/* end of message */
			{
				checkSum += inChar;			/* accumulate checksum */
				if ((checkSum & 0x00FF) == 0) /* correct checksum */
				{
					messageDecode();
				}
				else						/* checksum error */
				{
					/* checksum error response */
					respPrepare(PCMASTER_STC_CMDSERR,1);	
				}
				
				/* clear flag */
				periphBitClear(ST_STARTED | ST_ST_CHAR_REC,&status);	
				sendResponse(&response);	/* send response to PC */
				/* disable Rx Full Interrupt during transmitting*/
				ioctl(SCI_0, SCI_RX_FULL_INT, SCI_DISABLE);  

			}
		}
	}
	else			/* start of message */
	{
		/* reset receiver, read first byte of message */
		periphBitSet(ST_STARTED,&status);			/* message receiving */
		/* read byte, start of checksum accumulating */
		checkSum = dataBuff[0] = inChar;	
		/* next position in buffer */
		pos = 1;							
		/* value sufficient for standard format commands */
		length = 2;							
		
		if (inChar >= 0xC0)		/* special format command */
		{
			length=((inChar & 0x30) >> 3) + 1;	/* length decoding */
		}
		else	
		/* standard format command (next byte will be length of the message) */
		{
			periphBitSet(ST_STD_CMD,&status);			/* wait for next character */
		}
	}
}

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

#pragma interrupt
void pcmasterRxTxIsr(void)
{
	archPushAllRegisters();
	
	/* message is transmitted */
	if (status & ST_SENDING) sendBuffer(); 	/* send data */			
	else
	{
		ioctl(SCI_0, SCI_RX_FULL_INT, SCI_DISABLE);  /* disable Rx Full Interrupt */
		archEnableInt();	/* enable interrupts to make this routine interruptible */
	
		inChar = ioctl(SCI_0, SCI_GET_STATUS_REG, NULL);
		inChar = ioctl(SCI_0, SCI_READ_DATA, NULL);		/* read received character */

		if ((status & ST_ST_CHAR_REC) == 0)	/* last byte was not '+' */
		{
			if (inChar == '+') 			/* '+' received */
			{
				periphBitSet(ST_ST_CHAR_REC,&status);
			}
			else 						/* any byte received */
			{
				messageData(0);			/* byte received */
			}
		}
		else				/* the last byte was '+' */
		{
			if (inChar == '+') 			/* doubled '+' (this is the second one) */
			{
				messageData(0);			/* byte received */
			}
			else					/* start of message */
			{
				messageData(1);		/* byte received */
			}	

				periphBitClear(ST_ST_CHAR_REC,&status);	/* clear flag */
		}
	
		ioctl(SCI_0, SCI_RX_FULL_INT, SCI_ENABLE); /* enable Rx Full Interrupt */
	}
	
 	archPopAllRegisters();
}

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

#pragma interrupt
void pcmasterRxErrorIsr(void)
{
	UWord16 pcm_tmp;

	pcm_tmp = ioctl(SCI_0, SCI_GET_STATUS_REG, NULL);
	ioctl(SCI_0, SCI_CLEAR_STATUS_REG, NULL);
}		

/*-----------------------------------------------------------------------------
	SPECIAL FEATURES FUNCTIONS
  ---------------------------------------------------------------------------*/

void pcmasterRxTxPoll(void)
{
	/* message is transmitted */
	if ((status & ST_SENDING)&&(ioctl(SCI_0, SCI_GET_TX_EMPTY, NULL))) sendBuffer(); 	/* send data */			
	else if (ioctl(SCI_0, SCI_GET_RX_FULL, NULL))
	{

		inChar = ioctl(SCI_0, SCI_GET_STATUS_REG, NULL);
		inChar = ioctl(SCI_0, SCI_READ_DATA, NULL);		/* read received character */

		if ((status & ST_ST_CHAR_REC) == 0)	/* last byte was not '+' */
		{
			if (inChar == '+') 			/* '+' received */
			{
				periphBitSet(ST_ST_CHAR_REC,&status);
			}
			else 						/* any byte received */
			{
				messageData(0);			/* byte received */
			}
		}
		else				/* the last byte was '+' */
		{
			if (inChar == '+') 			/* doubled '+' (this is the second one) */
			{
				messageData(0);			/* byte received */
			}
			else					/* start of message */
			{
				messageData(1);		/* byte received */
			}	

				periphBitClear(ST_ST_CHAR_REC,&status);	/* clear flag */
		}
	}
}

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

#ifdef PCMASTER_INCLUDE_CMD_RECORDER
UWord16 *pcmasterGetRecBuffPtr(void) 
{
	return (recorderBuff);
}
#endif

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

void pcmasterRecBuffFull(void) 
{
	status &= ~ST_REC_ACTIVATED;
}

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

UWord16 pcmasterGetRecBuffEmpty(void) 
{
	return (status & ST_REC_ACTIVATED);
}				

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

⌨️ 快捷键说明

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