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

📄 drv_serial.c

📁 Atmel公司的AT91 ARM7平台上的串口驱动程序
💻 C
📖 第 1 页 / 共 3 页
字号:
	if(dwnl_state!= DWNL_INQUIRY && dwnl_state != DWNL_CMD)
		return;
	
	/*led status showed due to seqno*/
	if(seqno==1)
		led_status=0;
	else
		set_chnl_led(0, led_status++, 0);		
	led_status=led_status%8;
	set_chnl_led(0, led_status, 1);

	/*if continue down load data*/
	if(!(CMD_DLD_END == cmd)) /* serial process download not end */
	{
		packet_size = *(ptr -2)-DATA_LEN_OFFSET;
		if(dwnl_state == DWNL_INQUIRY)
		{
			dwnl_state = DWNL_CMD;
			seqno = 1;
			pos = 0;
		}
		
		/*save the packet start address to start_addr*/
		start_addr = *(ptr + 3);
		start_addr = (start_addr << 8) + *(ptr + 2) - 0x4000;

		/*get the sequence number*/
		tmp=*(ptr + 1);	
#ifdef DEBUG
		usart_print(DBG_PORT_ID,"receive packet sequence: ");
		usart_print(DBG_PORT_ID,byte2decstr(tmp));		
		usart_print(DBG_PORT_ID,"\n");
#endif
		/*if receive error packet*/
		if(seqno < tmp)
		{
#ifdef DEBUG
			usart_print(DBG_PORT_ID,"seqno < tmp\n");
#endif	
			response_NACK_dwnl_cmd(seqno);
		}
		/*receive correct packet*/
		else
		if(seqno==tmp)
		{			
			//should add code to write the data to sram
			dataptr = ptr + 4;
			memcpy((UINT8 *)(APP_DNLD_BASE + start_addr),dataptr,packet_size);
			
			pos += packet_size;
			response_ACK_dwnl_cmd(seqno);			
			seqno++;
		}
		/*it is received before, just nack with the one we want*/		
		/* fail,need to redownload */
		else
		{
#ifdef DEBUG
			usart_print(DBG_PORT_ID,"seqno > tmp\n");
#endif	
			response_NACK_dwnl_cmd(seqno);
		}
	}
	/*serial process download end*/
	else 
	{
		total_seq = *(ptr + 1);
		if(total_seq == (seqno - 1))
		{
			response_ACK_dwnl_cmd(total_seq);
		}
		download_flag=0;
		dwnl_state = DWNL_IDLE;
		seqno = 1;
		pos = 0;
#ifdef DEBUG
		usart_print(DBG_PORT_ID,"PCSI firmware dl end, ");
#endif
		ret = verchksum_chk(CPUSW_TYPE);
		if (ret != OK)
		{
			*boot_ver_ptr = 0;
#ifdef DEBUG
			usart_print(DBG_PORT_ID,"version check fail.\n");
#endif

			/* turn off the activate plane, and turn off the deactivate plane */
			for (i=0;i<MAX_TOTAL_PORTS;i++)
			{
				set_chnl_led(DEACTIVATE, i, LED_OFF);
				set_chnl_led(ACTIVATE,  i, LED_OFF);
			}
		}
		else
		{
			*boot_ver_ptr = 1;
#ifdef DEBUG
			usart_print(DBG_PORT_ID,"version check ok.\n");
#endif

			/* turn on the activate plane, and turn on the deactivate plane */
			for (i=0;i<MAX_TOTAL_PORTS;i++)
			{
				set_chnl_led(DEACTIVATE, i, LED_ON);
				set_chnl_led(ACTIVATE,  i, LED_ON);
			}
		}
		
#ifdef TEST
		AT91F_PIO_CfgOutput( AT91C_BASE_PIO, RST_CTL ) ;
#endif
		reset(); /* reset system */
	}
	return;
}

/*response down inquiry message from csc*/
void serial_process_dwnl_inq(void)
{
#ifdef DEBUG
	usart_print(DBG_PORT_ID,"enter serial_process_dwnl_inq\n");
#endif

	if(0 == download_flag)	/*need not download*/
	{
		response_NACK_dwnl_inq();
	}
	else	/*need download*/
	{
		dwnl_state = DWNL_INQUIRY;
		seqno = 1;
		pos = 0;

		response_ACK_dwnl_inq();
	}

	return;	
}

/****************************************************************************
**
**  Function Name: serial_process_unknown_cmd
**
**  Description:
**		operate port
**		called by serial_receive_process() in basic loop
**
**  Argument        Type     IO           Description
**  --------------- -------- -- ---------------------------------
**  port             	UINT8      	I   		port number(0,1)
**  cmd              	UINT8      	I   		cmd
**  Return Value: N/A
**
****************************************************************************/
void serial_process_unknown_cmd (UINT8 cmd,UINT8 length, UINT8  *ptr)
{
	unknown_cmd++;

#ifdef DEBUG
	usart_print(DBG_PORT_ID,"enter serial_process_unknown_cmd\n");
#endif

	return;
}

/****************************************************************************
**
**  Function Name: serial_receive_process
**
**  Description:
**		process the received message  
**		called by main in basic loop 
**
**  Argument        Type     IO           Description
**  --------------- -------- -- ---------------------------------
**  N/A
**
**  Return Value: N/A
**
****************************************************************************/
void serial_receive_process_boot(void)
{   
	UINT8  ch1, ch2; 
	UINT8  *ptr;
	INT16  length;

	if(!received) 
		return;	
		
	length = serial_buf[out_rcv_ptr++];  /*  length field for whole packet */
	slot_addr=serial_buf[out_rcv_ptr+length];
#ifdef DEBUG
	dump_buffer1("Slot:",&slot_addr, 1);//dump slot addr
	dump_buffer1("  receive msg:",&serial_buf[out_rcv_ptr-1], 10);//dump msg content
#endif
	while(length>0)
	{
		l3_received++;
		ch2 = ch1 = serial_buf[out_rcv_ptr++];	/* first byte */
		ch1 >>= 4;

		switch(ch1)
		{
			case MUTI_BYTE_MSG: 	/* multiple byte message */
				ch2 = serial_buf[out_rcv_ptr++]; 	/* length field for the cmd*/
				length -= ch2;
				length -=2;							/* adjust the length */
				ch1 = serial_buf[out_rcv_ptr];   	/* command ID for RTU test */
				ptr = &serial_buf[out_rcv_ptr];	 	/* get address */
				ptr++;
				out_rcv_ptr += ch2;

				switch(ch1)
				{
					case CMD_DLD_CMD:
#ifdef DEBUG
						usart_print(DBG_PORT_ID,"receive dl cmd\n");
#endif
						serial_process_dwnl_cmd(ch1, ptr);				
						break;

					case CMD_DLD_END:
#ifdef DEBUG
					   usart_print(DBG_PORT_ID,"receive dl end cmd\n");
#endif
						serial_process_dwnl_cmd(ch1, ptr);				
						break;
						
					default:
						serial_process_unknown_cmd(ch1,ch2,ptr);
						break;
				}
				break;
				
			case DL_INQ_MSG:
			   /* system command message */
			   /* no need to adjust out_rcv_ptr since it's a single byte command */
			   length--;
			   if(CMD_DL_INQ == ch2)
				   serial_process_dwnl_inq();
				break;
				
			default:
			   length--;
				serial_process_unknown_cmd(ch1,ch2,NULL);
				break;
		}
   }	/* end of while(length)	*/
   received--;
   inc_rcv_ptr(out_rcv_ptr);
}

/****************************************************************************
**
**  Function Name: serial_receive_process
**
**  Description:
**		process the received message  
**		called by main in basic loop 
**
**  Argument        Type     IO           Description
**  --------------- -------- -- ---------------------------------
**  N/A
**
**  Return Value: N/A
**
****************************************************************************/
void serial_receive_process_ap(void)
{   
	UINT8  *ptr;
	UINT32  length;

	cli_cmd_process();
	if(!received) 
		return;	
		
	length = serial_buf[out_rcv_ptr++];  /*  length field for whole packet */
	slot_addr=serial_buf[out_rcv_ptr+length];
   if(slot_addr != slot_no) /* only process the messages which are sent to self board */
 	{
		out_rcv_ptr += length;
	   received--;
	   inc_rcv_ptr(out_rcv_ptr);
	   return;
 	}
   
#ifdef DEBUG
	dump_buffer1("Slot:",&slot_addr, 1);//dump slot addr
	dump_buffer1("  receive msg:",&serial_buf[out_rcv_ptr-1], 10);//dump msg content
#endif

	ptr = &serial_buf[out_rcv_ptr];
	serial_process_cmd(length, ptr);
	
	out_rcv_ptr += length;
   received--;
   inc_rcv_ptr(out_rcv_ptr);
}

void cli_cmd_process(void)
{
	static UINT8 output=0;
	INT8 chch;

	if(usart_getchar(DBG_PORT_ID,&chch)==OK)
	{
		switch(chch)
		{
			case 'm':
			case 'M':
#ifdef DEBUG
				dump_buffer();
#endif
				break;
				
			case 'o':
			case 'O':
				output=!output;
				if(output)
				{
					set_print_flag(1);
					usart_print(DBG_PORT_ID,"\nDebug trace enabled!!!\n");
				}
				else
				{
					usart_print(DBG_PORT_ID,"\nDebug trace disabled!!!\n");
					set_print_flag(0);
				}				
				break;

			default:
				usart_print(DBG_PORT_ID,"\nUnknown CLI command\n");
				break;
		}
	}
}

#ifdef DEBUG

INT8 tempstring[10];
INT8 *byte2hexstr(UINT8 value)
{
	int2str(value,16,2,tempstring);
	return tempstring;
}

INT8 *byte2decstr(UINT8 value)
{
	int2str(value,10,0,tempstring);
	return tempstring;
}

void	dump_buffer(void)
{
	UINT8 i,j;
	usart_print(DBG_PORT_ID,"\nRCV QUE\n");
	for(i=0;i<RCV_QUE;i++)
	{
		if(i==(in_rcv_ptr/255))
			usart_print(DBG_PORT_ID,">");
		else
			usart_print(DBG_PORT_ID," ");
		if(i==(out_rcv_ptr/255))
			usart_print(DBG_PORT_ID,"<");
		else
			usart_print(DBG_PORT_ID," ");
		usart_print(DBG_PORT_ID,byte2hexstr(i+1));
		usart_print(DBG_PORT_ID,"||");
		for(j=0;j<15;j++)
		{
			usart_print(DBG_PORT_ID,byte2hexstr(serial_buf[i*(RCV_BUF+1)+j]));
			usart_print(DBG_PORT_ID,"  ");
		}
		usart_print(DBG_PORT_ID,"\n");
	}
	usart_print(DBG_PORT_ID,"XMT QUE\n");
	for(i=0;i<XMT_QUE;i++)
	{
		if(i==(xmt_in_ptr/255)-RCV_QUE)
			usart_print(DBG_PORT_ID,">");
		else
			usart_print(DBG_PORT_ID," ");
		if(i==(xmt_out_ptr/255)-RCV_QUE)
			usart_print(DBG_PORT_ID,"<");
		else
			usart_print(DBG_PORT_ID," ");
	
		usart_print(DBG_PORT_ID,byte2hexstr(i+1));
		usart_print(DBG_PORT_ID,"||");
		for(j=0;j<15;j++)
		{
			usart_print(DBG_PORT_ID,byte2hexstr(serial_buf[RCV_QUE*(RCV_BUF+1)+i*(XMT_BUF+1)+j]));
			usart_print(DBG_PORT_ID,"  ");
		}
		usart_print(DBG_PORT_ID,"\n");
	}
	/******************download state***************************/
	usart_print(DBG_PORT_ID,"download state: ");
	switch(dwnl_state)
	{
		case DWNL_IDLE:
			usart_print(DBG_PORT_ID,"DWNL_IDLE");
			break;
		case DWNL_INQUIRY:
			usart_print(DBG_PORT_ID,"DWNL_INQUIRY");
			break;
		case DWNL_CMD:
			usart_print(DBG_PORT_ID,"DWNL_CMD");
			break;
		default:
			usart_print(DBG_PORT_ID,"Unknown Download status-");
			usart_print(DBG_PORT_ID,byte2hexstr(dwnl_state));
			break;
	}
	/******************download flag***************************/
	if(download_flag)
		usart_print(DBG_PORT_ID,"\nMulti-slot download mode\n");
	else
		usart_print(DBG_PORT_ID,"\nNormal process mode\n");

	/****RCV FULL count, to see if it has fulled***********/
	usart_print(DBG_PORT_ID,"************************************\nRCV_FULL counter:");
	usart_print(DBG_PORT_ID,byte2hexstr(rcv_full));
	usart_print(DBG_PORT_ID,"\n");
	/****XMT FULL count, to see if it has fulled***********/
	usart_print(DBG_PORT_ID,"XMT_FULL counter:");
	usart_print(DBG_PORT_ID,byte2hexstr(xmt_full));
	usart_print(DBG_PORT_ID,"\n");
}

void dump_buffer1(INT8 *str,UINT8 *ptr,UINT8 len)
{
	UINT8 j;
	usart_print(DBG_PORT_ID,str);
	for(j=0;j<len;j++)
	{
		if(*ptr==0)
			usart_print(DBG_PORT_ID,"0 ");
		else
			usart_print(DBG_PORT_ID,byte2hexstr(*ptr));
		usart_print(DBG_PORT_ID,"  ");
		ptr++;
	}
	usart_print(DBG_PORT_ID,"\n");
}

#endif

⌨️ 快捷键说明

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