AnsweredAssumed Answered

SMT32F4 Discovery CAN Example with Interrupt

Question asked by karadag.veysel on Aug 29, 2014
Latest reply on Aug 29, 2014 by Clive One
Hi everyone,

I need a help about CAN integration . I want to create a CAN network with using 2 STM32F discovery board. 

When I set  STM32, CAN_Loopback mode, When I send data to CAN also I will reacive it form my CAN1 interrupt.  

Interrupts look like working , But I cant read all data which I send 


CAN1 interrupt handler - its only send data to serial port.
/**
  * @brief  This function handles CAN1 RX0 Handler.
  * @param  None
  * @retval None
  */
 
void CAN1_RX0_IRQHandler(void)
{
  CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
 
 
  printf("CAN_RX %04X - %02X - %02X %02X %02X %02X %02X %02X %02X %02X \r\n",RxMessage.StdId,RxMessage.DLC,
                RxMessage.Data[0],RxMessage.Data[1],RxMessage.Data[2],RxMessage.Data[3],
                RxMessage.Data[4],RxMessage.Data[5],RxMessage.Data[6],RxMessage.Data[7]);
 
}


user button interrupt code is
void EXTI0_IRQHandler(void)
{
    static int8_t data;
  if(EXTI_GetITStatus(EXTI_Line0) != RESET)
  {
    /* Clear the EXTI line 0 pending bit */
    EXTI_ClearITPendingBit(EXTI_Line0);
 
    /* Toggle LED3 */
    CAN_Tx_CMD(data); // SEND DATA to CAN NETWORK
    toggle_led(LED2);
    data++;
 
  }
}


CAN Transmit code is just add one to data and my CAN data[0] will increase every button pressed
uint8_t CAN_Tx_CMD(int8_t data)
{
    CanTxMsg TxMessage;
 
    TxMessage.StdId = 0x0123;
    TxMessage.ExtId = 0x0;
    TxMessage.RTR = CAN_RTR_DATA;
    TxMessage.IDE = CAN_ID_STD;
    TxMessage.DLC = 1;
    TxMessage.Data[0] = ((uint8_t)data);
    TxMessage.Data[1] = ((uint8_t)0xFF);
    TxMessage.Data[2] = ((uint8_t)0xFA);
    TxMessage.Data[3] = ((uint8_t)0xFF);
    TxMessage.Data[4] = ((uint8_t)0xFA);
    TxMessage.Data[5] = ((uint8_t)0xFF);
    TxMessage.Data[6] = ((uint8_t)0xFA);
    TxMessage.Data[7] = ((uint8_t)0xFF);
 
 
    CAN_Transmit(CAN1, &TxMessage);
 
    if(CAN_TransmitStatus(CAN1, 0)==0){
        printf("TRANSMIT CAN ERROR\r\n");
        return 0;
    }else{
        return 1;
    }
}


CAN init function is ( I think all problem could be in this)
void can_init(void)
{
        CAN_InitTypeDef CAN_InitStructure;
        GPIO_InitTypeDef GPIO_InitStructure;
        CAN_FilterInitTypeDef CAN_FilterInitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
 
        /* CAN1 clock enable */
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
 
         /* Init CAN GPIO clocks */
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
 
         /* Connect CAN_RX & CAN_TX to AF9 */
        GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_CAN1); //CAN_RX = PB12
        GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_CAN1); //CAN_TX = PB13
 
         /* Configure CAN RX and TX pins */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
        GPIO_Init(GPIOD, &GPIO_InitStructure);
 
        /* CAN1 reset */
        CAN_DeInit(CAN1);
        /* 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_Normal; // 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(CAN1, &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 = 0; // 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);
 
        // CAN INTERRUPT Configration
 
        NVIC_InitStructure.NVIC_IRQChannel                   = CAN1_RX0_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority        = 1;
        NVIC_InitStructure.NVIC_IRQChannelCmd                = ENABLE;
        NVIC_Init(&NVIC_InitStructure);
 
        CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
 
}


So , When I pressed user button , my serial output is like 

CAN_RX 0123 - 01 - 00 00 00 00 00 00 00 00
CAN_RX 0123 - 01 - 01 00 00 00 00 00 00 00
CAN_RX 0123 - 01 - 02 00 00 00 00 00 00 00
CAN_RX 0123 - 01 - 03 00 00 00 00 00 00 00

Why  RxMessage[1] -- [7] is 00 ,  It  has to be FF FA FF FA ..  


Best Regards

Outcomes