AnsweredAssumed Answered

Can bus not working in normal mode

Question asked by l.marek on Jan 31, 2014
Latest reply on Feb 10, 2014 by l.marek
Hello, Iam working on my can project with discovery f429 kit. Now I have it conneced to MCP2551 transciever and to NI-CAN reader (as far as I know it should act as 2. node as it is sending acknowledgment)

Iam fine with loopback mode. I can read the data from NI-CAN and also I can receive them looped at my board. So it looks like can is working perfectly with baudrate and GPIO output. Problem is when I want to go normal mode. It does nothing.

Iam initialising it as loopback and at the end of all peripheral init I switch it to normal mode.

Thank you for help :)

AN_InitTypeDef CAN_InitStructure;
 
 
void CAN_RCC_Configuration(void)
{
    /* CAN1 clock enable */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
    /* CAN2 clock enable */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2, ENABLE);
 
    /* Init CAN GPIO clocks */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
}
 
void CAN_GPIO_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStructureCAN_RX;
    GPIO_InitTypeDef GPIO_InitStructureCAN_TX;
 
    /* GPIO CAN_RX Configuration */
    GPIO_InitStructureCAN_RX.GPIO_Pin = GPIO_Pin_12;
    GPIO_InitStructureCAN_RX.GPIO_Mode = GPIO_Mode_AF;
    //GPIO_InitStructureCAN_RX.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_InitStructureCAN_RX.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(GPIOB, &GPIO_InitStructureCAN_RX);
 
    /* GPIO CAN_TX Configuration */
    GPIO_InitStructureCAN_TX.GPIO_Pin = GPIO_Pin_13;
    GPIO_InitStructureCAN_TX.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructureCAN_TX.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructureCAN_TX.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_InitStructureCAN_TX.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructureCAN_TX);
 
    /* Connect CAN_RX & CAN_TX to AF9 */
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_CAN2); //CAN_RX = PB12
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_CAN2); //CAN_TX = PB13
}
 
void CAN_Configuration(void)
{
    /* CAN2 reset */
    CAN_DeInit(CAN2);
    /* CAN2 configuration */
    CAN_InitStructure.CAN_TTCM = DISABLE; // time-triggered communication mode = DISABLED
    CAN_InitStructure.CAN_ABOM = DISABLE; // automatic bus-off management mode = DISABLED
    CAN_InitStructure.CAN_AWUM = DISABLE; // automatic wake-up mode = DISABLED
    CAN_InitStructure.CAN_NART = DISABLE; // non-automatic retransmission mode = DISABLED
    CAN_InitStructure.CAN_RFLM = DISABLE; // receive FIFO locked mode = DISABLED
    CAN_InitStructure.CAN_TXFP = DISABLE; // transmit FIFO priority = DISABLED
    //CAN_InitStructure.CAN_Mode = CAN_Mode_LoopBack; // Debug can mode
    CAN_InitStructure.CAN_Mode = CAN_Mode_LoopBack; // Debug can mode
    CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; // synchronization jump width = 1
    CAN_InitStructure.CAN_BS1 = CAN_BS1_12tq; //10
    CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq; //4
    CAN_InitStructure.CAN_Prescaler = 6; // baudrate 500 kbps APB1 = 45MHz   tq + tq*BS1 + tq*BS2 73%
 
    CAN_Init(CAN2, &CAN_InitStructure);
}
 
void CAN_SetFilter(void)
{
    CAN_FilterInitTypeDef CAN_FilterInitStructure;
    CAN_InitTypeDef CAN_InitStructure;
 
    /* Default filter - accept all to CAN_FIFO*/
 
    CAN_InitStructure.CAN_Mode = CAN_OperatingMode_Initialization; // Can to init mode
 
    CAN_FilterInitStructure.CAN_FilterActivation = DISABLE; // Disable filter
 
    CAN_FilterInitStructure.CAN_FilterNumber = 14; // 0..13 for CAN1, 14..27 for CAN2
    CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_FIFO0;
    CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;
    CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_16bit;
    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);
 
    // este nastavit 11 bitove ID
}
 
void CAN_SetNVIC(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;
 
    NVIC_InitStructure.NVIC_IRQChannel                   = CAN2_RX0_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
 
    CAN_ITConfig(CAN2, CAN_IT_FMP0, ENABLE);
}
 
uint8_t CAN_Tq_CMD(int16_t torque)
{
    CanTxMsg TxMessage;
 
    TxMessage.StdId = REG_R_RX;
    TxMessage.ExtId = 0x0;
    TxMessage.RTR = CAN_RTR_DATA;
    TxMessage.IDE = CAN_ID_STD;
    TxMessage.DLC = 3;
    TxMessage.Data[0] = REGID_TQ_CMD;
    TxMessage.Data[1] = (uint8_t)torque;
    TxMessage.Data[2] = (uint8_t)(torque>>8);
 
    CAN_Transmit(CAN2, &TxMessage);
 
    if(CAN_TransmitStatus(CAN2, 0)) return 0;
    else return 1;
}
 
int16_t CAN_RxMessage(void)
{
    CanRxMsg RxMessage;
    int16_t torque = 0;
 
    CAN_Receive(CAN2,CAN_FIFO0,&RxMessage);
 
    if((RxMessage.Data[0]) == REGID_TQ_CMD)
    {
        torque |= RxMessage.Data[1];
        torque |= RxMessage.Data[2]<<8;
    }
 
    return torque;
}
 
void initCan(void)
{
    // Init clock
    CAN_RCC_Configuration();
 
    // Init GPIO pins
    CAN_GPIO_Init();
 
    // CAN Configuration
    CAN_Configuration();
 
    // Messages id configuration
    CAN_SetFilter();
 
    // Enable message interrupt
    CAN_SetNVIC();
 
    CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
    CAN_Init(CAN2, &CAN_InitStructure);
}




Outcomes