Background technology
When communicating by letter by shared storage between two CPU of prior art, at present more common have two kinds of methods.
A kind of method is that shared memory is divided into two, respectively as transmission, the receiving cable of two CPU, as shown in Figure 1.Each passage provides a read-write zone bit, and this sign is initially can write attitude.To read and write sign after the transmit leg transmission data and be revised as readable attitude, wait for that the take over party receives by writing attitude.The take over party receives and will read and write sign after the data and be revised as by readable attitude and can write attitude, so that transmit leg can continue to send.Adopt this method, data interaction each time needs all to wait for that the other side reads or writes finishes, and efficient is very low.
Another kind method also is that shared memory is divided into two, and respectively as the transmission of two CPU, the passage of reception, each passage is realized the circulation read-write with a circle queue, as shown in Figure 2.Circle queue manages by the read-write pointer that leaves in the mailbox.Be sent as example with CPU1, when CPU1 had data to send, at first the reading and writing pointer by circle queue 1 calculated whether enough remaining spaces are arranged, if the space is enough, then the message bag is write write pointer indication position, and upgraded write pointer.CPU2 automatic regular polling circle queue 1 when finding new message bag is arranged, then reads the message bag, and upgrades read pointer.Because transmit leg and take over party can operate shared memory simultaneously, on efficient, improve greatly than preceding method.
But still there is following problem:
(1) data generally send by the fixed length packing, differ greatly for the message packet length, and for example minimum length is 4 bytes, and maximum length is 40 bytes, all will send by the 40 bytes packing of maximum, cause waste.
When (2) data send not according to the fixed length packing, need comprise length information in the message bag, because message bag random length, the take over party can't calculate pending message bar number by the read-write pointer.
(3) during message bag random length, sending continuously in the process of message, wherein the break down read-write pointer that will cause the back of certain bar is all disorderly.
(4) when the rear of queue end spaces is deposited the new information bag inadequately, generally for easy to operate do not allow the message winding around, just need to fill particular values (for example 0xFFFF) and indicate this tail end space invalid, and the message bag is placed into the formation head.Needs and particular value compare during reception, the filling value when judgement is normal messages bag or coiling.If comprise particular value in the normal messages bag, will produce conflict.
Therefore, prior art has yet to be improved and developed.
Summary of the invention
The object of the present invention is to provide a kind of double CPU communication method,, provide a kind of support variable message-length, efficient, reliable double CPU communication method at the shortcoming of aforementioned two kinds of methods based on shared storage.
Technical scheme of the present invention is as follows:
There is the transmitted in both directions passage in a kind of double CPU communication method based on shared storage between a CPU and the 2nd CPU, each passage uses two circle queues, and one is used to deposit message index, and one is used to deposit message entity; And each channel transfer process may further comprise the steps:
A, initialization flow process: finish the initialization of the responsible formation read-write pointer of this CPU before the communication, the initialization of described formation read-write pointer need be satisfied: write pointer is greater than unit of read pointer; Perhaps first unit of write pointers point formation head, and read pointer points to last unit of formation afterbody;
B, transmission message flow: the new information entity is write the message entity formation, and updating message entity queue write pointer, promptly point to new information entity available cell afterwards; The content of updating message index queue write pointer indication unit, the new information entity in the refer message entity queue, and updating message index queue write pointers point next unit;
C, receive message flow: message index formation read pointer is to unit of caudal directions reach, according to the read pointer of the pending message entity of index calculation in this unit, and updating message index queue read pointer; Read pending message entity and processing.
Described method, wherein, described steps A also comprises:
First unit of write pointers point formation head, read pointer point to last unit of formation afterbody.
Described method, wherein, described step B carries out when satisfying following condition successively:
B01, according to message entity formation reading and writing pointer and message entity queue size, judge whether to write the new information entity; Be then to continue; , do not send out again after then postponing;
B02, according to message index formation reading and writing pointer and message index queue size, judge whether to write the new information index; Be then to continue; , do not send out again after then postponing.
Described method, wherein, described step C carries out under the following conditions:
C01, according to message index formation reading and writing pointer and message index queue size, calculate pending message bar number, if do not have Message Processing then return.
Described method, wherein, among the described step C, described message index formation read pointer is when unit of caudal directions reach, if arrive the message index queue size to the caudal directions reach then unroll to 0.
Described method wherein, also comprises after the described step C:
D, updating message entity queue read pointer;
E, pending message bar number subtract 1;
Described step C, D are carried out in circulation and E is 0 up to pending message bar number.
Described method, wherein, described message index is the pointer that size is fixed as 4 bytes, refer message entity; Described message entity variable size.
Described method, wherein, described each circle queue is by leaving the read-write pointer management in the mailbox in, and described mailbox is a register, and write pointer represents that transmit leg can write, and the take over party is read-only; Read pointer represents that transmit leg is read-only, and the take over party can write.
Described method wherein, adopts the mirror image protection to described mailbox, to avoid the conflict that reads while write of pointer.
Described method wherein, when described transmit leg sends message, is provided with the other side's interruption status zone bit by interruption generation and control device; Described take over party interrupts receiving by response, or receives with the mode of poll.
A kind of double CPU communication method provided by the present invention based on shared storage, compared with prior art, owing in shared storage, introduce index queue and entity queue, reduce transmit leg and take over party's stand-by period, reduced the number of times that the take over party repeats into interruption or inquiry, improved the utilization factor of CPU, data throughout and processing speed have been improved, can support variable message-length, efficient, reliable, be particularly useful for the situation of the big and message bag random length of message traffic.
Embodiment
Below in conjunction with embodiment the method for the invention is further described.
Double CPU communication method based on shared storage of the present invention, as shown in Figure 3 and Figure 4, for convenience, the passage that definition CPU1 sends, CPU2 receives is a forward channel, the passage that CPU2 sends, CPU1 receives is a backward channel.Forward channel and backward channel have respectively used two circle queues, and one is used to deposit message index, and one is used to deposit message entity; Message index is the pointer that size is fixed as 4 bytes, refer message entity, the message entity variable size.
Each circle queue is by leaving the read-write pointer management in the mailbox in, and write pointer represents that transmit leg can write, and the take over party is read-only.Read pointer represents that transmit leg is read-only, and the take over party can write.Mailbox is adopted the mirror image protection, can avoid the collision problem that reads while write of pointer fully.
When transmit leg sends message in the inventive method, can the other side's interruption status zone bit be set by interrupting generation and control device, the take over party can interrupt receiving by response, also can receive with the mode of poll.
According to system's needs, set the message index queue size and the message entity queue size of forward channel and backward channel respectively.In the handling procedure of two CPU, comprise following three basic procedures:
First, the initialization flow process:
Finish the initialization of the responsible formation read-write pointer of this CPU before the communication, first unit of write pointers point formation head, read pointer point to last unit of formation afterbody.
Second portion sends message flow:
(1), judges whether to write the new information entity according to message entity formation reading and writing pointer and message entity queue size.Be then to continue; , do not send out again after then postponing.
(2), judge whether to write the new information index according to message index formation reading and writing pointer and message index queue size.Be then to continue; , do not send out again after then postponing.
(3) the new information entity is write the message entity formation, and updating message entity queue write pointer, promptly point to new information entity available cell afterwards.
(4) content of updating message index queue write pointer indication unit, the new information entity in the refer message entity queue, and updating message index queue write pointer promptly point to next unit.
Third part receives message flow:
(1), calculates pending message bar number according to message index formation reading and writing pointer and message index queue size.If do not have Message Processing then return.
(2) unit of message index formation read pointer reach, if reach arrives the message index queue size then unrolls to 0, according to the read pointer of the pending message entity of index calculation in this unit, and updating message index queue read pointer.
(3) read pending message entity and processing.
(4) updating message entity queue read pointer.
(5) pending message bar number subtracts 1.
(6) continuing for (2) step, is 0 up to pending message bar number.
(7) continued for (1) step.
In specific embodiments of the invention, as shown in Figure 5, C6x DSP and ARM9 use shared storage and mailbox to realize interacting message.Shared memory is divided into the formation of forward message index, the formation of forward message entity, reverse message index queue, reverse message entity queue totally 4 buffer zones.The read-write of forward and reverse message queue is all operated in the round-robin mode, and the used pointer of circulation read-write is undertaken by mailbox alternately.
Mailbox has two groups, is called to read in mailbox and write out mailbox (from ARM9 angle address).Reading in mailbox is the register file of a twoport, and the port that connects DSP is read-write, and the port that connects ARM is read-only.Write out the register file that mailbox also is a twoport, but the port of connection DSP is read-only, and the port of connection ARM is read-write.Two groups of mailbox register are piled inner dual-port and are equipped with separately mirror image register to support that dual-port reads while write, and therefore can avoid the collision problem that reads while write of pointer fully.
Two groups of mailboxes can produce interruption when write access, can produce interrupt notification ARM by the ARM interruptable controller to reading in the mailbox write access, are used for DSP and send message to ARM.Can produce interrupt notification DSP by the DSP interruptable controller to writing out the mailbox write access, be used for ARM and send message to DSP.The take over party can interrupt receiving by response, also can receive with the mode of poll.
Can provide recovery mechanism by reset controller: with DSP is main control end, and DSP is checked through queue full, waits for that then ARM handles and timing, surpasses the limiting time formation and still then is considered as ARM and has crashed for full, makes ARM recover normal by resetting.
The read-write of message index formation is carried out following operation in the round-robin mode in the inventive method:
(1) during initialization, first unit of write pointers point formation head of formation, read pointer point to last unit of formation afterbody.
(2) pointer moves toward afterbody from the head, when arriving last unit, is returned to the formation head again, so circulation.
(3) during write queue, write from write pointer indication position; When reading formation, unit of read pointer reach is read again.
(4) during write queue, judge whether and to write according to read-write pointer difference.
(5) read pointer catches up with write pointer, and guarantees to differ at least one unit between read pointer and the write pointer.When calculating pending message bar and counting, need deduct this element.
The read-write of message entity formation also is to operate in the round-robin mode.When sending message,, then move on to the queue heads operation if arrived message entity formation tail end and the tail end space is not enough.Owing to when depositing message entity, simultaneously its address is stored in the message index formation, so the take over party can get the message entity address by message index, need not to be concerned about whether the message entity formation reels.
According to message index formation reading and writing pointer and message index queue size, can calculate pending message bar number:
If write pointer>read pointer, then pending message bar number=write pointer-read pointer-1;
If write pointer<read pointer, then pending message bar number=queue size-read pointer+write pointer-1.
Fig. 7 example the inventive method calculate in typical case pending message bar number.Situation when Fig. 7 (a) is depicted as message index formation initialization, this moment, pending message bar number was 0.Fig. 7 (b) is depicted as the situation of write pointer>read pointer, and dash area is the index of pending message.Fig. 7 (c) is depicted as the situation of read pointer>write pointer, and dash area is the index of pending message, and this moment, write pointer unrolled to queue heads from rear of queue.Fig. 7 (d) is depicted as the situation that pending message bar number equals 0.
Specify in the specific embodiment of the inventive method DSP below to the message transport process of ARM, ARM to the message transport process of DSP similarly.
In order to send a message to ARM, as shown in Figure 6, DSP finishes associative operation according to the following steps:
(1) DSP reads forward message index formation write pointer, message entity formation write pointer from reading in mailbox, reads forward message index formation read pointer, message entity formation read pointer from writing out mailbox.
(2), calculate between available message entity queue empty, and judge whether enough to deposit the new information entity according to message entity formation reading and writing pointer and message entity queue size.This space must be continuously, if promptly write pointer is greater than read pointer, and formation afterbody free space calculates formation head free space when not enough.If the space enough then continue, otherwise send out after postponing again.
(3), calculate between available message index queue empty, and judge whether to write the new information index according to message index formation reading and writing pointer and message index queue size.The foundation of judging is to guarantee to differ at least one unit between read pointer and the write pointer.If the space enough then continue, otherwise send out after postponing again.
(4) the new information entity is write message entity formation (beginning to write), and upgrade the message entity formation write pointer (pointing to new information entity available cell afterwards) that reads in the mailbox from message entity formation write pointer indication unit.
(5) content of updating message index queue write pointer indication unit, the new information entity in the refer message entity queue, and upgrade the message index formation write pointer (sensing next unit) that reads in the mailbox.
Owing to read in the variation of mailbox write pointer, will trigger the interruption of ARM, ARM reads message from message queue according to the following steps:
(1) ARM reads forward message index formation write pointer from reading in mailbox, reads forward message index formation read pointer from writing out mailbox.
(2), calculate pending message bar number according to message index formation reading and writing pointer and message index queue size.If do not have Message Processing then return.
(3) unit of message index formation read pointer reach (if reach arrives the message index queue size then unrolls to 0) is according to the read pointer of the pending message entity of index calculation in this unit.And the message index formation read pointer in the mailbox is write out in renewal.
(4) read pending message entity and processing.
(5) upgrade the message entity formation read pointer that writes out in the mailbox.
(6) pending message bar number subtracts 1.
(7) circulation continued for (3) step, was 0 up to pending message bar number.
(8) circulation continued for (1) step.
To sum up, the double CPU communication method based on shared storage of the present invention is supported variable message-length; Although varying in size of message entity, every message be the message index refer message entity of corresponding one 4 byte all, so the take over party can read and write pointer by message index and calculates pending message bar number; When avoiding each passage only to use a circle queue, sending continuously in the process of message, wherein certain bar problem of the read-write pointer disorder that causes the back that breaks down.
The inventive method the processing messages winding around the time need not to fill particular value, when message entity rear of queue end spaces is deposited the new information bag inadequately, directly be stored in queue heads, its address deposits the message index formation in, therefore the take over party can get message entity by message index, need not at all to be concerned about whether the message bag reels.
The inventive method has been avoided the collision problem that reads while write of pointer; Transmit leg can send message continuously, and multiple messages is deposited in the buffer zone, and the take over party can handle the multiple messages in the buffer zone continuously.So just reduce transmit leg and take over party's stand-by period, reduced the number of times that the take over party repeats into interruption or inquiry, improved the utilization factor of CPU, improved data throughout and processing speed.
Should be understood that above-mentioned description at specific embodiment is comparatively detailed, can not therefore think the restriction to scope of patent protection of the present invention, scope of patent protection of the present invention should be as the criterion with claims.