uint16_t AdcMcuRead( Adc_t *obj, uint8_t channel ) { uint16_t adcData = 0; /* Enable The HSI (16Mhz) */ RCC_HSICmd( ENABLE ); /* Check that HSI oscillator is ready */ while( RCC_GetFlagStatus( RCC_FLAG_HSIRDY ) == RESET ); RCC_APB2PeriphClockCmd( RCC_APB2Periph_ADC1, ENABLE ); // Temperature or Vref measurement if( ( channel == ADC_Channel_16 ) || ( channel == ADC_Channel_17 ) ) { // Yes, enable temperature sensor and internal reference voltage ADC_TempSensorVrefintCmd( ENABLE ); } // Configure selected channel ADC_RegularChannelConfig( ADC1, channel, 1, ADC_SampleTime_192Cycles ); /* Define delay between ADC1 conversions */ ADC_DelaySelectionConfig( ADC1, ADC_DelayLength_Freeze ); /* Enable ADC1 Power Down during Delay */ ADC_PowerDownCmd( ADC1, ADC_PowerDown_Idle_Delay, ENABLE ); /* Enable ADC1 */ ADC_Cmd( ADC1, ENABLE ); /* Wait until ADC1 ON status */ while( ADC_GetFlagStatus( ADC1, ADC_FLAG_ADONS ) == RESET ) { } /* Start ADC1 Software Conversion */ ADC_SoftwareStartConv( ADC1 ); /* Wait until ADC Channel 5 or 1 end of conversion */ while( ADC_GetFlagStatus( ADC1, ADC_FLAG_EOC ) == RESET ) { } adcData = ADC_GetConversionValue( ADC1 ); ADC_Cmd( ADC1, DISABLE ); if( ( channel == ADC_Channel_16 ) || ( channel == ADC_Channel_17 ) ) { // De-initialize ADC ADC_TempSensorVrefintCmd( DISABLE ); } RCC_APB2PeriphClockCmd( RCC_APB2Periph_ADC1, DISABLE ); RCC_HSICmd( DISABLE ); return adcData; }
static void rng_seed() { ADC_InitTypeDef ADC_InitStructure; ot_u16 ADCdata; int n; /* Enable The HSI (16Mhz) */ RCC_HSICmd(ENABLE); // ADC can only use HSI? ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = ENABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = 1; ADC_Init(ADC1, &ADC_InitStructure); ADC_RegularChannelConfig(ADC1, ADC_Channel_TempSensor, 1, ADC_SampleTime_4Cycles); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Wait until ADC1 ON status */ while (ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET) { asm("nop"); } /* Start ADC1 Software Conversion */ ADC_SoftwareStartConv(ADC1); r = 0; for (n = 0; n < 30; n++) { /* Wait until end of conversion */ while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET) asm("nop"); /* Read ADC conversion result */ ADCdata = ADC_GetConversionValue(ADC1); r += ADCdata & 0x07; // take lower noise bits } //debug_printf("r: %x\r\n", r); ADC_Cmd(ADC1, DISABLE); RCC_HSICmd(DISABLE); // assuming HSI not used }
/** * @brief Configures the different system clocks. * @param None * @retval None */ void RCC_Configuration(void) { /* Enable HSI Clock */ RCC_HSICmd(ENABLE); /*!< Wait till HSI is ready */ while (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET) {} RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI); RCC_MSIRangeConfig(RCC_MSIRange_6); RCC_HSEConfig(RCC_HSE_OFF); if(RCC_GetFlagStatus(RCC_FLAG_HSERDY) != RESET ) { while(1); } /* Enable comparator clock LCD and PWR mngt */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_LCD | RCC_APB1Periph_PWR, ENABLE); /* Enable ADC clock & SYSCFG */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_SYSCFG, ENABLE); }
/** * @brief Configures the ADC1 channel5. * @param None * @retval None */ void ADC_Config(void) { /* Enable The HSI (16Mhz) */ RCC_HSICmd(ENABLE); /* Enable the GPIOF or GPIOA Clock */ RCC_AHBPeriphClockCmd(IDD_MEASUREMENT_GPIO_CLK, ENABLE); /* Configure PF.11 (ADC Channel11) or PA.05 (ADC Channe5) in analog mode */ GPIO_InitStructure.GPIO_Pin = IDD_MEASUREMENT_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(IDD_MEASUREMENT_GPIO, &GPIO_InitStructure); /* Check that HSI oscillator is ready */ while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET); /* ADC1 Configuration ------------------------------------------------------*/ /* Enable ADC1 clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); #ifdef USE_STM32L152D_EVAL /* Select ADC Bank channel */ ADC_BankSelection(ADC1, ADC_Bank_B); #endif ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = 1; ADC_Init(ADC1, &ADC_InitStructure); /* ADC1 regular channel5 or channel1 configuration */ ADC_RegularChannelConfig(ADC1, IDD_MEASUREMENT_ADC_CHANNEL, 1, ADC_SampleTime_192Cycles); /* Define delay between ADC1 conversions */ ADC_DelaySelectionConfig(ADC1, ADC_DelayLength_Freeze); /* Enable ADC1 Power Down during Delay */ ADC_PowerDownCmd(ADC1, ADC_PowerDown_Idle_Delay, ENABLE); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Wait until ADC1 ON status */ while (ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET) { } /* Start ADC1 Software Conversion */ ADC_SoftwareStartConv(ADC1); /* Wait until ADC Channel 5 or 1 end of conversion */ while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET) { } }
void Clk_Init (void) { // 1. Cloking the controller from internal HSI RC (8 MHz) RCC_HSICmd(ENABLE); // wait until the HSI is ready while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET); RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI); // 2. Enable ext. high frequency OSC RCC_HSEConfig(RCC_HSE_ON); // wait until the HSE is ready while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET); // 3. Init PLL RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9); // 72MHz // RCC_PLLConfig(RCC_PLLSource_HSE_Div2,RCC_PLLMul_9); // 72MHz RCC_PLLCmd(ENABLE); // wait until the PLL is ready while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); // 4. Set system clock divders RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5); RCC_ADCCLKConfig(RCC_PCLK2_Div8); RCC_PCLK2Config(RCC_HCLK_Div1); RCC_PCLK1Config(RCC_HCLK_Div2); RCC_HCLKConfig(RCC_SYSCLK_Div1); // Flash 1 wait state *(vu32 *)0x40022000 = 0x12; // 5. Clock system from PLL RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); }
//---------------------------------------------------------------------------- void RCC_Configuration(void) { unsigned int startDelay; RCC_DeInit(); //-- RCC system reset(for debug purpose) RCC_HSICmd(ENABLE); RCC_HSEConfig(RCC_HSE_OFF); // delay_40ms startDelay = 960000; while (startDelay) startDelay--; //-- Enable Prefetch Buffer FLASH_PrefetchBufferCmd(ENABLE); FLASH_SetLatency(FLASH_Latency_3); //-- Flash 3 wait state for 120MHz //-- PLLCLK = 16MHz/16 * 240/2 = 120 MHz RCC_PLLCmd(DISABLE); RCC_HCLKConfig (RCC_SYSCLK_Div1 ) ; RCC_PCLK1Config ( RCC_HCLK_Div4) ; RCC_PCLK2Config ( RCC_HCLK_Div2) ; RCC_PLLConfig(RCC_PLLSource_HSI, TPLL_M, TPLL_N, TPLL_P, TPLL_Q); // // //-- Enable PLL & wait till PLL is ready RCC_PLLCmd(ENABLE); while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); // startDelay = 240000; // while (startDelay) startDelay--; // //-- Select PLL as system clock source & wait till PLL is used // //-- as system clock source startDelay = 10000; while (startDelay) startDelay--; RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); // while(RCC_GetSYSCLKSource() != 0x08); // // RCC_ADCCLKConfig(RCC_PCLK2_Div4); startDelay = RCC_GetSYSCLKSource() ; /* DMA clock enable */ RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_DMA1 | RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_BKPSRAM, ENABLE); /* Enable USART2 clock */ // uart4 rs-485, uart6 irps RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART3 | RCC_APB1Periph_UART4 | RCC_APB1Periph_PWR | RCC_APB1Periph_TIM2, ENABLE); /* Enable USART1, GPIOA, GPIOx and AFIO clocks */ RCC_APB2PeriphClockCmd( RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2 | RCC_APB2Periph_USART1 , ENABLE); RCC_AHB3PeriphClockCmd ( RCC_AHB3Periph_FSMC , ENABLE); }
/** * @brief To select HSI as System clock source * @caller ADC_Icc_Test * @param None * @retval None */ void SetHSICLK(void) { /* Enable HSI Clock */ RCC_HSICmd(ENABLE); /*!< Wait till HSI is ready */ while (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET); /* Enable 64-bit access */ FLASH_ReadAccess64Cmd(ENABLE); /* Enable Prefetch Buffer */ FLASH_PrefetchBufferCmd(ENABLE); /* Flash 1 wait state */ FLASH_SetLatency(FLASH_Latency_1); RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI); while (RCC_GetSYSCLKSource() != 0x04); RCC_HCLKConfig(RCC_SYSCLK_Div1); /* PCLK2 = HCLK */ RCC_PCLK2Config(RCC_HCLK_Div1); /* PCLK1 = HCLK */ RCC_PCLK1Config(RCC_HCLK_Div1); }
void flash_init() { RCC_HSICmd(ENABLE); FLASH_Unlock(); FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP|FLASH_FLAG_PGERR |FLASH_FLAG_WRPRTERR); }
/** * @brief ADC configuration for automatic IDD measurement. * @param None * @retval None */ void IDD_Measurement_ADC_Config(void) { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* Enable GPIOA Clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); /* Configure IDD Measurement pin (ADC Channelxx) as analog input -----------*/ GPIO_InitStructure.GPIO_Pin = IDD_MEASUREMENT_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_Init(IDD_MEASUREMENT_GPIO_PORT, &GPIO_InitStructure); /* ADC1 configuration --------------------------------------------------------*/ /* Enable HSI clock for ADC clock */ RCC_HSICmd(ENABLE); /*!< Wait till HSI is ready */ while (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET) {} /* Enable ADC clock ----------------------------------------------------------*/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* de-initialize ADC ---------------------------------------------------------*/ ADC_DeInit(ADC1); /* ADC configured as follows: - NbrOfChannel = 1 - ADC_Channel_5 - Mode = Single ConversionMode(ContinuousConvMode Enabled) - Resolution = 12Bits - Prescaler = /1 - Sampling time 192 */ /* ADC Configuration */ ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = 1; ADC_Init(ADC1, &ADC_InitStructure); /* ADC1 regular channel4 configuration */ ADC_RegularChannelConfig(ADC1, IDD_MEASUREMENT_ADC_CHANNEL, 1, ADC_SampleTime_192Cycles); ADC_DelaySelectionConfig(ADC1, ADC_DelayLength_Freeze); ADC_PowerDownCmd(ADC1, ADC_PowerDown_Idle_Delay, ENABLE); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Wait until ADC1 ON status */ while (ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET) { } }
void initClocks() { // enable HSI and set it as the system clock source RCC_HSICmd(ENABLE); while(!(RCC->CR & RCC_CR_HSIRDY)); // wait for it to be ready RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI); // disable PLL and PLLI2S RCC_PLLCmd(DISABLE); RCC_PLLI2SCmd(DISABLE); // disable HSE and CSS (disabling the HSE also disables CSS) RCC_HSEConfig(RCC_HSE_OFF); // Configure PLL values and set source to HSE RCC_PLLConfig( RCC_PLLSource_HSE, DESIRED_PLL_M, DESIRED_PLL_N, DESIRED_PLL_P, DESIRED_PLL_Q ); // set PLL I2S to our new values RCC_PLLI2SConfig( DESIRED_PLL_I2S_N, DESIRED_PLL_I2S_R ); // set I2S clock source to PLLI2S RCC_I2SCLKConfig(RCC_I2S2CLKSource_PLLI2S); // set AHB, APB1, APB2 prescalers RCC_HCLKConfig(DESIRED_HCLK_DIV); RCC_PCLK1Config(DESIRED_PCLK1_DIV); RCC_PCLK2Config(DESIRED_PCKL2_DIV); // enable HSE RCC_HSEConfig(RCC_HSE_ON); if(RCC_WaitForHSEStartUp() == ERROR) RCC_DeInit(); // SHUT DOWN, EVERYTHING! // enable CSS RCC_ClockSecuritySystemCmd(ENABLE); // enable PLL RCC_PLLCmd(ENABLE); while(!(RCC->CR & RCC_CR_PLLRDY)); // wait for ready // enable PLL I2S RCC_PLLI2SCmd(ENABLE); // set system clock source to PLL RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); if(!checkClocks()){ // No actual error reporting done here because we don't want to depend on GPIO etc while(1); } }
void RCC_Config_HSI_default(void){ RCC_DeInit(); /*configurar clock interno a 8Mhz*/ RCC_HSICmd(ENABLE); // ou RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_2); RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI); //garantir que o clock está correcto while(RCC_GetSYSCLKSource()!=0x00); }
/** * @brief To select MSI as System clock source * @caller ADC_Icc_Test * @param Frequence, DIV by 2 ot not , With or without RTC * @retval None */ void SetHSICLKToMSI(uint32_t freq,bool div2,bool With_RTC) { /* RCC system reset */ RCC_DeInit(); /* Flash 1 wait state */ FLASH_SetLatency(FLASH_Latency_0); /* Disable Prefetch Buffer */ FLASH_PrefetchBufferCmd(DISABLE); /* Disable 64-bit access */ FLASH_ReadAccess64Cmd(DISABLE); /* Disable FLASH during SLeep */ FLASH_SLEEPPowerDownCmd(ENABLE); /* Enable the PWR APB1 Clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); /* Select the Voltage Range 3 (1.2V) */ PWR_VoltageScalingConfig(PWR_VoltageScaling_Range3); /* Wait Until the Voltage Regulator is ready */ while (PWR_GetFlagStatus(PWR_FLAG_VOS) != RESET) {} /* To configure the MSI frequency */ RCC_MSIRangeConfig(freq); /* Select MSI as system clock source */ RCC_SYSCLKConfig(RCC_SYSCLKSource_MSI); /* Wait till MSI is used as system clock source */ while (RCC_GetSYSCLKSource() != 0x00) {} if (div2) { RCC_HCLKConfig(RCC_SYSCLK_Div2); } RCC_HSICmd(DISABLE); /* Disable HSE clock */ RCC_HSEConfig(RCC_HSE_OFF); /* Disable LSE clock */ if (! With_RTC) RCC_LSEConfig(RCC_LSE_OFF); /* Disable LSI clock */ RCC_LSICmd(DISABLE); }
/** * @brief Restore peripheral config before entering STOP mode. * @param None * @retval None */ void RestoreConfiguration(void) { /* Restore system clock to 32MHz */ SetSysClock(); /* Enable HSI clock for ADC */ RCC_HSICmd(ENABLE); /* LSI Enable */ RCC_LSICmd(ENABLE); }
void initClock(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_SYSCFG , ENABLE); // divide HCLK / 2 RCC_HCLKConfig(RCC_SYSCLK_Div2); // enable HSI RCC_HSICmd(ENABLE); RCC_PLLCmd(DISABLE); // wait for HSI to get ready while (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET); // configure PLL - x4 /2 RCC_PLLConfig( RCC_PLLSource_HSI, RCC_PLLMul_8, RCC_PLLDiv_4 ); RCC_PLLCmd(ENABLE); while ( RCC_GetFlagStatus( RCC_FLAG_PLLRDY ) == RESET ); RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); RCC_ClocksTypeDef RCC_Clocks; RCC_GetClocksFreq(&RCC_Clocks); SysTick_Config((RCC_Clocks.SYSCLK_Frequency / 2) / 1000); // Cannot exceed 16,777,215 /* Set SysTick Preemption Priority, it's a system handler rather than a regular interrupt */ NVIC_SetPriority(SysTick_IRQn, 0x04); // setup lse for rtc PWR_RTCAccessCmd(ENABLE); // Reset RTC Backup Domain RCC_RTCResetCmd(ENABLE); RCC_RTCResetCmd(DISABLE); // LSE Enable RCC_LSEConfig(RCC_LSE_ON); // Wait until LSE is ready while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET); // RTC Clock Source Selection RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); // Enable the RTC RCC_RTCCLKCmd(ENABLE); RTC_WaitForSynchro(); }
/** * @brief ADC1初始化 * @param 无 * @retval 无 */ void ADC1_Init(void) { /* Enable the HSI oscillator */ RCC_HSICmd(ENABLE); //ADC使用 /* Check that HSI oscillator is ready */ while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET); ADC1_BATTEST_GPIO_Config(); ADC1_BATTEST_Mode_Config(); ADC1_GPIO_Config(); ADC1_Mode_Config(); }
/** * @brief Configures the different system clocks. * @param None * @retval None */ void RCC_Configuration(void) { RCC_HSICmd(ENABLE);//Enable HSI Clock while (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET);//!< Wait till HSI is ready RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI); //Set HSI as sys clock RCC_MSIRangeConfig(RCC_MSIRange_6);//Set MSI clock range to ~4.194MHz*/ RCC_HSEConfig(RCC_HSE_OFF); /*Disable HSE*/ if(RCC_GetFlagStatus(RCC_FLAG_HSERDY) != RESET ) { while(1); //Stay in infinite loop if HSE is not disabled*/ } }
void ClkSwitch2HseSystemInit (void) { ErrorStatus HSEStartUpStatus; /* RCC system reset(for debug purpose) */ RCC_DeInit(); RCC_HSICmd(DISABLE); //Turn of the internal RC; /* Enable HSE */ RCC_HSEConfig(RCC_HSE_ON); /* Wait till HSE is ready */ HSEStartUpStatus = RCC_WaitForHSEStartUp(); if(HSEStartUpStatus == SUCCESS) { /* Enable Prefetch Buffer */ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); /* Flash 2 wait state */ FLASH_SetLatency(FLASH_Latency_2); /* HCLK = SYSCLK */ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* PCLK2 = HCLK */ RCC_PCLK2Config(RCC_HCLK_Div1); /* PCLK1 = HCLK/2 */ RCC_PCLK1Config(RCC_HCLK_Div2); /* PLLCLK = 10MHz * 7 = 70 MHz */ RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_7); /* Enable PLL */ RCC_PLLCmd(ENABLE); /* Wait till PLL is ready */ while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) { } /* Select PLL as system clock source */ RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); /* Wait till PLL is used as system clock source */ while(RCC_GetSYSCLKSource() != 0x08) { } } }
/** * @brief Configures the different system clocks. * @param None * @retval None */ void RCC_Configuration(void) { /* Enable HSI Clock */ RCC_HSICmd(ENABLE); /*!< Wait till HSI is ready */ while (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET) {} RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI); RCC_MSIRangeConfig(RCC_MSIRange_6); /* Enable the GPIOs Clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB | RCC_AHBPeriph_GPIOC| RCC_AHBPeriph_GPIOD| RCC_AHBPeriph_GPIOE| RCC_AHBPeriph_GPIOH, ENABLE); /* Enable comparator clock LCD and PWR mngt */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_COMP | RCC_APB1Periph_LCD | RCC_APB1Periph_PWR,ENABLE); /* Enable ADC clock & SYSCFG */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_SYSCFG , ENABLE); /* Allow access to the RTC */ PWR_RTCAccessCmd(ENABLE); /* Reset Backup Domain */ RCC_RTCResetCmd(ENABLE); RCC_RTCResetCmd(DISABLE); /* LSE Enable */ RCC_LSEConfig(RCC_LSE_ON); /* Wait till LSE is ready */ while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) {} RCC_RTCCLKCmd(ENABLE); /* LCD Clock Source Selection */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); RCC_HSEConfig(RCC_HSE_OFF); if(RCC_GetFlagStatus(RCC_FLAG_HSERDY) != RESET ) { while(1); } }
static void RCC_Configuration(void) { /* Enable GPIOB clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); /* Enable DMA1 clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); /* Enable the HSI oscillator */ RCC_HSICmd(ENABLE); /* Enable ADC1 clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); }
/** ****************************************************************** * @brief Initialise FLASH access capability * * @note This library requires HSI to be enabled, and will enable * it in this INIT. * * @retval HW_NVM_OK Flash initialised succesfully * @retval HW_NVM_INIT_FAILED Flash initialisation failed ******************************************************************* */ hw_nvm_ret_t hw_STM32F0_FLASH_drv_Init(void) { uint32_t i; //enable necessary hardware RCC_HSICmd(ENABLE); // Enable HSI (high-speed internal oscillator) for(i=0;i<HW_NVM_INIT_RETRIES;i++){ if(RCC_GetFlagStatus(RCC_CR_HSIRDY) != SET) break; } //return result return (i == HW_NVM_INIT_RETRIES) ? HW_NVM_INIT_FAILED : HW_NVM_OK ; }
void fdi_clock_start_high_speed_internal(void) { RCC_HSICmd(ENABLE); while (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET); RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI); FLASH_SetLatency(FLASH_Latency_2); RCC_PLLCmd(DISABLE); RCC_PLLConfig(RCC_PLLSource_HSI, 16, 336, 4, 7); // SYSCLK 84MHz & USB 48MHz RCC_PLLCmd(ENABLE); while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); }
/** * @brief Samples, converts and returns the voltage on the specified ADC channel. * @param xAdcCh ADC channel. * This parameter can be any value of @ref SdkEvalAdcChannel. * @retval uint16_t Converted voltage in an unsiged integer 16-bit format. */ uint16_t SdkEvalPmGetV(SdkEvalAdcChannel xAdcCh) { uint16_t convValue=2785; if( !SdkEvalGetVersion() ){ /* ADC1 channel configuration */ ADC_RegularChannelConfig(ADC1, xAdcCh, 1, ADC_SampleTime_48Cycles); /* Enables HSI clock */ RCC_HSICmd(ENABLE); /* Enables ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Waits until the ADC1 is ready */ while(ADC_GetFlagStatus(ADC1, ADC_FLAG_ADONS) == RESET); /* Start ADC1 Software Conversion */ ADC_SoftwareStartConv(ADC1); /* Waits until the end of conversion */ while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); /* Gets the ADC conversion value */ convValue = ADC_GetConversionValue(ADC1); /* Disables ADC */ ADC_Cmd(ADC1, DISABLE); /* Disables HSI clock */ RCC_HSICmd(DISABLE); } /* Return the converted value */ return convValue; }
/** * @brief Initializes the MCO1 (pin PA8) as XCLK for OV7670 camera module * @note HSI (16MHz) is used as clock source for MCO1 directly (without prescaler) * @param None * @retval ERROR if HSI startup is failed * SUCESS if HSI startup is correct */ static ErrorStatus OV7670_XCLK_Conf(void) { __IO uint32_t startupcounter = 0; ErrorStatus status = ERROR; FlagStatus HSI_Status = RESET; GPIO_InitTypeDef GPIO_InitStructure; // Enable high speed internal 16MHz oscillator ******************************* RCC_HSICmd(ENABLE); // Wait till HSI is ready and if Time out is reached exit do { HSI_Status = RCC_GetFlagStatus(RCC_FLAG_HSIRDY); startupcounter++; }while((startupcounter != HSI_STARTUP_TIMEOUT) && (HSI_Status == RESET)); if (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) != RESET) { status = SUCCESS; } else { status = ERROR; } // Output HSI clock on MCO1 pin(PA8) **************************************** // Enable the GPIOA peripheral RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); // Connect MCO1 pin to AF0 // Connect to AF0 is default after reset // GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_MCO); // Configure MCO1 pin(PA8) in alternate function GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); // HSI clock selected to output on MCO1 pin(PA8) RCC_MCO1Config(RCC_MCO1Source_HSI, RCC_MCO1Div_1); return(status); }
void RCC_Internal_Configuration(void) { ErrorStatus HSEStartUpStatus; //将外设 RCC寄存器重设为缺省值 RCC_DeInit(); RCC_HSICmd(ENABLE); while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET); if(HSEStartUpStatus == SUCCESS) { RCC_HCLKConfig(RCC_SYSCLK_Div1); RCC_PCLK2Config(RCC_HCLK_Div1); RCC_PCLK1Config(RCC_HCLK_Div2); //设置 PLL 时钟源及倍频系数 RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_2); //使能或者失能 PLL,这个参数可以取:ENABLE或者DISABLE RCC_PLLCmd(ENABLE);//如果PLL被用于系统时钟,那么它不能被失能 //等待指定的 RCC 标志位设置成功 等待PLL初始化成功 while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); //设置系统时钟(SYSCLK) 设置PLL为系统时钟源 RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //等待PLL成功用作于系统时钟的时钟源 // 0x00:HSI 作为系统时钟 // 0x04:HSE作为系统时钟 // 0x08:PLL作为系统时钟 while(RCC_GetSYSCLKSource() != 0x08); } }
void EnableClock() { // divide HCLK / 2 RCC_HCLKConfig(RCC_SYSCLK_Div2); // enable HSI RCC_HSICmd(ENABLE); RCC_PLLCmd(DISABLE); // wait for HSI to get ready while (RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET); // configure PLL - x4 /2 RCC_PLLConfig( RCC_PLLSource_HSI, RCC_PLLMul_8, RCC_PLLDiv_4 ); RCC_PLLCmd(ENABLE); while ( RCC_GetFlagStatus( RCC_FLAG_PLLRDY ) == RESET ); // set hsi as clock //RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI); // set pll as clock RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); // setup lse for rtc and lcd /* Allow access to the RTC */ PWR_RTCAccessCmd(ENABLE); /* Reset RTC Backup Domain */ RCC_RTCResetCmd(ENABLE); RCC_RTCResetCmd(DISABLE); /* LSE Enable */ RCC_LSEConfig(RCC_LSE_ON); /* Wait until LSE is ready */ while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET); /* RTC Clock Source Selection */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); /* Enable the RTC */ RCC_RTCCLKCmd(ENABLE); RTC_WaitForSynchro(); }
int mote_main(void) { uint8_t status; board_init(); RCC_HSICmd(ENABLE); flash_init(); /* ******************************************************* make page 62~255 becoming written protection by setting WRP3 as WRP3_VALUE or non written protection by setting nWRP3_VALUE *******************************************************/ flash_erase_optByte(); //if you want to add written protection on page 62~255, replace 0xFF by 0x7F status = flash_write_optByte(WRP3_ADDRESS,0xFF); /****************************************************** if non-written protection, write EUI64 to page 255, then make the page written protection ********************************************************/ // checking status of page 62~255 if(flash_read_optByte(WRP3_ADDRESS)&0x80) { // no written protection flash_erasePage(PAGE255_ADDRESS); flash_write(ID_ADDRESS, HEADBYTE_FIR); flash_write(ID_ADDRESS+2,HEADBYTE_SEC); flash_write(ID_ADDRESS+4,HEADBYTE_THR); flash_write(ID_ADDRESS+6,HEADBYTE_FOU); // make page 62~255 written protection flash_erase_optByte(); status = flash_write_optByte(WRP3_ADDRESS,0x7F); // check writing status if(status == 0x04) leds_sync_on(); } else { leds_error_on(); } while (1) { board_sleep(); } }
void vHardwareInit() { // Enable HSE: RCC_HSEConfig(RCC_HSE_ON); // Wait for HSE to be ready: while (RCC_WaitForHSEStartUp() != SUCCESS); // Set PLL to be 9 * HSE = 72 MHz: RCC_PLLCmd(DISABLE); RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); RCC_PLLCmd(ENABLE); // Wait for PLL to be ready: while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) != SET); // Two wait states, if 48 MHz < SYSCLK <= 72 MHz: FLASH_SetLatency(FLASH_Latency_2); // Set PLL as system clock: RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); // Disable HSI: RCC_HSICmd(DISABLE); // Set APB low-speed clock (PCLK1), divide by 2: RCC_PCLK1Config(RCC_HCLK_Div2); // Set APB high-speed clock (PCLK2), do not divide: RCC_PCLK2Config(RCC_HCLK_Div1); // Set AHB clock (HCLK), do not divide: RCC_HCLKConfig(RCC_SYSCLK_Div1); // 3 bits for pre-emption priority 1 bits for subpriority: NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3); // Set core clock as SYSTICK source: SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK); #ifdef RAM_BOOT // Put vector interrupt table in RAM: NVIC_SetVectorTable(NVIC_VectTab_RAM, SCB_VTOR_TBLBASE); #endif }
void Startup::clkInit() { // Set STKALIGN in NVIC SCB->CCR |= 0x200; // 1. Clocking the controller from internal HSI RC (8 MHz) RCC_HSICmd(ENABLE); // wait until the HSI is ready while(RCC_GetFlagStatus(RCC_FLAG_HSIRDY) == RESET); RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI); // 2. Enable ext. high frequency OSC RCC_HSEConfig(RCC_HSE_ON); // wait until the HSE is ready while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET); // 3. Init PLL RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9); // 72MHz // RCC_PLLConfig(RCC_PLLSource_HSE_Div2,RCC_PLLMul_9); // 72MHz RCC_PLLCmd(ENABLE); // wait until the PLL is ready while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); // 4. Set system clock dividers RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5); RCC_ADCCLKConfig(RCC_PCLK2_Div8); RCC_PCLK2Config(RCC_HCLK_Div1); RCC_PCLK1Config(RCC_HCLK_Div2); RCC_HCLKConfig(RCC_SYSCLK_Div1); // Flash 1 wait state // *(vu32 *)0x40022000 = 0x12; // *(vu32 *)0x40022000 = 0x01; // Enable Prefetch Buffer FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); // Flash 2 wait state FLASH_SetLatency(FLASH_Latency_2); // Select PLL as system clock source RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); // Wait till PLL is used as system clock source while(RCC_GetSYSCLKSource() != 0x08) { } }
void RtcRecoverMcuStatus( void ) { if( TimerGetLowPowerEnable( ) == true ) { if( ( LowPowerDisableDuringTask == false ) && ( RtcTimerEventAllowsLowPower == true ) ) { // Disable IRQ while the MCU is not running on HSI __disable_irq( ); /* After wake-up from STOP reconfigure the system clock */ /* Enable HSI */ RCC_HSICmd( ENABLE ); /* Wait till HSI is ready */ while( RCC_GetFlagStatus( RCC_FLAG_HSIRDY ) == RESET ) {} /* Enable PLL */ RCC_PLLCmd( ENABLE ); /* Wait till PLL is ready */ while( RCC_GetFlagStatus( RCC_FLAG_PLLRDY ) == RESET ) {} /* Select PLL as system clock source */ RCC_SYSCLKConfig( RCC_SYSCLKSource_PLLCLK ); /* Wait till PLL is used as system clock source */ while( RCC_GetSYSCLKSource( ) != 0x0C ) {} /* Set MCU in ULP (Ultra Low Power) */ PWR_UltraLowPowerCmd( DISABLE ); // add up to 3ms wakeup time /* Enable the Power Voltage Detector */ PWR_PVDCmd( ENABLE ); BoardInitMcu( ); __enable_irq( ); } } }
//=============================================== void RCC_Configuration(void) { FLASH_PrefetchBufferCmd(ENABLE); FLASH_SetLatency(FLASH_ACR_LATENCY_1); RCC_DeInit(); RCC_HSICmd(ENABLE); RCC_HCLKConfig(RCC_SYSCLK_Div1); RCC_PCLK1Config(RCC_HCLK_Div1); RCC_PCLK2Config(RCC_HCLK_Div1); RCC_ADCCLKConfig(RCC_PCLK2_Div6); RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_6); RCC_PLLCmd(ENABLE); /* Wait till PLL is ready */ while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) {} /* Select PLL as system clock source */ RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); /* Wait till PLL is used as system clock source */ while (RCC_GetSYSCLKSource() != 0x08) {}; };