AnsweredAssumed Answered

Data is not getting transmitted via CAN.

Question asked by kasar.sunil on Aug 22, 2016
Latest reply on Aug 23, 2016 by FTITI.Walid
Hi all,
          I am using STM32F429ZI board. I have configured CAN1. But unable to see data in CANalyzer. Can someone tell me what is the problem in d code????

Here is the code:
***************************************************
#include "stm32f4xx.h"
#include "stm32f4xx_it.h"
#include "cansched.h"
#include "stm32f4xx_tim.h"
#include "j1939_c.h"


#define CAN1_PORTA 0x01
#define CAN1_PORTB 0x02
#define CAN1_PORTD 0x03


#define CAN_BAUD_125  0x01
#define CAN_BAUD_250  0x02
#define CAN_BAUD_500  0x03
#define CAN_BAUD_1000 0x04


UINT32 SysClk;
UINT32 HClk;
UINT32 PClk1;
UINT32 PClk2;


UINT32 multiplier;
UINT32 CanSelPort;
UINT32 CanSetBaud;
UINT8 DM1_List[12];
static UINT32 counter10ms_u32 = 0;


void DelayMs(UINT32 millis) 
{
     char i = 0;
     UINT32 tmpmillis = millis;
     
     for (i=0; i<2; i++)
     {
          millis = tmpmillis;
          millis = 665 * millis * multiplier + 50 ;   // caution : DO not alter these values.
          while (millis--);
     }
}








int main(void)
{
  GPIO_InitTypeDef       GPIO_InitStructure;
  RCC_ClocksTypeDef      RCC_Clocks;
  CAN_InitTypeDef        CAN_InitStructure;
  CAN_FilterInitTypeDef  CAN_FilterInitStructure;
  NVIC_InitTypeDef       NVIC_InitStructure;
  CanTxMsg TxMessage;
 
  RCC_GetClocksFreq(&RCC_Clocks);
     SysClk = RCC_Clocks.SYSCLK_Frequency;
  HClk   = RCC_Clocks.HCLK_Frequency;
  PClk1  = RCC_Clocks.PCLK1_Frequency;
  PClk2  = RCC_Clocks.PCLK2_Frequency;


     multiplier = RCC_Clocks.HCLK_Frequency / 4000000;
     
#if(0)     
     /* To test the delay */
     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
     GPIO_Init(GPIOA, &GPIO_InitStructure);
     
     while(1)
     {
          
          GPIO_WriteBit(GPIOA, GPIO_Pin_11, Bit_SET);
          DelayMs(100);
          GPIO_WriteBit(GPIOA, GPIO_Pin_11, Bit_RESET);
          DelayMs(100);
     }
#endif     
     
     CanSelPort = CAN1_PORTB;
     CanSetBaud = CAN_BAUD_250;
     
     switch(CanSelPort)
     {
          case CAN1_PORTA:
          {
               RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
     
               GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_CAN1);
      GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_CAN1);
               
               //PA11 ----> CAN1_RX to MPC2562 RX (Pin No.4)
               //PA12 ----> CAN1_TX to MPC2562 TX (Pin No.1)
               GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12;
               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;   // caution : DO not alter these values. If you alter to  GPIO_PuPd_NOPULL then 1MBaud will not work  
               GPIO_Init(GPIOA, &GPIO_InitStructure);
               
               break;
          }
          case CAN1_PORTB:
          {
                    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
                    GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_CAN1);
                    GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_CAN1);
                    
                    //PB8 ----> CAN1_RX to MPC2562 RX (Pin No.4)
                    //PB9 ----> CAN1_TX to MPC2562 TX (Pin No.1)
                    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
                    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;   // caution : DO not alter these values. If you alter to  GPIO_PuPd_NOPULL then 1MBaud will not work 
                    GPIO_Init(GPIOB, &GPIO_InitStructure);
                    break;
          }
          case CAN1_PORTD:
          {
               RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
     
               GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_CAN1);
               GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_CAN1);
               
               //PD0 ----> CAN1_RX to MPC2562 RX (Pin No.4)
               //PD1 ----> CAN1_TX to MPC2562 TX (Pin No.1)
               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;   // caution : DO not alter these values. If you alter to  GPIO_PuPd_NOPULL then 1MBaud will not work 
               GPIO_Init(GPIOD, &GPIO_InitStructure);
               
               break;
          }
     }
     
  /* CAN configuration */
  /* Enable CAN clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);
  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);
     
  /* 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;
 /* caution : DO not alter the can Baud rate settings*/
     switch(CanSetBaud)
     {
          case CAN_BAUD_125:
          {
               /* CAN Baudrate = 1Mbps (CAN clocked at 42 MHz) Prescale = 24 */
               /* quanta 1+6+7 = 14; 14 * 24 = 336; ( 42000000 / 336 ) = 125000 */
               CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
               CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
               CAN_InitStructure.CAN_BS2 = CAN_BS2_7tq;
               CAN_InitStructure.CAN_Prescaler = 24; //RCC_Clocks.PCLK1_Frequency / (14 * 125000); // quanta by baudrate
               break;
          }
          case CAN_BAUD_250:
          {
               /* CAN Baudrate = 1Mbps (CAN clocked at 42 MHz) Prescale = 12 */
               /* quanta 1+6+7 = 14; 14 * 12 = 168; ( 42000000 / 168 ) = 250000 */
               CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
               CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
               CAN_InitStructure.CAN_BS2 = CAN_BS2_7tq;
               CAN_InitStructure.CAN_Prescaler = 12; //RCC_Clocks.PCLK1_Frequency / (14 * 250000); // quanta by baudrate
               break;
          }
          case CAN_BAUD_500:
          {
                    /* CAN Baudrate = 1Mbps (CAN clocked at 42 MHz) Prescale = 6 */
                    /* quanta 1+6+7 = 14; 14 * 6 = 84; ( 42000000 / 84 ) = 500000 */
                    CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
                    CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
                    CAN_InitStructure.CAN_BS2 = CAN_BS2_7tq;
                    CAN_InitStructure.CAN_Prescaler = 6; //RCC_Clocks.PCLK1_Frequency / (14 * 500000); // quanta by baudrate
                    break;
          }
          case CAN_BAUD_1000:
          {
               /* CAN Baudrate = 1Mbps (CAN clocked at 42 MHz) Prescale = 3 */
               /* quanta 1+14+6 = 21; 21 * 2 = 42; ( 42000000 / 42 ) = 1000000 */
               CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
               CAN_InitStructure.CAN_BS1 = CAN_BS1_14tq;
               CAN_InitStructure.CAN_BS2 = CAN_BS2_6tq;
               CAN_InitStructure.CAN_Prescaler = 2;     //RCC_Clocks.PCLK1_Frequency / (14 * 1000000); // quanta by baudrate
               break;
          }
     }
     
  CAN_Init(CAN1, &CAN_InitStructure);
     
  /* CAN filter init */
  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);
 
  /* Enable FIFO 0 message pending Interrupt */
  CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
          
  // transmit */
     DM1_List[0] = 0x01;
     DM1_List[1] = 0x02;     
     DM1_List[2] = 0x03;
     DM1_List[3] = 0x04;
     DM1_List[4] = 0x05;
     DM1_List[5] = 0x06;
     DM1_List[6] = 0x07;
     DM1_List[7] = 0x08;
     DM1_List[8] = 0x09;
     DM1_List[9] = 0x10;
     DM1_List[10] = 0x11;
     DM1_List[11] = 0x12;
     
  TxMessage.StdId = 0x7A1;
  TxMessage.ExtId = 0x00;
  TxMessage.RTR = CAN_RTR_DATA;
  TxMessage.IDE = CAN_ID_STD;
  TxMessage.DLC = 8;
  
     /*Init J1939 tp*/ 
     ///j1939_tpinit();
  while(1)
  {
    volatile UINT32 i;
    static int j = 0;
    UINT8 TransmitMailbox = 0;     
          
          TxMessage.Data[0] = 0xCB;
          TxMessage.Data[1] = 0xFE;
          TxMessage.Data[2] = 0x03;
          TxMessage.Data[3] = 0x04;
          
    TxMessage.Data[7] = (j >>  0) & 0xFF;
    TxMessage.Data[6] = (j >>  8) & 0xFF;
    TxMessage.Data[5] = (j >> 16) & 0xFF;
    TxMessage.Data[4] = (j >> 24) & 0xFF;
    j++;
    TransmitMailbox = CAN_Transmit(CAN1, &TxMessage);
    i = 0;
          
    while((CAN_TransmitStatus(CAN1, TransmitMailbox) != CANTXOK) && (i != 0xFFFFFF)) // Wait on Transmit
    {
      i++;
    }
}

Outcomes