硬件设计清楚后,然后,我们来设计软件部分,首先需要在板级初始化文件 arch/arm/mach-mx27/mx27mdk27v0.c中,做如下初始化
1.根据硬件设计来分配串口的资源数据
/*!
* The serial port definition structure. The fields contain:
* {UART, CLK, PORT, IRQ, FLAGS}
*/
static struct plat_serial8250_port serial_platform_data[] = {
{
.membase = (void __iomem *)(CS4_BASE_ADDR_VIRT), //驱动中必须用内核虚拟地址来访问寄存器
.mapbase = (unsigned long)(CS4_BASE_ADDR), //寄存器实际的物理地址
.irq = IOMUX_TO_IRQ(MX27_PIN_SSI1_TXDAT), //获得该GPIO引脚所对应的中断编号,mx27的每个GPIO口都可以配置为中断模式
.uartclk = 14745600, //根据实际外接的晶振来设置
.regshift = 1, //根据硬件设计和WEIM口数据总线位数来决定
.iotype = UPIO_MEM, //按字节方式读写寄存器
.flags = UPF_BOOT_AUTOCONF | UPF_SKIP_TEST,
/*.pm = serial_platform_pm, */
},
{
.membase = (void __iomem *)(CS4_BASE_ADDR_VIRT + 0x10),
.mapbase = (unsigned long)(CS4_BASE_ADDR + 0x10),
.irq = IOMUX_TO_IRQ(MX27_PIN_SSI1_RXDAT),
.uartclk = 14745600,
.regshift = 1,
.iotype = UPIO_MEM,
.flags = UPF_BOOT_AUTOCONF | UPF_SKIP_TEST,
/*.pm = serial_platform_pm, */
},
{},
};
/*!
* REVISIT: document me
*/
static struct platform_device serial_device = {
.name = "serial8250",
.id = 0,
.dev = {
.platform_data = serial_platform_data,
},
};
2.初始化相关各个引脚的功能和初始化状态,复位串口芯片,添加和注册一个平台驱动设备
static int __init mxc_init_extuart(void)
{
int ret = 0;
//将MX27_PIN_SSI1_FS 做GPIO用,做串口复位引脚
ret = gpio_request_mux(MX27_PIN_SSI1_FS, GPIO_MUX_GPIO);
if (ret) {
printk(KERN_ERR "Request MUX SSI1_FS failed.\n");
}
//设置为输出
mxc_set_gpio_direction(MX27_PIN_SSI1_FS, 0);
//给串口芯片一个复位信号,重新复位芯片
mxc_set_gpio_dataout(MX27_PIN_SSI1_FS, 1);
udelay(1000);
mxc_set_gpio_dataout(MX27_PIN_SSI1_FS, 0);
//将 MX27_PIN_SSI1_TXDAT 做串口1的接收中断引脚
ret = gpio_request_mux(MX27_PIN_SSI1_TXDAT, GPIO_MUX_GPIO);
if (ret) {
printk(KERN_ERR "Request MUX SSI1_TXDAT failed.\n");
}
//设置为输入
mxc_set_gpio_direction(MX27_PIN_SSI1_TXDAT, 1);
//禁止上拉
gpio_set_puen(MX27_PIN_SSI1_TXDAT, 0);
set_irq_type(IOMUX_TO_IRQ(MX27_PIN_SSI1_TXDAT), IRQT_RISING);
//将 MX27_PIN_SSI1_CLK 做片选输出引脚也就是原理图上的GPIO_CS4
ret = gpio_request_mux(MX27_PIN_SSI1_CLK, GPIO_MUX_GPIO);
if (ret) {
printk(KERN_ERR "Request MUX SSI1_CLK failed.\n");
}
//设置为输出
mxc_set_gpio_direction(MX27_PIN_SSI1_CLK, 0);
mxc_set_gpio_dataout(MX27_PIN_SSI1_CLK, 1);
//将 MX27_PIN_SSI1_RXDAT 做串口2的接收中断引脚
ret = gpio_request_mux(MX27_PIN_SSI1_RXDAT, GPIO_MUX_GPIO);
if (ret) {
printk(KERN_ERR "Request MUX SSI1_RXDAT failed.\n");
}
//做输入用
mxc_set_gpio_direction(MX27_PIN_SSI1_RXDAT, 1);
gpio_set_puen(MX27_PIN_SSI1_RXDAT, 0);
set_irq_type(IOMUX_TO_IRQ(MX27_PIN_SSI1_RXDAT), IRQT_RISING);
//注册平台设备
ret = platform_device_register(&serial_device);
if (ret < 0) {
pr_info("Error: register external uart failure\n");
}
return ret;
}
}
文章评论(0条评论)
登录后参与讨论