/** \brief Inicializa a I2C1 em PB8 e PB9 (SCL e SDA). * * @param I2Cx Escolha da I2C a ser inicializada. * */ void c_common_i2c_init(I2C_TypeDef* I2Cx){ if(I2Cx==I2C1) { GPIO_InitTypeDef GPIO_InitStruct; I2C_InitTypeDef I2C_InitStruct; // enable APB1 peripheral clock for I2C1 RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); // enable clock for SCL and SDA pins RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); /* setup SCL and SDA pins * You can connect I2C1 to two different * pairs of pins: * 1. SCL on PB6 and SDA on PB7 * 2. SCL on PB8 and SDA on PB9 <----------- */ GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_OType = GPIO_OType_OD; // set output to open drain --> the line has to be only pulled low, not driven high GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; // enable pull up resistors GPIO_Init(GPIOB, &GPIO_InitStruct); // init GPIOB // Connect I2C1 pins to AF GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_I2C1); // SCL GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1); // SDA // configure I2C1 I2C_InitStruct.I2C_ClockSpeed = 100000; // 100kHz I2C_InitStruct.I2C_Mode = I2C_Mode_I2C; // I2C mode I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2; // 50% duty cycle --> standard I2C_InitStruct.I2C_OwnAddress1 = 0x00; // own address, not relevant in master mode I2C_InitStruct.I2C_Ack = I2C_Ack_Disable; // disable acknowledge when reading (can be changed later on) I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; // set address length to 7 bit addresses I2C_Init(I2C1, &I2C_InitStruct); // init I2C1 // enable I2C1 I2C_Cmd(I2C1, ENABLE); } else if(I2Cx==I2C2) { GPIO_InitTypeDef GPIO_InitStruct; I2C_InitTypeDef I2C_InitStruct; // enable APB1 peripheral clock for I2C1 RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE); // enable clock for SCL and SDA pins RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE); /* setup SCL and SDA pins * You can connect I2C1 to two different * pairs of pins: * 1. SCL on PF1 and SDA on PF0 * 2. SCL on PF1 and SDA on PF0 <----------- */ GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_0; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_OType = GPIO_OType_OD; // set output to open drain --> the line has to be only pulled low, not driven high GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; // enable pull up resistors GPIO_Init(GPIOF, &GPIO_InitStruct); // init GPIOF // Connect I2C2 pins to AF GPIO_PinAFConfig(GPIOF, GPIO_PinSource1, GPIO_AF_I2C2); // SCL GPIO_PinAFConfig(GPIOF, GPIO_PinSource0, GPIO_AF_I2C2); // SDA // configure I2C2 I2C_InitStruct.I2C_ClockSpeed = 100000; // 100kHz I2C_InitStruct.I2C_Mode = I2C_Mode_I2C; // I2C mode I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2; // 50% duty cycle --> standard I2C_InitStruct.I2C_OwnAddress1 = 0x00; // own address, not relevant in master mode I2C_InitStruct.I2C_Ack = I2C_Ack_Disable; // disable acknowledge when reading (can be changed later on) I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; // set address length to 7 bit addresses I2C_Init(I2C2, &I2C_InitStruct); // init I2C1 // enable I2C2 I2C_Cmd(I2C2, ENABLE); } }
/* * Uart 3 initialisieren */ void Xbee::Init(void) { /* USARTx configured as follow: - BaudRate = 115200 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled */ GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitTypeDef GPIO_InitStructure2; USART_InitTypeDef USART_InitStructure; /* Enable GPIO clock */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); /* Enable UART clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); /* Connect PXx to USARTx_Tx*/ GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); /* Connect PXx to USARTx_Rx*/ GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2); /* Configure USART Tx as alternate function */ GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure USART Rx as alternate function */ GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_3; GPIO_Init(GPIOA, &GPIO_InitStructure2); 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; /* USART configuration */ USART_Init(USART2, &USART_InitStructure); /* Enable USART */ USART_Cmd(USART2, ENABLE); USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
/** * @brief Initializes the hardware resources (I2C and GPIO) used to configure * the OV2640 camera. * @param None * @retval None */ void OV2640_HW_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; I2C_InitTypeDef I2C_InitStruct; /*** Configures the DCMI GPIOs to interface with the OV2640 camera module ***/ /* Enable DCMI GPIOs clocks */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOH | RCC_AHB1Periph_GPIOI, ENABLE); /* Connect DCMI pins to AF13 */ GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_DCMI); GPIO_PinAFConfig(GPIOH, GPIO_PinSource8, GPIO_AF_DCMI); GPIO_PinAFConfig(GPIOH, GPIO_PinSource9, GPIO_AF_DCMI); GPIO_PinAFConfig(GPIOH, GPIO_PinSource10, GPIO_AF_DCMI); GPIO_PinAFConfig(GPIOH, GPIO_PinSource11, GPIO_AF_DCMI); GPIO_PinAFConfig(GPIOH, GPIO_PinSource12, GPIO_AF_DCMI); GPIO_PinAFConfig(GPIOH, GPIO_PinSource14, GPIO_AF_DCMI); GPIO_PinAFConfig(GPIOI, GPIO_PinSource5, GPIO_AF_DCMI); GPIO_PinAFConfig(GPIOI, GPIO_PinSource6, GPIO_AF_DCMI); GPIO_PinAFConfig(GPIOI, GPIO_PinSource7, GPIO_AF_DCMI); GPIO_PinAFConfig(GPIOI, GPIO_PinSource4, GPIO_AF_DCMI); /* DCMI GPIO configuration */ /* D0..D4(PH9/10/11/12/14), HSYNC(PH8) */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_14| GPIO_Pin_8; 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(GPIOH, &GPIO_InitStructure); /* D5..D7(PI4/6/7), VSYNC(PI5) */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_5; GPIO_Init(GPIOI, &GPIO_InitStructure); /* PCLK(PA6) */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); /****** Configures the I2C1 used for OV2640 camera module configuration *****/ /* I2C1 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); /* GPIOB clock enable */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); /* Connect I2C1 pins to AF4 */ GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1); GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1); /* Configure I2C1 GPIOs */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOB, &GPIO_InitStructure); /* Configure I2C1 */ /* I2C DeInit */ I2C_DeInit(I2C1); /* Enable the I2C peripheral */ I2C_Cmd(I2C1, ENABLE); /* Set the I2C structure parameters */ I2C_InitStruct.I2C_Mode = I2C_Mode_I2C; I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStruct.I2C_OwnAddress1 = 0xFE; I2C_InitStruct.I2C_Ack = I2C_Ack_Enable; I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStruct.I2C_ClockSpeed = 30000; /* Initialize the I2C peripheral w/ selected parameters */ I2C_Init(I2C1, &I2C_InitStruct); }
void PWM_IN_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; TIM_ICInitTypeDef TIM_ICInitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3|RCC_APB1Periph_TIM4, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOD, ENABLE); NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = NVIC_PWMIN_P; NVIC_InitStructure.NVIC_IRQChannelSubPriority = NVIC_PWMIN_S; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = NVIC_PWMIN_P; NVIC_InitStructure.NVIC_IRQChannelSubPriority = NVIC_PWMIN_S; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); ////////////////////////////////////////////////////////////////////////////////////////////// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; 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_DOWN; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; 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_DOWN; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM3); GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM3); GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_TIM3); GPIO_PinAFConfig(GPIOB, GPIO_PinSource1, GPIO_AF_TIM3); TIM3->PSC = (168/2)-1; TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0x0; TIM_ICInit(TIM3, &TIM_ICInitStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0x0; TIM_ICInit(TIM3, &TIM_ICInitStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_3; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0x0; TIM_ICInit(TIM3, &TIM_ICInitStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_4; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0x0; TIM_ICInit(TIM3, &TIM_ICInitStructure); /* TIM enable counter */ TIM_Cmd(TIM3, ENABLE); /* Enable the CC2 Interrupt Request */ TIM_ITConfig(TIM3, TIM_IT_CC1, ENABLE); TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE); TIM_ITConfig(TIM3, TIM_IT_CC3, ENABLE); TIM_ITConfig(TIM3, TIM_IT_CC4, ENABLE); ///////////////////////////////////////////////////////////////////////////////////////////// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15; 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_DOWN; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM3); GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_TIM3); GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_TIM3); GPIO_PinAFConfig(GPIOD, GPIO_PinSource15, GPIO_AF_TIM3); TIM4->PSC = (168/2)-1; TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0x0; TIM_ICInit(TIM4, &TIM_ICInitStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0x0; TIM_ICInit(TIM4, &TIM_ICInitStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_3; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0x0; TIM_ICInit(TIM4, &TIM_ICInitStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_4; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0x0; TIM_ICInit(TIM4, &TIM_ICInitStructure); /* TIM enable counter */ TIM_Cmd(TIM4, ENABLE); /* Enable the CC2 Interrupt Request */ TIM_ITConfig(TIM4, TIM_IT_CC1, ENABLE); TIM_ITConfig(TIM4, TIM_IT_CC2, ENABLE); TIM_ITConfig(TIM4, TIM_IT_CC3, ENABLE); TIM_ITConfig(TIM4, TIM_IT_CC4, ENABLE); }
/* ********************************************************************************************************* * 函 数 名: RTC_Config * 功能说明: 用于配置时间戳功能 * 形 参:无 * 返 回 值: 无 ********************************************************************************************************* */ void RTC_Config(void) { int RtcTimeout; /* RTC超时计数 */ RTC_TimeTypeDef RTC_TimeStructure; RTC_InitTypeDef RTC_InitStructure; RTC_DateTypeDef RTC_DateStructure; Mem_Set(&RTC_TimeStructure, 0x00, sizeof(RTC_TimeTypeDef)); Mem_Set(&RTC_InitStructure, 0x00, sizeof(RTC_InitTypeDef)); Mem_Set(&RTC_DateStructure, 0x00, sizeof(RTC_DateTypeDef)); RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);/* 使能PWR时钟 */ PWR_BackupAccessCmd(ENABLE);/* 允许访问RTC */ try_again: if(OBCBootInfo.BootRTC_Source == 0) /* 选择LSE作为时钟源 */ { RCC_LSEConfig(RCC_LSE_ON); /* 使能LSE振荡器 */ RtcTimeout = 0; /* 等待就绪 */ while((RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) && ((RtcTimeout++) < RTC_TIMEOUT_US))//20 { bsp_DelayUS(1); /* STM32延迟函数 */ } if(RtcTimeout > RTC_TIMEOUT_US) //18 /* 判断是否有超时 */ { DEBUG_LOG("Init RTC Wrong: Oscillator time out! \r\n"); RCC_LSEConfig(RCC_LSE_OFF); OBCBootInfo.BootLSE_Error = 1; OBCBootInfo.BootRTC_Source = 1; /* 自动配置RTC为内部时钟 */ } if(OBCBootInfo.BootRTC_Source == 0) { DEBUG_LOG("Enable RTC with LSE \r\n"); RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); /* 选择RTC时钟源 */ uwSynchPrediv = 0xFF; uwAsynchPrediv = 0x7F; } } if(OBCBootInfo.BootRTC_Source == 1) /* 选择LSI作为时钟源 */ { RCC_LSICmd(ENABLE); /* 使能内部时钟 */ RtcTimeout = 0; /* 等待内部时钟稳定*/ while((RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) && ((RtcTimeout++) < RTC_TIMEOUT_US)) { bsp_DelayUS(1); /* STM32延迟函数 */ } if(RtcTimeout > RTC_TIMEOUT_US) { DEBUG_LOG("All Oscillator time out! \r\n"); //RCC_LSICmd(DISABLE); OBCBootInfo.BootLSI_Error = 1; OBCBootInfo.BootRTC_Source = 2; /* 自动配置RTC为内部时钟 */ } if(OBCBootInfo.BootRTC_Source == 1) { DEBUG_LOG("Enable RTC with LSI \r\n"); RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); /* 选择RTC时钟源 */ uwSynchPrediv = 0xFF; uwAsynchPrediv = 0x7F; } } if((OBCBootInfo.BootRTC_Source == 0)||(OBCBootInfo.BootRTC_Source == 1)) { RCC_RTCCLKCmd(ENABLE); /* 使能RTC时钟 */ RTC_WaitForSynchro(); /* 等待RTC APB寄存器同步 */ RTC_TimeStampCmd(RTC_TimeStampEdge_Falling, ENABLE); /* 使能时间戳 */ RTC_TimeStampPinSelection(RTC_TamperPin_PC13); RTC_InitStructure.RTC_AsynchPrediv = uwAsynchPrediv; /* 配置RTC数据寄存器和分频器 */ RTC_InitStructure.RTC_SynchPrediv = uwSynchPrediv; RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; if (RTC_Init(&RTC_InitStructure) == ERROR) /* 检测RTC初始化 */ { DEBUG_LOG("RTC Init wrong \r\n"); if(OBCBootInfo.BootRTC_Source == 0) { OBCBootInfo.BootRTC_Source = 1; goto try_again; } } /* 设置年月日和星期 */ RTC_DateStructure.RTC_Year = 0x14; RTC_DateStructure.RTC_Month = RTC_Month_October; RTC_DateStructure.RTC_Date = 0x23; RTC_DateStructure.RTC_WeekDay = RTC_Weekday_Thursday; RTC_SetDate(RTC_Format_BCD, &RTC_DateStructure); /* 设置时分秒,以及显示格式 */ RTC_TimeStructure.RTC_H12 = RTC_H12_AM; RTC_TimeStructure.RTC_Hours = 0x00; RTC_TimeStructure.RTC_Minutes = 0x00; RTC_TimeStructure.RTC_Seconds = 0x00; RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure); } }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f0xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f0xx.c file */ /* Configure Tamper Button */ STM_EVAL_PBInit(BUTTON_TAMPER,BUTTON_MODE_GPIO); /* Loop while Tamper button is maintained pressed */ while(STM_EVAL_PBGetState(BUTTON_TAMPER) == RESET) { } /* Enable PWR APB1 Clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to Backup */ PWR_BackupAccessCmd(ENABLE); /* Reset RTC Domain */ RCC_BackupResetCmd(ENABLE); RCC_BackupResetCmd(DISABLE); /* Loop while Tamper button is maintained pressed */ while(STM_EVAL_PBGetState(BUTTON_TAMPER) != RESET) { } /* Loop while Tamper button is maintained pressed */ while(STM_EVAL_PBGetState(BUTTON_TAMPER) == RESET) { } #if defined (SLEEP_MODE) /* Sleep Mode Entry - System Running at PLL (48MHz) - Flash 3 wait state - Prefetch and Cache enabled - Code running from Internal FLASH - All peripherals disabled. - Wakeup using EXTI Line (Joystick SEL Button PA.00) */ SleepMode_Measure(); #elif defined (STOP_MODE) /* STOP Mode Entry - RTC Clocked by LSI - Regulator in LP mode - HSI, HSE OFF and LSI OFF if not used as RTC Clock source - No IWDG - FLASH in deep power down mode - Automatic Wakeup using RTC clocked by LSI */ StopMode_Measure(); #elif defined (STANDBY_MODE) /* STANDBY Mode Entry - RTC OFF - IWDG and LSI OFF - Wakeup using WakeUp Pin 2(PC.13) */ StandbyMode_Measure(); #elif defined (STANDBY_RTC_MODE) /* STANDBY Mode with RTC on LSI Entry - RTC Clocked by LSI - IWDG OFF and LSI OFF if not used as RTC Clock source - Automatic Wakeup using RTC clocked by LSI */ StandbyRTCMode_Measure(); #else /* Initialize LED3 on EVAL board */ STM_EVAL_LEDInit(LED3); /* Infinite loop */ while (1) { /* Toggle The LED3 */ STM_EVAL_LEDToggle(LED3); /* Inserted Delay */ for(i = 0; i < 0x7FFF; i++); } #endif }
/** * @brief Main program * @param None * @retval None */ int main(void) { NVIC_InitTypeDef NVIC_InitStructure; EXTI_InitTypeDef EXTI_InitStructure; /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup files (startup_stm32f40_41xxx.s/startup_stm32f427_437xx.s) before to branch to application main. */ /* Configure the external interrupt "WAKEUP" and "TAMPER" buttons */ STM_EVAL_PBInit(BUTTON_TAMPER , BUTTON_MODE_GPIO); STM_EVAL_PBInit(BUTTON_WAKEUP , BUTTON_MODE_GPIO); /* Initialize the LCD */ LCD_Init(); /* Configure the LCD Log Module */ LCD_LOG_Init(); LCD_LOG_SetHeader((uint8_t*)"RTC Backup Domain Example"); LCD_LOG_SetFooter ((uint8_t*)" Copyright (c) STMicroelectronics" ); /* Display the default RCC BDCR and RTC TAFCR Registers */ LCD_UsrLog ("Entry Point \n"); LCD_UsrLog ("RCC BDCR = 0x%x\n", RCC->BDCR); LCD_UsrLog ("RTC TAFCR = 0x%x\n", RTC->TAFCR); /* Enable the PWR APB1 Clock Interface */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); /* Configure one bit for preemption priority */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); /* Enable the RTC Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = RTC_WKUP_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* EXTI configuration */ EXTI_ClearITPendingBit(EXTI_Line22); EXTI_InitStructure.EXTI_Line = EXTI_Line22; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); if(RTC_ReadBackupRegister(RTC_BKP_DR0) != FIRST_DATA) { LCD_UsrLog ("RTC Config PLZ Wait. \n"); /* RTC Configuration */ RTC_Config(); /* Adjust Current Time */ Time_Adjust(); /* Adjust Current Date */ Date_Adjust(); } else { /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); RTC_ClearITPendingBit(RTC_IT_WUT); EXTI_ClearITPendingBit(EXTI_Line22); /* Backup SRAM ***************************************************************/ /* Enable BKPSRAM Clock */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_BKPSRAM, ENABLE); /* Check the written Data */ for (uwIndex = 0x0; uwIndex < 0x1000; uwIndex += 4) { if ((*(__IO uint32_t *) (BKPSRAM_BASE + uwIndex)) != uwIndex) { uwErrorIndex++; } } if(uwErrorIndex) { LCD_ErrLog ("BKP SRAM Number of errors = %d\n", uwErrorIndex); } else { LCD_UsrLog ("BKP SRAM Content OK \n"); } /* RTC Backup Data Registers **************************************************/ /* Check if RTC Backup DRx registers data are correct */ if (CheckBackupReg(FIRST_DATA) == 0x00) { /* OK, RTC Backup DRx registers data are correct */ LCD_UsrLog ("OK, RTC Backup DRx registers data are correct. \n"); } else { /* Error, RTC Backup DRx registers data are not correct */ LCD_ErrLog ("RTC Backup DRx registers data are not correct\n"); } } while (1) { /* Infinite loop */ Calendar_Show(); } }
void motorCfg(void) { /* -1- */ GPIO_InitTypeDef Gpio_InitStr; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; /* -2- */ //TIM1 Clock Enable RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //TIM3 TIM4 TIM5 Clock Enable RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3 | RCC_APB1Periph_TIM4 | RCC_APB1Periph_TIM5, ENABLE); /* -3- */ // GPIOA GPIOB GPIOD clock enable RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE); //TIM1 CH1(PA8) CH2(PA9) CH3(PA10) CH4(PA11) //TIM1 No Remap Gpio_InitStr.GPIO_Mode = GPIO_Mode_AF_PP; Gpio_InitStr.GPIO_Speed = GPIO_Speed_50MHz; Gpio_InitStr.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11; GPIO_Init(GPIOA, &Gpio_InitStr); //TIM3 CH1(PB4) CH2(PB5) //TIM3 Partial Remap TIM3_CH1&TIM3_CH2 //TIM3_CH1: PA6 --- PB4 //TIM3_CH2: PA7 --- PB5 GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE); //¹Ø±Õjtag //TIM2 Partial Remap //PA15 Gpio_InitStr.GPIO_Pin = GPIO_Pin_15; GPIO_Init(GPIOA, &Gpio_InitStr); //PB3 Gpio_InitStr.GPIO_Pin = GPIO_Pin_3; GPIO_Init(GPIOB, &Gpio_InitStr); GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2, ENABLE); Gpio_InitStr.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5; GPIO_Init(GPIOB, &Gpio_InitStr); GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); GPIO_SetBits(GPIOB,GPIO_Pin_3); //TIM4 CH3(PD14) CH4(PD15) //TIM4 Full Remap (Motor use CH3~CH4 Ultrasonic use CH1~CH2) //TIM4_CH1: PB6 --- PD12 //TIM4_CH2: PB7 --- PD13 //TIM4_CH3: PB8 --- PD14 //TIM4_CH4: PB9 --- PD15 Gpio_InitStr.GPIO_Pin = GPIO_Pin_14 | GPIO_Pin_15; GPIO_Init(GPIOD, &Gpio_InitStr); GPIO_PinRemapConfig(GPIO_Remap_TIM4, ENABLE); //TIM5 CH1(PA0) CH2(PA1) //TIM1 No Remap Gpio_InitStr.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; GPIO_Init(GPIOA, &Gpio_InitStr); /* -4- */ // Time Base configuration TIM_TimeBaseStructure.TIM_Prescaler = getHclkFrequency()/2000000-1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_Period = 100-1; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; //TIM1 TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //TIM2 TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //TIM3 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //TIM4 TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //TIM5 TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); //PWM set TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0;//Init with duty cycle zero. //High active TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //TIM1 TIM_OC1Init(TIM1, &TIM_OCInitStructure); TIM_OC2Init(TIM1, &TIM_OCInitStructure); TIM_OC3Init(TIM1, &TIM_OCInitStructure); TIM_OC4Init(TIM1, &TIM_OCInitStructure); //TIM2 (PWM CH1~CH2) TIM_OC1Init(TIM2, &TIM_OCInitStructure); TIM_OC2Init(TIM2, &TIM_OCInitStructure); //TIM3(PWM CH1~CH2) TIM_OC1Init(TIM3, &TIM_OCInitStructure); TIM_OC2Init(TIM3, &TIM_OCInitStructure); //TIM4(PWM CH3~CH4) TIM_OC3Init(TIM4, &TIM_OCInitStructure); TIM_OC4Init(TIM4, &TIM_OCInitStructure); //TIM5 TIM_OC1Init(TIM5, &TIM_OCInitStructure); TIM_OC2Init(TIM5, &TIM_OCInitStructure); //TIM1 TIM_Cmd(TIM1, ENABLE); //TIM1 TIM_Cmd(TIM2, ENABLE); //TIM3 TIM_Cmd(TIM3, ENABLE); //TIM4 TIM_Cmd(TIM4, ENABLE); //TIM5 TIM_Cmd(TIM5, ENABLE); //TIM1 Main Output Enable TIM_CtrlPWMOutputs(TIM1, ENABLE); //TIM2 Main Output Enable TIM_CtrlPWMOutputs(TIM2, ENABLE); //TIM3 Main Output Enable TIM_CtrlPWMOutputs(TIM3, ENABLE); //TIM4 Main Output Enable TIM_CtrlPWMOutputs(TIM4, ENABLE); //TIM5 Main Output Enable TIM_CtrlPWMOutputs(TIM5, ENABLE); /* -5- */ TIM_SetCompare1(TIM2, 50); TIM_SetCompare2(TIM2, 50); // //TIM1(PWM CH1~CH4) // TIM_SetCompare1(TIM1, 50); // TIM_SetCompare2(TIM1, 50); // TIM_SetCompare3(TIM1, 50); // TIM_SetCompare4(TIM1, 50); // // //TIM3(PWM CH1~CH2) // TIM_SetCompare1(TIM3, 50); // TIM_SetCompare2(TIM3, 50); // // //TIM4(PWM CH3~CH4) // TIM_SetCompare3(TIM4, 50); // TIM_SetCompare4(TIM4, 50); // // //TIM5(PWM CH1~CH2) // TIM_SetCompare1(TIM5, 50); // TIM_SetCompare2(TIM5, 50); LeftFootMove(50); LeftKneeMove(50); LeftArmMove(50); LeftWristMove(50); HeadMove(50); WaistMove(50); RightFootMove(50); RightKneeMove(70); RightArmMove(50); RightWristMove(50); }
void codec_init() { GPIO_InitTypeDef PinInitStruct; GPIO_StructInit(&PinInitStruct); I2S_InitTypeDef I2S_InitType; I2C_InitTypeDef I2C_InitType; //Reset pin as GPIO PinInitStruct.GPIO_Pin = CODEC_RESET_PIN; PinInitStruct.GPIO_Mode = GPIO_Mode_OUT; PinInitStruct.GPIO_PuPd = GPIO_PuPd_DOWN; PinInitStruct.GPIO_OType = GPIO_OType_PP; PinInitStruct.GPIO_Speed = GPIO_Speed_50MHz; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOB, ENABLE); GPIO_Init(GPIOD, &PinInitStruct); // I2C pins PinInitStruct.GPIO_Mode = GPIO_Mode_AF; PinInitStruct.GPIO_OType = GPIO_OType_OD; PinInitStruct.GPIO_Pin = I2C_SCL_PIN | I2C_SDA_PIN; PinInitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; PinInitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &PinInitStruct); GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1); GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1); //enable I2S and I2C clocks //RCC_I2SCLKConfig(RCC_I2S2CLKSource_PLLI2S); RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1 | RCC_APB1Periph_SPI3, ENABLE); RCC_PLLI2SCmd(ENABLE); // I2S pins PinInitStruct.GPIO_OType = GPIO_OType_PP; PinInitStruct.GPIO_Pin = I2S3_SCLK_PIN | I2S3_SD_PIN | I2S3_MCLK_PIN; GPIO_Init(GPIOC, &PinInitStruct); PinInitStruct.GPIO_Pin = I2S3_WS_PIN; GPIO_Init(GPIOA, &PinInitStruct); //prepare output ports for alternate function GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_SPI3); GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_SPI3); GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_SPI3); GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_SPI3); //keep Codec off for now GPIO_ResetBits(GPIOD, CODEC_RESET_PIN); // configure I2S port SPI_I2S_DeInit(CODEC_I2S); I2S_InitType.I2S_AudioFreq = 24000;//12000//Normal 10000 I2S_InitType.I2S_MCLKOutput = I2S_MCLKOutput_Enable; I2S_InitType.I2S_DataFormat = I2S_DataFormat_16b; I2S_InitType.I2S_Mode = I2S_Mode_MasterTx; I2S_InitType.I2S_Standard = I2S_Standard_Phillips; I2S_InitType.I2S_CPOL = I2S_CPOL_Low; I2S_Init(CODEC_I2S, &I2S_InitType); //I2S_Cmd(CODEC_I2S, ENABLE); // configure I2C port I2C_DeInit(CODEC_I2C); I2C_InitType.I2C_ClockSpeed = 100000; I2C_InitType.I2C_Mode = I2C_Mode_I2C; I2C_InitType.I2C_OwnAddress1 = CORE_I2C_ADDRESS; I2C_InitType.I2C_Ack = I2C_Ack_Enable; I2C_InitType.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitType.I2C_DutyCycle = I2C_DutyCycle_2; I2C_Cmd(CODEC_I2C, ENABLE); I2C_Init(CODEC_I2C, &I2C_InitType); }
/** * @brief Configure the RTC peripheral by selecting the clock source. * @param None * @retval None */ static void RTC_Config(void) { NVIC_InitTypeDef NVIC_InitStructure; EXTI_InitTypeDef EXTI_InitStructure; /* Enable the PWR clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to RTC */ PWR_BackupAccessCmd(ENABLE); /* LSI used as RTC source clock */ /* The RTC Clock may varies due to LSI frequency dispersion. */ /* Enable the LSI OSC */ RCC_LSICmd(ENABLE); /* Wait till LSI is ready */ while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) { } /* Select the RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); /* Enable the RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); /* Calendar Configuration with LSI supposed at 32KHz */ RTC_InitStructure.RTC_AsynchPrediv = 0x7F; RTC_InitStructure.RTC_SynchPrediv = 0xFF; /* (32KHz / 128) - 1 = 0xFF*/ RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; RTC_Init(&RTC_InitStructure); /* EXTI configuration *******************************************************/ EXTI_ClearITPendingBit(EXTI_Line22); EXTI_InitStructure.EXTI_Line = EXTI_Line22; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); /* Enable the RTC Wakeup Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = RTC_WKUP_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Configure the RTC WakeUp Clock source: CK_SPRE (1Hz) */ RTC_WakeUpClockConfig(RTC_WakeUpClock_CK_SPRE_16bits); RTC_SetWakeUpCounter(0x0); /* Enable the RTC Wakeup Interrupt */ RTC_ITConfig(RTC_IT_WUT, ENABLE); /* Enable Wakeup Counter */ RTC_WakeUpCmd(ENABLE); }
/** * @brief Configures TIM5 to measure the LSI oscillator frequency. * @param None * @retval LSI Frequency */ static uint32_t GetLSIFrequency(void) { NVIC_InitTypeDef NVIC_InitStructure; TIM_ICInitTypeDef TIM_ICInitStructure; RCC_ClocksTypeDef RCC_ClockFreq; /* Enable the LSI oscillator ************************************************/ RCC_LSICmd(ENABLE); /* Wait till LSI is ready */ while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) {} /* TIM5 configuration *******************************************************/ /* Enable TIM5 clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); /* Connect internally the TIM5_CH4 Input Capture to the LSI clock output */ TIM_RemapConfig(TIM5, TIM5_LSI); /* Configure TIM5 presclaer */ TIM_PrescalerConfig(TIM5, 0, TIM_PSCReloadMode_Immediate); /* TIM5 configuration: Input Capture mode --------------------- The LSI oscillator is connected to TIM5 CH4 The Rising edge is used as active edge, The TIM5 CCR4 is used to compute the frequency value ------------------------------------------------------------ */ TIM_ICInitStructure.TIM_Channel = TIM_Channel_4; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV8; TIM_ICInitStructure.TIM_ICFilter = 0; TIM_ICInit(TIM5, &TIM_ICInitStructure); /* Enable TIM5 Interrupt channel */ NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable TIM5 counter */ TIM_Cmd(TIM5, ENABLE); /* Reset the flags */ TIM5->SR = 0; /* Enable the CC4 Interrupt Request */ TIM_ITConfig(TIM5, TIM_IT_CC4, ENABLE); /* Wait until the TIM5 get 2 LSI edges (refer to TIM5_IRQHandler() in stm32f4xx_it.c file) ******************************************************/ while(uwCaptureNumber != 2) { } /* Deinitialize the TIM5 peripheral registers to their default reset values */ TIM_DeInit(TIM5); /* Compute the LSI frequency, depending on TIM5 input clock frequency (PCLK1)*/ /* Get SYSCLK, HCLK and PCLKx frequency */ RCC_GetClocksFreq(&RCC_ClockFreq); /* Get PCLK1 prescaler */ if ((RCC->CFGR & RCC_CFGR_PPRE1) == 0) { /* PCLK1 prescaler equal to 1 => TIMCLK = PCLK1 */ return ((RCC_ClockFreq.PCLK1_Frequency / uwPeriodValue) * 8); } else { /* PCLK1 prescaler different from 1 => TIMCLK = 2 * PCLK1 */ return (((2 * RCC_ClockFreq.PCLK1_Frequency) / uwPeriodValue) * 8) ; } }
/*********************************************************** * 函数说明:系统端口初始化函数 * * 输入: 无 * * 输出: 无 * * 调用函数:无 * ***********************************************************/ static void PORT_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; //PORTE 初始化 RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO | ledall_rcc, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_SPI1, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); /* USART1管脚配置 */ /* PA9 USART1_Tx */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //推挽输出-TX GPIO_Init(GPIOA, &GPIO_InitStructure); /* PA10 USART1_Rx */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //GPIO_Mode_IN_FLOATING;//浮空输入-RX GPIO_Mode_IPU GPIO_Init(GPIOA, &GPIO_InitStructure); #if 1 /* USART2管脚配置 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; /* PA2 USART1_Tx */ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //推挽输出-TX GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //GPIO_Mode_IN_FLOATING;//浮空输入-RX GPIO_Mode_IPU GPIO_Init(GPIOA, &GPIO_InitStructure); #endif #if 1 /* ads8329 */ /* Configure SPI1 pins: SCK, MISO and MOSI (ADS8329 using) */ GPIO_InitStructure.GPIO_Pin = ADS8329_SPI_SCK | ADS8329_SPI_MISO | ADS8329_SPI_MOSI; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(ADS8329_SPI_PORT, &GPIO_InitStructure); /* (ADS8329_CS_PORT, ADS8329_CS_PIN), Configure SPI1 pin: NSS */ GPIO_InitStructure.GPIO_Pin = ADS8329_CS_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(ADS8329_CS_PORT, &GPIO_InitStructure); GPIO_SetBits(ADS8329_CS_PORT, ADS8329_CS_PIN); GPIO_InitStructure.GPIO_Pin = ADS8329_NCONVST_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(ADS8329_NCONVST_PORT, &GPIO_InitStructure); GPIO_SetBits(ADS8329_NCONVST_PORT, ADS8329_NCONVST_PIN); GPIO_InitStructure.GPIO_Pin = ADS8329_EOC_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(ADS8329_EOC_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = SELECT_PA_CHANNEL_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(SELECT_PA_CHANNEL_PORT, &GPIO_InitStructure); close_pa_switch(); GPIO_InitStructure.GPIO_Pin = SELECT_PB_CHANNEL_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(SELECT_PB_CHANNEL_PORT, &GPIO_InitStructure); close_pb_switch(); GPIO_InitStructure.GPIO_Pin = SELECT_PC_CHANNEL_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(SELECT_PC_CHANNEL_PORT, &GPIO_InitStructure); close_pc_switch(); open_pa_switch(); #endif /* 恒温盒内温度采集及ABC三相的均值, ADC1, ADC_Channel_0->PA0->恒温盒内温度 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOA, &GPIO_InitStructure); /* * led */ GPIO_InitStructure.GPIO_Pin = led1_pin; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(led1_gpio, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = led2_pin; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(led2_gpio, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = led3_pin; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(led3_gpio, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = led4_pin; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(led4_gpio, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = led5_pin; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(led5_gpio, &GPIO_InitStructure); #if 1 /* 作为pwm信号输出管脚-对应timer2通道2 PB3 需进行Remap设置 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); #endif return; }
/* * @brief Should take an integer 0-255 and create a PWM signal with a duty cycle from 0-100%. * TIM_PWM_FREQ is set at 500 Hz */ void analogWrite(uint16_t pin, uint8_t value) { if (pin >= TOTAL_PINS || PIN_MAP[pin].timer_peripheral == NULL) { return; } // SPI safety check if (SPI.isEnabled() == true && (pin == SCK || pin == MOSI || pin == MISO)) { return; } // I2C safety check if (Wire.isEnabled() == true && (pin == SCL || pin == SDA)) { return; } // Serial1 safety check if (Serial1.isEnabled() == true && (pin == RX || pin == TX)) { return; } if(PIN_MAP[pin].pin_mode != OUTPUT && PIN_MAP[pin].pin_mode != AF_OUTPUT_PUSHPULL) { return; } TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; //PWM Frequency : 500 Hz uint16_t TIM_Prescaler = (uint16_t)(SystemCoreClock / 24000000) - 1;//TIM Counter clock = 24MHz uint16_t TIM_ARR = (uint16_t)(24000000 / TIM_PWM_FREQ) - 1; // TIM Channel Duty Cycle(%) = (TIM_CCR / TIM_ARR + 1) * 100 uint16_t TIM_CCR = (uint16_t)(value * (TIM_ARR + 1) / 255); // AFIO clock enable RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); pinMode(pin, AF_OUTPUT_PUSHPULL); // TIM clock enable if(PIN_MAP[pin].timer_peripheral == TIM2) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); else if(PIN_MAP[pin].timer_peripheral == TIM3) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); else if(PIN_MAP[pin].timer_peripheral == TIM4) RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); // Time base configuration TIM_TimeBaseStructure.TIM_Period = TIM_ARR; TIM_TimeBaseStructure.TIM_Prescaler = TIM_Prescaler; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(PIN_MAP[pin].timer_peripheral, &TIM_TimeBaseStructure); // PWM1 Mode configuration TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OCInitStructure.TIM_Pulse = TIM_CCR; if(PIN_MAP[pin].timer_ch == TIM_Channel_1) { // PWM1 Mode configuration: Channel1 TIM_OC1Init(PIN_MAP[pin].timer_peripheral, &TIM_OCInitStructure); TIM_OC1PreloadConfig(PIN_MAP[pin].timer_peripheral, TIM_OCPreload_Enable); } else if(PIN_MAP[pin].timer_ch == TIM_Channel_2) { // PWM1 Mode configuration: Channel2 TIM_OC2Init(PIN_MAP[pin].timer_peripheral, &TIM_OCInitStructure); TIM_OC2PreloadConfig(PIN_MAP[pin].timer_peripheral, TIM_OCPreload_Enable); } else if(PIN_MAP[pin].timer_ch == TIM_Channel_3) { // PWM1 Mode configuration: Channel3 TIM_OC3Init(PIN_MAP[pin].timer_peripheral, &TIM_OCInitStructure); TIM_OC3PreloadConfig(PIN_MAP[pin].timer_peripheral, TIM_OCPreload_Enable); } else if(PIN_MAP[pin].timer_ch == TIM_Channel_4) { // PWM1 Mode configuration: Channel4 TIM_OC4Init(PIN_MAP[pin].timer_peripheral, &TIM_OCInitStructure); TIM_OC4PreloadConfig(PIN_MAP[pin].timer_peripheral, TIM_OCPreload_Enable); } TIM_ARRPreloadConfig(PIN_MAP[pin].timer_peripheral, ENABLE); // TIM enable counter TIM_Cmd(PIN_MAP[pin].timer_peripheral, ENABLE); }
void SPIX_Init(uint8_t SPI_NO) { // GPIO initial GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; if (SPI_NO == SPIX_1) { RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC|RCC_APB2Periph_SPI1, ENABLE ); // Turn on SPI clock and GPIOA clock. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7; //PA5:SCK, PA6:MISO, PA7:MOSI GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_SetBits(GPIOA,GPIO_Pin_5|GPIO_Pin_7); // Pull high. } else if (SPI_NO == SPIX_2) { RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE ); RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE ); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15; //PB13:SCK, PB14:MISO, PB15:MOSI GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_SetBits(GPIOB,GPIO_Pin_13|GPIO_Pin_15); // Pull high. } // SPI function initial. SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; // Data size 8 bits. SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; // Normal clock is high. SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; // NSS self control. SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; //set, but not use. //=========================================================================== // SPI CRC using note: ///* Enable SPI CRC calculation */ // SPI_CalculateCRC(SPI, ENABLE); <-- in setting. // // During send loop: // /* Send last SPI2_Buffer_Tx data */ // SPI_I2S_SendData(SPI2, SPI2_Buffer_Tx[TxIdx]); // /* Enable SPI2 CRC transmission */ // SPI_TransmitCRC(SPI2); // ref link: https://github.com/david2004kang/gcc-arm-none-eabi_samples/blob/master/STM32F10x_StdPeriph_Lib_V3.5.0/Project/STM32F10x_StdPeriph_Examples/SPI/CRC/main.c if (SPI_NO == SPIX_1 && !u8SPI1_Inital) { SPI_Init(SPI1, &SPI_InitStructure); SPI_SSOutputCmd(SPI1, ENABLE);// Configure?PA.4(NSS) ,but we need use it as GPIO SPI_Cmd(SPI1, ENABLE); //============================================================= // SET UP other ext pins. // PA3:LCD reset, , 0 = reset, normal 1 // PA4:NSS, Enable = 0, Disable = 1 // PA8:D/C data = 1, command = 0 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_8; // Self control NSS, DC, RST GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_SetBits(GPIOA, GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_8); // Pull high. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; // NRF24L01 CSN GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_SetBits(GPIOC, GPIO_Pin_4); // Pull high. INIT_RINGBUFFER(SPI1_TXCmdBuffer); INIT_RINGBUFFER(SPI1_RXBuffer); u8Nss1Low = 0; u8SPI1_Inital = 1; } else if (SPI_NO == SPIX_2 && !u8SPI2_Inital) { SPI_Init(SPI2, &SPI_InitStructure); SPI_SSOutputCmd(SPI2, ENABLE);// Configure?PA.4(NSS) ,but we need use it as GPIO SPI_Cmd(SPI2, ENABLE); //============================================================= // SET UP other ext pins. // PB11:LCD reset, , 0 = reset, normal 1 // PB12:NSS, Enable = 0, Disable = 1 // PB10:D/C data = 1, command = 0 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12; // Self control NSS, DC, RST GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_SetBits(GPIOB, GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12); // Pull high. INIT_RINGBUFFER(SPI2_TXCmdBuffer); INIT_RINGBUFFER(SPI2_RXBuffer); u8Nss2Low = 0; u8SPI2_Inital = 1; } printf("NokiaLCD5110Init() create thread OK.\r\n"); }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f10x_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f10x.c file */ /* NVIC configuration */ NVIC_Configuration(); /* Initialize Leds mounted on STM3210X-EVAL board */ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); /* Enable PWR and BKP clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); /* Enable write access to Backup domain */ PWR_BackupAccessCmd(ENABLE); /* Disable Tamper pin */ BKP_TamperPinCmd(DISABLE); /* Disable Tamper interrupt */ BKP_ITConfig(DISABLE); /* Tamper pin active on low level */ BKP_TamperPinLevelConfig(BKP_TamperPinLevel_Low); /* Clear Tamper pin Event(TE) pending flag */ BKP_ClearFlag(); /* Enable Tamper interrupt */ BKP_ITConfig(ENABLE); /* Enable Tamper pin */ BKP_TamperPinCmd(ENABLE); /* Write data to Backup DRx registers */ WriteToBackupReg(0xA53C); /* Check if the written data are correct */ if(CheckBackupReg(0xA53C) == 0x00) { /* Turn on LED1 */ STM_EVAL_LEDOn(LED1); } else { /* Turn on LED2 */ STM_EVAL_LEDOn(LED2); } while (1) { } }
/*初始化*/ static rt_err_t dev_gps_init( rt_device_t dev ) { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOC, ENABLE ); RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOD, ENABLE ); RCC_APB1PeriphClockCmd( RCC_APB1Periph_UART5, ENABLE ); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPS_PWR_PIN; GPIO_Init( GPS_PWR_PORT, &GPIO_InitStructure ); GPIO_ResetBits( GPS_PWR_PORT, GPS_PWR_PIN ); if(Module_3017A!=GPS_MODULE_TYPE) { //#ifdef HC_595_CONTROL // 2013-4-20 更改PCB 用PD4 : GPS 天线开路 PB6 : GPS 天线短路 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; // IN //------------------- PD4 ----------------------------- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; //GPS 天线开路 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_Init(GPIOD, &GPIO_InitStructure); //------------------- PB6 ----------------------------- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //GPS 天线短路 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_Init(GPIOB, &GPIO_InitStructure); //#endif } /*uart5 管脚设置*/ GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_Init( GPIOC, &GPIO_InitStructure ); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_Init( GPIOD, &GPIO_InitStructure ); GPIO_PinAFConfig( GPIOC, GPIO_PinSource12, GPIO_AF_UART5 ); GPIO_PinAFConfig( GPIOD, GPIO_PinSource2, GPIO_AF_UART5 ); /*NVIC 设置*/ NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init( &NVIC_InitStructure ); gps_baud( 9600 ); USART_Cmd( UART5, ENABLE ); USART_ITConfig( UART5, USART_IT_RXNE, ENABLE ); GPIO_ResetBits( GPIOD, GPIO_Pin_10 ); return RT_EOK; }
/** * @brief Initializes peripherals used by the I2C EEPROM driver. * @param None * @retval None */ void sEE_LowLevel_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; /*!< sEE_I2C Periph clock enable */ RCC_APB1PeriphClockCmd(sEE_I2C_CLK, ENABLE); /*!< sEE_I2C_SCL_GPIO_CLK and sEE_I2C_SDA_GPIO_CLK Periph clock enable */ RCC_AHB1PeriphClockCmd(sEE_I2C_SCL_GPIO_CLK | sEE_I2C_SDA_GPIO_CLK, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); /* Reset sEE_I2C IP */ RCC_APB1PeriphResetCmd(sEE_I2C_CLK, ENABLE); /* Release reset signal of sEE_I2C IP */ RCC_APB1PeriphResetCmd(sEE_I2C_CLK, DISABLE); /*!< GPIO configuration */ /* Connect PXx to I2C_SCL*/ GPIO_PinAFConfig(sEE_I2C_SCL_GPIO_PORT, sEE_I2C_SCL_SOURCE, sEE_I2C_SCL_AF); /* Connect PXx to I2C_SDA*/ GPIO_PinAFConfig(sEE_I2C_SDA_GPIO_PORT, sEE_I2C_SDA_SOURCE, sEE_I2C_SDA_AF); /*!< Configure sEE_I2C pins: SCL */ GPIO_InitStructure.GPIO_Pin = sEE_I2C_SCL_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(sEE_I2C_SCL_GPIO_PORT, &GPIO_InitStructure); /*!< Configure sEE_I2C pins: SDA */ GPIO_InitStructure.GPIO_Pin = sEE_I2C_SDA_PIN; GPIO_Init(sEE_I2C_SDA_GPIO_PORT, &GPIO_InitStructure); /* Configure and enable I2C DMA TX Channel interrupt */ NVIC_InitStructure.NVIC_IRQChannel = sEE_I2C_DMA_TX_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = sEE_I2C_DMA_PREPRIO; NVIC_InitStructure.NVIC_IRQChannelSubPriority = sEE_I2C_DMA_SUBPRIO; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Configure and enable I2C DMA RX Channel interrupt */ NVIC_InitStructure.NVIC_IRQChannel = sEE_I2C_DMA_RX_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = sEE_I2C_DMA_PREPRIO; NVIC_InitStructure.NVIC_IRQChannelSubPriority = sEE_I2C_DMA_SUBPRIO; NVIC_Init(&NVIC_InitStructure); /*!< I2C DMA TX and RX channels configuration */ /* Enable the DMA clock */ RCC_AHB1PeriphClockCmd(sEE_I2C_DMA_CLK, ENABLE); /* Clear any pending flag on Rx Stream */ DMA_ClearFlag(sEE_I2C_DMA_STREAM_TX, sEE_TX_DMA_FLAG_FEIF | sEE_TX_DMA_FLAG_DMEIF | sEE_TX_DMA_FLAG_TEIF | \ sEE_TX_DMA_FLAG_HTIF | sEE_TX_DMA_FLAG_TCIF); /* Disable the EE I2C Tx DMA stream */ DMA_Cmd(sEE_I2C_DMA_STREAM_TX, DISABLE); /* Configure the DMA stream for the EE I2C peripheral TX direction */ DMA_DeInit(sEE_I2C_DMA_STREAM_TX); sEEDMA_InitStructure.DMA_Channel = sEE_I2C_DMA_CHANNEL; sEEDMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)sEE_I2C_DR_Address; sEEDMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)0; /* This parameter will be configured durig communication */; sEEDMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; /* This parameter will be configured durig communication */ sEEDMA_InitStructure.DMA_BufferSize = 0xFFFF; /* This parameter will be configured durig communication */ sEEDMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; sEEDMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; sEEDMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; sEEDMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; sEEDMA_InitStructure.DMA_Mode = DMA_Mode_Normal; sEEDMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; sEEDMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable; sEEDMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full; sEEDMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; sEEDMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(sEE_I2C_DMA_STREAM_TX, &sEEDMA_InitStructure); /* Clear any pending flag on Rx Stream */ DMA_ClearFlag(sEE_I2C_DMA_STREAM_RX, sEE_RX_DMA_FLAG_FEIF | sEE_RX_DMA_FLAG_DMEIF | sEE_RX_DMA_FLAG_TEIF | \ sEE_RX_DMA_FLAG_HTIF | sEE_RX_DMA_FLAG_TCIF); /* Disable the EE I2C DMA Rx stream */ DMA_Cmd(sEE_I2C_DMA_STREAM_RX, DISABLE); /* Configure the DMA stream for the EE I2C peripheral RX direction */ DMA_DeInit(sEE_I2C_DMA_STREAM_RX); DMA_Init(sEE_I2C_DMA_STREAM_RX, &sEEDMA_InitStructure); /* Enable the DMA Channels Interrupts */ DMA_ITConfig(sEE_I2C_DMA_STREAM_TX, DMA_IT_TC, ENABLE); DMA_ITConfig(sEE_I2C_DMA_STREAM_RX, DMA_IT_TC, ENABLE); }
/** * @brief Initializes peripherals: I2Cx, GPIO, DMA channels . * @param None * @retval None */ void I2C_LowLevel_Init(I2C_TypeDef* I2Cx) { GPIO_InitTypeDef GPIO_InitStructure; I2C_InitTypeDef I2C_InitStructure; /* GPIOB clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); /* Enable the DMA1 clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); if (I2Cx == I2C1) { /* I2C1 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE); /* I2C1 SDA and SCL configuration */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_Init(GPIOB, &GPIO_InitStructure); /* Enable I2C1 reset state */ RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE); /* Release I2C1 from reset state */ RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE); } else /* I2Cx = I2C2 */ { // Configure PB0-2 pins as input pull-down for terminating possibly pending I2C connections GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOB, &GPIO_InitStructure); // SCL as Output GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); terminate_i2c(); /* I2C2 clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE); /* I2C1 SDA and SCL configuration */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // SCL GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; // SDA GPIO_Init(GPIOB, &GPIO_InitStructure); /* Enable I2C2 reset state */ RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE); /* Release I2C2 from reset state */ RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE); } /* I2C1 and I2C2 configuration */ I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = OwnAddress1; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = 100000; I2C_Init(I2C1, &I2C_InitStructure); I2C_InitStructure.I2C_OwnAddress1 = 0x10; I2C_Init(I2C2, &I2C_InitStructure); if (I2Cx == I2C1) { /* I2C1 TX DMA Channel configuration */ DMA_DeInit(I2C1_DMA_CHANNEL_TX); I2CDMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)I2C1_DR_Address; I2CDMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)0; /* This parameter will be configured durig communication */ I2CDMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; /* This parameter will be configured durig communication */ I2CDMA_InitStructure.DMA_BufferSize = 0xFFFF; /* This parameter will be configured durig communication */ I2CDMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; I2CDMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; I2CDMA_InitStructure.DMA_PeripheralDataSize = DMA_MemoryDataSize_Byte; I2CDMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; I2CDMA_InitStructure.DMA_Mode = DMA_Mode_Normal; I2CDMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; I2CDMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(I2C1_DMA_CHANNEL_TX, &I2CDMA_InitStructure); /* I2C1 RX DMA Channel configuration */ DMA_DeInit(I2C1_DMA_CHANNEL_RX); DMA_Init(I2C1_DMA_CHANNEL_RX, &I2CDMA_InitStructure); } else /* I2Cx = I2C2 */ { /* I2C2 TX DMA Channel configuration */ DMA_DeInit(I2C2_DMA_CHANNEL_TX); I2CDMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)I2C2_DR_Address; I2CDMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)0; /* This parameter will be configured durig communication */ I2CDMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; /* This parameter will be configured durig communication */ I2CDMA_InitStructure.DMA_BufferSize = 0xFFFF; /* This parameter will be configured durig communication */ I2CDMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; I2CDMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; I2CDMA_InitStructure.DMA_PeripheralDataSize = DMA_MemoryDataSize_Byte; I2CDMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; I2CDMA_InitStructure.DMA_Mode = DMA_Mode_Normal; I2CDMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; I2CDMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(I2C2_DMA_CHANNEL_TX, &I2CDMA_InitStructure); /* I2C2 RX DMA Channel configuration */ DMA_DeInit(I2C2_DMA_CHANNEL_RX); DMA_Init(I2C2_DMA_CHANNEL_RX, &I2CDMA_InitStructure); } }
/** * @brief Configure the RTC peripheral by selecting the clock source. * @param None * @retval None */ static void RTC_Config(void) { /* Enable the PWR clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Allow access to RTC */ PWR_BackupAccessCmd(ENABLE); #if defined (RTC_CLOCK_SOURCE_LSI) /* LSI used as RTC source clock*/ /* The RTC Clock may varies due to LSI frequency dispersion. */ /* Enable the LSI OSC */ RCC_LSICmd(ENABLE); /* Wait till LSI is ready */ while(RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET) { } /* Select the RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); /* ck_spre(1Hz) = RTCCLK(LSI) /(uwAsynchPrediv + 1)*(uwSynchPrediv + 1)*/ uwSynchPrediv = 0xFF; uwAsynchPrediv = 0x7F; #elif defined (RTC_CLOCK_SOURCE_LSE) /* LSE used as RTC source clock */ /* Enable the LSE OSC */ RCC_LSEConfig(RCC_LSE_ON); /* Wait till LSE is ready */ while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) { } /* Select the RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); /* ck_spre(1Hz) = RTCCLK(LSE) /(uwAsynchPrediv + 1)*(uwSynchPrediv + 1)*/ uwSynchPrediv = 0xFF; uwAsynchPrediv = 0x7F; #else #error Please select the RTC Clock source inside the main.c file #endif /* RTC_CLOCK_SOURCE_LSI */ /* Enable the RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC APB registers synchronisation */ RTC_WaitForSynchro(); /* Write to the first RTC Backup Data Register */ RTC_WriteBackupRegister(RTC_BKP_DR0, FIRST_DATA); /* Display the new RCC BDCR and RTC TAFCR Registers */ LCD_UsrLog ("RTC Reconfig \n"); LCD_UsrLog ("RCC BDCR = 0x%x\n", RCC->BDCR); LCD_UsrLog ("RTC TAFCR = 0x%x\n", RTC->TAFCR); /* Set the Time */ RTC_TimeStructure.RTC_Hours = 0x08; RTC_TimeStructure.RTC_Minutes = 0x00; RTC_TimeStructure.RTC_Seconds = 0x00; /* Set the Date */ RTC_DateStructure.RTC_Month = RTC_Month_January; RTC_DateStructure.RTC_Date = 0x11; RTC_DateStructure.RTC_Year = 0x13; RTC_DateStructure.RTC_WeekDay = RTC_Weekday_Friday; /* Calendar Configuration */ RTC_InitStructure.RTC_AsynchPrediv = uwAsynchPrediv; RTC_InitStructure.RTC_SynchPrediv = uwSynchPrediv; RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24; RTC_Init(&RTC_InitStructure); /* Set Current Time and Date */ RTC_SetTime(RTC_Format_BCD, &RTC_TimeStructure); RTC_SetDate(RTC_Format_BCD, &RTC_DateStructure); /* Configure the RTC Wakeup Clock source and Counter (Wakeup event each 1 second) */ RTC_WakeUpClockConfig(RTC_WakeUpClock_RTCCLK_Div16); RTC_SetWakeUpCounter(0x7FF); /* Enable the Wakeup Interrupt */ RTC_ITConfig(RTC_IT_WUT, ENABLE); /* Enable Wakeup Counter */ RTC_WakeUpCmd(ENABLE); /* Backup SRAM ***************************************************************/ /* Enable BKPRAM Clock */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_BKPSRAM, ENABLE); /* Write to Backup SRAM with 32-Bit Data */ for (uwIndex = 0x0; uwIndex < 0x1000; uwIndex += 4) { *(__IO uint32_t *) (BKPSRAM_BASE + uwIndex) = uwIndex; } /* Check the written Data */ for (uwIndex = 0x0; uwIndex < 0x1000; uwIndex += 4) { if ((*(__IO uint32_t *) (BKPSRAM_BASE + uwIndex)) != uwIndex) { uwErrorIndex++; } } if(uwErrorIndex) { LCD_ErrLog ("BKP SRAM Number of errors = %d\n", uwErrorIndex); } else { LCD_UsrLog ("BKP SRAM write OK \n"); } /* Enable the Backup SRAM low power Regulator to retain it's content in VBAT mode */ PWR_BackupRegulatorCmd(ENABLE); /* Wait until the Backup SRAM low power Regulator is ready */ while(PWR_GetFlagStatus(PWR_FLAG_BRR) == RESET) { } /* RTC Backup Data Registers **************************************************/ /* Write to RTC Backup Data Registers */ WriteToBackupReg(FIRST_DATA); }
/** * Initialize the UART : set pins, enable clocks, set uart, enable interrupt * */ static inline void init(uint32_t baudrate) { GPIO_InitTypeDef GPIO_InitStruct; USART_InitTypeDef USART_InitStruct; NVIC_InitTypeDef NVIC_InitStructure; //General settings of pins TX/RX GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; switch (USART_ID) { case 1: RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; // Pins B6 (TX) and B7 (RX) GPIO_Init(GPIOB, &GPIO_InitStruct); GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1); GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1); NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; break; case 2: RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; // Pins A2 (TX) and A3 (RX) GPIO_Init(GPIOA, &GPIO_InitStruct); GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2); NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; break; case 3: RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; // Pins D8 (TX) and D9 (RX) GPIO_Init(GPIOD, &GPIO_InitStruct); GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3); GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART3); NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; break; case 4: RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11; // Pins C10 (TX) and C11 (RX) GPIO_Init(GPIOC, &GPIO_InitStruct); GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_UART4); GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_UART4); NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn; break; case 5: RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12; // Pin C12 (TX) GPIO_Init(GPIOC, &GPIO_InitStruct); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2; // Pin D2 (RX) GPIO_Init(GPIOD, &GPIO_InitStruct); GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_UART5); GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_UART5); NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn; break; case 6: RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; // Pins C6 (TX) and C7 (RX) GPIO_Init(GPIOC, &GPIO_InitStruct); GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6); GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6); NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn; break; } //UART setting USART_InitStruct.USART_BaudRate = baudrate; USART_InitStruct.USART_WordLength = USART_WordLength_8b; // octet comme taille élémentaore (standard) USART_InitStruct.USART_StopBits = USART_StopBits_1; // bit de stop = 1 (standard) USART_InitStruct.USART_Parity = USART_Parity_No; // pas de bit de parité (standard) USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // pas de controle de flux (standard) USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; USART_Init(USARTx, &USART_InitStruct); USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE); //Setting of interrupt NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); //Enable UART USART_Cmd(USARTx, ENABLE); }
/** * \fn initCAN * \brief initialize the can interface * * \param[in] app_rx_funktion pointer to callback function * \return None */ void initCAN(CANRxCatch_t app_rx_funktion) { /* variable for CAN init */ GPIO_InitTypeDef CAN_gpio; CAN_InitTypeDef CAN_InitStructure; CAN_FilterInitTypeDef CAN_FilterInitStructure; uint8_t filter_count; /* pins configuration */ CAN_gpio.GPIO_Pin = CAN_PIN_TX | CAN_PIN_RX; CAN_gpio.GPIO_Mode = CAN_PINS_MODE; CAN_gpio.GPIO_OType = CAN_PINS_TYPE; CAN_gpio.GPIO_Speed = CAN_PINS_SPEED; CAN_gpio.GPIO_PuPd = CAN_PINS_PUPD; /* enable the CAN-interface interrupt */ rx_function = app_rx_funktion; initCANInterrupt(CAN_INTERFACE); /* Enable GPIO clock */ RCC_AHB1PeriphClockCmd(CAN_PORT_RCC,ENABLE); #if defined(CAN_PIN_EN_NUMBER) && defined(CAN_EN_PORT_LETTER) && defined(CAN_PIN_EN_MODE) && defined(CAN_LEVEL_ACTIVE) RCC_AHB1PeriphClockCmd(CAN_EN_PORT_RCC,ENABLE); /* EN Pin */ #endif /* Enable CAN clock */ RCC_APB1PeriphClockCmd(CAN_PIN_RCC, ENABLE); /* Connect PXx to CANTx_Tx*/ GPIO_PinAFConfig(CAN_PORT, CAN_PIN_TX_SOURCE, CAN_PIN_AF); /* Connect PXx to CANRx_Rx*/ GPIO_PinAFConfig(CAN_PORT, CAN_PIN_RX_SOURCE, CAN_PIN_AF); /* Init Tx- and Rx-pins */ GPIO_Init(CAN_PORT,&CAN_gpio); /* CAN EN Pin */ #if defined(CAN_PIN_EN_NUMBER) && defined(CAN_EN_PORT_LETTER) && defined(CAN_PIN_EN_MODE) && defined(CAN_LEVEL_ACTIVE) CAN_gpio.GPIO_Pin = CAN_PIN_EN; CAN_gpio.GPIO_Mode = CAN_PIN_EN_MODE; GPIO_Init( CAN_EN_PORT, &CAN_gpio ); #if CAN_LEVEL_ACTIVE == 'L' GPIO_ResetBits(CAN_EN_PORT,CAN_PIN_EN); /* set the RS pin of the CAN-transceiver to low -> no sleep-mode */ #else GPIO_SetBits(CAN_EN_PORT,CAN_PIN_EN); /* set the RS pin of the CAN-transceiver to low -> no sleep-mode */ #endif #endif /* CAN register reset */ CAN_DeInit(CAN_INTERFACE); /* CAN cell init */ CAN_InitStructure.CAN_TTCM = DISABLE; /* Time Triggered Communication Mode */ CAN_InitStructure.CAN_ABOM = DISABLE; /* Automatic Bus-Off Management */ CAN_InitStructure.CAN_AWUM = DISABLE; /* Automatic Wakeup Mode */ CAN_InitStructure.CAN_NART = ENABLE; /* No Automatic Retransmission */ CAN_InitStructure.CAN_RFLM = DISABLE; /* Receive FIFO Locked Mode */ CAN_InitStructure.CAN_TXFP = DISABLE; /* Transmit FIFO Priority */ CAN_InitStructure.CAN_Mode = CAN_Mode_Normal; CAN_InitStructure.CAN_SJW = CAN_SJW_1tq; /* CAN Baudrate */ CAN_InitStructure.CAN_BS1 = CAN_TIME_BS1; CAN_InitStructure.CAN_BS2 = CAN_TIME_BS2; CAN_InitStructure.CAN_Prescaler = CAN_PRESCALER; CAN_Init(CAN_INTERFACE, &CAN_InitStructure); /* CAN filter init */ for(filter_count = 0; filter_count < filter_bank_number + 1; filter_count++) { CAN_FilterInitStructure.CAN_FilterNumber = filter_count; CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdList; CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_16bit; CAN_FilterInitStructure.CAN_FilterFIFOAssignment = 0; CAN_FilterInitStructure.CAN_FilterActivation = ENABLE; CAN_FilterInitStructure.CAN_FilterIdLow = filter_bank[filter_count][0]; /* lower 16bit CAN_FxR1 */ CAN_FilterInitStructure.CAN_FilterMaskIdLow = filter_bank[filter_count][1]; /* upper 16bit CAN_FxR1 */ CAN_FilterInitStructure.CAN_FilterIdHigh = filter_bank[filter_count][2]; /* lower 16bit CAN_FxR2 */ CAN_FilterInitStructure.CAN_FilterMaskIdHigh = filter_bank[filter_count][3]; /* upper 16bit CAN_FxR2 */ CAN_FilterInit(&CAN_FilterInitStructure); } /* Enable FIFO 0 message pending Interrupt */ CAN_ITConfig(CAN_INTERFACE, CAN_IT_FMP0, ENABLE); }
/**************************** * PB0,PWM输出 ****************************/ void motor_init() { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TimOCInitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //控制电机方向 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //控制电机方向 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); TIM_DeInit(TIM3); TIM_InternalClockConfig(TIM3); //预分频系数为72,这样计数器时钟为72MHz/72 = 1MHz TIM_TimeBaseStructure.TIM_Prescaler = 72; //设置时钟分割 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置计数器模式为向上计数模式 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //设置计数溢出大小,每计1000个数就产生一个更新事件 TIM_TimeBaseStructure.TIM_Period = 1000-1; //将配置应用到TIM1中 TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure); //禁止ARR预装载缓冲器 TIM_ARRPreloadConfig(TIM3, DISABLE); TIM_Cmd(TIM3, DISABLE); //失能TIMx外设 //设置缺省值 TIM_OCStructInit(&TimOCInitStructure); //PWM模式1输出 TimOCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //设置占空比,占空比=(CCRx/ARR)*100%或(TIM_Pulse/TIM_Period)*100% TimOCInitStructure.TIM_Pulse = 500-1; //TIM输出比较极性高 TimOCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //使能输出状态 TimOCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //TimOCInitStructure.TIM_Channel = TIM_Channel_4; //TIM1的CH4输出 TIM_OC3Init(TIM3, &TimOCInitStructure); //TIM_OC4Init(TIM1, &TimOCInitStructure); //设置TIM2的PWM输出为使能 TIM_CtrlPWMOutputs(TIM3,ENABLE); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
void rt_stm32f10x_spi_init(void) { rt_kprintf("rt_stm32f10x_spi_init \r\n"); #ifdef USING_SPI1 /* SPI1 config */ { //rt_kprintf("USE_SPI1 \r\n"); static struct stm32_spi_bus stm32_spi_1; GPIO_InitTypeDef GPIO_InitStructure; /* Enable SPI1 Periph clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO | RCC_APB2Periph_SPI1, ENABLE); #ifdef SPI_USE_DMA rt_kprintf("SPI_USE_DMA \r\n"); /* Enable the DMA1 Clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); #endif /* #ifdef SPI_USE_DMA */ /* Configure SPI1 pins: PA5-SCK, PA6-MISO and PA7-MOSI */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); stm32_spi_register(SPI1, &stm32_spi_1, "spi1"); } /* SPI1 config */ #endif #ifdef USING_SPI2 /* SPI config */ { static struct stm32_spi_bus stm32_spi_2; GPIO_InitTypeDef GPIO_InitStructure; /* Enable SPI2 Periph clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); /*!< Enable the SPI and GPIO clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); #ifdef SPI_USE_DMA /* Enable the DMA1 Clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); #endif /* Configure SPI2 pins: PB13-SCK, PB14-MISO and PB15-MOSI */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /*!< SPI SCK pin configuration */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; /* PB13 SPI2_SCK */ GPIO_Init(GPIOB, &GPIO_InitStructure); /*!< SPI MISO pin configuration */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; /* PB14 SPI2_MISO */ GPIO_Init(GPIOB, &GPIO_InitStructure); /*!< SPI MOSI pin configuration */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; /* PB15 SPI2_MOSI */ GPIO_Init(GPIOB, &GPIO_InitStructure); stm32_spi_register(SPI2, &stm32_spi_2, "spi2"); } /* SPI config */ #endif #ifdef USING_SPI3 /* SPI config */ { static struct stm32_spi_bus stm32_spi_3; GPIO_InitTypeDef GPIO_InitStructure; /* Enable SPI2 Periph clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); /*!< Enable the SPI and GPIO clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); #ifdef SPI_USE_DMA /* Enable the DMA2 Clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE); #endif /* Configure SPI2 pins: PB13-SCK, PB14-MISO and PB15-MOSI */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /*!< SPI SCK pin configuration */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; /* PB13 SPI2_SCK */ GPIO_Init(GPIOB, &GPIO_InitStructure); /*!< SPI MISO pin configuration */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; /* PB14 SPI2_MISO */ GPIO_Init(GPIOB, &GPIO_InitStructure); /*!< SPI MOSI pin configuration */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; /* PB15 SPI2_MOSI */ GPIO_Init(GPIOB, &GPIO_InitStructure); stm32_spi_register(SPI3, &stm32_spi_3, "spi3"); } /* SPI config */ #endif }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f2xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f2xx.c file */ /* Preconfiguration before using DAC----------------------------------------*/ GPIO_InitTypeDef GPIO_InitStructure; /* DMA1 clock and GPIOA clock enable (to be used with DAC) */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1 | RCC_AHB1Periph_GPIOA, ENABLE); /* DAC Periph clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); /* DAC channel 1 & 2 (DAC_OUT1 = PA.4)(DAC_OUT2 = PA.5) configuration */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); /* TIM6 Configuration ------------------------------------------------------*/ TIM6_Config(); /* Configures Key Button */ STM_EVAL_PBInit(BUTTON_KEY, BUTTON_MODE_EXTI); while (1) { /* If the Key is pressed */ if (KeyPressed != RESET) { DAC_DeInit(); /* select waves forms according to the Key Button status */ if (SelectedWavesForm == 1) { /* The sine wave and the escalator wave has been selected */ /* Escalator Wave generator ------------------------------------------*/ DAC_Ch1_EscalatorConfig(); /* Sine Wave generator -----------------------------------------------*/ DAC_Ch2_SineWaveConfig(); } else { /* The triangle wave and the noise wave has been selected */ /* Noise Wave generator ----------------------------------------------*/ DAC_Ch1_NoiseConfig(); /* Triangle Wave generator -------------------------------------------*/ DAC_Ch2_TriangleConfig(); } KeyPressed = RESET; } } }
//红外遥控初始化 //设置IO以及定时器4的输入捕获 void IRRemote_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; TIM_OCInitTypeDef TIM_OCInitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE); //使能PORTB时钟 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE); //TIM4 时钟使能 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PB9 输入 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //上拉输入 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_SetBits(GPIOB,GPIO_Pin_9); //初始化GPIOB.9 TIM_TimeBaseStructure.TIM_Period = 10000; //设定计数器自动重装值 最大10ms溢出 TIM_TimeBaseStructure.TIM_Prescaler =(72-1); //预分频器,1M的计数频率,1us加1. TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式 TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx TIM_ICInitStructure.TIM_Channel = TIM_Channel_4; // 选择输入端 IC4映射到TI4上 TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获 TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频 TIM_ICInitStructure.TIM_ICFilter = 0x03;//IC4F=0011 配置输入滤波器 8个定时器时钟周期滤波 TIM_ICInit(TIM4, &TIM_ICInitStructure);//初始化定时器输入捕获通道 TIM_Cmd(TIM4,ENABLE ); //使能定时器4 NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //TIM3中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占优先级0级 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //从优先级3级 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能 NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器 TIM_ITConfig( TIM4,TIM_IT_Update|TIM_IT_CC4,ENABLE);//允许更新中断 ,允许CC4IE捕获中断 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8,ENABLE); GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); //TIM8基本计数器设置(设置PWM频率) //频率=TIM8_CLK/(ARR+1) TIM_TimeBaseStructure.TIM_Period = 1895; //不分频。PWM频率=72000/1895=38Khz TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); //启用ARR的影子寄存器(直到产生更新事件才更改设置) TIM_ARRPreloadConfig(TIM8, ENABLE); //TIM8_OC1模块设置(设置1通道占空比) TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; TIM_OCInitStructure.TIM_Pulse = (1895+1)/3; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(TIM8, &TIM_OCInitStructure); //启用CCR1寄存器的影子寄存器(直到产生更新事件才更改设置) TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Enable); TIM_Cmd(TIM8, ENABLE); //TIM8_OC通道输出PWM TIM_CtrlPWMOutputs(TIM8, ENABLE); }
/******** 功能:配置PWM_TIMER 为PWM模式输出 计数频率为TIM_FREQ,PWM周期为PWM_PERIOD,初始占空比为0 ********/ void Servo_TIM_PWM_Init(void) { uint16_t CCR_Val = 1500; int PrescalerValue=1; GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; //TIM clock enable //根据TIM选择使能不同的时钟 if((PWM_TIMER == TIM2) || (PWM_TIMER == TIM3)||(PWM_TIMER == TIM4) || (PWM_TIMER == TIM5)||(PWM_TIMER == TIM6) || (PWM_TIMER == TIM7)) { RCC_APB1PeriphClockCmd(PWM_TIMER_CLK, ENABLE); } if((PWM_TIMER != TIM1) && (PWM_TIMER != TIM8)) { RCC_APB2PeriphClockCmd(PWM_TIMER_CLK, ENABLE); } //enable GPIO clocks RCC_APB2PeriphClockCmd( PWM_OUT_PORT_CLK | RCC_APB2Periph_AFIO,ENABLE); //配置相应的GPIO输出 GPIO_InitStructure.GPIO_Pin=PWM_OUT_PIN1|PWM_OUT_PIN2|PWM_OUT_PIN3|PWM_OUT_PIN4; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(PWM_OUT_PORT,&GPIO_InitStructure); //配置TIMER 分频系数 计数周期 工作模式 PrescalerValue= (uint16_t)(SystemCoreClock/TIM_FREQ-1); TIM_TimeBaseStructure.TIM_Period = PWM_PERIOD-1; TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(PWM_TIMER , &TIM_TimeBaseStructure); //PWM模式配置 通道1 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = CCR_Val; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(PWM_TIMER , &TIM_OCInitStructure); TIM_OC1PreloadConfig(PWM_TIMER , TIM_OCPreload_Enable); //通道2 TIM_OCInitStructure.TIM_Pulse = CCR_Val; TIM_OC2Init(PWM_TIMER , &TIM_OCInitStructure); TIM_OC2PreloadConfig(PWM_TIMER , TIM_OCPreload_Enable); //通道3 TIM_OCInitStructure.TIM_Pulse = CCR_Val; TIM_OC3Init(PWM_TIMER , &TIM_OCInitStructure); TIM_OC3PreloadConfig(PWM_TIMER , TIM_OCPreload_Enable); //通道4 TIM_OCInitStructure.TIM_Pulse = CCR_Val; TIM_OC4Init(PWM_TIMER , &TIM_OCInitStructure); TIM_OC4PreloadConfig(PWM_TIMER , TIM_OCPreload_Enable); TIM_ARRPreloadConfig(PWM_TIMER , ENABLE); /* TIM enable counter */ TIM_Cmd(PWM_TIMER , ENABLE); }
OSStatus platform_pwm_init( const platform_pwm_t* pwm, uint32_t frequency, float duty_cycle ) { TIM_TimeBaseInitTypeDef tim_time_base_structure; TIM_OCInitTypeDef tim_oc_init_structure; RCC_ClocksTypeDef rcc_clock_frequencies; uint16_t period = 0; float adjusted_duty_cycle = ( ( duty_cycle > 100.0f ) ? 100.0f : duty_cycle ); OSStatus err = kNoErr; require_action_quiet( pwm != NULL, exit, err = kParamErr); platform_mcu_powersave_disable(); RCC_GetClocksFreq( &rcc_clock_frequencies ); if ( pwm->tim == TIM1 || pwm->tim == TIM8 || pwm->tim == TIM9 || pwm->tim == TIM10 || pwm->tim == TIM11 ) { RCC_APB2PeriphClockCmd( pwm->tim_peripheral_clock, ENABLE ); if( rcc_clock_frequencies.PCLK2_Frequency == rcc_clock_frequencies.HCLK_Frequency ) period = (uint16_t)( rcc_clock_frequencies.PCLK2_Frequency / 20 / frequency - 1 ); /* Auto-reload value counts from 0; hence the minus 1 */ else period = (uint16_t)( rcc_clock_frequencies.PCLK2_Frequency * 2 / 20 / frequency - 1 ); /* Auto-reload value counts from 0; hence the minus 1 */ } else { RCC_APB1PeriphClockCmd( pwm->tim_peripheral_clock, ENABLE ); if( rcc_clock_frequencies.PCLK1_Frequency == rcc_clock_frequencies.HCLK_Frequency ) period = (uint16_t)( rcc_clock_frequencies.PCLK1_Frequency / 20 / frequency - 1 ); /* Auto-reload value counts from 0; hence the minus 1 */ else period = (uint16_t)( rcc_clock_frequencies.PCLK1_Frequency * 2 / 20 / frequency - 1 ); /* Auto-reload value counts from 0; hence the minus 1 */ } /* Set alternate function */ platform_gpio_set_alternate_function( pwm->pin->port, pwm->pin->pin_number, GPIO_OType_PP, GPIO_PuPd_UP, pwm->gpio_af ); /* Time base configuration */ tim_time_base_structure.TIM_Period = (uint32_t) period; tim_time_base_structure.TIM_Prescaler = (uint16_t) 19; /* Divide clock by 19 + 1 to enable a count of high cycle + low cycle = 1 PWM cycle */ tim_time_base_structure.TIM_ClockDivision = 0; tim_time_base_structure.TIM_CounterMode = TIM_CounterMode_Up; tim_time_base_structure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit( pwm->tim, &tim_time_base_structure ); /* PWM1 Mode configuration */ tim_oc_init_structure.TIM_OCMode = TIM_OCMode_PWM1; tim_oc_init_structure.TIM_OutputState = TIM_OutputState_Enable; tim_oc_init_structure.TIM_OutputNState = TIM_OutputNState_Enable; tim_oc_init_structure.TIM_Pulse = (uint16_t) ( adjusted_duty_cycle * (float) period / 100.0f ); tim_oc_init_structure.TIM_OCPolarity = TIM_OCPolarity_High; tim_oc_init_structure.TIM_OCNPolarity = TIM_OCNPolarity_High; tim_oc_init_structure.TIM_OCIdleState = TIM_OCIdleState_Reset; tim_oc_init_structure.TIM_OCNIdleState = TIM_OCIdleState_Set; switch ( pwm->channel ) { case 1: { TIM_OC1Init( pwm->tim, &tim_oc_init_structure ); TIM_OC1PreloadConfig( pwm->tim, TIM_OCPreload_Enable ); break; } case 2: { TIM_OC2Init( pwm->tim, &tim_oc_init_structure ); TIM_OC2PreloadConfig( pwm->tim, TIM_OCPreload_Enable ); break; } case 3: { TIM_OC3Init( pwm->tim, &tim_oc_init_structure ); TIM_OC3PreloadConfig( pwm->tim, TIM_OCPreload_Enable ); break; } case 4: { TIM_OC4Init( pwm->tim, &tim_oc_init_structure ); TIM_OC4PreloadConfig( pwm->tim, TIM_OCPreload_Enable ); break; } default: { break; } } exit: platform_mcu_powersave_enable(); return err; }
void CAN_Close() { CAN_DeInit(); RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN, DISABLE); }
L298::L298() : SimpleMotorController( { L298_Motor_Names[0], L298_Motor_Names[1], L298_Motor_Names[2], L298_Motor_Names[3] }) { // Enable timers { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; // Enable TIM1 RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); TIM_TimeBaseStructure.TIM_Period = MOTOR_MAX_VAL + 1; TIM_TimeBaseStructure.TIM_Prescaler = 31; //PrescalerValue; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); // Enable TIM3 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); TIM_TimeBaseStructure.TIM_Period = MOTOR_MAX_VAL + 1; TIM_TimeBaseStructure.TIM_Prescaler = 15; //PrescalerValue; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); } // init pwm and gpio { TIM_OCInitTypeDef TIM_OCInitStructure; TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; // TIMER 1 // PWM KANAL 1 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable; TIM_OCInitStructure.TIM_Pulse = 0; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Set; TIM_OC1Init(TIM1, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //PWM KANAL2 TIM_OC2Init(TIM1, &TIM_OCInitStructure); TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable); //PWM KANAL3 TIM_OC3Init(TIM1, &TIM_OCInitStructure); TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable); //PWM KANAL 4 TIM_OC4Init(TIM1, &TIM_OCInitStructure); TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable); TIM_ARRPreloadConfig(TIM1, ENABLE); TIM_Cmd(TIM1, ENABLE); TIM_CtrlPWMOutputs(TIM1, ENABLE); // TIMER 3 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; // PWM KANAL 1 TIM_OC1Init(TIM3, &TIM_OCInitStructure); TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable); //PWM KANAL2 TIM_OC2Init(TIM3, &TIM_OCInitStructure); TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); //PWM KANAL3 TIM_OC3Init(TIM3, &TIM_OCInitStructure); TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable); //PWM KANAL 4 TIM_OC4Init(TIM3, &TIM_OCInitStructure); TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable); TIM_ARRPreloadConfig(TIM3, ENABLE); TIM_Cmd(TIM3, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; // init pwm pins for (int i = 0; i < 4; i++) { // EN pin GPIO_InitStructure.GPIO_Pin = motorIOConfig[i].EN_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(motorIOConfig[i].EN_PORT, &GPIO_InitStructure); // IN1 GPIO_InitStructure.GPIO_Pin = motorIOConfig[i].IN1_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(motorIOConfig[i].IN1_PORT, &GPIO_InitStructure); // set alternate function of enable pin to pwm GPIO_PinAFConfig(motorIOConfig[i].IN1_PORT, motorIOConfig[i].IN1_PIN_SOURCE, GPIO_AF_TIM1); /// IN2 GPIO_InitStructure.GPIO_Pin = motorIOConfig[i].IN2_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(motorIOConfig[i].IN2_PORT, &GPIO_InitStructure); // set alternate function of enable pin to pwm GPIO_PinAFConfig(motorIOConfig[i].IN2_PORT, motorIOConfig[i].IN2_PIN_SOURCE, GPIO_AF_TIM3); } } }
/** * @brief Initializes the peripherals used by the I2C FLASH driver. * @param None * @retval : None */ void I2C_FLASH_Init(void) { I2C_InitTypeDef I2C_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; DMA_InitTypeDef DMA_InitStructure; /* Enable peripheral clocks --------------------------------------------------*/ /* Enable I2C1 and I2C2 clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1 , ENABLE); /* Enable DMA1 clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); /* Enable GPIOB clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); /* Configure I2C1 pins: SCL and SDA ----------------------------------------*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_Init(GPIOB, &GPIO_InitStructure); /*Reset the configuration of the I2C interface*/ I2C_DeInit(I2C1); /* DMA1 channel6 configuration ----------------------------------------------*/ DMA_DeInit(DMA1_Channel6); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)I2C1_DR_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)I2C1_Buffer_Tx; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_BufferSize = BufferSize; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel6, &DMA_InitStructure); /* Enable I2C1 DMA */ /* DMA1 channel7 configuration ---------------------------------------------*/ DMA_DeInit(DMA1_Channel7); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)I2C1_DR_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)I2C1_Buffer_Rx; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = BufferSize; DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; DMA_Init(DMA1_Channel7, &DMA_InitStructure); /* I2C1 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 = ClockSpeed; I2C_Init(I2C1, &I2C_InitStructure); /* Enable I2C ------------------------------------------------------------*/ I2C_Cmd(I2C1, ENABLE); I2C_DMACmd(I2C1, ENABLE); }