www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

當前位置:首頁 > 嵌入式 > 嵌入式軟件
[導讀] 一:前言接著前面的終端控制臺分析,接下來分析serial的驅動。在linux中,serial也對應著終端,通常被稱為串口終端。在shell上,我們看到的/dev/ttyS*就是串口終端所對應的

 一:前言

接著前面的終端控制臺分析,接下來分析serial的驅動。在linux中,serial也對應著終端,通常被稱為串口終端。在shell上,我們看到的/dev/ttyS*就是串口終端所對應的設備節(jié)點。

在分析具體的serial驅動之前。有必要先分析uart驅動架構。uart是Universal Asynchronous Receiver and Transmitter的縮寫。翻譯成中文即為”通用異步收發(fā)器”。它是串口設備驅動的封裝層。

二:uart驅動架構概貌

如下圖所示:

上圖中紅色部份標識即為uart部份的操作。

從上圖可以看到,uart設備是繼tty_driver的又一層封裝。實際上uart_driver就是對應tty_driver.在它的操作函數(shù)中,將操作轉入uart_port.

在寫操作的時候,先將數(shù)據(jù)放入一個叫做circ_buf的環(huán)形緩存區(qū)。然后uart_port從緩存區(qū)中取數(shù)據(jù),將其寫入到串口設備中。

當uart_port從serial設備接收到數(shù)據(jù)時,會將設備放入對應line discipline的緩存區(qū)中。

這樣。用戶在編寫串口驅動的時候,只先要注冊一個uart_driver.它的主要作用是定義設備節(jié)點號。然后將對設備的各項操作封裝在uart_port.驅動工程師沒必要關心上層的流程,只需按硬件規(guī)范將uart_port中的接口函數(shù)完成就可以了。

三:uart驅動中重要的數(shù)據(jù)結構及其關聯(lián)

我們可以自己考慮下,基于上面的架構代碼應該要怎么寫。首先考慮以下幾點:

1: 一個uart_driver通常會注冊一段設備號。即在用戶空間會看到uart_driver對應有多個設備節(jié)點。例如:

/dev/ttyS0 /dev/ttyS1

每個設備節(jié)點是對應一個具體硬件的,從上面的架構來看,每個設備文件應該對應一個uart_port.

也就是說:uart_device怎么同多個uart_port關系起來?怎么去區(qū)分操作的是哪一個設備文件?

2:每個uart_port對應一個circ_buf,所以uart_port必須要和這個緩存區(qū)關系起來

回憶tty驅動架構中。tty_driver有一個叫成員指向一個數(shù)組,即tty->ttys.每個設備文件對應設數(shù)組中的一項。而這個數(shù)組所代碼的數(shù)據(jù)結構為tty_struct. 相應的tty_struct會將tty_driver和ldisc關聯(lián)起來。

那在uart驅動中,是否也可用相同的方式來處理呢?

將uart驅動常用的數(shù)據(jù)結構表示如下:

結合上面提出的疑問??梢院芮宄目炊@些結構的設計。

四:uart_driver的注冊操作

Uart_driver注冊對應的函數(shù)為: uart_register_driver()代碼如下:

int uart_register_driver(struct uart_driver *drv)

{

struct tty_driver *normal = NULL;

int i, retval;

BUG_ON(drv->state);

/*

* Maybe we should be using a slab cache for this, especially if

* we have a large number of ports to handle.

*/

drv->state = kzalloc(sizeof(struct uart_state) * drv->nr, GFP_KERNEL);

retval = -ENOMEM;

if (!drv->state)

goto out;

normal = alloc_tty_driver(drv->nr);

if (!normal)

goto out;

drv->tty_driver = normal;

normal->owner = drv->owner;

normal->driver_name = drv->driver_name;

normal->name = drv->dev_name;

normal->major = drv->major;

normal->minor_start = drv->minor;

normal->type = TTY_DRIVER_TYPE_SERIAL;

normal->subtype = SERIAL_TYPE_NORMAL;

normal->init_termios = tty_std_termios;

normal->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL;

normal->init_termios.c_ispeed = normal->init_termios.c_ospeed = 9600;

normal->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV;

normal->driver_state = drv;

tty_set_operations(normal, &uart_ops);

/*

* Initialise the UART state(s)。

*/

for (i = 0; i < drv->nr; i++) {

struct uart_state *state = drv->state + i;

state->close_delay = 500; /* .5 seconds */

state->closing_wait = 30000; /* 30 seconds */

mutex_init(&state->mutex);

}

retval = tty_register_driver(normal);

out:

if (retval < 0) {

put_tty_driver(normal);

kfree(drv->state);

}

return retval;

}

從上面代碼可以看出。uart_driver中很多數(shù)據(jù)結構其實就是tty_driver中的。將數(shù)據(jù)轉換為tty_driver之后,注冊tty_driver.然后初始化uart_driver->state的存儲空間。

這樣,就會注冊uart_driver->nr個設備節(jié)點。主設備號為uart_driver-> major. 開始的次設備號為uart_driver-> minor.

值得注意的是。在這里將tty_driver的操作集統(tǒng)一設為了uart_ops.其次,在tty_driver-> driver_state保存了這個uart_driver.這樣做是為了在用戶空間對設備文件的操作時,很容易轉到對應的uart_driver.

另外:tty_driver的flags成員值為: TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV.里面包含有TTY_DRIVER_DYNAMIC_DEV標志。結合之前對tty的分析。如果包含有這個標志,是不會在初始化的時候去注冊device.也就是說在/dev/下沒有動態(tài)生成結點(如果是/dev下靜態(tài)創(chuàng)建了這個結點就另當別論了^_^)。

流程圖如下:

[!--empirenews.page--]

五: uart_add_one_port()操作

在前面提到。在對uart設備文件過程中。會將操作轉換到對應的port上,這個port跟uart_driver是怎么關聯(lián)起來的呢?這就是uart_add_ont_port()的主要工作了。

顧名思義,這個函數(shù)是在uart_driver增加一個port.代碼如下:

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

{

struct uart_state *state;

int ret = 0;

struct device *tty_dev;

BUG_ON(in_interrupt());

if (port->line >= drv->nr)

return -EINVAL;

state = drv->state + port->line;

mutex_lock(&port_mutex);

mutex_lock(&state->mutex);

if (state->port) {

ret = -EINVAL;

goto out;

}

state->port = port;

state->pm_state = -1;

port->cons = drv->cons;

port->info = state->info;

/*

* If this port is a console, then the spinlock is already

* initialised.

*/

if (!(uart_console(port) && (port->cons->flags & CON_ENABLED))) {

spin_lock_init(&port->lock);

lockdep_set_class(&port->lock, &port_lock_key);

}

uart_configure_port(drv, state, port);

/*

* Register the port whether it‘s detected or not. This allows

* setserial to be used to alter this ports parameters.

*/

tty_dev = tty_register_device(drv->tty_driver, port->line, port->dev);

if (likely(!IS_ERR(tty_dev))) {

device_can_wakeup(tty_dev) = 1;

device_set_wakeup_enable(tty_dev, 0);

} else

printk(KERN_ERR "Cannot register tty device on line %dn",

port->line);

/*

* Ensure UPF_DEAD is not set.

*/

port->flags &= ~UPF_DEAD;

out:

mutex_unlock(&state->mutex);

mutex_unlock(&port_mutex);

return ret;

}

首先這個函數(shù)不能在中斷環(huán)境中使用。 Uart_port->line就是對uart設備文件序號。它對應的也就是uart_driver->state數(shù)組中的uart_port->line項。

它主要初始化對應uart_driver->state項。接著調用uart_configure_port()進行port的自動配置。然后注冊tty_device.如果用戶空間運行了udev或者已經(jīng)配置好了hotplug.就會在/dev下自動生成設備文件了。

操作流程圖如下所示:

六:設備節(jié)點的open操作

在用戶空間執(zhí)行open操作的時候,就會執(zhí)行uart_ops->open. Uart_ops的定義如下:

static const struct tty_operations uart_ops = {

.open = uart_open,

.close = uart_close,

.write = uart_write,

.put_char = uart_put_char,

.flush_chars = uart_flush_chars,

.write_room = uart_write_room,

.chars_in_buffer= uart_chars_in_buffer,

.flush_buffer = uart_flush_buffer,

.ioctl = uart_ioctl,

.throttle = uart_throttle,

.unthrottle = uart_unthrottle,

.send_xchar = uart_send_xchar,

.set_termios = uart_set_termios,

.stop = uart_stop,

.start = uart_start,

.hangup = uart_hangup,

.break_ctl = uart_break_ctl,

.wait_until_sent= uart_wait_until_sent,

#ifdef CONFIG_PROC_FS

.read_proc = uart_read_proc,

#endif

.tiocmget = uart_tiocmget,

.tiocmset = uart_tiocmset,

};

對應open的操作接口為uart_open.代碼如下:

static int uart_open(struct tty_struct *tty, struct file *filp)

{

struct uart_driver *drv = (struct uart_driver *)tty->driver->driver_state;

struct uart_state *state;

int retval, line = tty->index;

BUG_ON(!kernel_locked());

pr_debug("uart_open(%d) calledn", line);

/*

* tty->driver->num won‘t change, so we won‘t fail here with

* tty->driver_data set to something non-NULL (and therefore

* we won‘t get caught by uart_close())。

*/

retval = -ENODEV;

if (line >= tty->driver->num)

goto fail;

/*

* We take the semaphore inside uart_get to guarantee that we won‘t

* be re-entered while allocating the info structure, or while we

* request any IRQs that the driver may need. This also has the nice

* side-effect that it delays the action of uart_hangup, so we can

* guarantee that info->tty will always contain something reasonable.

*/

state = uart_get(drv, line);

if (IS_ERR(state)) {

retval = PTR_ERR(state);

goto fail;

}

/*

* Once we set tty->driver_data here, we are guaranteed that

* uart_close() will decrement the driver module use count.

* Any failures from here onwards should not touch the count.

*/

tty->driver_data = state;

tty->low_latency = (state->port->flags & UPF_LOW_LATENCY) ? 1 : 0;[!--empirenews.page--]

tty->alt_speed = 0;

state->info->tty = tty;

/*

* If the port is in the middle of closing, bail out now.

*/

if (tty_hung_up_p(filp)) {

retval = -EAGAIN;

state->count--;

mutex_unlock(&state->mutex);

goto fail;

}

/*

* Make sure the device is in D0 state.

*/

if (state->count == 1)

uart_change_pm(state, 0);

/*

* Start up the serial port.

*/

retval = uart_startup(state, 0);

/*

* If we succeeded, wait until the port is ready.

*/

if (retval == 0)

retval = uart_block_til_ready(filp, state);

mutex_unlock(&state->mutex);

/*

* If this is the first open to succeed, adjust things to suit.

*/

if (retval == 0 && !(state->info->flags & UIF_NORMAL_ACTIVE)) {

state->info->flags |= UIF_NORMAL_ACTIVE;

uart_update_termios(state);

}

fail:

return retval;

}

int ret = 0;

state = drv->state + line;

if (mutex_lock_interruptible(&state->mutex)) {

ret = -ERESTARTSYS;

goto err;

}

state->count++;

if (!state->port || state->port->flags & UPF_DEAD) {

ret = -ENXIO;

goto err_unlock;

}

if (!state->info) {

state->info = kzalloc(sizeof(struct uart_info), GFP_KERNEL);

if (state->info) {

init_waitqueue_head(&state->info->open_wait);

init_waitqueue_head(&state->info->delta_msr_wait);

/*

* Link the info into the other structures.

*/

state->port->info = state->info;

tasklet_init(&state->info->tlet, uart_tasklet_action,

(unsigned long)state);

} else {

ret = -ENOMEM;

goto err_unlock;

}

}

return state;

err_unlock:

state->count--;

mutex_unlock(&state->mutex);

err:

return ERR_PTR(ret);

}

從代碼中可以看出。這里注要是操作是初始化state->info.注意port->info就是state->info的一個副本。即port直接通過port->info可以找到它要操作的緩存區(qū)。

uart_startup()代碼如下:

static int uart_startup(struct uart_state *state, int init_hw)

{

struct uart_info *info = state->info;

struct uart_port *port = state->port;

unsigned long page;

int retval = 0;

if (info->flags & UIF_INITIALIZED)

return 0;

/*

* Set the TTY IO error marker - we will only clear this

* once we have successfully opened the port. Also set

* up the tty->alt_speed kludge

*/

set_bit(TTY_IO_ERROR, &info->tty->flags);

if (port->type == PORT_UNKNOWN)

return 0;

/*

* Initialise and allocate the transmit and temporary

* buffer.

*/

if (!info->xmit.buf) {

page = get_zeroed_page(GFP_KERNEL);

if (!page)

return -ENOMEM;

info->xmit.buf = (unsigned char *) page;

uart_circ_clear(&info->xmit);

}

retval = port->ops->startup(port);

if (retval == 0) {

if (init_hw) {

/*

* Initialise the hardware port settings.

*/

uart_change_speed(state, NULL);

/*

* Setup the RTS and DTR signals once the

* port is open and ready to respond.

*/

if (info->tty->termios->c_cflag & CBAUD)

uart_set_mctrl(port, TIOCM_RTS | TIOCM_DTR);

}

if (info->flags & UIF_CTS_FLOW) {

spin_lock_irq(&port->lock);

if (!(port->ops->get_mctrl(port) & TIOCM_CTS))

info->tty->hw_stopped = 1;

spin_unlock_irq(&port->lock);

}

info->flags |= UIF_INITIALIZED;

clear_bit(TTY_IO_ERROR, &info->tty->flags);

}

if (retval && capable(CAP_SYS_ADMIN))

retval = 0;

return retval;

}

在這里,注要完成對環(huán)形緩沖,即info->xmit的初始化。然后調用port->ops->startup( )將這個port帶入到工作狀態(tài)。其它的是一個可調參數(shù)的設置,就不詳細講解了。

七:設備節(jié)點的write操作

Write操作對應的操作接口為uart_write( )。代碼如下:

static int

uart_write(struct tty_struct *tty, const unsigned char *buf, int count)

{

struct uart_state *state = tty->driver_data;

struct uart_port *port;

struct circ_buf *circ;

unsigned long flags;

int c, ret = 0;

/*

* This means you called this function _after_ the port was

* closed. No cookie for you.

*/

if (!state || !state->info) {

WARN_ON(1);

return -EL3HLT;

}

port = state->port;[!--empirenews.page--]

circ = &state->info->xmit;

if (!circ->buf)

return 0;

spin_lock_irqsave(&port->lock, flags);

while (1) {

c = CIRC_SPACE_TO_END(circ->head, circ->tail, UART_XMIT_SIZE);

if (count < c)

c = count;

if (c <= 0)

break;

memcpy(circ->buf + circ->head, buf, c);

circ->head = (circ->head + c) & (UART_XMIT_SIZE - 1);

buf += c;

count -= c;

ret += c;

}

spin_unlock_irqrestore(&port->lock, flags);

uart_start(tty);

return ret;

}

Uart_start()代碼如下:

static void uart_start(struct tty_struct *tty)

{

struct uart_state *state = tty->driver_data;

struct uart_port *port = state->port;

unsigned long flags;

spin_lock_irqsave(&port->lock, flags);

__uart_start(tty);

spin_unlock_irqrestore(&port->lock, flags);

}

static void __uart_start(struct tty_struct *tty)

{

struct uart_state *state = tty->driver_data;

struct uart_port *port = state->port;

if (!uart_circ_empty(&state->info->xmit) && state->info->xmit.buf &&

!tty->stopped && !tty->hw_stopped)

port->ops->start_tx(port);

}

顯然,對于write操作而言,它就是將數(shù)據(jù)copy到環(huán)形緩存區(qū)。然后調用port->ops->start_tx()將數(shù)據(jù)寫到硬件寄存器。

八:Read操作

Uart的read操作同Tty的read操作相同,即都是調用ldsic->read()讀取read_buf中的內容。有對這部份內容不太清楚的,參閱《 linux設備模型之tty驅動架構》.

九:小結

本小節(jié)是分析serial驅動的基礎。在理解了tty驅動架構之后,再來理解uart驅動架構應該不是很難。隨著我們在linux設備驅動分析的深入,越來越深刻的體會到,linux的設備驅動架構很多都是相通的。只要深刻理解了一種驅動架構。舉一反三。也就很容易分析出其它架構的驅動了。

本站聲明: 本文章由作者或相關機構授權發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內容真實性等。需要轉載請聯(lián)系該專欄作者,如若文章內容侵犯您的權益,請及時聯(lián)系本站刪除。
換一批
延伸閱讀

慕尼黑2025年9月12日 /美通社/ -- 慕尼黑當?shù)貢r間9月10日,在2025德國國際汽車及智慧出行博覽會(IAA MOBILITY)上,國際獨立第三方檢測、檢驗和認證機...

關鍵字: 測試 慕尼黑 模型 HUBER

天津2025年9月11日 /美通社/ -- 國際能源署(IEA)數(shù)據(jù)顯示,2024 年全球數(shù)據(jù)中心電力消耗達 415 太瓦時,占全球總用電量的 1.5%,預計到 2030 年,這一數(shù)字將飆升至 945 太瓦時,近乎翻番,...

關鍵字: 模型 AI 數(shù)據(jù)中心 BSP

深圳2025年9月9日 /美通社/ -- PART 01活動背景 當技術的鋒芒刺穿行業(yè)壁壘,萬物互聯(lián)的生態(tài)正重塑產(chǎn)業(yè)疆域。2025年,物聯(lián)網(wǎng)產(chǎn)業(yè)邁入?"破界創(chuàng)造"與"共生進化"?的裂變時代——AI大模型消融感知邊界,...

關鍵字: BSP 模型 微信 AIOT

浙江寧波2025年9月8日 /美通社/ -- 人工智能的熱流在寧波翻涌。9月4日,由浙江數(shù)字經(jīng)濟百人會主辦的"智能時代 浙里領航——智能體創(chuàng)新與應用圓桌會",成為一場重量級思想碰撞的樞紐。匯聚政產(chǎn)學研...

關鍵字: 智能體 離散 AI 模型

北京2025年9月5日 /美通社/ -- 9月4日,在北京市人民政府新聞辦公室舉行的"一把手發(fā)布?京華巡禮"系列主題新聞發(fā)布會上,北京經(jīng)開區(qū)對外發(fā)布,北京經(jīng)濟技術開發(fā)區(qū)(簡稱"北京經(jīng)開區(qū)&q...

關鍵字: 人工智能 模型 開源 AI

杭州2025年9月2日 /美通社/ -- 9月2日,央視《朝聞天下》欄目發(fā)布報道,重點關注中控技術在"人工智能+工業(yè)"領域的最新成果——時間序列大模型TPT 2(Time-series Pre-tra...

關鍵字: 人工智能 模型 PLAYER ASIA

北京2025年9月2日 /美通社/ -- 近日,深圳云天暢想信息科技有限公司(下稱"云天暢想")與浪潮信息正式簽署元腦生態(tài)戰(zhàn)略合作協(xié)議。雙方將聚焦教育行業(yè)AIGC應用落地,在AIGC實訓平臺、智能體平...

關鍵字: AI 模型 開發(fā)平臺 智能體

北京2025年8月28日 /美通社/ -- 8月28日,北京亦莊創(chuàng)新發(fā)布消息,北京經(jīng)濟技術開發(fā)區(qū)(簡稱"北京經(jīng)開區(qū)",又稱"北京亦莊")創(chuàng)新推出"一張清單、一鏈延伸、一套...

關鍵字: 接線 數(shù)字化 智能化 模型

深圳2025年8月28日 /美通社/ -- 8月27日,全球領先的無線通信與AI解決方案提供商廣和通發(fā)布新一代具身智能開發(fā)平臺 Fibot。Fibot已成功應用于Physic...

關鍵字: PHYSICAL 開發(fā)平臺 模型 INTELLIGENCE

首個采用高分辨率太陽觀測數(shù)據(jù)訓練的太陽物理學人工智能 (AI) 基礎模型,旨在深入探索太陽動態(tài)表面,對可能干擾地球和太空技術的太陽天氣做出有效規(guī)劃。 該模型已發(fā)布在 Hugging Face 開源平臺,旨在加快...

關鍵字: IBM NASA 開源 模型
關閉