AnsweredAssumed Answered

CAN Bus communication between CAN1 and CAN2 discoveryf4

Question asked by hadj_taieb.rami on Jan 10, 2014
Latest reply on Jan 16, 2014 by hadj_taieb.rami
Hello everyone,

I am trying  to make a test code to my newly created CAN Bus transceiver board. I am using Discoveryf4 board. I have wrote the program below but it seems like I have made a mistake because the initialization ends successfully but I didn't receive any think in the interruption function. 


CAN_InitTypeDef        CAN_InitStructure;
CAN_FilterInitTypeDef  CAN_FilterInitStructure;
CanTxMsg TxMessage;
uint32_t uwCounter = 0;
uint8_t TransmitMailbox = 0;
extern CanRxMsg RxMessage;
/* Private function prototypes -----------------------------------------------*/
static void NVIC_Config(void);
static void CAN_Config(void);
static void Delay(void);
void Init_RxMes(CanRxMsg *RxMessage);


int main(void)
{
 
 /* Transmit Structure preparation */
  TxMessage.StdId = 0x321;
  TxMessage.ExtId = 0x01;
  TxMessage.RTR = CAN_RTR_DATA;
  TxMessage.IDE = CAN_ID_STD;
  TxMessage.DLC = 1; 
  TxMessage.Data[0]=0XFE;
  
  
  /* CAN configuration */
  CAN_Config();
  /* NVIC configuration */
  NVIC_Config();
 
  /* Infinite loop */
  while(1)
  {      
      uwCounter = 0;
      TransmitMailbox = CAN_Transmit(CANx, &TxMessage);
      while((CAN_TransmitStatus(CANx, TransmitMailbox)  !=  CANTXOK) && (uwCounter  !=  0xFFFF))
        {
          uwCounter++;
        }


     Delay();
  }
}
/*******************************************************************************
*******************************************************************************/
                        /* Configures the CAN */
/*******************************************************************************
*******************************************************************************/


static void CAN_Config(void)
{
  GPIO_InitTypeDef  GPIO_InitStructure;
  
  /* CAN GPIOs configuration **************************************************/


  /* Enable GPIO clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1,ENABLE);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2,ENABLE);
  
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD , ENABLE);
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);


  /* Connect CAN 1 pins to AF9 */
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_CAN1);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_CAN1); 
  
    /* Connect CAN  2 pins to AF9 */
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_CAN2);
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_CAN2); 
  
  /* Configure CAN 1 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);
  
  
  /* Configure CAN 2 RX and TX pins */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6;
  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(GPIOB, &GPIO_InitStructure);


  /* CAN configuration ********************************************************/  
  /* Enable CAN 1 clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
  
   /* Enable CAN 2 clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2, ENABLE);
  
  /* CAN1 register init */
  CAN_DeInit(CANx);
  
    /* CAN2 register init */
  CAN_DeInit(CAN2);


  /* CAN 1 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_Mode_LoopBack;//
  CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
    
  /* CAN 1 Baudrate = 1 MBps (CAN clocked at 30 MHz) */
  CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
  CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
  CAN_InitStructure.CAN_Prescaler = 2;
  CAN_Init(CAN1, &CAN_InitStructure);


  CAN_Init(CAN2, &CAN_InitStructure);
  
  /* CAN1 filter init */
  CAN_FilterInitStructure.CAN_FilterNumber = 14;  // 0..13 for CAN1, 14..27 for CAN2
  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_FilterInit(&CAN_FilterInitStructure);
  
  /* Enable FIFO 0 message pending Interrupt */
  CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
}




/*******************************************************************************
*******************************************************************************/
                        /* Configures the NVIC for CAN*/
/*******************************************************************************
*******************************************************************************/
static void NVIC_Config(void)
{
  NVIC_InitTypeDef  NVIC_InitStructure;


  NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}


/*******************************************************************************
*******************************************************************************/
                        /*Initializes the Rx Message*/
/*******************************************************************************
*******************************************************************************/
void Init_RxMes(CanRxMsg *RxMessage)
{
  uint8_t ubCounter = 0;


  RxMessage->StdId = 0x00;
  RxMessage->ExtId = 0x00;
  RxMessage->IDE = CAN_ID_STD;
  RxMessage->DLC = 0;
  RxMessage->FMI = 0;
  for (ubCounter = 0; ubCounter < 8; ubCounter++)
  {
    RxMessage->Data[ubCounter] = 0x00;
  }
}






/*******************************************************************************
*******************************************************************************/
                        /* Delay */
/*******************************************************************************
*******************************************************************************/
static void Delay(void)
{
  uint16_t nTime = 0x0000;


  for(nTime = 0; nTime <0xFFF; nTime++)
  {
  }
}
/*******************************************************************************
*******************************************************************************/
                        /*  CAN1 RX0 request handler  */
/*******************************************************************************
*******************************************************************************/
void CAN1_RX0_IRQHandler(void)
{
  CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
}

Any insight would be greatly appreciated!

Outcomes