Skip to main content
Ramazan Gülcan
Associate III
March 8, 2018
Solved

CAN Bus Comminucation

  • March 8, 2018
  • 29 replies
  • 6389 views
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.
This topic has been closed for replies.
Best answer by T J
Posted on March 09, 2018 at 14:45

you need a pull up on both Rx pins.

did you set the ABOM bit ?

now you must go out and buy a CANBUS transceiver board and start transmitting from there first.

work on your receiver code first, then transmitter.

I used the CanDo unit to start. Running totally in a few days.

29 replies

Szymon PANECKI
Senior III
March 8, 2018
Posted on March 08, 2018 at 11:28

Hello Ramazan,

Each STM32 Cube package contains example applications for CAN. I don't know which STM32 MCUs family you want to use, so let me provide you an example for STM32L4. You acn find application under this link:

...\STM32Cube_FW_L4_V1.0\Projects\STM32L476G_EVAL\Examples\CAN

In general you need to go to your Cube package,open Projects folder, select your board, open Examples folder and refer to CAN subfolder. It should contain CAN_Networking example application.This example is not available for all boards, but for many of them it is.

Additionally let me share with you presentation, which guides hwo to develop simple CAN application for STM32L4 MCUs step by step from scratch. Please find this presentation attached. However please note, that this presentation is now a bit obsolete, because it uses not up to date CAN driver. CAN driver was recently updated, so if you would like to use the latest implementation, which is of course recommended, please refer to

https://community.st.com/thread/47095-stm32f4xx-rework-of-hal-can-driver-examples?commentID=186316#comment

.

Regards

Szymon

________________

Attachments :

STM32L4 CAN.pdf : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HxuS&d=%2Fa%2F0X0000000b28%2FOa5m1KEqVQ1Xg4yxLQUNFFR.40q3lDem2bmMMYWL6ns&asPdf=false
gowtham K
Associate II
March 8, 2018
Posted on March 08, 2018 at 11:37

Thanks for the answer.

Even I am facing an issue with the CAN communication.

Microcontroller used: STM32F769NI (stm32f769-disc1)

I tried to use the examples from stm32f7cube (contains the CAN old drivers).

I have tried with the CAN_loopback example along with 'System workbench for STM32'. However it didn't workout.

Do you have any suggestions?

Szymon PANECKI
Senior III
March 8, 2018
Posted on March 08, 2018 at 11:56

HelloGowtham,

Please provide a more detailed description wahtexactly is not working. Otherwise it is difficult to say what could be a root cause of your issue.

Some time ago I wasworking with STM32F7 and CAN. My hardware platform was eval baord with STM32F756NG. I prepared a presentation as well how to develop simple CANapplication. Please find it attached. Please try to follow all steps from slides. I hope you will suceed to run the CAN communication.

Regards

Szymon

________________

Attachments :

STM32F7 CAN.pdf : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HxuN&d=%2Fa%2F0X0000000b26%2FL6GGGZ40vPawqPZNzl9HddhGeQVIhDnCe.9jzEfXzL4&asPdf=false
T J
Senior III
March 8, 2018
Posted on March 08, 2018 at 15:53

I can help you if you want. I have implemented CAN on the '767 and '091

I use HAL and the Cube,

I use HAL but short cut the interrupt system.

whereby 'Hal' is left interrupt enabled and always ready to receive a packet.

my short circuit pumps the FiFo data into RxFrames[64] deep, HAL cant see it so its left enabled.

then my foreground process can see the RxFrames count move and processes it.

that way, when my singular foreground process uses printf, the console never has intertwined messages.

But the best starting point, is to purchase a CANBUS transceiver and start it transmitting your required frame every 1 second at first, and when you are ready crank it up to a frame every 10mS

I purchased a CAN DO unit.had 250KHz CAN running in a few days.

then you find out about Bus Contention.

my rule is no two units can transmit the same messageID.

I encode 2 parts of information in the messageID

the Address 0x040x .. 0x06Fx   is my address range for a unit, only 48 units.

then I use 0x0xx0.. 0x0xxF as 16 command channels or endpoints you may say;

a messageID of 0x0415 is from my Terminal Master requesting Unit 2 to echo this next char

a messageID of 0x0416 is from Unit 2 and has the echo response

these messageIDs have a unique known address source and contain instructions/flags pertaining to the contents of the data following.

T J
Senior III
March 30, 2018
Posted on March 30, 2018 at 01:08

I pull the data from the fifos before HAL gets to it.

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);

}

Luca Giuliodori
Associate III
March 30, 2018
Posted on March 30, 2018 at 10:14

One question: if 'canFifo1FullFlag ' or 'canFifo0FullFlag' are greater than 1 you read however only the first message and you have to wait next interrupt to read the remaining messages, is it correct?

?brahim Yaylal?
Visitor II
May 10, 2018
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