STM32F407使用CAN通信,回环模式行,为什么正常模式不行

2019-07-20 07:27发布

第一次使用stm32f407的can总线进行通讯,发现ST的can总线并不像TI F28335以及atmel sam3的好用,参照标准库的例子做的,只是把STM32F407的PD0 PD1接在CAN收发器上使用CAN1(此收发器完好,F28335能用此完美进行can通信),先做回环模式测试,程序通过,调用CAN_TransmitStatus(CAN1,mbindex)能返回1;但是改成正常模式就不行了,调用CAN_TransmitStatus(CAN1,mbindex)返回的是2----CAN_TxStatus_Pending,即使用while(CAN_TransmitStatus(CAN1,mbindex)!=1),也等不到发送成功,请大家指点:初始化
[mw_shl_code=c,true]CAN_DeInit(this->CAN);

                RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1,ENABLE);
                this->CAN_AF = GPIO_AF_CAN1;
       
        pinMode(PD0,GPIO_Mode_AF,GPIO_Speed_100MHz,GPIO_PuPd_UP,GPIO_OType_PP);
        pinMode(PD1,GPIO_Mode_AF,GPIO_Speed_100MHz,GPIO_PuPd_UP,GPIO_OType_PP);
        GPIO_PinAFConfig(GPIOD,0,GPIO_AF_CAN1);
        GPIO_PinAFConfig(GPIOD,1,GPIO_AF_CAN1);
       
        CAN_InitTypeDef CAN_InitStructure;
       
        CAN_InitStructure.CAN_TTCM = DISABLE;
  CAN_InitStructure.CAN_ABOM = DISABLE;
  CAN_InitStructure.CAN_AWUM = DISABLE;
  CAN_InitStructure.CAN_NART = ENABLE;
  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 = 1 MBps (CAN clocked at 42 MHz) */
        CAN_InitStructure.CAN_BS1 = CAN_BS1_5tq;
        CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq;
        CAN_InitStructure.CAN_Prescaler = 3;
        CAN_Init(CAN1,&CAN_InitStructure);[/mw_shl_code]

发送
[mw_shl_code=c,true]transData(uint32_t target_id,uint8_t dlc,bool rtr,bool ide,uint32_t LDWord,uint32_t HDWord){
        uint8_t transmit_mailbox = 0;
        if ((CAN1->TSR&CAN_TSR_TME0) == CAN_TSR_TME0)
  {
    transmit_mailbox = 0;
  }
  else if ((CAN1->TSR&CAN_TSR_TME1) == CAN_TSR_TME1)
  {
    transmit_mailbox = 1;
  }
  else if ((CAN1->TSR&CAN_TSR_TME2) == CAN_TSR_TME2)
  {
    transmit_mailbox = 2;
  }
  else
  {
    transmit_mailbox = CAN_TxStatus_NoMailBox;
  }
        if (transmit_mailbox != CAN_TxStatus_NoMailBox)
  {
    /* Set up the Id */
CAN1->sTxMailBox[transmit_mailbox].TIR &= TMIDxR_TXRQ;
CAN1->sTxMailBox[transmit_mailbox].TIR |= ((target_id << 3) | (ide << 2) | (rtr << 1));
    /* Set up the DLC */
    dlc &= (uint8_t)0x0000000F;
CAN1->sTxMailBox[transmit_mailbox].TDTR &= (uint32_t)0x00000000;
CAN1->sTxMailBox[transmit_mailbox].TDTR |= dlc;
    /* Set up the data field */
CAN1->sTxMailBox[transmit_mailbox].TDLR = LDWord;
CAN1->sTxMailBox[transmit_mailbox].TDHR = HDWord;
    /* Request transmission */
CAN1->sTxMailBox[transmit_mailbox].TIR |= TMIDxR_TXRQ;
  }
               
  return transmit_mailbox;
}[/mw_shl_code]

[mw_shl_code=c,true]uint16_t mbindex = transData(0x200000,8,0,0,data2,data1);
uint16_t i = 0;
while(CAN_TransmitStatus(CAN1,mbindex)!=1&&i<0xFFFF)i++;
SERIAL_ECHOPAIRLN("trans statue",(long)CAN_TransmitStatus(CAN1,mbindex));[/mw_shl_code]

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。