2 ******************************************************************************
\r
3 * @file stm32l1xx_syscfg.c
\r
4 * @author MCD Application Team
\r
6 * @date 31-December-2010
\r
7 * @brief This file provides firmware functions to manage the following
\r
8 * functionalities of the SYSCFG and RI peripherals:
\r
9 * - SYSCFG Initialization and Configuration
\r
10 * - RI Initialization and Configuration
\r
14 * ===================================================================
\r
15 * How to use this driver
\r
16 * ===================================================================
\r
18 * This driver provides functions for:
\r
20 * 1. Remapping the memory accessible in the code area using
\r
21 * SYSCFG_MemoryRemapConfig()
\r
22 * 2. Manage the EXTI lines connection to the GPIOs using
\r
23 * SYSCFG_EXTILineConfig().
\r
24 * 3. Routing of I/Os toward the input captures of timers (TIM2, TIM3 and TIM4).
\r
25 * 4. Input routing of COMP1 and COMP2
\r
26 * 5. Routing of internal reference voltage VREFINT to PB0 and PB1.
\r
28 * 6. The RI registers can be accessed only when the comparator
\r
29 * APB interface clock is enabled.
\r
30 * To enable comparator clock use:
\r
31 * RCC_APB1PeriphClockCmd(RCC_APB1Periph_COMP, ENABLE);
\r
33 * Following functions uses RI registers:
\r
34 * - SYSCFG_RIDeInit()
\r
35 * - SYSCFG_RITIMSelect()
\r
36 * - SYSCFG_RITIMInputCaptureConfig()
\r
37 * - SYSCFG_RIResistorConfig()
\r
38 * - SYSCFG_RIIOSwitchConfig()
\r
39 * - SYSCFG_RISwitchControlModeCmd()
\r
40 * - SYSCFG_RIHysteresisConfig()
\r
42 * 7- The SYSCFG registers can be accessed only when the SYSCFG
\r
43 * interface APB clock is enabled.
\r
44 * To enable SYSCFG APB clock use:
\r
45 * RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
\r
47 * Following functions uses SYSCFG registers:
\r
48 * - SYSCFG_MemoryRemapConfig()
\r
49 * - SYSCFG_USBPuCmd()
\r
50 * - SYSCFG_EXTILineConfig()
\r
54 ******************************************************************************
\r
57 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
\r
58 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
\r
59 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
\r
60 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
\r
61 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
\r
62 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
\r
64 * <h2><center>© COPYRIGHT 2010 STMicroelectronics</center></h2>
\r
65 ******************************************************************************
\r
68 /* Includes ------------------------------------------------------------------*/
\r
69 #include "stm32l1xx_syscfg.h"
\r
70 #include "stm32l1xx_rcc.h"
\r
72 /** @addtogroup STM32L1xx_StdPeriph_Driver
\r
76 /** @defgroup SYSCFG
\r
77 * @brief SYSCFG driver modules
\r
81 /* Private typedef -----------------------------------------------------------*/
\r
82 /* Private define ------------------------------------------------------------*/
\r
83 #define TIM_SELECT_MASK ((uint32_t)0xFFFCFFFF) /*!< TIM select mask */
\r
84 #define IC_ROUTING_MASK ((uint32_t)0x0000000F) /*!< Input Capture routing mask */
\r
86 /* Private macro -------------------------------------------------------------*/
\r
87 /* Private variables ---------------------------------------------------------*/
\r
88 /* Private function prototypes -----------------------------------------------*/
\r
89 /* Private functions ---------------------------------------------------------*/
\r
91 /** @defgroup SYSCFG_Private_Functions
\r
95 /** @defgroup SYSCFG_Group1 SYSCFG Initialization and Configuration functions
\r
96 * @brief SYSCFG Initialization and Configuration functions
\r
99 ===============================================================================
\r
100 SYSCFG Initialization and Configuration functions
\r
101 ===============================================================================
\r
108 * @brief Deinitializes the SYSCFG registers to their default reset values.
\r
111 * @ Note: MEMRMP bits are not reset by APB2 reset.
\r
113 void SYSCFG_DeInit(void)
\r
115 RCC_APB2PeriphResetCmd(RCC_APB2Periph_SYSCFG, ENABLE);
\r
116 RCC_APB2PeriphResetCmd(RCC_APB2Periph_SYSCFG, DISABLE);
\r
120 * @brief Deinitializes the RI registers to their default reset values.
\r
124 void SYSCFG_RIDeInit(void)
\r
126 RI->ICR = ((uint32_t)0x00000000); /*!< Set RI->ICR to reset value */
\r
127 RI->ASCR1 = ((uint32_t)0x00000000); /*!< Set RI->ASCR1 to reset value */
\r
128 RI->ASCR2 = ((uint32_t)0x00000000); /*!< Set RI->ASCR2 to reset value */
\r
129 RI->HYSCR1 = ((uint32_t)0x00000000); /*!< Set RI->HYSCR1 to reset value */
\r
130 RI->HYSCR2 = ((uint32_t)0x00000000); /*!< Set RI->HYSCR2 to reset value */
\r
131 RI->HYSCR3 = ((uint32_t)0x00000000); /*!< Set RI->HYSCR3 to reset value */
\r
135 * @brief Changes the mapping of the specified memory.
\r
136 * @param SYSCFG_Memory: selects the memory remapping.
\r
137 * This parameter can be one of the following values:
\r
138 * @arg SYSCFG_MemoryRemap_Flash: Main Flash memory mapped at 0x00000000
\r
139 * @arg SYSCFG_MemoryRemap_SystemFlash: System Flash memory mapped at 0x00000000
\r
140 * @arg SYSCFG_MemoryRemap_SRAM: Embedded SRAM mapped at 0x00000000
\r
143 void SYSCFG_MemoryRemapConfig(uint8_t SYSCFG_MemoryRemap)
\r
145 /* Check the parameters */
\r
146 assert_param(IS_SYSCFG_MEMORY_REMAP_CONFING(SYSCFG_MemoryRemap));
\r
147 SYSCFG->MEMRMP = SYSCFG_MemoryRemap;
\r
151 * @brief Control the internal pull-up on USB DP line.
\r
152 * @param NewState: New state of the internal pull-up on USB DP line.
\r
153 * This parameter can be ENABLE: Connect internal pull-up on USB DP line.
\r
154 * or DISABLE: Disconnect internal pull-up on USB DP line.
\r
157 void SYSCFG_USBPuCmd(FunctionalState NewState)
\r
159 /* Check the parameters */
\r
160 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
162 if (NewState != DISABLE)
\r
164 /* Connect internal pull-up on USB DP line */
\r
165 SYSCFG->PMC |= (uint32_t) SYSCFG_PMC_USB_PU;
\r
169 /* Disconnect internal pull-up on USB DP line */
\r
170 SYSCFG->PMC &= (uint32_t)(~SYSCFG_PMC_USB_PU);
\r
175 * @brief Selects the GPIO pin used as EXTI Line.
\r
176 * @param EXTI_PortSourceGPIOx : selects the GPIO port to be used as source
\r
177 * for EXTI lines where x can be (A, B, C, D, E or H).
\r
178 * @param EXTI_PinSourcex: specifies the EXTI line to be configured.
\r
179 * This parameter can be EXTI_PinSourcex where x can be (0..15)
\r
182 void SYSCFG_EXTILineConfig(uint8_t EXTI_PortSourceGPIOx, uint8_t EXTI_PinSourcex)
\r
184 uint32_t tmp = 0x00;
\r
186 /* Check the parameters */
\r
187 assert_param(IS_EXTI_PORT_SOURCE(EXTI_PortSourceGPIOx));
\r
188 assert_param(IS_EXTI_PIN_SOURCE(EXTI_PinSourcex));
\r
190 tmp = ((uint32_t)0x0F) << (0x04 * (EXTI_PinSourcex & (uint8_t)0x03));
\r
191 SYSCFG->EXTICR[EXTI_PinSourcex >> 0x02] &= ~tmp;
\r
192 SYSCFG->EXTICR[EXTI_PinSourcex >> 0x02] |= (((uint32_t)EXTI_PortSourceGPIOx) << (0x04 * (EXTI_PinSourcex & (uint8_t)0x03)));
\r
199 /** @defgroup SYSCFG_Group2 RI Initialization and Configuration functions
\r
200 * @brief RI Initialization and Configuration functions
\r
203 ===============================================================================
\r
204 RI Initialization and Configuration functions
\r
205 ===============================================================================
\r
212 * @brief Configures the routing interface to select which Timer to be routed.
\r
213 * @note Routing capability can be applied only on one of the three timers
\r
214 * (TIM2, TIM3 or TIM4) at a time.
\r
215 * @param TIM_Select: Timer select.
\r
216 * This parameter can be one of the following values:
\r
217 * @arg TIM_Select_None: No timer selected and default Timer mapping is enabled.
\r
218 * @arg TIM_Select_TIM2: Timer 2 Input Captures to be routed.
\r
219 * @arg TIM_Select_TIM3: Timer 3 Input Captures to be routed.
\r
220 * @arg TIM_Select_TIM4: Timer 4 Input Captures to be routed.
\r
223 void SYSCFG_RITIMSelect(uint32_t TIM_Select)
\r
225 uint32_t tmpreg = 0;
\r
227 /* Check the parameters */
\r
228 assert_param(IS_RI_TIM(TIM_Select));
\r
230 /* Get the old register value */
\r
233 /* Clear the TIMx select bits */
\r
234 tmpreg &= TIM_SELECT_MASK;
\r
236 /* Select the Timer */
\r
237 tmpreg |= (TIM_Select);
\r
239 /* Write to RI->ICR register */
\r
244 * @brief Configures the routing interface to map Input Capture 1, 2, 3 or 4
\r
245 * to a selected I/O pin.
\r
246 * @param RI_InputCapture selects which input capture to be routed.
\r
247 * This parameter can be one (or combination) of the following parameters:
\r
248 * @arg RI_InputCapture_IC1: Input capture 1 is selected.
\r
249 * @arg RI_InputCapture_IC2: Input capture 2 is selected.
\r
250 * @arg RI_InputCapture_IC3: Input capture 3 is selected.
\r
251 * @arg RI_InputCapture_IC4: Input capture 4 is selected.
\r
252 * @param RI_InputCaptureRouting: selects which pin to be routed to Input Capture.
\r
253 * This parameter can be one of the following values:
\r
254 * @arg RI_InputCaptureRouting_0 to RI_InputCaptureRouting_15
\r
256 * SYSCFG_RITIMSelect(TIM_Select_TIM2)
\r
257 * SYSCFG_RITIMInputCaptureConfig(RI_InputCapture_IC1, RI_InputCaptureRouting_1)
\r
258 * allows routing of Input capture IC1 of TIM2 to PA4.
\r
259 * For details about correspondence between RI_InputCaptureRouting_x
\r
260 * and I/O pins refer to the parameters' description in the header file
\r
261 * or refer to the product reference manual.
\r
262 * @note Input capture selection bits are not reset by this function.
\r
263 * To reset input capture selection bits, use SYSCFG_RIDeInit() function.
\r
264 * @note The I/O should be configured in alternate function mode (AF14) using
\r
265 * GPIO_PinAFConfig() function.
\r
268 void SYSCFG_RITIMInputCaptureConfig(uint32_t RI_InputCapture, uint32_t RI_InputCaptureRouting)
\r
270 uint32_t tmpreg = 0;
\r
272 /* Check the parameters */
\r
273 assert_param(IS_RI_INPUTCAPTURE(RI_InputCapture));
\r
274 assert_param(IS_RI_INPUTCAPTURE_ROUTING(RI_InputCaptureRouting));
\r
276 /* Get the old register value */
\r
279 /* Select input captures to be routed */
\r
280 tmpreg |= (RI_InputCapture);
\r
282 if((RI_InputCapture & RI_InputCapture_IC1) == RI_InputCapture_IC1)
\r
284 /* Clear the input capture select bits */
\r
285 tmpreg &= (uint32_t)(~IC_ROUTING_MASK);
\r
287 /* Set RI_InputCaptureRouting bits */
\r
288 tmpreg |= (uint32_t)( RI_InputCaptureRouting);
\r
291 if((RI_InputCapture & RI_InputCapture_IC2) == RI_InputCapture_IC2)
\r
293 /* Clear the input capture select bits */
\r
294 tmpreg &= (uint32_t)(~(IC_ROUTING_MASK << 4));
\r
296 /* Set RI_InputCaptureRouting bits */
\r
297 tmpreg |= (uint32_t)( (RI_InputCaptureRouting << 4));
\r
300 if((RI_InputCapture & RI_InputCapture_IC3) == RI_InputCapture_IC3)
\r
302 /* Clear the input capture select bits */
\r
303 tmpreg &= (uint32_t)(~(IC_ROUTING_MASK << 8));
\r
305 /* Set RI_InputCaptureRouting bits */
\r
306 tmpreg |= (uint32_t)( (RI_InputCaptureRouting << 8));
\r
309 if((RI_InputCapture & RI_InputCapture_IC4) == RI_InputCapture_IC4)
\r
311 /* Clear the input capture select bits */
\r
312 tmpreg &= (uint32_t)(~(IC_ROUTING_MASK << 12));
\r
314 /* Set RI_InputCaptureRouting bits */
\r
315 tmpreg |= (uint32_t)( (RI_InputCaptureRouting << 12));
\r
318 /* Write to RI->ICR register */
\r
322 * @brief Configures the Pull-up and Pull-down Resistors
\r
323 * @param RI_Resistor selects the resistor to connect.
\r
324 * This parameter can be one of the following values:
\r
325 * @arg RI_Resistor_10KPU: 10K pull-up resistor
\r
326 * @arg RI_Resistor_400KPU: 400K pull-up resistor
\r
327 * @arg RI_Resistor_10KPD: 10K pull-down resistor
\r
328 * @arg RI_Resistor_400KPD: 400K pull-down resistor
\r
329 * @param NewState: New state of the analog switch associated to the selected
\r
331 * This parameter can be:
\r
332 * ENABLE so the selected resistor is connected
\r
333 * or DISABLE so the selected resistor is disconnected
\r
334 * @note To avoid extra power consumption, only one resistor should be enabled
\r
338 void SYSCFG_RIResistorConfig(uint32_t RI_Resistor, FunctionalState NewState)
\r
340 /* Check the parameters */
\r
341 assert_param(IS_RI_RESISTOR(RI_Resistor));
\r
342 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
344 if (NewState != DISABLE)
\r
346 /* Enable the resistor */
\r
347 COMP->CSR |= (uint32_t) RI_Resistor;
\r
351 /* Disable the Resistor */
\r
352 COMP->CSR &= (uint32_t) (~RI_Resistor);
\r
357 * @brief Close or Open the routing interface Input Output switches.
\r
358 * @param RI_IOSwitch: selects the I/O analog switch number.
\r
359 * This parameter can be one of the following values:
\r
360 * @arg RI_IOSwitch_CH0 --> RI_IOSwitch_CH15
\r
361 * @arg RI_IOSwitch_CH18 --> RI_IOSwitch_CH25
\r
362 * @arg RI_IOSwitch_GR10_1 --> RI_IOSwitch_GR10_4
\r
363 * @arg RI_IOSwitch_GR6_1 --> RI_IOSwitch_GR6_2
\r
364 * @arg RI_IOSwitch_GR5_1 --> RI_IOSwitch_GR5_3
\r
365 * @arg RI_IOSwitch_GR4_1 --> RI_IOSwitch_GR4_3
\r
366 * @arg RI_IOSwitch_VCOMP
\r
367 * @param NewState: New state of the analog switch.
\r
368 * This parameter can be
\r
369 * ENABLE so the Input Output switch is closed
\r
370 * or DISABLE so the Input Output switch is open
\r
373 void SYSCFG_RIIOSwitchConfig(uint32_t RI_IOSwitch, FunctionalState NewState)
\r
375 uint32_t ioswitchmask = 0;
\r
377 /* Check the parameters */
\r
378 assert_param(IS_RI_IOSWITCH(RI_IOSwitch));
\r
380 /* Read Analog switch register index */
\r
381 ioswitchmask = RI_IOSwitch >> 31;
\r
383 /* Get Bits[30:0] of the IO switch */
\r
384 RI_IOSwitch &= 0x7FFFFFFF;
\r
387 if (NewState != DISABLE)
\r
389 if (ioswitchmask != 0)
\r
391 /* Close the analog switches */
\r
392 RI->ASCR1 |= RI_IOSwitch;
\r
396 /* Open the analog switches */
\r
397 RI->ASCR2 |= RI_IOSwitch;
\r
402 if (ioswitchmask != 0)
\r
404 /* Close the analog switches */
\r
405 RI->ASCR1 &= (~ (uint32_t)RI_IOSwitch);
\r
409 /* Open the analog switches */
\r
410 RI->ASCR2 &= (~ (uint32_t)RI_IOSwitch);
\r
416 * @brief Enable or disable the switch control mode.
\r
417 * @param NewState: New state of the switch control mode. This parameter can
\r
418 * be ENABLE: ADC analog switches closed if the corresponding
\r
419 * I/O switch is also closed.
\r
420 * When using COMP1 switch control mode must be enabled.
\r
421 * or DISABLE: ADC analog switches open or controlled by the ADC interface.
\r
422 * When using the ADC for acquisition switch control mode
\r
423 * must be disabled.
\r
424 * @note COMP1 comparator and ADC cannot be used at the same time since
\r
425 * they share the ADC switch matrix.
\r
428 void SYSCFG_RISwitchControlModeCmd(FunctionalState NewState)
\r
430 /* Check the parameters */
\r
431 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
433 if (NewState != DISABLE)
\r
435 /* Enable the Switch control mode */
\r
436 RI->ASCR1 |= (uint32_t) RI_ASCR1_SCM;
\r
440 /* Disable the Switch control mode */
\r
441 RI->ASCR1 &= (uint32_t)(~RI_ASCR1_SCM);
\r
446 * @brief Enable or disable Hysteresis of the input schmitt triger of Ports A..E
\r
447 * When the I/Os are programmed in input mode by standard I/O port
\r
448 * registers, the Schmitt trigger and the hysteresis are enabled by default.
\r
449 * When hysteresis is disabled, it is possible to read the
\r
450 * corresponding port with a trigger level of VDDIO/2.
\r
451 * @param RI_Port: selects the GPIO Port.
\r
452 * This parameter can be one of the following values:
\r
453 * @arg RI_PortA : Port A is selected
\r
454 * @arg RI_PortB : Port B is selected
\r
455 * @arg RI_PortC : Port C is selected
\r
456 * @arg RI_PortD : Port D is selected
\r
457 * @arg RI_PortE : Port E is selected
\r
458 * @param RI_Pin : Selects the pin(s) on which to enable or disable hysteresis.
\r
459 * This parameter can any value from RI_Pin_x where x can be (0..15) or RI_Pin_All.
\r
460 * @param NewState new state of the Hysteresis.
\r
461 * This parameter can be:
\r
462 * ENABLE so the Hysteresis is on
\r
463 * or DISABLE so the Hysteresis is off
\r
466 void SYSCFG_RIHysteresisConfig(uint8_t RI_Port, uint16_t RI_Pin,
\r
467 FunctionalState NewState)
\r
469 /* Check the parameters */
\r
470 assert_param(IS_RI_PORT(RI_Port));
\r
471 assert_param(IS_RI_PIN(RI_Pin));
\r
472 assert_param(IS_FUNCTIONAL_STATE(NewState));
\r
474 if(RI_Port == RI_PortA)
\r
476 if (NewState != DISABLE)
\r
478 /* Hysteresis on */
\r
479 RI->HYSCR1 &= (uint32_t)~((uint32_t)RI_Pin);
\r
483 /* Hysteresis off */
\r
484 RI->HYSCR1 |= (uint32_t) RI_Pin;
\r
488 else if(RI_Port == RI_PortB)
\r
491 if (NewState != DISABLE)
\r
493 /* Hysteresis on */
\r
494 RI->HYSCR1 &= (uint32_t) (~((uint32_t)RI_Pin) << 16);
\r
498 /* Hysteresis off */
\r
499 RI->HYSCR1 |= (uint32_t) ((uint32_t)(RI_Pin) << 16);
\r
503 else if(RI_Port == RI_PortC)
\r
506 if (NewState != DISABLE)
\r
508 /* Hysteresis on */
\r
509 RI->HYSCR2 &= (uint32_t) (~((uint32_t)RI_Pin));
\r
513 /* Hysteresis off */
\r
514 RI->HYSCR2 |= (uint32_t) (RI_Pin );
\r
517 else if(RI_Port == RI_PortD)
\r
519 if (NewState != DISABLE)
\r
521 /* Hysteresis on */
\r
522 RI->HYSCR2 &= (uint32_t) (~((uint32_t)RI_Pin) << 16);
\r
526 /* Hysteresis off */
\r
527 RI->HYSCR2 |= (uint32_t) ((uint32_t)(RI_Pin) << 16);
\r
531 else /* RI_Port == RI_PortE */
\r
533 if (NewState != DISABLE)
\r
535 /* Hysteresis on */
\r
536 RI->HYSCR3 &= (uint32_t) (~((uint32_t)RI_Pin));
\r
540 /* Hysteresis off */
\r
541 RI->HYSCR3 |= (uint32_t) (RI_Pin );
\r
561 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
\r