Connect with us

STM32F105Rb CAN communication in loopback mode

Discussion in 'Microcontrollers, Programming and IoT' started by yogesh kaintura, Aug 25, 2014.

Scroll to continue with content
  1. yogesh kaintura

    yogesh kaintura

    5
    0
    Nov 15, 2012
    hello,
    i am presently working with stm32f1905rb micro controller,i want two stm32 controllers to communicate with each another using can protocol.so,for testing purpose i used one controller and operated it in loop back mode.when i did so,i didn't make any connection like connecting to transceiver or can bus,i just wrote down the program and ran it.for testing purpose i used inbuilt led to glow when receiving is done,and when i executed the program it started glowing,now i am confused because i have not used any can bus nor transceiver but still it is receiving
    now i want to confirm that is it necessary to use can bus for loop back mode.or i have done mistake in my program.
    please help me on this.
     
  2. KrisBlueNZ

    KrisBlueNZ Sadly passed away in 2015

    8,393
    1,270
    Nov 28, 2011
    I have removed the ALL CAPS from your thread title.

    You want us to tell you whether there is a mistake in your program? Were you hoping we could read your program using a crystal ball, or "remote viewing"? Or do you think it might be a good idea to post your program here?
     
    yogesh kaintura likes this.
  3. yogesh kaintura

    yogesh kaintura

    5
    0
    Nov 15, 2012
    well the main thing i want to know is that do we need can bus and can transreceiver when we are using just one controller and that too in loopback mode.
     
  4. yogesh kaintura

    yogesh kaintura

    5
    0
    Nov 15, 2012
    well my program is like this


    #include <__cross_studio_io.h>
    #include <stm32f10x.h>
    #include <stm32f10x_can.h>
    #include <stm32f10x_rcc.h>
    #include <stm32f10x_gpio.h>

    #define LED_PORT GPIOC


    GPIO_InitTypeDef GPIO_InitStructure;
    int i;
    typedef enum
    {
    FAILED = 0, PASSED = !FAILED
    } TestStatus;
    //__IO uint32_t ret = 0; /* for return of the interrupt handling */
    volatile TestStatus TestRx;

    /***************************************************************************//**
    * Declare function prototypes
    ******************************************************************************/
    //void NVIC_Configuration(void);
    TestStatus CAN_Polling(void);
    //TestStatus CAN_Interrupt(void);
    void CAN1_RX0_IRQHandler(void);
    void USB_LP_CAN1_RX0_IRQHandler(void);

    /***************************************************************************//**
    * @brief CAN LoopBack example.
    ******************************************************************************/
    void main(void)
    {
    /* CAN1 Periph clock enable */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOB, ENABLE);
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
    GPIO_Init(LED_PORT, &GPIO_InitStructure);

    /* NVIC Configuration */
    //NVIC_Configuration();

    for (i = 0; i < 2; i++);

    /* CAN transmit at 100Kb/s and receive by polling in loopback mode */
    TestRx = CAN_Polling();

    if (TestRx == FAILED)
    {
    GPIO_WriteBit(LED_PORT, GPIO_Pin_7, Bit_SET);
    }

    else
    {
    /* Turn on LED PC8 */
    GPIO_WriteBit(LED_PORT, GPIO_Pin_8, Bit_SET);
    }

    for (i = 0; i < 2000000; i++);
    while (1)
    {
    }
    }
    TestStatus CAN_Polling(void)
    {
    CAN_InitTypeDef CAN_InitStructure;
    CAN_FilterInitTypeDef CAN_FilterInitStructure;
    CanTxMsg TxMessage;
    CanRxMsg RxMessage;
    uint32_t i = 0;
    uint8_t TransmitMailbox = 0;

    /* CAN register init */
    CAN_DeInit( CAN1);
    CAN_StructInit(&CAN_InitStructure);

    /* CAN cell init */
    CAN_InitStructure.CAN_TTCM = DISABLE;
    CAN_InitStructure.CAN_ABOM = DISABLE;
    CAN_InitStructure.CAN_AWUM = DISABLE;
    CAN_InitStructure.CAN_NART = DISABLE;
    CAN_InitStructure.CAN_RFLM = DISABLE;
    CAN_InitStructure.CAN_TXFP = DISABLE;
    CAN_InitStructure.CAN_Mode = CAN_Mode_LoopBack;
    CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
    CAN_InitStructure.CAN_BS1 = CAN_BS1_8tq;
    CAN_InitStructure.CAN_BS2 = CAN_BS2_7tq;
    CAN_InitStructure.CAN_Prescaler = 5;
    CAN_Init(CAN1, &CAN_InitStructure);

    /* CAN filter init */
    CAN_FilterInitStructure.CAN_FilterNumber = 0;
    CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
    CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;
    CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0000;
    CAN_FilterInitStructure.CAN_FilterIdLow = 0x0000;
    CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0000;
    CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;
    CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0;
    CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
    CAN_FilterInit(&CAN_FilterInitStructure);

    /* transmit */
    TxMessage.StdId = 0x11;
    TxMessage.RTR = CAN_RTR_DATA;
    TxMessage.IDE = CAN_ID_STD;
    TxMessage.DLC = 2;
    TxMessage.Data[0] = 0xCA;
    TxMessage.Data[1] = 0xFE;

    TransmitMailbox = CAN_Transmit(CAN1, &TxMessage);
    i = 0;
    while ((CAN_TransmitStatus(CAN1, TransmitMailbox) != CANTXOK)
    && (i != 0xFF))
    {
    i++;
    }

    i = 0;
    while ((CAN_MessagePending(CAN1, CAN_FIFO0) < 1) && (i != 0xFF))
    {
    i++;
    }

    /* receive */
    RxMessage.StdId = 0x00;
    RxMessage.IDE = CAN_ID_STD;
    RxMessage.DLC = 0;
    RxMessage.Data[0] = 0x00;
    RxMessage.Data[1] = 0x00;
    CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);

    if (RxMessage.StdId != 0x11)
    {
    return FAILED;
    }

    if (RxMessage.IDE != CAN_ID_STD)
    {
    return FAILED;
    }

    if (RxMessage.DLC != 2)
    {
    return FAILED;
    }

    if ((RxMessage.Data[0] << 8 | RxMessage.Data[1]) != 0xCAFE)
    {
    return FAILED;
    }

    return PASSED; /* Test Passed */
    }
     
    Last edited: Aug 25, 2014
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-