您的位置:首页 > 运维架构 > Linux

拨开迷雾-单片机和嵌入式LINUX开发的那点事儿(下)

2010-04-30 15:12 363 查看
2.1.2 是否通用
有些单片机厂家也给客户提供了大量的驱动程序,比如USB
HOST驱动程序,这可以让客户很容易就可以在它的上面编写程序读写U盘。但是客户写的这些程序,只能在这种芯片、这个驱动程序上使用;更换另一种芯片
后,即使芯片公司也提供了驱动程序,但是接口绝对不一样,客户又得重新编写应用程序。

基于操作系统的驱动程序要遵循统一的接口,比如对于不同的芯片的USB HOST驱动,它们都要向上提供一个相同的数据结构,在里面实现了各自的USB操作。
下面是S3C2410/S3C2440的USB驱动向上层提供的数据结构:
static const struct hc_driver ohci_s3c2410_hc_driver = {

.description = hcd_name,

.product_desc = "S3C24XX OHCI",

.hcd_priv_size = sizeof(struct ohci_hcd),
/*

* generic hardware linkage

*/

.irq = ohci_irq,

.flags = HCD_USB11 | HCD_MEMORY,
/*

* basic lifecycle operations

*/

.start = ohci_s3c2410_start,

.stop = ohci_stop,

.shutdown = ohci_shutdown,
/*

* managing i/o requests and associated device resources

*/

.urb_enqueue = ohci_urb_enqueue,

.urb_dequeue = ohci_urb_dequeue,

.endpoint_disable = ohci_endpoint_disable,
/*

* scheduling support

*/

.get_frame_number = ohci_get_frame,
/*

* root hub support

*/

.hub_status_data = ohci_s3c2410_hub_status_data,

.hub_control = ohci_s3c2410_hub_control,

.hub_irq_enable = ohci_rhsc_enable,

#ifdef CONFIG_PM

.bus_suspend = ohci_bus_suspend,

.bus_resume = ohci_bus_resume,

#endif

.start_port_reset = ohci_start_port_reset,

};
下面是ATMEL公司的ARM芯片的USB驱动向上层提供的数据结构:
/*-------------------------------------------------------------------------*/
static const struct hc_driver ohci_at91_hc_driver = {

.description = hcd_name,

.product_desc = "AT91 OHCI",

.hcd_priv_size = sizeof(struct ohci_hcd),
/*

* generic hardware linkage

*/

.irq = ohci_irq,

.flags = HCD_USB11 | HCD_MEMORY,
/*

* basic lifecycle operations

*/

.start = ohci_at91_start,

.stop = ohci_stop,

.shutdown = ohci_shutdown,
/*

* managing i/o requests and associated device resources

*/

.urb_enqueue = ohci_urb_enqueue,

.urb_dequeue = ohci_urb_dequeue,

.endpoint_disable = ohci_endpoint_disable,
/*

* scheduling support

*/

.get_frame_number = ohci_get_frame,
/*

* root hub support

*/

.hub_status_data = ohci_hub_status_data,

.hub_control = ohci_hub_control,

.hub_irq_enable = ohci_rhsc_enable,

#ifdef CONFIG_PM

.bus_suspend = ohci_bus_suspend,

.bus_resume = ohci_bus_resume,

#endif

.start_port_reset = ohci_start_port_reset,

};
基于通用性,即使是你自己写的Linux驱动,简单到只是点亮一个LED,基于“通用性”,这个驱动也要向上提供统一的接口。下面是单片机LED驱动程序和Linux下的LED驱动程序的部分代码。
单片机LED驱动程序:
void led_init(void)

{

GPBCON = GPB5_out; // 将LED对应的GPB5引脚设为输出

}
void led_on(void)

{

GPBDAT &= ~(1<<5);

}
void led_off(void)

{

GPBDAT |= (1<<5);

}
Linux的LED驱动程序:

#define DEVICE_NAME "leds" /* 加载模式后,执行”cat /proc/devices”命令看到的设备名称 */

#define LED_MAJOR 231 /* 主设备号 */
/* 应用程序执行ioctl(fd, cmd, arg)时的第2个参数 */

#define IOCTL_LED_ON 0

#define IOCTL_LED_OFF 1
/* 用来指定LED所用的GPIO引脚 */

static unsigned long led_table [] = {

S3C2410_GPB5,

S3C2410_GPB6,

S3C2410_GPB7,

S3C2410_GPB8,

};
/* 用来指定GPIO引脚的功能:输出 */

static unsigned int led_cfg_table [] = {

S3C2410_GPB5_OUTP,

S3C2410_GPB6_OUTP,

S3C2410_GPB7_OUTP,

S3C2410_GPB8_OUTP,

};
/* 应用程序对设备文件/dev/leds执行open(...)时,

* 就会调用s3c24xx_leds_open函数

*/

static int s3c24xx_leds_open(struct inode *inode, struct file *file)

{

int i;
for (i = 0; i < 4; i++) {

// 设置GPIO引脚的功能:本驱动中LED所涉及的GPIO引脚设为输出功能

s3c2410_gpio_cfgpin(led_table[i], led_cfg_table[i]);

}

return 0;

}
/* 应用程序对设备文件/dev/leds执行ioclt(...)时,

* 就会调用s3c24xx_leds_ioctl函数

*/

static int s3c24xx_leds_ioctl(

struct inode *inode,

struct file *file,

unsigned int cmd,

unsigned long arg)

{

if (arg > 4) {

return -EINVAL;

}
switch(cmd) {

case IOCTL_LED_ON:

// 设置指定引脚的输出电平为0

s3c2410_gpio_setpin(led_table[arg], 0);

return 0;
case IOCTL_LED_OFF:

// 设置指定引脚的输出电平为1

s3c2410_gpio_setpin(led_table[arg], 1);

return 0;
default:

return -EINVAL;

}

}
/* 这个结构是字符设备驱动程序的核心

* 当应用程序操作设备文件时所调用的open、read、write等函数,

* 最终会调用这个结构中指定的对应函数

*/

static struct file_operations s3c24xx_leds_fops = {

.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */

.open = s3c24xx_leds_open,

.ioctl = s3c24xx_leds_ioctl,

};
/*

* 执行insmod命令时就会调用这个函数

*/

static int __init s3c24xx_leds_init(void)

{

int ret;
/* 注册字符设备

* 参数为主设备号、设备名字、file_operations结构;

* 这样,主设备号就和具体的file_operations结构联系起来了,

* 操作主设备为LED_MAJOR的设备文件时,就会调用s3c24xx_leds_fops中的相关成员函数

* LED_MAJOR可以设为0,表示由内核自动分配主设备号

*/

ret = register_chrdev(LED_MAJOR, DEVICE_NAME, &s3c24xx_leds_fops);

if (ret < 0) {

printk(DEVICE_NAME " can't register major number/n");

return ret;

}
printk(DEVICE_NAME " initialized/n");

return 0;

}
/*

* 执行rmmod命令时就会调用这个函数

*/

static void __exit s3c24xx_leds_exit(void)

{

/* 卸载驱动程序 */

unregister_chrdev(LED_MAJOR, DEVICE_NAME);

}
/* 这两行指定驱动程序的初始化函数和卸载函数 */

module_init(s3c24xx_leds_init);

module_exit(s3c24xx_leds_exit);
2.2. 应用程序开发的区别
2.2.1 对于不带操作系统的应用编程,应用程序和驱动程序之间的间隔并不明显。
举个例子,要在LCD上显示字母“a”,在单片机上的做法是:

① 事先在Flash上保存“a”的点阵数据,假设它的象素大小是8x8,那么这个点阵大小就是8x8=64 bits,即8字节

② 应用程序读取这64bit数据,逐个象素地在LCD上描点
相对的,基于操作系统的应用编程,就不需要懂得硬件知识,执行一个简单的“echo a > /dev/tty1”就可以在LCD上显示“a”了。
2.2.2 不带操作系统的应用程序,可借用的软件资源很少;
带操作系统的应用程序,网上各种开源的软件很多。
比如要做一个播放器,在不带操作系统上实现会非常困难;如果是在Linux下,有现成的。
2.2.3 不带操作系统的应用程序,各个任务是串行执行的;
带操作系统的应用程序,各个任务是并行执行的。
2.2.4 不带操作系统的应用程序,一旦发生程序错误,整个系统将崩溃
带操作系统的应用程序,即使发生了程序错误,操作系统本身并不会崩溃
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: