Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Better method of implementation

Status
Not open for further replies.
I am faced with some implementation confusion with regard to timing efficiency.
There are two core processors in single micro controller one core will be receiving peripheral CAN interrupts and the other will be processing these interrupt CAN data. Both will be running parallely
Method1:
Let us say in "x" time the number of interrupts received is "n" by one processor.

interrupt can_receive()
{
n++;
if(n == fixedvalue)
{
n =0;
}
}

The other will be processing these n messages like
main()
{
// some other functionality
copy n to local variable make n =0;
localvalue = n;
while(localvalue)
{
processMessages();
localvalue--;
}
}

Method2:

First processor
interrupt can_receive()
{
n++;
}

Second processor
main()
{
while(1)
{
// someother functionality
processMessage();
n--;
}

I have implemented the first method and the second method is something like Stack. My question is which is the better method in terms of timing. Few things are i do not know the time when the processMessages() will be executed in the sense by the time it executes n can be any number. And the data coming in the interrupt is stored in an array which i fixed to some value. if n exceeds this limit in the interrupt and by that time if it cannot be processed i make it "0" in the interrupt but i hope that the processMessage will be called before this.

i am not sure if i made my question clearly but please provide some advice. If require further information please let me know. The micro is s12x and the two cores are s12x and xgate.

Thanks in advance.
 
No difference.

Stack is LIFO (Last in First out). You probably need FIFO (First in First out), not a stack. It's not clear how your message buffer is organized from your description. If you use FIFO, the best structure is circular buffer, in which case you don't need n.

The most important thing is to organize access to data, so nothing gets destroyed when both processors want to access the same thing.
 
Sorry my mistake yes it is FIFO. As I told you my message buffer is an array like array[index]. Yes you said correctly that i have to organize my data correctly that is where i am facing the following problem

In the message interrupt routine the index will be incremented the pseudo code is like this as part of xgate code

unsigned char array[25];
void interrupt canreceive(void)
{
/* start locking all the variables using semaphores */
array[index];
index++;

if(index == 25)
{
index=0;
}
/* Unlock the semaphore */
}

as a part of s12x core the pseudo code is

void main(void)
{

while(1)
{
/* lock the semaphore and read the data into local array*/
local_index = index;
for(loop=0; loop < index; loop++)
local_array[loop] = array[index];
index=0;
/* Unlock the semaphore */

for(loop=0; loop<index; index++)
{
transmitMessage(array[index]);
}
}

The transmitMessage function checks the configuration file of some predefined messages and if it is part of it then transmits, i mean to say it will take some time before it transmits . The requirement is receive messages from one channel gateway to other channels (3 other channels depending on the address) without missing the sequence. Let us say i have to take from channel 1 and transmit to channel 2 where i am facing the problem. From the trace what i have observed is i receive msg1_ch1 and msg2_ch1 before my main starts processing it then it gateways msg1_ch1 to channel2 before it completes transmission of msg2_ch1 to channel 2 it receives message3. At one instant of time it receives 3 messages from channel1 instead of transmitting all the three on channel it only transmits one messages and skips the remaining two and transmits after some time and hence misses the sequence. I think i confused you. Can you give some advice.

channel 1 ---- msg1_ch1
channel 1 ---- msg2_ch1
channel 2 ---- msg1_ch1
channel 1 ---- msg3_ch1
channel 2 ---- msg2_ch1
channel1 ---- msg4_ch1
channel1 ----- msg5_ch1
channel1 ---- msg6_ch1
channel2 ---- msg5_ch1 ---- missed the sequence here should have been msg3_ch1. why it happens i don't know.

Problem with CAN bus itself i do not know. I want to say that if i receive message i can gateway correctly but if i receive more than 1 more message at one shot i am facing problems.
 
Your pseudo-code doesn't do what you say it is supposed to do. Google "circular buffer".
 
Can you please hint the condition under which it fails. Because iam facing problem when i receive continuous 3 messages the current implementation should not fail atleast in this case. Can you please provide your comments. I just wanted to know the root cause of the present issue. I have kept the circular buffer code ready and tested it for one time same problem is happening.
 
As written, you "main" code passes the semaphore part, then enter an endless loop transmitting more or less random values from array[index] while incrementing index in both the interrupt and the main. It would be challenging to find conditions where it doesn't fail.

It is also not great conceptually. Your interrupt code should push the message into FIFO, your main code should check if there's a message in FIFO, and if it is, pull it out of there. That's all. FIFO must be large enough to hold all messages at all times. The required size may be calculated based on the worst case scenario.

local_array (which BTW is not used in your code) doesn't add anything useful to the scheme.
 
Sorry my mistakes i corrected them i will very soon post the corrected code as i am trying to write it. I am still facing lot of issues.
 
i avoided the implementation of storing in the buffer and transmitting as i am always facing the buffer overflow situation, instead as soon as the message is received i gateway immediately to the other channels. As of now it works but i am not sure if the bus load is more how it will perform.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top