void cpu_deinit_all() { //** ADC_DeInit(LPC_ADC); CAN_DeInit(LPC_CAN1); CAN_DeInit(LPC_CAN2); EMAC_DeInit(); EXTI_DeInit(); I2C_DeInit(LPC_I2C0); I2C_DeInit(LPC_I2C1); I2C_DeInit(LPC_I2C2); I2S_DeInit(LPC_I2S); NVIC_DeInit(); NVIC_SCBDeInit(); PWM_DeInit(LPC_PWM1); QEI_DeInit(LPC_QEI); //** RIT_DeInit(LPC_RIT); RTC_DeInit(LPC_RTC); SPI_DeInit(LPC_SPI); SSP_DeInit(LPC_SSP0); SSP_DeInit(LPC_SSP1); UART_DeInit(LPC_UART0); UART_DeInit((LPC_UART_TypeDef *) LPC_UART1); //** UART_DeInit(LPC_UART2); //** UART_DeInit(LPC_UART3); TIM_DeInit(LPC_TIM0); TIM_DeInit(LPC_TIM1); //** TIM_DeInit(LPC_TIM2); //** TIM_DeInit(LPC_TIM3); }
void CAN_CAN_Config(void) { CAN_InitTypeDef CAN_InitStructure; /* Enable CAN clock */ #if USE_CAN1_R0 || USE_CAN1_R1 RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1 , ENABLE); CAN_DeInit(CAN1); #endif #if USE_CAN2_R0 || USE_CAN2_R1 RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2 , ENABLE); CAN_DeInit(CAN2); #endif /* 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;//失能接收FIFO锁定模式,即FIFO溢出时新数据覆盖 CAN_InitStructure.CAN_TXFP = ENABLE; //使能发送FIFO优先级,报文顺序由发送顺序决定 DISABLE:ID决定 CAN_InitStructure.CAN_Mode = CAN_Mode_Normal; CAN_InitStructure.CAN_SJW = CAN_SJW_2tq; //BTR-SJW 重新同步跳跃宽度 1个时间单元 CAN_InitStructure.CAN_BS1 = CAN_BS1_3tq; //BTR-TS1 时间段1 占用了2个时间单元 CAN_InitStructure.CAN_BS2 = CAN_BS2_3tq; //BTR-TS1 时间段2 占用了3个时间单元 //总线波特率,波特率计算请参见 F4手册“24.7.7 Bit timing” #if CAN1_BAUDRATE == 1000 /* 1MBps */ CAN_InitStructure.CAN_Prescaler =6; //BTR-BRP 波特率分频器 定义了时间单元的时间长度 42/(1+3+3)/6=1Mbps #elif CAN1_BAUDRATE == 500 /* 500KBps */ CAN_InitStructure.CAN_Prescaler =12; #elif CAN1_BAUDRATE == 250 /* 250KBps */ CAN_InitStructure.CAN_Prescaler =24; #elif CAN1_BAUDRATE == 125 /* 125KBps */ CAN_InitStructure.CAN_Prescaler =48; #endif #if USE_CAN1_R0 || USE_CAN1_R1 CAN_Init(CAN1, &CAN_InitStructure); #endif #if USE_CAN2_R0 || USE_CAN2_R1 CAN_Init(CAN2, &CAN_InitStructure); #endif }
void CAN_config_init(void) { CAN_InitTypeDef CAN_InitStructure; CAN_FilterInitTypeDef CAN_FilterInitStructure; CAN_DeInit(CAN1); CAN_StructInit(&CAN_InitStructure); // 36M 250k速率 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_LoopBack; CAN_InitStructure.CAN_Mode=CAN_Mode_Normal; CAN_InitStructure.CAN_SJW=CAN_SJW_1tq; CAN_InitStructure.CAN_BS1=CAN_BS1_2tq; CAN_InitStructure.CAN_BS2=CAN_BS2_3tq; CAN_InitStructure.CAN_Prescaler=12; CAN_Init(CAN1, &CAN_InitStructure); CAN_OperatingModeRequest( CAN1, CAN_OperatingMode_Normal); 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); }
u32 platform_can_setup( unsigned id, u32 clock ) { LPC_CAN_TypeDef * canx; uint32_t div; switch (id) { case 0: canx = LPC_CAN1; break; case 1: canx = LPC_CAN2; break; default: return 0; } CAN_DeInit(canx); CAN_Init(canx, clock); CAN_ModeConfig(canx, CAN_OPERATING_MODE, ENABLE); CAN_IRQCmd(canx, CANINT_RIE, ENABLE); // Receive IRQ CAN_IRQCmd(canx, CANINT_EIE, ENABLE); // Error IRQ CAN_IRQCmd(canx, CANINT_BEIE, ENABLE); // Bus error IRQ LPC_CANAF->AFMR = 2; // Filter bypass (receive all messages) NVIC_EnableIRQ(CAN_IRQn); // Enable IRQs // Fix clock LPC_SC->PCLKSEL0 &= ~(3<<26 | 3<<28 | 3<<30); // PCLK / 2 LPC_SC->PCLKSEL0 |= (2<<26 | 2<<28 | 2<<30); div = (SystemCoreClock / 20) / clock; div --; canx->MOD = 0x01; // Enter reset mode canx->BTR = (div & 0x3FF) | (3<<14) | (5<<16) | (2<<20) ; // Set bit timing canx->MOD = 0; // Return to normal operating // Change pin function (for now using the MBED ones) // And read clock divider if (id == 0) { // Pin function LPC_PINCON->PINSEL0 &= ~(3<<0 | 3<<2); LPC_PINCON->PINSEL0 |= (1<<0 | 1<<2); // No pull up / pull down LPC_PINCON->PINMODE0 &= ~(3<<0 | 3<<2); LPC_PINCON->PINMODE0 |= (2<<0 | 2<<2); // NOT open drain LPC_PINCON->PINMODE_OD0 &= ~(1<<0 | 1<<1); } else { LPC_PINCON->PINSEL0 &= ~(3<<8 | 3<<10); LPC_PINCON->PINSEL0 |= (2<<8 | 2<<10); // No pull up / pull down LPC_PINCON->PINMODE0 &= ~(3<<8 | 3<<10); LPC_PINCON->PINMODE0 |= (2<<8 | 2<<10); // NOT open drain LPC_PINCON->PINMODE_OD0 &= ~(1<<4 | 1<<5); } return (SystemCoreClock / 20) / ((div & 0x3FF)+1); }
static void CAN_device_init_1(int baud) { CAN_DeInit(CAN1); /* CAN GPIOs configuration ************************************************* */ /* Enable GPIO clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); /* Connect CAN pins to Alternate Function */ GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_9); GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_9); initGPIO(GPIOB, GPIO_Pin_8 | GPIO_Pin_9); /* CAN configuration ******************************************************* */ /* Enable CAN clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE); initCAN(CAN1, baud); //set default filter CAN_device_set_filter(0, 0, 0, 0, 0); /* Enable FIFO 0 message pending Interrupt */ CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE); initCANInterrupts(CAN1, USB_LP_CAN1_RX0_IRQn); }
/** * Initialize the CAN driver and return an opaque id * @param[out] id the CAN interface handle * @param[in] cfg the configuration structure * @return 0 if successful, negative otherwise */ int32_t PIOS_CAN_Init(uintptr_t *can_id, const struct pios_can_cfg *cfg) { PIOS_DEBUG_Assert(can_id); PIOS_DEBUG_Assert(cfg); can_dev = (struct pios_can_dev *) PIOS_CAN_alloc(); if (!can_dev) goto out_fail; /* Bind the configuration to the device instance */ can_dev->cfg = cfg; /* Configure the CAN device */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE); /* Map pins to CAN function */ if (can_dev->cfg->remap) { if (can_dev->cfg->rx.gpio != 0) GPIO_PinAFConfig(can_dev->cfg->rx.gpio, can_dev->cfg->rx.pin_source, can_dev->cfg->remap); if (can_dev->cfg->tx.gpio != 0) GPIO_PinAFConfig(can_dev->cfg->tx.gpio, can_dev->cfg->tx.pin_source, can_dev->cfg->remap); } /* Initialize the CAN Rx and Tx pins */ if (can_dev->cfg->rx.gpio != 0) GPIO_Init(can_dev->cfg->rx.gpio, (GPIO_InitTypeDef *)&can_dev->cfg->rx.init); if (can_dev->cfg->tx.gpio != 0) GPIO_Init(can_dev->cfg->tx.gpio, (GPIO_InitTypeDef *)&can_dev->cfg->tx.init); *can_id = (uintptr_t)can_dev; CAN_DeInit(can_dev->cfg->regs); CAN_Init(can_dev->cfg->regs, (CAN_InitTypeDef *)&can_dev->cfg->init); /* CAN filter init */ CAN_FilterInitTypeDef CAN_FilterInitStructure; 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 = 1; CAN_FilterInitStructure.CAN_FilterActivation = ENABLE; CAN_FilterInit(&CAN_FilterInitStructure); // Enable the receiver IRQ NVIC_Init((NVIC_InitTypeDef*) &can_dev->cfg->rx_irq.init); NVIC_Init((NVIC_InitTypeDef*) &can_dev->cfg->tx_irq.init); return(0); out_fail: return(-1); }
void TSVN_CAN_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; CAN_InitTypeDef CAN_InitStructure; CAN_FilterInitTypeDef CAN_FilterInitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_PinRemapConfig(GPIO_Remap2_CAN1 , ENABLE); GPIO_PinRemapConfig(GPIO_Remap_PD01 , ENABLE); CAN_DeInit(CAN1); 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_Normal; CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; CAN_InitStructure.CAN_BS1 = CAN_BS1_3tq; //3 CAN_InitStructure.CAN_BS2 = CAN_BS2_5tq; //5 CAN_InitStructure.CAN_Prescaler = 4; 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); CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE); CAN_Init(CAN1, &CAN_InitStructure); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
void InitCAN(void) { // Setup CAN Pins RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = CAN_RX_PIN | CAN_TX_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); // Alternate functions GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_CAN1); GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_CAN1); // Setup CAN-modules RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE); /* CAN register init */ CAN_DeInit(CAN1); /* CAN cell init */ CAN_InitTypeDef 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 = DISABLE; CAN_InitStructure.CAN_Mode = CAN_Mode_Normal; CAN_InitStructure.CAN_SJW = CAN_SJW_3tq; /* CAN Baudrate = 500 KBps (CAN clocked at 42 MHz) */ CAN_InitStructure.CAN_BS1 = CAN_BS1_4tq; CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq; CAN_InitStructure.CAN_Prescaler = (42000000 / 7) / 500000; CAN_Init(CAN1, &CAN_InitStructure); /* CAN filter init */ CAN_FilterInitTypeDef CAN_FilterInitStructure; 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); /* Enable FIFO 0 message pending Interrupt */ CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE); }
void CAN_Configuration(void) { /* CAN1 Periph clock enable */ CAN_InitTypeDef CAN_InitStructure; CAN_FilterInitTypeDef CAN_FilterInitStructure; /* CAN register init */ CAN_DeInit(CAN1); CAN_DeInit(CAN2); 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_Normal; CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; CAN_InitStructure.CAN_BS1 = CAN_BS1_3tq; CAN_InitStructure.CAN_BS2 = CAN_BS2_5tq; CAN_InitStructure.CAN_Prescaler = 16;//2 4=1m 8=500k CAN_Init(CAN1, &CAN_InitStructure); CAN_Init(CAN2, &CAN_InitStructure); /* CAN filter init */ 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_FilterInitStructure.CAN_FilterNumber=0; CAN_FilterInit(&CAN_FilterInitStructure); CAN_FilterInitStructure.CAN_FilterNumber=14; CAN_FilterInit(&CAN_FilterInitStructure); }
/** * @file CAN_Configuration * @brief Configures the CAN * @param 无 * @retval 无 */ void CAN_Configuration(void) { CAN_InitTypeDef CAN_InitStructure; CAN_FilterInitTypeDef CAN_FilterInitStructure; GPIO_Configuration(); /* CAN register init */ CAN_DeInit(CAN1); CAN_StructInit(&CAN_InitStructure); /* CAN cell init */ CAN_InitStructure.CAN_TTCM = DISABLE; /* 时间触发禁止, 时间触发:CAN硬件的内部定时器被激活,并且被用于产生时间戳 */ CAN_InitStructure.CAN_ABOM = DISABLE; /* 自动离线禁止,自动离线:一旦硬件监控到128次11个隐性位,就自动退出离线状态。在这里要软件设定后才能退出 */ CAN_InitStructure.CAN_AWUM = DISABLE; /* 自动唤醒禁止,有报文来的时候自动退出休眠 */ CAN_InitStructure.CAN_NART = DISABLE; /* 报文重传, 如果错误一直传到成功止,否则只传一次 */ CAN_InitStructure.CAN_RFLM = DISABLE; /* 接收FIFO锁定, 1--锁定后接收到新的报文摘不要,0--接收到新的报文则覆盖前一报文 */ CAN_InitStructure.CAN_TXFP = ENABLE; /* 发送优先级 0---由标识符决定 1---由发送请求顺序决定 */ CAN_InitStructure.CAN_Mode = CAN_Mode_Normal; /* 模式 */ CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; /* 重新同步跳宽,只有can硬件处于初始化模式时才能访问这个寄存器 */ CAN_InitStructure.CAN_BS1 = CAN_BS1_4tq; /* 时间段1 */ CAN_InitStructure.CAN_BS2 = CAN_BS2_3tq; /* 时间段2 */ CAN_InitStructure.CAN_Prescaler = 45; /* 波特率预分频数 */ /* 波特率计算方法 */ /* CANbps= Fpclk/((BRP+1)*((Tseg1+1)+(Tseg2+1)+1) 此处计算为 CANbps=36000000/(45*(4+3+1))=100kHz */ //此处Tseg1+1 = CAN_BS1_8tp /* 配置大方向: Tseg1>=Tseg2 Tseg2>=tq; Tseg2>=2TSJW */ if (CAN_Init(CAN1,&CAN_InitStructure) == CANINITFAILED) { /* 初始化时先设置CAN_MCR的初始化位 */ /* 然后查看硬件是否真的设置了CAN_MSR的初始化位来确认是否进入了初始化模式 */ } /* 配置CAN过滤器 */ /* 32位对应的id */ /* stdid[10:0],extid[17:0],ide,rtr */ /* 16位对应的id */ /* stdid[10:0],ide,rtr,extid[17:15] */ /* 一般使用屏蔽模式 */ /* 要注意的是fifo接收存满了中断,还有就是fifo的概念,即取的一直是最早那一个数据, 要释放才能取下一个数据 */ /* 常使用的中断有 */ /* 1,有信息中断,即fifo挂号中断 */ /* 2,fifo满中断 */ /* 3,fifo满之后又有信息来则中断,即fifo溢出中断 */ CAN_FilterInitStructure.CAN_FilterNumber=0; /* 过滤器0 */ CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask; /* 屏敝模式 */ CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit; /* 32位 */ CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000; /* 以下四个都为0, 表明不过滤任何id */ CAN_FilterInitStructure.CAN_FilterIdLow=0x0000; CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000; CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000; CAN_FilterInitStructure.CAN_FilterFIFOAssignment=0; /* 能够通过该过滤器的报文存到fifo0中 */ CAN_FilterInitStructure.CAN_FilterActivation=ENABLE; CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE); /* 挂号中断, 进入中断后读fifo的报文函数释放报文清中断标志 */ CAN_FilterInit(&CAN_FilterInitStructure); }
void CAN_Open() { CAN_InitTypeDef CAN_InitStructure; CAN_FilterInitTypeDef CAN_FilterInitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN, ENABLE); // Configure CAN pin: RX GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOA, &GPIO_InitStructure); // Configure CAN pin: TX GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); /* CAN register init */ CAN_DeInit(); 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_NART=ENABLE; //no automatic retransmission CAN_InitStructure.CAN_RFLM=DISABLE; CAN_InitStructure.CAN_TXFP=DISABLE; CAN_InitStructure.CAN_Mode=CAN_Mode_Normal; //CAN block is clocked with Low Speed APB1 clock (PCLK1) = 36MHz CAN_InitStructure.CAN_SJW=CAN_SJW_1tq; CAN_InitStructure.CAN_BS1=CAN_BS1_9tq; //CAN_BS1_8tq @ 24mhz CAN_InitStructure.CAN_BS2=CAN_BS2_8tq; //CAN_BS2_7tq @24mhz CAN_InitStructure.CAN_Prescaler=4; //=3 500kbit@24mhz CAN_Init(&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); }
/** * \brief Call this method to make the CANTestcase driver configure pins, * set up the CAN peripheral et cetera. */ void CANTestcase::setUp() { // Enable CAN1 peripheral clock. RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1,ENABLE); // Enable GPIO clock of CAN_Tx and CAN_Rx pins. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); /* Configure CAN pin: RX */ GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure CAN pin: TX */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); // Configure CAN peripheral: CAN_InitTypeDef CAN_InitStructure; /* CAN register 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 = ENABLE; CAN_InitStructure.CAN_RFLM = DISABLE; CAN_InitStructure.CAN_TXFP = DISABLE; CAN_InitStructure.CAN_Mode = CAN_Mode_Normal; CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; CAN_InitStructure.CAN_BS1 = CAN_BS1_3tq; CAN_InitStructure.CAN_BS2 = CAN_BS2_5tq; CAN_InitStructure.CAN_Prescaler = 20; CAN_Init(CAN1, &CAN_InitStructure); /* CAN filter init */ CAN_FilterInitTypeDef CAN_FilterInitStructure; 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); }//eof
void CAN2_Configuration(void) { CAN_InitTypeDef can; CAN_FilterInitTypeDef can_filter; GPIO_InitTypeDef gpio; NVIC_InitTypeDef nvic; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2, ENABLE); GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_CAN2); GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_CAN2); gpio.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 ; gpio.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(GPIOB, &gpio); nvic.NVIC_IRQChannel = CAN2_RX0_IRQn; nvic.NVIC_IRQChannelPreemptionPriority = 1; nvic.NVIC_IRQChannelSubPriority = 0; nvic.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&nvic); CAN_DeInit(CAN2); CAN_StructInit(&can); can.CAN_TTCM = DISABLE; can.CAN_ABOM = DISABLE; can.CAN_AWUM = DISABLE; can.CAN_NART = DISABLE; can.CAN_RFLM = DISABLE; can.CAN_TXFP = ENABLE; can.CAN_Mode = CAN_Mode_Normal; can.CAN_SJW = CAN_SJW_1tq; can.CAN_BS1 = CAN_BS1_9tq; can.CAN_BS2 = CAN_BS2_4tq; can.CAN_Prescaler = 3; //CAN BaudRate 42/(1+9+4)/3=1Mbps CAN_Init(CAN2, &can); can_filter.CAN_FilterNumber=14; can_filter.CAN_FilterMode=CAN_FilterMode_IdMask; can_filter.CAN_FilterScale=CAN_FilterScale_32bit; can_filter.CAN_FilterIdHigh=0x0000; can_filter.CAN_FilterIdLow=0x0000; can_filter.CAN_FilterMaskIdHigh=0x0000; can_filter.CAN_FilterMaskIdLow=0x0000; can_filter.CAN_FilterFIFOAssignment=0;//the message which pass the filter save in fifo0 can_filter.CAN_FilterActivation=ENABLE; CAN_FilterInit(&can_filter); CAN_ITConfig(CAN2,CAN_IT_FMP0,ENABLE); }
void CANController_Init(void) { CAN_FilterInitTypeDef CAN_FilterInitStructure; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE); // Configure CAN pin: RX GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOA, &GPIO_InitStructure); // Configure CAN pin: TX GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); // Reset CAN1 CAN_DeInit(CAN1); // CAN filter init - all messages to FIFO0 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); NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 14; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStructure.NVIC_IRQChannel = USB_HP_CAN1_TX_IRQn; NVIC_Init(&NVIC_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn; NVIC_Init(&NVIC_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = CAN1_SCE_IRQn; NVIC_Init(&NVIC_InitStructure); // enable intertrupts, TODO: less magic values ! CAN1->IER = 0x00008F13; // enable all interrupts (except FIFOx full/overrun, sleep/wakeup) }
void EOBD_CAN_Configure(int baudrate) { CAN_InitTypeDef CAN_InitStructure; CAN_DeInit(CAN1); CAN_StructInit(&CAN_InitStructure); CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; CAN_InitStructure.CAN_BS1 = CAN_BS1_7tq; CAN_InitStructure.CAN_BS2 = CAN_BS2_6tq; CAN_InitStructure.CAN_Prescaler = scale250KB; // 1/((1/112M)*(bs1+bs2+1))*scale CAN_Init(CAN1,&CAN_InitStructure); }
void CAN_Initialise() { CAN_InitTypeDef CAN_InitStruct; CAN_FilterInitTypeDef CAN_FilterInitStructure; void GPIO_Initialise(); RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2,ENABLE); /* CAN register init */ CAN_DeInit(CAN2); /* CAN cell init */ CAN_InitStruct.CAN_TTCM = DISABLE; CAN_InitStruct.CAN_ABOM = DISABLE; CAN_InitStruct.CAN_AWUM = DISABLE; CAN_InitStruct.CAN_NART = DISABLE; CAN_InitStruct.CAN_RFLM = DISABLE; CAN_InitStruct.CAN_TXFP = DISABLE; CAN_InitStruct.CAN_Mode = CAN_Mode_Normal; CAN_InitStruct.CAN_SJW = CAN_SJW_3tq; //Bus length 3-5m. /* CAN Baudrate = 500 KBps (CAN clocked at 42 MHz) */ CAN_InitStruct.CAN_BS1 = CAN_BS1_4tq; CAN_InitStruct.CAN_BS2 = CAN_BS2_2tq; CAN_InitStruct.CAN_Prescaler = (42000000 / 7) / 500000; //12 CAN_Init(CAN2, &CAN_InitStruct); /* 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); /* Enable FIFO 0 message pending Interrupt */ CAN_ITConfig(CAN2, CAN_IT_FMP0, ENABLE); }
void canHWReinit() { CAN_Int.NVIC_IRQChannelCmd = DISABLE; CAN_Int.NVIC_IRQChannel = USB_HP_CAN1_TX_IRQn; NVIC_Init(&CAN_Int); CAN_Int.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn; NVIC_Init(&CAN_Int); CAN_Int.NVIC_IRQChannel = CAN1_SCE_IRQn; NVIC_Init(&CAN_Int); // Reset CAN1 - clears the error state CAN_DeInit(CAN1); canFilterApply(); // enable intertrupts //CAN1->IER = 0x00008F13; // enable all interrupts (except FIFOx full/overrun, sleep/wakeup) CAN1->IER = CAN_IER_TMEIE ;//| CAN_IER_FMPIE0; CAN1->IER |= CAN_IER_EWGIE | CAN_IER_EPVIE | CAN_IER_BOFIE | CAN_IER_LECIE; // CAN1->IER |= CAN_IER_ERRIE; // FIXME // enter the init mode CAN1->MCR &= ~CAN_MCR_SLEEP; CAN1->MCR |= CAN_MCR_INRQ; // wait for it ! while ((CAN1->MSR & CAN_MSR_INAK) != CAN_MSR_INAK); /* setup timing */ CAN1->BTR = (CAN_Timing->ts << 16) | CAN_Timing->brp; /* finish bxCAN setup */ CAN1->MCR |= CAN_MCR_ABOM; CAN1->MCR |= CAN_MCR_TXFP | CAN_MCR_RFLM | CAN_MCR_AWUM; // automatic wakeup, tx round-robin mode CAN1->MCR &= ~(CAN_MCR_SLEEP | 0x10000); // we don't support sleep, no debug-freeze CAN1->MCR &= ~CAN_MCR_INRQ; // leave init mode CAN_Int.NVIC_IRQChannelCmd = ENABLE; CAN_Int.NVIC_IRQChannel = USB_HP_CAN1_TX_IRQn; NVIC_Init(&CAN_Int); CAN_Int.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn; NVIC_Init(&CAN_Int); CAN_Int.NVIC_IRQChannel = CAN1_SCE_IRQn; NVIC_Init(&CAN_Int); }
/** * @brief Configures the CAN. * @param None * @retval : None */ void CAN_Config(void) { /* CAN register init */ CAN_DeInit(CAN1); 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_Normal; CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; CAN_InitStructure.CAN_BS1 = CAN_BS1_10tq; CAN_InitStructure.CAN_BS2 = CAN_BS2_7tq; CAN_InitStructure.CAN_Prescaler = 2; CAN_Init(CAN1, &CAN_InitStructure); /* CAN filter init */ /* 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 = 0x321; TxMessage.ExtId = 0x01; TxMessage.RTR = CAN_RTR_DATA; TxMessage.IDE = CAN_ID_STD; TxMessage.DLC = 1; }
/* * 函数名:CAN_Mode_Config * 描述 :CAN的模式 配置 * 输入 :无 * 输出 : 无 * 调用 :内部调用 */ static void CAN_Mode_Config(void) { CAN_InitTypeDef CAN_InitStructure; /************************CAN通信参数设置**********************************/ /*CAN寄存器初始化*/ CAN_DeInit(CAN1); CAN_StructInit(&CAN_InitStructure); /*CAN单元初始化*/ CAN_InitStructure.CAN_TTCM=DISABLE; //MCR-TTCM 时间触发通信模式使能 CAN_InitStructure.CAN_ABOM=DISABLE; //MCR-ABOM 自动离线管理 CAN_InitStructure.CAN_AWUM=DISABLE; //MCR-AWUM 自动唤醒模式 CAN_InitStructure.CAN_NART=DISABLE; //MCR-NART 禁止报文自动重传 DISABLE-自动重传 CAN_InitStructure.CAN_RFLM=DISABLE; //MCR-RFLM 接收FIFO 锁定模式 DISABLE-溢出时新报文会覆盖原有报文 CAN_InitStructure.CAN_TXFP=DISABLE; //MCR-TXFP 发送FIFO优先级 DISABLE-优先级取决于报文标示符 CAN_InitStructure.CAN_Mode = CAN_Mode_Normal; //正常发送模式 CAN_InitStructure.CAN_SJW=CAN_SJW_2tq; //BTR-SJW 重新同步跳跃宽度 2个时间单元 CAN_InitStructure.CAN_BS1=CAN_BS1_6tq; //BTR-TS1 时间段1 占用了6个时间单元 CAN_InitStructure.CAN_BS2=CAN_BS2_3tq; //BTR-TS1 时间段2 占用了3个时间单元 CAN_InitStructure.CAN_Prescaler =4; ////BTR-BRP 波特率分频器 定义了时间单元的时间长度 36/(1+6+3)/4=0.8Mbps CAN_Init(CAN1, &CAN_InitStructure); }
void CAN1_Configuration(void) { CAN_InitTypeDef CAN_InitStructure; CAN_FilterInitTypeDef CAN_FilterInitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1,ENABLE); //注意,挂在APB1 /************************模式与波特率设置*************/ 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=DISABLE; CAN_InitStructure.CAN_Mode = CAN_Mode_Normal; //CAN_Mode_LoopBack:回环模式,CAN_Mode_Normal:正常模式 CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; CAN_InitStructure.CAN_BS1 = CAN_BS1_5tq; CAN_InitStructure.CAN_BS2 = CAN_BS2_3tq; CAN_InitStructure.CAN_Prescaler = 4; //100K通信速度 CAN_Init(CAN1,&CAN_InitStructure); /***********************过滤器设置********************/ CAN_FilterInitStructure.CAN_FilterNumber = 0; //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 = CAN_FIFO0; //传入FIFO0 CAN_FilterInitStructure.CAN_FilterActivation = ENABLE; CAN_FilterInit(&CAN_FilterInitStructure); CAN_ITConfig(CAN1,CAN_IT_FMP0, ENABLE); //开启接收中断 }
static void CAN_Mode_Config(void) { CAN_InitTypeDef CAN_InitStructure; /* 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 }
static void CAN_device_init_2(int baud) { CAN_DeInit(CAN2); /* CAN GPIOs configuration ************************************************* */ /* Enable GPIO clock */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); /* Connect CAN pins to Alternate Function */ GPIO_PinAFConfig(GPIOB, GPIO_PinSource12, GPIO_AF_CAN2); GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_CAN2); initGPIO(GPIOB, GPIO_Pin_12 | GPIO_Pin_13); /* CAN configuration ******************************************************* */ /* Enable CAN clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2, ENABLE); initCAN(CAN2, baud); /* Enable FIFO 0 message pending Interrupt */ CAN_ITConfig(CAN2, CAN_IT_FMP1, ENABLE); initCANInterrupts(CAN2, CAN2_RX1_IRQn); }
/******************************************************************************* * Function Name : CAN_Initialize * Description : Configures the CAN, transmit and receive using interrupt. * Input : None * Output : None * Return : PASSED if the reception is well done, FAILED in other case *******************************************************************************/ _io *Initialize_CAN(int loop) { CAN_InitTypeDef CAN_InitStructure; CAN_FilterInitTypeDef CAN_FilterInitStructure; GPIO_InitTypeDef GPIO_InitStructure; GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_CAN2); GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_CAN2); RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE); // glej opis driverja, šmafu, treba inicializirat c RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2, ENABLE); CAN_StructInit(&CAN_InitStructure); CAN_DeInit(__CAN__); CAN_InitStructure.CAN_TTCM=DISABLE; CAN_InitStructure.CAN_ABOM=ENABLE; CAN_InitStructure.CAN_AWUM=DISABLE; CAN_InitStructure.CAN_NART=ENABLE; CAN_InitStructure.CAN_RFLM=DISABLE; //... pomembn.. da ne zamesa mailboxov in jih oddaja po vrstnem redu vpisovanja... ni default !!! CAN_InitStructure.CAN_TXFP=ENABLE; if(loop) CAN_InitStructure.CAN_Mode=CAN_Mode_LoopBack; else CAN_InitStructure.CAN_Mode=CAN_Mode_Normal; CAN_InitStructure.CAN_SJW=CAN_SJW_4tq; CAN_InitStructure.CAN_BS1=CAN_BS1_10tq; CAN_InitStructure.CAN_BS2=CAN_BS2_4tq; CAN_InitStructure.CAN_Prescaler=4; CAN_Init(__CAN__,&CAN_InitStructure); CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdList; CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit; CAN_FilterInitStructure.CAN_FilterMaskIdLow=0; CAN_FilterInitStructure.CAN_FilterIdLow=0; CAN_FilterInitStructure.CAN_FilterActivation=ENABLE; CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0; // filtri za PFM in EC CAN_FilterInitStructure.CAN_FilterIdHigh=_ID_SYS2PFM<<5; CAN_FilterInitStructure.CAN_FilterMaskIdHigh=_ID_SYS2EC<<5; CAN_FilterInitStructure.CAN_FilterNumber=__FILT_BASE__+0; CAN_FilterInit(&CAN_FilterInitStructure); CAN_FilterInitStructure.CAN_FilterIdHigh=_ID_SYS2PFMcom<<5; CAN_FilterInitStructure.CAN_FilterMaskIdHigh=_ID_PFMcom2SYS<<5; CAN_FilterInitStructure.CAN_FilterNumber=__FILT_BASE__+1; CAN_FilterInit(&CAN_FilterInitStructure); CAN_FilterInitStructure.CAN_FilterIdHigh=_ID_SYS_TRIGG<<5; CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0<<5; CAN_FilterInitStructure.CAN_FilterNumber=__FILT_BASE__+2; CAN_FilterInit(&CAN_FilterInitStructure); // filtri za IAP mode // CAN_FilterInitStructure.CAN_FilterIdHigh=_ID_IAP_GO<<5; // CAN_FilterInitStructure.CAN_FilterMaskIdHigh=_ID_IAP_ADDRESS<<5; // CAN_FilterInitStructure.CAN_FilterNumber=__FILT_BASE__+3; // CAN_FilterInit(&CAN_FilterInitStructure); // CAN_FilterInitStructure.CAN_FilterIdHigh=_ID_IAP_DWORD<<5; // CAN_FilterInitStructure.CAN_FilterMaskIdHigh=_ID_IAP_ERASE<<5; // CAN_FilterInitStructure.CAN_FilterNumber=__FILT_BASE__+4; // CAN_FilterInit(&CAN_FilterInitStructure); // CAN_FilterInitStructure.CAN_FilterIdHigh=_ID_IAP_ACK<<5; // CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0<<5; // CAN_FilterInitStructure.CAN_FilterNumber=__FILT_BASE__+5; // CAN_FilterInit(&CAN_FilterInitStructure); CAN_ITConfig(__CAN__, CAN_IT_FMP0, ENABLE); return(_io_init(100*sizeof(CanRxMsg),100*sizeof(CanTxMsg))); }
static int can_init(const can_cfg_t *cfg) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE); CAN_InitTypeDef CAN_InitStructure; CAN_FilterInitTypeDef CAN_FilterInitStructure; RCC_ClocksTypeDef RCC_Clocks; #ifdef CONFIG_CAN1_REMAP RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); /*RX = PB8, TX = PB9 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_PinRemapConfig(GPIO_Remap1_CAN1 , ENABLE); #else RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); /*RX = PA11 TX = PA12 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); #endif /* CAN register init */ CAN_DeInit(CAN1); CAN_StructInit(&CAN_InitStructure); //get apb clock RCC_GetClocksFreq(&RCC_Clocks); /* CAN cell init */ CAN_InitStructure.CAN_TTCM = DISABLE; CAN_InitStructure.CAN_ABOM = ENABLE; CAN_InitStructure.CAN_AWUM = DISABLE; CAN_InitStructure.CAN_NART = DISABLE; CAN_InitStructure.CAN_RFLM = DISABLE; CAN_InitStructure.CAN_TXFP = ENABLE; CAN_InitStructure.CAN_Mode = (cfg -> silent) ? CAN_Mode_Silent : CAN_Mode_Normal; CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; CAN_InitStructure.CAN_BS1 = CAN_BS1_3tq; CAN_InitStructure.CAN_BS2 = CAN_BS2_5tq; CAN_InitStructure.CAN_Prescaler = RCC_Clocks.PCLK1_Frequency/cfg->baud/(1+3+5); CAN_Init(CAN1, &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); #if ENABLE_CAN_INT NVIC_InitTypeDef NVIC_InitStructure; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); #ifndef STM32F10X_CL NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn; #else NVIC_InitStructure.NVIC_IRQChannel = CAN1_RX0_IRQn; #endif NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); CAN_ClearITPendingBit(CAN1, CAN_IT_FMP0); CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE); circle_header = 0; circle_tailer = 0; circle_number = 0; #endif can_flush(); return 0; }
/** * @brief Configures the CAN. * @param None * @retval None */ static void CAN_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; /* CAN GPIOs configuration **************************************************/ /* Enable GPIO clock */ RCC_AHB1PeriphClockCmd(CAN_GPIO_CLK, ENABLE); /* Connect CAN pins to AF9 */ GPIO_PinAFConfig(CAN_GPIO_PORT, CAN_RX_SOURCE, CAN_AF_PORT); GPIO_PinAFConfig(CAN_GPIO_PORT, CAN_TX_SOURCE, CAN_AF_PORT); /* Configure CAN RX and TX pins */ GPIO_InitStructure.GPIO_Pin = CAN_RX_PIN | CAN_TX_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(CAN_GPIO_PORT, &GPIO_InitStructure); /* CAN configuration ********************************************************/ /* Enable CAN clock */ RCC_APB1PeriphClockCmd(CAN_CLK, ENABLE); /* CAN register init */ CAN_DeInit(CANx); /* 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_Normal; CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; /* CAN Baudrate = 1 MBps (CAN clocked at 30 MHz) */ CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq; CAN_InitStructure.CAN_BS2 = CAN_BS2_8tq; CAN_InitStructure.CAN_Prescaler = 2; CAN_Init(CANx, &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 Structure preparation */ TxMessage.StdId = 0x321; TxMessage.ExtId = 0x01; TxMessage.RTR = CAN_RTR_DATA; TxMessage.IDE = CAN_ID_STD; TxMessage.DLC = 1; /* Enable FIFO 0 message pending Interrupt */ CAN_ITConfig(CANx, CAN_IT_FMP0, ENABLE); }
/** * @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 */ } }
/** * @brief Configures the CAN. * @param None * @retval None */ void CAN_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; /* GPIO clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); #ifdef __CAN1_USED__ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_CAN1, ENABLE); #else /*__CAN2_USED__*/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_CAN1, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_CAN2, ENABLE); #endif /* __CAN1_USED__ */ /* Configure CAN pin: RX */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_CAN_RX; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIO_CAN, &GPIO_InitStructure); /* Configure CAN pin: TX */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_CAN_TX; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIO_CAN, &GPIO_InitStructure); GPIO_PinRemapConfig(GPIO_Remapping_CAN , ENABLE); /* CANx Periph clock enable */ #ifdef __CAN1_USED__ RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE); #else /*__CAN2_USED__*/ RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN2, ENABLE); #endif /* __CAN1_USED__ */ /* CAN register init */ CAN_DeInit(CANx); 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_Normal; /* CAN Baudrate = 1MBps*/ CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; CAN_InitStructure.CAN_BS1 = CAN_BS1_3tq; CAN_InitStructure.CAN_BS2 = CAN_BS2_5tq; CAN_InitStructure.CAN_Prescaler = 4; CAN_Init(CANx, &CAN_InitStructure); /* CAN filter init */ #ifdef __CAN1_USED__ CAN_FilterInitStructure.CAN_FilterNumber = 0; #else /*__CAN2_USED__*/ CAN_FilterInitStructure.CAN_FilterNumber = 14; #endif /* __CAN1_USED__ */ 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 = 0x321; TxMessage.ExtId = 0x01; TxMessage.RTR = CAN_RTR_DATA; TxMessage.IDE = CAN_ID_STD; TxMessage.DLC = 1; }
/** * @brief Configures CAN1 and CAN2. * @param None * @retval None */ void CAN_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; /* Configure CAN1 and CAN2 IOs **********************************************/ /* GPIOB, GPIOD and AFIO clocks enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOB, ENABLE); /* Configure CAN1 RX pin */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOD, &GPIO_InitStructure); /* Configure CAN2 RX pin */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_Init(GPIOB, &GPIO_InitStructure); /* Configure CAN1 TX pin */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); /* Configure CAN2 TX pin */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_Init(GPIOB, &GPIO_InitStructure); /* Remap CAN1 and CAN2 GPIOs */ GPIO_PinRemapConfig(GPIO_Remap2_CAN1 , ENABLE); GPIO_PinRemapConfig(GPIO_Remap_CAN2, ENABLE); /* Configure CAN1 and CAN2 **************************************************/ /* CAN1 and CAN2 Periph clocks enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1 | RCC_APB1Periph_CAN2, ENABLE); /* CAN1 and CAN2 register init */ CAN_DeInit(CAN1); CAN_DeInit(CAN2); /* Struct init*/ CAN_StructInit(&CAN_InitStructure); /* CAN1 and CAN2 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 = ENABLE; CAN_InitStructure.CAN_Mode = CAN_Mode_Normal; CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; CAN_InitStructure.CAN_BS1 = CAN_BS1_3tq; CAN_InitStructure.CAN_BS2 = CAN_BS2_2tq; #if CAN_BAUDRATE == 1000 /* 1MBps */ CAN_InitStructure.CAN_Prescaler =6; #elif CAN_BAUDRATE == 500 /* 500KBps */ CAN_InitStructure.CAN_Prescaler =12; #elif CAN_BAUDRATE == 250 /* 250KBps */ CAN_InitStructure.CAN_Prescaler =24; #elif CAN_BAUDRATE == 125 /* 125KBps */ CAN_InitStructure.CAN_Prescaler =48; #elif CAN_BAUDRATE == 100 /* 100KBps */ CAN_InitStructure.CAN_Prescaler =60; #elif CAN_BAUDRATE == 50 /* 50KBps */ CAN_InitStructure.CAN_Prescaler =120; #elif CAN_BAUDRATE == 20 /* 20KBps */ CAN_InitStructure.CAN_Prescaler =300; #elif CAN_BAUDRATE == 10 /* 10KBps */ CAN_InitStructure.CAN_Prescaler =600; #else #error "Please select first the CAN Baudrate in Private defines in main.c " #endif /* CAN_BAUDRATE == 1000 */ /*Initializes the CAN1 and CAN2 */ CAN_Init(CAN1, &CAN_InitStructure); CAN_Init(CAN2, &CAN_InitStructure); /* CAN1 filter init */ CAN_FilterInitStructure.CAN_FilterNumber = 1; CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask; CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit; CAN_FilterInitStructure.CAN_FilterIdHigh = 0x6420; 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); /* CAN2 filter init */ CAN_FilterInitStructure.CAN_FilterIdHigh =0x2460; CAN_FilterInitStructure.CAN_FilterNumber = 15; CAN_FilterInit(&CAN_FilterInitStructure); /* Transmit */ TxMessage.StdId = 0x321; TxMessage.ExtId = 0x01; TxMessage.RTR = CAN_RTR_DATA; TxMessage.IDE = CAN_ID_STD; TxMessage.DLC = 1; }
u32 platform_can_setup( unsigned id, u32 clock ) { CAN_InitTypeDef CAN_InitStructure; CAN_FilterInitTypeDef CAN_FilterInitStructure; GPIO_InitTypeDef GPIO_InitStructure; int cbaudidx = -1; // Configure IO Pins -- This is for STM32F103RE GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init( GPIOB, &GPIO_InitStructure ); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init( GPIOB, &GPIO_InitStructure ); GPIO_PinRemapConfig( GPIO_Remap1_CAN1, ENABLE ); // Select baud rate up to requested rate, except for below min, where min is selected if ( clock >= can_baud_rate[ CAN_BAUD_COUNT - 1 ] ) // round down to peak rate if >= peak rate cbaudidx = CAN_BAUD_COUNT - 1; else { for( cbaudidx = 0; cbaudidx < CAN_BAUD_COUNT - 1; cbaudidx ++ ) { if( clock < can_baud_rate[ cbaudidx + 1 ] ) // take current idx if next is too large break; } } /* Deinitialize CAN Peripheral */ CAN_DeInit( CAN1 ); 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_Normal; CAN_InitStructure.CAN_SJW=can_baud_sjw[ cbaudidx ]; CAN_InitStructure.CAN_BS1=can_baud_bs1[ cbaudidx ]; CAN_InitStructure.CAN_BS2=can_baud_bs2[ cbaudidx ]; CAN_InitStructure.CAN_Prescaler=can_baud_pre[ cbaudidx ]; CAN_Init( CAN1, &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=CAN_FIFO0; CAN_FilterInitStructure.CAN_FilterActivation=ENABLE; CAN_FilterInit(&CAN_FilterInitStructure); return can_baud_rate[ cbaudidx ]; }
/* * 函数名: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 */ }