Chinaunix首页 | 论坛 | 博客
  • 博客访问: 110537
  • 博文数量: 13
  • 博客积分: 489
  • 博客等级: 一等列兵
  • 技术积分: 175
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-12 10:06
文章分类

全部博文(13)

文章存档

2011年(13)

分类: LINUX

2011-02-14 14:48:48

TI平台使用SIRF GPS
1.底层驱动的实现
GPS厂商提供的库通过echo sysfs的方式控制gps的onoff和reset,这需要linux实现GPS的sysfs模型驱动,在driver文件夹中增加gps.c文件
(1)填充一个bin_attribute的结构体,驱动加载后就会在系统的/sys/module目录下创建sirf文件
  1. static struct bin_attribute sirf_mode_attr = {
  2.     .attr = {
  3.         .name = "sirf",
  4.         .owner = THIS_MODULE,
  5.         .mode = S_IRUGO | S_IWUGO,
  6.     },
  7.     .size = 0,
  8.     .read = readcmd,
  9.     .write = writecmd,
  10. };
(2)probe函数
  1. static int __init gps_probe(struct platform_device *pdev)
  2. {    
  3.     int ret;
  4.     ret = sysfs_create_bin_file(&(module_kset->kobj), &sirf_mode_attr);
  5.     if (ret) {
  6.             printk(KERN_ERR " Failed to create sys file\n");
  7.         return -ENOMEM;
  8.     }
  9.     return 0;
  10. }

(3)remove函数

  1. static int __init gps_remove(struct platform_device *pdev)
  2. {
  3.     sysfs_remove_bin_file(&(module_kset->kobj), &sirf_mode_attr);

  4.     return 0;
  5. }

(4)平台驱动的注册

  1. static struct platform_driver gps_driver = {
  2.     .probe        = gps_probe,
  3.     .remove        = gps_remove,
  4.     .driver        = {
  5.         .name    = "gps",
  6.     },
  7. };

  8. static int __init gps_init(void)
  9. {
  10.     return platform_driver_register(&gps_driver);
  11. }

  12. static void __exit gps_exit(void)
  13. {
  14.     platform_driver_unregister(&gps_driver);
  15. }

  16. late_initcall(gps_init);
  17. module_exit(gps_exit);

(5)write和read函数(read暂时为空,没有用到)

  1. static ssize_t readcmd(struct kobject *kobj,
  2.             struct bin_attribute *attr,
  3.             char *buf, loff_t off, size_t count)
  4. {
  5. }

  6. static ssize_t writecmd(struct kobject *kobj,
  7.             struct bin_attribute *attr,
  8.             char *buf, loff_t off, size_t count)
  9. {
  10.     char *tail;
  11.     int op;
  12.     op = simple_strtol(buf, &tail, 10);
  13.     switch(op) {
  14.         case 1:
  15.         case 2:
  16.             break;
  17.         case 3:
  18.             printk(KERN_BUG " Chip running...\n");
  19.             set_power_on();            
  20.             run_mode = RUN_MODE;             
  21.             break;
  22.         case 4:
  23.             printk(KERN_BUG " Chip stopped...\n");
  24.             set_power_off();
  25.             run_mode = STOP_MODE;
  26.             break;
  27.         case 5:
  28.             printk(KERN_BUG " Chip reset on high...\n");
  29.             set_reset(1);        
  30.             break;
  31.         case 6:
  32.             printk(KERN_BUG " Chip reset on low...\n");
  33.             set_reset(0);
  34.             break;
  35.         default:
  36.             printk(KERN_BUG " Unknown operation %d\n", op);
  37.             break;
  38.     }
  39.     return count;
  40. }

现在通过echo number > /sys/module/sirf就能控制GPS的power on/off和reset。

阅读(3546) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~