金牌会员
- 积分
- 2394
- 金钱
- 2394
- 注册时间
- 2016-4-14
- 在线时间
- 225 小时
|
10金钱
本帖最后由 hechuanxi0312 于 2016-5-31 21:05 编辑
STM32F072的CAN初始化失败,具体是进入到CAN_init()函数中后最后有个while循环等待硬件置为,返回CAN初始化失败,有没有人知道具体原因,求助
[mw_shl_code=c,true]uint8_t CAN_Init(CAN_TypeDef* CANx, CAN_InitTypeDef* CAN_InitStruct)
{
uint8_t InitStatus = CAN_InitStatus_Failed;
uint32_t wait_ack = 0x00000000;
/* Check the parameters */
assert_param(IS_CAN_ALL_PERIPH(CANx));
assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_TTCM));
assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_ABOM));
assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_AWUM));
assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_NART));
assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_RFLM));
assert_param(IS_FUNCTIONAL_STATE(CAN_InitStruct->CAN_TXFP));
assert_param(IS_CAN_MODE(CAN_InitStruct->CAN_Mode));
assert_param(IS_CAN_SJW(CAN_InitStruct->CAN_SJW));
assert_param(IS_CAN_BS1(CAN_InitStruct->CAN_BS1));
assert_param(IS_CAN_BS2(CAN_InitStruct->CAN_BS2));
assert_param(IS_CAN_PRESCALER(CAN_InitStruct->CAN_Prescaler));
/* Exit from sleep mode */
CANx->MCR &= (~(uint32_t)CAN_MCR_SLEEP);
/* Request initialisation */
CANx->MCR |= CAN_MCR_INRQ ;
/* Wait the acknowledge */
while (((CANx->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) && (wait_ack != INAK_TIMEOUT))
{
wait_ack++;
}
/* Check acknowledge */
if ((CANx->MSR & CAN_MSR_INAK) != CAN_MSR_INAK)
{
InitStatus = CAN_InitStatus_Failed;
}
else
{
/* Set the time triggered communication mode */
if (CAN_InitStruct->CAN_TTCM == ENABLE)
{
CANx->MCR |= CAN_MCR_TTCM;
}
else
{
CANx->MCR &= ~(uint32_t)CAN_MCR_TTCM;
}
/* Set the automatic bus-off management */
if (CAN_InitStruct->CAN_ABOM == ENABLE)
{
CANx->MCR |= CAN_MCR_ABOM;
}
else
{
CANx->MCR &= ~(uint32_t)CAN_MCR_ABOM;
}
/* Set the automatic wake-up mode */
if (CAN_InitStruct->CAN_AWUM == ENABLE)
{
CANx->MCR |= CAN_MCR_AWUM;
}
else
{
CANx->MCR &= ~(uint32_t)CAN_MCR_AWUM;
}
/* Set the no automatic retransmission */
if (CAN_InitStruct->CAN_NART == ENABLE)
{
CANx->MCR |= CAN_MCR_NART;
}
else
{
CANx->MCR &= ~(uint32_t)CAN_MCR_NART;
}
/* Set the receive FIFO locked mode */
if (CAN_InitStruct->CAN_RFLM == ENABLE)
{
CANx->MCR |= CAN_MCR_RFLM;
}
else
{
CANx->MCR &= ~(uint32_t)CAN_MCR_RFLM;
}
/* Set the transmit FIFO priority */
if (CAN_InitStruct->CAN_TXFP == ENABLE)
{
CANx->MCR |= CAN_MCR_TXFP;
}
else
{
CANx->MCR &= ~(uint32_t)CAN_MCR_TXFP;
}
/* Set the bit timing register */
CANx->BTR = (uint32_t)((uint32_t)CAN_InitStruct->CAN_Mode << 30) | \
((uint32_t)CAN_InitStruct->CAN_SJW << 24) | \
((uint32_t)CAN_InitStruct->CAN_BS1 << 16) | \
((uint32_t)CAN_InitStruct->CAN_BS2 << 20) | \
((uint32_t)CAN_InitStruct->CAN_Prescaler - 1);
/* Request leave initialisation */
CANx->MCR &= ~(uint32_t)CAN_MCR_INRQ;
/* Wait the acknowledge */
wait_ack = 0;
while (((CANx->MSR & CAN_MSR_INAK) == (uint16_t)CAN_MSR_INAK) && (wait_ack != INAK_TIMEOUT))//程序就死在这里,一直循环直到跳出,然后进入下面的if判断,can初始化失败
{
wait_ack++;
}
/* ...and check acknowledged */
if ((CANx->MSR & CAN_MSR_INAK) == CAN_MSR_INAK)
{
InitStatus = CAN_InitStatus_Failed;
}
else
{
InitStatus = CAN_InitStatus_Success ;
}
}
/* At this step, return the status of initialization */
return InitStatus;
}[/mw_shl_code]
[mw_shl_code=c,true]u8 CAN_Mode_Init(u8 tsjw,u8 tbs2,u8 tbs1,u16 brp,u8 mode)
{
GPIO_InitTypeDef GPIO_InitStructure;
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
#if CAN_RX_INT_ENABLE //êÇ·ñê1ÄüCAN½óêÕÖD¶Ï
NVIC_InitTypeDef NVIC_InitStructure;
#endif
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB,ENABLE);//ê1ÄüCAN¶Ôó|μÄ1ü½ÅμÄê±Öó
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN,ENABLE);//ê1ÄüCANê±Öó
GPIO_PinAFConfig(GPIOB,GPIO_PinSource8,GPIO_AF_4);//Ñ¡ÔñCAN1ü½Åμĸ′óÃ1|Äü£¬B8->RX
GPIO_PinAFConfig(GPIOB,GPIO_PinSource9,GPIO_AF_4);//Ñ¡ÔôCAN1ü½»μĸ′óÃ1|Äü£¬B9->TX
//
// /*3õê¼»ˉCANμÄ1ü½Å£¬B8->RX*/
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//Ñ¡Ôñ¸′óÃÄ£ê½
// GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
// GPIO_Init(GPIOB,&GPIO_InitStructure);
// /*3õê¼»ˉCANμÄ1ü½Å£¬B9->TX*/
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//Ñ¡Ôñ¸′óÃÄ£ê½
// GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//íÆíì
// GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_Init(GPIOB,&GPIO_InitStructure);
/*3õê¼»ˉCAN1ü½Å*/
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_Init(GPIOB, &GPIO_InitStructure);
/*CANÏà1ØéèÖÃ*/
/* CAN register init */
CAN_DeInit(CAN);
CAN_StructInit(&CAN_InitStructure);
/*CANμ¥ÔaéèÖÃ*/
CAN_InitStructure.CAN_TTCM = DISABLE; /* ê±¼ä′¥·¢½ûÖ1, ê±¼ä′¥·¢£oCANó2¼tμÄÄú2¿¶¨ê±Æ÷±»¼¤»î£¬2¢Çò±»óÃóú2úéúê±¼ä′á */
CAN_InitStructure.CAN_ABOM = DISABLE; /* ×Ô¶ˉàëÏß½ûÖ1£¬×Ô¶ˉàëÏߣoò»μ©ó2¼t¼à¿Øμ½128′Î11¸öòtDÔ룬¾í×Ô¶ˉíË3öàëÏß×′쬡£ÔúÕaàïòaèí¼té趨oó2ÅÄüíË3ö */
CAN_InitStructure.CAN_AWUM = DISABLE; /* ×Ô¶ˉ»½DѽûÖ1£¬óD±¨ÎÄà′μÄê±oò×Ô¶ˉíË3öDYÃß */
CAN_InitStructure.CAN_NART = DISABLE; /* ±¨ÎÄÖØ′«, èç1û′íÎóò»Ö±′«μ½3é1|Ö1£¬·ñÔòÖ»′«ò»′Î */
CAN_InitStructure.CAN_RFLM = DISABLE; /* ½óêÕFIFOËø¶¨, 1--Ëø¶¨oó½óêÕμ½DÂμı¨ÎÄÕa2»òa£¬0--½óêÕμ½DÂμı¨ÎÄÔò¸2¸ÇÇ°ò»±¨ÎÄ */
CAN_InitStructure.CAN_TXFP = ENABLE; /* ·¢ËíóÅÏ輶 0---óé±ê궷û¾ö¶¨ 1---óé·¢ËíÇëÇóË3Dò¾ö¶¨ */
CAN_InitStructure.CAN_Mode = mode;//Ä£ê½éèÖão mode:0,ÆÕí¨Ä£ê½;1,»Ø»·Ä£ê½;
CAN_InitStructure.CAN_SJW = tsjw;//ÖØDÂí¬2½ìøÔ¾¿í¶è(Tsjw)Îatsjw+1¸öê±¼äμ¥λ CAN_SJW_1tq~CAN_SJW_4tq
CAN_InitStructure.CAN_BS1 = tbs1;//Tbs1=tbs1+1¸öê±¼äμ¥λCAN_BS1_1tq ~CAN_BS1_16tq
CAN_InitStructure.CAN_BS2 = tbs2;//Tbs2=tbs2+1¸öê±¼äμ¥λCAN_BS2_1tq ~CAN_BS2_8tq
CAN_InitStructure.CAN_Prescaler = brp;//·ÖÆμÏμêy(Fdiv)Îabrp+1
//CAN_StructInit(&CAN_InitStructure);
CAN_Init(CAN,&CAN_InitStructure);
/*CANÂË2¨éèÖÃ*/
CAN_FilterInitStructure.CAN_FilterNumber=0; /* 1yÂËÆ÷0 */
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask; /* Æá±ÖÄ£ê½ */
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit; /* 32λ */
CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000; /* òÔÏÂËĸö¶¼Îa0, ±íÃ÷2»1yÂËèÎoÎid */
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;//32λID
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;//32λMASK
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0; /* Äü1»í¨1y¸Ã1yÂËÆ÷μı¨ÎÄ′æμ½fifo0ÖD */
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;//¼¤»î1yÂËÆ÷0
CAN_FilterInit(&CAN_FilterInitStructure);
#if CAN_RX_INT_ENABLE//êÇ·ñê1ÄüCAN½óêÕÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannel = CEC_CAN_IRQn;//é趨CANÖD¶ÏμÄí¨μà
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;//CANÖD¶ÏóÅÏ輶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//ê1ÄüÖD¶Ï
NVIC_Init(&NVIC_InitStructure);//3õê¼»ˉ
CAN_ITConfig(CAN,CAN_IT_FMP0, ENABLE);//FIFO0ÏûÏ¢1òoÅÖD¶Ïê1Äü
#endif
return 0;
}[/mw_shl_code]
|
最佳答案
查看完整内容[请看2#楼]
今天折腾了一天,发现之前的原理图上CAN的RX,TX均接了上拉电阻,将上拉电阻去掉后,初始化就能成功了,这个有什么说法么?
附上原理图,收发器用的TAJ1041,不知道这个电路图CAN部分还有没有其他问题?
|