μC / OS-II multi-task information flow and the CAN bus driver

Abstract: This paper μC / OS-II multi-mission critical technology and interrupt the flow of information dealing with the general approach and the basic concepts of PC system interrupt; to CAN bus, detailed analysis in x86 real mode on μC / OS-II of the CAN The implementation of the bus driver.

μC / OS-II is an American Jean Labrosse prepared a free, open source embedded real-time kernel. Embedded computer applications for the development of the technical staff is a highly practical, real-time embedded operating system ERTOS (Embedded Real Time Operation System).

To develop a comprehensive ERTOS, we will have multi-task scheduling and I / O device operation stability, in terms of coordination to make a lot of work, this is my process in the development ERTOS deeply appreciate the focus. I hope this can develop ERTOS technical staff in the multi-task information flow and I / O-driven aspects of an inspiration.

More than one mission critical technical information flow

Multi-task information flow in the discussion before the discussion about multi-tasking working condition. In μC / OS, each task is an infinite loop, each task in the following five states: dormant, ready state, running state, suspend state and suspended state shown in Figure 1.

μC / OS-II multi-task information flow and the CAN bus driver

Multi-task scheduling and the driver of the preparation process will inevitably involve the public code and the shared storage area of the protection of Wen Ti. Even the original C function, in terms of reuse in the absence of verification of the theory and practice cases, need to be protected. This need to rationalize the algorithm to the common code segment, Gongxiangcunchu areas to protect, Bimian operating system running reuse problem arising in the process of Er Dao Zhi run results unpredictable.

System in the development process, we should consider to reduce the complexity of the system, but also take into account the stability and efficiency requirements. This requires us to various algorithms rational choice: the situation in stability can be guaranteed, the choice is relatively simple, CPU-less time of the algorithm; in Wending Xing can not be assured, consider comprehensive selection algorithm. The only way to make the operating system configuration in a certain environment to achieve maximum efficiency.

Then were used to void CanSendMessageProcess (void * data), void CanSendMessage (void * data), void CanReceiveMessageProcess (void * data), and void CanReceiveMessage (void * data) to describe the four tasks using message queues, mailboxes and semaphores communication mechanism for information flow in the transfer process.

(1) message queue communication mechanism

Message queue is initialized when a designated space array, the array is achieved when using the concept of circular buffers. The array will not be eliminated during operation, thus avoiding duplication of time to establish an array of memory leak problem. When a task sends a message to the message queue when the corresponding pointer plus 1 (OSQIn 1), the queue is full (OSQEntries = OSQSize), OSQIn then point to the same unit with OSQOut. If the point of the unit in OSQIn insert a new pointer pointing to a message, it constitutes a FIFO (First-In-First-Out) queue. Conversely, if the next unit in the OSQOut point to insert a new pointer to a cell, constitutes LIFO queue (Last-In-First-Out). In this instance, we define the FIFO queue. Message pointer always points to the unit from OSQOut out. OSQStart and OSQEnd define the message head and tail pointer array in order to OSQIn and OSQOut reach the edge of the queue, to the border inspection and pointer adjustment needed to achieve its circulation.

Message queue data structure is as follows:

typedef struct os_q (
struct os_q * OSQPtr; / * Queue control block in the free link to all of the queue control block * /
void * OSQStart; / * message queue pointer pointing to starting address of the array pointer * /
void * OSQEnd; / * point to the end of the message queue address of the next cell pointer * /
void * OSQIn; / * point to the message queue position to insert the next message pointer * /
void * OSQOut; / * point to the next message queue in the pointer position out message * /
INT16U OSQSize; / * message queue in the total number of elements * /
INT16U OSQEntries; / * message queue in the total number of messages * /
) OS_Q;

Figure 2 shows the flow of information message queue description.

① CanSendMessageProcess task after the completion of the calculation of information will be sent the information to send a message queue.

② CanSendMessage message queue a mandate to obtain inside information.

③ through the CAN bus I / O port sends data to the bus up. No information if the message queue, then the task by the running into the waiting state until the message queue from the information received so far.


④ CanReceiveMessage task responsible for reading the bus above information.

⑤ CanReceiveMessage task will be to read the information into the message queue 2.

⑥ CanReceiveMessageProcess task is removed from the message queue information 2 start work, if the message queue is empty, then the task into the wait state.

Message Queue apply one to one, one to many, many to many and many to one relationship. In other words, the message queue can be used as a shared public areas, to Shi Shi mutually exclusive, need to synchronize between tasks; to cooperation, inter-process requires the exchange of information, this also implements synchronization and communication.

μC / OS-II multi-task information flow and the CAN bus driver

(2) E-mail communication mechanism

E-mail concepts and pipelines (pipelines) are similar to the definition of a task or interrupt service routine task of sending a pointer to another type of variable, which contains a pointer to a specific "message" of the data structure. The source side of the task can only be written to the mailbox in the destination task can only be read from the mailbox. E-mail transport stream data, that is a continuous string of bytes or a stream. Therefore, to access a mailbox like to access a sequential file. E-mail can be used to notify the occurrence of an event (sending a message), can also be used to share certain resources, such mail will be treated as a binary semaphore.

Figure 3 shows the flow of information for the email instructions.

① CanSendMessageProcess task will calculate the best data sent to CanSendMessage task, and then wait for the response signal into the ready state. CanSendMessage the same time send a response in the receiver handshake signals to CanSendMessageProcess, confirmation has been received.

② CanSendMessage task CanSend MessageProcess mission sent to the information sent to the CAN bus, to send into the ready state after waiting for the next transmission work.

③ CanReceiveMessage task to receive the flow of information from the bus, will receive the information sent to the Can ReceiveMessageProcess task, enter the ready state to wait for response signal.

 

④ CanReceiveMessageProcess task received send a response message handshake.

(3) semaphore communication mechanism

Semaphore (semaphore) is a contract mechanism: two or more tasks by a simple signal of cooperation, a task can be forced to stop at a location until it receives a specific signal. In general the multi-tasking kernel semaphore for:

◇ mark the occurrence of an event;

◇ control the right to use shared resources (to meet the mutually exclusive conditions);

◇ make the behavior of the two tasks simultaneously.

Semaphore implementation of three main steps:

◇ A semaphore can be initialized non-negative;

◇ wait (wait) operation of the signal amount minus 1. If the value becomes negative, then the implementation of the task is blocked waiting.

◇ have the right to use the task singal CPU operation to increase a semaphore. If the value is not positive, the task was blocked waiting for operations to be unblocked.

To meet the information transfer process of the principle of real-time high, in part, in the message queue to introduce the concept of semaphores. Is CanSendMessageProcess task, the number of bytes of information at once sent to the message queue, so that by the operation plus 1 semaphore wait state into the suspended state. In CanSendMessage task was to enter ready state after the semaphore, wait for the right to use the CPU into the running state. Into the running state after the task and the signal amount minus one removed from the message queue message via I / O port to send to the CAN bus. CanReceiveMessage tasks and CanReceive MessageProcess mandate and contrary to the above operation. This example illustrates the semaphore signs for the occurrence of an event. (See Figure 2.)

μC / OS-II multi-task information flow and the CAN bus driver

2 μC / OS-II interrupt handling

μC / OS-II, the interrupt service routine generally written in assembly language. The following is indicative interrupt service routine code.

User interrupt service routine:

Save all the CPU registers;

Call OSIntEnter or OSIntNesting direct plus 1;

Implementation of the user code to interrupt;

Call OSIntExit;

Restore all CPU registers;

Implementation of the interrupt return instruction;

Here μC / OS-II provides two ISR and the kernel interface function: OSIntEnter and OSIntExit. OSIntEnter notify μC / OS-II kernel, interrupt service routine is running. In fact, this function do is to add a global variable OSIntNesting 1. And so have accumulated in the x86 CPU instructions, you can use the command instead of OSIntEnter:

INC BYTE PTR OSIntNesting

The interrupt nesting counter to ensure that all interrupt processing is complete and then for task scheduling. Another interface function is to inform the kernel OSIntExit, interrupt service has ended. According to the situation accordingly, returning to the breakpoint (which may be a task or interrupt service routine is nested), or by the kernel for task scheduling.

The user's ISR must be prepared to install a position to interrupt occurs, CPU number according to the corresponding interrupt service program is running and accurate. Many real-time operating systems are available to install, uninstall interrupt service routine of the API interface function, and even some mature RTOS interrupt controller's management has a corresponding API function. But the μC / OS-II kernel does not provide a similar interface function, the corresponding CPU requires the user to realize his transplantation. The interface function with the specific hardware environment, the next PC system interrupt handling this under the detailed description.

3 PC system interrupt under

X86 family of processors can support up to 256 interrupt, and methods used to scale associated with each interrupt and the corresponding position of ISR. In real mode, the interrupt vector table (IVT) stored in memory, the low end of 1K. Each entry to the scale, 4 bytes, save one ISR segment address and offset information. PC systems use two cascaded programmable interrupt controller 82C59A. A 82C59A can connect eight hardware interrupts, numbered IRQ0 ~ IRQ7. PC can manage a total of 15 external interrupt sources, PC's interrupt controller shown in Figure 4. (For more on the 82C59A uses can be found in the information.)

In μC / OS under, CAN bus I / O port interrupt vector to set pseudo-code:


void CanInitHW (UI segment, BYTE Irq0, BYTE Irq1) (
Save the old interrupt vector
Save the mask register value
So 82C59A mask register (0x21) the position 1, close the interrupt input
Close CPU interrupt
Set new interrupt vector
Interruption in service to respond to service against another (assuming the current service interruption is IRQ5)
Open CPU interrupt
Clear 82C59A mask register (0X21, 0XA1) you open the interrupt input
)

Buffer 4 semaphore queue with the support of the CAN bus driver

Introduced in front of μC / OS-II kernel key technology multi-task scheduling, interrupt and interrupt the normal PC system under way. You Yi 82C59A interrupt 5 (IRQ5), 0x0D interrupt vector, for example, introduced the interrupt service routine of re-distribution and response SJA1000 controller to send and receive interrupt service routine.

Here, under the ring signal volume with the buffer queue and the relationship between the interrupt handler, and this is part of the core device driver.

ERTOS drivers and other operating systems are different. Such as Windows, Unix, Solaris, Linux and other operating system, weakening the concept of the device, the user process can use the device file system to complete. However, in μC / OS-II to develop CAN bus driver is not so strict, as long as the equipment to meet the CPU time in a row do not occur when using the time overlap on it.

Character serial devices or other peripheral devices are there speed and CPU speed does not match the problem, so it is necessary to establish the appropriate buffer. CAN port to send data, as long as the data is written to the buffer, and then removed one by one by the SJA1000 controller made out of. CAN receive data from the mouth, they often receive such number of bytes after the CPU needs to process, so these data can be received in advance stored in the buffer before. Buffer can be set to receive the number of bytes after the interruption of CPU, so as to avoid frequent interruptions and reduce the CPU system in real time.

μC / OS-II multi-task information flow and the CAN bus driver

In the process of reading and writing the buffer zone, often encounter want to send data, send the buffer is full; want to read, the receiver buffer is empty. For the end user program can work with queries that can not read and write operation to give up, and then frequently to try this operation until the success of the efficiency of this process is clearly lower. If the introduction of reading and writing, respectively, two signals at both ends of the buffer to synchronize the operation, the problem will be solved. However, the user task write buffer is full, the signal quantity of sleep, so that CPU to run other tasks until the ISR data from the buffer time to go sleep and wake up to this task; Similarly, the user tasks like reading, but when the buffer is empty can also be the signal quantity of sleep, when the external device has data to the re-awakening. Because μC / OS-II provides a semaphore wait timeout mechanism, CAN I of course have the ability to read and write timeouts.

With the amount of buffer and signals to send and receive part of the CAN port network, see Articles Supplementary Edition (http://www.dpj.com.cn).

Interface functions are summarized below.

void CanInitHW (UI segment, BYTE irq0, BYTE IRQ1)
/ * Set the SJA1000 controller port interrupt vector * /
int canReleaseHW () / * clear the SJA1000 controller port interrupt vector * /
int canSendMsg (CANBYTE port, MSG_STRUCT msg)
/ * SJA1000 controller to the custom port to send data * /
int canReceiveMsg (CANBYTE port, MSG_STRUCT msg_ptr)
/ * SJA1000 controller port to receive from the custom data
int canConfig (CANBYTE port, CAN_STRUCT can)
/ * Initialize and configure the SJA1000 controller * /
int canNormalRun (CANBYTE port)
/ * Set SJA1000 normal (Normal) operation mode * /
int canReset (CANBYTE port)
/ * SJA1000 controller port reset, the buffer Set 0xff * /
CANBYTE can0r (CANBYTE addr)
/ * Read custom SJA1000 controller port 0 register values * /
CANBYTE can1r (CANBYTE addr)
/ * Read the SJA1000 controller port 1 of the custom register value * /

Receive and transmit data buffer data structure definition:

typedef struct (
INT16U RingBufRxCtr; / * number of characters to receive buffer * /
OS_EVENT RingBufRxSem; / * Receive semaphore * /
INT8U RingBufRxInPtr; / * Receive buffer to write the next character position * /
INT8U RingBufRxOutPtr; / * Receive buffer to be read the next character position * /
INT8U RingBufRx [CAN_RX_BUF_SIZE]; / * receiver ring buffer * /
INT16U RingBufTxCtr;
/ * Send the number of characters in buffer * /
OS_EVENT * RingBufTxSem; / * send a semaphore * /
INT8U * RingBufTxInPtr;
/ * Send the buffer to write the next character position * /
INT8U * RingBufTxOutPtr;
/ * Send the buffer to be read the next character position * /
INT8U RingBufTx [CAN_TX_BUF_SIZE]; / * send the ring buffer * /
) CAN_RING_BUF;

Concluding Remarks

This article is in the field of application of embedded computer technology background made, after the end of the project development, system uptime more than 27 days. Hoped that this paper put forward on the development of embedded operating systems and technical personnel can help, but also hope that the same developers to explore areas of common development.

Declined comment

一线免费高清在线视频-久久精品人人做人人爽电影-天天做天天爱夜夜爽女人爽
<蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <蜘蛛词>| <文本链> <文本链> <文本链> <文本链> <文本链> <文本链>