How to configure the STM32103 for CAN and remapping the pins to PB8 and PB9

Daniel picture Daniel · Nov 22, 2012 · Viewed 22.8k times · Source

I am trying to, at this stage, simply send a transmission using CAN on the STM32F103 V8T6. The chip is implemented on a board that was customized by my company. Looking at the schematic I see that the CAN Tx and Rx pins were remapped to PB9 (Tx) and PB8 (Rx). All that being said, I have been able to use LoopBack mode successfully (using the latest example from STM "V3.5.0") but have have NOT been able to get Normal mode working. Can someone please let me know if they see an obvious flaw in my initial configuration?! I have included only the code related to configuration and left out the transmission function call.

int main (void)
{

RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);



NVIC_InitTypeDef NVIC_InitStructure;

NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;

NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);



 GPIO_InitTypeDef GPIO_InitStructure;


/* Configure CAN pin: RX */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

GPIO_Init(GPIOB, &GPIO_InitStructure);


/* Configure CAN pin: TX */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_Init(GPIOB, &GPIO_InitStructure);


// Remap2 is for PB8 and PB9

GPIO_PinRemapConfig(GPIO_Remap2_CAN1 , ENABLE);


}

Here is my transmit function

void CAN_TransmitMyMsg(void)
{
  CAN_InitTypeDef        CAN_InitStructure;
  CAN_FilterInitTypeDef  CAN_FilterInitStructure;
  CanTxMsg TxMessage;
  uint32_t i = 0;
  uint8_t TransmitMailbox = 0;
  uint8_t status = 0;

  /* CAN register init */
  CAN_DeInit(CANx);
  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;

  /* Baudrate = 125kbps*/
  CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;
  CAN_InitStructure.CAN_BS1=CAN_BS1_2tq;
  CAN_InitStructure.CAN_BS2=CAN_BS2_3tq;
  CAN_InitStructure.CAN_Prescaler=48;
  CAN_Init(CANx, &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 */
  TxMessage.StdId=0x11;
  TxMessage.RTR=CAN_RTR_DATA;
  TxMessage.IDE=CAN_ID_STD;
  TxMessage.DLC=2;
  TxMessage.Data[0]=0xCA;
  TxMessage.Data[1]=0xFE;

  TransmitMailbox=CAN_Transmit(CANx, &TxMessage);

  //wait until CAN transmission is OK
  i = 0;
  while((status != CANTXOK) && (i != 0xFFFF))               
  {
    status = CAN_TransmitStatus(CANx, TransmitMailbox);
    i++;
  }

}

This resource has been helpful, but ultimately insufficient.

STM32F103 microcontroller CAN messages

Thanks!

Daniel

Answer

Vojta picture Vojta · Dec 11, 2012

I had similar problems. My problem was in bad selection of comm parameters (timequantums and prescaler)

I debug it in this way:

  • I checked, that RSS pin of CAN transceiver (I used MCP 2551 I/P)
  • I switched it to loopback and with osciloscope check output signal on TX channel and on CANH to CANL (if the output is configured properly)
  • I looked to RCC configuration - the prescaler of APB1 clock has been changed to 2 (I excepted 4)