串口驱动学习
- 格式:doc
- 大小:156.00 KB
- 文档页数:15
Linux-2.6.32.2内核在mini2440上的移植(十八)---将UART2更成普通串口驱动移植环境(红色粗字体字为修改后内容,蓝色粗体字为特别注意内容)1,主机环境:VMare下CentOS 5.5 ,1G内存。
2,集成开发环境:Elipse IDE3,编译编译环境:arm-linux-gcc v4.4.3,arm-none-linux-gnueabi-gcc v4.5.1。
4,开发板:mini2440,2M nor flash,128M nand flash。
5,u-boot版本:u-boot-2009.086,linux 版本:linux-2.6.32.27,参考文章:嵌入式linux应用开发完全手册,韦东山,编著。
Mini2440 之Linux 移植开发实战指南【1】硬件原理S3C2440 芯片具有3 个串口:UART0,1,2,我们下载的Linux-2.6.32.2 已经具备完善的UART0,1 的驱动,但对UART2 却用作了红外通讯(Irda),因此我们需要把UART2 驱动稍微调整一下,以便作为普通串口来用。
先看看S3C2440 串口部分寄存器的说明,如下图:【2】修改平台配置代码接下来我们修改内核中关于UART2 的配置,打开arch/arm/mach-s3c2440/mach-mini2440.c文件,定位到112行附近,找到mini2440_uartcfgs[],如下红色代码为修改后的:static struct s3c2410_uartcfg mini2440_uartcfgs[] __initdata = {[0] = {.hwport = 0,.flags = 0,.ucon = 0x3c5,.ulcon = 0x03,.ufcon = 0x51,},[1] = {.hwport = 1,.flags = 0,.ucon = 0x3c5,.ulcon = 0x03,.ufcon = 0x51,},/* IR port */[2] = {.hwport = 2,.flags = 0,.ucon = 0x3c5,.ulcon = 0x03, //0x43,/* 把UART2 改为普通串口*/.ufcon = 0x51,}};再修改串口所使用的端口初始化,打开linux-2.6.32.2/drivers/serial/samsung.c,定位到55行附近,先加入所需头文件,如下:#include <mach/hardware.h>#include <mach/map.h>#include <plat/regs-serial.h>//需要添加的头文件#include <linux/gpio.h>#include <mach/regs-gpio.h>#include "samsung.h"/* UART name and device definitions */然后再定位到435 行左右,添加如下红色部分代码:dbg("s3c24xx_serial_startup ok\n");/* the port reset code should have done the correct* register setup for the port controls *///串口2 对应的端口初始化if (port->line == 2){s3c2410_gpio_cfgpin(S3C2410_GPH(6), S3C2410_GPH6_TXD2);s3c2410_gpio_pullup(S3C2410_GPH(6), 1);s3c2410_gpio_cfgpin(S3C2410_GPH(7), S3C2410_GPH7_RXD2);s3c2410_gpio_pullup(S3C2410_GPH(7), 1);}return ret;err:s3c24xx_serial_shutdown(port);return ret;}这样,我们就完成了UART2 的修改。
【3】测试串口内核源代码根目录执行:make uImage,把生成的uImage复制到/nfsboot/kernel,然后重启开发板。
为了测试该驱动程序,我们还需要编写一个简单的测试程序,在友善官方提供的光盘中已经提供了该测试程序的源代码,它位于\linux 示例代码\examples\comtest目录中,文件名为:comtest.c。
将其复制到主机/root/linux-test/codetest目录下,下面是其中的代码:# include <stdio.h># include <stdlib.h># include <termio.h># include <unistd.h># include <fcntl.h># include <getopt.h># include <time.h># include <errno.h># include <string.h>static void Error(const char *Msg){fprintf (stderr, "%s\n", Msg);fprintf (stderr, "strerror() is %s\n", strerror(errno));exit(1);}static void Warning(const char *Msg){fprintf (stderr, "Warning: %s\n", Msg);}static int SerialSpeed(const char *SpeedString){int SpeedNumber = atoi(SpeedString);# define TestSpeed(Speed) if (SpeedNumber == Speed) return B##Speed TestSpeed(1200);TestSpeed(2400);TestSpeed(4800);TestSpeed(9600);TestSpeed(19200);TestSpeed(38400);TestSpeed(57600);TestSpeed(115200);TestSpeed(230400);Error("Bad speed");return -1;}static void PrintUsage(void){fprintf(stderr, "comtest - interactive program of comm port\n");fprintf(stderr, "press [ESC] 3 times to quit\n\n");fprintf(stderr, "Usage: comtest [-d device] [-t tty] [-s speed] [-7] [-c] [-x] [-o] [-h]\n"); fprintf(stderr, " -7 7 bit\n");fprintf(stderr, " -x hex mode\n");fprintf(stderr, " -o output to stdout too\n");fprintf(stderr, " -c stdout output use color\n");fprintf(stderr, " -h print this help\n");exit(-1);}static inline void WaitFdWriteable(int Fd){fd_set WriteSetFD;FD_ZERO(&WriteSetFD);FD_SET(Fd, &WriteSetFD);if (select(Fd + 1, NULL, &WriteSetFD, NULL, NULL) < 0) { Error(strerror(errno));}}int main(int argc, char **argv){int CommFd, TtyFd;struct termios TtyAttr;struct termios BackupTtyAttr;int DeviceSpeed = B115200;int TtySpeed = B115200;int ByteBits = CS8;const char *DeviceName = "/dev/ttySAC1";const char *TtyName = "/dev/tty";int OutputHex = 0;int OutputToStdout = 0;int UseColor = 0;opterr = 0;for (;;){int c = getopt(argc, argv, "d:s:t:7xoch");if (c == -1)break;switch(c){case 'd':DeviceName = optarg;break;case 't':TtyName = optarg;break;case 's':if (optarg[0] == 'd'){DeviceSpeed = SerialSpeed(optarg + 1);}else if (optarg[0] == 't'){TtySpeed = SerialSpeed(optarg + 1);}elseTtySpeed = DeviceSpeed = SerialSpeed(optarg);break;case 'o':OutputToStdout = 1;break;case '7':ByteBits = CS7;break;case 'x':OutputHex = 1;break;case 'c':UseColor = 1;break;case '?':case 'h':default:PrintUsage();}}if (optind != argc)PrintUsage();CommFd = open(DeviceName, O_RDWR, 0);if (CommFd < 0)Error("Unable to open device");if (fcntl(CommFd, F_SETFL, O_NONBLOCK) < 0)Error("Unable set to NONBLOCK mode");memset(&TtyAttr, 0, sizeof(struct termios));TtyAttr.c_iflag = IGNPAR;TtyAttr.c_cflag = DeviceSpeed | HUPCL | ByteBits | CREAD | CLOCAL; TtyAttr.c_cc[VMIN] = 1;if (tcsetattr(CommFd, TCSANOW, &TtyAttr) < 0)Warning("Unable to set comm port");TtyFd = open(TtyName, O_RDWR | O_NDELAY, 0);if (TtyFd < 0)Error("Unable to open tty");TtyAttr.c_cflag = TtySpeed | HUPCL | ByteBits | CREAD | CLOCAL;if (tcgetattr(TtyFd, &BackupTtyAttr) < 0)Error("Unable to get tty");if (tcsetattr(TtyFd, TCSANOW, &TtyAttr) < 0)Error("Unable to set tty");for (;;){unsigned char Char = 0;fd_set ReadSetFD;void OutputStdChar(FILE *File){char Buffer[10];int Len = sprintf(Buffer, OutputHex ? "%.2X " : "%c", Char);fwrite(Buffer, 1, Len, File);}FD_ZERO(&ReadSetFD);FD_SET(CommFd, &ReadSetFD);FD_SET( TtyFd, &ReadSetFD);#define max(x,y) ( ((x) >= (y)) ? (x) : (y) )if (select(max(CommFd, TtyFd) + 1, &ReadSetFD, NULL, NULL, NULL) < 0) {Error(strerror(errno));}#undef maxif (FD_ISSET(CommFd, &ReadSetFD)){while (read(CommFd, &Char, 1) == 1){WaitFdWriteable(TtyFd);if (write(TtyFd, &Char, 1) < 0){Error(strerror(errno));}if (OutputToStdout){if (UseColor)fwrite("\x1b[01;34m", 1, 8, stdout);OutputStdChar(stdout);if (UseColor)fwrite("\x1b[00m", 1, 8, stdout);fflush(stdout);}}}if (FD_ISSET(TtyFd, &ReadSetFD)){while (read(TtyFd, &Char, 1) == 1){static int EscKeyCount = 0;WaitFdWriteable(CommFd);if (write(CommFd, &Char, 1) < 0){Error(strerror(errno));}if (OutputToStdout){if (UseColor)fwrite("\x1b[01;31m", 1, 8, stderr);OutputStdChar(stderr);if (UseColor)fwrite("\x1b[00m", 1, 8, stderr);fflush(stderr);}if (Char == '\x1b'){EscKeyCount ++;if (EscKeyCount >= 3)goto ExitLabel;} elseEscKeyCount = 0;}}}ExitLabel:if (tcsetattr(TtyFd, TCSANOW, &BackupTtyAttr) < 0) Error("Unable to set tty");return 0;}在开发板上进行测试之前,要注意如下问题:(1)串口对应的内核/dev/下的设备,在串口终端执行:[root@mini2440 /]# ls /devs3c2410_serial0ttyt3s3c2410_serial1ttyt4s3c2410_serial2可以看到UART0 、UART1 和UART2 分别对应的设备是s3c2410_serial0、s3c2410_seria10和s3c2410_serial2。