2024-01-30 03:31:27 +00:00
|
|
|
|
|
|
|
|
|
/******************** (C) COPYRIGHT 2011 Ƕ<><C7B6>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ********************
|
|
|
|
|
* <EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD>bsp_can.c
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD>
|
|
|
|
|
*
|
|
|
|
|
* ʵ<EFBFBD><EFBFBD>ƽ̨<EFBFBD><EFBFBD>
|
|
|
|
|
* Ӳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD>
|
|
|
|
|
*
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD>
|
|
|
|
|
* ʱ<EFBFBD><EFBFBD> <EFBFBD><EFBFBD>2022-10-12
|
|
|
|
|
* <EFBFBD>汾 <EFBFBD><EFBFBD>V1.0
|
|
|
|
|
**********************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include "can.h"
|
|
|
|
|
#include "stm32f10x_can.h"
|
|
|
|
|
#include "misc.h"
|
|
|
|
|
#include "Sys.h"
|
|
|
|
|
#include "bsp_can.h"
|
|
|
|
|
#include "canbusdrv.h"
|
|
|
|
|
#include "REV_CarMessage.h"
|
|
|
|
|
|
|
|
|
|
#define DEBUG_BSP_CAN 1
|
|
|
|
|
|
|
|
|
|
#if DEBUG_BSP_CAN > 0
|
|
|
|
|
#define CAN_PRINT DEPRINT
|
|
|
|
|
#define CAN_PRINT_ARRAY DEPRINT_ARRAY
|
|
|
|
|
#else
|
|
|
|
|
#define CAN_PRINT(...) ;
|
|
|
|
|
#define CAN_PRINT_ARRAY(...) ;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
u8 CAN_Mode_Init(void)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
|
|
|
CAN_InitTypeDef CAN_InitStructure;
|
|
|
|
|
CAN_FilterInitTypeDef CAN_FilterInitStructure;
|
|
|
|
|
#if CAN_RX0_INT_ENABLE
|
|
|
|
|
NVIC_InitTypeDef NVIC_InitStructure;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);//ʹ<><CAB9>PORTDʱ<44><CAB1>,ʹ<>ܹ<EFBFBD><DCB9>ܸ<EFBFBD><DCB8><EFBFBD>ʱ<EFBFBD><CAB1>
|
|
|
|
|
|
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);//ʹ<><CAB9>CAN1ʱ<31><CAB1>
|
|
|
|
|
|
|
|
|
|
CAN_OperatingModeRequest(CAN1, CAN_OperatingMode_Initialization);
|
|
|
|
|
|
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
|
|
|
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure); //<2F><>ʼ<EFBFBD><CABC>IO
|
|
|
|
|
|
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
|
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);//<2F><>ʼ<EFBFBD><CABC>IO
|
|
|
|
|
|
|
|
|
|
// GPIO_PinRemapConfig(GPIO_Remap2_CAN1,ENABLE);
|
|
|
|
|
//GPIO_PinRemapConfig(GPIO_Remap1_CAN1,ENABLE);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//CAN<41><4E>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD>
|
|
|
|
|
CAN_InitStructure.CAN_TTCM=DISABLE; //<2F><>ʱ<EFBFBD>䴥<EFBFBD><E4B4A5>ͨ<EFBFBD><CDA8>ģʽ //
|
|
|
|
|
CAN_InitStructure.CAN_ABOM=ENABLE;
|
|
|
|
|
// CAN_InitStructure.CAN_ABOM=DISABLE; //<2F><><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD>߹<EFBFBD><DFB9><EFBFBD> //
|
|
|
|
|
CAN_InitStructure.CAN_AWUM=DISABLE; //˯<><CBAF>ģʽͨ<CABD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28><><EFBFBD><EFBFBD>CAN->MCR<43><52>SLEEPλ)//
|
|
|
|
|
CAN_InitStructure.CAN_NART=DISABLE; //<2F><>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD> //
|
|
|
|
|
CAN_InitStructure.CAN_RFLM=DISABLE; //<2F><><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD>,<2C>µĸ<C2B5><C4B8>Ǿɵ<C7BE> //
|
|
|
|
|
CAN_InitStructure.CAN_TXFP=DISABLE; //<2F><><EFBFBD>ȼ<EFBFBD><C8BC>ɱ<EFBFBD><C9B1>ı<EFBFBD>ʶ<EFBFBD><CAB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //
|
|
|
|
|
CAN_InitStructure.CAN_Mode= CAN_Mode_Normal; //ģʽ<C4A3><CABD><EFBFBD>ã<EFBFBD> mode:0,<2C><>ͨģʽ;1,<2C>ػ<EFBFBD>ģʽ; //
|
|
|
|
|
//<2F><><EFBFBD>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
#if 0
|
|
|
|
|
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq; //<2F><><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD>Ծ<EFBFBD><D4BE><EFBFBD><EFBFBD>(Tsjw)Ϊtsjw+1<><31>ʱ<EFBFBD>䵥λ CAN_SJW_1tq CAN_SJW_2tq CAN_SJW_3tq CAN_SJW_4tq
|
|
|
|
|
CAN_InitStructure.CAN_BS1=CAN_BS1_8tq; //Tbs1=tbs1+1<><31>ʱ<EFBFBD>䵥λCAN_BS1_1tq ~CAN_BS1_16tq
|
|
|
|
|
CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;//Tbs2=tbs2+1<><31>ʱ<EFBFBD>䵥λCAN_BS2_1tq ~ CAN_BS2_8tq
|
|
|
|
|
CAN_InitStructure.CAN_Prescaler=9; //<2F><>Ƶϵ<C6B5><CFB5>(Fdiv)Ϊbrp+1 //
|
|
|
|
|
#else
|
|
|
|
|
/* 250k<30><6B><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>87.5%Ϊ<>ã<EFBFBD><C3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>87.5%*/
|
|
|
|
|
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq; //<2F><><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD>Ծ<EFBFBD><D4BE><EFBFBD><EFBFBD>(Tsjw)Ϊtsjw+1<><31>ʱ<EFBFBD>䵥λ CAN_SJW_1tq CAN_SJW_2tq CAN_SJW_3tq CAN_SJW_4tq
|
|
|
|
|
CAN_InitStructure.CAN_BS1=CAN_BS1_13tq; //Tbs1=tbs1+1<><31>ʱ<EFBFBD>䵥λCAN_BS1_1tq ~CAN_BS1_16tq
|
|
|
|
|
CAN_InitStructure.CAN_BS2=CAN_BS2_2tq;//Tbs2=tbs2+1<><31>ʱ<EFBFBD>䵥λCAN_BS2_1tq ~ CAN_BS2_8tq
|
|
|
|
|
CAN_InitStructure.CAN_Prescaler=9; //<2F><>Ƶϵ<C6B5><CFB5>(Fdiv)Ϊbrp+1 //
|
|
|
|
|
#endif
|
|
|
|
|
CAN_Init(CAN1, &CAN_InitStructure); // <20><>ʼ<EFBFBD><CABC>CAN1 CAN = 36000000 / 9 / (1 + 7 + 8); 250k
|
|
|
|
|
|
|
|
|
|
CAN_FilterInitStructure.CAN_FilterNumber=0; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0
|
|
|
|
|
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
|
|
|
|
|
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit; //32λ
|
|
|
|
|
CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;////32λID
|
|
|
|
|
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
|
|
|
|
|
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;//32λMASK
|
|
|
|
|
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
|
|
|
|
|
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO0;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD>FIFO0
|
|
|
|
|
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0
|
|
|
|
|
|
|
|
|
|
CAN_FilterInit(&CAN_FilterInitStructure);//<2F>˲<EFBFBD><CBB2><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
|
|
|
|
|
#if CAN_RX0_INT_ENABLE
|
|
|
|
|
|
|
|
|
|
CAN_ITConfig(CAN1,CAN_IT_FMP0,ENABLE);//FIFO0<4F><30>Ϣ<EFBFBD>Һ<EFBFBD><D2BA>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD>.
|
|
|
|
|
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn;
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // <20><><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>Ϊ1
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // <20><><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>Ϊ0
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
|
|
|
|
NVIC_Init(&NVIC_InitStructure);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
CAN_ITConfig(CAN1,CAN_IT_TME,ENABLE);//FIFO0<4F><30>Ϣ<EFBFBD>Һ<EFBFBD><D2BA>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD>.
|
|
|
|
|
// CAN_ClearITPendingBit(CAN1, CAN_IT_EPV);
|
|
|
|
|
// CAN_ClearITPendingBit(CAN1, CAN_IT_BOF);
|
|
|
|
|
CAN_ITConfig(CAN1,CAN_IT_EPV,ENABLE); /* Error passive Interrupt */
|
|
|
|
|
CAN_ITConfig(CAN1,CAN_IT_BOF,ENABLE); /* Error bus off Interrupt */
|
|
|
|
|
CAN_ITConfig(CAN1,CAN_IT_ERR,ENABLE); /* Error bus off Interrupt */
|
|
|
|
|
|
|
|
|
|
CAN_OperatingModeRequest(CAN1, CAN_OperatingMode_Normal);
|
|
|
|
|
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannel = CAN1_TX_IRQn;
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // <20><><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>Ϊ1
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // <20><><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>Ϊ0
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
|
|
|
|
NVIC_Init(&NVIC_InitStructure);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannel = CAN1_SCE_IRQn;
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // <20><><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>Ϊ1
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // <20><><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>Ϊ0
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
|
|
|
|
NVIC_Init(&NVIC_InitStructure);
|
|
|
|
|
|
|
|
|
|
printf("can 1 open!\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void CAN1_TX_IRQHandler(void)
|
|
|
|
|
{
|
|
|
|
|
unsigned char ret;
|
|
|
|
|
|
|
|
|
|
ret = CAN_TransmitStatus(CAN1, 0);
|
|
|
|
|
if (ret != CAN_TxStatus_Pending)
|
|
|
|
|
{
|
|
|
|
|
bsp_can_msg_sent_hook(0, 0, ret == CAN_TxStatus_Ok);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = CAN_TransmitStatus(CAN1, 1);
|
|
|
|
|
if (ret != CAN_TxStatus_Pending)
|
|
|
|
|
{
|
|
|
|
|
bsp_can_msg_sent_hook(0, 1, ret == CAN_TxStatus_Ok);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = CAN_TransmitStatus(CAN1, 2);
|
|
|
|
|
if (ret != CAN_TxStatus_Pending)
|
|
|
|
|
{
|
|
|
|
|
bsp_can_msg_sent_hook(0, 2, ret == CAN_TxStatus_Ok);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CAN_ClearITPendingBit(CAN1, CAN_IT_TME);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void CAN1_SCE_IRQHandler(void)
|
|
|
|
|
{
|
|
|
|
|
if (CAN_GetITStatus(CAN1, CAN_IT_EPV) == SET)
|
|
|
|
|
{
|
|
|
|
|
if (CAN_GetFlagStatus(CAN1, CAN_FLAG_EPV) == SET)
|
|
|
|
|
{
|
|
|
|
|
can_bus_err_passive_hook(0);
|
|
|
|
|
// printf("!!!CAN1 ERR passive\n");
|
|
|
|
|
//CAN_ClearFlag(CAN1, CAN_FLAG_EPV);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// printf("!!!CAN1 ERR active\n");
|
|
|
|
|
}
|
|
|
|
|
CAN_ClearITPendingBit(CAN1, CAN_IT_EPV);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (CAN_GetITStatus(CAN1, CAN_IT_BOF) == SET)
|
|
|
|
|
{
|
|
|
|
|
if (CAN_GetFlagStatus(CAN1, CAN_FLAG_BOF) == SET)
|
|
|
|
|
{
|
|
|
|
|
can_bus_off_hook(0);
|
|
|
|
|
// printf("!!!CAN1 BUS OFF\n");
|
|
|
|
|
//CAN_ClearFlag(CAN1, CAN_FLAG_BOF);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// printf("!!!CAN1 BUS ON\n");
|
|
|
|
|
}
|
|
|
|
|
CAN_ClearITPendingBit(CAN1, CAN_IT_BOF);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CAN2_SCE_IRQHandler(void)
|
|
|
|
|
{
|
|
|
|
|
if (CAN_GetITStatus(CAN2, CAN_IT_EPV) == SET)
|
|
|
|
|
{
|
|
|
|
|
if (CAN_GetFlagStatus(CAN2, CAN_FLAG_EPV) == SET)
|
|
|
|
|
{
|
|
|
|
|
can_bus_err_passive_hook(1);
|
|
|
|
|
// printf("!!!CAN2 ERR passive\n");
|
|
|
|
|
CAN_ClearFlag(CAN2, CAN_FLAG_EPV);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// printf("!!!CAN2 ERR active\n");
|
|
|
|
|
}
|
|
|
|
|
CAN_ClearITPendingBit(CAN2, CAN_IT_EPV);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (CAN_GetITStatus(CAN2, CAN_IT_BOF) == SET)
|
|
|
|
|
{
|
|
|
|
|
if (CAN_GetFlagStatus(CAN2, CAN_FLAG_BOF) == SET)
|
|
|
|
|
{
|
|
|
|
|
can_bus_off_hook(1);
|
|
|
|
|
// printf("!!!CAN2 BUS OFF\n");
|
|
|
|
|
CAN_ClearFlag(CAN2, CAN_FLAG_BOF);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// printf("!!!CAN2 BUS ON\n");
|
|
|
|
|
}
|
|
|
|
|
CAN_ClearITPendingBit(CAN2, CAN_IT_BOF);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if CAN_RX0_INT_ENABLE //ʹ<><CAB9>RX0<58>ж<EFBFBD>
|
|
|
|
|
//<2F>жϷ<D0B6><CFB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
|
|
|
|
void CAN1_RX0_IRQHandler(void)
|
|
|
|
|
{
|
|
|
|
|
CanRxMsg RxMessage;
|
|
|
|
|
// int i=0;
|
|
|
|
|
// CAN_Receive(CAN1, 0, &RxMessage);
|
|
|
|
|
// Can_Send_Msg(RxMessage.Data,8);
|
|
|
|
|
|
|
|
|
|
CAN_Receive(CAN1,CAN_FIFO0,&RxMessage);
|
|
|
|
|
bsp_can_msg_receive_hook(0, (CanTxMsg *)&RxMessage); /* ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>CanRxMsg <20><> CanTxMsg <20><>ʽ<EFBFBD><CABD>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܼ<EFBFBD><DCBC><EFBFBD>*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////CAN2////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void CAN2_TX_IRQHandler(void)
|
|
|
|
|
{
|
|
|
|
|
unsigned char ret;
|
|
|
|
|
|
|
|
|
|
ret = CAN_TransmitStatus(CAN2, 0);
|
|
|
|
|
if (ret != CAN_TxStatus_Pending)
|
|
|
|
|
{
|
|
|
|
|
bsp_can_msg_sent_hook(1, 0, ret == CAN_TxStatus_Ok);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = CAN_TransmitStatus(CAN2, 1);
|
|
|
|
|
if (ret != CAN_TxStatus_Pending)
|
|
|
|
|
{
|
|
|
|
|
bsp_can_msg_sent_hook(1, 1, ret == CAN_TxStatus_Ok);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = CAN_TransmitStatus(CAN2, 2);
|
|
|
|
|
if (ret != CAN_TxStatus_Pending)
|
|
|
|
|
{
|
|
|
|
|
bsp_can_msg_sent_hook(1, 2, ret == CAN_TxStatus_Ok);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CAN_ClearITPendingBit(CAN2, CAN_IT_TME);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
u8 CAN2_Mode_Init(void)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
|
|
|
CAN_InitTypeDef CAN_InitStructure;
|
|
|
|
|
CAN_FilterInitTypeDef CAN_FilterInitStructure;
|
|
|
|
|
#if CAN_RX2_INT_ENABLE
|
|
|
|
|
NVIC_InitTypeDef NVIC_InitStructure;
|
|
|
|
|
#endif
|
|
|
|
|
SYS_ENTER_CRITICAL();
|
|
|
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);//ʹ<><CAB9>PORTBʱ<42><CAB1>
|
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);//ʹ<><CAB9>CAN1ʱ<31><CAB1>
|
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2, ENABLE);//ʹ<><CAB9>CAN2ʱ<32><CAB1>
|
|
|
|
|
|
|
|
|
|
CAN_OperatingModeRequest(CAN2, CAN_OperatingMode_Initialization);
|
|
|
|
|
|
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
|
|
|
|
|
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
GPIO_Init(GPIOB, &GPIO_InitStructure); //<2F><>ʼ<EFBFBD><CABC>IO
|
|
|
|
|
|
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
|
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
GPIO_Init(GPIOB, &GPIO_InitStructure);//<2F><>ʼ<EFBFBD><CABC>IO
|
|
|
|
|
|
|
|
|
|
// GPIO_PinRemapConfig(GPIO_Remap_CAN2,ENABLE);
|
|
|
|
|
|
|
|
|
|
//CAN<41><4E>Ԫ<EFBFBD><D4AA><EFBFBD><EFBFBD>
|
|
|
|
|
CAN_InitStructure.CAN_TTCM=DISABLE; //<2F><>ʱ<EFBFBD>䴥<EFBFBD><E4B4A5>ͨ<EFBFBD><CDA8>ģʽ //
|
|
|
|
|
CAN_InitStructure.CAN_ABOM=ENABLE; //<2F><><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD>߹<EFBFBD><DFB9><EFBFBD> //
|
|
|
|
|
CAN_InitStructure.CAN_AWUM=DISABLE; //˯<><CBAF>ģʽͨ<CABD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28><><EFBFBD><EFBFBD>CAN->MCR<43><52>SLEEPλ)//
|
|
|
|
|
CAN_InitStructure.CAN_NART=DISABLE; //<2F><>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD> //
|
|
|
|
|
CAN_InitStructure.CAN_RFLM=DISABLE; //<2F><><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD><EFBFBD><EFBFBD>,<2C>µĸ<C2B5><C4B8>Ǿɵ<C7BE> //
|
|
|
|
|
CAN_InitStructure.CAN_TXFP=ENABLE; //<2F><><EFBFBD>ȼ<EFBFBD><C8BC>ɱ<EFBFBD><C9B1>ı<EFBFBD>ʶ<EFBFBD><CAB6><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //
|
|
|
|
|
CAN_InitStructure.CAN_Mode= CAN_Mode_Normal; //ģʽ<C4A3><CABD><EFBFBD>ã<EFBFBD> mode:0,<2C><>ͨģʽ;1,<2C>ػ<EFBFBD>ģʽ; //
|
|
|
|
|
//<2F><><EFBFBD>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
#if 0
|
|
|
|
|
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq; //<2F><><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD>Ծ<EFBFBD><D4BE><EFBFBD><EFBFBD>(Tsjw)Ϊtsjw+1<><31>ʱ<EFBFBD>䵥λ CAN_SJW_1tq CAN_SJW_2tq CAN_SJW_3tq CAN_SJW_4tq
|
|
|
|
|
CAN_InitStructure.CAN_BS1=CAN_BS1_4tq; //Tbs1=tbs1+1<><31>ʱ<EFBFBD>䵥λCAN_BS1_1tq ~CAN_BS1_16tq
|
|
|
|
|
CAN_InitStructure.CAN_BS2=CAN_BS2_3tq; //Tbs2=tbs2+1<><31>ʱ<EFBFBD>䵥λCAN_BS2_1tq ~ CAN_BS2_8tq
|
|
|
|
|
CAN_InitStructure.CAN_Prescaler=9; //<2F><>Ƶϵ<C6B5><CFB5>(Fdiv)Ϊbrp+1 //
|
|
|
|
|
#else
|
|
|
|
|
/* 500kpbs <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD>80%<25><>һ<EFBFBD><D2BB>Ϊ<EFBFBD><CEAA> */
|
|
|
|
|
CAN_InitStructure.CAN_SJW=CAN_SJW_2tq;
|
|
|
|
|
CAN_InitStructure.CAN_BS1=CAN_BS1_9tq;
|
|
|
|
|
CAN_InitStructure.CAN_BS2=CAN_BS2_2tq;
|
|
|
|
|
CAN_InitStructure.CAN_Prescaler=6; // 83.3%<25><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //
|
|
|
|
|
#endif
|
|
|
|
|
CAN_Init(CAN2, &CAN_InitStructure); // <20><>ʼ<EFBFBD><CABC>CAN2
|
|
|
|
|
|
|
|
|
|
#ifdef _Filter
|
|
|
|
|
CAN_FilterInitStructure.CAN_FilterNumber=14; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0
|
|
|
|
|
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask; //<2F><><EFBFBD><EFBFBD>λģʽ
|
|
|
|
|
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit; //32λ<32><CEBB>
|
|
|
|
|
CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000; //32λID
|
|
|
|
|
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
|
|
|
|
|
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;//32λMASK
|
|
|
|
|
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
|
|
|
|
|
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_Filter_FIFO1;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0<EFBFBD><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD>FIFO0
|
|
|
|
|
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0
|
|
|
|
|
CAN_FilterInit(&CAN_FilterInitStructure); //<2F>˲<EFBFBD><CBB2><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
|
CAN_Filter_register(Standard_frame,Evcan_HandleTable);
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if CAN_RX2_INT_ENABLE
|
|
|
|
|
|
|
|
|
|
CAN_ITConfig(CAN2,CAN_IT_FMP1,ENABLE);//FIFO0<4F><30>Ϣ<EFBFBD>Һ<EFBFBD><D2BA>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD>.
|
|
|
|
|
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannel = CAN2_RX1_IRQn;
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // <20><><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>Ϊ1
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // <20><><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>Ϊ0
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
|
|
|
|
NVIC_Init(&NVIC_InitStructure);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
CAN_ITConfig(CAN2,CAN_IT_TME,ENABLE);//FIFO0<4F><30>Ϣ<EFBFBD>Һ<EFBFBD><D2BA>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD>.
|
|
|
|
|
|
|
|
|
|
// CAN_ClearITPendingBit(CAN1, CAN_IT_EPV);
|
|
|
|
|
// CAN_ClearITPendingBit(CAN1, CAN_IT_BOF);
|
|
|
|
|
CAN_ITConfig(CAN2,CAN_IT_EPV,ENABLE); /* Error passive Interrupt */
|
|
|
|
|
CAN_ITConfig(CAN2,CAN_IT_BOF,ENABLE); /* Error bus off Interrupt */
|
|
|
|
|
CAN_ITConfig(CAN2,CAN_IT_ERR,ENABLE); /* Error bus off Interrupt */
|
|
|
|
|
CAN_OperatingModeRequest(CAN2, CAN_OperatingMode_Normal);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannel = CAN2_TX_IRQn;
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // <20><><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>Ϊ1
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // <20><><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>Ϊ0
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
|
|
|
|
NVIC_Init(&NVIC_InitStructure);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannel = CAN2_SCE_IRQn;
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // <20><><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>Ϊ1
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // <20><><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>Ϊ0
|
|
|
|
|
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
|
|
|
|
NVIC_Init(&NVIC_InitStructure);
|
|
|
|
|
SYS_EXIT_CRITICAL();
|
|
|
|
|
printf("can 2 open!\n");
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////CAN2////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
#if CAN_RX2_INT_ENABLE //ʹ<><CAB9>RX2<58>ж<EFBFBD>
|
|
|
|
|
//<2F>жϷ<D0B6><CFB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
void CAN2_RX1_IRQHandler(void)
|
|
|
|
|
{
|
|
|
|
|
CanRxMsg RxMessage2;
|
|
|
|
|
|
|
|
|
|
// CAN_Receive(CAN2, CAN_FIFO0, &RxMessage);
|
|
|
|
|
CAN_Receive(CAN2, CAN_FIFO1, &RxMessage2);
|
|
|
|
|
if (RxMessage2.RTR == CAN_RTR_DATA)
|
|
|
|
|
{
|
|
|
|
|
bsp_can_msg_receive_hook(1, (CanTxMsg *)&RxMessage2); /* ע<><D7A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>CanRxMsg <20><> CanTxMsg <20><>ʽ<EFBFBD><CABD>ͬ<EFBFBD><CDAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܼ<EFBFBD><DCBC><EFBFBD>*/
|
|
|
|
|
}
|
|
|
|
|
// Can2_Send_Msg(RxMessage2.Data,8);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief: <EFBFBD>ӿڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣ
|
|
|
|
|
*
|
|
|
|
|
* @param channel: can ͨ<EFBFBD><EFBFBD><EFBFBD>ţ<EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>MCU_CAN_CHANNEL_NUM
|
|
|
|
|
* @param pt_bsp_cfg[IN]: <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
*
|
|
|
|
|
* @return:
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>ؽ<EFBFBD><EFBFBD><EFBFBD>: true - open <EFBFBD>ɹ<EFBFBD> false - open ʧ<EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
bool bsp_can_open(unsigned char channel, const bsp_can_cfg_t * pt_bsp_cfg)
|
|
|
|
|
{
|
|
|
|
|
if (channel == 0)
|
|
|
|
|
{
|
|
|
|
|
CAN_Mode_Init();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
CAN2_Mode_Init();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief: <EFBFBD>رսӿ<EFBFBD>
|
|
|
|
|
*
|
|
|
|
|
* @return:
|
|
|
|
|
* <EFBFBD><EFBFBD><EFBFBD>ؽ<EFBFBD><EFBFBD><EFBFBD>: true - open <EFBFBD>ɹ<EFBFBD> false - open ʧ<EFBFBD><EFBFBD>
|
|
|
|
|
*/
|
|
|
|
|
bool bsp_can_close(unsigned char channel)
|
|
|
|
|
{
|
|
|
|
|
if (channel == 0)
|
|
|
|
|
{
|
|
|
|
|
CAN_OperatingModeRequest(CAN1, CAN_OperatingMode_Initialization);
|
|
|
|
|
CAN_DeInit(CAN1);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
CAN_OperatingModeRequest(CAN2, CAN_OperatingMode_Initialization);
|
|
|
|
|
CAN_DeInit(CAN2);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned char bsp_can_msg_send(unsigned char channel, const can_frame_t * pt_fram)
|
|
|
|
|
{
|
|
|
|
|
u8 mbox;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mbox= CAN_Transmit((channel == 0)?CAN1:CAN2, (can_frame_t *)pt_fram);
|
|
|
|
|
|
|
|
|
|
if (mbox == CAN_TxStatus_NoMailBox)
|
|
|
|
|
{
|
|
|
|
|
mbox = BSP_CAN_SEND_FAIL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return mbox;
|
|
|
|
|
}
|
|
|
|
|
|