diff --git a/CAN/CAN/kf_it.c b/CAN/CAN/kf_it.c index 7a7c41c4ab343c7f103156fbade11faf499a1c89..bf84a773fa240a4d6d52efff176806b31d1e3b17 100644 --- a/CAN/CAN/kf_it.c +++ b/CAN/CAN/kf_it.c @@ -1,62 +1,89 @@ -/**************************************************************************************** - * - * 文件名 kf_it.c - * 作 者 ChipON_AE/FAE_Group - * 版 本 V2.1 - * 日 期 2019-11-16 - * 描 述 该文件提供了中断入口地址,在CAN1中断处理RAM报文 - * - ****************************************************************************************/ +/**************************************************************************************** + * + * 鏂囦欢鍚 kf_it.c + * 浣 鑰 ChipON_AE/FAE_Group + * 鐗 鏈 V2.1 + * 鏃 鏈 2019-11-16 + * 鎻 杩 璇ユ枃浠舵彁渚涗簡涓柇鍏ュ彛鍦板潃,鍦–AN1涓柇澶勭悊RAM鎶ユ枃 + * + ****************************************************************************************/ #include"system_init.h" - - -//***************************************************************************************** -// CAN1中断函数 -//***************************************************************************************** // - -void __attribute__((interrupt))_CAN1_exception (void) -{ - uint8_t i; - static uint8_t Receice_addr=0x00;//接收RAM偏移地址 - CAN_MessageTypeDef CAN_MessageStructrue;//接收报文结构体 - /* 清CAN中断标志 */ - INT_Clear_Interrupt_Flag(INT_CAN1); - - /* 判断是否为CAN发送标志 */ - if(CAN_Get_INT_Flag(CAN1_SFR,CAN_INT_TRANSMIT) != RESET) - { - CAN_Clear_INT_Flag(CAN1_SFR,CAN_INT_TRANSMIT); - } - - - /* 判断是否为CAN接收标志 */ - if(CAN_Get_INT_Flag(CAN1_SFR,CAN_INT_RECEIVE) != RESET) - { - GPIO_Toggle_Output_Data_Config (GPIOB_SFR,GPIO_PIN_MASK_8); - /* 接收RAM数据 */ - CAN_Receive_Message_Configuration(CAN1_SFR,Receice_addr,&CAN_MessageStructrue); - /* RAM地址自跳 */ - Receice_addr+=0x10; - /* 释放一次计数器 */ - CAN_Release_Receive_Buffer(CAN1_SFR,1); - - if(CAN_MessageStructrue.m_RemoteTransmit != CAN_DATA_FRAME)//远程帧 - { - //用户代码 - ReceiveData[0]=0xAA; - USART_Send(USART2_SFR,ReceiveData,1); - } - else //数据帧 - { - /* 获取数据 */ - for(i=0;iCTLR &= ~0x300; + } + + /* 鍒ゆ柇鎬荤嚎閿欒鏍囧織 */ + if(CAN_Get_INT_Flag(CAN1_SFR,CAN_INT_BUS_ERROR) != RESET) + { + CAN_Clear_INT_Flag(CAN1_SFR,CAN_INT_BUS_ERROR); + CAN1_SFR->CTLR &= ~0x300; //鎬荤嚎閿欒涓嶉噸鍙 + } + /* 鍒ゆ柇浠茶鏍囧織 */ + if(CAN_Get_INT_Flag(CAN1_SFR,CAN_INT_ARBITRATION_LOST) != RESET) + { + CAN_Clear_INT_Flag(CAN1_SFR,CAN_INT_ARBITRATION_LOST); + bus_temp = CAN1_SFR->RCR;//璇诲彇RCR鏍囧織浣嶏紝浣滅敤涓嬩竴娆′徊瑁佹爣蹇椾綅 + CAN1_SFR->CTLR = CAN1_SFR->CTLR | 0x300;//浠茶澶辫触閲嶅彂 + } + + /* 鍒ゆ柇鏄惁涓篊AN鎺ユ敹鏍囧織 */ + if(CAN_Get_INT_Flag(CAN1_SFR,CAN_INT_RECEIVE) != RESET) + { +// GPIO_Toggle_Output_Data_Config (GPIOB_SFR,GPIO_PIN_MASK_8); + tmprmc = (CAN1_SFR->CTLR&CAN_CTLR_CANRMC)>>CAN_CTLR_CANRMC0_POS;//褰撳墠閭鎶ユ枃閭璁℃暟 + tmprmd =CAN1_SFR->RCR >>16; //浠匒02鐗堟湰鏈夋鍔熻兘锛岃鍙栦笅涓涓偖绠辨寚閽堝湴鍧 + if(tmprmd >= tmprmc) + Receice_addr =(tmprmd -tmprmc)*0x10; + else + Receice_addr =(tmprmd +16 -tmprmc)*0x10; + + /* 鎺ユ敹RAM鏁版嵁 */ + CAN_Receive_Message_Configuration(CAN1_SFR,Receice_addr,&CAN_MessageStructrue); + + /* 閲婃斁涓娆¤鏁板櫒 */ + j=30; + SFR_SET_BIT_ASM(CAN1_SFR->CTLR, CAN_CTLR_RELRX_POS); + while((tmprmc - ((CAN1_SFR->CTLR&CAN_CTLR_CANRMC)>>CAN_CTLR_CANRMC0_POS) != 1)&j) + j--; //绛夊緟璁℃暟鏍囧織浣嶉噴鏀惧畬鎴愬強瓒呮椂閫鍑 + SFR_CLR_BIT_ASM(CAN1_SFR->CTLR, CAN_CTLR_RELRX_POS); + + if(CAN_MessageStructrue.m_RemoteTransmit != CAN_DATA_FRAME)//杩滅▼甯 + { + //鐢ㄦ埛浠g爜 + ReceiveData[0]=0xAA; + + } + else //鏁版嵁甯 + { + /* 鑾峰彇鏁版嵁 */ + for(i=0;i