Embedded Linux: From Beginner to Abandonment (24)

Embedded Linux: From Beginner to Abandonment (24)
Four Ways for APP to Access Hardware
References:
Four Ways for APP to Access Hardware (64~67)

Four Ways for APP to Access Hardware

For example, how does a mother know if her baby has woken up while sleeping?

How does a mother know if the child in the bedroom has woken up?

1. Occasionally going into the room to check: Query method

Simple, but tiring.

2. Going into the room to sleep with the child, the child will wake her up when they wake: Sleep-Wake

Not tiring, but the mother can’t do any work.

3. The mother has many tasks to do but can accompany the child for a while, setting an alarm: Polling method

It wastes some time, but she can continue to work.

The mother will either be woken by the child or the alarm.

4. The mother is working in the living room, and when the child wakes up, they will come out of the room to tell the mother: Asynchronous notification

The mother and child do not interfere with each other.

There is no superiority or inferiority among these four methods; different methods are used in different situations.

Getting Device Information

Getting device information through ioctl, the parameters of ioctl are as follows:

int ioctl(int fd, unsigned long request, …);

Some drivers have specific requirements for the format of the request, as shown below:

Embedded Linux: From Beginner to Abandonment (24)

Embedded Linux: From Beginner to Abandonment (24)

Embedded Linux: From Beginner to Abandonment (24)

Query Method

The APP calls the open function and passes in “O_NONBLOCK” to indicate “non-blocking”.

When the APP calls the read function to read data, if there is data in the driver, the APP’s read function will return data; otherwise, it will immediately return an error.

Sleep-Wake

The APP calls the open function without passing “O_NONBLOCK”.

When the APP calls the read function to read data, if there is data in the driver, the APP’s read function will return data; otherwise, the APP will sleep in kernel mode, and when there is data, the driver will wake the APP up, allowing the read function to resume execution and return data to the APP.

POLL/SELECT

The POLL mechanism and SELECT mechanism are identical; only the APP interface functions differ.

Simply put, they are like setting an alarm: when calling the poll or select functions, a “timeout” can be passed in. During this time, if the conditions are met (for example, data is readable or space is writable), it will return immediately; otherwise, it will return an error when the “timeout” ends.

Usage is as follows:

⚫ The APP first calls the open function.

⚫ Instead of directly calling the read function, the APP first calls the poll or select function, which can take a “timeout”.

Their purpose is: If there is data in the driver, it returns immediately; otherwise, it sleeps.

During the sleep period, if someone operates the hardware, once the driver obtains data, it will wake the APP up, causing poll or select to return immediately; if no one operates the hardware within the “timeout”, poll or select will also return when the time is up.

The APP can determine the return reason based on the return value of the function: Is there data? Did it timeout without data?

⚫ After the APP determines that there is data based on the return value of poll or select, it calls the read function to read the data, which will then return data immediately.

⚫ The poll/select function can monitor multiple files and various events:

Embedded Linux: From Beginner to Abandonment (24)

Asynchronous Notification

What is synchronous? It means “you are slow, I will wait for you”.

Then asynchronous means: if you are slow, you can play by yourself, I will do my own thing, and notify me when something happens.

Asynchronous notification means that the APP can be busy with its own tasks, and when the driver has data, it will actively send a signal to the APP, causing the APP to execute the signal handling function.

Embedded Linux: From Beginner to Abandonment (24)

You can do an experiment on a virtual machine:

Embedded Linux: From Beginner to Abandonment (24)

Embedded Linux: From Beginner to Abandonment (24)

Embedded Linux: From Beginner to Abandonment (24)

That’s all for today’s notes!

Thank you!

Leave a Comment