admin 管理员组

文章数量: 887021


2024年1月5日发(作者:mysql索引可以为空吗)

};static can_cfg_t can_cfg[CAN_DEV_CNT]={ [CAN_DEV1] = { .can_dev_id = CAN_DEV1, .fifo_no = CAN_Filter_FIFO0, .t_pin = { GPIO_Pin_12, RCC_APB2Periph_GPIOA, GPIOA }, // CAN1_TX PA12 .r_pin = { GPIO_Pin_11, RCC_APB2Periph_GPIOA, GPIOA }, // CAN1_RX PA11 .pin_map = 0, .can_rcc = RCC_APB1Periph_CAN1, .can_ch = CAN1, .filter_start = 0,//0~13 .irq = CAN1_RX0_IRQn, .it_flag = CAN_IT_FMP0, }, [CAN_DEV2] = { .can_dev_id = CAN_DEV2, .fifo_no = CAN_Filter_FIFO1, .t_pin = { GPIO_Pin_13, RCC_APB2Periph_GPIOB, GPIOB }, // CAN2_TX PB13 .r_pin = { GPIO_Pin_12, RCC_APB2Periph_GPIOB, GPIOB }, // CAN2_RX PB12 .pin_map = 0, .can_rcc = RCC_APB1Periph_CAN2, .can_ch = CAN2, .filter_start = CAN2_FILTER_S,//14~27 .irq = CAN2_RX1_IRQn, .it_flag = CAN_IT_FMP1, }};static can_tq_t *__get_tp(int br){ int i = 0; while(can_baud_array[i].can_baudrate != 0){ if(can_baud_array[i].can_baudrate == br){ return &can_baud_array[i]; } i++; } return NULL;}static void __can_enable(int can_dev, u32 val){ if(can_dev == CAN_DEV1){ if(val == CAN_BUS_ENABLE){ _debug(SYS_INFO, "CAN_1 bus enablen"); pp_ctrl_set(PIN_CAN1_STB,0);/*0 enable*/ }else{ pp_ctrl_set(PIN_CAN1_STB,1);/*1 disable*/ } }else if(can_dev == CAN_DEV2){ if(val == CAN_BUS_ENABLE){ _debug(SYS_INFO, "CAN_2 bus enablen"); pp_ctrl_set(PIN_CAN2_STB,0);/*0 enable*/ }else{ pp_ctrl_set(PIN_CAN2_STB,1);/*1 disable*/ } }}int can_dev_init(int can_dev, int can_mode, int brp){ GPIO_InitTypeDef GPIO_InitStructure; CAN_InitTypeDef CAN_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; can_cfg_t* pparam = &can_cfg[can_dev];

int p_can_open(int can_dev,int bps){ int ret = can_dev_init(can_dev,CAN_Mode_Normal,bps); if(ret == 0){ __can_enable(can_dev,CAN_BUS_ENABLE); } return ret;}void p_can_close(int can_dev){ NVIC_InitTypeDef NVIC_InitStructure = {0}; can_cfg_t* pparam = &can_cfg[can_dev]; __can_enable(can_dev,CAN_BUS_DISABLE); CAN_ITConfig(pparam->can_ch, pparam->it_flag, DISABLE); NVIC__IRQChannel = pparam->irq; NVIC__IRQChannelCmd = DISABLE;

NVIC_Init(&NVIC_InitStructure); CAN_DeInit(pparam->can_ch);}int p_can_read(int can_dev, can_msg_t* msg){ can_cfg_t* pparam = &can_cfg[can_dev]; CanRxMsg RxMessage; /*

判别是哪个接收邮箱有数据挂起 */ if (CAN_MessagePending(pparam->can_ch, CAN_FIFO0)) { CAN_Receive(pparam->can_ch, CAN_FIFO0, &RxMessage); } else if (CAN_MessagePending(pparam->can_ch, CAN_FIFO1)) { CAN_Receive(pparam->can_ch, CAN_FIFO1, &RxMessage); } else { _error("read error can msg"); return 0; } if(CAN_Id_Standard == ){ msg->id_type = CAN_ID_ST; msg->id = ; _debug(SYS_INFO, "CAN%d FIFO0 receive msg Std_id = 0x%x ...n", pparam->can_dev_id + 1, ); }else{ msg->id_type = CAN_ID_EX; msg->id = ; _debug(SYS_INFO, "CAN%d FIFO1 receive msg EXT_id = 0x%x ...n", pparam->can_dev_id + 1, ); } if(CAN_RTR_Data == ){ msg->id_frame = CAN_FR_DATA; }else{ msg->id_frame = CAN_FR_REMOTE; } msg->len = ; memcpy(msg->data,, ); return ;}int p_can_send(int can_dev, can_msg_t* msg){ can_cfg_t* pparam = &can_cfg[can_dev]; CanTxMsg TxMsg = {0}; if(msg->id_type == CAN_ID_ST){ = CAN_Id_Standard; = msg->id; }else{ = CAN_Id_Extended; = msg->id;

= msg->id; } if(msg->id_frame == CAN_FR_DATA){ = CAN_RTR_Data; }else{ = CAN_RTR_Remote;

} = msg->len; memcpy(,msg->data,); pparam->_s = CAN_TxStatus_NoMailBox; uint8_t tx_mbox = CAN_Transmit(pparam->can_ch, &TxMsg); if (tx_mbox == CAN_TxStatus_NoMailBox) { _debug(SYS_INFO, "CAN Transmit Status is No MailBoxn"); return -1; } pparam->_s = tx_mbox; return 0;}int p_can_send_ok(int can_dev){ can_cfg_t* pparam = &can_cfg[can_dev]; if(pparam->_s == CAN_TxStatus_NoMailBox){ return -1; } if(CAN_TransmitStatus(pparam->can_ch, pparam->_s) == CAN_TxStatus_Failed){ return 0; } return 1;}#define CAN_ID_STD_STD CAN_ID_ST#define CAN_ID_EXT_EXT CAN_ID_EX#define CAN_ID_STD_EXT 2#define CAN_ID_EXT_STD 3static int __genc_ids(int format,uint32_t *ids,uint32_t *pout){ switch (format) { case CAN_ID_STD_STD: pout[0] = (ids[0] << 21) | CAN_Id_Standard; pout[1] = (ids[1] << 21) | CAN_Id_Standard; break; case CAN_ID_STD_EXT: pout[0] = (ids[0] << 21) | CAN_Id_Standard; pout[1] = (ids[1] << 3) | CAN_Id_Extended; break; case CAN_ID_EXT_STD: pout[0] = (ids[0] << 3) | CAN_Id_Extended; pout[1] = (ids[1] << 21) | CAN_Id_Standard; break; case CAN_ID_EXT_EXT: pout[0] = (ids[0] << 3) | CAN_Id_Extended; pout[1] = (ids[1] << 3) | CAN_Id_Extended; break; default: { return -1; }

} return 0;}

}static int __p_can_set_filter_list(int filter_id, int fifo_no, int format, uint32_t id1, uint32_t id2){ CAN_FilterInitTypeDef CAN_FilterInitStructure = { 0 }; uint32_t in_ids[2] = {id1,id2}; uint32_t out_ids[2] = {0}; if(__genc_ids(format,in_ids,out_ids)){ _debug(SYS_INFO, "CAN geting id is failed n"); return -1; } _debug(SYS_INFO, "filter_Id = 0x%x 0x%x, filter_number = %d, CAN_Filter_FIFO = %dn", out_ids[0], out_ids[1], filter_id, fifo_no); CAN__FilterNumber = filter_id; CAN__FilterMode = CAN_FilterMode_IdList; CAN__FilterScale = CAN_FilterScale_32bit; CAN__FilterIdHigh = (out_ids[0] >> 16) & 0xffff; CAN__FilterIdLow = out_ids[0] & 0xffff; CAN__FilterMaskIdHigh = (out_ids[1] >> 16) & 0xffff; CAN__FilterMaskIdLow = out_ids[1] & 0xffff; CAN__FilterFIFOAssignment = fifo_no; CAN__FilterActivation = ENABLE; CAN_FilterInit(&CAN_FilterInitStructure); return 0;}static int __p_can_set_filter_mask(int filter_id, int fifo_no, int format, uint32_t id, uint32_t mask){ CAN_FilterInitTypeDef CAN_FilterInitStructure = { 0 }; uint32_t in_ids[2] = {id,mask}; uint32_t out_ids[2] = {0}; if(__genc_ids(format,in_ids,out_ids)){ _debug(SYS_INFO, "CAN geting id is successed ...n"); return -1; } _debug(SYS_INFO, "------ filter_Id = 0x%x 0x%x, filter_number = %d, CAN_Filter_FIFO = %d ----------n", out_ids[0], out_ids[1], filter_id, fifo_no); CAN__FilterNumber = filter_id; CAN__FilterMode = CAN_FilterMode_IdMask; CAN__FilterScale = CAN_FilterScale_32bit; CAN__FilterIdHigh = (out_ids[0] >> 16) & 0xffff; CAN__FilterIdLow = out_ids[0] & 0xffff; CAN__FilterMaskIdHigh = (out_ids[1] >> 16) & 0xffff; CAN__FilterMaskIdLow = out_ids[1] & 0xffff; CAN__FilterFIFOAssignment = fifo_no; // CAN_Filter_FIFO0 CAN__FilterActivation = ENABLE; CAN_FilterInit(&CAN_FilterInitStructure); return 0;}/*@filter_mode FILTER_LIST FILTER_MASK@format CAN_ID_ST CAN_ID_EX*/int p_can_set_filter(int can_dev, int filter_mode, int format, uint32_t* ids, int id_cnt){ int i; can_cfg_t* pparam = &can_cfg[can_dev]; uint32_t r_ids[28] = {0}; CAN_SlaveStartBank(CAN2_FILTER_S);

if (id_cnt & 1) { //奇数 for(i = 0; i < id_cnt; i++){ r_ids[i] = ids[i]; } if(filter_mode == FILTER_LIST){

if(filter_mode == FILTER_LIST){ r_ids[i] = r_ids[i-1]; }else{ r_ids[i] = 0xffffffff; } id_cnt++; }else{ for(i = 0; i < id_cnt; i++){ r_ids[i] = ids[i]; } } if (filter_mode == FILTER_LIST) { for (i = 0; i < id_cnt / 2; i++) { __p_can_set_filter_list(pparam->filter_start + i, pparam->fifo_no, format, r_ids[2*i], r_ids[2*i + 1]); } } else { for (i = 0; i < id_cnt / 2; i++) { __p_can_set_filter_mask(pparam->filter_start + i, pparam->fifo_no, format, r_ids[2*i], r_ids[2*i + 1]); } } return 0;}(2)can.h

#ifndef __p_can_h__#define __p_can_h__#if 0 .word CAN2_TX_IRQHandler .word CAN2_RX0_IRQHandler .word CAN2_RX1_IRQHandler .word CAN2_SCE_IRQHandler#define CAN_Id_Standard ((uint32_t)0x00000000) /*!< Standard Id */#define CAN_Id_Extended ((uint32_t)0x00000004) /*!< Extended Id */#endiftypedef enum { CAN_DEV1, CAN_DEV2, CAN_DEV_CNT} CAN_DEV_E;#define CAN_ID_ST 0 //标准帧#define CAN_ID_EX 1 //扩展帧#define CAN_FR_DATA 0#define CAN_FR_REMOTE 1#define FILTER_LIST 0#define FILTER_MASK 1typedef struct _can_msg_t { uint8_t id_type; //0:标准帧 1:扩展帧 uint8_t id_frame; //0:数据帧 1:远程帧 uint8_t len; uint8_t res; uint32_t id; uint8_t data[8];} can_msg_t;int can_dev_init(int can_dev, int can_mode, int brp);int p_can_open(int can_dev,int bps);void p_can_close(int can_dev);int p_can_read(int can_dev,can_msg_t *msg);int p_can_send(int can_dev,can_msg_t *msg);/*@filter_mode FILTER_LIST FILTER_MASK@format CAN_ID_ST CAN_ID_EX*/int p_can_set_filter(int can_dev, int filter_mode, int format, uint32_t* ids, int id_cnt);void p_can_task_start(int can_dev);void p_can_task_stop(int can_dev);void p_can_task(void);#endif


本文标签: 邮箱 接收 数据 判别