GPS NMEA 0183 4.10协议/GPS Linux串口驱动

来源:互联网 发布:js给span的value赋值 编辑:程序博客网 时间:2024/05/16 16:20
   NMEA 0183是美国国家海洋电子协会(National Marine Electronics Association)为海用电子设备制定的标准格式。现在已经成为GPS导航设备统一的RTCM(Radio Technical Commission for Maritime services)标准协议。 

  下图为
GYM-1010-B 北斗导航模块,同时支持北斗二代 B1、GPS L1 信号 支持协议  NMEA 0183 4.10 版本,默认波特率9600bps,数据位8bit,无校验位,停止位1bit;默认输出频率1HZ
图片
 
  基本上接上天线,供电引脚电压正常的话,就可以通过串口读取GPS数据了(GPS芯片会源源不断的输出数据)。所以GPS编程最主要的是解析数据,要解析数据就得了解协议格式。北斗导航模块输出 6 条 NAME 协议,分别为 GPGGA、GPGLL、GPGSA、GPGSV、GPRMC 和 GPVTG 等 6 条协议。

 一、下面详细学习这六种不同的输出协议的数据格式。

(1) $GPGGA GPS定位信息)

  协议格式:

  1. $GNGGA,<1>,<2>,<3>,<4>,<5>,<6>,<7>,<8>,<9>,<10>,<11>,<12>,<13>,<14>*hh<CR><LF>  

  样例数据:

  1. $GPGGA,161229.487,3723.2475,N,12158.3416,W,1,07,1.0,9.0,M, , ,,0000*18  

  协议格式详细分析:

(2) $GPGLL (地理定位信息)

  协议格式:

  1. $GPGLL,<1>,<2>,<3>,<4>,<5>,<6>*hh<CR><LF>  

  样例数据:

  1. $GPGLL,3723.2475,N,12158.3416,W,161229.487,A*2C  

  协议格式详细分析:

(3) $GPGSA (当前卫星信息)

  协议格式:

  1. $GPGSA,<1>,<2>,<3>,<4>,<5>,<6>,<7>,<8>,<9>,<10>,<11>,<12>,<13>,<14>,<15>,<16>,<17>*hh<CR><LF>  

  样例数据:

  1. $GPGSA,A,3,07,02,26,27,09,04,15, , , , , ,1.8,1.0,1.5*33  

  协议格式详细分析:

(4) $GPGSV(可见卫星信息)

  协议格式:

  1. $GPGSV, <1>,<2>,<3>,<4>,<5>,<6>,<7>,...,<4>,<5>,<6>,<7>*hh<CR><LF>  

  样例数据:

  1. $GPGSV,2,1,07,07,79,048,42,02,51,062,43,26,36,256,42,27,27,138,42*71  
  2. $GPGSV,2,2,07,09,23,313,42,04,19,159,41,15,12,041,42*41  

  需要注意的是这里的样例数据有2条,这是因为当前可见卫星一共有7个,但是每条语句最多包括四颗卫星的信息,所以分成了2条语句。每颗卫星的信息有四个数据项,即:<4>(卫星编号)、<5>(卫星仰角)、<6>(卫星方位角)、<7>(信噪比)。

  协议格式详细分析(只分析第1条样例数据语句):

(5) $GPRMC(最简定位信息)

  协议格式:

  1. $GPRMC,<1>,<2>,<3>,<4>,<5>,<6>,<7>,<8>,<9>,<10>,<11>*hh<CR><LF>  

  样例数据:

  1. $GPRMC,161229.487,A,3723.2475,N,12158.3416,W,0.13,309.62,120598,,*10  

  协议格式详细分析:

(6) $GPVTG(地面速度信息)

  协议格式:

  1. $GPVTG,<1>,<2>,<3>,<4>,<5>,<6>,<7>,<8>*hh<CR><LF>  

  样例数据:

  1. $GPVTG,309.62,T, ,M,0.13,N,0.2,K*6E  

  协议格式详细分析:

  有了上面对NMEA0183协议的详细学习,剩下的就是串口编程了。无论是通过单片机,还是Windows/WinCE/Linux系统,编写串口程序把这些数据读取到都是比较容易的,剩下就是通过c++那些查找算法函数,或者MFC CString字符串的相关函数进行解析就OK了。

二、Linux串口驱动分析设计

A、核心数据结构
  串口驱动有3个核心数据结构,它们都定义在<#include linux/serial_core.h>
   1、uart_driver
   uart_driver包含了串口设备名、串口驱动名、主次设备号、串口控制台(可选)等信息,还封装了tty_driver(底层串口驱动无需关心tty_driver)。

图片

 
 2、uart_port

uart_port用于描述串口端口的I/O端口或I/O内存地址、FIFO大小、端口类型、串口时钟等信息。实际上,一个uart_port实例对应一个串口设备.

   struct uart_port {

    spinlock_t             lock;           /* 串口端口锁 */

    unsigned int           iobase;         /* IO端口基地址 */

    unsigned char __iomem *membase;        /* IO内存基地址,经映射(如ioremap)后的IO内存虚拟基地址 */

    unsigned int           irq;            /* 中断号 */

    unsigned int           uartclk;        /* 串口时钟 */

    unsigned int           fifosize;       /* 串口FIFO缓冲大小 */

    unsigned char          x_char;         /* xon/xoff字符 */

    unsigned char          regshift;       /* 寄存器位移 */

    unsigned char          iotype;         /* IO访问方式 */

    unsigned char          unused1;


#define UPIO_PORT        (0)               /* IO端口 */

#define UPIO_HUB6        (1)

#define UPIO_MEM         (2)               /* IO内存 */

#define UPIO_MEM32       (3)

#define UPIO_AU          (4)               /* Au1x00 type IO */

#define UPIO_TSI         (5)               /* Tsi108/109 type IO */

#define UPIO_DWAPB       (6)               /* DesignWare APB UART */

#define UPIO_RM9000      (7)               /* RM9000 type IO */


    unsigned int        read_status_mask;  /* 关心的Rx error status */

    unsigned int        ignore_status_mask;/* 忽略的Rx error status */

    struct uart_info      *info;           /* pointer to parent info */

    struct uart_icount     icount;         /* 计数器 */


    struct console        *cons;           /* console结构体 */

#ifdef CONFIG_SERIAL_CORE_CONSOLE

    unsigned long         sysrq;           /* sysrq timeout */

#endif


    upf_t                 flags;


#define UPF_FOURPORT         ((__force upf_t) (1 << 1))

#define UPF_SAK              ((__force upf_t) (1 << 2))

#define UPF_SPD_MASK         ((__force upf_t) (0x1030))

#define UPF_SPD_HI           ((__force upf_t) (0x0010))

#define UPF_SPD_VHI          ((__force upf_t) (0x0020))

#define UPF_SPD_CUST         ((__force upf_t) (0x0030))

#define UPF_SPD_SHI          ((__force upf_t) (0x1000))

#define UPF_SPD_WARP         ((__force upf_t) (0x1010))

#define UPF_SKIP_TEST        ((__force upf_t) (1 << 6))

#define UPF_AUTO_IRQ         ((__force upf_t) (1 << 7))

#define UPF_HARDPPS_CD       ((__force upf_t) (1 << 11))

#define UPF_LOW_LATENCY      ((__force upf_t) (1 << 13))

#define UPF_BUGGY_UART       ((__force upf_t) (1 << 14))

#define UPF_MAGIC_MULTIPLIER ((__force upf_t) (1 << 16))

#define UPF_CONS_FLOW        ((__force upf_t) (1 << 23))

#define UPF_SHARE_IRQ        ((__force upf_t) (1 << 24))

#define UPF_BOOT_AUTOCONF    ((__force upf_t) (1 << 28))

#define UPF_FIXED_PORT       ((__force upf_t) (1 << 29))

#define UPF_DEAD             ((__force upf_t) (1 << 30))

#define UPF_IOREMAP          ((__force upf_t) (1 << 31))


#define UPF_CHANGE_MASK      ((__force upf_t) (0x17fff))

#define UPF_USR_MASK         ((__force upf_t) (UPF_SPD_MASK|UPF_LOW_LATENCY))


    unsigned int             mctrl;        /* 当前的moden设置 */

    unsigned int             timeout;      /* character-based timeout */        

    unsigned int             type;         /* 端口类型 */

    const struct uart_ops   *ops;          /* 串口端口操作函数集 */

    unsigned int             custom_divisor;

    unsigned int             line;         /* 端口索引 */

    resource_size_t          mapbase;      /* IO内存物理基地址,可用于ioremap */

    struct device           *dev;          /* 父设备 */

    unsigned char            hub6;         /* this should be in the 8250 driver */

    unsigned char            suspended;

    unsigned char            unused[2];

    void                    *private_data; /* 端口私有数据,一般为platform数据指针 */

};

   
uart_iconut为串口信息计数器,包含了发送字符计数、接收字符计数等。在串口的发送中断处理函数和接收中断处理函数中,我们需要管理这些计数。


struct uart_icount {
    __u32    cts;
    __u32    dsr;
    __u32    rng;
    __u32    dcd;
    __u32    rx
;      /* 发送字符计数 */
    __u32    tx;      /* 接受字符计数 */
    __u32    frame;   /* 帧错误计数 */
    __u32    overrun; /* Rx FIFO溢出计数 */
    __u32    parity /* 帧校验错误计数 */
    __u32    brk;     /* break计数 */
    __u32    buf_overrun;
};
 

uart_info有两个成员在底层串口驱动会用到:xmit和tty。用户空间程序通过串口发送数据时,上层驱动将用户数据保存在xmit;而串口发送中断处理函数就是通过xmit获取到用户数据并将它们发送出去。串口接收中断处理函数需要通过tty将接收到的数据传递给行规则层。
 
/* uart_info实例仅在串口端口打开时有效,它可能在串口关闭时被串口核心层释放。因此,在使用uart_port的uart_info成员时必须保证串口已打开。底层驱动和核心层驱动都可以修改uart_info实例。
 * This is the state information which is only valid when the port
 * is open; it may be freed by the core driver once the device has
 * been closed. Either the low level driver or the core can modify
 * stuff here.
 */
struct uart_info {
    struct tty_struct     *tty;
    struct circ_buf        xmit;
    uif_t                  flags;

/*
 * Definitions for info->flags. These are _private_ to serial_core, and
 * are specific to this structure. They may be queried by low level drivers.
 */
#define UIF_CHECK_CD        ((__force uif_t) (1 << 25))
#define UIF_CTS_FLOW        ((__force uif_t) (1 << 26))
#define UIF_NORMAL_ACTIVE    ((__force uif_t) (1 << 29))
#define UIF_INITIALIZED        ((__force uif_t) (1 << 31))
#define UIF_SUSPENDED        ((__force uif_t) (1 << 30))

    int                     blocked_open;

    struct tasklet_struct   tlet;

    wait_queue_head_t       open_wait;
    wait_queue_head_t       delta_msr_wait;
};


3、uart_ops

uart_ops涵盖了串口驱动可对串口设备进行的所有操作。

/*

 * This structure describes all the operations that can be

 * done on the physical hardware.

 */

struct uart_ops {

    unsigned int (*tx_empty)(struct uart_port *); /* 串口的Tx FIFO缓存是否为空 */

    void         (*set_mctrl)(struct uart_port *, unsigned int mctrl); /* 设置串口modem控制 */

    unsigned int (*get_mctrl)(struct uart_port *); /* 获取串口modem控制 */

    void         (*stop_tx)(struct uart_port *); /* 禁止串口发送数据 */

    void         (*start_tx)(struct uart_port *); /* 使能串口发送数据 */

    void         (*send_xchar)(struct uart_port *, char ch);/* 发送xChar */

    void         (*stop_rx)(struct uart_port *); /* 禁止串口接收数据 */

    void         (*enable_ms)(struct uart_port *); /* 使能modem的状态信号 */

    void         (*break_ctl)(struct uart_port *, int ctl); /* 设置break信号 */

    int          (*startup)(struct uart_port *); /* 启动串口,应用程序打开串口设备文件时,该函数会被调用 */

    void         (*shutdown)(struct uart_port *); /* 关闭串口,应用程序关闭串口设备文件时,该函数会被调用 */

    void         (*set_termios)(struct uart_port *, struct ktermios *new, struct ktermios*old); /* 设置串口参数 */

    void         (*pm)(struct uart_port *, unsigned int state,

             unsigned int oldstate); /* 串口电源管理 */

    int          (*set_wake)(struct uart_port *, unsigned int state); /*  */

    const char  *(*type)(struct uart_port *); /* 返回一描述串口类型的字符串 */

    void         (*release_port)(struct uart_port *); /* 释放串口已申请的IO端口/IO内存资源,必要时还需iounmap */

    int          (*request_port)(struct uart_port *); /* 申请必要的IO端口/IO内存资源,必要时还可以重新映射串口端口 */

    void         (*config_port)(struct uart_port *, int); /* 执行串口所需的自动配置 */

    int          (*verify_port)(struct uart_port *, struct serial_struct *); /* 核实新串口的信息 */

    int          (*ioctl)(struct uart_port *, unsigned int, unsigned long); /* IO控制 */

};

 
B、串口驱动API
 
1、uart_register_driver
 /* 功能:    uart_register_driver用于将串口驱动uart_driver注册到内核(串口核心层)中,通常在模块初始化函数调用该函数。
 * 参数 drv:要注册的uart_driver
 * 返回值:  成功,返回0;否则返回错误码
 */
int uart_register_driver(struct uart_driver *drv);
2、uart_unregister_driver
 /* 功能:    uart_unregister_driver用于注销我们已注册的uart_driver,通常在模块卸载函数调用该函数
 * 参数 drv:要注销的uart_driver
 * 返回值:  成功,返回0;否则返回错误码
 */
void uart_unregister_driver(struct uart_driver *drv);
3、uart_add_one_port
 /* 功能:    uart_add_one_port用于为串口驱动添加一个串口端口,通常在探测到设备后(驱动的设备probe方法)调用该函数
 * 参数 drv:串口驱动
 *      port:要添加的串口端口
 * 返回值:  成功,返回0;否则返回错误码
 */
int uart_add_one_port(struct uart_driver *drv, struct uart_port *port);
4、uart_remove_one_port
 /* 功能:     uart_remove_one_port用于删除一个已添加到串口驱动中的串口端口,通常在驱动卸载时调用该函数
 * 参数 drv: 串口驱动
 *      port: 要删除的串口端口
 * 返回值:   成功,返回0;否则返回错误码
 */
int uart_remove_one_port(struct uart_driver *drv, struct uart_port *port);
5、uart_write_wakeup
 /* 功能:     uart_write_wakeup唤醒上层因向串口端口写数据而阻塞的进程,通常在串口发送中断处理函数中调用该函数
 * 参数 port:需要唤醒写阻塞进程的串口端口
 */
void uart_write_wakeup(struct uart_port *port);
6、uart_suspend_port
 /* 功能:     uart_suspend_port用于挂起特定的串口端口
 * 参数 drv: 要挂起的串口端口所属的串口驱动
 *      port:要挂起的串口端口
 * 返回值:   成功返回0;否则返回错误码
 */
int uart_suspend_port(struct uart_driver *drv, struct uart_port *port);
7、uart_resume_port
 /* 功能:     uart_resume_port用于恢复某一已挂起的串口
 * 参数 drv: 要恢复的串口端口所属的串口驱动
 *      port:要恢复的串口端口
 * 返回值:   成功返回0;否则返回错误码
 */
int uart_resume_port(struct uart_driver *drv, struct uart_port *port)
8、uart_get_baud_rate
 /* 功能:        uart_get_baud_rate通过解码termios结构体来获取指定串口的波特率
 * 参数 port:  要获取波特率的串口端口
 *     termios:当前期望的termios配置(包含串口波特率)
 *     old:    以前的termios配置,可以为NULL
 *     min:    可接受的最小波特率
 *     max:    可接受的最大波特率
 * 返回值:     串口的波特率
 */
unsigned int
uart_get_baud_rate(struct uart_port *port, struct ktermios *termios,
     struct ktermios *old, unsigned int min, unsigned int max);
9、uart_get_divisor
 /* 功能:     uart_get_divisor用于计算某一波特率的串口时钟分频数(串口波特率除数)
 * 参数 port:要计算时钟分频数的串口端口
 *      baud:期望的波特率
 *返回值:    串口时钟分频数
 */
unsigned int uart_get_divisor(struct uart_port *port, unsigned int baud);
10、uart_update_timeout
 /* 功能:      uart_update_timeout用于更新(设置)串口FIFO超时时间
 * 参数 port: 要更新超时时间的串口端口
 *     cflag:termios结构体的cflag值
 *     baud: 串口的波特率
 */
void uart_update_timeout(struct uart_port *port, unsigned int cflag, unsigned int baud);
11、uart_match_port
 /* 功能:uart_match_port用于判断两串口端口是否为同一端口
 * 参数 port1、port2:要判断的串口端口
 * 返回值:不同返回0;否则返回非0
 */
int uart_match_port(struct uart_port *port1, struct uart_port *port2);
12、uart_console_write
 /* 功能:        uart_console_write用于向串口端口写一控制台信息
 * 参数 port:    要写信息的串口端口
 *     s:       要写的信息
 *     count:   信息的大小
 *     putchar: 用于向串口端口写字符的函数,该函数函数有两个参数:串口端口和要写的字符
 */
void uart_console_write(struct uart_port *port, const char *s,
            unsigned int count,
            void (*putchar)(struct uart_port *, int))

三、针对 GYM-1010-B 串口的编写 
       该串口驱动例子是我针对海思 Hi3516 处理器的串口2(uart2)独立开发的。该驱动将串口看作平台(platform)设备。platform可以看作一伪总线,用于将集成于片上系统的轻量级设备与Linux设备驱动模型联系到一起,它包含以下两部分(有关platform的声明都在#include <linux/platform_device.h>,具体实现在drivers/base/platform.c):
       1、platform设备。我们需要为每个设备定义一个platform_device实例

      struct platform_device {
    const char      *name;         /* 设备名 */
    int              id;           /* 设备的id号 */
    struct device    dev;          /* 其对应的device */
    u32              num_resources;/* 该设备用有的资源数 */
    struct resource *resource;     /* 资源数组 */
   };

 
      为我们的设备创建platform_device实例有两种方法:填充一个platform_device结构体后用platform_device_register(一次注册一个)或platform_add_devices(一次可以注册多个platform设备)将platform_device注册到内核;更简单的是使用platform_device_register_simple来建立并注册我们的platform_device。

    2、platform驱动。platform设备由platform驱动进行管理。当设备加入到系统中时,platform_driver的probe方法会被调用来见对应的设备添加或者注册到内核;当设备从系统中移除时,platform_driver的remove方法会被调用来做一些清理工作,如移除该设备的一些实例、注销一些已注册到系统中去的东西。

      struct platform_driver {

    int  (*probe)(struct platform_device *);

    int  (*remove)(struct platform_device *);

    void (*shutdown)(struct platform_device *);

    int  (*suspend)(struct platform_device *, pm_message_t state);

    int  (*suspend_late)(struct platform_device *, pm_message_t state);

    int  (*resume_early)(struct platform_device *);

    int  (*resume)(struct platform_device *);

    struct device_driver driver;

};


更详细platform资料可参考其他资料,略。。。。。。。。。。




下面是串口驱动例子和其GPS测试程序源码 .....







0 0