[Beijing Xunwei] i.MX6ULL Terminator Linux RS232 / 485 controlador experimento marco de controlador UART en Linux

1 estructura uart_driver

En Linux, uart, como I2C y SPI, proporciona un marco de controlador de puerto serie. Solo necesita compilar el controlador de acuerdo con las funciones de marco de puerto serie proporcionadas. En términos generales, el controlador del puerto serie se ha implementado, todo lo que tenemos que hacer es agregar el nodo de dispositivo correspondiente en el archivo de árbol de dispositivos. Cuando el dispositivo y el controlador coinciden correctamente, el puerto serie puede funcionar normalmente.
En Linux, la estructura uart_driver se usa para describir el puerto serie, uart_driver se define en el archivo include / linux / serial_core.h, y el contenido es el siguiente:

295 struct uart_driver {
    
     
296        struct module *owner; /* 模块所属者 */ 
297        const char *driver_name; /* 驱动名字 */ 
298        const char *dev_name; /* 设备名字 */ 
299        int major; /* 主设备号 */ 
300        int minor; /* 次设备号 */ 
301        int nr; /* 设备数 */ 
302        struct console *cons; /* 控制台 */ 
303 
304        /* 
305        * these are private; the low level driver should not 
306        * touch these; they should be initialised to NULL 
307        */ 
308        struct uart_state *state; 
309        struct tty_driver *tty_driver; 
310 };

Generalmente, hay varios puertos serie en la placa de desarrollo, y cada controlador de puerto serie necesita definir una estructura uart_driver para representar.
Al igual que otros dispositivos, cuando se crea la estructura uart_driver, se registra en el kernel. Utilice la función uart_register_driver para completar el comportamiento de registro. El prototipo de la función es el siguiente: el
int uart_register_driver(struct uart_driver *drv)
parámetro drv es crear la estructura uart_driver que se registrará, y devuelve 0, indicando éxito y devolviendo un valor negativo en caso de fallo.
Dado que hay una función de registro, también hay una función de desregistro uart_unregister_driver. El prototipo de función es el siguiente: el
void uart_unregister_driver(struct uart_driver *drv)
parámetro drv es la estructura uart_driver que se va a desregistrar y no tiene valor de retorno.

2 estructura uart_port

uart_port se utiliza para describir el puerto de E / S o la dirección de la memoria de E / S, el tamaño FIFO, el tipo de puerto y otra información de un puerto UART (que corresponde directamente a un puerto serie).
uart_port se define en el archivo include / linux / serial_core.h, parte del contenido es el siguiente:

117 struct uart_port {
    
     
118        spinlock_t lock; /* port lock */ 
119     unsigned long iobase; /* in/out[bwl] */ 
120        unsigned char __iomem *membase; /* read/write[bwl] */ 
...... 
235        const struct uart_ops *ops; 
236        unsigned int custom_divisor; 
237        unsigned int line; /* port index */ 
238        unsigned int minor; 
239        resource_size_t mapbase; /* for ioremap */ 
240        resource_size_t mapsize; 
241        struct device *dev; /* parent device */ 
...... 
250 }; 

En la estructura uart_port, nos centramos principalmente en el miembro ops, que contiene la función de controlador específica del puerto serie, que se explicará en detalle más adelante.
Cada UART tiene una estructura uart_port, cómo combinar uart_port y uart_driver, use la función uart_add_one_port, el prototipo de la función es el siguiente:

int uart_add_one_port(struct uart_driver *drv, 
struct uart_port *uport)

drv: la estructura uart_driver correspondiente a uart_port,
uport: la estructura uart_port que se agregará a la estructura uart_driver.
Valor de retorno: 0, indica éxito, valor negativo, indica fallo.
Al desinstalar el controlador UART, también debe eliminar el uart_port del uart_driver correspondiente, y usar la función uart_remove_one_port para implementarlo. El prototipo de la función es el siguiente:
int uart_remove_one_port(struct uart_driver *drv, struct uart_port *uport)
drv: uart_driver correspondiente al uart_port que se va a desinstalar.
uport: uart_port que se desinstalará.
Valor de retorno: 0, indica éxito, valor negativo, indica fallo.

3 estructura uart_ops

La estructura uart_ops contiene las funciones de controlador específicas en el marco UART El sistema Linux envía y recibe datos y finalmente llama a las funciones en operaciones. ops es una variable de puntero de estructura de tipo uart_ops, uart_ops se define en el archivo include / linux / serial_core.h, y el contenido es el siguiente:

49 struct uart_ops {
    
     
50         unsigned int (*tx_empty)(struct uart_port *); 
51     void (*set_mctrl)(struct uart_port *, unsigned int mctrl); 
52         unsigned int (*get_mctrl)(struct uart_port *); 
53         void (*stop_tx)(struct uart_port *); 
54         void (*start_tx)(struct uart_port *); 
55         void (*throttle)(struct uart_port *);
56         void (*unthrottle)(struct uart_port *); 
57         void (*send_xchar)(struct uart_port *, char ch); 
58         void (*stop_rx)(struct uart_port *); 
59         void (*enable_ms)(struct uart_port *); 
60         void (*break_ctl)(struct uart_port *, int ctl); 
61         int (*startup)(struct uart_port *); 
62         void (*shutdown)(struct uart_port *); 
63         void (*flush_buffer)(struct uart_port *); 
64         void (*set_termios)(struct uart_port *, struct ktermios *new, 
65         struct ktermios *old); 
66         void (*set_ldisc)(struct uart_port *, struct ktermios *); 
67         void (*pm)(struct uart_port *, unsigned int state, 
68         unsigned int oldstate); 
69 
70         /* 
71         * Return a string describing the type of the port 
72         */ 
73         const char *(*type)(struct uart_port *); 
74 
75         /* 
76         * Release IO and memory resources used by the port. 
77         * This includes iounmap if necessary. 
78         */ 
79         void (*release_port)(struct uart_port *); 
80 
81         /* 
82         * Request IO and memory resources used by the port. 
83         * This includes iomapping the port if necessary. 
84         */ 
85         int (*request_port)(struct uart_port *); 
86         void (*config_port)(struct uart_port *, int); 
87         int (*verify_port)(struct uart_port *, struct serial_struct *); 
88         int (*ioctl)(struct uart_port *, unsigned int, unsigned long); 
89         #ifdef CONFIG_CONSOLE_POLL 
90         int (*poll_init)(struct uart_port *); 
91         void (*poll_put_char)(struct uart_port *, unsigned char); 
92         int (*poll_get_char)(struct uart_port *); 
93         #endif 
94 };

Los escritores de controladores UART deben implementar uart_ops, porque uart_ops es la interfaz del controlador UART inferior, que en realidad se ocupa de los registros UART. Para conocer el significado específico de estas funciones en la estructura uart_ops, consulte el documento Documentation / serial / driver.

Inserte la descripción de la imagen aquí

Supongo que te gusta

Origin blog.csdn.net/BeiJingXunWei/article/details/112600374
Recomendado
Clasificación