cancel
Showing results for 
Search instead for 
Did you mean: 

CAN Loopback/Normal

yannick23
Associate II
Posted on February 22, 2016 at 16:58

Hi Everyone,

I'm trying to make the CAN working on my board, I use a STM32F103, with the HAL example, (cube F1).

I did init as the example, I'm using the same pin PB8 & PB9. I calculate the baudrate to be at 250kBits

Like other I do see CAN trace on the output in loopback mode, nothing in Normal.

I do have a 120Ohm resistor,

I can see trace on PEAKCAN in loopback, not in Normal.

Can_Init(void)

{

    CAN_FilterConfTypeDef sFilterConfig;

    HAL_CAN_DeInit(&CanHandle);

    /*♯♯-1- Configure the CAN peripheral ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

    CanHandle.Instance = CANx;

    CanHandle.pTxMsg = &TxMessage;

    CanHandle.pRxMsg = &RxMessage;

    CanHandle.Init.TTCM = DISABLE;

    CanHandle.Init.ABOM = DISABLE;

    CanHandle.Init.AWUM = DISABLE;

    CanHandle.Init.NART = DISABLE;

    CanHandle.Init.RFLM = DISABLE;

    CanHandle.Init.TXFP = ENABLE;

    CanHandle.Init.Mode = CAN_MODE_LOOPBACK;//CAN_MODE_LOOPBACK;//CAN_MODE_NORMAL;

    CanHandle.Init.SJW = CAN_SJW_1TQ;

    CanHandle.Init.BS1 = CAN_BS1_14TQ;

    CanHandle.Init.BS2 = CAN_BS2_3TQ;

    CanHandle.Init.Prescaler = 16;

    /* 72 000 / 250 = 288     (250k)*/

    /* 288 / 16 = 18          (16 as random)*/

    /* 18-1 = 17              (-1 as datasheet)*/

    /* 17 * 0.875 = 14 -> BS1 (0.875 needed for canOPEN)*/

    /* 17-14 = 3 -> BS2*/

    CANx_FORCE_RESET();

    CANx_RELEASE_RESET();

    if (HAL_CAN_Init(&CanHandle) != HAL_OK)

    {

        /* Initiliazation Error */

        trace_Send(0, ''HAL_CAN_Init !'');

    }

    /*♯♯-2- Configure the CAN Filter ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

    sFilterConfig.FilterNumber = 0;

    sFilterConfig.FilterMode = CAN_FILTERMODE_IDMASK;

    sFilterConfig.FilterScale = CAN_FILTERSCALE_32BIT;

    sFilterConfig.FilterIdHigh = 0x0000;

    sFilterConfig.FilterIdLow = 0x0000;

    sFilterConfig.FilterMaskIdHigh = 0x0000;

    sFilterConfig.FilterMaskIdLow = 0x0000;

    sFilterConfig.FilterFIFOAssignment = 0;

    sFilterConfig.FilterActivation = ENABLE;

    sFilterConfig.BankNumber = 0;

    if (HAL_CAN_ConfigFilter(&CanHandle, &sFilterConfig) != HAL_OK)

    {

        /* Filter configuration Error */

        trace_Send(0, ''HAL_CAN_ConfigFilter !'');

    }

    /*♯♯-3- Configure Transmission process ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

    CanHandle.pTxMsg->StdId = 0x321;

    CanHandle.pTxMsg->ExtId = 0x01;

    CanHandle.pTxMsg->RTR = CAN_RTR_DATA;

    CanHandle.pTxMsg->IDE = CAN_ID_STD;

    CanHandle.pTxMsg->DLC = 8;

    CanHandle.pTxMsg->Data[0]= 0x30;

    CanHandle.pTxMsg->Data[1]= 0x31;

    CanHandle.pTxMsg->Data[2]= 0x32;

    CanHandle.pTxMsg->Data[3]= 0x33;

    CanHandle.pTxMsg->Data[4]= 0x34;

    CanHandle.pTxMsg->Data[5]= 0x35;

    CanHandle.pTxMsg->Data[6]= 0x36;

    CanHandle.pTxMsg->Data[7]= 0x37;

}

void HAL_CAN_MspInit(CAN_HandleTypeDef *hcan)

{

    GPIO_InitTypeDef   GPIO_InitStruct;

    /*♯♯-1- Enable peripherals and GPIO Clocks ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

    CANx_GPIO_CLK_ENABLE();

    CANx_CLK_ENABLE();

    /* Enable AFIO clock and Remap CAN PINs to PB8 and PB9*******/

    CANx_AFIO_REMAP_CLK_ENABLE();

    CANx_AFIO_REMAP_RX_TX_PIN();

    /*♯♯-2- Configure peripheral GPIO ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

    /* CAN1 TX GPIO pin configuration */

    GPIO_InitStruct.Pin = CANx_TX_PIN;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

    GPIO_InitStruct.Pull = GPIO_PULLUP;//GPIO_PULLUP;GPIO_NOPULL

    HAL_GPIO_Init(CANx_TX_GPIO_PORT, &GPIO_InitStruct);

    /* CAN1 RX GPIO pin configuration */

    GPIO_InitStruct.Pin = CANx_RX_PIN;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

    GPIO_InitStruct.Pull = GPIO_PULLUP;//GPIO_PULLUP;GPIO_NOPULL

    HAL_GPIO_Init(CANx_RX_GPIO_PORT, &GPIO_InitStruct);

    /*♯♯-3- Configure the NVIC ♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯♯*/

    /* NVIC configuration for CAN1 Reception complete interrupt */

    HAL_NVIC_SetPriority(CANx_RX_IRQn, 1, 0);

    HAL_NVIC_EnableIRQ(CANx_RX_IRQn);

}

void Can_Send(void)

{

    if(HAL_CAN_Transmit(&CanHandle, 800)){

        trace_Send(0, ''CAN Error'');

    }

    else{

        trace_Send(0, ''CAN Send'');

    }

}

I call can_send from the main every 1 sec, and I get the trace ''CAN Send'' in loopback, and ''CAN Error'' in normal.

Any help will be appreciated.

#can-no-data-loopback-normal #no-hablo-hal
4 REPLIES 4
Posted on February 22, 2016 at 17:33

Normal mode requires a responding node in the network in addition to your board, loopback does not.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
yannick23
Associate II
Posted on February 22, 2016 at 18:07

So plug the STM with a peakCAN wont work ?

Not even one frame will be sent though the CAN TX pin ?
Posted on February 22, 2016 at 18:53

I don't know, is it designed to be non-invasive and transparent?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
yannick23
Associate II
Posted on February 23, 2016 at 07:44

PCAN is active and has a 120Ohm on the end,

I can send messages to simulate data ''from'' and ''to'' the CAN node.

This ''installation'' is working with another STM32F105 Connectivity line.

Plus I have a oscilloscope (Picoscope) and this one as you can imagine is ''transparent'' without interfering on CAN.