/** * @brief Configures the CAN, transmit and receive by polling * @param None * @retval PASSED if the reception is well done, FAILED in other case */ TestStatus CAN_Polling(void) { CAN_InitTypeDef CAN_InitStructure; CAN_FilterInitTypeDef CAN_FilterInitStructure; CanTxMsg TxMessage; CanRxMsg RxMessage; uint32_t i = 0; uint8_t TransmitMailbox = 0; /* CAN register init */ CAN_DeInit(CAN); CAN_StructInit(&CAN_InitStructure); /* CAN cell init */ 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 = DISABLE; CAN_InitStructure.CAN_Mode = CAN_Mode_LoopBack; CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; /* CAN Baudrate = 125kbps (CAN clocked at 36 MHz) */ CAN_InitStructure.CAN_BS1 = CAN_BS1_9tq; CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq; CAN_InitStructure.CAN_Prescaler = 16; CAN_Init(CAN, &CAN_InitStructure); /* CAN filter init */ CAN_FilterInitStructure.CAN_FilterNumber = 0; 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 = 0; CAN_FilterInitStructure.CAN_FilterActivation = ENABLE; CAN_FilterInit(&CAN_FilterInitStructure); /* transmit */ TxMessage.StdId = 0x11; TxMessage.RTR = CAN_RTR_DATA; TxMessage.IDE = CAN_ID_STD; TxMessage.DLC = 2; TxMessage.Data[0] = 0xCA; TxMessage.Data[1] = 0xFE; TransmitMailbox = CAN_Transmit(CAN, &TxMessage); i = 0; while((CAN_TransmitStatus(CAN, TransmitMailbox) != CANTXOK) && (i != 0xFFFF)) { i++; } i = 0; while((CAN_MessagePending(CAN, CAN_FIFO0) < 1) && (i != 0xFFFF)) { i++; } /* receive */ RxMessage.StdId = 0x00; RxMessage.IDE = CAN_ID_STD; RxMessage.DLC = 0; RxMessage.Data[0] = 0x00; RxMessage.Data[1] = 0x00; CAN_Receive(CAN, CAN_FIFO0, &RxMessage); if (RxMessage.StdId != 0x11) { return FAILED; } if (RxMessage.IDE != CAN_ID_STD) { return FAILED; } if (RxMessage.DLC != 2) { return FAILED; } if ((RxMessage.Data[0]<<8|RxMessage.Data[1]) != 0xCAFE) { return FAILED; } return PASSED; /* Test Passed */ }
//受信 int main(void) { char str[150] = {0}; init(); GPIOD->BSRRL = GPIO_Pin_12; sprintf(str,"Start CAN!\n\r"); transmit_uart3_s(str); while(1) { #ifndef USE_INTERRUPT_CAN_RX GPIOD->BSRRL = GPIO_Pin_13; GPIOD->BSRRH = GPIO_Pin_14; while(0 == CAN_MessagePending(CAN1, CAN_FIFO0)); CAN_Receive(CAN1, CAN_FIFO0, &can_rx_flame);//受信 GPIOD->BSRRL = GPIO_Pin_14; GPIOD->BSRRH = GPIO_Pin_13; #else if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0) == SET){ GPIOD->BSRRH = GPIO_Pin_13; }else{ GPIOD->BSRRL = GPIO_Pin_13; } #endif while(ticker > 300){ ticker = 0; sprintf(str,"StId:0x%X \n\rIDE:0x%X \n\rRTR:%d \n\rDLC:%X \n\rData[0]:0x%X \n\rData[1]:0x%X \n\rData[2]:0x%X \n\rData[3]:0x%X \n\rData[4]:0x%X \n\rData[5]:0x%X \n\rData[6]:0x%X \n\rData[7]:0x%X \n\rFMI:%d \n\rRerror:%d \n\rTerror:%d\n\r\n" ,can_rx_flame.StdId ,can_rx_flame.IDE ,can_rx_flame.RTR ,can_rx_flame.DLC ,can_rx_flame.Data[0] ,can_rx_flame.Data[1] ,can_rx_flame.Data[2] ,can_rx_flame.Data[3] ,can_rx_flame.Data[4] ,can_rx_flame.Data[5] ,can_rx_flame.Data[6] ,can_rx_flame.Data[7] ,can_rx_flame.FMI ,CAN_GetReceiveErrorCounter(CAN1) ,CAN_GetLSBTransmitErrorCounter(CAN1)); /* sprintf(str," start:%X\n\r select:%X\n\r left_x:%X\n\r left_y:%X\n\r right_x:%X\n\r right_y:%X\n\r L1:%X\n\r L2:%X\n\r R1:%X\n\r R2:%X\n\r Tri:%X\n\r Cir:%X\n\r Cro:%X\n\r Squ:%X\n\r Up:%X\n\r Right:%X\n\r Dow:%X\n\r left:%X\n\r CAN_C:%d\n\r" ,psbutton.start.value ,psbutton.select.value ,psbutton.left_x.value ,psbutton.left_y.value ,psbutton.right_x.value ,psbutton.right_y.value ,psbutton.L1.value ,psbutton.L2.value ,psbutton.R1.value ,psbutton.R2.value ,psbutton.Triangle.value ,psbutton.Circle.value ,psbutton.Cross.value ,psbutton.Square.value ,psbutton.Up.value ,psbutton.Down.value ,psbutton.Right.value ,psbutton.Left.value ,can_tx_count); */ transmit_uart3_s(str); //VCP_send_str(str); } } }
/** * @brief Configures the CAN, transmit and receive using interrupt. * @param None * @retval PASSED if the reception is well done, FAILED in other case */ TestStatus CAN_Interrupt(void) { CAN_InitTypeDef sCAN; CAN_TxMsgTypeDef TxMsg; uint32_t i = 0; /* Set the HCLK division factor = 1 for CAN1*/ CAN_BRGInit(MDR_CAN1,CAN_HCLKdiv1); /* CAN register init */ CAN_DeInit(MDR_CAN1); /* CAN cell init */ CAN_StructInit (&sCAN); sCAN.CAN_ROP = ENABLE; sCAN.CAN_SAP = ENABLE; sCAN.CAN_STM = ENABLE; sCAN.CAN_ROM = DISABLE; sCAN.CAN_PSEG = CAN_PSEG_Mul_2TQ; sCAN.CAN_SEG1 = CAN_SEG1_Mul_3TQ; sCAN.CAN_SEG2 = CAN_SEG2_Mul_2TQ; sCAN.CAN_SJW = CAN_SJW_Mul_2TQ; sCAN.CAN_SB = CAN_SB_3_SAMPLE; sCAN.CAN_BRP = 1; CAN_Init (MDR_CAN1,&sCAN); CAN_Cmd(MDR_CAN1, ENABLE); /* Enable CAN1 interrupt */ NVIC_EnableIRQ(CAN1_IRQn); /* Enable CAN1 GLB_INT and RX_INT interrupts */ CAN_ITConfig( MDR_CAN1, CAN_IT_GLBINTEN | CAN_IT_RXINTEN, ENABLE); /* Enable CAN1 interrupt from receive buffer */ CAN_RxITConfig( MDR_CAN1 ,(1<<rx_buf), ENABLE); /* Enable CAN1 interrupt from transmit buffer */ CAN_TxITConfig( MDR_CAN1 ,(1<<tx_buf), ENABLE); /* receive buffer enable */ CAN_Receive(MDR_CAN1, rx_buf, DISABLE); /* transmit */ TxMsg.IDE = CAN_ID_EXT; TxMsg.DLC = 0x04; TxMsg.PRIOR_0 = DISABLE; TxMsg.ID = 0x15555555; TxMsg.Data[1] = 0; TxMsg.Data[0] = 0x12345678; CAN_Transmit(MDR_CAN1, tx_buf, &TxMsg); /* initialize the value that will be returned */ ret = 0xFF; /* receive message with interrupt handling */ i=0; while((ret == 0xFF) && (i < 0xFFF)) { i++; } if (i == 0xFFF) { ret=0; } /* disable interrupt handling */ CAN_ITConfig(MDR_CAN1, CAN_IT_GLBINTEN | CAN_IT_RXINTEN, DISABLE); return (TestStatus)ret; }
/* * 函数名:CAN_Polling * 描述 :配置 CAN 的工作模式为 回环模式 * 输入 :无 * 输出 : -PASSED 成功 * -FAILED 失败 * 调用 :内部调用 */ TestStatus CAN_Polling(void) { CAN_InitTypeDef CAN_InitStructure; CAN_FilterInitTypeDef CAN_FilterInitStructure; CanTxMsg TxMessage; CanRxMsg RxMessage; uint32_t i = 0; uint8_t TransmitMailbox = 0; /* CAN register init */ CAN_DeInit(CAN1); CAN_StructInit(&CAN_InitStructure); /* CAN cell init */ CAN_InitStructure.CAN_TTCM=DISABLE; // 时间触发通信禁止 CAN_InitStructure.CAN_ABOM=DISABLE; // 离线退出是在中断置位清0后退出 CAN_InitStructure.CAN_AWUM=DISABLE; // 自动唤醒模式:清零sleep CAN_InitStructure.CAN_NART=DISABLE; // 自动重新传送豹纹,知道发送成功 CAN_InitStructure.CAN_RFLM=DISABLE; // FIFO没有锁定,新报文覆盖旧报文 CAN_InitStructure.CAN_TXFP=DISABLE; // 发送报文优先级确定:标志符 CAN_InitStructure.CAN_Mode=CAN_Mode_LoopBack; // 回环模式 CAN_InitStructure.CAN_SJW=CAN_SJW_1tq; // 1tq、BS1、BS2的值跟波特率有关 CAN_InitStructure.CAN_BS1=CAN_BS1_8tq; CAN_InitStructure.CAN_BS2=CAN_BS2_7tq; CAN_InitStructure.CAN_Prescaler=5; // 分频系数为5 CAN_Init(CAN1, &CAN_InitStructure); // 初始化CAN1 /* CAN filter init */ CAN_FilterInitStructure.CAN_FilterNumber=0; 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=0; CAN_FilterInitStructure.CAN_FilterActivation=ENABLE; CAN_FilterInit(&CAN_FilterInitStructure); /* transmit */ TxMessage.StdId=0x11; // 设定标准标识符(11位,扩展的为29位) TxMessage.RTR=CAN_RTR_DATA; // 传输消息的帧类型为数据帧(还有远程帧) TxMessage.IDE=CAN_ID_STD; // 消息标志符实验标准标识符 TxMessage.DLC=2; // 发送两帧,一帧8位 TxMessage.Data[0]=0xCA; // 第一帧数据 TxMessage.Data[1]=0xFE; // 第二帧数据 TransmitMailbox=CAN_Transmit(CAN1, &TxMessage); i = 0; // 用于检查消息传输是否正常 while((CAN_TransmitStatus(CAN1, TransmitMailbox) != CANTXOK) && (i != 0xFF)) { i++; } i = 0; // 检查返回的挂号的信息数目 while((CAN_MessagePending(CAN1, CAN_FIFO0) < 1) && (i != 0xFF)) { i++; } /* receive */ RxMessage.StdId=0x00; RxMessage.IDE=CAN_ID_STD; RxMessage.DLC=0; RxMessage.Data[0]=0x00; RxMessage.Data[1]=0x00; CAN_Receive(CAN1, CAN_FIFO0, &RxMessage); if (RxMessage.StdId!=0x11) { return FAILED; } if (RxMessage.IDE!=CAN_ID_STD) { return FAILED; } if (RxMessage.DLC!=2) { return FAILED; } /* 判断发送的信息和接收的信息是否相等 */ if ((RxMessage.Data[0]<<8|RxMessage.Data[1])!=0xCAFE) { return FAILED; } //printf("receive data:0X%X,0X%X",RxMessage.Data[0], RxMessage.Data[1]); return PASSED; /* Test Passed */ }
TestStatus CAN_Polling(void) { CanTxMsg TxMessage; CanRxMsg RxMessage; uint32_t i = 0; uint8_t TransmitMailbox = 0; /* transmit */ TxMessage.StdId=0x11; // 设定标准标识符(11位,扩展的为29位) TxMessage.RTR=CAN_RTR_DATA; // 传输消息的帧类型为数据帧(还有远程帧) TxMessage.IDE=CAN_ID_STD; // 消息标志符实验标准标识符 TxMessage.DLC=2; // 发送两帧,一帧8位 TxMessage.Data[0]=0xCA; // 第一帧数据 TxMessage.Data[1]=0xFE; // 第二帧数据 TransmitMailbox=CAN_Transmit(CAN1, &TxMessage); i = 0; // 用于检查消息传输是否正常 while((CAN_TransmitStatus(CAN1, TransmitMailbox) != CANTXOK) && (i != 0xFF)) { i++; } i = 0; // 检查返回的挂号的信息数目 while((CAN_MessagePending(CAN1, CAN_FIFO0) < 1) && (i != 0xFF)) { i++; } /* receive */ RxMessage.StdId=0x00; RxMessage.IDE=CAN_ID_STD; RxMessage.DLC=0; RxMessage.Data[0]=0x00; RxMessage.Data[1]=0x00; CAN_Receive(CAN1, CAN_FIFO0, &RxMessage); if (RxMessage.StdId!=0x11) { return FAILED; } if (RxMessage.IDE!=CAN_ID_STD) { return FAILED; } if (RxMessage.DLC!=2) { return FAILED; } /* 判断发送的信息和接收的信息是否相等 */ if ((RxMessage.Data[0]<<8|RxMessage.Data[1])!=0xCAFE) { return FAILED; } //printf("receive data:0X%X,0X%X",RxMessage.Data[0], RxMessage.Data[1]); return PASSED; /* Test Passed */ }
/** * @brief Configures the CAN, transmit and receive using interrupt. * @param None * @retval PASSED if the reception is well done, FAILED in other case */ TestStatus CAN_Interrupt(void) { CAN_InitTypeDef sCAN; CAN_RTRReplyTypeDef RTRReply; CAN_RTRMessageTypeDef RTRMes; CAN_FilterInitTypeDef Filter; uint32_t i = 0; /* Set the HCLK division factor = 1 for CAN1*/ CAN_BRGInit(MDR_CAN1,CAN_HCLKdiv1); /* CAN register init */ CAN_DeInit(MDR_CAN1); /* CAN cell init */ CAN_StructInit (&sCAN); sCAN.CAN_ROP = ENABLE; sCAN.CAN_SAP = ENABLE; sCAN.CAN_STM = ENABLE; sCAN.CAN_ROM = DISABLE; sCAN.CAN_PSEG = CAN_PSEG_Mul_2TQ; sCAN.CAN_SEG1 = CAN_SEG1_Mul_3TQ; sCAN.CAN_SEG2 = CAN_SEG2_Mul_2TQ; sCAN.CAN_SJW = CAN_SJW_Mul_2TQ; sCAN.CAN_SB = CAN_SB_3_SAMPLE; sCAN.CAN_BRP = 1; CAN_Init (MDR_CAN1,&sCAN); CAN_Cmd(MDR_CAN1, ENABLE); /* Enable CAN1 interrupt */ NVIC_EnableIRQ(CAN1_IRQn); /* Enable CAN1 GLB_INT and RX_INT interrupts */ CAN_ITConfig( MDR_CAN1, CAN_IT_GLBINTEN | CAN_IT_RXINTEN, ENABLE); /* Enable CAN1 interrupt from receive buffer */ CAN_RxITConfig( MDR_CAN1 ,(1<<rx_buf), ENABLE); /* config buffer for receive RTR reply*/ CAN_Receive(MDR_CAN1, rx_buf, DISABLE); Filter.Filter_ID = 0x00005678; Filter.Mask_ID = 0x0000FFFF; CAN_FilterInit(MDR_CAN1, rx_buf, &Filter); /* config buffer for transmit RTR reply*/ RTRReply.ID = 0x00005678; RTRReply.IDE = CAN_ID_EXT; RTRReply.DLC = 2; RTRReply.Data[0] = 0x7777; RTRReply.Data[1] = 0x0; CAN_RTRReply(MDR_CAN1, rtr_reply_buf, &RTRReply); Filter.Filter_ID = 0x00001234; Filter.Mask_ID = 0x0000FFFF; CAN_FilterInit(MDR_CAN1, rtr_reply_buf, &Filter); /* config buffer for transmit RTR request*/ RTRMes.ID = 0x00001234; RTRMes.IDE = CAN_ID_EXT; RTRMes.PRIOR_0 = DISABLE; /* transmit RTR*/ CAN_SendRTR(MDR_CAN1, rtr_request_buf, &RTRMes); /* initialize the value that will be returned */ ret = 0xFF; /* receive message with interrupt handling */ i=0; while((ret == 0xFF) && (i < 0xFFF)) { i++; } if (i == 0xFFF) { ret=0; } /* disable interrupt handling */ CAN_ITConfig(MDR_CAN1, CAN_IT_GLBINTEN | CAN_IT_RXINTEN, DISABLE); return (TestStatus)ret; }
//Ö÷¿ØÏòGPS°å·¢Ë͵ÄÊýŸÝ void CAN1_RX1_IRQHandler(void) { CanRxMsg RxMessage; CAN_Receive(CAN1, CAN_FIFO1, &RxMessage); }
/******************************************************************************* * Function Name : PollCAN * Description : periodièno procesiranje CAN protokola v glavni zanki * Input : * Output : * Return : FLASH_COMPLETE na bootloader strani, FLASH_STATUS na strani * klienta (glej stm32f10x_flash.h) *******************************************************************************/ int PollCAN(CanRxMsg *p) { static int addr,n=0; // statièni register za zaèetno. adreso, index IAP stringa int i; // .... CanRxMsg Rx; if(!p) { if(!CAN_MessagePending(CAN1, CAN_FIFO0)) return(EOF); p=&Rx; CAN_Receive(CAN1,CAN_FIFO0, p); } switch(p->StdId) { //---------------------------------------------------------------------------------------------- // client - deep sleep (watchdog), no ack. case _ID_IAP_GO: NVIC_SystemReset(); break; //---------------------------------------------------------------------------------------------- // client - sign FW case _ID_IAP_SIGN: SendAck(crcSIGN()); break; //---------------------------------------------------------------------------------------------- // client - setup adrese, no ack case _ID_IAP_ADDRESS: addr=*(int *)p->Data; break; //---------------------------------------------------------------------------------------------- // client - programiranje 2x4 bytov, ack case _ID_IAP_DWORD: for(i=p->DLC; i<8; ++i) p->Data[i]=((char *)addr)[i]; i=FlashProgram32(addr,*(int *)(&p->Data[0])); addr+=4; ++_Words32Received; if(p->DLC>4) { i |= FlashProgram32(addr,*(int *)(&p->Data[4])); } addr+=4; ++_Words32Received; SendAck(i); break; //---------------------------------------------------------------------------------------------- // client - brisanje, ack case _ID_IAP_ERASE: _Words32Received=0; Watchdog(); SendAck(EraseFLASH(*(int *)p->Data)); break; //---------------------------------------------------------------------------------------------- // client - brisanje, ack case _ID_IAP_STRING: for(i=0; i<p->DLC && n<_IAP_STRING_LEN; ++i, ++n) _Iap_string[n]=p->Data[i]; if(_Iap_string[n-1]=='\0' || _Iap_string[n-1]=='\r' || _Iap_string[n-1]=='\n' || n==_IAP_STRING_LEN) { n=0; CanHexProg(NULL); } break; //---------------------------------------------------------------------------------------------- // client - brisanje, ack case _ID_IAP_PING: SendAck(0); break; //---------------------------------------------------------------------------------------------- // server - acknowledge received case _ID_IAP_ACK: return(p->Data[0]); //---------------------------------------------------------------------------------------------- default: break; } return(EOF); }
//为了提高任务效率,改用事件驱动方式 // void app_task_obd (void *p_arg) { u8 var_uchar; //Request Supported PIDs from Vehicle, check sae j1979 2002.pdf page 26 u8 determine_pid[]="\x02\x01\x00\x00\x00\x00\x00\x00"; u16 can_id_idx = 0; u32 can_id ; CanRxMsg can_rx_msg; CPU_INT08U os_err; (void)p_arg; //can_enquire_determine_pid( ); OSSemPend(sem_obd_task, 0, &os_err);//Wait task manger ask to start //BKP_DR2, OBD Flag: 15~12:KWP_TYPEDEF // 11~8:CAN2_TYPEDEF // 7~4: CAN1_TYPEDEF // 3~0: OBD_TYPEDEF //BKP_DR3, can_snd_id index, check app_obd.c switch ( obd_read_type ) { case OBD_TYPE_CAN1 ://CAN1 debug_obd("OBD is CAN1 "); can_id_idx = obd_read_canid_idx ; if ( !can_id_idx ) {//no CAN send id index obd_auto_detect( ); can_id_idx = obd_read_canid_idx ; } switch ( obd_can1_type ) { case CAN1_TYPE_STD_250 ://CAN1_TYPE_STD_250 debug_obd("STD 250\r\n"); break; case CAN1_TYPE_EXT_250 ://CAN1_TYPE_EXT_250 debug_obd("EXT 250\r\n"); break; case CAN1_TYPE_STD_500 ://CAN1_TYPE_STD_500 debug_obd("STD 500\r\n"); break; case CAN1_TYPE_EXT_500 ://CAN1_TYPE_EXT_500 debug_obd("EXT 500\r\n"); break; case CAN1_TYPE_NO_DEFINE ://CAN1 no define default://maybe error, treat as no define debug_obd("but type ERR!\r\n"); BKP_WriteBackupRegister(BKP_DR2, 0);//clean all flag, prevent err BKP_WriteBackupRegister(BKP_DR3, 0);//clean all flag, prevent err obd_auto_detect( ); can_id_idx = obd_read_canid_idx ; break; } break; case OBD_TYPE_CAN2 ://CAN2 debug_obd("OBD is CAN2 bus\r\n"); if ( !obd_read_canid_idx ) {//no CAN send id index obd_auto_detect( ); can_id_idx = obd_read_canid_idx ; } break; case OBD_TYPE_NO_DFN ://no define default://maybe error, treat as no define debug_obd("No OBD type\r\n"); BKP_WriteBackupRegister(BKP_DR2, 0);//clean all flag, prevent err BKP_WriteBackupRegister(BKP_DR3, 0);//clean all flag, prevent err obd_auto_detect( ); can_id_idx = obd_read_canid_idx ; break; } while ( !can_id_idx ) { //no CAN ID index, err debug_obd("BK2: %04X BK3: %04X\r\n",BKP_ReadBackupRegister(BKP_DR2),BKP_ReadBackupRegister(BKP_DR3)); debug_obd("can_id_idx = %d\r\n", can_id_idx); OSSemPend(sem_obd_task, 0, &os_err); debug_obd("CAN ID index ERR\r\n"); //Report err to server for ( var_uchar = 0 ; var_uchar < MAX_WARN_MSG ; var_uchar++) { if ( !my_icar.warn[var_uchar].msg ) { //empty msg //unsigned int msg;//file name(1 Byte), msg(1 Byte), line(2 B) my_icar.warn[var_uchar].msg = (F_APP_OBD) << 24 ; my_icar.warn[var_uchar].msg |= ERR_CAN_ID_IDX << 16 ;//Error CAN ID index my_icar.warn[var_uchar].msg |= __LINE__ ; var_uchar = MAX_WARN_MSG ;//end the loop } } obd_auto_detect( ); can_id_idx = obd_read_canid_idx ; } //uart3_init( ); //to OBD, K-BUS while ( !can_enquire_support_pid( ) ) { //update support table ERR debug_obd("Get CAN PID ERR\r\n"); //Report err to server for ( var_uchar = 0 ; var_uchar < MAX_WARN_MSG ; var_uchar++) { if ( !my_icar.warn[var_uchar].msg ) { //empty msg //unsigned int msg;//file name(1 Byte), msg(1 Byte), line(2 B) my_icar.warn[var_uchar].msg = (F_APP_OBD) << 24 ; my_icar.warn[var_uchar].msg |= ERR_CAN_SUP_PID << 16 ;//Error get CAN support PID my_icar.warn[var_uchar].msg |= __LINE__ ; var_uchar = MAX_WARN_MSG ;//end the loop } } OSTimeDlyHMSM(0, 0, 30, 0);//wait 30s } for ( var_uchar = 0 ; var_uchar <= OBD_MAX_PID ; var_uchar++) { if ( can_read_pid(var_uchar) ) {//read PID error debug_obd("Read PID:%d ERR\r\n",var_uchar); } else { debug_obd("Read PID:%d OK\r\n",var_uchar); } } /* Enable Interrupt for receive FIFO 0 and FIFO overflow */ CAN_ITConfig(CAN1,CAN_IT_FMP0 | CAN_IT_FOV0, ENABLE); /* Enable Interrupt for receive FIFO 1 and FIFO overflow */ CAN_ITConfig(CAN1,CAN_IT_FMP1 | CAN_IT_FOV1, ENABLE); while ( 1 ) { //debug_obd("OBD task Pend...\r\n"); OSSemPend(sem_obd_task, 0, &os_err); if ( !os_err ) { //CAN TX flag, for test only if ( my_icar.obd.can_tx_cnt ) { //send CAD ID: 0x18DB33F1 can_send( can_snd_id[can_id_idx], DAT_FRAME, 8, determine_pid ); //CAN_Transmit(CAN1, &TxMessage); my_icar.obd.can_tx_cnt--; } //execute CMD from app_taskmanager switch ( my_icar.obd.cmd ) { case READ_PID ://update support table debug_obd("CMD is Read ECU pid:%d\r\n",my_icar.obd.pid); my_icar.obd.cmd = NO_CMD ; var_uchar = can_read_pid( my_icar.obd.pid );//result in my_icar.obd.rx_buf if ( var_uchar ) {//read PID error debug_obd("Read PID err: %d\r\n", var_uchar); } else {//get ECU data //debug_obd("Read PID OK: %d\r\n", var_uchar); switch ( my_icar.obd.pid ) { case OBD_PID_ENGINE_RPM: debug_obd("Read RPM OK.\r\n"); //debug_obd("Rec %d:",rec_len); //for ( var_uchar = 0 ; var_uchar < rec_len ; var_uchar++ ) { //printf(" %02X",my_icar.obd.rx_buf[var_uchar]); //} printf(", from ID:%08X\r\n", can_id); break; default://maybe error debug_obd("PID err:%d\r\n",my_icar.obd.pid); break; } } break; case SPEED ://engine speed debug_obd("CMD is get engine speed\r\n"); my_icar.obd.cmd = NO_CMD ; can_send( can_snd_id[can_id_idx], DAT_FRAME, 8, determine_pid ); break; case NO_CMD ://no define default://maybe error, treat as NO_CMD ;//debug_obd("No OBD CMD\r\n"); break; } //FIFO0 has data, max.: 3 datas if ( (CAN1->RF0R)&0x03 ) { CAN_Receive(CAN1,CAN_FIFO0, &can_rx_msg); if (can_rx_msg.IDE == CAN_ID_STD) { debug_obd("CAN FIFO_0 rec %d ID: %X\r\n", (CAN1->RF0R)&0x03,can_rx_msg.StdId); debug_obd("DLC %d :", can_rx_msg.DLC); //show the data for ( var_uchar = 0 ; var_uchar < can_rx_msg.DLC ; var_uchar++ ) { printf(" %02X",can_rx_msg.Data[var_uchar]); } printf("\r\n"); } else { debug_obd("CAN FIFO_0 rec %d ID: %X %s,%d\r\n", (CAN1->RF0R)&0x03,can_rx_msg.ExtId,\ __FILE__,__LINE__); debug_obd("DLC %d :", can_rx_msg.DLC); //show the data for ( var_uchar = 0 ; var_uchar < can_rx_msg.DLC ; var_uchar++ ) { printf(" %02X",can_rx_msg.Data[var_uchar]); } printf("\r\n"); } if ( (CAN1->RF0R)&0x03==0 ) { //no data CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE);//enable FIFO0 FMP int } else { //still has data OSSemPost( sem_obd_task );//2012/9/26 14:55:33 verify } } //FIFO1 receive unknow CAN ID, then report to server //FIFO0 has data, max.: 3 datas if ( (CAN1->RF1R)&0x03 ) { CAN_Receive(CAN1,CAN_FIFO1, &can_rx_msg); if (can_rx_msg.IDE == CAN_ID_STD) { //unknow CAN STD ID, report to server can_id = can_rx_msg.StdId ; } else { //unknow CAN EXT ID, report to server can_id = can_rx_msg.ExtId ; } obd_report_unknow_canid( can_id ); debug_obd("Unknow CAN ID: %X FIFO:%d\r\n",can_id,(CAN1->RF1R)&0x03); if ( (CAN1->RF1R)&0x03==0 ) { //no data CAN_ITConfig(CAN1,CAN_IT_FMP1, ENABLE);//enable FIFO1 FMP int } else { //still has data OSSemPost( sem_obd_task );//no verify } } } } }
/*! \fn static void receive_can_msg(void) * * \brief 接收CAN消息 * * \param 无 * * \exception 此函数只在接受中断中使用 * * \return 无 */ static void receive_can_msg(void) { uint8 i = 0; CanRxMsg stCanMsg = {0}; uint8 ucSrcId = 0, ucDestId = 0, ucSegNum = 0; do { /*! CAN接收消息 */ CAN_Receive(CAN1, CAN_FIFO0, &stCanMsg); LED2 = ~LED2; /*! 帧判断 */ if((CAN_Id_Extended != stCanMsg.IDE) \ || (CAN_RTR_Data != stCanMsg.RTR) || (1 > stCanMsg.DLC)) { break; } /*! 标识符 */ ucSrcId = (uint8)((stCanMsg.ExtId & SRC_ID_MASK)>>22); ucDestId = (uint8)((stCanMsg.ExtId & DEST_ID_MASK)>>16); ucSegNum = (uint8)((stCanMsg.ExtId & SEG_CODE_NUM_MASK)>>8); #if 1 if((1 != ucSrcId) || (0 != ucDestId)) { break; } #endif if(8 < stCanMsg.DLC) { stCanMsg.DLC = 8; } /*! 数据 */ if(FRAME_NONE_SEG == (stCanMsg.ExtId & SEG_CODE_FLAG_MASK)) { /*! 无分段,直接将数据复制到帧接收缓冲区 */ for(i=0; i<stCanMsg.DLC; i++) { s_aucCanSegMsgBuf[s_unCanSegMsgWriteIndex][i] = stCanMsg.Data[i]; } s_unCanSegMsgWriteIndex++; if(CAN_MSG_BUF_SIZE <= s_unCanSegMsgWriteIndex) { s_unCanSegMsgWriteIndex = 0; } } else if(FRAME_FIRST_SEG == (stCanMsg.ExtId & SEG_CODE_FLAG_MASK)) { if((0 != ucSegNum) || (8 != stCanMsg.DLC)) { break; } s_ucCanSegMsgCnt = 0; for(i=0; i<8; i++) { s_aucCanSegMsgBuf[s_unCanSegMsgWriteIndex][s_ucCanSegMsgCnt] = stCanMsg.Data[i]; s_ucCanSegMsgCnt++; } s_ucLastSegNum = ucSegNum; } else if(FRAME_MIDDLE_SEG == (stCanMsg.ExtId & SEG_CODE_FLAG_MASK)) { s_ucLastSegNum++; if((s_ucLastSegNum != ucSegNum) || (1 > ucSegNum) || (8 != stCanMsg.DLC)) { s_ucLastSegNum = 0xff; break; } for(i=0; i<8; i++) { s_aucCanSegMsgBuf[s_unCanSegMsgWriteIndex][s_ucCanSegMsgCnt] = stCanMsg.Data[i]; s_ucCanSegMsgCnt++; } s_ucLastSegNum = ucSegNum; } else if(FRAME_END_SEG == (stCanMsg.ExtId & SEG_CODE_FLAG_MASK)) { if((0 != ucSegNum) || (0xff == s_ucLastSegNum)) { break; } for(i=0; i<stCanMsg.DLC; i++) { s_aucCanSegMsgBuf[s_unCanSegMsgWriteIndex][s_ucCanSegMsgCnt] = stCanMsg.Data[i]; s_ucCanSegMsgCnt++; } s_unCanSegMsgWriteIndex++; if(CAN_MSG_BUF_SIZE <= s_unCanSegMsgWriteIndex) { s_unCanSegMsgWriteIndex = 0; } } else { break; } }while(0); }
int main(void) { CanTxMsg TxMessage; float X_offset =0.0f,Y_offset =0.0f,Z_offset =0.0f; float test_float=0.0f; uint8_t test_int=0; float GyX =0.0f, GyY =0.0f, GyZ =0.0f; float GyX_prev=0.0f,GyY_prev=0.0f,GyZ_prev=0.0f; uint16_t x_len=240; uint16_t y_len=320; uint16_t i=0; uint16_t buffer_screen[x_len][y_len]; /* For gyro receiving */ float receivedGyro1=0,receivedGyro1_prev=0; float receivedGyro2=0,receivedGyro2_prev=0; float receivedGyro3=0,receivedGyro3_prev=0; uint8_t *ptr = & receivedGyro1; // uint16_t *buf_ptr = &buffer_screen; float runner=-8.0; rectangular_t rect1; rectangular_t prev_rect; rectangular_t rect_screen; char lcd_text_main[100]; /* LCD Initialization */ lcd_init(); lcd_drawBackground(20,60,250); //lcd_drawBGPersimmon(20, 60, 250); /* LED Initialization */ LED_Initialization(); /* CAN Initialization */ CAN2_Config(); CAN2_NVIC_Config(); /* MEMS Initialization */ Demo_GyroConfig(); Delay_1us(10000); #define CALIBRATE_COUNT 1000 for (i=0;i<CALIBRATE_COUNT ;i++){ Demo_GyroReadAngRate (Buffer); X_offset+= Buffer[0]; Y_offset+= Buffer[1]; Z_offset+= Buffer[2]; } X_offset = X_offset/ (float)CALIBRATE_COUNT; Y_offset = Y_offset/ (float)CALIBRATE_COUNT; Z_offset = Z_offset/ (float)CALIBRATE_COUNT; rect_screen.xlen = x_len; rect_screen.ylen = y_len; rect_screen.xpos = 0; rect_screen.ypos = 0; #define NEEDLE_RADIUS 65 #define NEEDLE_BASE_WIDTH 14 #define NEEDLE_FRAME_THICKNESS 5 #define NEEDLE1_CENTER_X 80 #define NEEDLE1_CENTER_Y 100 #define NEEDLE2_CENTER_X 80 #define NEEDLE2_CENTER_Y 200 #define NEEDLE3_CENTER_X 80 #define NEEDLE3_CENTER_Y 300 /* Drawing Needle frame 1 */ LCD_SetLayer(LCD_BACKGROUND_LAYER); DrawThickCircle(NEEDLE1_CENTER_X ,NEEDLE1_CENTER_Y,NEEDLE_RADIUS+NEEDLE_FRAME_THICKNESS, 4,LCD_COLOR_BLACK,LCD_COLOR_WHITE-1); LCD_SetLayer(LCD_FOREGROUND_LAYER); DrawThickCircle(NEEDLE1_CENTER_X ,NEEDLE1_CENTER_Y,NEEDLE_BASE_WIDTH, 6,LCD_COLOR_BLACK,LCD_COLOR_WHITE-1); LCD_SetLayer(LCD_BACKGROUND_LAYER); LCD_SetColors(ASSEMBLE_RGB(20, 60, 250),LCD_COLOR_BLACK); LCD_DrawFullRect(NEEDLE1_CENTER_X- NEEDLE_RADIUS-NEEDLE_FRAME_THICKNESS*2,NEEDLE1_CENTER_Y+ NEEDLE_BASE_WIDTH+3,NEEDLE_RADIUS*2+NEEDLE_FRAME_THICKNESS*4,NEEDLE_RADIUS); LCD_SetLayer(LCD_FOREGROUND_LAYER); LCD_SetColors(LCD_COLOR_BLACK,LCD_COLOR_BLACK); LCD_DrawFullRect(NEEDLE1_CENTER_X- NEEDLE_RADIUS,NEEDLE1_CENTER_Y+ NEEDLE_BASE_WIDTH,NEEDLE_RADIUS*2,NEEDLE_FRAME_THICKNESS-1); /* Drawing Needle frame 2 */ LCD_SetLayer(LCD_BACKGROUND_LAYER); DrawThickCircle(NEEDLE2_CENTER_X ,NEEDLE2_CENTER_Y,NEEDLE_RADIUS+NEEDLE_FRAME_THICKNESS, 4,LCD_COLOR_BLACK,LCD_COLOR_WHITE-1); LCD_SetLayer(LCD_FOREGROUND_LAYER); DrawThickCircle(NEEDLE2_CENTER_X ,NEEDLE2_CENTER_Y,NEEDLE_BASE_WIDTH, 6,LCD_COLOR_BLACK,LCD_COLOR_WHITE-1); LCD_SetLayer(LCD_BACKGROUND_LAYER); LCD_SetColors(ASSEMBLE_RGB(20, 60, 250),LCD_COLOR_BLACK); LCD_DrawFullRect(NEEDLE2_CENTER_X- NEEDLE_RADIUS-NEEDLE_FRAME_THICKNESS*2,NEEDLE2_CENTER_Y+ NEEDLE_BASE_WIDTH+3,NEEDLE_RADIUS*2+NEEDLE_FRAME_THICKNESS*4,NEEDLE_RADIUS); LCD_SetLayer(LCD_FOREGROUND_LAYER); LCD_SetColors(LCD_COLOR_BLACK,LCD_COLOR_BLACK); LCD_DrawFullRect(NEEDLE2_CENTER_X- NEEDLE_RADIUS,NEEDLE2_CENTER_Y+ NEEDLE_BASE_WIDTH,NEEDLE_RADIUS*2,NEEDLE_FRAME_THICKNESS-1); /* Drawing Needle frame 2 */ LCD_SetLayer(LCD_BACKGROUND_LAYER); DrawThickCircle(NEEDLE3_CENTER_X ,NEEDLE3_CENTER_Y,NEEDLE_RADIUS+NEEDLE_FRAME_THICKNESS, 4,LCD_COLOR_BLACK,LCD_COLOR_WHITE-1); LCD_SetLayer(LCD_FOREGROUND_LAYER); DrawThickCircle(NEEDLE3_CENTER_X ,NEEDLE3_CENTER_Y,NEEDLE_BASE_WIDTH, 6,LCD_COLOR_BLACK,LCD_COLOR_WHITE-1); LCD_SetLayer(LCD_BACKGROUND_LAYER); LCD_SetColors(ASSEMBLE_RGB(20, 60, 250),LCD_COLOR_BLACK); LCD_DrawFullRect(NEEDLE3_CENTER_X- NEEDLE_RADIUS-NEEDLE_FRAME_THICKNESS*2,NEEDLE3_CENTER_Y+ NEEDLE_BASE_WIDTH+3,NEEDLE_RADIUS*2+NEEDLE_FRAME_THICKNESS*4,NEEDLE_RADIUS); LCD_SetLayer(LCD_FOREGROUND_LAYER); LCD_SetColors(LCD_COLOR_BLACK,LCD_COLOR_BLACK); LCD_DrawFullRect(NEEDLE3_CENTER_X- NEEDLE_RADIUS,NEEDLE3_CENTER_Y+ NEEDLE_BASE_WIDTH,NEEDLE_RADIUS*2,NEEDLE_FRAME_THICKNESS-1); /* Clear drawing buffer */ PadRectangular(&buffer_screen,x_len,y_len,LCD_COLOR_WHITE, &rect_screen); while(1) { board_ID = PIN_ID_Read(); LCD_SetColors(LCD_COLOR_BLACK,LCD_COLOR_WHITE-1); sprintf(lcd_text_main," CAN Demo ID:%d ",board_ID); LCD_DisplayStringLine(LINE(0), (uint8_t*)lcd_text_main); Demo_GyroReadAngRate (Buffer); //Delay_1us(1000); /* MEMS Filtering */ #define LP_ALPHA 0.1f GyX = GyX*(1.0f - LP_ALPHA) + (Buffer[0] - X_offset)*LP_ALPHA; GyY = GyY*(1.0f - LP_ALPHA) + (Buffer[1] - Y_offset)*LP_ALPHA; GyZ = GyZ*(1.0f - LP_ALPHA) + (Buffer[2] - Z_offset)*LP_ALPHA; if(GyX > 90.0f) GyX = 90.0f; if(GyX < -90.0f) GyX = -90.0f; if(GyY > 90.0f) GyY = 90.0f; if(GyY < -90.0f) GyY = -90.0f; if(GyZ > 90.0f) GyZ = 90.0f; if(GyZ < -90.0f) GyZ = -90.0f; /* Start drawing rectangular */ prev_rect = rect1; rect1.xlen = 25; rect1.ylen = 30; rect1.xpos = x_len/2+ (int16_t)(GyY)-10; rect1.ypos = y_len/2 + (int16_t)(GyX)-10; if(board_ID == 1){ MoveNeedle(LCD_BACKGROUND_LAYER,&buffer_screen,x_len,y_len,LCD_COLOR_GREEN,NEEDLE1_CENTER_X,NEEDLE1_CENTER_Y,-GyZ,-GyZ_prev,NEEDLE_RADIUS,NEEDLE_BASE_WIDTH); }else if(board_ID == 2){ MoveNeedle(LCD_BACKGROUND_LAYER,&buffer_screen,x_len,y_len,LCD_COLOR_GREEN,NEEDLE2_CENTER_X,NEEDLE2_CENTER_Y,-GyZ,-GyZ_prev,NEEDLE_RADIUS,NEEDLE_BASE_WIDTH); }else { MoveNeedle(LCD_BACKGROUND_LAYER,&buffer_screen,x_len,y_len,LCD_COLOR_GREEN,NEEDLE3_CENTER_X,NEEDLE3_CENTER_Y,-GyZ,-GyZ_prev,NEEDLE_RADIUS,NEEDLE_BASE_WIDTH); } CAN2_TransmitGyro(board_ID,GyZ); /* Received Data */ if( can2_rx_isr_flag ==1){ do{ if( can2_rx_isr_flag ==1){ can2RxMessage = CAN2_PassRXMessage(); can2_rx_isr_flag=0; }else{ CAN_Receive(CAN2, CAN_FIFO0, &can2RxMessage); } GPIO_ToggleBits(GPIOG,GPIO_Pin_14); ptr[0] = can2RxMessage.Data[0]; ptr[1] = can2RxMessage.Data[1]; ptr[2] = can2RxMessage.Data[2]; ptr[3] = can2RxMessage.Data[3]; if(( can2RxMessage.ExtId & 0x0000FFFF) == 1){ ptr = & receivedGyro1; ptr[0] = can2RxMessage.Data[0]; ptr[1] = can2RxMessage.Data[1]; ptr[2] = can2RxMessage.Data[2]; ptr[3] = can2RxMessage.Data[3]; MoveNeedle(LCD_BACKGROUND_LAYER,&buffer_screen,x_len,y_len,LCD_COLOR_RED,NEEDLE1_CENTER_X,NEEDLE1_CENTER_Y,-receivedGyro1,-receivedGyro1_prev,NEEDLE_RADIUS,NEEDLE_BASE_WIDTH); receivedGyro1_prev = receivedGyro1; }else if (( can2RxMessage.ExtId & 0x0000FFFF) == 2){ ptr = & receivedGyro2; ptr[0] = can2RxMessage.Data[0]; ptr[1] = can2RxMessage.Data[1]; ptr[2] = can2RxMessage.Data[2]; ptr[3] = can2RxMessage.Data[3]; MoveNeedle(LCD_BACKGROUND_LAYER,&buffer_screen,x_len,y_len,LCD_COLOR_RED,NEEDLE2_CENTER_X,NEEDLE2_CENTER_Y,-receivedGyro2,-receivedGyro2_prev,NEEDLE_RADIUS,NEEDLE_BASE_WIDTH); receivedGyro2_prev = receivedGyro2; }else if (( can2RxMessage.ExtId & 0x0000FFFF) == 3){ ptr = & receivedGyro3; ptr[0] = can2RxMessage.Data[0]; ptr[1] = can2RxMessage.Data[1]; ptr[2] = can2RxMessage.Data[2]; ptr[3] = can2RxMessage.Data[3]; MoveNeedle(LCD_BACKGROUND_LAYER,&buffer_screen,x_len,y_len,LCD_COLOR_RED,NEEDLE3_CENTER_X,NEEDLE3_CENTER_Y,-receivedGyro3,-receivedGyro3_prev,NEEDLE_RADIUS,NEEDLE_BASE_WIDTH); receivedGyro3_prev = receivedGyro3; } // LCD_SetColors(LCD_COLOR_BLACK,LCD_COLOR_WHITE-1); // sprintf(lcd_text_main," ID :%d ", can2RxMessage.StdId); // LCD_DisplayStringLine(LINE(1), (uint8_t*)lcd_text_main); // LCD_SetColors(LCD_COLOR_BLACK,LCD_COLOR_WHITE-1); // sprintf(lcd_text_main," Data :%f ", receivedGyro); // LCD_DisplayStringLine(LINE(2), (uint8_t*)lcd_text_main); }while(CAN_MessagePending(CAN2, CAN_FIFO0) > 0); } // { // uint8_t status=0; // while(CAN_TransmitStatus(CAN2, 0) != CAN_TxStatus_Ok ){ // status = CAN_TransmitStatus(CAN2, 0); // if(status == CAN_TxStatus_Failed){ // GPIO_ToggleBits(GPIOG,GPIO_Pin_14); // } // } // } // TxMessage.StdId = (uint32_t)board_ID; // TxMessage.RTR = CAN_RTR_DATA; // TxMessage.IDE = CAN_ID_STD; // TxMessage.DLC = 8; // TxMessage.Data[0] = 0x01; // TxMessage.Data[1] = 0x01; // TxMessage.Data[2] = 0x01; // TxMessage.Data[3] = 0x01; // TxMessage.Data[4] = 0x01; // TxMessage.Data[5] = 0x01; // TxMessage.Data[6] = 0x01; // TxMessage.Data[7] = 0x01; // CAN_Transmit(CAN2, &TxMessage); //CAN2_TransmitGyro(test_int++,test_float); test_float += 0.1f; GyX_prev = GyX; GyZ_prev = GyZ; GyY_prev = GyY; runner += 1.0f; /* Faster method */ //MoveAndUpdateRectangular(LCD_FOREGROUND_LAYER,&buffer_screen,x_len,y_len,LCD_COLOR_BLACK,&prev_rect, &rect1); /* Regular method */ // PadRectangular(&buffer_screen,x_len,y_len,LCD_COLOR_WHITE, &prev_rect); // PadRectangular(&buffer_screen,x_len,y_len,LCD_COLOR_BLACK, &rect1); //DrawBufferToScreen(LCD_FOREGROUND_LAYER,buf_ptr,0,0, x_len,y_len); } }
void CAN1_RX0_IRQHandler(void) { CanRxMsg can_rx_msg; CAN_Receive(CAN1, CAN_FIFO0, &can_rx_msg); // TODO: Process message }
/************************************************************************* CAN2_RX0_IRQHandler ���������������ǡ���������CAN���ݽ����ж� *************************************************************************/ void CAN2_RX0_IRQHandler(void) { CanRxMsg rx_message; if (CAN_GetITStatus(CAN2,CAN_IT_FMP0)!= RESET) { CAN_ClearITPendingBit(CAN2, CAN_IT_FMP0); CAN_Receive(CAN2, CAN_FIFO0, &rx_message); //�������������� if(rx_message.StdId == 0x401) { gyro_ok_flag = 1; temp_yaw_angle = (int32_t)(rx_message.Data[0]<<24)|(int32_t)(rx_message.Data[1]<<16) | (int32_t)(rx_message.Data[2]<<8) | (int32_t)(rx_message.Data[3]); last_yaw_angle = this_yaw_angle; this_yaw_angle = -((float)temp_yaw_angle*0.01); } //ң���� ��� ��̨ͨ�� if(rx_message.StdId == 0x402) { temp_yaw = (uint16_t)(rx_message.Data[0]<<8)|(uint16_t)(rx_message.Data[1]); temp_pitch = (uint16_t)(rx_message.Data[2]<<8)|(uint16_t)(rx_message.Data[3]); shooting_flag = (uint8_t)rx_message.Data[4]; mode_flag = (uint8_t)rx_message.Data[6];//S2 ���� //for mouse if(shooting_flag == 1) //cyq:��ǹ { if(ShootFlag == 1) { Motor_PWM_Set(MOTOR_NUM1,-1000); ShootFlag=0; } } else { if(ShootFlag == 0) { ShootFlag=1; } } if (mode_flag == 1) { target_pitch_angle += (temp_pitch - 1024)/66.0;//ң�� target_yaw_angle += (temp_yaw - 1024)/600.0 ;//cyq } else { target_pitch_angle -= (temp_pitch - 1024)/10.0;//cyq ������� target_yaw_angle += (temp_yaw - 1024)/10.0 ;//cyq:����µij��� } if(target_pitch_angle > pitch_max) { target_pitch_angle = pitch_max; } else if(target_pitch_angle < -pitch_max) { target_pitch_angle = -pitch_max; } } } }
void Can0_Interrupt(){ if (CAN_GetITStatus(CAN1,CAN_IT_FMP0)){ CAN_Receive(CAN1, CAN_FIFO0, &can_rx_flame); Can0::read((int)can_rx_flame.StdId,(int)can_rx_flame.DLC,can_rx_flame.Data); } }
/** * @brief Configures the CAN, transmit and receive by polling * @param None * @retval PASSED if the reception is well done, FAILED in other case */ TestStatus CAN_Polling(void) { CAN_InitTypeDef sCAN; CAN_TxMsgTypeDef TxMsg; CAN_RxMsgTypeDef RxMsg; uint32_t i = 0; /* Set the HCLK division factor = 1 for CAN1*/ CAN_BRGInit(MDR_CAN1,CAN_HCLKdiv1); /* CAN register init */ CAN_DeInit(MDR_CAN1); /* CAN cell init */ CAN_StructInit (&sCAN); sCAN.CAN_ROP = ENABLE; sCAN.CAN_SAP = ENABLE; sCAN.CAN_STM = ENABLE; sCAN.CAN_ROM = DISABLE; sCAN.CAN_PSEG = CAN_PSEG_Mul_2TQ; sCAN.CAN_SEG1 = CAN_SEG1_Mul_5TQ; sCAN.CAN_SEG2 = CAN_SEG2_Mul_5TQ; sCAN.CAN_SJW = CAN_SJW_Mul_4TQ; sCAN.CAN_SB = CAN_SB_3_SAMPLE; sCAN.CAN_BRP = 4; CAN_Init (MDR_CAN1,&sCAN); CAN_Cmd(MDR_CAN1, ENABLE); /* Disable all CAN1 interrupt */ CAN_ITConfig( MDR_CAN1, CAN_IT_GLBINTEN | CAN_IT_RXINTEN | CAN_IT_TXINTEN | CAN_IT_ERRINTEN | CAN_IT_ERROVERINTEN, DISABLE); /* Enable CAN1 interrupt from receive buffer */ CAN_RxITConfig( MDR_CAN1 ,(1<<rx_buf), ENABLE); /* Enable CAN1 interrupt from transmit buffer */ CAN_TxITConfig( MDR_CAN1 ,(1<<tx_buf), ENABLE); /* receive buffer enable */ CAN_Receive(MDR_CAN1, rx_buf, DISABLE); /* transmit */ TxMsg.IDE = CAN_ID_EXT; TxMsg.DLC = 0x08; TxMsg.PRIOR_0 = DISABLE; TxMsg.ID = 0x12345678; TxMsg.Data[1] = 0x01234567; TxMsg.Data[0] = 0x89ABCDEF; CAN_Transmit(MDR_CAN1, tx_buf, &TxMsg); i = 0; while(((CAN_GetStatus(MDR_CAN1) & CAN_STATUS_TX_READY) != RESET) && (i != 0xFFF)) { i++; } CAN_ITClearRxTxPendingBit(MDR_CAN1, tx_buf, CAN_STATUS_TX_READY); i = 0; while(((CAN_GetStatus(MDR_CAN1) & CAN_STATUS_RX_READY) == RESET) && (i != 0xFFF)) { i++; } /* receive */ CAN_GetRawReceivedData(MDR_CAN1, rx_buf, &RxMsg); CAN_ITClearRxTxPendingBit(MDR_CAN1, rx_buf, CAN_STATUS_RX_READY); CAN_Cmd(MDR_CAN1, DISABLE); if(RxMsg.Rx_Header.IDE != TxMsg.IDE) { return FAILED; } if(RxMsg.Rx_Header.DLC != TxMsg.DLC) { return FAILED; } if(RxMsg.Rx_Header.ID != TxMsg.ID) { return FAILED; } if(RxMsg.Data[1] != TxMsg.Data[1]) { return FAILED; } if(RxMsg.Data[0] != TxMsg.Data[0]) { return FAILED; } else { return PASSED; /* Test Passed */ } }