移植环境(红色粗字体字为修改后内容,蓝色粗体字为特别注意内容)
1,主机环境:VMare下CentOS 5.5 ,1G内存。
2,集成开发环境:Elipse IDE
3,编译编译环境: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.08
6,linux 版本:linux-2.6.32.2
7,参考文章:
嵌入式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
#include
#include
//需要添加的头文件
#include
#include
#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
# include
# include
# include
# include
# include
# include
# include
# include
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);
}
else
TtySpeed = 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 max
if (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;
} else
EscKeyCount = 0;
}
}
}
ExitLabel:
if (tcsetattr(TtyFd, TCSANOW, &BackupTtyAttr) < 0)
Error("Unable to set tty");
return 0;
}
在开发板上进行测试之前,要注意如下问题:
(1)串口对应的内核/dev/下的设备,在串口终端执行:
[root@mini2440 /]# ls /dev
s3c2410_serial0 ttyt3
s3c2410_serial1 ttyt4
s3c2410_serial2
可以看到UART0 、UART1 和UART2 分别对应的设备是s3c2410_serial0、s3c2410_seria10和s3c2410_serial2。
(2)mini2440 开发板UART0作为串口终端的通讯接口已经由RS232接口引出,而UART1和UART2 并没有做成从RS232 端口引出,而是分别做成对应的COM1和CON3 排针引出了,测试时需要外界RS232转换电路。
因此,需要修改上面的代码,使之能打开对应的设备。修改如下:
int DeviceSpeed = B115200;
int TtySpeed = B115200;
int ByteBits = CS8;
const char *DeviceName = "/dev/s3c2410_serial1"; //mini2440's uart1 <---> s3c2410_serial1
const char *TtyName = "/dev/tty";
在终端中进入到codetest目录,然后执行:
[root@localhost codetest]# ls
adc_test backlight_test buttons_test.c led pwm_test.c
adc_test.c backlight_test.c comtest.c led.c tstest
adc_test.c~ buttons_test i2c pwm_test tstest.c
[root@localhost codetest]# arm-linux-gcc -o comtest comtest.c
[root@localhost codetest]# cp comtest /nfsboot/nfs
将生成的可执行目标文件pwm_test复制到与开发板共享的nfsboot/nfs中,当COM1端口和主机COM口连接好之后就可以在开发板的命令行终端进入到/mnt/nfs目录下执行:./comtest 进行测试了。
[root@mini2440 nfs]#ls
adc_test buttons_test led tstest
backlight_test comtest pwm_test yesterday.mp3
bigworld.wav i2c test1.wav
[root@mini2440 nfs]#./comtest
内核移植的学习和研究进行到先暂告一段了,后面等到连接的硬件准备好之后进行实际测试一下。