移植環境(紅色粗字型字為修改後內容,藍色粗體字為特別注意內容)
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 <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);
}
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
核心移植的學習和研究進行到先暫告一段了,後面等到串連的硬體準備好之後進行實際測試一下。