使用官方的初始化,以及发送判断函数,50ms发送一次,有什么原因会导致可以正常发送的但是突然无法在发送了,重新上电有正常了
下面是相关代码:
/**
* @brief This function :
- Enables GPIO clock
- Configures the CAN pins on GPIO PA11 PA12
* @param None
* @retval None
*/
__INLINE void Configure_GPIO_CAN(void)
{
/* Enable the peripheral clock of GPIOA */
RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
/* GPIO configuration for CAN signals */
/* (1) Select AF mode (10) on PA11 and PA12 */
/* (2) AF4 for CAN signals */
GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER11 | GPIO_MODER_MODER12))\
| (GPIO_MODER_MODER11_1 | GPIO_MODER_MODER12_1); /* (1) */
GPIOA->AFR[1] = (GPIOA->AFR[1] &~ (GPIO_AFRH_AFR11 | GPIO_AFRH_AFR12))\
| (4 << (3 * 4)) | (4 << (4 * 4)); /* (2) */
}
/**
* @brief This function configures CAN.
* @param None
* @retval None
*/
__INLINE void Configure_CAN(void)
{
/* Enable the peripheral clock CAN */
RCC->APB1ENR |= RCC_APB1ENR_CANEN;
uint32_t wait_ack = 0x00000000;
/* Configure CAN */
/* (1) Enter CAN init mode to write the configuration */
/* (2) Wait the init mode entering */
/* (3) Exit sleep mode */
/* (4) Loopback mode, set timing to 1Mb/s: BS1 = 4, BS2 = 3, prescaler = 6 */
/* (5) Leave init mode */
/* (6) Wait the init mode leaving */
/* (7) Enter filter init mode, (16-bit + mask, filter 0 for FIFO 0) */
/* (8) Acivate filter 0 */
/* (9) Identifier list mode */
/* (11) Set the Id list */
/* (12) Set the Id + mask (all bits of standard id will care) */
/* (13) Leave filter init */
/* (14) Set FIFO0 message pending IT enable */
CAN->MCR |= CAN_MCR_INRQ; /* (1) */
while((CAN->MSR & CAN_MSR_INAK)!=CAN_MSR_INAK) /* (2) */
{
wait_ack++;
/* add time out here for a robust application */
}
wait_ack=0;
CAN->MCR &=~ CAN_MCR_SLEEP; /* (3) */
//CAN->BTR |= CAN_BTR_LBKM | 2 << 20 | 3 << 16 | 5 << 0; /* (4) */
//CAN->BTR |= 2 << 20 | 3 << 16 | 5 << 0; /* (4) 左移20位表示BS2,左移16位表示BS1,低十位表示预分频,位25,24表示同步,然后加1表示实际同步*/
//CAN->BTR |= 2 << 20 | 3 << 16 | 47 << 0; /*125000波特率*/
CAN->BTR |= 2 << 20 | 3 << 16 | 11 << 0; /*500000波特率,48/(1+3+4)/(11+1)*/
CAN->MCR &=~ CAN_MCR_INRQ; /* (5) */
while((CAN->MSR & CAN_MSR_INAK)==CAN_MSR_INAK) /* (6) */
{
wait_ack++;
/* add time out here for a robust application */
}
wait_ack=0;
CAN->FMR = CAN_FMR_FINIT; /* (7) 进入过滤器初始化模式*/
CAN->FA1R = CAN_FA1R_FACT0; /* (8)激活过滤器0 */
#if (FILTER_LIST)
CAN->FM1R = CAN_FM1R_FBM0; /* (9) */
CAN->sFilterRegister[0].FR1 = CAN_ID2 << 5 | CAN_ID1 << (16+5); /* (10) */
#else
//CAN->sFilterRegister[0].FR1 = CAN_ID1 << 5 | CAN_ID_MASK << 16; /* (11)关注低16位, */
CAN->sFilterRegister[0].FR1 = 0;
#endif /* FILTER_LIST */
CAN->FMR &=~ CAN_FMR_FINIT; /* (12)激活过滤器0 */
CAN->IER |= CAN_IER_FMPIE0; /* (13) 接收中断*/
/* Configure IT */
/* (14) Set priority for CAN_IRQn */
/* (15) Enable CAN_IRQn */
NVIC_SetPriority(CEC_CAN_IRQn, 0); /* (16) */
NVIC_EnableIRQ(CEC_CAN_IRQn); /* (17) */
}
void CAN_send_0(void)
{
if((CAN->TSR & CAN_TSR_TME0) == CAN_TSR_TME0)/* check mailbox 0 is empty */
{
CAN->sTxMailBox[0].TDTR = 8; /* fill data length = 1 */
CAN->sTxMailBox[0].TDLR = value_0; /* fill 8-bit data */
CAN->sTxMailBox[0].TDHR = 0; /* fill 8-bit data */
CAN->sTxMailBox[0].TIR = (uint32_t)(CAN_ID1 << 21 | CAN_TI0R_TXRQ); /* fill Id field and request a transmission */
}
else
{
CAN->TSR |= CAN_TSR_ABRQ0; /* abort transmission if not empty */
}
}
can发送代码,
void CAN_send_1(void)
{
if((CAN->TSR & CAN_TSR_TME1) == CAN_TSR_TME1)/* check mailbox 1 is empty */
{
CAN->sTxMailBox[1].TDTR = 8; /* fill data length = 1 */
CAN->sTxMailBox[1].TDLR = value_1; /* fill 8-bit data */
CAN->sTxMailBox[1].TDHR = 0; /* fill 8-bit data */
CAN->sTxMailBox[1].TIR = (uint32_t)(CAN_ID2 << 21 | CAN_TI0R_TXRQ); /* fill Id field and request a transmission */
}
else
{
CAN->TSR |= CAN_TSR_ABRQ1; /* abort transmission if not empty */
}
}
void CAN_send_2(void)
{
if((CAN->TSR & CAN_TSR_TME2) == CAN_TSR_TME2)/* check mailbox 1 is empty */
{
CAN->sTxMailBox[2].TDTR = 8; /* fill data length = 1 */
CAN->sTxMailBox[2].TDLR = 0xffffffff; /* fill 8-bit data */
CAN->sTxMailBox[2].TDHR = value_2; /* fill 8-bit data */
CAN->sTxMailBox[2].TIR = (uint32_t)(CAN_ID3 << 21 | CAN_TI0R_TXRQ); /* fill Id field and request a transmission */
}
else
{
CAN->TSR |= CAN_TSR_ABRQ2; /* abort transmission if not empty */
}
}
有没有人了解原因的?
|