a20 uart0 test demo

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

//
char *dev_path = "/dev/ttyS0";
int uart0_fd;
  
//串口配置:
//端口(ttyS0)+ 波特率(115200)+ 数据位(8)
//+ 停止位(1)+效验位(none) + 流控制(none)
int set_opt(int fd)
{
	struct termios old_cfg, new_cfg;
	
	printf("uart0: set_opt fn.\n");
	
//stage#1
	//保存原先串口配置
	//tcgetattr(fd, &old_cfg);
	tcgetattr(fd, &new_cfg);
	
//stage#2
	//激活选项有CLOCAL和CREAD,用于本地连接和接收使用
	new_cfg.c_cflag |= (CLOCAL | CREAD);
	
	//设置波特率:115200
	cfsetispeed(&new_cfg, B115200);	//输入波特率
	cfsetospeed(&new_cfg, B115200);	//输出波特率
	
	//设置数据位,需使用掩码设置
	//设置数据位数:8位
	new_cfg.c_cflag &= ~CSIZE;
	new_cfg.c_cflag |= CS8;
	
	//设置奇偶校验:无校验(none)
	new_cfg.c_cflag &= ~PARENB;
	
	//设置停止位:1位
	new_cfg.c_cflag &= ~CSTOPB;
	
	//设置最少字符和等待时间
	new_cfg.c_cc[VTIME] = 0;
	new_cfg.c_cc[VMIN] = 0;
	
	//处理要写入的引用对象
	tcflush(fd,TCIFLUSH);

//stage#3
	//激活配置
	tcsetattr(fd, TCSANOW, &new_cfg);

	return 0;
}

/*thread*/	
char write_buf;
void *thread_uart0_write(void *arg)
{
	printf("write thread.\n");
	write_buf = 'a';
	
	while (1) {
		printf("write loop: ");
		printf("fd = %d; write_buf = %c.\n", uart0_fd, write_buf);
		write(uart0_fd, &write_buf, 1);
	}
	
	return NULL;
}

#if 1
char read_buf;
void *thread_uart0_read(void *arg)
{
	printf("read thread.\n");
	while (1) 
	{
		printf("read loop: ");
		read(uart0_fd, &read_buf, 1);
		printf("fd = %d; read_buf = %c.\n", uart0_fd, read_buf);
	}
	return NULL;
}
#endif

/*main*/
void main(int argc, char **argv)
{
	int ret;
	pthread_t write_pthread;
	pthread_t read_pthread;
	
    /*open*/
    uart0_fd = open(dev_path, (O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK));
    printf("uart0 open; fd = %d.\n", uart0_fd);

#if 0
	ret = fcntl(uart0_fd, F_SETFL, 0);
	if (ret < 0) {
		printf("uart0: fcntl fail.\n");
	}
#endif
	
    /*setting*/
	//ttyS0: 115200+8+1+none+none;
	set_opt(uart0_fd);
	
#if 1
    printf("posix thread write.\n");
	pthread_create(&read_pthread, NULL, thread_uart0_read, NULL);
	pthread_create(&write_pthread, NULL, thread_uart0_write, NULL);
	
	/*pthread_join*/
	pthread_join(read_pthread, NULL);
	pthread_join(write_pthread, NULL);
	
#else
	char buf;
	int i;
	printf("read.\n");
	for (i = 0; i <1000; i++) {
		printf("i = %d.\n", i);
		//ret = read(uart0_fd, &buf, 1);
		write(uart0_fd, 'a', 1);
		//printf("read: ret = %d; buf = %c.\n", ret, buf);
	}
#endif
	
    /*close*/
    close(uart0_fd);
    printf("uart0 close.\n");
	
    return;
}

/*EOF*/


LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)
LOCAL_C_INCLUDES:= external/uart0-test/
LOCAL_SRC_FILES:= uart0-test.c
LOCAL_MODULE := uart0-test
LOCAL_SHARED_LIBRARIES:= libcutils libutils 
LOCAL_MODULE_TAGS := optional

include $(BUILD_EXECUTABLE)











  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值