This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CAN TIME MANAGEMENT UNIT EXAMPLE?

Hi, all.

I am trying to use the CAN time management unit in order to know if a message is transmitted in 20 msec, but I can not find any example about how to use it. 

The CAN bit rate is 500Kbps, therefore the time stamp counter register must be increased in 10000 units to count 20msec. I do not know how to do it and I hope someone can help me.

I have planned two options.

1st option:

- Every time that I want to transmit a message, I read CANTSC register, add 10000 units to its value and write this value in the MOTO register.

- Start the transmission (TRS=1) and wait until TA = 1 (transmission OK) or CANTOS bit = 1 (fault transmission)

- In the case of fault transmission, I suppose that I must abort the transmission using the corresponding CANTRR bit.

2nd option:

- Every time that I want to transmit a message, reset CANTSC register writing a 00000 and charge MOTO register with the value 10000,

- Start the transmission (TRS=1) and wait until TA = 1 (transmission OK) or CANTOS bit = 1 (fault transmission)

- In the case of fault transmission, I suppose that I must abort the transmission using the corresponding CANTRR bit.

I think both options can work successfully, but what happen in the first option if CANTSC value + 10000 is overflowing the maximum value of a 32-bit MOTO register.

The second option, I think it can work well with only 1 mailbox working but if you want to use more than one mailbox, we can not reset the CANTSC register every time that I charge a new mailbox with a new value. I Think CANTSC counter register is the same for all mailboxes.

Other doubt. According to the datasheet, TSC is a 32-bit free-running timer, so I can not stop/start it. Is it running since RESET the micro or is it running since I enable something in the CAN module?

Thanks in advance.

  • The following routine seems to work properly. It corresponds to the first option that I described in my previous mail.

    This routine does not take into account a possible overflow when loading the register  MOTO with the CANTSC read + 10000

    //Every time that I want to transmit a message, I read CANTSC register
    //Add 10000 units (10000* Tbit CAN = 10000* 2useg (at 500Kbps) to its value
    //and write this value in the MOTO register.
    ECanaShadow.CANTSC = ECanaRegs.CANTSC;
    ECanaMOTORegs.MOTO5= ECanaShadow.CANTSC + 10000;
    ECanaShadow.CANTOC.all = ECanaRegs.CANTOC.all;
    ECanaShadow.CANTOC.bit.TOC5 = 1; //Enable time-out funcionality for mailbox 5
    ECanaRegs.CANTOC.all = ECanaShadow.CANTOC.all;
    //Start the transmission (TRS=1)
    //and wait until TA = 1 (transmission OK) or CANTOS bit = 1 (fault transmission)
    ECanaShadow.CANTRS.all = 0;
    ECanaShadow.CANTRS.bit.TRS5 = 1; // Set TRS for mailbox transmission
    ECanaRegs.CANTRS.all = ECanaShadow.CANTRS.all;
    do
    {
    ECanaShadow.CANTA.all=ECanaRegs.CANTA.all;
    ECanaShadow.CANTOS.all=ECanaRegs.CANTOS.all;
    } while (ECanaShadow.CANTA.bit.TA5 == 0 && ECanaShadow.CANTOS.bit.TOS5 == 0);
    //In the case of fault transmission, I suppose that I must abort the transmission using the corresponding CANTRR bit.
    if(ECanaRegs.CANTA.bit.TA5 == 1)
    {
    ECanaShadow.CANTA.all = 0;
    ECanaShadow.CANTA.bit.TA5 = 1; // Clear Transmit Acknowledge #5
    ECanaRegs.CANTA.all = ECanaShadow.CANTA.all;
    return TRUE; /* Return with success */
    }
    else //if transmission timeout
    {
    ECanaShadow.CANTRR.all=ECanaRegs.CANTRR.all;
    ECanaShadow.CANTRR.bit.TRR5 = 1; // Set TRR to cancel mailbox transmission
    ECanaRegs.CANTRR.all=ECanaShadow.CANTRR.all;
    ECanaShadow.CANTOC.all = ECanaRegs.CANTOC.all;
    ECanaShadow.CANTOC.bit.TOC5 = 0; //Disable time-out funcionality for mailbox 5 & clear timeout status for mailbox 5
    ECanaRegs.CANTOC.all = ECanaShadow.CANTOC.all;
    return FALSE; /* Return with failure */
    }

  • Any suggestions to know if the TSC value + 10000 overflows the maximum capacity of the 32-bit MOTO register?

    According to the CAN module documentation (SPRUEU1) we have several indicator bits.

    Bit TCOFn-CANGIFn.16 indicates if the highest bit of the TSC counter changes to 1.

    Bit TCC-CANMC.14. Writing a 1 to this bit, the most significant bit of the TSC registr is cleared.

    With this two bits, I think that the following solution is possible, but i am not sure about it.

    if (ECanaRegs.CANGIF0.bit.TCOF0 == 1) //if the highest bit of TSC is 1
    {
    EALLOW;
    ECanaRegs.CANMC.bit.TCC=1; //The higest bit of TSC is reset to o
    EDIS;

    }
    ECanaShadow.CANTSC = ECanaRegs.CANTSC;
    ECanaMOTORegs.MOTO5= ECanaShadow.CANTSC + 10000;

    Does anyone disagree with this solution? or Other suggestion to solve the MOTO/TSC overflow problem?