stm32f4xx_syscfg.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_syscfg.c
  4. * @author MCD Application Team
  5. * @version V1.8.1
  6. * @date 27-January-2022
  7. * @brief This file provides firmware functions to manage the SYSCFG peripheral.
  8. *
  9. @verbatim
  10. ===============================================================================
  11. ##### How to use this driver #####
  12. ===============================================================================
  13. [..] This driver provides functions for:
  14. (#) Remapping the memory accessible in the code area using SYSCFG_MemoryRemapConfig()
  15. (#) Swapping the internal flash Bank1 and Bank2 this features is only visible for
  16. STM32F42xxx/43xxx devices Devices.
  17. (#) Manage the EXTI lines connection to the GPIOs using SYSCFG_EXTILineConfig()
  18. (#) Select the ETHERNET media interface (RMII/RII) using SYSCFG_ETH_MediaInterfaceConfig()
  19. -@- SYSCFG APB clock must be enabled to get write access to SYSCFG registers,
  20. using RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
  21. @endverbatim
  22. ******************************************************************************
  23. * @attention
  24. *
  25. * Copyright (c) 2016 STMicroelectronics.
  26. * All rights reserved.
  27. *
  28. * This software is licensed under terms that can be found in the LICENSE file
  29. * in the root directory of this software component.
  30. * If no LICENSE file comes with this software, it is provided AS-IS.
  31. *
  32. ******************************************************************************
  33. */
  34. /* Includes ------------------------------------------------------------------*/
  35. #include "stm32f4xx_syscfg.h"
  36. #include "stm32f4xx_rcc.h"
  37. /** @addtogroup STM32F4xx_StdPeriph_Driver
  38. * @{
  39. */
  40. /** @defgroup SYSCFG
  41. * @brief SYSCFG driver modules
  42. * @{
  43. */
  44. /* Private typedef -----------------------------------------------------------*/
  45. /* Private define ------------------------------------------------------------*/
  46. /* ------------ RCC registers bit address in the alias region ----------- */
  47. #define SYSCFG_OFFSET (SYSCFG_BASE - PERIPH_BASE)
  48. /* --- MEMRMP Register ---*/
  49. /* Alias word address of UFB_MODE bit */
  50. #define MEMRMP_OFFSET SYSCFG_OFFSET
  51. #define UFB_MODE_BitNumber ((uint8_t)0x8)
  52. #define UFB_MODE_BB (PERIPH_BB_BASE + (MEMRMP_OFFSET * 32) + (UFB_MODE_BitNumber * 4))
  53. /* --- PMC Register ---*/
  54. /* Alias word address of MII_RMII_SEL bit */
  55. #define PMC_OFFSET (SYSCFG_OFFSET + 0x04)
  56. #define MII_RMII_SEL_BitNumber ((uint8_t)0x17)
  57. #define PMC_MII_RMII_SEL_BB (PERIPH_BB_BASE + (PMC_OFFSET * 32) + (MII_RMII_SEL_BitNumber * 4))
  58. /* --- CMPCR Register ---*/
  59. /* Alias word address of CMP_PD bit */
  60. #define CMPCR_OFFSET (SYSCFG_OFFSET + 0x20)
  61. #define CMP_PD_BitNumber ((uint8_t)0x00)
  62. #define CMPCR_CMP_PD_BB (PERIPH_BB_BASE + (CMPCR_OFFSET * 32) + (CMP_PD_BitNumber * 4))
  63. /* --- MCHDLYCR Register ---*/
  64. /* Alias word address of BSCKSEL bit */
  65. #define MCHDLYCR_OFFSET (SYSCFG_OFFSET + 0x30)
  66. #define BSCKSEL_BIT_NUMBER POSITION_VAL(SYSCFG_MCHDLYCR_BSCKSEL)
  67. #define MCHDLYCR_BSCKSEL_BB (uint32_t)(PERIPH_BB_BASE + (MCHDLYCR_OFFSET * 32) + (BSCKSEL_BIT_NUMBER * 4))
  68. /* Private macro -------------------------------------------------------------*/
  69. /* Private variables ---------------------------------------------------------*/
  70. /* Private function prototypes -----------------------------------------------*/
  71. /* Private functions ---------------------------------------------------------*/
  72. /** @defgroup SYSCFG_Private_Functions
  73. * @{
  74. */
  75. /**
  76. * @brief Deinitializes the Alternate Functions (remap and EXTI configuration)
  77. * registers to their default reset values.
  78. * @param None
  79. * @retval None
  80. */
  81. void SYSCFG_DeInit(void)
  82. {
  83. RCC_APB2PeriphResetCmd(RCC_APB2Periph_SYSCFG, ENABLE);
  84. RCC_APB2PeriphResetCmd(RCC_APB2Periph_SYSCFG, DISABLE);
  85. }
  86. /**
  87. * @brief Changes the mapping of the specified pin.
  88. * @param SYSCFG_Memory: selects the memory remapping.
  89. * This parameter can be one of the following values:
  90. * @arg SYSCFG_MemoryRemap_Flash: Main Flash memory mapped at 0x00000000
  91. * @arg SYSCFG_MemoryRemap_SystemFlash: System Flash memory mapped at 0x00000000
  92. * @arg SYSCFG_MemoryRemap_FSMC: FSMC (Bank1 (NOR/PSRAM 1 and 2) mapped at 0x00000000 for STM32F405xx/407xx, STM32F415xx/417xx and STM32F413_423xx devices.
  93. * @arg SYSCFG_MemoryRemap_FMC: FMC (Bank1 (NOR/PSRAM 1 and 2) mapped at 0x00000000 for STM32F42xxx/43xxx devices.
  94. * @arg SYSCFG_MemoryRemap_ExtMEM: External Memory mapped at 0x00000000 for STM32F446xx/STM32F469_479xx devices.
  95. * @arg SYSCFG_MemoryRemap_SRAM: Embedded SRAM (112kB) mapped at 0x00000000
  96. * @arg SYSCFG_MemoryRemap_SDRAM: FMC (External SDRAM) mapped at 0x00000000 for STM32F42xxx/43xxx devices.
  97. * @retval None
  98. */
  99. void SYSCFG_MemoryRemapConfig(uint8_t SYSCFG_MemoryRemap)
  100. {
  101. /* Check the parameters */
  102. assert_param(IS_SYSCFG_MEMORY_REMAP_CONFING(SYSCFG_MemoryRemap));
  103. SYSCFG->MEMRMP = SYSCFG_MemoryRemap;
  104. }
  105. /**
  106. * @brief Enables or disables the Internal FLASH Bank Swapping.
  107. *
  108. * @note This function can be used only for STM32F42xxx/43xxx devices.
  109. *
  110. * @param NewState: new state of Internal FLASH Bank swapping.
  111. * This parameter can be one of the following values:
  112. * @arg ENABLE: Flash Bank2 mapped at 0x08000000 (and aliased @0x00000000)
  113. * and Flash Bank1 mapped at 0x08100000 (and aliased at 0x00100000)
  114. * @arg DISABLE:(the default state) Flash Bank1 mapped at 0x08000000 (and aliased @0x0000 0000)
  115. and Flash Bank2 mapped at 0x08100000 (and aliased at 0x00100000)
  116. * @retval None
  117. */
  118. void SYSCFG_MemorySwappingBank(FunctionalState NewState)
  119. {
  120. /* Check the parameters */
  121. assert_param(IS_FUNCTIONAL_STATE(NewState));
  122. *(__IO uint32_t *) UFB_MODE_BB = (uint32_t)NewState;
  123. }
  124. /**
  125. * @brief Selects the GPIO pin used as EXTI Line.
  126. * @param EXTI_PortSourceGPIOx : selects the GPIO port to be used as source for
  127. * EXTI lines where x can be (A..K) for STM32F42xxx/43xxx devices, (A..I)
  128. * for STM32F405xx/407xx and STM32F415xx/417xx devices or (A, B, C, D and H)
  129. * for STM32401xx devices.
  130. *
  131. * @param EXTI_PinSourcex: specifies the EXTI line to be configured.
  132. * This parameter can be EXTI_PinSourcex where x can be (0..15, except
  133. * for EXTI_PortSourceGPIOI x can be (0..11) for STM32F405xx/407xx
  134. * and STM32F405xx/407xx devices and for EXTI_PortSourceGPIOK x can
  135. * be (0..7) for STM32F42xxx/43xxx devices.
  136. *
  137. * @retval None
  138. */
  139. void SYSCFG_EXTILineConfig(uint8_t EXTI_PortSourceGPIOx, uint8_t EXTI_PinSourcex)
  140. {
  141. uint32_t tmp = 0x00;
  142. /* Check the parameters */
  143. assert_param(IS_EXTI_PORT_SOURCE(EXTI_PortSourceGPIOx));
  144. assert_param(IS_EXTI_PIN_SOURCE(EXTI_PinSourcex));
  145. tmp = ((uint32_t)0x0F) << (0x04 * (EXTI_PinSourcex & (uint8_t)0x03));
  146. SYSCFG->EXTICR[EXTI_PinSourcex >> 0x02] &= ~tmp;
  147. SYSCFG->EXTICR[EXTI_PinSourcex >> 0x02] |= (((uint32_t)EXTI_PortSourceGPIOx) << (0x04 * (EXTI_PinSourcex & (uint8_t)0x03)));
  148. }
  149. /**
  150. * @brief Selects the ETHERNET media interface
  151. * @param SYSCFG_ETH_MediaInterface: specifies the Media Interface mode.
  152. * This parameter can be one of the following values:
  153. * @arg SYSCFG_ETH_MediaInterface_MII: MII mode selected
  154. * @arg SYSCFG_ETH_MediaInterface_RMII: RMII mode selected
  155. * @retval None
  156. */
  157. void SYSCFG_ETH_MediaInterfaceConfig(uint32_t SYSCFG_ETH_MediaInterface)
  158. {
  159. assert_param(IS_SYSCFG_ETH_MEDIA_INTERFACE(SYSCFG_ETH_MediaInterface));
  160. /* Configure MII_RMII selection bit */
  161. *(__IO uint32_t *) PMC_MII_RMII_SEL_BB = SYSCFG_ETH_MediaInterface;
  162. }
  163. /**
  164. * @brief Enables or disables the I/O Compensation Cell.
  165. * @note The I/O compensation cell can be used only when the device supply
  166. * voltage ranges from 2.4 to 3.6 V.
  167. * @param NewState: new state of the I/O Compensation Cell.
  168. * This parameter can be one of the following values:
  169. * @arg ENABLE: I/O compensation cell enabled
  170. * @arg DISABLE: I/O compensation cell power-down mode
  171. * @retval None
  172. */
  173. void SYSCFG_CompensationCellCmd(FunctionalState NewState)
  174. {
  175. /* Check the parameters */
  176. assert_param(IS_FUNCTIONAL_STATE(NewState));
  177. *(__IO uint32_t *) CMPCR_CMP_PD_BB = (uint32_t)NewState;
  178. }
  179. /**
  180. * @brief Checks whether the I/O Compensation Cell ready flag is set or not.
  181. * @param None
  182. * @retval The new state of the I/O Compensation Cell ready flag (SET or RESET)
  183. */
  184. FlagStatus SYSCFG_GetCompensationCellStatus(void)
  185. {
  186. FlagStatus bitstatus = RESET;
  187. if ((SYSCFG->CMPCR & SYSCFG_CMPCR_READY ) != (uint32_t)RESET)
  188. {
  189. bitstatus = SET;
  190. }
  191. else
  192. {
  193. bitstatus = RESET;
  194. }
  195. return bitstatus;
  196. }
  197. #if defined(STM32F410xx) || defined(STM32F412xG) || defined(STM32F413_423xx)
  198. /**
  199. * @brief Connects the selected parameter to the break input of TIM1.
  200. * @note The selected configuration is locked and can be unlocked by system reset
  201. * @param SYSCFG_Break: selects the configuration to be connected to break
  202. * input of TIM1
  203. * This parameter can be any combination of the following values:
  204. * @arg SYSCFG_Break_PVD: PVD interrupt is connected to the break input of TIM1/8.
  205. * @arg SYSCFG_Break_HardFault: Lockup output of CortexM4 is connected to the break input of TIM1/8.
  206. * @retval None
  207. */
  208. void SYSCFG_BreakConfig(uint32_t SYSCFG_Break)
  209. {
  210. /* Check the parameter */
  211. assert_param(IS_SYSCFG_LOCK_CONFIG(SYSCFG_Break));
  212. SYSCFG->CFGR2 |= (uint32_t) SYSCFG_Break;
  213. }
  214. #endif /* STM32F410xx || STM32F412xG || STM32F413_423xx */
  215. #if defined(STM32F413_423xx)
  216. /**
  217. * @brief Select the DFSDM2 or TIM2_OC1 as clock source for the bitstream clock.
  218. * @param source: BITSTREAM_CLOCK_DFSDM2.
  219. * BITSTREAM_CLOCK_TIM2OC1.
  220. * @retval None
  221. */
  222. void DFSDM_BitstreamClock_SourceSelection(uint32_t source)
  223. {
  224. uint32_t tmp = 0;
  225. tmp = SYSCFG->MCHDLYCR;
  226. tmp = (tmp &(~SYSCFG_MCHDLYCR_BSCKSEL));
  227. SYSCFG->MCHDLYCR = (tmp|source);
  228. }
  229. /**
  230. * @brief Disable Delay Clock for DFSDM1/2.
  231. * @param MCHDLY: MCHDLY_CLOCK_DFSDM2.
  232. * MCHDLY_CLOCK_DFSDM1.
  233. * @retval None
  234. */
  235. void DFSDM_DisableDelayClock(uint32_t MCHDLY)
  236. {
  237. uint32_t tmp = 0;
  238. tmp = SYSCFG->MCHDLYCR;
  239. if(MCHDLY == MCHDLY_CLOCK_DFSDM2)
  240. {
  241. tmp =tmp &(~SYSCFG_MCHDLYCR_MCHDLY2EN);
  242. }
  243. else
  244. {
  245. tmp =tmp &(~SYSCFG_MCHDLYCR_MCHDLY1EN);
  246. }
  247. SYSCFG->MCHDLYCR = tmp;
  248. }
  249. /**
  250. * @brief Enable Delay Clock for DFSDM1/2.
  251. * @param MCHDLY: MCHDLY_CLOCK_DFSDM2.
  252. * MCHDLY_CLOCK_DFSDM1.
  253. * @retval None
  254. */
  255. void DFSDM_EnableDelayClock(uint32_t MCHDLY)
  256. {
  257. uint32_t tmp = 0;
  258. tmp = SYSCFG->MCHDLYCR;
  259. tmp = tmp & ~MCHDLY;
  260. SYSCFG->MCHDLYCR = (tmp|MCHDLY);
  261. }
  262. /**
  263. * @brief Select the source for CKin signals for DFSDM1/2.
  264. * @param source: DFSDM2_CKIN_PAD.
  265. * DFSDM2_CKIN_DM.
  266. * DFSDM1_CKIN_PAD.
  267. * DFSDM1_CKIN_DM.
  268. * @retval None
  269. */
  270. void DFSDM_ClockIn_SourceSelection(uint32_t source)
  271. {
  272. uint32_t tmp = 0;
  273. tmp = SYSCFG->MCHDLYCR;
  274. if((source == DFSDM2_CKIN_PAD) || (source == DFSDM2_CKIN_DM))
  275. {
  276. tmp = (tmp & ~SYSCFG_MCHDLYCR_DFSDM2CFG);
  277. }
  278. else
  279. {
  280. tmp = (tmp & ~SYSCFG_MCHDLYCR_DFSDM1CFG);
  281. }
  282. SYSCFG->MCHDLYCR |= (source|tmp);
  283. }
  284. /**
  285. * @brief Select the source for CKOut signals for DFSDM1/2.
  286. * @param source: DFSDM2_CKOUT_DFSDM2.
  287. * DFSDM2_CKOUT_M27.
  288. * DFSDM1_CKOUT_DFSDM1.
  289. * DFSDM1_CKOUT_M27.
  290. * @retval None
  291. */
  292. void DFSDM_ClockOut_SourceSelection(uint32_t source)
  293. {
  294. uint32_t tmp = 0;
  295. tmp = SYSCFG->MCHDLYCR;
  296. if((source == DFSDM2_CKOUT_DFSDM2) || (source == DFSDM2_CKOUT_M27))
  297. {
  298. tmp = (tmp & ~SYSCFG_MCHDLYCR_DFSDM2CKOSEL);
  299. }
  300. else
  301. {
  302. tmp = (tmp & ~SYSCFG_MCHDLYCR_DFSDM1CKOSEL);
  303. }
  304. SYSCFG->MCHDLYCR |= (source|tmp);
  305. }
  306. /**
  307. * @brief Select the source for DataIn0 signals for DFSDM1/2.
  308. * @param source: DATAIN0_DFSDM2_PAD.
  309. * DATAIN0_DFSDM2_DATAIN1.
  310. * DATAIN0_DFSDM1_PAD.
  311. * DATAIN0_DFSDM1_DATAIN1.
  312. * @retval None
  313. */
  314. void DFSDM_DataIn0_SourceSelection(uint32_t source)
  315. {
  316. uint32_t tmp = 0;
  317. tmp = SYSCFG->MCHDLYCR;
  318. if((source == DATAIN0_DFSDM2_PAD)|| (source == DATAIN0_DFSDM2_DATAIN1))
  319. {
  320. tmp = (tmp & ~SYSCFG_MCHDLYCR_DFSDM2D0SEL);
  321. }
  322. else
  323. {
  324. tmp = (tmp & ~SYSCFG_MCHDLYCR_DFSDM1D0SEL);
  325. }
  326. SYSCFG->MCHDLYCR |= (source|tmp);
  327. }
  328. /**
  329. * @brief Select the source for DataIn2 signals for DFSDM1/2.
  330. * @param source: DATAIN2_DFSDM2_PAD.
  331. * DATAIN2_DFSDM2_DATAIN3.
  332. * DATAIN2_DFSDM1_PAD.
  333. * DATAIN2_DFSDM1_DATAIN3.
  334. * @retval None
  335. */
  336. void DFSDM_DataIn2_SourceSelection(uint32_t source)
  337. {
  338. uint32_t tmp = 0;
  339. tmp = SYSCFG->MCHDLYCR;
  340. if((source == DATAIN2_DFSDM2_PAD)|| (source == DATAIN2_DFSDM2_DATAIN3))
  341. {
  342. tmp = (tmp & ~SYSCFG_MCHDLYCR_DFSDM2D2SEL);
  343. }
  344. else
  345. {
  346. tmp = (tmp & ~SYSCFG_MCHDLYCR_DFSDM1D2SEL);
  347. }
  348. SYSCFG->MCHDLYCR |= (source|tmp);
  349. }
  350. /**
  351. * @brief Select the source for DataIn4 signals for DFSDM2.
  352. * @param source: DATAIN4_DFSDM2_PAD.
  353. * DATAIN4_DFSDM2_DATAIN5
  354. * @retval None
  355. */
  356. void DFSDM_DataIn4_SourceSelection(uint32_t source)
  357. {
  358. uint32_t tmp = 0;
  359. tmp = SYSCFG->MCHDLYCR;
  360. tmp = (tmp & ~SYSCFG_MCHDLYCR_DFSDM2D4SEL);
  361. SYSCFG->MCHDLYCR |= (source|tmp);
  362. }
  363. /**
  364. * @brief Select the source for DataIn6 signals for DFSDM2.
  365. * @param source: DATAIN6_DFSDM2_PAD.
  366. * DATAIN6_DFSDM2_DATAIN7.
  367. * @retval None
  368. */
  369. void DFSDM_DataIn6_SourceSelection(uint32_t source)
  370. {
  371. uint32_t tmp = 0;
  372. tmp = SYSCFG->MCHDLYCR;
  373. tmp = (tmp & ~SYSCFG_MCHDLYCR_DFSDM2D6SEL);
  374. SYSCFG->MCHDLYCR |= (source|tmp);
  375. }
  376. /**
  377. * @brief Configure the distribution of the bitstream clock gated from TIM4.
  378. * @param source: DFSDM1_CLKIN0_TIM4OC2
  379. * DFSDM1_CLKIN2_TIM4OC2
  380. * DFSDM1_CLKIN1_TIM4OC1
  381. * DFSDM1_CLKIN3_TIM4OC1
  382. * @retval None
  383. */
  384. void DFSDM1_BitStreamClk_Config(uint32_t source)
  385. {
  386. uint32_t tmp = 0;
  387. tmp = SYSCFG->MCHDLYCR;
  388. if ((source == DFSDM1_CLKIN0_TIM4OC2) || (source == DFSDM1_CLKIN2_TIM4OC2))
  389. {
  390. tmp = (tmp & ~SYSCFG_MCHDLYCR_DFSDM1CK02SEL);
  391. }
  392. else
  393. {
  394. tmp = (tmp & ~SYSCFG_MCHDLYCR_DFSDM1CK13SEL);
  395. }
  396. SYSCFG->MCHDLYCR |= (source|tmp);
  397. }
  398. /**
  399. * @brief Configure the distribution of the bitstream clock gated from TIM3.
  400. * @param source: DFSDM2_CLKIN0_TIM3OC4
  401. * DFSDM2_CLKIN4_TIM3OC4
  402. * DFSDM2_CLKIN1_TIM3OC3
  403. * DFSDM2_CLKIN5_TIM3OC3
  404. * DFSDM2_CLKIN2_TIM3OC2
  405. * DFSDM2_CLKIN6_TIM3OC2
  406. * DFSDM2_CLKIN3_TIM3OC1
  407. * DFSDM2_CLKIN7_TIM3OC1
  408. * @retval None
  409. */
  410. void DFSDM2_BitStreamClk_Config(uint32_t source)
  411. {
  412. uint32_t tmp = 0;
  413. tmp = SYSCFG->MCHDLYCR;
  414. if ((source == DFSDM2_CLKIN0_TIM3OC4) || (source == DFSDM2_CLKIN4_TIM3OC4))
  415. {
  416. tmp = (tmp & ~SYSCFG_MCHDLYCR_DFSDM2CK04SEL);
  417. }
  418. else if ((source == DFSDM2_CLKIN1_TIM3OC3) || (source == DFSDM2_CLKIN5_TIM3OC3))
  419. {
  420. tmp = (tmp & ~SYSCFG_MCHDLYCR_DFSDM2CK15SEL);
  421. }else if ((source == DFSDM2_CLKIN2_TIM3OC2) || (source == DFSDM2_CLKIN6_TIM3OC2))
  422. {
  423. tmp = (tmp & ~SYSCFG_MCHDLYCR_DFSDM2CK26SEL);
  424. }
  425. else
  426. {
  427. tmp = (tmp & ~SYSCFG_MCHDLYCR_DFSDM2CK37SEL);
  428. }
  429. SYSCFG->MCHDLYCR |= (source|tmp);
  430. }
  431. #endif /* STM32F413_423xx */
  432. /**
  433. * @}
  434. */
  435. /**
  436. * @}
  437. */
  438. /**
  439. * @}
  440. */