关于Linux下AT91SAM9261的RS485驱动

AT91SAM9261和ATMEL其它型号的ARM处理器使用高度相似的USART外设,因 而驱动程序也具有高度的相似性,而且USART硬件支持485模式,用RTS信号作为485的收发控制。在使用中,只要将其置为485模式,其余的操作和 普通的232操作一模一样,RTS的翻转是自动的。
我的板子上使用USART0扩展了一个485接口,而Linux原有串口驱动只支持普通的232模式,为了在Linux下方便地使用485接口,需要对原来的驱动程序稍加改动。

有关的文件
arch/arm/mach-at91/目录下:
at91sam9261_devices.c中的相关函数(不用改):
at91_add_device_serial()
并在此文件中确认相关的引脚已被正确配置,在这里关心的是USART0的TXD,RXD和RTS引脚
static inline void configure_usart0_pins(void)
{
 at91_set_A_periph(AT91_PIN_PC8, 1);  /* TXD0 */
 at91_set_A_periph(AT91_PIN_PC9, 0);  /* RXD0 */
 at91_set_A_periph(AT91_PIN_PC10, 0);  /* RTS0 */
 at91_set_A_periph(AT91_PIN_PC11, 0);  /* CTS0 */
}

这是另一个相关的文件,也不用改
board-sam9261ek.c

include/asm-arm/arch-at91/目录下
gpio.h中有相关引脚和at91_set_A_periph的原形声明
extern int __init_or_module at91_set_GPIO_periph(unsigned pin, int use_pullup);
extern int __init_or_module at91_set_A_periph(unsigned pin, int use_pullup);
extern int __init_or_module at91_set_B_periph(unsigned pin, int use_pullup);
extern int __init_or_module at91_set_gpio_input(unsigned pin, int use_pullup);
extern int __init_or_module at91_set_gpio_output(unsigned pin, int value);
extern int __init_or_module at91_set_deglitch(unsigned pin, int is_on);
extern int __init_or_module at91_set_multi_drive(unsigned pin, int is_on);

接下来需要确定在哪里修改USART0的配置。在芯片手册中,可以找到相关的寄存器名,以此为关键字搜索源码树。
在Linux源码树下搜索:
grep -r US_MR ./
找到:drivers/serial/atmel_serial.c
其中有对串口的初始化代码,在文件中查找UART_PUT_MR,找到atmel_set_termios()函数
该函数是用户程序设置串口参数时必须调用的
在其中判断;当操作USART0时,设置为485模式:
if(AT91SAM9261_BASE_US0 == (port)->membase){  // in include/asm-arm/arch-at91/at91sam9261.h
  mode &= 0xfffffff0;
  mode |= ATMEL_US_USMODE_RS485;  // in drivers/serial/atmel_serial.h
}
将上面的代码插在UART_PUT_MR(port, mode)之前即可

 

 

 

 

下面是从外国的一个论坛上找到的:关于uart配置成rs485模式的

 

to change each USART in RS-485 mode, add this line in linux/drivers/serial/atmel_serial.c , in function atmel_set_termios ,
just before the instruction "UART_PUT_MR(port, mode) ;" that configures the USART mode.

mode |= ATMEL_US_USMODE_RS485; //Take effect for all USART that you configure

or if you want filter for a specific USART, i used the dedicated IRQ number :

if (port->irq == AT91SAM9260_ID_US2) // USART2 is a rs485
{
mode |= ATMEL_US_USMODE_RS485;
}

I don't know if it's the best solution but it works very well.

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值