Hi everyone,
I have seen an implementation of the CAN driver on the Xgate and the processing on the s12x using the following code. The goal of the project is to do processing on some msgs and direct to other channels some other messages.
In the Xgate core
void __interrupt XGATE_CAN_RECEIVE(XGCANstruct * channel)
{
release[msg_index]=1;
// do some processing
rxmsg[msg_index].msgID = rxid;
release[msg_index] =0;
msg_index++;
if(msg_index == 10)
msg_index=0;
-sif();
}
In the s12x core
void __interrupt CAN1_rxnotificationisr(void)
{
XGATE.xgif_50 = BIT9;
}
main()
{
while(1)
{
// some logic here
can_rx();
}
}
void can_rx(void)
{
for(index=0; index<=msg_index; index++)
{
while(release[index] == 1)
{
}
// do the processing some big logic here
}
msg_index=0;
}
i had serious doubts about the above implementation and modified as shown below.
Xgate core
void __interrupt XGATE_CAN_RECEIVE(XGCANstruct * channel)
{
// do some processing
rxmsg[l_canrxcounter_u8].msgID = rxid;
-sif();
}
S12x core
void __interrupt CAN1_rxnotificationisr(void)
{
l_canrxcounter_u8++;
}
and in the main file
int main(void)
{
while(1)
{
// some big logic here
can_rx();
}
void can_rx(void)
{
if(l_canrxcounter_u8 > 0)
{
//do the message processing
rxmsg[l_canrxcounter_u8-1];
l_canrxcounter_u8--;
// do processing on
}
}
I have written the variable l_canrxcounter_u8 as the shared variable between the two cores without semaphores. Please help me if it is correct and do i need to implement using semaphores. How should i ensure that i am not missing any messages, because in the first implementation i saw that a lot of messages are received but not all by passed to other channels, some messages are missing and also not immediately after the message received it is happening after some messages.
For example
Channel1: msg1 rxed
channel1:msg2 rxed
channel2: msg1 txed
channel1:msg3 rxed
channel1: msg4 rxed
channel2: msg2 txed.
after that the sequence is completely missing. Please help me.
Thank you very much i will go through the code. If i have any clarifications in the code can you help me out.
You need to use HW semaphore in your case (where both cores write to the same variable). In multiCPU application++ and -- are not atomic operations. Even though S12X CPU can do ++ or -- using single CPU instruction, this instruction involves 1) reading variable to CPU, 2) calculating new value of variable, and 3) writing new calculated value back to vatiable. XGATE core may read or write the same variable at any stage. Without semaphore it easily may look like some ++ or -- are not executed.
For the same reason you need to use HW semaphore even toggling bits in the same peripheral register, even if both cores are toggling different bits of the same register.