|
请问STM32F107 RT-Thread 下CAN如何配置才能进接收中断?
目前配置如下:
static void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
//NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
/* Enable the CAN Interrupt */
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);
NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
}
static void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1| RCC_APB1Periph_CAN2, ENABLE);
}
static void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* CAN1 初始化 */
GPIO_InitStructure.GPIO_Pin = CAN1_Tx_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(CAN1_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = CAN1_Rx_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(CAN1_GPIO, &GPIO_InitStructure);
/* 如果定义了映射标记,则重映射 */
#if CAN1_REMAP_FLAG
GPIO_PinRemapConfig(CAN1_REMAP_CONFIG , ENABLE);
#endif
/* CAN2 初始化 */
GPIO_InitStructure.GPIO_Pin = CAN2_Tx_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(CAN2_GPIO, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = CAN2_Rx_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(CAN2_GPIO, &GPIO_InitStructure);
/* 如果定义了映射标记,则重映射 */
#if CAN2_REMAP_FLAG
GPIO_PinRemapConfig(CAN2_REMAP_CONFIG , ENABLE);
#endif
}
void rt_hw_can_init(void)
{
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDef CAN_FilterInitStructure;
GPIO_Configuration();
NVIC_Configuration();
RCC_Configuration();
/* CAN1 cell init */
CAN_DeInit(CAN1);
CAN_StructInit(&CAN_InitStructure);
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 = ENABLE;
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_11tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_6tq;
CAN_InitStructure.CAN_Prescaler = 100; //200: 10K ; 100 - 20K
/* Initializes the CAN1 */
CAN_Init(CAN1, &CAN_InitStructure);
/* IT Configuration for CAN1 */
CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
/* CAN2 cell init */
CAN_DeInit(CAN2);
CAN_StructInit(&CAN_InitStructure);
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 = ENABLE;
CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;
CAN_InitStructure.CAN_SJW = CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1 = CAN_BS1_11tq;
CAN_InitStructure.CAN_BS2 = CAN_BS2_6tq;
CAN_InitStructure.CAN_Prescaler = 100; //200: 10K ; 100 - 20K
/* Initializes the CAN2 */
CAN_Init(CAN2, &CAN_InitStructure);
/* IT Configuration for CAN1 */
CAN_ITConfig(CAN2, CAN_IT_FMP0, ENABLE);
/* 初始化过滤器 */
CAN_FilterInitStructure.CAN_FilterNumber = 14;
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 = CAN_FIFO0;
CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
/* 发送消息清空 */
m_TxMessage.StdId = 0x00;
m_TxMessage.ExtId = 0x00;
m_TxMessage.RTR = CAN_RTR_DATA;
m_TxMessage.IDE = CAN_ID_STD;
m_TxMessage.DLC = 1;//一次发送的数据长度
/* 注_册CAN1设备 */
can1_device.type = RT_Device_Class_CAN;
can1_device.init = RT_NULL;
can1_device.open = RT_NULL;
can1_device.close = RT_NULL;
can1_device.read = rt_hw_can_read;
can1_device.write = rt_hw_can_write;
can1_device.control = rt_hw_can_control;
can1_device.private = RT_NULL;
rt_device_register(&can1_device, "can1", RT_DEVICE_FLAG_RDWR);
/* 注_册CAN2设备 */
can2_device.type = RT_Device_Class_CAN;
can2_device.init = RT_NULL;
can2_device.open = RT_NULL;
can2_device.close = RT_NULL;
can2_device.read = rt_hw_can_read;
can2_device.write = rt_hw_can_write;
can2_device.control = rt_hw_can_control;
can2_device.private = RT_NULL;
rt_device_register(&can2_device, "can2", RT_DEVICE_FLAG_RDWR);
}
现在发送可以,接收一直进不了中断,请求帮助 |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|