AnsweredAssumed Answered

stm32 Can Bus receive Error

Question asked by ASSAAD on Feb 24, 2015
Latest reply on Nov 30, 2017 by shilpa Reddy
Hi , i'm using stm32 Can-Bus  on a project. In transmit section ( i controlled on debugger) it is sending message which i wrote .But in recieve section , it is reciving wrong messages. i will post my codes if anyone helps i'll be thankful.
//TRANSMIT CODE
#include "stm32f4xx.h"
#include "usart.h"
#include "config.h"
#include <stdlib.h>
 
 
#include "GPIO_CON.h"
#include "stm32f4xx_dac.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_can.h"
#include "defines.h"
#include "dac.h"
#include "adc.h"
 
 
#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
#define USER_KEY_Port                   GPIOC
#define USER_KEY_Pin                    GPIO_Pin_7
#define USER_KEY_RCC_AHBPeriph          RCC_AHB1Periph_GPIOC
 
    RCC_ClocksTypeDef     RCC_Clocks;
    CAN_InitTypeDef        CAN_InitStructure;
  CAN_FilterInitTypeDef  CAN_FilterInitStructure;
   CanTxMsg TxMessage;
   CanRxMsg  RxMessage_sami;
    //uint16_t CAN1_ID;
    uint8_t CAN1_DATA0,CAN1_DATA1,CAN1_DATA2,CAN1_DATA3,CAN1_DATA4,CAN1_DATA5,CAN1_DATA6,CAN1_DATA7;
    uint8_t mail;
__IO uint8_t Can1Flag,Can2Flag;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
 
 
unsigned int berat,bit,AD1;
 
 
  
  void INTTIM_Config(void);
  void NVIC_Config(void);
  void CAN1_Config(void);
  void Can1WriteData(uint16_t ID);
    void Key_Config(void);
 
 
int main(void)
{
    unsigned int i;
    //---------
 /* Initialize system */
              SystemInit();
           GPIO_Configuration();
              RCC_GetClocksFreq(&RCC_Clocks);
             CAN1_Config();
 
                                                GPIO_ResetBits(GPIOD,GPIO_Pin_10);
                                             GPIO_ResetBits(GPIOD,GPIO_Pin_11);
                                              GPIO_SetBits(GPIOD,GPIO_Pin_11);                                         
     CAN_FIFORelease( CAN1,CAN_FIFO0 );
 
 
while (1)
  {
         
    if( CAN_MessagePending( CAN1,CAN_FIFO0))
    {
    CAN_Receive(CAN1, CAN_FIFO0,&RxMessage_sami);
     GPIO_ToggleBits(GPIOD,GPIO_Pin_10);
         
        if( RxMessage_sami.Data[0] == 0x02 )
      {
    GPIO_ToggleBits(GPIOD,GPIO_Pin_11);                                         }
     
    }
}
}
 
void CAN1_Config(void)
{
  GPIO_InitTypeDef  GPIO_InitStructure;
   
  /* CAN GPIOs configuration **************************************************/
 
  /* Enable GPIO clock */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
 
  /* Connect CAN pins */
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_CAN1);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_CAN1);
   
  /* 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);
 
  /* CAN configuration ********************************************************/ 
  /* Enable CAN clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
   
  /* 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_Normal;
  CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
     
//  /* CAN Baudrate = 1MBps (CAN clocked at 48 MHz) */
//  CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
//  CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
//  CAN_InitStructure.CAN_Prescaler = 2;
    CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; // 1+6+7 = 14, 1+14+6 = 21, 1+15+5 = 21
  CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
  CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
  CAN_InitStructure.CAN_Prescaler =4; //RCC_Clocks.PCLK1_Frequency / (14 * 1000000); // quanta by baudrate
  
     
     
  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_FilterInitStructure.CAN_FilterNumber = 0; // CAN1 [ 0..13]
  
  CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask; // IdMask or IdList
  CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit; // 16 or 32
  
  CAN_FilterInitStructure.CAN_FilterIdHigh      = 0x0000; // Everything, otherwise 11-bit in top bits
  CAN_FilterInitStructure.CAN_FilterIdLow       = 0x0000;
  CAN_FilterInitStructure.CAN_FilterMaskIdHigh  = 0x0000;
  CAN_FilterInitStructure.CAN_FilterMaskIdLow   = 0x0000;
  
  CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_FIFO0; // Rx
  CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
  CAN_FilterInit(&CAN_FilterInitStructure);
   
  /* Transmit Structure preparation */
  TxMessage.StdId = 0x123;
  TxMessage.ExtId = 0x01;
  TxMessage.RTR = CAN_RTR_DATA;
  TxMessage.IDE = CAN_ID_STD;
  TxMessage.DLC = 8;
}
 
void Can1WriteData(uint16_t ID)
{
  CanTxMsg TxMessage;
unsigned int i;
  CAN1_DATA0=rand()%0xff;  CAN1_DATA1=rand()%0xff; 
  CAN1_DATA2=rand()%0xff;  CAN1_DATA3=rand()%0xff; 
  CAN1_DATA4=rand()%0xff;  CAN1_DATA5=rand()%0xff;
  CAN1_DATA6=rand()%0xff;  CAN1_DATA7=rand()%0xff;
 
  /* transmit */
  TxMessage.StdId = ID;
TxMessage.ExtId = 0x00;
//  TxMessage.RTR = CAN_RTR_DATA;
//  TxMessage.IDE = CAN_ID_STD;
  TxMessage.DLC = 8;
  TxMessage.Data[0] = 0x02;   
  TxMessage.Data[1] = CAN1_DATA1;   
  TxMessage.Data[2] = CAN1_DATA2;   
  TxMessage.Data[3] = CAN1_DATA3;   
  TxMessage.Data[4] = CAN1_DATA4;   
  TxMessage.Data[5] = CAN1_DATA5;    
  TxMessage.Data[6] = CAN1_DATA6;   
  TxMessage.Data[7] = CAN1_DATA7;     
  mail=CAN_Transmit(CAN1,&TxMessage);
    // for(i=0;i<0x00ffff;i++);
          while(( CAN_TransmitStatus( CAN1, mail ))!=CAN_TxStatus_Ok);
    }
/////timer
void NVIC_Config(void)
{
  NVIC_InitTypeDef  NVIC_InitStructure;
 
  NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
 
//  NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX0_IRQn;
//  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
//  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
//  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//  NVIC_Init(&NVIC_InitStructure);
}
void INTTIM_Config(void)
{
  /* TIM2 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 41999;  // 1 MHz down to 10 KHz (0.1 ms)
  TIM_TimeBaseStructure.TIM_Prescaler =1; // 24 MHz Clock down to 1 MHz (adjust per your clock)
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1  ;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
  /* TIM2 enable counter */
  TIM_Cmd(TIM2, ENABLE);
}
////timer
 
void Key_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_AHB1PeriphClockCmd(USER_KEY_RCC_AHBPeriph, ENABLE);
     
    /* Configure Input pushpull mode */
    GPIO_InitStructure.GPIO_Pin = USER_KEY_Pin;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(USER_KEY_Port, &GPIO_InitStructure); 
}
    






//RECEIVE CODE
#include "stm32f4xx.h"
#include "usart.h"
#include "config.h"
#include <stdlib.h>
 
 
#include "GPIO_CON.h"
#include "stm32f4xx_dac.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_can.h"
#include "defines.h"
#include "dac.h"
#include "adc.h"
 
#ifdef __GNUC__
  /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
     set to 'Yes') calls __io_putchar() */
  #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
  #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
#define USER_KEY_Port                   GPIOC
#define USER_KEY_Pin                    GPIO_Pin_7
#define USER_KEY_RCC_AHBPeriph          RCC_AHB1Periph_GPIOC
 
    RCC_ClocksTypeDef     RCC_Clocks;
    CAN_InitTypeDef        CAN_InitStructure;
  CAN_FilterInitTypeDef  CAN_FilterInitStructure;
   CanTxMsg TxMessage;
   CanRxMsg  RxMessage_sami;
    //uint16_t CAN1_ID;
    uint8_t CAN1_DATA0,CAN1_DATA1,CAN1_DATA2,CAN1_DATA3,CAN1_DATA4,CAN1_DATA5,CAN1_DATA6,CAN1_DATA7;
    uint8_t mail;
__IO uint8_t Can1Flag,Can2Flag;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
 
 
unsigned int berat,bit,AD1;
 
 
  
  void INTTIM_Config(void);
  void NVIC_Config(void);
  void CAN1_Config(void);
  void Can1WriteData(uint16_t ID);
    void Key_Config(void);
 
 
int main(void)
{
     
    unsigned int i;
      SystemInit();
           GPIO_Configuration();
      RCC_GetClocksFreq(&RCC_Clocks);
    CAN1_Config();
  CAN_FIFORelease( CAN1,CAN_FIFO0 );
 
 
   while (1)
  {
 
 
 
 
          GPIO_ToggleBits(GPIOD,GPIO_Pin_2);
         
          
          Can1WriteData(0x123);
       for(i=0;i<0xffffff;i++);
                for(i=0;i<0xffffff;i++);
         
         
        CAN_Receive( CAN1, CAN_FIFO0, &RxMessage );
        if( RxMessage.Data[0] == 214 )
                                                         {
                                           GPIO_SetBits(GPIOC,GPIO_Pin_2 );
                                                        }
    
}
}
void CAN1_Config(void)
{
  GPIO_InitTypeDef  GPIO_InitStructure;
   
  /* CAN GPIOs configuration **************************************************/
 
  /* Enable GPIO clock */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
 
  /* Connect CAN pins */
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_CAN1);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_CAN1);
   
  /* 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);
 
  /* CAN configuration ********************************************************/ 
  /* Enable CAN clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
   
  /* 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_Normal;
  CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
 
 
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; // 1+6+7 = 14, 1+14+6 = 21, 1+15+5 = 21
  CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
  CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
  CAN_InitStructure.CAN_Prescaler =4;
 
  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_FilterInitStructure.CAN_FilterNumber = 0; // CAN1 [ 0..13]
  
  CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask; // IdMask or IdList
  CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit; // 16 or 32
  
  CAN_FilterInitStructure.CAN_FilterIdHigh      = 0x0000; // Everything, otherwise 11-bit in top bits
  CAN_FilterInitStructure.CAN_FilterIdLow       = 0x0000;
  CAN_FilterInitStructure.CAN_FilterMaskIdHigh  = 0x0000;
  CAN_FilterInitStructure.CAN_FilterMaskIdLow   = 0x0000;
  
  CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_FIFO0; // Rx
  CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
  CAN_FilterInit(&CAN_FilterInitStructure);
   
  /* Transmit Structure preparation */
  TxMessage.StdId = 0x123;
  TxMessage.ExtId = 0x01;
  TxMessage.RTR = CAN_RTR_DATA;
  TxMessage.IDE = CAN_ID_STD;
  TxMessage.DLC = 8;
   
//  RxMessage_sami.IDE=CAN_ID_STD;
//  RxMessage_sami.ExtId=0x00;
//  RxMessage_sami.StdId=0x123;
//  RxMessage_sami.FMI=0x00;
  /* Enable FIFO 0 messa=0xge pending Interrupt */
 //* CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
}
 
void Can1WriteData(uint16_t ID)
{
  CanTxMsg TxMessage;
unsigned int i;
  CAN1_DATA0=rand()%0xff;  CAN1_DATA1=rand()%0xff; 
  CAN1_DATA2=rand()%0xff;  CAN1_DATA3=rand()%0xff; 
  CAN1_DATA4=rand()%0xff;  CAN1_DATA5=rand()%0xff;
  CAN1_DATA6=rand()%0xff;  CAN1_DATA7=rand()%0xff;
 
  /* transmit */
  TxMessage.StdId = ID;
TxMessage.ExtId = 0x00;
//  TxMessage.RTR = CAN_RTR_DATA;
//  TxMessage.IDE = CAN_ID_STD;
  TxMessage.DLC = 8;
  TxMessage.Data[0] = 0x02;   
  TxMessage.Data[1] = CAN1_DATA1;   
  TxMessage.Data[2] = CAN1_DATA2;   
  TxMessage.Data[3] = CAN1_DATA3;   
  TxMessage.Data[4] = CAN1_DATA4;   
  TxMessage.Data[5] = CAN1_DATA5;    
  TxMessage.Data[6] = CAN1_DATA6;   
  TxMessage.Data[7] = CAN1_DATA7;     
  mail=CAN_Transmit(CAN1,&TxMessage);
    // for(i=0;i<0x00ffff;i++);
          while(( CAN_TransmitStatus( CAN1, mail ))!=CAN_TxStatus_Ok);
     
     
     
     
     
}
/////timer
void NVIC_Config(void)
{
  NVIC_InitTypeDef  NVIC_InitStructure;
 
  NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
 
//  NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX0_IRQn;
//  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
//  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
//  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//  NVIC_Init(&NVIC_InitStructure);
}
void INTTIM_Config(void)
{
  /* TIM2 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 41999;  // 1 MHz down to 10 KHz (0.1 ms)
  TIM_TimeBaseStructure.TIM_Prescaler =1; // 24 MHz Clock down to 1 MHz (adjust per your clock)
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1  ;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
  /* TIM2 enable counter */
  TIM_Cmd(TIM2, ENABLE);
}
////timer
 
void Key_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_AHB1PeriphClockCmd(USER_KEY_RCC_AHBPeriph, ENABLE);
     
    /* Configure Input pushpull mode */
    GPIO_InitStructure.GPIO_Pin = USER_KEY_Pin;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
    GPIO_Init(USER_KEY_Port, &GPIO_InitStructure); 
}
 
 
 
/**
  * @brief  Configures the CAN.
  * @param  None
  * @retval None
  */
 
 
#ifdef  USE_FULL_ASSERT
 
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
  
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  
#endif
 

Outcomes