UART 接口测试

UART 接口测试

一、系统下串口测试

1.minicom
minicom -s

组合键 Ctrl+a 进入设置状态
按z打开帮助菜单,或者直接输入菜单对应的字母即可
S键:发送文件到目标系统中
W键:自动卷屏。当显示的内容超过一行之後,自动将後面的内容换行
C键:清除屏幕的显示内容
B键:浏览minicom的历史显示
X键:退出minicom

2.stty命令
stty -F /dev/ttyS0 -a  				查看串口参数
stty -F /dev/ttyS0 ispeed 115200 ospeed 115200 cs8 设置串口参数
cat /dev/ttyS0 						打印串口数据
echo “hello word” >  /dev/ttyS0   	向串口发送数据
3.microcom
microcom /dev/ttyS0   ctrl+x打断

二、串口UART测试程序

带传参波特率、奇偶校验、停止位、数据位

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <termios.h>
#include <errno.h>

typedef  unsigned int uint32_t ; 
static speed_t speed_arr[] = {B230400, B115200, B57600, B38400, B19200, B9600, B4800, B2400, B1200, B600, B300};
static int      name_arr[] = { 230400,  115200,  57600,  38400,  19200,  9600,  4800,  2400,  1200, 600,  300};

static int uart_fd;

/**
 *@brief  设置串口通信速率
 *@param  fd     类型 int  打开串口的文件句柄
 *@param  speed  类型 int  串口速度
 *@return  void
 */
static void set_speed(int fd, int speed){
	uint32_t  i; 
	struct termios opt;

	tcgetattr(fd, &opt); 
	tcflush(fd, TCIOFLUSH);     
	cfmakeraw(&opt);
#if 1
	for(i= 0; i < sizeof(speed_arr)/sizeof(speed_t); i++) { 
		if  (speed == name_arr[i]) {     
			printf("serial speed=%d ", speed);
			cfsetispeed(&opt, speed_arr[i]);  
			cfsetospeed(&opt, speed_arr[i]);   
		}    
	}
#else
    cfsetispeed(&opt, B115200);
    cfsetospeed(&opt, B115200);
#endif
    if (tcsetattr(fd, TCSANOW, &opt) == -1) {
            printf("tcsetattr(): %s", strerror(errno));
            return;
    }
	tcflush(fd,TCIOFLUSH);   
}

/**
 *@brief   设置串口数据位,停止位和效验位
 *@param  fd     类型  int  打开的串口文件句柄
 *@param  databits 类型  int 数据位   取值 为 7 或者8
 *@param  stopbits 类型  int 停止位   取值为 1 或者2
 *@param  parity  类型  int  效验类型 取值为N,E,O,,S
 */
static int set_parity(int fd, int speed, int databits,char *parity,int stopbits)
{ 
    set_speed(fd,speed);

	struct termios options; 
	if  ( tcgetattr( fd,&options)  !=  0) { 
		perror("SetupSerial 1");     
		return -1;  
	}
	options.c_cflag &= ~CSIZE; 
	switch (databits) /*设置数据位数*/
	{   
		case 7:		
			options.c_cflag |= CS7; 
			break;
		case 8:     
			options.c_cflag |= CS8;
			break;   
		default:    
			fprintf(stderr,"Unsupported data size\n"); 
			return -1;  
	}
	printf("databits=%d ",databits);
	switch (parity[0]) 
	{   
		case 'n':
		case 'N':    
			options.c_cflag &= ~PARENB;   /* Clear parity enable */
			options.c_iflag &= ~INPCK;     /* Enable parity checking */ 
			break;  
		case 'o':   
		case 'O':     
			options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/  
			options.c_iflag |= INPCK;             /* Disnable parity checking */ 
			break;  
		case 'e':  
		case 'E':   
			options.c_cflag |= PARENB;     /* Enable parity */    
			options.c_cflag &= ~PARODD;   /* 转换为偶效验*/     
			options.c_iflag |= INPCK;       /* Disnable parity checking */
			break;
		case 'S': 
		case 's':  /*as no parity*/   
			options.c_cflag &= ~PARENB;
			options.c_cflag &= ~CSTOPB;
			break;  
		default:   
			fprintf(stderr,"Unsupported parity\n");    
			return -1;  
	}  
	printf("parity=%c ",parity[0]);
	/* 设置停止位*/  
	switch (stopbits)
	{   
		case 1:    
			options.c_cflag &= ~CSTOPB;  
			break;  
		case 2:    
			options.c_cflag |= CSTOPB;  
			break;
		default:    
			fprintf(stderr,"Unsupported stop bits\n");  
			return -1; 
	} 
	printf("stopbits=%d\n",stopbits);
	/* Set input parity option */ 
	if (parity[0] != 'n')   
		options.c_iflag |= INPCK; 
	tcflush(fd,TCIFLUSH);
	//options.c_cc[VTIME] = 150; /* 设置超时15 seconds*/   
	//options.c_cc[VMIN] = 0; /* Update the options and do it NOW */
	if (tcsetattr(fd,TCSANOW,&options) != 0)   
	{ 
		perror("SetupSerial 3");   
		return -1;  
	} 
	return 0;  
}


static void uart_init(char * pserial_dev, int speed, int databits, char *parity, int stopbits)
{
	int ret = 0;
    printf("dev name is: %s \r\n", pserial_dev);
    uart_fd = open(pserial_dev, O_RDWR | O_NOCTTY);
    if (uart_fd < 0) {
        printf("open(): %s\r\n", strerror(errno));
        exit(1);
    }
    //set_parity(uart_fd, speed, 8, 'O', 1);
    ret = set_parity(uart_fd, speed, databits, parity, stopbits);
	
	if(ret) {
        printf("\noperating error!\r\n");
		close(uart_fd);
        exit(1);
	}
}

static int test_send(char * file_name)
{
    char buf[1024];
    int  fd, ret, tmp;
    fd = open(file_name, O_RDONLY);
	if(fd < 0) {
		printf("open %s faild!\n",file_name);
		return -1;
	}
    while(1) {
		ret = 0;
		tmp = 0;
		ret = read(fd, buf, sizeof(buf)); 
        if(ret <= 0) {
			//printf("read error!\n");
            break;
		}
		do {
			tmp += write(uart_fd, buf+tmp, ret-tmp);
		} while(tmp != ret);
    }
	close(fd);

    return 0;
}

static int test_receive(char * file_name)
{
	char buf[1024];
    int  fd, ret, tmp;
    fd = open(file_name,O_WRONLY|O_TRUNC|O_CREAT,0666);
	if(fd < 0) {
		printf("open %s faild!\n",file_name);
		return -1;
	}
    while(1) {
		ret = 0;
		tmp = 0;
		ret = read(uart_fd, buf, sizeof(buf)); 
        if(ret <= 0) {
			printf("read error!\n");
            break;
		}
		do {
			tmp += write(fd, buf+tmp, ret-tmp);
		} while(tmp != ret);
    }
	close(fd);
    return 0;
}

void print_info(void)
{
	int i;
	printf("\n./test_uart /dev/ttyX speed databits parity stopbits file-name transport\n\n");
	printf("	/dev/ttyX	/dev/ttySn or /dev/ttyUSBn\n");
	printf("	speed		");
	for(i=0;name_arr[i];i++) {
		printf("%d ",name_arr[i]);
	}
	printf("\n");
	printf("	databits	8	7\n");
	printf("	parity		n	o	e\n");
	printf("	stopbits	1	2\n");
	printf("	file-name	./1.log\n");
	printf("	transport	send	receive\n");	
	printf("\n	E.g# ./test_uart /dev/ttyS1 115200 8 n 1 1.log receive\n\n");
}

int main(int argc,char *argv[])
{
	char * parity;
	int speed, databits, stopbits;
	if(argc != 8) {
		print_info();
        exit(1);
	}
	parity	 = argv[4];
	speed    = atoi(argv[2]);
	databits = atoi(argv[3]);
	stopbits = atoi(argv[5]);
	
	uart_init(argv[1],speed,databits,parity,stopbits);
	
	if(!strcmp(argv[7],"send")) {
		test_send(argv[6]);
	} else if(!strcmp(argv[7],"receive")) {
		test_receive(argv[6]);
	}else{
		print_info();
	}
	close(uart_fd);
	return 0;
}

1.测试接受
	将串口ttyS1接受到的数据保存在1.log文件里
	./uart_text /dev/ttyS1 115200 8 n 1 1.log receive
2.测试发送
	将文件1.log内容发送到串口ttyS1上
	./uart_text /dev/ttyS1 115200 8 n 1 1.log send  
  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
STM32F103是一款基于ARM Cortex-M3内核的微控制器,具有广泛的应用场景。在项目开发中,可以通过UART接口实现BootLoader功能,以便在不依赖外部编程器的情况下更新应用程序。以下是基于UART接口的STM32F103 BootLoader实现过程: 1. 确定BootLoader启动方式 在开发BootLoader之前,需要确定何时启动BootLoader。可以通过按下按键或检测特定引脚的电平来触发BootLoader启动。在本例中,我们将使用GPIO引脚(PA0)作为BootLoader启动引脚。 2. 实现串口通信 为了与计算机通信,需要使用串口通信实现。在STM32F103中,可以使用USART1或USART2实现串口通信。在本例中,我们将使用USART1。 首先,需要配置USART1的GPIO引脚。USART1_TX引脚配置为输出模式,USART1_RX引脚配置为输入模式。然后,需要配置USART1的波特率、数据位、停止位和校验位等参数。 3. 实现BootLoader功能 BootLoader的主要功能是从外部存储器中加载应用程序,并将其写入内部Flash中。在本例中,我们将使用SD卡作为外部存储器,并通过SPI接口与STM32F103进行通信。 需要实现以下BootLoader功能: 1. 检测SD卡并初始化SPI接口。 2. 读取应用程序文件并将其写入内部Flash中。 3. 跳转到应用程序的入口地址并执行。 在编写BootLoader时,需要考虑以下问题: 1. 如何检测SD卡是否存在? 2. 如何读取应用程序文件? 3. 如何将应用程序写入内部Flash中? 4. 如何跳转到应用程序的入口地址? 在本例中,我们将使用FatFs文件系统库来读取SD卡中的应用程序文件,并使用STM32F103的Flash编程库将应用程序写入内部Flash中。跳转到应用程序的入口地址可以通过设置堆栈指针和程序计数器来实现。 4. 测试BootLoader 在完成BootLoader的编写后,需要测试其功能。可以通过以下步骤测试BootLoader: 1. 将SD卡插入STM32F103的SD卡插槽中。 2. 按下BootLoader启动按键或将BootLoader启动引脚拉低。 3. 使用串口调试工具连接STM32F103,并打开BootLoader程序。 4. 选择要更新的应用程序文件并将其写入SD卡。 5. 发送命令启动应用程序更新。 6. BootLoader将读取SD卡中的应用程序文件,并将其写入内部Flash中。 7. BootLoader将跳转到应用程序的入口地址并执行。 通过以上步骤可以测试BootLoader的功能,确保其可以正确地从外部存储器中加载应用程序并更新系统。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大、猫

感谢支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值