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

📄 osek_com.c.svn-base

📁 我们自己开发的一个OSEK操作系统!不知道可不可以?
💻 SVN-BASE
📖 第 1 页 / 共 2 页
字号:
		else 
		{
			if (QUEUED)
			{
				message.Queued=1;
				EnterQueue(ReceiveMessageQ,message);
			}
			else
			{
				message.Queued=0;
				EnterQueue(ReceiveMessageUQ,message);
			}
		}
	}
	return E_OK;
}

int AckService(int serviceid)
{
	//if()  查询CAN,数据是否全收到
		return 1;
	//else
	//	return 0;

}




int ResetCAN(ResetMode type)
{
	if(type==Hard_Reset)
	{
		
	}
	if(type==Soft_Reset)
	{
		TCNMCR=0;
		CANICR=0;
		CANTCR=0;
		CANCTRL0=CANCTRL1=0;
		PRESDIV=0;
		CANCTRL2=0;
		TIMER=0;
		RXGMSKHI=(unsigned int) (mypow(2,16)-mypow(2,4)-1);
		RXGMSKHO=(unsigned int) (mypow(2,16)-2);
		RX14MSKHI=(unsigned int) (mypow(2,16)-mypow(2,4)-1);
		RX14MSKLO=(unsigned int) (mypow(2,16)-2);
		RX15MSKHI=(unsigned int) (mypow(2,16)-mypow(2,4)-1);
		RX15MSKLO=(unsigned int) (mypow(2,16)-2);
		ESTAT=0;
		IMASK=0;
		IFLAG=0;
		RXECTR=0;
		TXECTR=0;
		return 1;
	}
	return 0;
}

void InitCAN()
{
    /*初始化操作模式*/
	/*PROPSEG (from 0 to 7)indicate Propagation Segment Time = (PROPSEG + 1) Time Quanta,1 Time Quantum = 1 Serial Clock (S-Clock) Period*/
	int PROPSEG=3;
	CANCTRL1=CANCTRL1|PROPSEG;
	/*The RJW(from 0 to 3) field defines the maximum number of time quanta a bit time may be changed during resynchronization. Resynchronizaton Jump Width = (RJW + 1) Time Quanta*/
	int RJW=1;
	RJW=RJW*64;
	CANCTRL2=CANCTRL2|RJW;
	/*PSEG1[2:0] — Phase buffer segment 1. The PSEG1 field defines the length of phase buffer segment one in the bit time. Phase Buffer Segment 1 = (PSEG1 + 1) Time Quanta*/
	int PSEG1=3;
	PSEG1=PSEG1*8;
	CANCTRL2=CANCTRL2|PSEG1;
	/*PSEG2 — Phase Buffer Segment 2. The PSEG2 field defines the length of phase buffer segment two in the bit time.Phase Buffer Segment 2 = (PSEG2 + 1) Time Quanta*/
	int PSEG2=3;
	CANCTRL2=CANCTRL2|PSEG2;
	/*PRESDIV(from 0 to 255) determines the ratio between the IMB clock frequency and the serial clock (S-clock).S-clock=fSYS/(PRESDIV + 1)*/
	PRESDIV=0;
	/*The LBUF bit defines the transmit-first scheme.
	0 = Message buffer with lowest ID is transmitted first.
	1 = Lowest numbered buffer is transmitted first.*/
	int LBUF=0;
	LBUF=(int)(LBUF*mypow(2,4));
	CANCTRL1=CANCTRL1|LBUF;
	
	/*初始化消息缓冲区*/
	/*Message Buffer Codes for Receive Buffers:
	0b0000 NOT ACTIVE — message buffer is not active.
	0b0100 EMPTY — message buffer is active and empty.
	0b0010 FULL — message buffer is full.
	0b0110 OVERRUN — second frame was received into a full buffer before the CPU read the first one.
	0b0XY1 BUSY — message buffer is now being filled with a new receive frame. This condition will be cleared within 20 cycles.*/
	/*Message Buffer Codes for Transmit Buffers:
	RTR=X 0b1000 Message buffer not ready for transmit.
	RTR=0 0b1100 Data frame to be transmitted once, unconditionally.
	RTR=1 0b1100 Remote frame to be transmitted once, and message buffer becomes an RX message buffer for data frames.
	RTR=0 0b1010 Data frame to be transmitted only as a response to a remote frame, always.
	RTR=0 0b1110 Data frame to be transmitted only once, unconditionally, and then only as a response to remote frame, always.*/
	int code=0;
	int i;
	code=code*16;
	for(i=0;i<MAXBUFFER;i++)
	{
		buffer[i].CS=code;
		buffer[i].ID_High=0;
		buffer[i].ID_Low=0;
		buffer[i].Reserved=0;
	}

	/*初始化屏蔽寄存器*/
	/*IMASK contains two 8-bit fields, IMASKH and IMASKL. IMASK can be accessed with a 16-bit read or write, and IMASKH and IMASKL can be accessed with byte reads or writes.
	IMASK contains one interrupt mask bit per buffer. It allows the CPU to designate which buffers will generate interrupts after successful transmission/reception. Setting a bit in IMASK
	enables interrupt requests for the corresponding message buffer.
	NOTE: Bit 15 (LSB) corresponds to message buffer 0. Bit 0 (MSB) corresponds to message buffer 15.*/
	IMASK=1;

	/*初始化CAN中断处理程序*/
	/*Interrupt request level(from 0 to 7). When the TouCAN generates an interrupt request, this field determines which of the interrupt request signals is asserted.*/
	int IRL=3;
	IRL=IRL*256;
	CANICR=CANICR|IRL;
	/*Interrupt level byte select. This field selects one of four time-multiplexed slots during which the interrupt request is asserted. The ILBS and IRL fields together select one of 32 effective interrupt levels.
	00 = Levels 0 to7
	01 = Levels 8 to 15
	10 = Levels 16 to 23
	11 = Levels 24 to 31*/
	int ILBS=1;
	ILBS=ILBS*64;
	CANICR=CANICR|ILBS;
	/*Wakeup interrupt mask. The WAKEMSK bit enables wake-up interrupt requests.
	0 = Wake up interrupt is disabled
	1 = Wake up interrupt is enabled*/
	int WAKEMSK=1;
	WAKEMSK=WAKEMSK*1024;
	TCNMCR=TCNMCR|WAKEMSK;
	/*Bus off interrupt mask. The BOFF MASK bit provides a mask for the bus off interrupt.
	0 = Bus off interrupt disabled
	1 = Bus off interrupt enabled*/
	int BOFFMSK=0;
	BOFFMSK=(int)(BOFFMSK*mypow(2,15));
	CANCTRL0=CANCTRL0|BOFFMSK;
	/************************************************************************/
	/* Error interrupt mask. The ERRMSK bit provides a mask for the error interrupt.
	0 = Error interrupt disabled
	1 = Error interrupt enabled                                                   */
	/************************************************************************/
	int ERRMSK=0;
	ERRMSK=(int)(ERRMSK*mypow(2,15));
	CANCTRL0=CANCTRL0|ERRMSK;

	/*HALT位取反*/
	/*Halt TouCAN S-Clock.*/
	int HALT=1;
	HALT=(int)(HALT*mypow(2,12));
	TCNMCR=TCNMCR^HALT;

	IDE=0;/*对标准帧操作*/
	lbuf=0;/*如果LBUF=0,ID号最小的先发送;如果LBUF=1,最小的消息缓冲区号先发送  */        
}

int InitSMB(MessageBuffer MB)
{
	MB.CS=0;
	unsigned long ID=MESSAGE_ID;
	unsigned int ID_H,ID_L;
	ID_H=getH(ID);
	ID_L=getL(ID);
	/*
	printf("%u\n",ID_H);
	printf("%u\n",ID_L);
	*/
	/*If extended format frame is used, this field should be set to one. If zero, standard format frame should be used.*/
	/*对扩展帧操作*/

	if(IDE==1)
	{
		/*printf("This is extend frame!\n");*/
		/*Contains a fixed recessive bit, used only in extended format. Should be set to one by the user for Tx buffers. It will be stored as received on the CAN bus for Rx buffers.*/
		int SRR=0;
		SRR=SRR*16;
		IDE=IDE*8;
		int i=(int)ID_H/8;
		int j=ID_H<<15;
		j=(int)(j/mypow(2,15));
		i=i<<5;
		ID_H=i|SRR|IDE|j;
		/*This bit is located in the least significant bit of the ID LOW word of the message buffer;0 = Data Frame, 1 = Remote Frame.*/
		int RTR=0;
		ID_L=(ID_L<<1)|RTR;
	}
	else
	{
		/*对标准帧操作*/
		if(IDE==0)
		{
			/*printf("This is standard frame!\n");*/
			/*This bit is located in the least significant bit of the ID LOW word of the message buffer;0 = Data Frame, 1 = Remote Frame.*/
			int RTR=0;
			ID_H=(ID_L<<1)|RTR;
			ID_H=ID_H<<4;
			/*Time Stamp.The ID LOW word, which is not needed for standard format, is used in a standard format buffer to store the 16-bit value of the free-running timer which is captured at the beginning of the identifier field of the frame on the CAN bus.*/
			int Time_S=1000;
			ID_L=Time_S;
		}
		else 
			return 0;
	}
	/*
	printf("%u\n",ID_H);
	printf("%u\n",ID_L);
	*/
	/*对消息缓冲区进行初始化*/
	MB.ID_High=ID_H;
	MB.ID_Low=ID_L;
	MB.Reserved=0;
	/*对控制/状态位操作*/
	/*Time Stamp. Contains a copy of the high byte of the free running timer, which is captured at the beginning of the identifier field of the frame on the CAN bus.*/
	int TS=0;
	TS=TS<<8;
	int code=8;
	code=code<<4;
	int m=0;
	int n=Down_Front;
	int p=0;
	int ServiceID = Ipdu_Down[Down_Front].IpduID;
	while(ServiceID==Ipdu_Down[Down_Front++].IpduID)
		{
			p++;
		}
	Down_Front=n;
	n=0;
	MB.Data[m++]=Ipdu_Down[Down_Front].IpduData[n++];
	while(m<MAXDATA&&p!=0)
	{
		if(n==7)
		{
			n=0;
			Down_Front++;
			p--;
		}
		MB.Data[m++]=Ipdu_Down[Down_Front].IpduData[n++];
	}
	if(m==MAXDATA)
	{
		/*printf("The message is out of distance!\n");*/
		return 0;
	}
	MB.CS=MB.CS|TS|code|m;
	/*
	printf("%u\n",MB.CS);	
	*/
	return 1;
}

int InitRMB()
{
	int flag=0;
	while(buffer[flag++].CS!=0&&flag<MAXBUFFER-2)
	{
			
	}
	if(flag==MAXBUFFER-2)
	{
		/*printf("The message buffer is full!\n");*/
		return 0;
	}
	
	unsigned long ID=MESSAGE_ID;
	unsigned int ID_H,ID_L;
	ID_H=getH(ID);
	ID_L=getL(ID);
	/*
	printf("%u\n",ID_H);
	printf("%u\n",ID_L);
	*/
	/************************************************************************/
	/*If extended format frame is used, this field should be set to one.
	  If zero, standard format frame should be used./
	/************************************************************************/

	/*对扩展帧操作*/
	if(IDE==1)
	{
		/*printf("This is extend frame!\n");*/
		/*Contains a fixed recessive bit, used only in extended format. Should be set to one by the user for Tx buffers. It will be stored as received on the CAN bus for Rx buffers.*/
		int SRR=0;
		SRR=SRR*16;
		IDE=IDE*8;
		int i=(int)ID_H/8;
		int j=ID_H<<15;
		j=(int)(j/mypow(2,15));
		i=i<<5;
		ID_H=i|SRR|IDE|j;
		/*This bit is located in the least significant bit of the ID LOW word of the message buffer;0 = Data Frame, 1 = Remote Frame.*/
		int RTR=0;
		ID_L=(ID_L<<1)|RTR;
	}
	else
	{
		/*对标准帧操作*/
		if(IDE==0)
		{
			/*printf("This is standard frame!\n");*/
			/*This bit is located in the least significant bit of the ID LOW word of the message buffer;0 = Data Frame, 1 = Remote Frame.*/
			int RTR=0;
			ID_H=(ID_L<<1)|RTR;
			ID_H=ID_H<<4;
			/*Time Stamp.The ID LOW word, which is not needed for standard format, is used in a standard format buffer to store the 16-bit value of the free-running timer which is captured at the beginning of the identifier field of the frame on the CAN bus.*/
			int Time_S=1000;
			ID_L=Time_S;
		}
		else 
			return 0;
	}
	/*
	printf("%u\n",ID_H);
	printf("%u\n",ID_L);
	*/
	/*对选中的消息缓冲区进行初始化*/
	buffer[flag].ID_High=ID_H;
	buffer[flag].ID_Low=ID_L;
	buffer[flag].Reserved=0;
	/*对控制/状态位操作*/
	/*Time Stamp. Contains a copy of the high byte of the free running timer, which is captured at the beginning of the identifier field of the frame on the CAN bus.*/
	int TS=0;
	TS=TS<<8;
	int code=4;
	code=code<<4;
	buffer[flag].CS=buffer[flag].CS|TS|code;
	/*
	printf("%u\n",buffer[flag].CS);
	*/
	return flag;
}

int SendFrame(MessageBuffer MB)
{
	int i=0;
	while(buffer[i].CS!=0&&i<MAXBUFFER-2)
	{
		i++;
	}
	if(i==MAXBUFFER-2)
	{
		/*printf("Message buffer is full!\n");*/
		return 0;
	}
	/*存入空闲缓冲区*/
	buffer[i].CS=MB.CS;
	buffer[i].ID_High=MB.ID_High;
	buffer[i].ID_Low=MB.ID_Low;
	int j;
	for(j=0;j<MAXDATA;j++)
	{
		buffer[i].Data[j]=MB.Data[j];
	}
	/************************************************************************/
	/* 对所有的非空报文缓冲区进行冲裁                                       */
	/************************************************************************/
	int flag;
	/************************************************************************/
	/* 方法1:如果LBUF=1,最小的消息缓冲区号先发送                          */
	/************************************************************************/

	if(lbuf==1)
	{
		flag=MAXBUFFER-2;
		for(j=0;j<MAXBUFFER-2;j++)
		{
			if((buffer[j].CS&15)!=0)
			{
				if(j<flag)
					flag=j;
			}
		}
	}
	/************************************************************************/
	/* 方法2:如果LBUF=0,ID号最小的先发送                                  */
	/************************************************************************/
	if(lbuf==0)
	{
		int temp;
		temp=buffer[0].ID_High;
		for(j=0;j<MAXBUFFER-2;j++)
		{
			if((buffer[j].CS&15)!=0)
			{
				if(buffer[j].ID_High<temp)
				{
					temp=buffer[j].ID_High;
					flag=j;
				}
			}
		}
	}
	/************************************************************************/
	/* 将报文发送到连续缓冲区14                                             */
	/************************************************************************/
	buffer[14].CS=buffer[flag].CS;
	buffer[14].ID_High=buffer[flag].ID_High;
	buffer[14].ID_Low=buffer[flag].ID_Low;
	buffer[14].Reserved=buffer[flag].Reserved;
	for(i=0;i<MAXDATA;i++)
	{
		buffer[14].Data[i]=buffer[flag].Data[i];
	}
	ClearMB(buffer[flag]);
	return 1;
	

}

int RequestService()
{
	while(!IpduEmpty(Down_Front,Down_Rear))
	{
		MessageBuffer aaa;
		if(!InitSMB(aaa))
			return 0;
		SendFrame(aaa);
		if(!AckService(ServiceID))
		{
			//error
			return 0;
		}	
		Down_Front++;
	}
	return 1;
}

int ReceiveFrame(MessageBuffer MB)
{
	if((buffer[15].ID_High==MB.ID_High)&&(buffer[15].ID_Low==MB.ID_Low))
	{
		int i;
		for(i=0;i<MAXDATA;i++)
		{
			MB.Data[i]=buffer[15].Data[i];
		}
		ClearMB(buffer[15]);
		return 1;
	}
	return 0;
}

int DictateService(MessageBuffer MB)
{
	if (IpduFull(Up_Front,Up_Rear))
	{
		return 	0;
	}
	/*调用CAN接口,接收数据*/
	int i;
	for(i=0;i<Ipdu_Up[Up_Rear].DataLength;i++)
	{
		Ipdu_Up[Up_Rear].IpduData[i]=MB.Data[i];
	}
	return 1;
}



/*int main()
{
	MessageIdentifier MID=12;
	Alignment alignment=left;
	Type type=inter;
	Usage usage=send;
	int Queued=0;
	TX_Notifacation tx_Notifacation=none;
	Transmission transmission=direct;
	unsigned char date[MAXSIZE]="adafasgsgw";
	ApplicationDataRef *p;
	p=&date[0];
	int i=0;
	while(date[i]!='/0')
	{
		MESSAGE_DATE[i]=date[i];
		i++;
	}
	UWORD Length=i;
	int App_type=0;

	InitCOM(MID,Length,alignment,type,usage,Queued,tx_Notifacation,transmission,date);
	StartCOM(App_type);
	InitMessage(MID,p);
	if(Length!=StandardSIZE&&Length!=0)
	{
		SendDynamicMessage(MID,p,Length);
	}
	else 
		if(Length==0)
		{
			SendZeroMessage(MID);
		}
		else
		{
			SendMessage(MID,p);
		}
		
}


*/

⌨️ 快捷键说明

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