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

📄 serial.c

📁 东芝机器人处理器代码,sh2有时间学习很好!
💻 C
字号:
/******************************************************
 *  PHR-001 (PINO-class Humanoid Robot 001) Behavior Command Program
*
* serial.c
 *
 *       File Created: 28, Mar. 2001 by  F.Yamasaki
 *       Modified: 28, Jun. 2001     by  K.Endo
 *
 *  Copyright (C) 2001 Kitano Symbiotic Systems Project, 
 *  Japan Science and Technology Corp.
 *  
 *  This file is part of PHR-001.
 *  
 *  PHR-001 is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *  
 *  PHR-001 is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with PHR-001; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *********************************************************************/

#include	"sh7060s.h"
#include	"def.h"
#include	"serial.h"

void iniSCI2(ulong bps){
	uchar	brr;

	switch ( bps ) {
	case 9600:
		brr = 97;
		break;
	case 19200:
		brr = 48;
		break;
	case 38400:
		brr = 23;
		break;
	case 115200:
		brr = 7;
		break;
	default:
		brr = 23;
		break;
	}

	SCI2.SCSCR.BYTE = 0x00;

	SCI2.SCFCR.BYTE = 0x06;
	SCI2.SCFCR.BYTE = 0x00;

	SCI2.SCSMR.BYTE = 0x01;
	SCI2.SCBRR = brr;

	SCI2.SCSCR.BYTE = 0x00;

	SCI2.SC1SSR.WORD &= (~0x0060);

	SCI2.SC2SSR.BYTE = 0x00;
	SCI2.SC2SSR.BYTE &= 0x00;

	SCI2.SCSCR.BYTE			= 0x30;
}

void BufFlush(void){
	uchar tmp;
	while (RxREADY){
		tmp = SCI2.SCFRDR;
		ClearRxREADY;
	}
}

void wait(ulong n){
	while(n--);
}

void waits(ulong n){
	while( n-- && !RxREADY );
}

void s_putc(schar c){
	while(!TxREADY);
	SCI2.SCFTDR = c;
	ClearTxREADY;
}

void s_puts(schar* s){
	while(*s) s_putc(*s++);
}

uchar s_getc(void){
	ushort err;
	uchar c;

	while( (SCI2.SC1SSR.WORD & 0x0078) == 0);
	if (RxREADY){
		c = SCI2.SCFRDR;
		ClearRxREADY;
	}
	else{
		err = SCI2.SC1SSR.WORD;
		SCI2.SC1SSR.WORD &= ~0x0078;
		c = -1;
	}
	return( c );
}

void get_cmd(char *cmd){
	char *tmp;
	char c;
	tmp=cmd;
	for(c=0;c<10;c++){
		*tmp++=s_getc();
	}
	*tmp=0;
}

void s_putByte(uchar n){
	char s[]="00";
	int i=1;
	char m;

	while(n>0)
	{
		m=n&0x0f;
		if(m<10) s[i]='0'+m; else s[i]='A'-10+m;
		n>>=4;
		i--;
	}
	s_puts(s);
}

void s_putWord(uint n)
{
	char s[]="0000";
	int i=3;
	char m;

	while(n>0)
	{
		m=n&0x0f;
		if(m<10) s[i]='0'+m; else s[i]='A'-10+m;
		n>>=4;
		i--;
	}
	s_puts(s);
}

void s_putLong(ulong n)
{
	char s[]="00000000";
	int i=7;
	char m;

	while(n>0)
	{
		m=n&0x0f;
		if(m<10) s[i]='0'+m; else s[i]='A'-10+m;
		n>>=4;
		i--;
	}
	s_puts(s);
}

void s_putDec(uint n)
{
	int temp=n;
	int temp2=0;
	
	int jyou;
	int i=4;
	char s[]="00000";
	
	while(i>=0)
	{
		switch(i)
		{
		case 0:
			jyou=1;
			break;
		case 1:
			jyou=10;
			break;
		case 2:
			jyou=100;
			break;
		case 3:
			jyou=1000;
			break;
		case 4:
			jyou=10000;
			break;
		}
		temp2=temp/jyou;
		s[4-i]=(temp2+'0');
		i--;
		temp-=temp2*jyou;
	}
	s_puts(s);
}

void mdump(ulong addr,ulong len,uchar typ){
	int i;
	ulong tmp;
	ulong add;
	uchar	*add_byte;
	uint	*add_word;
	ulong	*add_long;
	char m;
	char s[]="0x00000000";
	tmp=(len%0x10);
	len -= tmp*0x10;
	i=9;
	
	add = addr;
	
	while(add>0){
		m=add&0x0f;
		if(m<10) s[i]='0'+m; else s[i]='A'-10+m;
		add>>=4;
		i--;
	}
	s_puts("\r\n");
	switch(typ){
		case 0:		//byte
			add_byte = (uchar *)addr;
			
			while(len){
				s_puts(s);
				s_puts(":");
				for(i=0;i<16;i++){
					len--;
					s_puts(" ");
					s_putByte(*add_byte++);
				}
				if(len%0x10 == 0){
					s_puts("\r\n");

					i=9;
					addr+=0x10;
					add=addr;
					while(add>0){
						m=add&0x0f;
						if(m<10) s[i]='0'+m; else s[i]='A'-10+m;
						add>>=4;
						i--;
					}
				}
			}
			break;
		case 1:		//word
			add_word = (uint *)add;
			while(len){
				s_puts(s);
				s_puts(":");
				for(i=0;i<8;i++){
					len--;
					s_puts(" ");
					s_putByte(*add_word++);
				}
				if(len%0x10 == 0){
					s_puts("\r\n");

					i=9;
					addr+=0x10;
					add=addr;
					while(add>0){
						m=add&0x0f;
						if(m<10) s[i]='0'+m; else s[i]='A'-10+m;
						add>>=4;
						i--;
					}
				}
			}
			break;
		case 2:		//long
			add_long = (ulong *)add;
			while(len){
				s_puts(s);
				s_puts(":");
				for(i=0;i<4;i++){
					len--;
					s_puts(" ");
					s_putLong(*add_long++);
				}
				if(len%0x10 == 0){
					s_puts("\r\n");

					i=9;
					addr+=0x10;
					add=addr;
					while(add>0){
						m=add&0x0f;
						if(m<10) s[i]='0'+m; else s[i]='A'-10+m;
						add>>=4;
						i--;
					}
				}
			}
			break;
	}
	s_puts("\r\n");
}

⌨️ 快捷键说明

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