AnsweredAssumed Answered

STM2F105RCT6 CAN problem

Question asked by afzal.uzair on Jun 11, 2013
Latest reply on Jun 11, 2013 by Jack Peacock
I am using Dual CAN on STM32F105RCT6. I am using two TJA1040 CAN transceivers on both CAN1 and CAN2. Output CANL of CAN1 is connected with CANL of CAN2 and same for CANH. I want to send data through CAN1 and receive through CAN2. I am not getting any kind of response on inputs and outputs of both transceivers. In loopback mode, code is working fine but in normal mode nothing is working. I don't think that code is wrong. Please help !!!
/**
  * @brief  CAN1 Bus Configuration 
  * @param  None
  * @retval None
  */     
void CAN1_Config(void)
{
     // Enable GPIOB clock                                                   
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA| RCC_APB2Periph_AFIO , ENABLE);
     // CAN Periph clock enable 
     RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1 , ENABLE);
     
     // GPIO CAN1_RX (PA.11) Configuration 
     GPIO_InitStructureCAN_RX.GPIO_Pin = GPIO_Pin_11;
     GPIO_InitStructureCAN_RX.GPIO_Mode = GPIO_Mode_IPU;
     GPIO_InitStructureCAN_RX.GPIO_Speed = GPIO_Speed_10MHz;
     GPIO_Init(GPIOA, &GPIO_InitStructureCAN_RX);


     // GPIO CAN1_TX (PA.12) Configuration 
     GPIO_InitStructureCAN_TX.GPIO_Pin = GPIO_Pin_12;
     GPIO_InitStructureCAN_TX.GPIO_Mode = GPIO_Mode_AF_PP;
     GPIO_InitStructureCAN_TX.GPIO_Speed = GPIO_Speed_10MHz;
     GPIO_Init(GPIOA, &GPIO_InitStructureCAN_TX);
     
     // CAN1 Filter,FIFO and Baudrate settings 
     CAN1_Struct_Init();
     
     
     /* Enable CAN1 RX0 interrupt IRQ channel */
     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
     
     
     // IT Configuration for CAN1 FIFO 0 message received interrupt
     CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
     
     // Receive message initialized
     RxMessage.StdId = 0x00;
     RxMessage.ExtId = 0x00;
     RxMessage.IDE = CAN_ID_STD;
     RxMessage.DLC = 0;
  RxMessage.FMI = 0;
  for (i = 0; i < 8; i++)
  {
    RxMessage.Data[i] = 0;
  }
}
/**
  * @brief  Configures the CAN, transmit and receive by polling
  * @param  None
  * @retval None
  */
void CAN1_Struct_Init(void)
{
  // CAN1 register init 
  CAN_DeInit(CAN1);
  // CAN1 cell init 
     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;//CAN_Mode_LoopBack;// CAN_Mode_Normal;
     
  CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
     CAN_InitStructure.CAN_BS1 = CAN_BS1_2tq;
  CAN_InitStructure.CAN_BS2 = CAN_BS2_3tq;
  // CAN1 Baudrate = 250kbps (System clock and CAN clock is 36MHz)
     // CAN_Prescaler value= CAN_clock/(required_Baudrate*total_number_of_tq)
     // In present case, total_number_of_tq=CAN_SJW_1tq+CAN_BS1_2tq+CAN_BS2_3tq=6*tq
     // so CAN_Prescaler= 36MHz/(250kbps*6)=24
  
  CAN_InitStructure.CAN_Prescaler = 24;
  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 = CAN_FilterFIFO0;
  CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
  CAN_FilterInit(&CAN_FilterInitStructure);          
}




void CAN1_transmit_message(void)
{
     
     // Transmit message initialized
  TxMessage.StdId=0xC1;
     TxMessage.ExtId=0x00;
  TxMessage.RTR=CAN_RTR_DATA;
  TxMessage.IDE=CAN_ID_STD;//CAN_ID_STD   CAN_ID_EXT
  TxMessage.DLC=8;
  TxMessage.Data[0]=variable++;
  TxMessage.Data[1]=variable++;
     TxMessage.Data[2]=variable++;
     TxMessage.Data[3]=variable++;
     TxMessage.Data[4]=variable++;
     TxMessage.Data[5]=variable++;
  TxMessage.Data[6]=variable++;
     TxMessage.Data[7]=variable++;
     CAN_WakeUp(CAN1);
  TransmitMailbox=CAN_Transmit(CAN1, &TxMessage);
  i = 0;
  while((CAN_TransmitStatus(CAN1, TransmitMailbox) != CANTXOK) && (i != 0xFFFF))
  {
    i++;
  }
     UART4_SendData_s("CAN1 message://///////////////////////////\n\r");
     


}
/**
  * @brief  This function handles CAN1 RX FIFO 0 Handler.
  * @param  None
  * @retval None
  */
void CAN1_RX0_IRQHandler(void)
{


     if(CAN_GetITStatus(CAN1, CAN_IT_FMP0) != RESET)
     {
          UART4_SendData_s("CAN1 FIFO 0 IRQ:\n\r");
          CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);


          UART4_SendData_s("message StdID=");
          temp=(uint8_t) ((RxMessage.StdId & 0xFF000000)>>24);
          sprintf (buffer, "%X",temp );
          UART4_SendData_s(buffer);
          temp=(uint8_t) ((RxMessage.StdId & 0x00FF0000)>>16);     
          sprintf (buffer, "%X",temp );
          UART4_SendData_s(buffer);
          temp=(uint8_t) ((RxMessage.StdId & 0x0000FF00)>>8);     
          sprintf (buffer, "%X",temp );
          UART4_SendData_s(buffer);
          temp=(uint8_t) ((RxMessage.StdId & 0x000000FF));     
          sprintf (buffer, "%X",temp );
          UART4_SendData_s(buffer);
          UART4_SendData_s("\n\r");
     
          UART4_SendData_s("message ExtId=");
          temp=(uint8_t) ((RxMessage.ExtId & 0xFF000000)>>24);
          sprintf (buffer, "%X",temp );
          UART4_SendData_s(buffer);
          temp=(uint8_t) ((RxMessage.ExtId & 0x00FF0000)>>16);     
          sprintf (buffer, "%X",temp );
          UART4_SendData_s(buffer);
          temp=(uint8_t) ((RxMessage.ExtId & 0x0000FF00)>>8);     
          sprintf (buffer, "%X",temp );
          UART4_SendData_s(buffer);
          temp=(uint8_t) ((RxMessage.ExtId & 0x000000FF));     
          sprintf (buffer, "%X",temp );
          UART4_SendData_s(buffer);
          UART4_SendData_s("\n\r");
          


          UART4_SendData_s("CAN1 RTR=");
          temp=(uint8_t) ((RxMessage.RTR & 0xFF));     
          sprintf (buffer, "%X",temp );
          UART4_SendData_s(buffer);
          UART4_SendData_s("\n\r");
          UART4_SendData_s("CAN1 IDE=");
          temp=(uint8_t) ((RxMessage.IDE & 0xFF));     
          sprintf (buffer, "%X",temp );
          UART4_SendData_s(buffer);
          UART4_SendData_s("\n\r");
          UART4_SendData_s("CAN1 DLC=");
          temp=(uint8_t) ((RxMessage.DLC & 0xFF));     
          sprintf (buffer, "%X",temp );
          UART4_SendData_s(buffer);
          UART4_SendData_s("\n\r");
          UART4_SendData_s("CAN1 FMI=");
          temp=(uint8_t) ((RxMessage.FMI & 0xFF));     
          sprintf (buffer, "%X",temp );
          UART4_SendData_s(buffer);
          UART4_SendData_s("\n\r");
          
          UART4_SendData_s("CAN1 message=");
          for (k=RxMessage.DLC;k!=0 ;k--)
          {
               sprintf (buffer, "%X",((RxMessage.Data[k-1]& 0xF0)>>4) );
               UART4_SendData_s(buffer);
               sprintf (buffer, "%X",(RxMessage.Data[k-1]& 0x0F) );
               UART4_SendData_s(buffer);
          }
          
          Delay(0xfffff);
          Delay(0xfffff);
          Delay(0xfffff);
          Delay(0xfffff);
          Delay(0xfffff);
          Delay(0xfffff);
          Delay(0xfffff);
          Delay(0xfffff);
          Delay(0xfffff);
          Delay(0xfffff);
          Delay(0xfffff);
          Delay(0xfffff);


          
          UART4_SendData_s("\n\r");
          UART4_SendData_s("\n\r");
          UART4_SendData_s("\n\r");
     }
     
}




///////////////////////////////////////////////////////////// CAN2
////////////////////////////////////////////////////////////




/**
  * @brief  CAN2 Bus Configuration 
  * @param  None
  * @retval None
  */     
void CAN2_Config(void)
{
     // Enable GPIOB clock                                                   
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB| RCC_APB2Periph_AFIO , ENABLE);
     // CAN Periph clock enable 
     RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2 , ENABLE);
     
     // CAN pins are fully remapped to PB.08,PB.09 etc.
     //GPIO_PinRemapConfig(GPIO_Remap1_CAN1  , ENABLE);
     
     // GPIO CAN2_RX (PB.12) Configuration 
     GPIO_InitStructureCAN_RX.GPIO_Pin = GPIO_Pin_12;
     GPIO_InitStructureCAN_RX.GPIO_Mode = GPIO_Mode_IPU;
     GPIO_InitStructureCAN_RX.GPIO_Speed = GPIO_Speed_10MHz;
     GPIO_Init(GPIOB, &GPIO_InitStructureCAN_RX);


     // GPIO CAN2_TX (PB.13) Configuration 
     GPIO_InitStructureCAN_TX.GPIO_Pin = GPIO_Pin_13;
     GPIO_InitStructureCAN_TX.GPIO_Mode = GPIO_Mode_AF_PP;
     GPIO_InitStructureCAN_TX.GPIO_Speed = GPIO_Speed_10MHz;
     GPIO_Init(GPIOB, &GPIO_InitStructureCAN_TX);
     
     // CAN2 Filter,FIFO and Baudrate settings 
     CAN2_Struct_Init();
     
     /* Enable CAN2 RX0 interrupt IRQ channel */
     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX0_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
     
     // IT Configuration for CAN1 FIFO 0 message received interrupt
     CAN_ITConfig(CAN2, CAN_IT_FMP0, ENABLE);
     
     // Receive message initialized
     RxMessage.StdId = 0x00;
     RxMessage.ExtId = 0x00;
     RxMessage.IDE = CAN_ID_STD;
     RxMessage.DLC = 0;
  RxMessage.FMI = 0;
  for (i = 0; i < 8; i++)
  {
    RxMessage.Data[i] = 0;
  }
}
/**
  * @brief  Configures the CAN, transmit and receive by polling
  * @param  None
  * @retval None
  */
void CAN2_Struct_Init(void)
{
  // CAN1 register init 
  CAN_DeInit(CAN2);
  // CAN1 cell init 
     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;//CAN_Mode_LoopBack;// CAN_Mode_Normal;
     
  CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
     CAN_InitStructure.CAN_BS1 = CAN_BS1_2tq;
  CAN_InitStructure.CAN_BS2 = CAN_BS2_3tq;
  // CAN1 Baudrate = 250kbps (System clock and CAN clock is 36MHz)
     // CAN_Prescaler value= CAN_clock/(required_Baudrate*total_number_of_tq)
     // In present case, total_number_of_tq=CAN_SJW_1tq+CAN_BS1_2tq+CAN_BS2_3tq=6*tq
     // so CAN_Prescaler= 36MHz/(250kbps*6)=24
  
  CAN_InitStructure.CAN_Prescaler = 24;
  CAN_Init(CAN2, &CAN_InitStructure);


  // CAN filter init 
  CAN_FilterInitStructure.CAN_FilterNumber = 14;
  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 = CAN_FilterFIFO0;
  CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
  CAN_FilterInit(&CAN_FilterInitStructure);     
     
     
     
}




void CAN2_transmit_message(void)
{
     UART4_SendData_s("CAN2 message://///////////////////////////\n\r");
     // Transmit message initialized
  TxMessage.StdId=0xC1;
     TxMessage.ExtId=0x00;
  TxMessage.RTR=CAN_RTR_DATA;
  TxMessage.IDE=CAN_ID_STD;//CAN_ID_STD   CAN_ID_EXT
  TxMessage.DLC=8;
  TxMessage.Data[0]=variable++;
  TxMessage.Data[1]=variable++;
     TxMessage.Data[2]=variable++;
     TxMessage.Data[3]=variable++;
     TxMessage.Data[4]=variable++;
     TxMessage.Data[5]=variable++;
  TxMessage.Data[6]=variable++;
     TxMessage.Data[7]=variable++;
     CAN_WakeUp(CAN2);
  TransmitMailbox=CAN_Transmit(CAN2, &TxMessage);
  i = 0;
  while((CAN_TransmitStatus(CAN2, TransmitMailbox) != CANTXOK) && (i != 0xFFFF))
  {
    i++;
  }
     


}
/**
  * @brief  This function handles CAN2 RX FIFO 0 Handler.
  * @param  None
  * @retval None
  */
void CAN2_RX0_IRQHandler(void)
{
     UART4_SendData_s("CAN2 FIFO 0 IRQ:\n\r");
     if(CAN_GetITStatus(CAN2, CAN_IT_FMP0) != RESET)
     {
          UART4_SendData_s("CAN2 FIFO 0 IRQ:\n\r");
          CAN_Receive(CAN2, CAN_FIFO0, &RxMessage);


          UART4_SendData_s("message StdID=");
          temp=(uint8_t) ((RxMessage.StdId & 0xFF000000)>>24);
          sprintf (buffer, "%X",temp );
          UART4_SendData_s(buffer);
          temp=(uint8_t) ((RxMessage.StdId & 0x00FF0000)>>16);     
          sprintf (buffer, "%X",temp );
          UART4_SendData_s(buffer);
          temp=(uint8_t) ((RxMessage.StdId & 0x0000FF00)>>8);     
          sprintf (buffer, "%X",temp );
          UART4_SendData_s(buffer);
          temp=(uint8_t) ((RxMessage.StdId & 0x000000FF));     
          sprintf (buffer, "%X",temp );
          UART4_SendData_s(buffer);
          UART4_SendData_s("\n\r");
     
          UART4_SendData_s("message ExtId=");
          temp=(uint8_t) ((RxMessage.ExtId & 0xFF000000)>>24);
          sprintf (buffer, "%X",temp );
          UART4_SendData_s(buffer);
          temp=(uint8_t) ((RxMessage.ExtId & 0x00FF0000)>>16);     
          sprintf (buffer, "%X",temp );
          UART4_SendData_s(buffer);
          temp=(uint8_t) ((RxMessage.ExtId & 0x0000FF00)>>8);     
          sprintf (buffer, "%X",temp );
          UART4_SendData_s(buffer);
          temp=(uint8_t) ((RxMessage.ExtId & 0x000000FF));     
          sprintf (buffer, "%X",temp );
          UART4_SendData_s(buffer);
          UART4_SendData_s("\n\r");
          


          UART4_SendData_s("CAN2 RTR=");
          temp=(uint8_t) ((RxMessage.RTR & 0xFF));     
          sprintf (buffer, "%X",temp );
          UART4_SendData_s(buffer);
          UART4_SendData_s("\n\r");
          UART4_SendData_s("CAN2 IDE=");
          temp=(uint8_t) ((RxMessage.IDE & 0xFF));     
          sprintf (buffer, "%X",temp );
          UART4_SendData_s(buffer);
          UART4_SendData_s("\n\r");
          UART4_SendData_s("CAN2 DLC=");
          temp=(uint8_t) ((RxMessage.DLC & 0xFF));     
          sprintf (buffer, "%X",temp );
          UART4_SendData_s(buffer);
          UART4_SendData_s("\n\r");
          UART4_SendData_s("CAN2 FMI=");
          temp=(uint8_t) ((RxMessage.FMI & 0xFF));     
          sprintf (buffer, "%X",temp );
          UART4_SendData_s(buffer);
          UART4_SendData_s("\n\r");
          
          UART4_SendData_s("CAN2 message=");
          for (k=RxMessage.DLC;k!=0 ;k--)
          {
               sprintf (buffer, "%X",((RxMessage.Data[k-1]& 0xF0)>>4) );
               UART4_SendData_s(buffer);
               sprintf (buffer, "%X",(RxMessage.Data[k-1]& 0x0F) );
               UART4_SendData_s(buffer);
          }
          
          Delay(0xfffff);
          Delay(0xfffff);
          Delay(0xfffff);
          Delay(0xfffff);
          Delay(0xfffff);
          Delay(0xfffff);
          Delay(0xfffff);
          Delay(0xfffff);
          Delay(0xfffff);
          Delay(0xfffff);
          Delay(0xfffff);
          Delay(0xfffff);


          
          UART4_SendData_s("\n\r");
          UART4_SendData_s("\n\r");
          UART4_SendData_s("\n\r");
     }
     
}

Outcomes