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

📄 pcmaster.c

📁 基于FREESCALE MC56F802 BLDC的源程序
💻 C
📖 第 1 页 / 共 4 页
字号:
					respPrepare(PCMASTER_STC_RECRUN,1);	/* recorder running */
				}
			}
			else								/* recorder is not initialized */
			{
				respPrepare(PCMASTER_STC_NOTINIT,1);	/* recorder not initialized */
			}
		}break;
		case PCMASTER_CMD_STOPREC:					/* stop recorder */
		{
			if (recorder.varCnt)			/* recorder initialized */
			{
				if (status & ST_REC_ACTIVATED)		/* recorder activated */
				{	
					if (status & ST_REC_RUNNING)		/* recorder running */
					{
						respPrepare(PCMASTER_STC_RECDONE,1);	/* recorder finished */
					}
					else								/* recorder not running */
					{	
						/* initialize posttrigger value */
						(recorder.recToEnd) = (recorder.postTrigger);	
						periphBitSet(ST_REC_RUNNING,&status);	/* manually trigger 
																the recorder */
						respPrepare(PCMASTER_STC_OK,1);	/* OK */
					}
				}
				else								/* recorder not activated */
				{
					respPrepare(PCMASTER_STC_RECDONE,1);	/* recorder finished */
				}
			}						
			else									/* recorder not initialized */
			{
				respPrepare(PCMASTER_STC_NOTINIT,1);
			}
		}break;				
		case PCMASTER_CMD_GETRECBUFF:					/* get recorder buffer */
		{
			if ((recorder.varCnt) != 0)		/* recorder initialized */
			{
				if (!(status & ST_REC_RUNNING))		/* recorder not running */
				{
					/* recorder buffer address */
					dataBuff[1]=(unsigned int)(recorderBuff) & 0x00FF;	
					
					/* recorder buffer address */
					dataBuff[2]=(unsigned int)(recorderBuff) >> 8;		
					i=(recorder.recPos)\
						/(recorder.recSetLen);
					
					/* position of the last sample in recorder buffer */
					dataBuff[3]=(i) & 0x00FF;		
					
					/* position of the last sample in recorder buffer */
					dataBuff[4]=(i) >> 8;			
					
					respPrepare(PCMASTER_STC_OK,5);	/* OK */
				}
				else								/* recorder running */
				{
					respPrepare(PCMASTER_STC_SERVBUSY,1);	/* service busy */
				}
			} else									/* recorder initialized */
			{
				respPrepare(PCMASTER_STC_NOTINIT,1);	/* recorder not initialized */
			}
		}break;
		#endif
		#ifdef PCMASTER_INCLUDE_CMD_APPCMD
		case PCMASTER_CMD_GETAPPCMDSTS:	/* get user application	command call status */
		{
			dataBuff[1]=appCmdSts;		/* copy status byte in the output buffer */
			respPrepare(PCMASTER_STC_OK,2);			/* OK */
		}break;
		#endif
		#ifndef PCMASTER_INCLUDE_CMD_GETINFO_BRIEF
		case PCMASTER_CMD_GETINFO:					/* get info about hardware */
		{
			cmdGetInfo();							/* execute the command */
			
			respPrepare(PCMASTER_STC_OK,(1 + 10 + PCMASTER_IDT_STRING_LEN)); /* OK */
		}break;
		#endif
		#ifdef PCMASTER_INCLUDE_CMD_GETINFO_BRIEF
		case PCMASTER_CMD_GETINFOBRIEF:		/* get brief info about hardware */
		{
			cmdGetInfoBrief();						/* execute the command */
			
			respPrepare(PCMASTER_STC_OK,(1 + 6)); /* OK */
		}break;
		#endif		

		/* --------------------------
		    standard format commands
		   -------------------------- */
		#ifdef PCMASTER_INCLUDE_CMD_SCOPE
		case PCMASTER_CMD_SETUPSCOPE:					/* setup scope */
		{
			if ( (dataBuff[2]==0) || (dataBuff[2] > 8) )	
			/* varCnt is zero or greater than 8 */
			{
				respPrepare(PCMASTER_STC_INVBUFF,1);	/* invalid buffer size */
				return;
			}
		
			/* init scope */
			cmdScopeInit(&dataBuff[2], &scope.varCnt);
			
			/* check size of variable */
			for(i=0 ; i<(scope.varCnt) ; i++)	
			{
			/* varSize is 0 or 1,3,5,7,... */
				if ( ((scope.varDef[i].varSize)==0) || (((scope.varDef[i].varSize)%2)!=0) )
				{
					/* invalid size of variable */
					respPrepare(PCMASTER_STC_INVSIZE,1);	
					/* reset scope */
					scope.varCnt = 0;					
					return;
				}
			}
			respPrepare(PCMASTER_STC_OK,1);
		}break;
		#endif
		#ifdef PCMASTER_INCLUDE_CMD_RECORDER
		case PCMASTER_CMD_SETUPREC:				/* setup recorder */
		{
			periphBitClear(ST_REC_RUNNING | ST_REC_ACTIVATED, &status);   /* reset the recorder */

			if ( (dataBuff[17]==0) || (dataBuff[17] > 8) )	
			/* varCnt is zero or greater than 8 */
			{
				respPrepare(PCMASTER_STC_INVBUFF,1);	/* invalid buffer size */
				return;
			}

			/* init recorder */
			cmdRecInit(&dataBuff[2],\
				&(recorder.trgMode));	

			if (recorder.trgMode)
			{
				if (!( ((recorder.trgVarSize)==2) || ((recorder.trgVarSize)==4) ) )		
				/* trigger variable size is not 2 or 4 */
				{
					respPrepare(PCMASTER_STC_INVSIZE,1);	/* invalid buffer size */
					/* reset scope */
					(recorder.varCnt) = 0;			
					return;
				}
				status = (status & 0xfff0) | (((recorder.trgVarSize) << 1) + (recorder.trgVarSigned));
			}
			
			/* init scope */
			cmdScopeInit(&dataBuff[17],&(recorder.varCnt));	
			
			/* check variable sizes */
			for(i=0 ; i<(recorder.\
				varCnt) ; i++)	
			{
				if ( ((recorder.\
					varDef[i].varSize)==0) || (((recorder.varDef[i].varSize)%2)!=0) )	
				/* varSize is 0 or 1,3,5,7,... */
				{
					respPrepare(PCMASTER_STC_INVSIZE,1); /* invalid buffer size */
					/* reset scope */
					recorder.varCnt=0;					
					return;
				}
			}
		
			recorder.recTime = recorder.timeDiv;
			recorder.recToEnd=1;
			recorder.recPos=0;
						
			recorder.recSetLen=0;							
			for (i=0 ; i < recorder.varCnt ; i++)	
			/* check if recorder buffer size is valid */
			{
				/* length of one set of samples (in bytes) */
				recorder.recSetLen += recorder.varDef[i].varSize;			
			}
			/* length (in words) */
			recorder.recSetLen = (recorder.recSetLen) >> 1;	
			/* buffer length required (in words) */
			recorder.totalSmps = recorder.totalSmps * recorder.recSetLen;	
			
			if ((recorder.totalSmps) <= PCMASTER_REC_BUFF_LEN)		
			/* buffer length required is smaller than recorder buffer */
			{
				varaddr=(int *)recorder.trgVarAddr;
				switch (status & 0x000f)
				{
					/* unsigned word */
					case REC_WORD_UNSIGNED: recorder.recLastVal.uw=(unsigned int)*varaddr; break;	
					/* signed word */
					case REC_WORD_SIGNED: recorder.recLastVal.sw=(int)*varaddr; break;			
					/* unsigned double */
					case REC_DWORD_UNSIGNED: recorder.recLastVal.ud=(unsigned long)*varaddr; break;	
					/* unsigned double */
					case REC_DWORD_SIGNED: recorder.recLastVal.sd=(long)*varaddr; break;			
				}
				respPrepare(PCMASTER_STC_OK,1);
				periphBitSet(ST_REC_ACTIVATED, &status);		/* recorder activated */
			} 
			else
			/* invalid buffer size specified */
			{
				recorder.varCnt=0;
				respPrepare(PCMASTER_STC_INVBUFF,1);	/* invalid buffer size */
				break;
			}
		}break;
		#endif
		case PCMASTER_CMD_READMEM:					/* read block of memory */
		{
			length=dataBuff[2];
			
			if ((length+2) <= (PCMASTER_BUFFER_SIZE))
			/* memory block is too long */	
			{
				/* read memory */
				cmdReadMem(&dataBuff[3],&dataBuff[1]);	
				respPrepare(PCMASTER_STC_OK,(length+1));	/* OK */
			}
			else						
			/* response greater than buffer */
			{
				/* response buffer overflow */
				respPrepare(PCMASTER_STC_RSPBUFFOVF,1);	
			}
		}break;
		case PCMASTER_CMD_WRITEMEM:				/* write block of memory */
		{
			/* read length of memory block from the message */
			length=dataBuff[2];	
			
			asm{move sr,i};		/* store SR register (store IPL setting) */
			archDisableInt();	/* disable interrupts for this operation */
			/* write to memory */
			cmdWriteMem(&dataBuff[3],&dataBuff[5]);	
			asm{move i,sr};	/* restore SR register (restore IPL setting ) */
			
			respPrepare(PCMASTER_STC_OK,1);
		}break;
		case PCMASTER_CMD_WRITEMEMMASK:			/* write to memory with mask */
		{
			/* read length of memory block from the message */
			length=dataBuff[2];	
			
			asm{move sr,i};		/* store SR register (store IPL setting) */
			archDisableInt();	/* disable interrupts for this operation */
			/* write to memory */
			cmdWriteMemMask(&dataBuff[3],&dataBuff[length+3+2]); 
			asm{move i,sr};	/* restore SR register (restore IPL setting ) */
			
			respPrepare(PCMASTER_STC_OK,1);			/* OK */
		}break;	
		#ifdef PCMASTER_INCLUDE_CMD_APPCMD
		case PCMASTER_CMD_CALLAPPCMD:			/* call user application command */
		{
			if (dataBuff[1] > PCMASTER_APPCMD_BUFF_LEN)	
			/* check Application Command length */
			{
				respPrepare(PCMASTER_STC_INVBUFF,1);	/* invalid buffer size */
			}
			else
			{
				if (appCmdSts==PCMASTER_APPCMD_RUNNING)	
				/* Application Command already called */
				{
					respPrepare(PCMASTER_STC_SERVBUSY,1);	/* service is busy	 */
				}
				else					/* no Application Command was called */
				{
				/* copy Application Command data to Application Command buffer */
					cmdCallAppCmd(&dataBuff[1],appCmdBuff);	
					appCmdSts = PCMASTER_APPCMD_RUNNING;
					respPrepare(PCMASTER_STC_OK,1);	/* OK */
				}
			}
		}break;
		#endif
		default:									/* invalid command */
		{
			respPrepare(PCMASTER_STC_INVCMD,1);		/* invalid command */
		}break; 
	}
}

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

#ifdef PCMASTER_INCLUDE_CMD_RECORDER
void pcmasterRecorder(void)
{
int *addr;
union{
	unsigned int	uw;
	unsigned long	ud;
	signed int 		sw;
	signed long		sd;
} actual;

	if (status & ST_REC_ACTIVATED)	/* recorder activated */
	{
		if (recorder.recTime == recorder.timeDiv)
		/* now is the right time to make samples */
		{

            if (!(ST_REC_READ_PRETRIG & status))
            {
                if (recPretrigCount >= (recorder.totalSmps - 
                            (recorder.recSetLen * recorder.postTrigger))) 
                    periphBitSet(ST_REC_READ_PRETRIG, &status);
                    else recPretrigCount += recorder.recSetLen;
            }
		    
			switch (status & 0x000f)
			{
			case REC_WORD_UNSIGNED:
			/* size=2, unsigned */
			{
				addr=(int *)recorder.trgVarAddr;
				actual.uw=(unsigned int)*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.uw >= recorder.trgTreshold.uw) && \
						(recorder.recLastVal.uw <	recorder.trgTreshold.uw))
					/* 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.uw <= recorder.trgTreshold.uw) && \
							(recorder.recLastVal.uw > recorder.trgTreshold.uw))
					/* 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.uw = actual.uw;	
				/* reset recorder time */
				recorder.recTime = 0;		
			}break; 
			
			case REC_WORD_SIGNED:
			/* size=2, signed */			
			{
				addr=(int *)recorder.trgVarAddr;
				actual.sw=(int)*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;	
									

⌨️ 快捷键说明

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