Chinaunix首页 | 论坛 | 博客
  • 博客访问: 87058
  • 博文数量: 66
  • 博客积分: 110
  • 博客等级: 民兵
  • 技术积分: 410
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-18 09:34
文章分类
文章存档

2012年(66)

我的朋友

分类:

2012-07-26 11:21:54

REAL开发板 485 使用方法

实现方法对比

485 驱动可以在应用层每次发送之前拉高DE管脚。发送完成之后再拉低。但是这样应用程序显得有点臃肿。并且每次发送完后要延时较长时间确保数据全部发出。效率教低。
在驱动层实现效率较高。操作起来和普通串口一样。我们推荐用这种方法。

移植篇
只需要修改一个文件即 driver/serial/samsung.c
在其中加上对DE管脚的控制。
主要修改如下三个函数。在其中对485管脚进行控制
static void s3c24xx_serial_start_tx(struct uart_port *port) //使能发送

static void s3c24xx_serial_stop_tx(struct uart_port *port)//使能接收

static int s3c24xx_serial_startup(struct uart_port *port)//配置DE管脚


附上修改后的内容供参考。注意port->line == 0或者 port->line == 3根据板子不同设置不同
210使用的串口0   6410 使用的串口3
并且6410 使用函数gpio_set_value(S3C64XX_GPC(4), 0)控制DE电平(具体参考6410的samsung.c)
static void s3c24xx_serial_start_tx(struct uart_port *port)
{
        struct s3c24xx_uart_port *ourport = to_ourport(port);

#ifdef REAL210_485
        if (port->line == 0) {
                gpio_direction_output(S5PV210_GPH2(0), 1);
        //        printk("en tx\n");
               
        }
#endif


        if (!tx_enabled(port)) {
                if (port->flags & UPF_CONS_FLOW)
                        s3c24xx_serial_rx_disable(port);

                enable_irq(ourport->tx_irq);
                tx_enabled(port) = 1;
        }
}


static unsigned int s3c24xx_serial_tx_empty(struct uart_port *port);
static void s3c24xx_serial_stop_tx(struct uart_port *port)
{
        struct s3c24xx_uart_port *ourport = to_ourport(port);
        int i=0;
        if (tx_enabled(port)) {
                disable_irq_nosync(ourport->tx_irq);
                tx_enabled(port) = 0;
                if (port->flags & UPF_CONS_FLOW)
                        s3c24xx_serial_rx_enable(port);
        }
#ifdef REAL210_485
        if (port->line == 0) {
                while(!s3c24xx_serial_tx_empty(port))
                {
                        mdelay(3);
                        if(i++>16)
                        break;
                }
       
                gpio_direction_output(S5PV210_GPH2(0), 0);
        //        printk("en rx\n");
        }
#endif

}



static int s3c24xx_serial_startup(struct uart_port *port)
{
        struct s3c24xx_uart_port *ourport = to_ourport(port);
        int ret;

        dbg("s3c24xx_serial_startup: port=%p (%08lx,%p)\n",
            port, port->mapbase, port->membase);

        rx_enabled(port) = 1;
#ifdef REAL210_485
        if (port->line == 0) {
        gpio_free(S5PV210_GPH2(0));
        gpio_request(S5PV210_GPH2(0), "GPH2");       
        s3c_gpio_cfgpin(S5PV210_GPH2(0), S3C_GPIO_OUTPUT);
        gpio_direction_output(S5PV210_GPH2(0), 0);
        }
#endif


        ret = request_irq(ourport->rx_irq, s3c24xx_serial_rx_chars, 0,
                          s3c24xx_serial_portname(port), ourport);

        if (ret != 0) {
                printk(KERN_ERR "cannot get irq %d\n", ourport->rx_irq);
                return ret;
        }

        ourport->rx_claimed = 1;

        dbg("requesting tx irq...\n");

        tx_enabled(port) = 1;

        ret = request_irq(ourport->tx_irq, s3c24xx_serial_tx_chars, 0,
                          s3c24xx_serial_portname(port), ourport);

        if (ret) {
                printk(KERN_ERR "cannot get irq %d\n", ourport->tx_irq);
                goto err;
        }

        ourport->tx_claimed = 1;

        dbg("s3c24xx_serial_startup ok\n");

        /* the port reset code should have done the correct
         * register setup for the port controls */

        return ret;

err:
        s3c24xx_serial_shutdown(port);
        return ret;
}

测试篇

安装附件中的apk。将libserial_port.so放到/system/lib中并将485接口通过232转485接口连接到电脑。在电脑上打开串口调试助手。双方可以收发收据。
linux下将测试文件编译。并将执行文件放到/usr/bin/下。
说明:实际测试过程中使用14元一个的HEXIN 485 转232经常出现抽风,有时出现波特率高于19200 就会乱码,有时工作一会后接收到乱码,从新插拔后会恢复正常。但是后来我将两块开发板直接连接使用115200的波特率没有问题。如果使用过程中出现误码 建议使用独立供电的485转232模块或者通过降低波特率解决,我测试19200完全没有问题。
       
使用485发送后直接去读如果出现每次都能读到0x00的情况。调了两天最后看到了《RS-485_guide.pdf》第22页对这个问题的描述。在RO上加10K的上拉电阻问题解决。

附件使用篇
将对应内核的samsung.c文件拷贝到driver/serial/替换掉原来的samsung.c。然后重新编译内核。
对应目录下有测试程序。使用前注意查看说明。
阅读(438) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~