AnsweredAssumed Answered

CAN1 transmitting but not receiving

Question asked by lysaght.richard on Dec 8, 2012
Latest reply on Dec 8, 2012 by Clive One
I've been working on getting CAN1 up and running.  I'm transmitting data out and capturing it with PCANView.  But, the data I am transmitting from PCANView is not firing my CAN receive interrupt.  I've put a scope on the CAN chip receive pin and also on PA11 on my STM32F107 to confirm that the waveform was indeed there.  The data is making to the processor pin.

Here's my code:

void initialize_CAN()
{
            /**J2284** CAN Baudrate = 500kbps (CAN clocked at 36 MHz APB1 Bus)
            36Mhz / Baudrate = 36,000,000/500,000 = 72 clock cycles
            72 clk = CAN_Prescaler * (1 + CAN_BS1 + CAN_BS2)
            72 / 4 = 18
            18 = (1 + CAN_BS1 + CAN_BS2)
            18 = 1 + 14 + 3
            */

            /**J1939** CAN Baudrate = 250kbps (CAN clocked at 42 MHz APB1 Bus)
             36Mhz / Baudrate = 36,000,000/250,000 = 144 clock cycles
             144 clk = CAN_Prescaler * (1 + CAN_BS1 + CAN_BS2)
             144 / 8 = 18
             18 = (1 + CAN_BS1 + CAN_BS2)
             18 = 1 + 14 + 3
            */

            CAN_InitTypeDef         CAN_InitStructure;
            CAN_FilterInitTypeDef   CAN_FilterInitStructure;
            GPIO_InitTypeDef          GPIO_InitStructure;

           /* CAN register init */

           /* CANx Periph clock enable */

            /* GPIO clock enable */
            //RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
            //RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

            RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);
            RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);

            /* Configure CAN pin: RX */
            GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
            GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
            GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
            GPIO_Init(GPIOA, &GPIO_InitStructure);

            /* Configure CAN pin: TX */
            GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
            GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
            GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
            GPIO_Init(GPIOA, &GPIO_InitStructure);

            RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
            //GPIO_PinRemapConfig(GPIO_Remap_CAN2, ENABLE);
            //RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2, ENABLE);
            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;

            /* Baudrate = 250 Kbps */
            CAN_InitStructure.CAN_BS1=CAN_BS1_14tq;
            CAN_InitStructure.CAN_BS2=CAN_BS2_3tq;
            CAN_InitStructure.CAN_Prescaler=4;
            CAN_Init(CAN1, &CAN_InitStructure);

            CAN_FilterInitStructure.CAN_FilterNumber=15;

            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_FIFO0;
            CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
            CAN_FilterInit(&CAN_FilterInitStructure);

            /* CAN FIFO0 message pending interrupt enable */
            CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
}

void NVIC_Config(void)
{
   NVIC_InitTypeDef  NVIC_InitStructure;
   /* Configure the NVIC Preemption Priority Bits */
   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
   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);
}

void CAN1_RX0_IRQHandler(void)
{
    int X = 0;
    CanRxMsg RxMessage;

  RxMessage.StdId = 0x00;
  RxMessage.ExtId = 0x00;
  RxMessage.IDE = 0;
  RxMessage.DLC = 0;
  RxMessage.FMI = 0;

  //Clear data out from previous messages
  for (X=0; X<=7; X++)  {RxMessage.Data[X] = 0x00;}

  CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);

  //Move message to global buffer...
  J1939_message.StdId =  RxMessage.StdId;
  J1939_message.ExtId =  RxMessage.ExtId;
  J1939_message.IDE =  RxMessage.IDE;
  J1939_message.DLC =  RxMessage.DLC;
  J1939_message.FMI =  RxMessage.FMI;
  for (X=0; X<=7; X++)  {J1939_message.data_bytes[X+1] = RxMessage.Data[X] ;}

  //Break the message down into data for the gauges...
  J1939_decipher_message();

}

Any thoughts?

Rich

Outcomes