【Linux驱动开发】串口
生活随笔
收集整理的這篇文章主要介紹了
【Linux驱动开发】串口
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
UART驅動
串口驅動框架由SOC原廠編寫,只需要在設備樹中添加使用的串口節點信息,系統啟動后,串口設備和驅動匹配成功,生成/dev/ttymxcX(X=0,1,2...)文件。UART驅動程序本質是一個platform驅動
Linux定義uart_driver結構體表示UART驅動,include/linux/serial_core.h。
struct uart_driver {struct module *owner;const char *driver_name; /* 驅動名 */const char *dev_name; /* 設備名 */int major; /* 主設備號 */int minor; /* 次設備號 */int nr; /* 設備數 */struct console *cons; /* 控制臺 *//** these are private; the low level driver should not* touch these; they should be initialised to NULL*/struct uart_state *state;struct tty_driver *tty_driver; };使用uart_register_driver注冊uart_driver。
int uart_register_driver(struct uart_driver *drv)- drv:要注冊的uart_driver。
- 返回值:0,成功;負值,失敗。
使用uart_unregister_driver注銷uart_driver。
void uart_unregister_driver(struct uart_driver *drv)- drv:要注銷的uart_driver。
- 返回值:無。
uart_port結構體表示一個具體的port端口,include/linux/serial_core.h。其中
struct uart_port {/* ... */const struct uart_ops *ops;/* ... */ };struct uart_ops {unsigned int (*tx_empty)(struct uart_port *);void (*set_mctrl)(struct uart_port *, unsigned int mctrl);unsigned int (*get_mctrl)(struct uart_port *);void (*stop_tx)(struct uart_port *);void (*start_tx)(struct uart_port *);void (*throttle)(struct uart_port *);void (*unthrottle)(struct uart_port *);void (*send_xchar)(struct uart_port *, char ch);void (*stop_rx)(struct uart_port *);void (*enable_ms)(struct uart_port *);void (*break_ctl)(struct uart_port *, int ctl);int (*startup)(struct uart_port *);void (*shutdown)(struct uart_port *);void (*flush_buffer)(struct uart_port *);void (*set_termios)(struct uart_port *, struct ktermios *new,struct ktermios *old);void (*set_ldisc)(struct uart_port *, struct ktermios *);void (*pm)(struct uart_port *, unsigned int state,unsigned int oldstate);/** Return a string describing the type of the port*/const char *(*type)(struct uart_port *);/** Release IO and memory resources used by the port.* This includes iounmap if necessary.*/void (*release_port)(struct uart_port *);/** Request IO and memory resources used by the port.* This includes iomapping the port if necessary.*/int (*request_port)(struct uart_port *);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); #ifdef CONFIG_CONSOLE_POLLint (*poll_init)(struct uart_port *);void (*poll_put_char)(struct uart_port *, unsigned char);int (*poll_get_char)(struct uart_port *); #endif };const struct uart_ops *ops;包含UART具體驅動函數,Linux系統收發數據都是調用ops中的函數。uart_ops結構體中函數具體含義在Documentation/serial/driver文檔查看。
使用uart_add_one_port將uart_port添加到uart_driver中。
int uart_add_one_port(struct uart_driver *drv, struct uart_port *uport)- drv:uart_driver。
- uport:要添加到uart_driver中的port。
- 返回值:0,成功;負值,失敗。
使用uart_remove_one_port將uart_port從uart_driver移除。
int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)- drv:uart_driver。
- uport:要從uart_driver卸載的port。
- 返回值:0,成功;負值,失敗。
總結
以上是生活随笔為你收集整理的【Linux驱动开发】串口的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 松下PLC远程编程调试流程
- 下一篇: snap-社交网络分析