【引用】Linux-2.6.32.2内核在mini2440上的移植(十八)---将UART2更成普通串口驱动

本文详细介绍了如何将Linux-2.6.32.2内核在mini2440开发板上的UART2从红外通讯驱动调整为普通串口。主要涉及修改`s3c2440_uartcfgs`配置、添加头文件、初始化端口等步骤,并提供了一个简单的串口测试程序。
摘要由CSDN通过智能技术生成

移植环境(红色粗字体字修改后内容,蓝色粗体字为特别注意内容)

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 串口部分寄存器的说明,如下图:

\"Linux-2.6.32.2内核在mini2440上的移植(十八)---将UART2更成普通串口驱动

 【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\

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值