AnsweredAssumed Answered

STM32F042 CAN problems

Question asked by donchev.petar on Sep 18, 2015
Latest reply on Sep 18, 2015 by donchev.petar
Hi,
I have difficulties to enable CAN module on STM32F042. I did not face problems with CAN module on STM32F1, STM32F2, STM32F4, but now I'm stuck on STM32F0.
1. First, I'm confused from the peripheral library examples. System core clock is configured at 48MHz, AHB and APB have DIV1, but in CAN initialization function is stated that CAN is clocked at 36MHz. Why?
/* CAN Baudrate = 1MBps (CAN clocked at 36 MHz) */
CAN_InitStructure.CAN_BS1 = CAN_BS1_9tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
CAN_InitStructure.CAN_Prescaler = 2;

2. Second, this is my CAN initialization function. Is there something wrong or missing?
(I'm not using interrupts).
CanTxMsg TxMessage = {0};
 
static void CAN_Config(void)
{
  GPIO_InitTypeDef  GPIO_InitStructure;
  NVIC_InitTypeDef  NVIC_InitStructure;
  CAN_InitTypeDef        CAN_InitStructure;
  CAN_FilterInitTypeDef  CAN_FilterInitStructure;
     
  /* CAN GPIOs configuration **************************************************/
 
  /* Enable GPIO clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
 
 
      /* Connect CAN pins to AF7 */
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_4);
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_4);
     
  /* Configure CAN RX and TX pins */
  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;
    //GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOB, &GPIO_InitStructure);
 
 
     
  /* NVIC configuration *******************************************************/
  //NVIC_InitStructure.NVIC_IRQChannel = CEC_CAN_IRQn;
  //NVIC_InitStructure.NVIC_IRQChannelPriority = 0x0;
  //NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  //NVIC_Init(&NVIC_InitStructure);
   
  /* CAN configuration ********************************************************/ 
  /* Enable CAN clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN, ENABLE);
   
  /* CAN register init */
  CAN_DeInit(CAN);
  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 36 MHz) */
  CAN_InitStructure.CAN_BS1 = CAN_BS1_9tq; 
  CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq; 
  CAN_InitStructure.CAN_Prescaler = 2;
   
  /* CAN Baudrate = 1MBps (May be CAN clocked at 48 MHz) */
  //CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;
  //CAN_InitStructure.CAN_BS2 = CAN_BS2_5tq;
  //CAN_InitStructure.CAN_Prescaler = 4;
  CAN_Init(CAN, &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_FilterInit(&CAN_FilterInitStructure);
   
  /* Transmit Structure preparation */
  TxMessage.StdId = 0x321;
  TxMessage.ExtId = 0x01;
  TxMessage.RTR = CAN_RTR_DATA;
  TxMessage.IDE = CAN_ID_STD;
  TxMessage.DLC = 1;
   
  /* Enable FIFO 0 message pending Interrupt */
  //CAN_ITConfig(CANx, CAN_IT_FMP0, ENABLE);
}

3. I need to use USB together with the CAN bus. What consideration must take in the source code for proper END point settings to not mess with last the 256 bytes of USB memory?


Outcomes