cancel
Showing results for 
Search instead for 
Did you mean: 

CAN Bus Comminucation

Ramazan Gülcan
Associate III
Posted on March 08, 2018 at 10:32

Hi,

Do you have an example of CAN BUS Communication can be done with the current version HAL and CubeMx?

Regards

Ramazan

#can-bus

Note: this post was migrated and contained many threaded conversations, some content may be missing.
29 REPLIES 29
Posted on May 10, 2018 at 04:51

Hello Sir, 

I am testing CAN1 loop back on STM32F446RE nucleo board. but can not getting it worked. can you please help . 

basically i am not using transceivers and used example code found in the cubemx folder 1.21 latest version . 

the issue is 

  /*♯♯-5- Start the Reception process ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

if(HAL_CAN_GetRxFifoFillLevel(&hcan1, CAN_RX_FIFO0) != 1)

{

/* Reception Missing */

Error_Handler();

}

it is entering in to error_handler in the above function . 

Do i need to manually short tx and rx pin ? what should be the state of RX line ?

Can init :

static void MX_CAN1_Init(void)

{

hcan1.Instance = CAN1;

hcan1.Init.Prescaler = 16;

hcan1.Init.Mode = CAN_MODE_LOOPBACK;

hcan1.Init.SyncJumpWidth = CAN_SJW_1TQ;

hcan1.Init.TimeSeg1 = CAN_BS1_1TQ;

hcan1.Init.TimeSeg2 = CAN_BS2_1TQ;

hcan1.Init.TimeTriggeredMode = DISABLE;

hcan1.Init.AutoBusOff = ENABLE;

hcan1.Init.AutoWakeUp = DISABLE;

hcan1.Init.AutoRetransmission = DISABLE;

hcan1.Init.ReceiveFifoLocked = DISABLE;

hcan1.Init.TransmitFifoPriority = DISABLE;

if (HAL_CAN_Init(&hcan1) != HAL_OK)

{

_Error_Handler(__FILE__, __LINE__);

}

}

if you want i can send you full code. Thanks in advance . 

?brahim Yaylal?
Associate
Posted on May 10, 2018 at 13:30

Yo can find a solution here: 

https://www.udemy.com/stm32f4-discovery-ile-can-bus-haberlesme-keil-arm-cubemx/learn/v4/overview

 
Posted on May 10, 2018 at 13:17

Did you pullup the Can_Rx pin ?  You need to, if there is no transciever

I use checkCanRxFifos inside the

void CEC_CAN_IRQHandler(void) {

    /* USER CODE BEGIN CEC_CAN_IRQn 0 */

    CAN_IRQFlag = true;

    checkCanRxFifos();

    /* USER CODE END CEC_CAN_IRQn 0 */

    HAL_CAN_IRQHandler(&hcan);

    /* USER CODE BEGIN CEC_CAN_IRQn 1 */

    /* USER CODE END CEC_CAN_IRQn 1 */

}

     

void checkCanRxFifos(void) {

    int readCanBytecount;

    char canFifo1FullFlag = CAN->RF1R & CAN_RF1R_FMP1;

    if (canFifo1FullFlag) {

        {

            readCanBytecount = (CAN->sFIFOMailBox[1].RDTR & 0x0f);    

            canRxMsgBottomWord[canRxMsgIN]    = CAN->sFIFOMailBox[1].RDLR;

            canRxMsgTopWord[canRxMsgIN]    = CAN->sFIFOMailBox[1].RDHR;

            canRxMsgID[canRxMsgIN] = CAN->sFIFOMailBox[1].RIR >> 21;

            CAN->RF1R |= CAN_RF1R_RFOM1;                           // release FIFO

            canRxMsgLength[canRxMsgIN] =    readCanBytecount; 

            canRxMsgIN++;

            canRxMsgIN &= 0x3F;       // 64 entries only

            canRxMsgTableEMPTY = false;

            if (canRxMsgIN == canRxMsgOUT) canRxMsgTableFULL = true;

        }

        CAN->IER |= CAN_IER_FMPIE1;                              // (11)        Set FIFO1 message pending IT enable

    }

                     

    char canFifo0FullFlag = CAN->RF0R & CAN_RF0R_FMP0;

    if (canFifo0FullFlag) {

        {

            readCanBytecount         = (CAN->sFIFOMailBox[0].RDTR & 0x0f);   

            canRxMsgBottomWord[canRxMsgIN]    = CAN->sFIFOMailBox[0].RDLR;

            canRxMsgTopWord[canRxMsgIN]    = CAN->sFIFOMailBox[0].RDHR;

            uint32_t canRxmsgID =  CAN->sFIFOMailBox[0].RIR >> 21;

            CAN->RF0R |= CAN_RF0R_RFOM0;                          // release FIFO

            if(canRxMsgTableFULL) {    

                canRxMsgTableOverflow = true;   // now dump new frame...

            }else {

                canRxMsgID[canRxMsgIN] = canRxmsgID;

                canRxMsgLength[canRxMsgIN] = readCanBytecount;          // to signify FIFO0

                canRxMsgIN++;

                canRxMsgIN &= 0x3F;      // 64 entries only

                canRxMsgTableEMPTY = false;

                if (canRxMsgIN == canRxMsgOUT)

                    canRxMsgTableFULL = true;

            }

            

            //length = sprintf(string + length,'%08X, %08X :W',canFifoBuf.d32[0],canFifoBuf.d32[1]);

            //            for( int i = 0; i < readCanBytecount; i++){            

            //                canRxBuffer[canRxpointerIN++] =  canFifoBuf.d8[i];            

            //                canRxpointerIN &= 0xFF;

            //                if (canRxpointerIN == canRxpointerOUT )        CanRxbufferOverrun = true;

            //                //length += sprintf(string + length,'%02X, ',canFifoBuf.d8[i]);

            //            }

            //sprintf(string + length -2,'\n\r');        // remove 2 bytes, the last comma and space

            

        }

        CAN->IER |= CAN_IER_FMPIE0;          // (11)        Set FIFO1 message pending IT enable

    }

    //            if (length >0)  puts(string);

}

Posted on May 11, 2018 at 11:45

Thanks . i debug the problem and found that TX itself is failing . in the IRQ handler the control is reaching here . 

else if ((tsrflags & CAN_TSR_TERR0) != RESET)

{

/* Update error code */

errorcode |= HAL_CAN_ERROR_TX_TERR0;

}
Posted on May 11, 2018 at 12:24

i am using now 'loop back with silent ' , still same error 

HAL_CAN_ERROR_TX_TERR0

what should be the state of tx line ?

Posted on June 01, 2018 at 02:02

Hi,

I am reading your attached ppt file. It's very helpful. One question I have is I am using STM32L496 Nucleo-144 board and the Cube clock config is different than in the ppt file.

My question is 

Does the APB1/2 need to set to 48MHz? The original setting is 47.66666 and 23.66666.

Thanks,

Dick

Posted on June 01, 2018 at 02:43

>>The original setting..

Ok, because you've got some odd PLL settings. The APB1 and APB2 can both run at 80 MHz, so you've selected or accepted some sub-optimal numbers.

The frequency is off by 0.7%, might be tolerable, but WHY accept that margin erosion from the outset. Just pick some better factors of the PLL multipliers and dividers, it is just a simple application of ratios.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on June 01, 2018 at 18:23

I am using PEAK to send out CAN bus message to Nucleo-144. PEAK only have 20, 24, 30, 40, 60 80 MHz clock frequency available while I can only setup the Cube to have 24, 48, 80 MHz. 

So far I have try all of them but the HAL_CAN_GetRxMessage() function never success. The FIFO0 always empty.

I did try both FIFO0 and 1.

filter_config.FilterFIFOAssignment = CAN_RX_FIFO0;

I did my bit timing calculation from this website.

http://www.bittiming.can-wiki.info/

 

Any suggestions for FIFO always empty?

Thanks,

Dick

Posted on June 01, 2018 at 22:12

Please review my posts in this thread.

check out:

void checkCanRxFifos(void);  below here.

Posted on June 01, 2018 at 22:49

I am just pulling, not using any interrupt. The HAL function should work.