Can I ensure CAN message received at receive end by polling some status bit in K60?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Can I ensure CAN message received at receive end by polling some status bit in K60?

1,013 Views
roshantr
Contributor I

Hi,

I am trying to communicate between two K60 targets via CAN. I have modified the CAN example project provided for K60 target.

I am using mailbox 0 for transmission and mailbox 1 for reception.

I need to transfer 1K bytes. I am sending it by chunks of 8bytes in a while loop.

When I am giving a delay after one transmit call FLEXCAN_Tx_mailbox() the data is properly receiving at receive end (other K60 target).

But when I am replacing the delay with the _lwevent_wait_ticks() it is not working.

Actually the event is generating from the ISR by checking the IFLAG bit for tranmit complete.

Receive interrupt is generating at the receive end. But "Message box Empty" error is returning on reading the mail box.

Can I replace the delay after transmit with some event mechanism?

Please advice.

Roshan

4 Replies

560 Views
perlam_i_au
Senior Contributor I

As you mention you have modified an example I just want to ask you (and confirm) with you your reception procedure, I am using the reference manual K60P144M150SF3RM and on 52.4.3 Receive Process section is described that before reception is necessary to (first) inactive the mailbox and (then) active again the mailbox; may be if something is missing here there could be a problem.

Other thing you mention is you are replacing delay with _lwevent_wait_ticks(), then does that mean if you use delay everything works fine?

Then you also mention "Actually the event is generating from the ISR by checking the IFLAG bit for tranmit complete." here you mean the reception event?

When you said you are getting a "Message box Empty" you mean when you read the message box you get a code like: 0b0100: EMPTY - MB is active and empty.?

I am trying to figure out what is happening but I am afraid that I do not have enough information, if you want to share your code I would to review it.


Have a nice day,
Perla Moncada

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos

560 Views
roshantr
Contributor I

Dear Perla Moncada,

Thank you very much for the reply.

I inserted the code portion below.

Please see my reply for your questions.

Other thing you mention is you are replacing delay with _lwevent_wait_ticks(), then does that mean if you use delay everything works fine?

[Roshan] - Yes. When I am replacing _lwevent_wait_ticks() in function FragmentMsgToCANpacket() with _time_delay(5) it is working fine. Data is properly receiving and can read from receive mail box in destination target.

Then you also mention "Actually the event is generating from the ISR by checking the IFLAG bit for tranmit complete." here you mean the reception event?

[Roshan] - It is not receive event. I checked whether the transmit buffer is empty before writing next data.

When you said you are getting a "Message box Empty" you mean when you read the message box you get a code like: 0b0100: EMPTY - MB is active and empty.?

[Roshan] - The function FLEXCAN_Rx_message() return the error code FLEXCAN_NO_MESSAGE.

When I checked the driver code the function is returning FLEXCAN_NO_MESSAGE error when the code value is 0x6(CAN_RX_MSG_BUFFER_OVERRUN) or 0x4(CAN_RX_MSG_BUFFER_EMPTY).

So I suspect a buffer overflow in my case.

Thank you,

Regards,

Roshan

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

void MY_FLEXCAN_ISR

(

    /* [IN] FlexCAN base address */

    pointer canPtr

)

{

    volatile CAN_MemMapPtr        canRegPtr;

    vuint_32                      tmpReg;

    vuint_32                      temp;

    canRegPtr = (CAN_MemMapPtr)canPtr;

    tmpReg = (canRegPtr->IFLAG1 & CAN_IMASK1_BUFLM_MASK);

    // check Tx/Rx interrupt flag and clear the interrupt

    if(tmpReg){

        /* clear the interrupt and unlock message buffer */

        /* Start CR# 1751 */

        _lwevent_set(&event, tmpReg);

        //canRegPtr->IFLAG1 |= tmpReg;

        //Disable flags except the interrupt flags for Rx FIFO

        canRegPtr->IFLAG1 = tmpReg;

        /* End CR# 1751 */

        temp = canRegPtr->TIMER;

    }/* Endif */

    // Clear all other interrupts in ERRSTAT register (Error, Busoff, Wakeup)

    tmpReg = canRegPtr->ESR1;

    if(tmpReg & FLEXCAN_ALL_INT){

        /* Start CR# 1751 */

        canRegPtr->ESR1 |= (tmpReg & FLEXCAN_ALL_INT);

        /* End CR# 1751 */

    } /* Endif */

     

    return;

}

void ReceiveTask(void)

{

    uint_32 result = -1;

    uint_32 interrupt = FLEXCAN_ENABLE;

    uint_32 rxMailboxNo = 1;

    uchar   dptr[8] = {0, 0, 0, 0, 0, 0, 0, 0};

    uint_32 dataLength = 0;

    uint_32 rxID = 0;

    int_32 rxIdentifier = PROCESSOR_ID_SELF;

    uint_32 format = FLEXCAN_STANDARD;

    SetupRxMailbox(rxMailboxNo, rxIdentifier, format);

    while(1)

    {

        //_time_delay(50);

        if (_lwevent_wait_ticks(&event, 1 << rxMailboxNo, FALSE, 0) != MQX_OK)

        {

            printf("\nEvent Wait failed\n");

        }

        printf("\nGot Receive Event\n");

        result = FLEXCAN_Lock_mailbox (CAN_DEVICE, rxMailboxNo);

        if(result != FLEXCAN_OK)

        {

            printf("\nLock mailbox failed. Error Code: 0x%lx\n", result);

        }

        result = FLEXCAN_Rx_message(CAN_DEVICE, rxMailboxNo, &rxID, format, &dataLength, &dptr, interrupt);

        if(result != FLEXCAN_OK)

        {

            printf("\nReceived error. Error Code: 0x%lx\n", result);

            switch(result)

            {

            case FLEXCAN_MESSAGE_OVERWRITTEN:

                printf("\nMessage overwritten\n");

                break;

            case FLEXCAN_INVALID_ADDRESS:

                printf("\Wrong device Number\n");

                break; 

            case FLEXCAN_INVALID_MAILBOX:

                printf("\Wrong message box number\n");

                break; 

            case FLEXCAN_NO_MESSAGE:

                printf("\Mail box is empty\n");

                break;

            case FLEXCAN_MESSAGE_FORMAT_UNKNOWN:

                printf("\Wrong msg format\n");

                break; 

            }

        }

        else

        {

            AssembleCanPktToMsg(rxID, &dptr[0]);

        }

        result = FLEXCAN_Unlock_mailbox (CAN_DEVICE);

        if(result != FLEXCAN_OK)

        {

            printf("\nUnlock mailbox failed. Error Code: 0x%lx\n", result);

        }

    }

}

void SetupRxMailbox(uint_32 rxMailboxNum, uint_32 rxIdentifier, uint_32 format)

{

    uint_32 interrupt;

    uint_32 result;

    interrupt = FLEXCAN_ENABLE;

    /** Initialize mailbox */

    result = FLEXCAN_Initialize_mailbox( CAN_DEVICE, rxMailboxNum, rxIdentifier, 8, FLEXCAN_RX, format, interrupt);

    printf("\nFLEXCAN rx mailbox initialization. result: 0x%lx\n", result);

    //result = FLEXCAN_Activate_mailbox(CAN_DEVICE, rxMailboxNum, FLEXCAN_RX_MSG_BUFFER_FULL);

    result = FLEXCAN_Activate_mailbox(CAN_DEVICE, rxMailboxNum, FLEXCAN_RX_MSG_BUFFER_EMPTY);

    printf("\nFLEXCAN mailbox activation. result: 0x%lx\n", result);

    /** Install ISR */

    if(FLEXCAN_ENABLE == interrupt)

    {

        result = FLEXCAN_Install_isr( CAN_DEVICE, rxMailboxNum, MY_FLEXCAN_ISR );

        printf("\nFLEXCAN RX ISR install. result: 0x%lx\n", result);

    }

}

void FragmentMsgToCANpacket(InMsg *pMsg) // Calling from Transmit task

{

    uint_8 index;

    uint_8 SourceID;

    uint_8 destID;

    uint_8 seqTypeData;

    uint_8 dataLSB;

    uint_16 dataLength;

    uint_32 txMailboxNo = 0;

    uint_8 tempCanPacket[7];

    boolean result = 0;

    if(NULL != pMsg)

    {

        SourceID = (pMsg->sourcePID) | 0x80;

        destID = (pMsg->destPID) & 0x7F;

        dataLength = (pMsg->datalength);

        dataLSB = (pMsg->datalength) & 0xFF;

        seqTypeData = ((pMsg->type) << 6) | ((pMsg->seq) << 2) | (((pMsg->datalength) >> 8) & 0x03);

        memset(tempCanPacket, 0, 7);

        tempCanPacket[0] = pMsg->destFID;

        tempCanPacket[1] = pMsg->sourceFID;

        tempCanPacket[2] = pMsg->requestID;

        tempCanPacket[3] = seqTypeData;

        tempCanPacket[4] = dataLSB;

        index = 5;

        InitTxMailbox(destID, SourceID);

        while(dataLength)

        {

            if(index < 7)

            {

                tempCanPacket[index++] = *(pMsg->data++);

                dataLength -= 1;

            }

            else

            {

                SendCANpacket(destID, SourceID, index+1, &tempCanPacket);

                SourceID = (pMsg->sourcePID);

                index = 0;

                memset(tempCanPacket, 0, 7);

                //_time_delay(5);

                if (_lwevent_wait_ticks(&event, 1 << txMailboxNo, FALSE, 0) != MQX_OK)

                {

                    printf("\nEvent Wait failed\n");

                }

                printf("\nGot Transmit Event\n");

            }

        }

        if(index != 0)

        {

            SendCANpacket(destID, SourceID, index+1, &tempCanPacket);

            //_time_delay(5);

            if (_lwevent_wait_ticks(&event, 1 << txMailboxNo, FALSE, 0) != MQX_OK)

            {

                printf("\nEvent Wait failed\n");

            }

            printf("\nGot Transmit Event\n");

        }

    }

    else

    {

        printf("No message to fragment\n");

    }

}

void InitTxMailbox(uint_8 destinationID, uint_8 sourceID)

{

    uint_32 interrupt = FLEXCAN_ENABLE;

    uint_32 format = FLEXCAN_STANDARD;

    uint_32 txMailboxNum = 0;

    FLEXCAN_Initialize_mailbox( CAN_DEVICE, txMailboxNum, destinationID, 8, FLEXCAN_TX, format,interrupt);

    FLEXCAN_Activate_mailbox(CAN_DEVICE, txMailboxNum, FLEXCAN_TX_MSG_BUFFER_NOT_ACTIVE);

    if(FLEXCAN_ENABLE == interrupt)

    {

        FLEXCAN_Install_isr( CAN_DEVICE, txMailboxNum, (pointer)MY_FLEXCAN_ISR  );

    }

}

void SendCANpacket(uint_8 destinationID, uint_8 sourceID, uint_8 length, pointer packetData)

{

    uint_32 interrupt = FLEXCAN_ENABLE;

    int_32 data_len_code = length;

    uint_32 txMailboxNum = 0;

    uint_32 format = FLEXCAN_STANDARD;

    if(NULL != packetData)

    {

        FLEXCAN_Tx_message(CAN_DEVICE, txMailboxNum, destinationID, sourceID, format, data_len_code, packetData);

    }

}

560 Views
prasadrao
Contributor I

Hello all i am having  problem Flexcan_Rx_Message not reading messages and returning no messages in the buffer.

if its required all will  post the code also.

@@

0 Kudos

560 Views
prasadrao
Contributor I

Same code i used for Receiving the message id's which are send by the bus master but i am not getting any id's.

FlexCAN_Rx_Message returning there is no message in the buffer(FLEXCAN_NO_MESSAGE:).

Please replay it

0 Kudos