CARME-M4 BSP  V1.5
stm32f4xx_rcc.c
Go to the documentation of this file.
1 
58 /* Includes ------------------------------------------------------------------*/
59 #include "stm32f4xx_rcc.h"
60 
70 /* Private typedef -----------------------------------------------------------*/
71 /* Private define ------------------------------------------------------------*/
72 /* ------------ RCC registers bit address in the alias region ----------- */
73 #define RCC_OFFSET (RCC_BASE - PERIPH_BASE)
74 /* --- CR Register ---*/
75 /* Alias word address of HSION bit */
76 #define CR_OFFSET (RCC_OFFSET + 0x00)
77 #define HSION_BitNumber 0x00
78 #define CR_HSION_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (HSION_BitNumber * 4))
79 /* Alias word address of CSSON bit */
80 #define CSSON_BitNumber 0x13
81 #define CR_CSSON_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (CSSON_BitNumber * 4))
82 /* Alias word address of PLLON bit */
83 #define PLLON_BitNumber 0x18
84 #define CR_PLLON_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (PLLON_BitNumber * 4))
85 /* Alias word address of PLLI2SON bit */
86 #define PLLI2SON_BitNumber 0x1A
87 #define CR_PLLI2SON_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (PLLI2SON_BitNumber * 4))
88 
89 /* Alias word address of PLLSAION bit */
90 #define PLLSAION_BitNumber 0x1C
91 #define CR_PLLSAION_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (PLLSAION_BitNumber * 4))
92 
93 /* --- CFGR Register ---*/
94 /* Alias word address of I2SSRC bit */
95 #define CFGR_OFFSET (RCC_OFFSET + 0x08)
96 #define I2SSRC_BitNumber 0x17
97 #define CFGR_I2SSRC_BB (PERIPH_BB_BASE + (CFGR_OFFSET * 32) + (I2SSRC_BitNumber * 4))
98 
99 /* --- BDCR Register ---*/
100 /* Alias word address of RTCEN bit */
101 #define BDCR_OFFSET (RCC_OFFSET + 0x70)
102 #define RTCEN_BitNumber 0x0F
103 #define BDCR_RTCEN_BB (PERIPH_BB_BASE + (BDCR_OFFSET * 32) + (RTCEN_BitNumber * 4))
104 /* Alias word address of BDRST bit */
105 #define BDRST_BitNumber 0x10
106 #define BDCR_BDRST_BB (PERIPH_BB_BASE + (BDCR_OFFSET * 32) + (BDRST_BitNumber * 4))
107 
108 /* --- CSR Register ---*/
109 /* Alias word address of LSION bit */
110 #define CSR_OFFSET (RCC_OFFSET + 0x74)
111 #define LSION_BitNumber 0x00
112 #define CSR_LSION_BB (PERIPH_BB_BASE + (CSR_OFFSET * 32) + (LSION_BitNumber * 4))
113 
114 /* --- DCKCFGR Register ---*/
115 /* Alias word address of TIMPRE bit */
116 #define DCKCFGR_OFFSET (RCC_OFFSET + 0x8C)
117 #define TIMPRE_BitNumber 0x18
118 #define DCKCFGR_TIMPRE_BB (PERIPH_BB_BASE + (DCKCFGR_OFFSET * 32) + (TIMPRE_BitNumber * 4))
119 /* ---------------------- RCC registers bit mask ------------------------ */
120 /* CFGR register bit mask */
121 #define CFGR_MCO2_RESET_MASK ((uint32_t)0x07FFFFFF)
122 #define CFGR_MCO1_RESET_MASK ((uint32_t)0xF89FFFFF)
123 
124 /* RCC Flag Mask */
125 #define FLAG_MASK ((uint8_t)0x1F)
126 
127 /* CR register byte 3 (Bits[23:16]) base address */
128 #define CR_BYTE3_ADDRESS ((uint32_t)0x40023802)
129 
130 /* CIR register byte 2 (Bits[15:8]) base address */
131 #define CIR_BYTE2_ADDRESS ((uint32_t)(RCC_BASE + 0x0C + 0x01))
132 
133 /* CIR register byte 3 (Bits[23:16]) base address */
134 #define CIR_BYTE3_ADDRESS ((uint32_t)(RCC_BASE + 0x0C + 0x02))
135 
136 /* BDCR register base address */
137 #define BDCR_ADDRESS (PERIPH_BASE + BDCR_OFFSET)
138 
139 /* Private macro -------------------------------------------------------------*/
140 /* Private variables ---------------------------------------------------------*/
141 static __I uint8_t APBAHBPrescTable[16] = {0, 0, 0, 0, 1, 2, 3, 4, 1, 2, 3, 4, 6, 7, 8, 9};
142 
143 /* Private function prototypes -----------------------------------------------*/
144 /* Private functions ---------------------------------------------------------*/
145 
213 void RCC_DeInit(void)
214 {
215  /* Set HSION bit */
216  RCC->CR |= (uint32_t)0x00000001;
217 
218  /* Reset CFGR register */
219  RCC->CFGR = 0x00000000;
220 
221  /* Reset HSEON, CSSON, PLLON, PLLI2S and PLLSAI(STM32F42/43xxx devices) bits */
222  RCC->CR &= (uint32_t)0xEAF6FFFF;
223 
224  /* Reset PLLCFGR register */
225  RCC->PLLCFGR = 0x24003010;
226 
227  /* Reset PLLI2SCFGR register */
228  RCC->PLLI2SCFGR = 0x20003000;
229 
230  /* Reset PLLSAICFGR register, only available for STM32F42/43xxx devices */
231  RCC->PLLSAICFGR = 0x24003000;
232 
233  /* Reset HSEBYP bit */
234  RCC->CR &= (uint32_t)0xFFFBFFFF;
235 
236  /* Disable all interrupts */
237  RCC->CIR = 0x00000000;
238 
239  /* Disable Timers clock prescalers selection, only available for STM32F42/43xxx devices */
240  RCC->DCKCFGR = 0x00000000;
241 
242 }
243 
264 void RCC_HSEConfig(uint8_t RCC_HSE)
265 {
266  /* Check the parameters */
267  assert_param(IS_RCC_HSE(RCC_HSE));
268 
269  /* Reset HSEON and HSEBYP bits before configuring the HSE ------------------*/
270  *(__IO uint8_t *) CR_BYTE3_ADDRESS = RCC_HSE_OFF;
271 
272  /* Set the new HSE configuration -------------------------------------------*/
273  *(__IO uint8_t *) CR_BYTE3_ADDRESS = RCC_HSE;
274 }
275 
288 ErrorStatus RCC_WaitForHSEStartUp(void)
289 {
290  __IO uint32_t startupcounter = 0;
291  ErrorStatus status = ERROR;
292  FlagStatus hsestatus = RESET;
293  /* Wait till HSE is ready and if Time out is reached exit */
294  do
295  {
296  hsestatus = RCC_GetFlagStatus(RCC_FLAG_HSERDY);
297  startupcounter++;
298  } while((startupcounter != HSE_STARTUP_TIMEOUT) && (hsestatus == RESET));
299 
300  if (RCC_GetFlagStatus(RCC_FLAG_HSERDY) != RESET)
301  {
302  status = SUCCESS;
303  }
304  else
305  {
306  status = ERROR;
307  }
308  return (status);
309 }
310 
319 void RCC_AdjustHSICalibrationValue(uint8_t HSICalibrationValue)
320 {
321  uint32_t tmpreg = 0;
322  /* Check the parameters */
323  assert_param(IS_RCC_CALIBRATION_VALUE(HSICalibrationValue));
324 
325  tmpreg = RCC->CR;
326 
327  /* Clear HSITRIM[4:0] bits */
328  tmpreg &= ~RCC_CR_HSITRIM;
329 
330  /* Set the HSITRIM[4:0] bits according to HSICalibrationValue value */
331  tmpreg |= (uint32_t)HSICalibrationValue << 3;
332 
333  /* Store the new value */
334  RCC->CR = tmpreg;
335 }
336 
355 void RCC_HSICmd(FunctionalState NewState)
356 {
357  /* Check the parameters */
358  assert_param(IS_FUNCTIONAL_STATE(NewState));
359 
360  *(__IO uint32_t *) CR_HSION_BB = (uint32_t)NewState;
361 }
362 
380 void RCC_LSEConfig(uint8_t RCC_LSE)
381 {
382  /* Check the parameters */
383  assert_param(IS_RCC_LSE(RCC_LSE));
384 
385  /* Reset LSEON and LSEBYP bits before configuring the LSE ------------------*/
386  /* Reset LSEON bit */
387  *(__IO uint8_t *) BDCR_ADDRESS = RCC_LSE_OFF;
388 
389  /* Reset LSEBYP bit */
390  *(__IO uint8_t *) BDCR_ADDRESS = RCC_LSE_OFF;
391 
392  /* Configure LSE (RCC_LSE_OFF is already covered by the code section above) */
393  switch (RCC_LSE)
394  {
395  case RCC_LSE_ON:
396  /* Set LSEON bit */
397  *(__IO uint8_t *) BDCR_ADDRESS = RCC_LSE_ON;
398  break;
399  case RCC_LSE_Bypass:
400  /* Set LSEBYP and LSEON bits */
401  *(__IO uint8_t *) BDCR_ADDRESS = RCC_LSE_Bypass | RCC_LSE_ON;
402  break;
403  default:
404  break;
405  }
406 }
407 
420 void RCC_LSICmd(FunctionalState NewState)
421 {
422  /* Check the parameters */
423  assert_param(IS_FUNCTIONAL_STATE(NewState));
424 
425  *(__IO uint32_t *) CSR_LSION_BB = (uint32_t)NewState;
426 }
427 
463 void RCC_PLLConfig(uint32_t RCC_PLLSource, uint32_t PLLM, uint32_t PLLN, uint32_t PLLP, uint32_t PLLQ)
464 {
465  /* Check the parameters */
466  assert_param(IS_RCC_PLL_SOURCE(RCC_PLLSource));
467  assert_param(IS_RCC_PLLM_VALUE(PLLM));
468  assert_param(IS_RCC_PLLN_VALUE(PLLN));
469  assert_param(IS_RCC_PLLP_VALUE(PLLP));
470  assert_param(IS_RCC_PLLQ_VALUE(PLLQ));
471 
472  RCC->PLLCFGR = PLLM | (PLLN << 6) | (((PLLP >> 1) -1) << 16) | (RCC_PLLSource) |
473  (PLLQ << 24);
474 }
475 
486 void RCC_PLLCmd(FunctionalState NewState)
487 {
488  /* Check the parameters */
489  assert_param(IS_FUNCTIONAL_STATE(NewState));
490  *(__IO uint32_t *) CR_PLLON_BB = (uint32_t)NewState;
491 }
492 
493 #if defined (STM32F40_41xxx) || defined (STM32F401xx)
494 
516 void RCC_PLLI2SConfig(uint32_t PLLI2SN, uint32_t PLLI2SR)
517 {
518  /* Check the parameters */
519  assert_param(IS_RCC_PLLI2SN_VALUE(PLLI2SN));
520  assert_param(IS_RCC_PLLI2SR_VALUE(PLLI2SR));
521 
522  RCC->PLLI2SCFGR = (PLLI2SN << 6) | (PLLI2SR << 28);
523 }
524 #endif /* STM32F40_41xxx || STM32F401xx */
525 
526 #if defined (STM32F427_437xx) || defined (STM32F429_439xx)
527 
552 void RCC_PLLI2SConfig(uint32_t PLLI2SN, uint32_t PLLI2SQ, uint32_t PLLI2SR)
553 {
554  /* Check the parameters */
555  assert_param(IS_RCC_PLLI2SN_VALUE(PLLI2SN));
556  assert_param(IS_RCC_PLLI2SQ_VALUE(PLLI2SQ));
557  assert_param(IS_RCC_PLLI2SR_VALUE(PLLI2SR));
558 
559  RCC->PLLI2SCFGR = (PLLI2SN << 6) | (PLLI2SQ << 24) | (PLLI2SR << 28);
560 }
561 #endif /* STM32F427_437xx || STM32F429_439xx */
562 
569 void RCC_PLLI2SCmd(FunctionalState NewState)
570 {
571  /* Check the parameters */
572  assert_param(IS_FUNCTIONAL_STATE(NewState));
573  *(__IO uint32_t *) CR_PLLI2SON_BB = (uint32_t)NewState;
574 }
575 
598 void RCC_PLLSAIConfig(uint32_t PLLSAIN, uint32_t PLLSAIQ, uint32_t PLLSAIR)
599 {
600  /* Check the parameters */
601  assert_param(IS_RCC_PLLSAIN_VALUE(PLLSAIN));
602  assert_param(IS_RCC_PLLSAIR_VALUE(PLLSAIR));
603 
604  RCC->PLLSAICFGR = (PLLSAIN << 6) | (PLLSAIQ << 24) | (PLLSAIR << 28);
605 }
606 
616 void RCC_PLLSAICmd(FunctionalState NewState)
617 {
618  /* Check the parameters */
619  assert_param(IS_FUNCTIONAL_STATE(NewState));
620  *(__IO uint32_t *) CR_PLLSAION_BB = (uint32_t)NewState;
621 }
622 
634 void RCC_ClockSecuritySystemCmd(FunctionalState NewState)
635 {
636  /* Check the parameters */
637  assert_param(IS_FUNCTIONAL_STATE(NewState));
638  *(__IO uint32_t *) CR_CSSON_BB = (uint32_t)NewState;
639 }
640 
659 void RCC_MCO1Config(uint32_t RCC_MCO1Source, uint32_t RCC_MCO1Div)
660 {
661  uint32_t tmpreg = 0;
662 
663  /* Check the parameters */
664  assert_param(IS_RCC_MCO1SOURCE(RCC_MCO1Source));
665  assert_param(IS_RCC_MCO1DIV(RCC_MCO1Div));
666 
667  tmpreg = RCC->CFGR;
668 
669  /* Clear MCO1[1:0] and MCO1PRE[2:0] bits */
670  tmpreg &= CFGR_MCO1_RESET_MASK;
671 
672  /* Select MCO1 clock source and prescaler */
673  tmpreg |= RCC_MCO1Source | RCC_MCO1Div;
674 
675  /* Store the new value */
676  RCC->CFGR = tmpreg;
677 }
678 
697 void RCC_MCO2Config(uint32_t RCC_MCO2Source, uint32_t RCC_MCO2Div)
698 {
699  uint32_t tmpreg = 0;
700 
701  /* Check the parameters */
702  assert_param(IS_RCC_MCO2SOURCE(RCC_MCO2Source));
703  assert_param(IS_RCC_MCO2DIV(RCC_MCO2Div));
704 
705  tmpreg = RCC->CFGR;
706 
707  /* Clear MCO2 and MCO2PRE[2:0] bits */
708  tmpreg &= CFGR_MCO2_RESET_MASK;
709 
710  /* Select MCO2 clock source and prescaler */
711  tmpreg |= RCC_MCO2Source | RCC_MCO2Div;
712 
713  /* Store the new value */
714  RCC->CFGR = tmpreg;
715 }
716 
863 void RCC_SYSCLKConfig(uint32_t RCC_SYSCLKSource)
864 {
865  uint32_t tmpreg = 0;
866 
867  /* Check the parameters */
868  assert_param(IS_RCC_SYSCLK_SOURCE(RCC_SYSCLKSource));
869 
870  tmpreg = RCC->CFGR;
871 
872  /* Clear SW[1:0] bits */
873  tmpreg &= ~RCC_CFGR_SW;
874 
875  /* Set SW[1:0] bits according to RCC_SYSCLKSource value */
876  tmpreg |= RCC_SYSCLKSource;
877 
878  /* Store the new value */
879  RCC->CFGR = tmpreg;
880 }
881 
891 uint8_t RCC_GetSYSCLKSource(void)
892 {
893  return ((uint8_t)(RCC->CFGR & RCC_CFGR_SWS));
894 }
895 
916 void RCC_HCLKConfig(uint32_t RCC_SYSCLK)
917 {
918  uint32_t tmpreg = 0;
919 
920  /* Check the parameters */
921  assert_param(IS_RCC_HCLK(RCC_SYSCLK));
922 
923  tmpreg = RCC->CFGR;
924 
925  /* Clear HPRE[3:0] bits */
926  tmpreg &= ~RCC_CFGR_HPRE;
927 
928  /* Set HPRE[3:0] bits according to RCC_SYSCLK value */
929  tmpreg |= RCC_SYSCLK;
930 
931  /* Store the new value */
932  RCC->CFGR = tmpreg;
933 }
934 
935 
948 void RCC_PCLK1Config(uint32_t RCC_HCLK)
949 {
950  uint32_t tmpreg = 0;
951 
952  /* Check the parameters */
953  assert_param(IS_RCC_PCLK(RCC_HCLK));
954 
955  tmpreg = RCC->CFGR;
956 
957  /* Clear PPRE1[2:0] bits */
958  tmpreg &= ~RCC_CFGR_PPRE1;
959 
960  /* Set PPRE1[2:0] bits according to RCC_HCLK value */
961  tmpreg |= RCC_HCLK;
962 
963  /* Store the new value */
964  RCC->CFGR = tmpreg;
965 }
966 
979 void RCC_PCLK2Config(uint32_t RCC_HCLK)
980 {
981  uint32_t tmpreg = 0;
982 
983  /* Check the parameters */
984  assert_param(IS_RCC_PCLK(RCC_HCLK));
985 
986  tmpreg = RCC->CFGR;
987 
988  /* Clear PPRE2[2:0] bits */
989  tmpreg &= ~RCC_CFGR_PPRE2;
990 
991  /* Set PPRE2[2:0] bits according to RCC_HCLK value */
992  tmpreg |= RCC_HCLK << 3;
993 
994  /* Store the new value */
995  RCC->CFGR = tmpreg;
996 }
997 
1032 {
1033  uint32_t tmp = 0, presc = 0, pllvco = 0, pllp = 2, pllsource = 0, pllm = 2;
1034 
1035  /* Get SYSCLK source -------------------------------------------------------*/
1036  tmp = RCC->CFGR & RCC_CFGR_SWS;
1037 
1038  switch (tmp)
1039  {
1040  case 0x00: /* HSI used as system clock source */
1041  RCC_Clocks->SYSCLK_Frequency = HSI_VALUE;
1042  break;
1043  case 0x04: /* HSE used as system clock source */
1044  RCC_Clocks->SYSCLK_Frequency = HSE_VALUE;
1045  break;
1046  case 0x08: /* PLL used as system clock source */
1047 
1048  /* PLL_VCO = (HSE_VALUE or HSI_VALUE / PLLM) * PLLN
1049  SYSCLK = PLL_VCO / PLLP
1050  */
1051  pllsource = (RCC->PLLCFGR & RCC_PLLCFGR_PLLSRC) >> 22;
1052  pllm = RCC->PLLCFGR & RCC_PLLCFGR_PLLM;
1053 
1054  if (pllsource != 0)
1055  {
1056  /* HSE used as PLL clock source */
1057  pllvco = (HSE_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
1058  }
1059  else
1060  {
1061  /* HSI used as PLL clock source */
1062  pllvco = (HSI_VALUE / pllm) * ((RCC->PLLCFGR & RCC_PLLCFGR_PLLN) >> 6);
1063  }
1064 
1065  pllp = (((RCC->PLLCFGR & RCC_PLLCFGR_PLLP) >>16) + 1 ) *2;
1066  RCC_Clocks->SYSCLK_Frequency = pllvco/pllp;
1067  break;
1068  default:
1069  RCC_Clocks->SYSCLK_Frequency = HSI_VALUE;
1070  break;
1071  }
1072  /* Compute HCLK, PCLK1 and PCLK2 clocks frequencies ------------------------*/
1073 
1074  /* Get HCLK prescaler */
1075  tmp = RCC->CFGR & RCC_CFGR_HPRE;
1076  tmp = tmp >> 4;
1077  presc = APBAHBPrescTable[tmp];
1078  /* HCLK clock frequency */
1079  RCC_Clocks->HCLK_Frequency = RCC_Clocks->SYSCLK_Frequency >> presc;
1080 
1081  /* Get PCLK1 prescaler */
1082  tmp = RCC->CFGR & RCC_CFGR_PPRE1;
1083  tmp = tmp >> 10;
1084  presc = APBAHBPrescTable[tmp];
1085  /* PCLK1 clock frequency */
1086  RCC_Clocks->PCLK1_Frequency = RCC_Clocks->HCLK_Frequency >> presc;
1087 
1088  /* Get PCLK2 prescaler */
1089  tmp = RCC->CFGR & RCC_CFGR_PPRE2;
1090  tmp = tmp >> 13;
1091  presc = APBAHBPrescTable[tmp];
1092  /* PCLK2 clock frequency */
1093  RCC_Clocks->PCLK2_Frequency = RCC_Clocks->HCLK_Frequency >> presc;
1094 }
1095 
1156 void RCC_RTCCLKConfig(uint32_t RCC_RTCCLKSource)
1157 {
1158  uint32_t tmpreg = 0;
1159 
1160  /* Check the parameters */
1161  assert_param(IS_RCC_RTCCLK_SOURCE(RCC_RTCCLKSource));
1162 
1163  if ((RCC_RTCCLKSource & 0x00000300) == 0x00000300)
1164  { /* If HSE is selected as RTC clock source, configure HSE division factor for RTC clock */
1165  tmpreg = RCC->CFGR;
1166 
1167  /* Clear RTCPRE[4:0] bits */
1168  tmpreg &= ~RCC_CFGR_RTCPRE;
1169 
1170  /* Configure HSE division factor for RTC clock */
1171  tmpreg |= (RCC_RTCCLKSource & 0xFFFFCFF);
1172 
1173  /* Store the new value */
1174  RCC->CFGR = tmpreg;
1175  }
1176 
1177  /* Select the RTC clock source */
1178  RCC->BDCR |= (RCC_RTCCLKSource & 0x00000FFF);
1179 }
1180 
1188 void RCC_RTCCLKCmd(FunctionalState NewState)
1189 {
1190  /* Check the parameters */
1191  assert_param(IS_FUNCTIONAL_STATE(NewState));
1192 
1193  *(__IO uint32_t *) BDCR_RTCEN_BB = (uint32_t)NewState;
1194 }
1195 
1205 void RCC_BackupResetCmd(FunctionalState NewState)
1206 {
1207  /* Check the parameters */
1208  assert_param(IS_FUNCTIONAL_STATE(NewState));
1209  *(__IO uint32_t *) BDCR_BDRST_BB = (uint32_t)NewState;
1210 }
1211 
1222 void RCC_I2SCLKConfig(uint32_t RCC_I2SCLKSource)
1223 {
1224  /* Check the parameters */
1225  assert_param(IS_RCC_I2SCLK_SOURCE(RCC_I2SCLKSource));
1226 
1227  *(__IO uint32_t *) CFGR_I2SSRC_BB = RCC_I2SCLKSource;
1228 }
1229 
1243 void RCC_SAIPLLI2SClkDivConfig(uint32_t RCC_PLLI2SDivQ)
1244 {
1245  uint32_t tmpreg = 0;
1246 
1247  /* Check the parameters */
1248  assert_param(IS_RCC_PLLI2S_DIVQ_VALUE(RCC_PLLI2SDivQ));
1249 
1250  tmpreg = RCC->DCKCFGR;
1251 
1252  /* Clear PLLI2SDIVQ[4:0] bits */
1253  tmpreg &= ~(RCC_DCKCFGR_PLLI2SDIVQ);
1254 
1255  /* Set PLLI2SDIVQ values */
1256  tmpreg |= (RCC_PLLI2SDivQ - 1);
1257 
1258  /* Store the new value */
1259  RCC->DCKCFGR = tmpreg;
1260 }
1261 
1275 void RCC_SAIPLLSAIClkDivConfig(uint32_t RCC_PLLSAIDivQ)
1276 {
1277  uint32_t tmpreg = 0;
1278 
1279  /* Check the parameters */
1280  assert_param(IS_RCC_PLLSAI_DIVQ_VALUE(RCC_PLLSAIDivQ));
1281 
1282  tmpreg = RCC->DCKCFGR;
1283 
1284  /* Clear PLLI2SDIVQ[4:0] and PLLSAIDIVQ[4:0] bits */
1285  tmpreg &= ~(RCC_DCKCFGR_PLLSAIDIVQ);
1286 
1287  /* Set PLLSAIDIVQ values */
1288  tmpreg |= ((RCC_PLLSAIDivQ - 1) << 8);
1289 
1290  /* Store the new value */
1291  RCC->DCKCFGR = tmpreg;
1292 }
1293 
1311 void RCC_SAIBlockACLKConfig(uint32_t RCC_SAIBlockACLKSource)
1312 {
1313  uint32_t tmpreg = 0;
1314 
1315  /* Check the parameters */
1316  assert_param(IS_RCC_SAIACLK_SOURCE(RCC_SAIBlockACLKSource));
1317 
1318  tmpreg = RCC->DCKCFGR;
1319 
1320  /* Clear RCC_DCKCFGR_SAI1ASRC[1:0] bits */
1321  tmpreg &= ~RCC_DCKCFGR_SAI1ASRC;
1322 
1323  /* Set SAI Block A source selection value */
1324  tmpreg |= RCC_SAIBlockACLKSource;
1325 
1326  /* Store the new value */
1327  RCC->DCKCFGR = tmpreg;
1328 }
1329 
1347 void RCC_SAIBlockBCLKConfig(uint32_t RCC_SAIBlockBCLKSource)
1348 {
1349  uint32_t tmpreg = 0;
1350 
1351  /* Check the parameters */
1352  assert_param(IS_RCC_SAIBCLK_SOURCE(RCC_SAIBlockBCLKSource));
1353 
1354  tmpreg = RCC->DCKCFGR;
1355 
1356  /* Clear RCC_DCKCFGR_SAI1BSRC[1:0] bits */
1357  tmpreg &= ~RCC_DCKCFGR_SAI1BSRC;
1358 
1359  /* Set SAI Block B source selection value */
1360  tmpreg |= RCC_SAIBlockBCLKSource;
1361 
1362  /* Store the new value */
1363  RCC->DCKCFGR = tmpreg;
1364 }
1365 
1366 
1380 void RCC_LTDCCLKDivConfig(uint32_t RCC_PLLSAIDivR)
1381 {
1382  uint32_t tmpreg = 0;
1383 
1384  /* Check the parameters */
1385  assert_param(IS_RCC_PLLSAI_DIVR_VALUE(RCC_PLLSAIDivR));
1386 
1387  tmpreg = RCC->DCKCFGR;
1388 
1389  /* Clear PLLSAIDIVR[2:0] bits */
1390  tmpreg &= ~RCC_DCKCFGR_PLLSAIDIVR;
1391 
1392  /* Set PLLSAIDIVR values */
1393  tmpreg |= RCC_PLLSAIDivR;
1394 
1395  /* Store the new value */
1396  RCC->DCKCFGR = tmpreg;
1397 }
1398 
1417 void RCC_TIMCLKPresConfig(uint32_t RCC_TIMCLKPrescaler)
1418 {
1419  /* Check the parameters */
1420  assert_param(IS_RCC_TIMCLK_PRESCALER(RCC_TIMCLKPrescaler));
1421 
1422  *(__IO uint32_t *) DCKCFGR_TIMPRE_BB = RCC_TIMCLKPrescaler;
1423 
1424 }
1425 
1460 void RCC_AHB1PeriphClockCmd(uint32_t RCC_AHB1Periph, FunctionalState NewState)
1461 {
1462  /* Check the parameters */
1463  assert_param(IS_RCC_AHB1_CLOCK_PERIPH(RCC_AHB1Periph));
1464 
1465  assert_param(IS_FUNCTIONAL_STATE(NewState));
1466  if (NewState != DISABLE)
1467  {
1468  RCC->AHB1ENR |= RCC_AHB1Periph;
1469  }
1470  else
1471  {
1472  RCC->AHB1ENR &= ~RCC_AHB1Periph;
1473  }
1474 }
1475 
1492 void RCC_AHB2PeriphClockCmd(uint32_t RCC_AHB2Periph, FunctionalState NewState)
1493 {
1494  /* Check the parameters */
1495  assert_param(IS_RCC_AHB2_PERIPH(RCC_AHB2Periph));
1496  assert_param(IS_FUNCTIONAL_STATE(NewState));
1497 
1498  if (NewState != DISABLE)
1499  {
1500  RCC->AHB2ENR |= RCC_AHB2Periph;
1501  }
1502  else
1503  {
1504  RCC->AHB2ENR &= ~RCC_AHB2Periph;
1505  }
1506 }
1507 
1520 void RCC_AHB3PeriphClockCmd(uint32_t RCC_AHB3Periph, FunctionalState NewState)
1521 {
1522  /* Check the parameters */
1523  assert_param(IS_RCC_AHB3_PERIPH(RCC_AHB3Periph));
1524  assert_param(IS_FUNCTIONAL_STATE(NewState));
1525 
1526  if (NewState != DISABLE)
1527  {
1528  RCC->AHB3ENR |= RCC_AHB3Periph;
1529  }
1530  else
1531  {
1532  RCC->AHB3ENR &= ~RCC_AHB3Periph;
1533  }
1534 }
1535 
1572 void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
1573 {
1574  /* Check the parameters */
1575  assert_param(IS_RCC_APB1_PERIPH(RCC_APB1Periph));
1576  assert_param(IS_FUNCTIONAL_STATE(NewState));
1577 
1578  if (NewState != DISABLE)
1579  {
1580  RCC->APB1ENR |= RCC_APB1Periph;
1581  }
1582  else
1583  {
1584  RCC->APB1ENR &= ~RCC_APB1Periph;
1585  }
1586 }
1587 
1617 void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
1618 {
1619  /* Check the parameters */
1620  assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph));
1621  assert_param(IS_FUNCTIONAL_STATE(NewState));
1622 
1623  if (NewState != DISABLE)
1624  {
1625  RCC->APB2ENR |= RCC_APB2Periph;
1626  }
1627  else
1628  {
1629  RCC->APB2ENR &= ~RCC_APB2Periph;
1630  }
1631 }
1632 
1659 void RCC_AHB1PeriphResetCmd(uint32_t RCC_AHB1Periph, FunctionalState NewState)
1660 {
1661  /* Check the parameters */
1662  assert_param(IS_RCC_AHB1_RESET_PERIPH(RCC_AHB1Periph));
1663  assert_param(IS_FUNCTIONAL_STATE(NewState));
1664 
1665  if (NewState != DISABLE)
1666  {
1667  RCC->AHB1RSTR |= RCC_AHB1Periph;
1668  }
1669  else
1670  {
1671  RCC->AHB1RSTR &= ~RCC_AHB1Periph;
1672  }
1673 }
1674 
1688 void RCC_AHB2PeriphResetCmd(uint32_t RCC_AHB2Periph, FunctionalState NewState)
1689 {
1690  /* Check the parameters */
1691  assert_param(IS_RCC_AHB2_PERIPH(RCC_AHB2Periph));
1692  assert_param(IS_FUNCTIONAL_STATE(NewState));
1693 
1694  if (NewState != DISABLE)
1695  {
1696  RCC->AHB2RSTR |= RCC_AHB2Periph;
1697  }
1698  else
1699  {
1700  RCC->AHB2RSTR &= ~RCC_AHB2Periph;
1701  }
1702 }
1703 
1713 void RCC_AHB3PeriphResetCmd(uint32_t RCC_AHB3Periph, FunctionalState NewState)
1714 {
1715  /* Check the parameters */
1716  assert_param(IS_RCC_AHB3_PERIPH(RCC_AHB3Periph));
1717  assert_param(IS_FUNCTIONAL_STATE(NewState));
1718 
1719  if (NewState != DISABLE)
1720  {
1721  RCC->AHB3RSTR |= RCC_AHB3Periph;
1722  }
1723  else
1724  {
1725  RCC->AHB3RSTR &= ~RCC_AHB3Periph;
1726  }
1727 }
1728 
1762 void RCC_APB1PeriphResetCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
1763 {
1764  /* Check the parameters */
1765  assert_param(IS_RCC_APB1_PERIPH(RCC_APB1Periph));
1766  assert_param(IS_FUNCTIONAL_STATE(NewState));
1767  if (NewState != DISABLE)
1768  {
1769  RCC->APB1RSTR |= RCC_APB1Periph;
1770  }
1771  else
1772  {
1773  RCC->APB1RSTR &= ~RCC_APB1Periph;
1774  }
1775 }
1776 
1803 void RCC_APB2PeriphResetCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
1804 {
1805  /* Check the parameters */
1806  assert_param(IS_RCC_APB2_RESET_PERIPH(RCC_APB2Periph));
1807  assert_param(IS_FUNCTIONAL_STATE(NewState));
1808  if (NewState != DISABLE)
1809  {
1810  RCC->APB2RSTR |= RCC_APB2Periph;
1811  }
1812  else
1813  {
1814  RCC->APB2RSTR &= ~RCC_APB2Periph;
1815  }
1816 }
1817 
1852 void RCC_AHB1PeriphClockLPModeCmd(uint32_t RCC_AHB1Periph, FunctionalState NewState)
1853 {
1854  /* Check the parameters */
1855  assert_param(IS_RCC_AHB1_LPMODE_PERIPH(RCC_AHB1Periph));
1856  assert_param(IS_FUNCTIONAL_STATE(NewState));
1857  if (NewState != DISABLE)
1858  {
1859  RCC->AHB1LPENR |= RCC_AHB1Periph;
1860  }
1861  else
1862  {
1863  RCC->AHB1LPENR &= ~RCC_AHB1Periph;
1864  }
1865 }
1866 
1884 void RCC_AHB2PeriphClockLPModeCmd(uint32_t RCC_AHB2Periph, FunctionalState NewState)
1885 {
1886  /* Check the parameters */
1887  assert_param(IS_RCC_AHB2_PERIPH(RCC_AHB2Periph));
1888  assert_param(IS_FUNCTIONAL_STATE(NewState));
1889  if (NewState != DISABLE)
1890  {
1891  RCC->AHB2LPENR |= RCC_AHB2Periph;
1892  }
1893  else
1894  {
1895  RCC->AHB2LPENR &= ~RCC_AHB2Periph;
1896  }
1897 }
1898 
1912 void RCC_AHB3PeriphClockLPModeCmd(uint32_t RCC_AHB3Periph, FunctionalState NewState)
1913 {
1914  /* Check the parameters */
1915  assert_param(IS_RCC_AHB3_PERIPH(RCC_AHB3Periph));
1916  assert_param(IS_FUNCTIONAL_STATE(NewState));
1917  if (NewState != DISABLE)
1918  {
1919  RCC->AHB3LPENR |= RCC_AHB3Periph;
1920  }
1921  else
1922  {
1923  RCC->AHB3LPENR &= ~RCC_AHB3Periph;
1924  }
1925 }
1926 
1964 void RCC_APB1PeriphClockLPModeCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
1965 {
1966  /* Check the parameters */
1967  assert_param(IS_RCC_APB1_PERIPH(RCC_APB1Periph));
1968  assert_param(IS_FUNCTIONAL_STATE(NewState));
1969  if (NewState != DISABLE)
1970  {
1971  RCC->APB1LPENR |= RCC_APB1Periph;
1972  }
1973  else
1974  {
1975  RCC->APB1LPENR &= ~RCC_APB1Periph;
1976  }
1977 }
1978 
2009 void RCC_APB2PeriphClockLPModeCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
2010 {
2011  /* Check the parameters */
2012  assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph));
2013  assert_param(IS_FUNCTIONAL_STATE(NewState));
2014  if (NewState != DISABLE)
2015  {
2016  RCC->APB2LPENR |= RCC_APB2Periph;
2017  }
2018  else
2019  {
2020  RCC->APB2LPENR &= ~RCC_APB2Periph;
2021  }
2022 }
2023 
2055 void RCC_ITConfig(uint8_t RCC_IT, FunctionalState NewState)
2056 {
2057  /* Check the parameters */
2058  assert_param(IS_RCC_IT(RCC_IT));
2059  assert_param(IS_FUNCTIONAL_STATE(NewState));
2060  if (NewState != DISABLE)
2061  {
2062  /* Perform Byte access to RCC_CIR[14:8] bits to enable the selected interrupts */
2063  *(__IO uint8_t *) CIR_BYTE2_ADDRESS |= RCC_IT;
2064  }
2065  else
2066  {
2067  /* Perform Byte access to RCC_CIR[14:8] bits to disable the selected interrupts */
2068  *(__IO uint8_t *) CIR_BYTE2_ADDRESS &= (uint8_t)~RCC_IT;
2069  }
2070 }
2071 
2092 FlagStatus RCC_GetFlagStatus(uint8_t RCC_FLAG)
2093 {
2094  uint32_t tmp = 0;
2095  uint32_t statusreg = 0;
2096  FlagStatus bitstatus = RESET;
2097 
2098  /* Check the parameters */
2099  assert_param(IS_RCC_FLAG(RCC_FLAG));
2100 
2101  /* Get the RCC register index */
2102  tmp = RCC_FLAG >> 5;
2103  if (tmp == 1) /* The flag to check is in CR register */
2104  {
2105  statusreg = RCC->CR;
2106  }
2107  else if (tmp == 2) /* The flag to check is in BDCR register */
2108  {
2109  statusreg = RCC->BDCR;
2110  }
2111  else /* The flag to check is in CSR register */
2112  {
2113  statusreg = RCC->CSR;
2114  }
2115 
2116  /* Get the flag position */
2117  tmp = RCC_FLAG & FLAG_MASK;
2118  if ((statusreg & ((uint32_t)1 << tmp)) != (uint32_t)RESET)
2119  {
2120  bitstatus = SET;
2121  }
2122  else
2123  {
2124  bitstatus = RESET;
2125  }
2126  /* Return the flag status */
2127  return bitstatus;
2128 }
2129 
2137 void RCC_ClearFlag(void)
2138 {
2139  /* Set RMVF bit to clear the reset flags */
2140  RCC->CSR |= RCC_CSR_RMVF;
2141 }
2142 
2157 ITStatus RCC_GetITStatus(uint8_t RCC_IT)
2158 {
2159  ITStatus bitstatus = RESET;
2160 
2161  /* Check the parameters */
2162  assert_param(IS_RCC_GET_IT(RCC_IT));
2163 
2164  /* Check the status of the specified RCC interrupt */
2165  if ((RCC->CIR & RCC_IT) != (uint32_t)RESET)
2166  {
2167  bitstatus = SET;
2168  }
2169  else
2170  {
2171  bitstatus = RESET;
2172  }
2173  /* Return the RCC_IT status */
2174  return bitstatus;
2175 }
2176 
2191 void RCC_ClearITPendingBit(uint8_t RCC_IT)
2192 {
2193  /* Check the parameters */
2194  assert_param(IS_RCC_CLEAR_IT(RCC_IT));
2195 
2196  /* Perform Byte access to RCC_CIR[23:16] bits to clear the selected interrupt
2197  pending bits */
2198  *(__IO uint8_t *) CIR_BYTE3_ADDRESS = RCC_IT;
2199 }
2200 
2217 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
This file contains all the functions prototypes for the RCC firmware library.
void RCC_HCLKConfig(uint32_t RCC_SYSCLK)
Configures the AHB clock (HCLK).
void RCC_RTCCLKConfig(uint32_t RCC_RTCCLKSource)
Configures the RTC clock (RTCCLK).
void RCC_PLLCmd(FunctionalState NewState)
Enables or disables the main PLL.
void RCC_MCO1Config(uint32_t RCC_MCO1Source, uint32_t RCC_MCO1Div)
Selects the clock source to output on MCO1 pin(PA8).
void RCC_ClockSecuritySystemCmd(FunctionalState NewState)
Enables or disables the Clock Security System.
void RCC_SAIBlockACLKConfig(uint32_t RCC_SAIBlockACLKSource)
Configures SAI1BlockA clock source selection.
void RCC_SAIPLLI2SClkDivConfig(uint32_t RCC_PLLI2SDivQ)
Configures the SAI clock Divider coming from PLLI2S.
void RCC_AHB1PeriphClockLPModeCmd(uint32_t RCC_AHB1Periph, FunctionalState NewState)
Enables or disables the AHB1 peripheral clock during Low Power (Sleep) mode.
void RCC_PCLK2Config(uint32_t RCC_HCLK)
Configures the High Speed APB clock (PCLK2).
void RCC_APB1PeriphResetCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
Forces or releases Low Speed APB (APB1) peripheral reset.
void RCC_SAIPLLSAIClkDivConfig(uint32_t RCC_PLLSAIDivQ)
Configures the SAI clock Divider coming from PLLSAI.
uint32_t HCLK_Frequency
Definition: stm32f4xx_rcc.h:51
void RCC_AHB2PeriphClockCmd(uint32_t RCC_AHB2Periph, FunctionalState NewState)
Enables or disables the AHB2 peripheral clock.
void RCC_ITConfig(uint8_t RCC_IT, FunctionalState NewState)
Enables or disables the specified RCC interrupts.
void RCC_AHB1PeriphClockCmd(uint32_t RCC_AHB1Periph, FunctionalState NewState)
Enables or disables the AHB1 peripheral clock.
void RCC_AHB3PeriphClockLPModeCmd(uint32_t RCC_AHB3Periph, FunctionalState NewState)
Enables or disables the AHB3 peripheral clock during Low Power (Sleep) mode.
void RCC_PLLI2SCmd(FunctionalState NewState)
Enables or disables the PLLI2S.
void RCC_PLLSAIConfig(uint32_t PLLSAIN, uint32_t PLLSAIQ, uint32_t PLLSAIR)
Configures the PLLSAI clock multiplication and division factors.
uint8_t RCC_GetSYSCLKSource(void)
Returns the clock source used as system clock.
void RCC_I2SCLKConfig(uint32_t RCC_I2SCLKSource)
Configures the I2S clock source (I2SCLK).
void RCC_TIMCLKPresConfig(uint32_t RCC_TIMCLKPrescaler)
Configures the Timers clocks prescalers selection.
void RCC_AHB3PeriphClockCmd(uint32_t RCC_AHB3Periph, FunctionalState NewState)
Enables or disables the AHB3 peripheral clock.
void RCC_DeInit(void)
Resets the RCC clock configuration to the default reset state.
void RCC_APB2PeriphClockLPModeCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
Enables or disables the APB2 peripheral clock during Low Power (Sleep) mode.
void RCC_PCLK1Config(uint32_t RCC_HCLK)
Configures the Low Speed APB clock (PCLK1).
void RCC_AHB1PeriphResetCmd(uint32_t RCC_AHB1Periph, FunctionalState NewState)
Forces or releases AHB1 peripheral reset.
void RCC_SAIBlockBCLKConfig(uint32_t RCC_SAIBlockBCLKSource)
Configures SAI1BlockB clock source selection.
void RCC_GetClocksFreq(RCC_ClocksTypeDef *RCC_Clocks)
Returns the frequencies of different on chip clocks; SYSCLK, HCLK, PCLK1 and PCLK2.
FlagStatus RCC_GetFlagStatus(uint8_t RCC_FLAG)
Checks whether the specified RCC flag is set or not.
void RCC_APB1PeriphClockLPModeCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
Enables or disables the APB1 peripheral clock during Low Power (Sleep) mode.
void RCC_BackupResetCmd(FunctionalState NewState)
Forces or releases the Backup domain reset.
void RCC_LTDCCLKDivConfig(uint32_t RCC_PLLSAIDivR)
Configures the LTDC clock Divider coming from PLLSAI.
void RCC_MCO2Config(uint32_t RCC_MCO2Source, uint32_t RCC_MCO2Div)
Selects the clock source to output on MCO2 pin(PC9).
void RCC_SYSCLKConfig(uint32_t RCC_SYSCLKSource)
Configures the system clock (SYSCLK).
void RCC_AHB2PeriphResetCmd(uint32_t RCC_AHB2Periph, FunctionalState NewState)
Forces or releases AHB2 peripheral reset.
uint32_t SYSCLK_Frequency
Definition: stm32f4xx_rcc.h:50
void RCC_AHB3PeriphResetCmd(uint32_t RCC_AHB3Periph, FunctionalState NewState)
Forces or releases AHB3 peripheral reset.
void RCC_ClearFlag(void)
Clears the RCC reset flags. The reset flags are: RCC_FLAG_PINRST, RCC_FLAG_PORRST, RCC_FLAG_SFTRST, RCC_FLAG_IWDGRST, RCC_FLAG_WWDGRST, RCC_FLAG_LPWRRST.
ITStatus RCC_GetITStatus(uint8_t RCC_IT)
Checks whether the specified RCC interrupt has occurred or not.
void RCC_ClearITPendingBit(uint8_t RCC_IT)
Clears the RCC's interrupt pending bits.
void RCC_AHB2PeriphClockLPModeCmd(uint32_t RCC_AHB2Periph, FunctionalState NewState)
Enables or disables the AHB2 peripheral clock during Low Power (Sleep) mode.
void RCC_LSEConfig(uint8_t RCC_LSE)
Configures the External Low Speed oscillator (LSE).
void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState)
Enables or disables the Low Speed APB (APB1) peripheral clock.
void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
Enables or disables the High Speed APB (APB2) peripheral clock.
ErrorStatus RCC_WaitForHSEStartUp(void)
Waits for HSE start-up.
void RCC_PLLConfig(uint32_t RCC_PLLSource, uint32_t PLLM, uint32_t PLLN, uint32_t PLLP, uint32_t PLLQ)
Configures the main PLL clock source, multiplication and division factors.
void RCC_HSEConfig(uint8_t RCC_HSE)
Configures the External High Speed oscillator (HSE).
void RCC_RTCCLKCmd(FunctionalState NewState)
Enables or disables the RTC clock.
uint32_t PCLK2_Frequency
Definition: stm32f4xx_rcc.h:53
uint32_t PCLK1_Frequency
Definition: stm32f4xx_rcc.h:52
void RCC_PLLSAICmd(FunctionalState NewState)
Enables or disables the PLLSAI.
void RCC_APB2PeriphResetCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
Forces or releases High Speed APB (APB2) peripheral reset.
void RCC_HSICmd(FunctionalState NewState)
Enables or disables the Internal High Speed oscillator (HSI).
void RCC_AdjustHSICalibrationValue(uint8_t HSICalibrationValue)
Adjusts the Internal High Speed oscillator (HSI) calibration value.
void RCC_LSICmd(FunctionalState NewState)
Enables or disables the Internal Low Speed oscillator (LSI).