/** * Initialise a single USART device */ int32_t PIOS_USART_Init(uint32_t * usart_id, const struct pios_usart_cfg * cfg) { PIOS_DEBUG_Assert(usart_id); PIOS_DEBUG_Assert(cfg); struct pios_usart_dev * usart_dev; usart_dev = (struct pios_usart_dev *) PIOS_USART_alloc(); if (!usart_dev) goto out_fail; /* Bind the configuration to the device instance */ usart_dev->cfg = cfg; /* Clear buffer counters */ fifoBuf_init(&usart_dev->rx, usart_dev->rx_buffer, sizeof(usart_dev->rx_buffer)); fifoBuf_init(&usart_dev->tx, usart_dev->tx_buffer, sizeof(usart_dev->tx_buffer)); /* Enable the USART Pins Software Remapping */ if (usart_dev->cfg->remap) { GPIO_PinRemapConfig(usart_dev->cfg->remap, ENABLE); } /* Initialize the USART Rx and Tx pins */ GPIO_Init(usart_dev->cfg->rx.gpio, &usart_dev->cfg->rx.init); GPIO_Init(usart_dev->cfg->tx.gpio, &usart_dev->cfg->tx.init); /* Enable USART clock */ switch ((uint32_t)usart_dev->cfg->regs) { case (uint32_t)USART1: RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); break; case (uint32_t)USART2: RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); break; case (uint32_t)USART3: RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); break; } /* Configure the USART */ USART_Init(usart_dev->cfg->regs, &usart_dev->cfg->init); *usart_id = (uint32_t)usart_dev; /* Configure USART Interrupts */ NVIC_Init(&usart_dev->cfg->irq.init); USART_ITConfig(usart_dev->cfg->regs, USART_IT_RXNE, ENABLE); USART_ITConfig(usart_dev->cfg->regs, USART_IT_TXE, ENABLE); /* Enable USART */ USART_Cmd(usart_dev->cfg->regs, ENABLE); return(0); out_fail: return(-1); }
void systemInit(void) { GPIO_InitTypeDef GPIO_InitStructure; uint8_t i; gpio_config_t gpio_cfg[] = { { LED0_GPIO, LED0_PIN, GPIO_Mode_Out_PP }, // PB3 (LED) { LED1_GPIO, LED1_PIN, GPIO_Mode_Out_PP }, // PB4 (LED) #ifndef FY90Q { BEEP_GPIO, BEEP_PIN, GPIO_Mode_Out_OD }, // PA12 (Buzzer) #endif }; uint8_t gpio_count = sizeof(gpio_cfg) / sizeof(gpio_cfg[0]); // Turn on clocks for stuff we use RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3 | RCC_APB1Periph_TIM4 | RCC_APB1Periph_I2C2, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_TIM1 | RCC_APB2Periph_ADC1 | RCC_APB2Periph_USART1, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); RCC_ClearFlag(); // Make all GPIO in by default to save power and reduce noise GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_Init(GPIOC, &GPIO_InitStructure); // Turn off JTAG port 'cause we're using the GPIO for leds GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); // Configure gpio for (i = 0; i < gpio_count; i++) { GPIO_InitStructure.GPIO_Pin = gpio_cfg[i].pin; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = gpio_cfg[i].mode; GPIO_Init(gpio_cfg[i].gpio, &GPIO_InitStructure); } LED0_OFF; LED1_OFF; BEEP_OFF; // Init cycle counter cycleCounterInit(); // SysTick SysTick_Config(SystemCoreClock / 1000); // Configure the rest of the stuff adcInit(); #ifndef FY90Q i2cInit(I2C2); #endif // sleep for 100ms delay(100); }
//rcc-rcc-rcc-rcc-rcc-rcc-rcc-rcc-rcc-rcc-rcc// void UsartRcc_Configuration(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE); }
/** * @brief Configures the UART3/GPS periphral. * @param None * @retval None */ void GPS_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* Enable GPS GPIO Clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE); /* USART3 Remap Configuration */ GPIO_PinRemapConfig(GPIO_PartialRemap_USART3, ENABLE); /* Enable GPS USART3 Clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); /* Configure USART3 Tx as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOD, &GPIO_InitStructure); /* Configure USART3 Rx as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOD, &GPIO_InitStructure); /* Enable the USART3 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* USART3 configuration ------------------------------------------------------*/ /* USART3 configured as follow: - BaudRate = 9600 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; /* Configure USART3 */ USART_Init(USART3, &USART_InitStructure); /* Enable USART3 Receive and Transmit interrupts */ USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); /* Enable the USART3 */ USART_Cmd(USART3, ENABLE); }
//======================================== void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); GPIO_InitStructure.GPIO_Pin = LED_CLK_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(LED_CLK_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = LED_LAT_PIN; GPIO_Init(LED_LAT_PORT, &GPIO_InitStructure); /* #if DISPLAY_LED_STATIC GPIO_InitStructure.GPIO_Pin = LED_OE_PIN; GPIO_Init(LED_OE_PORT, &GPIO_InitStructure); #endif */ if(hwConfig.ledType == SCAN_STATIC) { GPIO_InitStructure.GPIO_Pin = LED_OE_PIN; GPIO_Init(LED_OE_PORT, &GPIO_InitStructure); } // Config PORTA pins GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7; GPIO_Init(GPIOA, &GPIO_InitStructure); // Config PORTB pins GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; GPIO_Init(GPIOB, &GPIO_InitStructure); // Config PORTB pins GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; GPIO_Init(GPIOC, &GPIO_InitStructure); // Config PORTB pins GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; GPIO_Init(GPIOD, &GPIO_InitStructure); // Config PORTB pins GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; GPIO_Init(GPIOE, &GPIO_InitStructure); // Set default value for control pins GPIO_Write(GPIOA,0x0000); GPIO_Write(GPIOB,0x0000); GPIO_Write(GPIOC,0x0000); GPIO_Write(GPIOD,0x0000); GPIO_Write(GPIOE,0x0000); // Configure USART1 Rx as input floating GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); // Configure USART1 Tx as alternate function push-pull GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); }
void I2C_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; I2C_InitTypeDef I2C_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); GPIO_PinRemapConfig(GPIO_Remap_I2C1, ENABLE); GPIO_InitStructure.GPIO_Pin = I2C_SCL_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_ResetBits(GPIOB, I2C_SCL_PIN); I2CDelay(SystemCoreClock / 3 / 100000); GPIO_InitStructure.GPIO_Pin = I2C_SDA_PIN; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_ResetBits(GPIOB, I2C_SDA_PIN); I2CDelay(SystemCoreClock / 3 / 100000); GPIO_SetBits(GPIOB, I2C_SCL_PIN); I2CDelay(SystemCoreClock / 3 / 100000); GPIO_SetBits(GPIOB, I2C_SDA_PIN); I2CDelay(SystemCoreClock / 3 / 100000); GPIO_InitStructure.GPIO_Pin = I2C_SCL_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = I2C_SDA_PIN; GPIO_Init(GPIOB, &GPIO_InitStructure); I2C_DeInit(I2C); I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = I2C_SLAVE_ADDR; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = I2C_SPEED; I2C_Init(I2C, &I2C_InitStructure); I2C_Cmd(I2C, ENABLE); if (I2C->SR2 & 0x02) //检测I2C { while (1) //DEBUG { } } }
/** * @brief Initializes the SD_SPI and CS pins. * @param None * @retval None */ void SD_LowLevel_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; SPI_InitTypeDef SPI_InitStructure; /*!< SD_SPI_CS_GPIO, SD_SPI_MOSI_GPIO, SD_SPI_MISO_GPIO, SD_SPI_DETECT_GPIO and SD_SPI_SCK_GPIO Periph clock enable */ RCC_APB2PeriphClockCmd(SD_CS_GPIO_CLK | SD_SPI_MOSI_GPIO_CLK | SD_SPI_MISO_GPIO_CLK | SD_SPI_SCK_GPIO_CLK | SD_DETECT_GPIO_CLK, ENABLE); /*!< SD_SPI Periph clock enable */ RCC_APB1PeriphClockCmd(SD_SPI_CLK, ENABLE); /*!< AFIO Periph clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); /*!< Remap SPI3 Pins */ GPIO_PinRemapConfig(GPIO_Remap_SPI3,ENABLE); /*!< Configure SD_SPI pins: SCK */ GPIO_InitStructure.GPIO_Pin = SD_SPI_SCK_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(SD_SPI_SCK_GPIO_PORT, &GPIO_InitStructure); /*!< Configure SD_SPI pins: MOSI */ GPIO_InitStructure.GPIO_Pin = SD_SPI_MOSI_PIN; GPIO_Init(SD_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure); /*!< Configure SD_SPI pins: MISO */ GPIO_InitStructure.GPIO_Pin = SD_SPI_MISO_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(SD_SPI_MISO_GPIO_PORT, &GPIO_InitStructure); /*!< Configure SD_SPI_CS_PIN pin: SD Card CS pin */ GPIO_InitStructure.GPIO_Pin = SD_CS_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(SD_CS_GPIO_PORT, &GPIO_InitStructure); /*!< Configure SD_SPI_DETECT_PIN pin: SD Card detect pin */ GPIO_InitStructure.GPIO_Pin = SD_DETECT_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(SD_DETECT_GPIO_PORT, &GPIO_InitStructure); /*!< SD_SPI Config */ SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SD_SPI, &SPI_InitStructure); SPI_Cmd(SD_SPI, ENABLE); /*!< SD_SPI enable */ }
void Tim1_Configuration(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; u16 CCR1_Val = 75; //前面的电机 u16 CCR2_Val = 75; //后面的电机 u16 CCR3_Val = 75; //左面的电机 u16 CCR4_Val = 75; //右面的电机 RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE); GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE); TIM_DeInit(TIM1); //重设为缺省值 /*TIM1时钟配置*/ TIM_TimeBaseStructure.TIM_Prescaler = 6; //预分频(时钟分频)72M/4000=18K TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数 TIM_TimeBaseStructure.TIM_Period = 999; //装载值 18k/144=125hz 就是说向上加的144便满了 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置了时钟分割 不懂得不管 TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0; //周期计数器值 不懂得不管 TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure); //初始化TIMx的时间基数单位 /* Channel 1 Configuration in PWM mode 通道一的PWM */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //PWM模式2 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //正向通道有效 PA8 //TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //反向通道也有效 PB13 TIM_OCInitStructure.TIM_Pulse = CCR1_Val; //占空时间 144 中有40的时间为高,互补的输出正好相反 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性 //TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low; //互补端的极性 TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset; //空闲状态下的非工作状态 不管 //TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; //先不管 TIM_OC1Init(TIM1,&TIM_OCInitStructure); //数初始化外设TIMx通道1这里2.0库为TIM_OCInit /* PWM1 Mode configuration: Channel2 */ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR2_Val; //设置通道2的电平跳变值,输出另外一个占空比的PWM TIM_OC2Init(TIM1, &TIM_OCInitStructure); //使能通道2 TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable); /* PWM1 Mode configuration: Channel3 */ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR3_Val; //设置通道3的电平跳变值,输出另外一个占空比的PWM TIM_OC3Init(TIM1, &TIM_OCInitStructure); //使能通道3 TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable); /* PWM1 Mode configuration: Channel4 */ TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR4_Val; //设置通道4的电平跳变值,输出另外一个占空比的PWM TIM_OC4Init(TIM1, &TIM_OCInitStructure); //使能通道4 TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable); /* TIM1 counter enable开定时器 */ TIM_Cmd(TIM1,ENABLE); /* TIM1 Main Output Enable 使能TIM1外设的主输出*/ TIM_CtrlPWMOutputs(TIM1,ENABLE); }
/*Init GPIOE (9,14,13) for PWM and GPIOC(6-8) for input*/ void modCounter_init_GPIO(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = 0;// очищаем после предыдущего GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//+5 TOLERANT?!!!!!!!!!!!!!!!!!!1 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); ///////////////////// /////////////////////////////////////////////////////// GPIO_InitStructure.GPIO_Pin = 0;// очищаем после предыдущего GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_14 | GPIO_Pin_13| GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOE, &GPIO_InitStructure); GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE); /////////////////////////////////////////////// GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE); GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE); GPIO_PinRemapConfig(GPIO_Remap_TIM4, ENABLE); }
void ledInit(){ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); GPIO_InitTypeDef gpioInit; gpioInit.GPIO_Pin = ledPins[0] | ledPins[1] | ledPins[2] | ledPins[3]; gpioInit.GPIO_Mode = GPIO_Mode_Out_PP; gpioInit.GPIO_Speed = GPIO_Speed_2MHz; GPIO_Init(GPIOB, &gpioInit); }
/** * @brief 配置指定SPI的引脚 * @param SPIx 需要使用的SPI * @retval None */ static void SPI_GPIO_Configuration(SPI_TypeDef* SPIx , u8 Remap ) { GPIO_InitTypeDef GPIO_InitStruct; if(Remap==0) //SPI没有重映射 { if(SPIx==SPI1){ GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6|GPIO_Pin_7; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStruct); //初始化片选输出引脚 GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_SetBits(GPIOA,GPIO_Pin_4); }else{ GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14|GPIO_Pin_15; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOB, &GPIO_InitStruct); //初始化片选输出引脚 GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStruct); GPIO_SetBits(GPIOB,GPIO_Pin_12); } } if(Remap==1) //SPI有重映射 { GPIO_PinRemapConfig(GPIO_Remap_SPI1, ENABLE); //SPI1重映射使能 if(SPIx==SPI1){ GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4|GPIO_Pin_5; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOB, &GPIO_InitStruct); //初始化片选输出引脚 GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_SetBits(GPIOA,GPIO_Pin_15); }else{ GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14|GPIO_Pin_15; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOB, &GPIO_InitStruct); //初始化片选输出引脚 GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStruct); GPIO_SetBits(GPIOB,GPIO_Pin_12); } } }
void HAL_USART_Begin(HAL_USART_Serial serial, uint32_t baud) { // AFIO clock enable RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); // Enable USART Clock *usartMap[serial]->usart_apbReg |= usartMap[serial]->usart_clock_en; NVIC_InitTypeDef NVIC_InitStructure; // Enable the USART Interrupt NVIC_InitStructure.NVIC_IRQChannel = usartMap[serial]->usart_int_n; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 7;//USART2_IRQ_PRIORITY; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); // Configure USART Rx as input floating HAL_Pin_Mode(usartMap[serial]->usart_rx_pin, INPUT); // Configure USART Tx as alternate function push-pull HAL_Pin_Mode(usartMap[serial]->usart_tx_pin, AF_OUTPUT_PUSHPULL); // Remap USARTn to alternate pins EG. USART1 to pins TX/PB6, RX/PB7 GPIO_PinRemapConfig(usartMap[serial]->usart_pin_remap, ENABLE); // USART default configuration // USART configured as follow: // - BaudRate = (set baudRate as 9600 baud) // - Word Length = 8 Bits // - One Stop Bit // - No parity // - Hardware flow control disabled (RTS and CTS signals) // - Receive and transmit enabled USART_InitStructure.USART_BaudRate = baud; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // Configure USART USART_Init(usartMap[serial]->usart_peripheral, &USART_InitStructure); // Enable USART Receive and Transmit interrupts USART_ITConfig(usartMap[serial]->usart_peripheral, USART_IT_RXNE, ENABLE); USART_ITConfig(usartMap[serial]->usart_peripheral, USART_IT_TXE, ENABLE); // Enable the USART USART_Cmd(usartMap[serial]->usart_peripheral, ENABLE); usartMap[serial]->usart_enabled = true; usartMap[serial]->usart_transmitting = false; }
void Bsp_Init(void) { RCC_Configuration(); /*********************************************************************************************/ // GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE); //初始化LED灯 led_init_com(); led_init_io(); //初始化串口 UART1_NVIC_Configuration(); UART1_Configuration(); UART1_GPIO_Configuration(); UART2_NVIC_Configuration(); UART2_Configuration(); UART2_GPIO_Configuration(); //清除标志位,否则第一位会丢失 USART_ClearFlag(USART1,USART_FLAG_TC); USART_ClearFlag(USART2,USART_FLAG_TC); //初始化输入输出接口 Io_Init(); //初始化E13_TLL模块 /*** E13_Io_Init(); //配置E13_TLL模块 if (E13_Configuration()==1) //E13模块配置失效,闪烁发送和接受LED灯,进入死循环 { while(1) { Delay_Ms(25); led_on_com_r(); led_on_com_t(); Delay_Ms(25); led_off_r(); led_off_t(); } } */////////////// led_on_in_3(); //点亮IN3作为电源指示灯 //初始化flash data_error =Read_Flash(0x0801fc00); communication_over_time =Read_Flash(0x0801f800); communication_miss =Read_Flash(0x0801f400); power_up=Read_Flash(0x0801f000); //Write_Flash(0x0801fc00,0); //Write_Flash(0x0801f800,0); }
static void RCC_Configuration(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); #ifdef RT_USING_UART1 /* Enable USART1 and GPIOA clocks */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE); #endif #ifdef RT_USING_UART2 #if (defined(STM32F10X_LD) || defined(STM32F10X_MD)) //|| defined(STM32F10X_CL))--2011del /* Enable AFIO and GPIOD clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOD, ENABLE); /* Enable the USART2 Pins Software Remapping */ GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE); #else /* Enable AFIO and GPIOA clock */ // RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA, ENABLE); GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOD, ENABLE); #endif /* Enable USART2 clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); #endif #ifdef RT_USING_UART3 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); /* Enable USART3 clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); /* DMA clock enable */ //RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); #endif #ifdef RT_USING_UART4 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); #endif }
/** * @brief Configures the different GPIO ports. * @param None * @retval None */ void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; #ifdef USE_STM3210E_EVAL /* Disable the JTAG interface and enable the SWJ interface This operation is not necessary for Connectivity-Line devices since SPI3 I/Os can be remapped on other GPIO pins */ GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); #endif /* USE_STM3210E_EVAL */ /* Configure SPI2 pins: CK, WS and SD ---------------------------------*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_15; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); #ifdef USE_STM3210C_EVAL /* Remap SPI3 on PC10-PC11-PC12-PA4 GPIO pins ------------------------*/ GPIO_PinRemapConfig(GPIO_Remap_SPI3, ENABLE); /* Configure SPI3 pins: CK and SD ------------------------------------*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_12; GPIO_Init(GPIOC, &GPIO_InitStructure); /* Configure SPI3 pins: WS -------------------------------------------*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_Init(GPIOA, &GPIO_InitStructure); #elif defined (USE_STM3210E_EVAL) /* Configure SPI3 pins: CK and SD ------------------------------------*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_5; GPIO_Init(GPIOB, &GPIO_InitStructure); /* Configure SPI3 pins: WS -------------------------------------------*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; GPIO_Init(GPIOA, &GPIO_InitStructure); #endif /* USE_STM3210C_EVAL */ }
static void rccInit(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); }
static int uart_Init(const uart_cfg_t *cfg) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef uartinfo; RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); #ifdef CONFIG_STM32F10X_CL /*configure PD5<uart2.tx>, PD6<uart2.rx>*/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE); GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOD, &GPIO_InitStructure); #else /*configure PA2<uart2.tx>, PA3<uart2.rx>*/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); #endif /*init serial port*/ USART_StructInit(&uartinfo); uartinfo.USART_BaudRate = cfg->baud; USART_Init(uart, &uartinfo); #ifdef ENABLE_TX_DMA RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); uart_SetupTxDMA(uart_fifo_tx, 0); USART_DMACmd(uart, USART_DMAReq_Tx, ENABLE); uart_fifo_tn = 0; uart_fifo_tp = 0; #endif #ifdef ENABLE_RX_DMA RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); uart_SetupRxDMA(uart_fifo_rx, RX_FIFO_SZ); USART_DMACmd(uart, USART_DMAReq_Rx, ENABLE); uart_fifo_rn = 0; #endif USART_Cmd(uart, ENABLE); return 0; }
/* --------------------------------------------------------------------------- ** Initialize the HW modules needed for I2C communication ** -------------------------------------------------------------------------*/ void I2CInit(void) { I2C_InitTypeDef I2C_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; #ifdef I2C_REMAP RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB,ENABLE); #else /*!< I2C_SCL_GPIO_CLK and I2C_SDA_GPIO_CLK Periph clock enable */ RCC_APB2PeriphClockCmd(RCC_APB1Periph_I2C1 | RCC_APB2Periph_GPIOB, ENABLE); #endif /*!< I2C Periph clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); /*!< GPIO configuration */ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; #ifdef I2C_REMAP GPIO_PinRemapConfig(GPIO_Remap_I2C1, ENABLE); /*!< Configure I2C pins: SCL -- PB8, SDA -- PB9*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; #else /*!< Configure I2C pins: SCK -- PB6, SDA -- PB7*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; #endif GPIO_Init(GPIOB, &GPIO_InitStructure); // #endif /*!< I2C configuration */ I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = I2C_SPEED; /* I2C Peripheral Enable */ I2C_Cmd(I2C1, ENABLE); /* Apply I2C configuration after enabling it */ I2C_Init(I2C1, &I2C_InitStructure); }
void uart3_init( void ) { /* init RCC */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); /* Enable USART3 interrupts */ NVIC_InitTypeDef nvic; nvic.NVIC_IRQChannel = USART3_IRQn; nvic.NVIC_IRQChannelPreemptionPriority = 2; nvic.NVIC_IRQChannelSubPriority = 1; nvic.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&nvic); /* Init GPIOS */ GPIO_PinRemapConfig(GPIO_PartialRemap_USART3, ENABLE); GPIO_InitTypeDef gpio; /* GPIOC: GPIO_Pin_10 USART3 Tx push-pull */ gpio.GPIO_Pin = UART3_TxPin; gpio.GPIO_Mode = GPIO_Mode_AF_PP; gpio.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(UART3_TxPort, &gpio); /* GPIOC: GPIO_Pin_11 USART3 Rx pin as floating input */ gpio.GPIO_Pin = UART3_RxPin; gpio.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(UART3_RxPort, &gpio); /* Configure USART3 */ USART_InitTypeDef usart; usart.USART_BaudRate = UART3_BAUD; usart.USART_WordLength = USART_WordLength_8b; usart.USART_StopBits = USART_StopBits_1; usart.USART_Parity = USART_Parity_No; usart.USART_HardwareFlowControl = USART_HardwareFlowControl_None; usart.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART3, &usart); /* Enable USART3 Receive interrupts */ USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); pprz_usart_set_baudrate(USART3, UART3_BAUD); /* Enable the USART3 */ USART_Cmd(USART3, ENABLE); // initialize the transmit data queue uart3_tx_extract_idx = 0; uart3_tx_insert_idx = 0; uart3_tx_running = FALSE; // initialize the receive data queuenn uart3_rx_extract_idx = 0; uart3_rx_insert_idx = 0; }
/* The core is running at 72MHz */ int main(void) { GPIO_InitTypeDef GPIO_InitStructure; /* Set the Vector Table base adress at 0x8004000 */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x4000); RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA , ENABLE); /* Full SWJ Disabled (JTAG-DP + SW-DP) */ GPIO_PinRemapConfig (GPIO_Remap_SWJ_Disable, ENABLE); //LED0 -> PB0, LED1 -> PB1 // Reset GPIO init structure GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); // Configure PA1 as input floating // Reset GPIO init structure GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//GPIO_Mode_IPD; GPIO_Init(GPIOA, &GPIO_InitStructure); USART1_Init(); ADC_Configuration(); while (1) { /* LED0-ON LED1-OFF */ GPIO_SetBits(GPIOB , GPIO_Pin_0); GPIO_ResetBits(GPIOB , GPIO_Pin_1); Delay(0xfffff); /* LED0-OFF LED1-ON */ GPIO_ResetBits(GPIOB , GPIO_Pin_0); GPIO_SetBits(GPIOB , GPIO_Pin_1); Delay(0xfffff); /* Print the ADC PA1 DIP Switch value to USART */ USART1_Print_Int(readDIPSwitch()); if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1) == 1) USART1_Print(", ON\n"); else USART1_Print(", OFF\n"); } }
/* * 函数名:TIM3_PWM_In_Init * 描述 :定时器3输入捕获初始化函数 * 输入 :arr:自动重装载寄存器周期的值;psc:时钟频率除数的预分频值 * 输出 :无 */ void TIM3_PWM_In_Init(u16 arr,u16 psc) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM3_ICInitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能GPIOB时钟 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //使能TIM3时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_ResetBits(GPIOB,GPIO_Pin_4); GPIO_ResetBits(GPIOB,GPIO_Pin_5); //初始化TIM3 TIM_TimeBaseStructure.TIM_Period = arr; //设定计数器自动重装值 TIM_TimeBaseStructure.TIM_Prescaler =psc; //预分频器 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //初始化TIMx的时间基数单位 //初始化TIM3通道1输入捕获参数 TIM3_ICInitStructure.TIM_Channel = TIM_Channel_1 ; //选择输入端 IC1映射到TI1上 TIM3_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获 TIM3_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上 TIM3_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频 TIM3_ICInitStructure.TIM_ICFilter = 0x00; //配置输入滤波器,不滤波 TIM_ICInit(TIM3, &TIM3_ICInitStructure); //初始化TIM3通道2输入捕获参数 TIM3_ICInitStructure.TIM_Channel = TIM_Channel_2 ; //选择输入端 IC1映射到TI1上 TIM3_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获 TIM3_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上 TIM3_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频 TIM3_ICInitStructure.TIM_ICFilter = 0x00; //配置输入滤波器 不滤波 TIM_ICInit(TIM3, &TIM3_ICInitStructure); //中断分组初始化 NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn ; //TIM3中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //先占优先级 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //从优先级 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能 NVIC_Init(&NVIC_InitStructure); //初始化外设NVIC寄存器 TIM_ITConfig(TIM3,TIM_IT_Update | TIM_IT_CC1 | TIM_IT_CC2,ENABLE); //允许更新中断 ,允许CC1IE捕获中断 TIM_Cmd(TIM3,ENABLE ); //使能定时器3 }
void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_PinRemapConfig(GPIO_Remap_I2C1,ENABLE); /* Configure I2C_EE pins: SCL and SDA */ GPIO_InitStructure.GPIO_Pin = I2C_EE_SCL | I2C_EE_SDA; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_Init(I2C_EE_GPIO, &GPIO_InitStructure); }
void uartInit(void) { USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* Enable GPIO and USART clock */ RCC_APB2PeriphClockCmd(UART_GPIO_PERIF, ENABLE); #if defined PLATFORM_REV_DEXMO_A_RIGHT RCC_APB1PeriphClockCmd(UART_PERIF, ENABLE); #elif defined PLATFORM_REV_DEXMO_A_LEFT || defined PLATFORM_REV_DEXMO_DONGLE_A RCC_APB2PeriphClockCmd(UART_PERIF, ENABLE); #endif /* Configure USART Rx as input floating */ GPIO_InitStructure.GPIO_Pin = UART_GPIO_RX; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(UART_GPIO_PORT, &GPIO_InitStructure); /* Configure USART Tx as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = UART_GPIO_TX; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(UART_GPIO_PORT, &GPIO_InitStructure); #ifdef PLATFORM_REV_DEXMO_A_RIGHT GPIO_PinRemapConfig(GPIO_PartialRemap_USART3, ENABLE); #endif USART_InitStructure.USART_BaudRate = 230400; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No ; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_Init(UART_TYPE, &USART_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = UART_NVIC_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_ITConfig(UART_TYPE, USART_IT_RXNE, ENABLE); USART_Cmd(UART_TYPE, ENABLE); // uartDmaInit(); DEBUG_PRINT("init successfully\n"); }
void USART3_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; /* Clock configuration -------------------------------------------------------*/ /* Configure the GPIO ports( USART3 Transmit and Receive Lines) */ /* Configure the USART3_Tx as Alternate function Push-Pull */ /* Configure USART3_Tx as alternate function push-pull */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); GPIO_PinRemapConfig(GPIO_FullRemap_USART3,ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOD, &GPIO_InitStructure); /* Configure USART3_Rx as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOD, &GPIO_InitStructure); /* USART3 configuration ------------------------------------------------------*/ /* USART3 configured as follow: - BaudRate = 9600 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; /* Configure the USART3 */ USART_Init(USART3, &USART_InitStructure); /* Enable USART3 interrupt */ USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); /* Enable the USART3 */ USART_Cmd(USART3, ENABLE); }
//核心组件初始化,包括串口(用于打印调试信息) static void coreInit() { NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); SystemCoreClockUpdate(); SysTick_Init(); LED_Config(); USART_Config(Debug_USART, Debug_BaudRate); //enable remap for max6675 pins RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); }
void eBoxInit(void) { SysTick_Config(SysTickOverFlowValue);// 每隔 (nhz/9,000,000)s产生一次中断 SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);//9Mhz的systemticks clock; Init_ADC1(); NVIC_PriorityGroupConfig(NVIC_GROUP_CONFIG); //将pb4默认设置为IO口,仅用jtag RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE); }
/** * @brief Configure the IO pins used by the I2C device. * @param Device : I2C Device instance. * @retval None. */ void CPAL_I2C_HAL_GPIOInit(CPAL_DevTypeDef Device) { GPIO_InitTypeDef GPIO_InitStructure; /* Enable I2Cx SCL and SDA Pin Clock */ __I2C_GPIO_CLK_CMD((CPAL_I2C_SCL_GPIO_CLK[Device] | CPAL_I2C_SDA_GPIO_CLK[Device]), ENABLE); /* Enable Pin Remap if PB8 (SCL) and PB9 (SDA) is used for I2C1 */ if ((Device == CPAL_I2C1) && (CPAL_I2C1_SCL_GPIO_PIN == GPIO_Pin_8) && (CPAL_I2C1_SDA_GPIO_PIN == GPIO_Pin_9)) { /* Enable GPIO Alternative Functions */ __AFIO_CLK_CMD(ENABLE); /* Enable I2C1 pin Remap */ GPIO_PinRemapConfig(GPIO_Remap_I2C1, ENABLE); } /* Set GPIO frequency to 50MHz */ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /* Select Output open-drain mode */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; /* Initialize I2Cx SCL Pin */ GPIO_InitStructure.GPIO_Pin = CPAL_I2C_SCL_GPIO_PIN[Device]; GPIO_Init((GPIO_TypeDef*)CPAL_I2C_SCL_GPIO_PORT[Device], &GPIO_InitStructure); /* Initialize I2Cx SDA Pin */ GPIO_InitStructure.GPIO_Pin = CPAL_I2C_SDA_GPIO_PIN[Device]; GPIO_Init((GPIO_TypeDef*)CPAL_I2C_SDA_GPIO_PORT[Device], &GPIO_InitStructure); i2cdevUnlockBus((GPIO_TypeDef*)CPAL_I2C_SCL_GPIO_PORT[Device], (GPIO_TypeDef*)CPAL_I2C_SDA_GPIO_PORT[Device], CPAL_I2C_SCL_GPIO_PIN[Device], CPAL_I2C_SDA_GPIO_PIN[Device]); /* Select Output open-drain mode */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; /* Initialize I2Cx SCL Pin */ GPIO_InitStructure.GPIO_Pin = CPAL_I2C_SCL_GPIO_PIN[Device]; GPIO_Init((GPIO_TypeDef*)CPAL_I2C_SCL_GPIO_PORT[Device], &GPIO_InitStructure); /* Initialize I2Cx SDA Pin */ GPIO_InitStructure.GPIO_Pin = CPAL_I2C_SDA_GPIO_PIN[Device]; GPIO_Init((GPIO_TypeDef*)CPAL_I2C_SDA_GPIO_PORT[Device], &GPIO_InitStructure); }
void init_SPI1() { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO | RCC_APB2Periph_SPI1, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; SPI_InitTypeDef SPI_InitStructure; SPI_StructInit(&SPI_InitStructure); //NSS вход выбор чипа GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA , &GPIO_InitStructure); //CLK GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); //MISO GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); //MOSI GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_PinRemapConfig(GPIO_Remap_SPI1, ENABLE); //Заполняем структуру с параметрами SPI модуля SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //полный дуплекс SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; // передаем по 8 бит SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; // Полярность и SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; // фаза тактового сигнала SPI_InitStructure.SPI_NSS = SPI_NSS_Hard; // Управлять состоянием сигнала NSS аппаратно SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; // Предделитель SCK SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; // Первым отправляется старший бит SPI_InitStructure.SPI_Mode = SPI_Mode_Slave; // Режим - слейв SPI_Init(SPI1, &SPI_InitStructure); //Настраиваем SPI1 SPI_Cmd(SPI1, ENABLE); // Включаем модуль SPI1.... SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_RXNE, ENABLE); //Включаем прерывание по приему байта NVIC_EnableIRQ(SPI1_IRQn); //Разрешаем прерывания от SPI1 }
void systemInit(void) { GPIO_InitTypeDef GPIO_InitStructure; gpio_config_t gpio_cfg[] = { { LEDR_GPIO, LEDR_PIN, GPIO_Mode_Out_PP }, { LEDG_GPIO, LEDG_PIN, GPIO_Mode_Out_PP }, }; uint8_t gpio_count = sizeof(gpio_cfg) / sizeof(gpio_cfg[0]); uint32_t i; // This is needed because some shit inside Keil startup f***s with SystemCoreClock, setting it back to 72MHz even on HSI. SystemCoreClockUpdate(); // Turn on clocks for stuff we use RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3 | RCC_APB1Periph_TIM4 | RCC_APB1Periph_I2C2 | RCC_APB1Periph_SPI2, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_TIM1 | RCC_APB2Periph_ADC1 | RCC_APB2Periph_USART1, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); RCC_ClearFlag(); // Make all GPIO in by default to save power and reduce noise GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_Init(GPIOC, &GPIO_InitStructure); // Turn off JTAG port 'cause we're using the GPIO for leds GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE); // Configure gpio for (i = 0; i < gpio_count; i++) { GPIO_InitStructure.GPIO_Pin = gpio_cfg[i].pin; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Mode = gpio_cfg[i].mode; GPIO_Init(gpio_cfg[i].gpio, &GPIO_InitStructure); } LEDR_OFF LEDG_OFF // Init cycle counter cycleCounterInit(); // SysTick SysTick_Config(SystemCoreClock / 1000); // Configure the rest of the stuff i2cInit(I2C2); // sleep for 100ms delay(100); }
void TIM3_PWM_Init(u16 arr,u16 psc) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE); //使能GPIO外设和AFIO复用功能模块时钟使能 GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE); //Timer3全映射 GPIOC-> 6,7,8,9 //用于TIM3的CH2输出的PWM通过该LED显示 //设置该引脚为复用输出功能,输出TIM3 CH1 CH2 CH3 CH4 的PWM脉冲波形 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; //初始化GPIO GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值 不分频 TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式1 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能 TIM_OCInitStructure.TIM_Pulse = 0; //设置待装入捕获比较寄存器的脉冲值 TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高 TIM_OC1Init(TIM3, &TIM_OCInitStructure); //根据TIM_OCInitStruct中指定的参数初始化外设TIMx TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); //使能TIMx在CCR1上的预装载寄存器 TIM_OC2Init(TIM3, &TIM_OCInitStructure); //根据TIM_OCInitStruct中指定的参数初始化外设TIMx TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); //使能TIMx在CCR2上的预装载寄存器 TIM_OC3Init(TIM3, &TIM_OCInitStructure); //根据TIM_OCInitStruct中指定的参数初始化外设TIMx TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable); //使能TIMx在CCR3上的预装载寄存器 TIM_OC4Init(TIM3, &TIM_OCInitStructure); //根据TIM_OCInitStruct中指定的参数初始化外设TIMx TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable); //使能TIMx在CCR4上的预装载寄存器 TIM_ARRPreloadConfig(TIM3, ENABLE); //使能TIMx在ARR上的预装载寄存器 TIM_Cmd(TIM3, ENABLE); //使能TIMx外设 }