• AVR Freaks

Helpful ReplyHot!33CK256MP508 CAN using MCC

Author
_dex
Junior Member
  • Total Posts : 117
  • Reward points : 0
  • Joined: 2008/03/19 13:57:34
  • Location: 0
  • Status: offline
2020/07/30 06:02:44 (permalink)
0

33CK256MP508 CAN using MCC

I have generated CAN routines for 33CK256MP506 PIC (MPLABX 5.30 MCC 3.95.0). For the beginning and actually for the future I just need normal Mode; up to 8 bytes data; the 2.0b implication.
 
I have added CAN to my code as it was written in header file, currently only TX, just to test it.
I do not understand why it is transmitting the buffer data all the time? From my understanding it should just empty TX buffer just once. I am bit lost
 
Why it is sending the data in loop?
Why it is working in the loop even I pause ICD4 debugger?(ICD is set to Freeze Peripherials)
I see the data frame on the scope and as well on the Can BUS Analyzer (MCHP).
 
The CAN code was added so:
int main(void)
{
    // initialize the device
    SYSTEM_Initialize();
         
    CAN1_OperationModeSet(CAN_CONFIGURATION_MODE);
            if(CAN_CONFIGURATION_MODE == CAN1_OperationModeGet())
        {
            if(CAN_OP_MODE_REQUEST_SUCCESS == CAN1_OperationModeSet(CAN_NORMAL_2_0_MODE))
            {
                msg.msgId = 0x00001;
                msg.field.formatType = CAN_2_0_FORMAT;
                msg.field.brs = CAN_NON_BRS_MODE;
                msg.field.frameType = CAN_FRAME_DATA;
                msg.field.idType = CAN_FRAME_EXT;
                msg.field.dlc = DLC_8;
                msg.data = data;
            
                if(CAN_TX_FIFO_AVAILABLE == (CAN1_TransmitFIFOStatusGet(CAN1_TX_FIFO1) & CAN_TX_FIFO_AVAILABLE))
                {
                    CAN1_Transmit(CAN1_TX_FIFO1, &msg);
                }
            }
        }

    INTCON2bits.GIE = true;
    TMR1_Start();
    
    while (1)
    {
        CAN1_Tasks();
 ...my rest code
 
    }
    return 1;
}

 
Is there perhaps anywhere any AN or example describing CAN by MCC like this one for 8-bit PICs
AN2714
http://ww1.microchip.com/downloads/en/AppNotes/MPLAB-Code-Configurator-CAN2.0B-Module-For-PIC18-00002714A.pdf

 
Can I relay on it? It is ECAN whereas my is CAN/FD CAN also bit complicated .
---
Bellow is a transmit procedure, which as for today I do not completely understand due to dozens of indirect (masked) operands /operations.
 
CAN_TX_MSG_REQUEST_STATUS CAN1_Transmit(const CAN1_TX_FIFO_CHANNELS fifoChannel, CAN_MSG_OBJ *txCanMsg)
{
    CAN1_FIFO_INFO fifoInfo;
    CAN1_FIFO_InfoGet(fifoChannel, &fifoInfo);
    CAN_TX_MSG_REQUEST_STATUS txMsgStatus = CAN_TX_MSG_REQUEST_SUCCESS;
    
    // If CAN module is configured in Non-BRS mode and TX message object has BRS set
    if((txCanMsg->field.brs == 1) && (C1CONLbits.BRSDIS == 1))
    {
        txMsgStatus |= CAN_TX_MSG_REQUEST_BRS_ERROR;
    }
    
    // If CAN 2.0 mode, Tx Message object has more than 8 bytes of DLC Size
    // CAN 2.0 mode DLC supports upto 8 byte
    if(txCanMsg->field.dlc > DLC_8)
    {
       txMsgStatus |= CAN_TX_MSG_REQUEST_DLC_EXCEED_ERROR;
    }
    
    //If any CAN TX message object has DLC size more than CAN TX FIFO Payload size
    if(CAN1_DlcToDataBytesGet(txCanMsg->field.dlc) > fifoInfo.payloadSize)
    {
        txMsgStatus |= CAN_TX_MSG_REQUEST_DLC_EXCEED_ERROR;
    }
    
    if(CAN_TX_MSG_REQUEST_SUCCESS == txMsgStatus)
    {
        if(CAN_TX_FIFO_AVAILABLE == CAN1_TransmitFIFOStatusGet(fifoChannel))
        {
            if(*(fifoInfo.address) != NULL)
            {
                CAN1_MessageWriteToFifo((uint16_t *) *fifoInfo.address, txCanMsg);
                CAN1_TX_FIFO_MessageSendRequest(fifoChannel);
            }
        }
        else
        {
            txMsgStatus |= CAN_TX_MSG_REQUEST_FIFO_FULL;
        }
    }

    return txMsgStatus;
}

post edited by _dex - 2020/07/30 06:58:16
#1
du00000001
Just Some Member
  • Total Posts : 3855
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: 33CK256MP508 CAN using MCC 2020/07/30 07:05:41 (permalink) ☄ Helpfulby _dex 2020/07/31 00:26:00
5 (1)
_dex
I do not understand why it is transmitting the buffer data all the time? From my understanding it should just empty TX buffer just once. I am bit lost

 
Let me guess: you have no other "CAN node" on the bus.
CAN is a bit different from what you might know: the transmitter expects at least 1 node to acknowledge the message. If it is acknowledged, everything is fine. If there is no acknowledge, the transmission will be repeated 'til eternity come.
(If you want to contradict with "but I have a second node attached": in this case you may have forgotten to add the termination resistor(s). CAN might or might not work without at least 1 termination resistor of 120 Ohms. The standard says "total termination resistance: 60 Ohms.)

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#2
_dex
Junior Member
  • Total Posts : 117
  • Reward points : 0
  • Joined: 2008/03/19 13:57:34
  • Location: 0
  • Status: offline
Re: 33CK256MP508 CAN using MCC 2020/07/30 11:59:04 (permalink)
0
Hello and thank you.
This is good guess, I would say bingo guess. My first node is a PIC + CAN transmitter on the MCLV2 board and a second is ( I thought it is enough) CAN BUS Analyser from Microchip. The bitrate is set correct and as well Termination is on both side, 2x 120 Ohm.
 
What was wondering me, is that when analyser is set to Normal mode, transmission occurs just once. When it is set to "Listen only mode" then I have a never-ending transmission.
Is this a normal behaviour?
#3
du00000001
Just Some Member
  • Total Posts : 3855
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: 33CK256MP508 CAN using MCC 2020/07/30 12:43:12 (permalink)
0
-dex
... Is this a normal behavior?

 
YES!
 
"Listen mode" was not covered by the first CAN Specification, being introduced some increment later.
"Listen" does suppress any transmit activity - including the transmission of the acknowledge. Thus ...
The idea behind the listen mode is to be able to monitor a CAN bus without influencing it in any way (neither NACK nor ACK nor ruining a transmission by transmitting an error indication - just listen.

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#4
_dex
Junior Member
  • Total Posts : 117
  • Reward points : 0
  • Joined: 2008/03/19 13:57:34
  • Location: 0
  • Status: offline
Re: 33CK256MP508 CAN using MCC 2020/07/30 13:10:56 (permalink)
0
All right, this clarify a lot. Thank you.
 
How about that it is transmitting when debugging is pasused/halted. Project properties has active freezing peripherals, however I did not see there the CAN module.
 
 
#5
du00000001
Just Some Member
  • Total Posts : 3855
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: 33CK256MP508 CAN using MCC 2020/07/30 13:45:48 (permalink)
5 (2)
Check the datasheet: freezing the CAN controller might not apply even when freezing the peripherals is selected. (CAN controllers are quite sophisticated devices: freezing could massively disturb the CAN bus - disturbing all other nodes. Thus ...)

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#6
_dex
Junior Member
  • Total Posts : 117
  • Reward points : 0
  • Joined: 2008/03/19 13:57:34
  • Location: 0
  • Status: offline
Re: 33CK256MP508 CAN using MCC 2020/07/31 05:00:20 (permalink)
0
What could be a Config mode for the Can bus Analyzer? The user guide say nothing about operational Modes, perhaps it is obvious; Normal and Listen now I know.

post edited by _dex - 2020/07/31 05:28:19

Attached Image(s)

#7
_dex
Junior Member
  • Total Posts : 117
  • Reward points : 0
  • Joined: 2008/03/19 13:57:34
  • Location: 0
  • Status: offline
Re: 33CK256MP508 CAN using MCC 2020/07/31 05:34:34 (permalink)
0
du00000001
...
CAN is a bit different from what you might know: the transmitter expects at least 1 node to acknowledge the message. If it is acknowledged, everything is fine.
...

Question:
If there are multiple nodes on the CAN bus, Do they start answering ACK all simultaneously ?
and only one with Lowest ID (due to Arbitration) successfully will send ACK?, or they all will answer ACK in order from Lowest ID to highest one, due to arbitration?
#8
du00000001
Just Some Member
  • Total Posts : 3855
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: 33CK256MP508 CAN using MCC 2020/07/31 08:01:29 (permalink)
4.5 (2)
IIRC, the ACK slot is 7 bit time long. This is due to signal delay times. As the line length differs fron node to ode, they dovnot achöknoledge "simultaneously" in the strictest sense - more like "as soon as possible"  :)
And if only 1 node detects an error (e.g. due to noise), the whole transmission is discarded and repeated - for ALL nodes.
The ACK/NACK is part of the message frame - there are no no separate "ACK messages". This is possible due to the "dominant bit" physical scheme of the CAN bus: the transmitter has it's "receiving moments" (aka "readback") even during transmission, the receiving nodes have the possibility to transmit even when currently receiving a message. Quite confusing ?
 
Re op mode: it should be obvious: either you're enabled to transmit (that's normal mode") or you're completely incapable to transmit (listen mode). There's nothing inbetween.

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#9
du00000001
Just Some Member
  • Total Posts : 3855
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: 33CK256MP508 CAN using MCC 2020/07/31 08:04:42 (permalink)
0
Oh - to add to confusion:
CAN message IDs are meant to identify messages - not nodes.
(Although this is just a convention and not mandatory. But it's the common undrrstanding usually followed.)

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#10
_dex
Junior Member
  • Total Posts : 117
  • Reward points : 0
  • Joined: 2008/03/19 13:57:34
  • Location: 0
  • Status: offline
Re: 33CK256MP508 CAN using MCC 2020/07/31 12:05:24 (permalink)
0
It is confusing and to be honest till last post I thought it is kind of command-answer communication including ACK-ing. Now I see it is kind clever sollution simillar to GSM where time slots are used as well.
Interesting is that they doing ACK before end of frame, what consists of few bits. This confusing me but I must think about it.

I really have Config mode aviable in Analyzer, this must be from a reason.
#11
du00000001
Just Some Member
  • Total Posts : 3855
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: 33CK256MP508 CAN using MCC 2020/07/31 12:43:20 (permalink)
5 (1)
OK - if "Config mode" is considered a "mode":
to set up the configuration, you have to set some bit to allow for configuration. During configuration, the CAN controller is not functional in terms of transmitting/receiving.
The tools I know handle the configuration automatically correct (setting the config bit where required, clearing it as soon as it is no longer necessary). So there is no real need to habe a "configuration mode" selectable.

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#12
_dex
Junior Member
  • Total Posts : 117
  • Reward points : 0
  • Joined: 2008/03/19 13:57:34
  • Location: 0
  • Status: offline
Re: 33CK256MP508 CAN using MCC 2020/08/01 07:01:55 (permalink)
0
How can I get the data sent from Computer?
Bellow is the receive routine (from MCC), which only return status (Bool).
When I sent test data from analyzer ( 10 times  something like 0x22,0x33,0x44 every second)I do not see them. Data frames are visible on proper pin on scope. When I stop debugging I do not see my data in any variable, actually I have problem locate the right one it. In CAN tool my receive firo is named FIFO1( it is gray I can not rename it). I can not locate data in my variables.
bool CAN1_Receive(CAN_MSG_OBJ *rxCanMsg)
{
    uint8_t fifoChannel, count;
    CAN1_RX_FIFO_STATUS rxMsgStatus;
    CAN1_FIFO_INFO fifoInfo;
    bool status = false;
    
    //Iterate all receive FIFO's and read the message object
    for(count = 0; count < CAN1_NUM_OF_RX_FIFO; count++)
    {
        fifoChannel = rxFIFOMsg[count].channel;
        CAN1_FIFO_InfoGet(fifoChannel, &fifoInfo);
        rxMsgStatus = CAN1_RX_FIFO_StatusGet(fifoChannel);
        
        //If message object is available
        if(CAN_RX_MSG_AVAILABLE == (rxMsgStatus & CAN_RX_MSG_AVAILABLE))
        {
            if(*(fifoInfo.address) != NULL)
            {
                CAN1_MessageReadFromFifo((uint16_t *) *fifoInfo.address, rxCanMsg);
                CAN1_RX_FIFO_IncrementMsgPtr(fifoChannel);
                
                // Update the RX FIFO Head count for CAN1_ReceivedMessageCountGet function
                rxFIFOMsg[count].headCount += 1; //Update the read one message
                if(rxFIFOMsg[count].headCount >= fifoInfo.msgDeepSize)
                {
                    rxFIFOMsg[count].headCount = 0; //Reset the read message count
                }
                                
                //User have to clear manually RX Overflow status
                if(CAN_RX_MSG_OVERFLOW == (rxMsgStatus & CAN_RX_MSG_OVERFLOW))
                {
                    CAN1_RX_FIFO_OverflowStatusFlagClear(fifoChannel);
                }
                
                status = true;
            }
            
            break;
        }
    }
    
    return status;
}

post edited by _dex - 2020/08/01 07:11:32

Attached Image(s)

#13
du00000001
Just Some Member
  • Total Posts : 3855
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: 33CK256MP508 CAN using MCC 2020/08/03 10:29:37 (permalink)
4 (1)
Sorry - this response required a full-blown keyboard...
  • CAN is not UART !
  • To receive "something", you have to set up a message buffer with an ID that matches the ID sent.
    (OK - "matches" may include setting some bits of the ID to "don't care" via the message filters. Then only the "care" bits have to match.)
  • If your receive routine is fast enough (or messages on the bus are rare), you may go for an RX filter that has all ID bits don't care. This way you'll receive whatever was sent into the same message buffer. (You still have to set up a message ID for this buffer, but the value would be without concern.)
  • Whatever message ID you wrote for the TRX message(s): when a message has been received into an RX buffer, the message ID to be read is the one of the message received, not the one originally written.
Enough to continue "experimenting" ?

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#14
_dex
Junior Member
  • Total Posts : 117
  • Reward points : 0
  • Joined: 2008/03/19 13:57:34
  • Location: 0
  • Status: offline
Re: 33CK256MP508 CAN using MCC 2020/08/03 12:40:33 (permalink)
0
Hello enough, yes. Transmitting part is done by the CAN Analyzer software from MCHP where I have set an ID and few "payload" bytes. Thus "TX" frame should be fine. I can repeat test frame n times every x miliseconds.
I did not set any filters thus I thought my CAN should receive the frame and keep the data somwhere in variables. Unfortunately I can locate it, as for today
#15
du00000001
Just Some Member
  • Total Posts : 3855
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: 33CK256MP508 CAN using MCC 2020/08/03 13:35:29 (permalink)
0
Yes. And reception requires a buffer allocated, the message ID filled-in.
CAB reception ressembles tuning a radio receiver: only the messages "tuned-in" will be received.

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#16
Jump to:
© 2020 APG vNext Commercial Version 4.5