STM8L15x Standard Peripherals Drivers
|
stm8l15x_syscfg.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file stm8l15x_syscfg.c 00004 * @author MCD Application Team 00005 * @version V1.5.0 00006 * @date 13-May-2011 00007 * @brief This file provides firmware functions to manage the following 00008 * functionalities of the RI and SYSCFG: 00009 * - RI configuration 00010 * - SYSCFG configuration 00011 * 00012 * @verbatim 00013 * 00014 * =================================================================== 00015 * How to use this driver 00016 * =================================================================== 00017 * This driver provides functions to configure the RI and SYSCFG 00018 * These functions are split in 2 groups: 00019 * 00020 * 1. RI configuration: this group includes all needed functions 00021 * to configure the RI: 00022 * - Routing the TIM1 Input capture channels 00023 * - Close and Open the I/O switches 00024 * - Close and Open the analog switches 00025 * - Configure the internal Pull-up and Pull-down resistors 00026 * 00027 * 2. SYSCFG configuration: this group includes all needed functions 00028 * to configure the SYSCFG: 00029 * - Configure the alternate function remapping of some peripherals 00030 * such as: USART, SPI, TIMs... 00031 * Remapping possibilities depends on the device (low-density, 00032 * medium-density or high density) so it is recommended to 00033 * refer to the product reference manual. 00034 * - Configure the DMA channels remapping of ADC1 and TIM4 00035 * 00036 * @endverbatim 00037 * 00038 ****************************************************************************** 00039 * 00040 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 00041 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 00042 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 00043 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 00044 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 00045 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 00046 * 00047 * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2> 00048 ****************************************************************************** 00049 */ 00050 00051 /* Includes ------------------------------------------------------------------*/ 00052 #include "stm8l15x_syscfg.h" 00053 00054 /** @addtogroup STM8L15x_StdPeriph_Driver 00055 * @{ 00056 */ 00057 00058 /** @defgroup SYSCFG 00059 * @brief SYSCFG driver modules 00060 * @{ 00061 */ 00062 00063 /* Private typedef -----------------------------------------------------------*/ 00064 /* Private define ------------------------------------------------------------*/ 00065 /* Private macro -------------------------------------------------------------*/ 00066 /* Private variables ---------------------------------------------------------*/ 00067 /* Private function prototypes -----------------------------------------------*/ 00068 /* Private functions ---------------------------------------------------------*/ 00069 00070 /** @defgroup SYSCFG_Private_Functions 00071 * @{ 00072 */ 00073 00074 /** @defgroup SYSCFG_Group1 RI configuration 00075 * @brief RI configuration 00076 * 00077 @verbatim 00078 =============================================================================== 00079 RI configuration functions 00080 =============================================================================== 00081 00082 =================================================================== 00083 SYSCFG Driver: how to configure RI 00084 =================================================================== 00085 00086 1. By default, TIM1 Input Capture channels 2 and 3 are connected to the 00087 I/O port assigned in the datasheet pinout (default routing). 00088 TIM1 Input Capture channels 2 and 3 can be routed by calling 00089 SYSCFG_RITIMInputCaptureConfig() 00090 Refer to the device reference manual for more details about 00091 TIM1 input capture routing 00092 00093 2. For more details about using SYSCFG_RIIOSwitchConfig() 00094 and SYSCFG_RIAnalogSwitchConfig() with COMP1 and COMP2, refer to 00095 comparators driver 00096 00097 3. To output the Internal reference voltage VREFINT, configure corresponding 00098 GPIO pin in input mode using GPIO_Init() then use SYSCFG_RIIOSwitchConfig() 00099 00100 4. To use the internal Pull-up and Pull-down resistors call 00101 SYSCFG_RIResistorConfig() 00102 00103 @endverbatim 00104 * @{ 00105 */ 00106 00107 /** 00108 * @brief Deinitializes the RI registers to their default reset values. 00109 * @param None 00110 * @retval None 00111 */ 00112 void SYSCFG_RIDeInit(void) 00113 { 00114 RI->ICR1 = RI_ICR1_RESET_VALUE; /*!< Set RI->ICR1 to reset value */ 00115 RI->ICR2 = RI_ICR2_RESET_VALUE; /*!< Set RI->ICR2 to reset value */ 00116 RI->IOSR1 = RI_IOSR1_RESET_VALUE; /*!< Set RI->IOSR1 to reset value */ 00117 RI->IOSR2 = RI_IOSR2_RESET_VALUE; /*!< Set RI->IOSR2 to reset value */ 00118 RI->IOSR3 = RI_IOSR3_RESET_VALUE; /*!< Set RI->IOSR3 to reset value */ 00119 RI->IOSR4 = RI_IOSR4_RESET_VALUE; /*!< Set RI->IOSR3 to reset value */ 00120 RI->ASCR1 = RI_ASCR1_RESET_VALUE; /*!< Set RI->ASCR1 to reset value */ 00121 RI->ASCR2 = RI_ASCR2_RESET_VALUE; /*!< Set RI->ASCR2 to reset value */ 00122 RI->RCR = RI_RCR_RESET_VALUE; /*!< Set RI->RCR to reset value */ 00123 } 00124 00125 /** 00126 * @brief Configures the routing interface to select which Input Output pin 00127 * to be routed to TIM1 Input Capture. 00128 * @param RI_InputCapture: selects the TIM1 input capture2 RI_InputCapture_IC2 00129 * or the TIM1 input capture3 RI_InputCapture_IC3 00130 * @param RI_InputCaptureRouting: selects the value to set in TIM1 Input Capture 00131 * routing register ICRx and can be from RI_InputCaptureRouting_0 to 00132 * RI_InputCaptureRouting_22. 00133 * @retval None. 00134 */ 00135 void SYSCFG_RITIMInputCaptureConfig(RI_InputCapture_TypeDef RI_InputCapture, 00136 RI_InputCaptureRouting_TypeDef RI_InputCaptureRouting) 00137 { 00138 /* Check the parameters */ 00139 assert_param(IS_RI_INPUTCAPTURE(RI_InputCapture)); 00140 assert_param(IS_RI_INPUTCAPTUREROUTING(RI_InputCaptureRouting)); 00141 00142 /* Check whether Input Capture 2 is selected */ 00143 if (RI_InputCapture == RI_InputCapture_IC2) 00144 { 00145 /* Set the value in ICR1 register to route TIM input Capture 2 */ 00146 RI->ICR1 = (uint8_t) RI_InputCaptureRouting; 00147 } 00148 00149 else /* The Input Capture 3 is selected */ 00150 { 00151 /* Set the value in ICR2 register to route TIM input capture 3 */ 00152 RI->ICR2 = (uint8_t) RI_InputCaptureRouting; 00153 } 00154 } 00155 00156 /** 00157 * @brief Enables or disables the Routing Interface Analog switch. 00158 * @param RI_AnalogSwitch: Analog Switch 00159 * This parameter can be one of the following values: 00160 * @arg RI_AnalogSwitch_0: Analog switch 0 00161 * @arg RI_AnalogSwitch_1: Analog switch 1 00162 * @arg RI_AnalogSwitch_2: Analog switch 2 00163 * @arg RI_AnalogSwitch_3: Analog switch 3 00164 * @arg RI_AnalogSwitch_4: Analog switch 4 00165 * @arg RI_AnalogSwitch_5: Analog switch 5 00166 * @arg RI_AnalogSwitch_6: Analog switch 6 00167 * @arg RI_AnalogSwitch_7: Analog switch 7 00168 * @arg RI_AnalogSwitch_8: Analog switch 8 00169 * @arg RI_AnalogSwitch_9: Analog switch 9 00170 * @arg RI_AnalogSwitch_10: Analog switch 10 00171 * @arg RI_AnalogSwitch_11: Analog switch 11 00172 * @arg RI_AnalogSwitch_12: Analog switch 14 00173 * @param NewState: new state of the analog switch. 00174 * This parameter can be ENABLE or DISABLE. 00175 * @retval None 00176 */ 00177 void SYSCFG_RIAnalogSwitchConfig(RI_AnalogSwitch_TypeDef RI_AnalogSwitch, 00178 FunctionalState NewState) 00179 { 00180 uint8_t AnalogSwitchRegister, AnalogSwitchIndex = 0; 00181 00182 /* Check the parameters */ 00183 assert_param(IS_RI_ANALOGSWITCH(RI_AnalogSwitch)); 00184 assert_param(IS_FUNCTIONAL_STATE(NewState)); 00185 00186 /* Get the analog switch register ASCR1 or ASCR2 */ 00187 AnalogSwitchRegister = (uint8_t) (RI_AnalogSwitch & (uint8_t) 0xF0); 00188 00189 /* Get the analog switch bit index in ASCRx register */ 00190 AnalogSwitchIndex = (uint8_t) (RI_AnalogSwitch & (uint8_t) 0x0F); 00191 00192 if (NewState != DISABLE) 00193 { 00194 if (AnalogSwitchRegister == (uint8_t) 0x10) 00195 { 00196 /* Enable the analog switch */ 00197 RI->ASCR1 |= (uint8_t) ((uint8_t)1 << (uint8_t) AnalogSwitchIndex); 00198 } 00199 else 00200 { 00201 /* Enable the analog switch */ 00202 RI->ASCR2 |= (uint8_t) ((uint8_t)1 << (uint8_t) AnalogSwitchIndex); 00203 } 00204 } 00205 else 00206 { 00207 if (AnalogSwitchRegister == (uint8_t) 0x10) 00208 { 00209 /* Disable the analog switch */ 00210 RI->ASCR1 &= (uint8_t) (~(uint8_t)((uint8_t)1 << AnalogSwitchIndex)); 00211 } 00212 else 00213 { 00214 /* Disable the analog switch */ 00215 RI->ASCR2 &= (uint8_t) (~ (uint8_t)((uint8_t)1 << AnalogSwitchIndex)); 00216 } 00217 } 00218 } 00219 00220 /** 00221 * @brief Closes or Opens the routing interface Input Output switches. 00222 * @param RI_IOSwitch: can be one of RI_IOSwitch_1..RI_IOSwitch_24 00223 * @param NewState: new state of the Input Output Switch. 00224 * This parameter can be ENABLE or DISABLE. 00225 * @retval None. 00226 */ 00227 void SYSCFG_RIIOSwitchConfig(RI_IOSwitch_TypeDef RI_IOSwitch, 00228 FunctionalState NewState) 00229 { 00230 uint8_t IOSwitchRegsiter, IOSwitchIndex = 0; 00231 00232 /* Check the parameters */ 00233 assert_param(IS_RI_IOSWITCH(RI_IOSwitch)); 00234 assert_param(IS_FUNCTIONAL_STATE(NewState)); 00235 00236 /* Get the Input Output switch bit index in IOSRx register */ 00237 IOSwitchIndex = (uint8_t) (RI_IOSwitch & (uint8_t) 0x0F); 00238 00239 /* Get the Input Output switch register IOSR1, IOSR2 or IOSR3 */ 00240 IOSwitchRegsiter = (uint8_t) (RI_IOSwitch & (uint8_t) 0xF0); 00241 00242 /* Check whether the Input Output switch control bit is in the IOSR1 register */ 00243 if (IOSwitchRegsiter == (uint8_t) 0x10) 00244 { 00245 if (NewState != DISABLE) 00246 { 00247 /* Close the Input Output switch */ 00248 RI->IOSR1 |= (uint8_t) ((uint8_t)1 << IOSwitchIndex); 00249 } 00250 else 00251 { 00252 /* Open the Input Output switch */ 00253 RI->IOSR1 &= (uint8_t) (~ (uint8_t)((uint8_t)1 << IOSwitchIndex)); 00254 } 00255 } 00256 00257 /* Check whether the Input Output switch control bit is in the IOSR2 register */ 00258 else if (IOSwitchRegsiter == (uint8_t) 0x20) 00259 { 00260 if (NewState != DISABLE) 00261 { 00262 /* Close the Input Output switch */ 00263 RI->IOSR2 |= (uint8_t) ((uint8_t)1 << IOSwitchIndex); 00264 } 00265 else 00266 { 00267 /* Open the Input Output switch */ 00268 RI->IOSR2 &= (uint8_t) (~(uint8_t)((uint8_t)1 << IOSwitchIndex)); 00269 } 00270 } 00271 00272 /* The Input Output switch control bit is in the IOSR3 register */ 00273 else if (IOSwitchRegsiter == (uint8_t) 0x30) 00274 { 00275 if (NewState != DISABLE) 00276 { 00277 /* Close the Input Output switch */ 00278 RI->IOSR3 |= (uint8_t) ((uint8_t)1 << IOSwitchIndex); 00279 } 00280 else 00281 { 00282 /* Open the Input Output switch */ 00283 RI->IOSR3 &= (uint8_t) (~ (uint8_t) ((uint8_t) 1 << IOSwitchIndex)); 00284 } 00285 } 00286 00287 /* The Input Output switch control bit is in the IOSR4 register */ 00288 else 00289 { 00290 if (NewState != DISABLE) 00291 { 00292 /* Close the Input Output switch */ 00293 RI->IOSR4 |= (uint8_t) ((uint8_t)1 << IOSwitchIndex); 00294 } 00295 else 00296 { 00297 /* Open the Input Output switch */ 00298 RI->IOSR4 &= (uint8_t) (~ (uint8_t) ((uint8_t) 1 << IOSwitchIndex)); 00299 } 00300 } 00301 } 00302 00303 /** 00304 * @brief Configures the Pull-up and Pull-down Resistors 00305 * @param RI_Resistor: selects the resistor to connect, 00306 * This parameter can be one of the following values: 00307 * @arg RI_Resistor_10KPU: 10K pull up 00308 * @arg RI_Resistor_400KPU: 400K pull up 00309 * @arg RI_Resistor_10KPD: 10K pull down 00310 * @arg RI_Resistor_400KPD: 400K pull down 00311 * @retval None 00312 */ 00313 void SYSCFG_RIResistorConfig(RI_Resistor_TypeDef RI_Resistor, FunctionalState NewState) 00314 { 00315 /* Check the parameters */ 00316 assert_param(IS_RI_RESISTOR(RI_Resistor)); 00317 assert_param(IS_FUNCTIONAL_STATE(NewState)); 00318 00319 if (NewState != DISABLE) 00320 { 00321 /* Enable the resistor */ 00322 RI->RCR |= (uint8_t) RI_Resistor; 00323 } 00324 else 00325 { 00326 /* Disable the Resistor */ 00327 RI->RCR &= (uint8_t) (~RI_Resistor); 00328 } 00329 } 00330 00331 /** 00332 * @} 00333 */ 00334 00335 /** @defgroup SYSCFG_Group2 SYSCFG configuration 00336 * @brief SYSCFG configuration 00337 * 00338 @verbatim 00339 =============================================================================== 00340 SYSCFG configuration functions 00341 =============================================================================== 00342 00343 =================================================================== 00344 SYSCFG Driver: how to use it for remapping 00345 =================================================================== 00346 00347 1. To remap the alternate function of some peripherals (such as: USART, 00348 SPI, TIMs...), use SYSCFG_REMAPPinConfig() 00349 00350 2. To remap the DMA channels of ADC1 and TIM4, use SYSCFG_REMAPDMAChannelConfig() 00351 00352 @endverbatim 00353 * @{ 00354 */ 00355 00356 /** 00357 * @brief Deinitializes the Remapping registers to their default reset values. 00358 * @param None 00359 * @retval None 00360 */ 00361 void SYSCFG_REMAPDeInit(void) 00362 { 00363 /*!< Set RMPCR1 to reset value */ 00364 SYSCFG->RMPCR1 = SYSCFG_RMPCR1_RESET_VALUE; 00365 00366 /*!< Set RMPCR2 to reset value */ 00367 SYSCFG->RMPCR2 = SYSCFG_RMPCR2_RESET_VALUE; 00368 00369 /*!< Set RMPCR3 to reset value */ 00370 SYSCFG->RMPCR3 = SYSCFG_RMPCR3_RESET_VALUE; 00371 } 00372 00373 /** 00374 * @brief Changes the mapping of the specified pins. 00375 * @param REMAP_Pin: selects the pin to remap. 00376 * This parameter can be one of the following values: 00377 * @arg REMAP_Pin_USART1TxRxPortA: USART1 Tx- Rx (PC3- PC2) remapping to PA2- PA3 00378 * @arg REMAP_Pin_USART1TxRxPortC: USART1 Tx- Rx (PC3- PC2) remapping to PC5- PC6 00379 * @arg REMAP_Pin_USART1Clk: USART1 CK (PC4) remapping to PA0 00380 * @arg REMAP_Pin_SPI1Full: SPI1 MISO- MOSI- SCK- NSS(PB7- PB6- PB5- PB4) 00381 * remapping to PA2- PA3- PC6- PC5 00382 * @arg REMAP_Pin_ADC1ExtTRIG1: ADC1 External Trigger 1 (PA6) remapping to PD0 00383 * @arg REMAP_Pin_TIM2TRIGPortA: TIM2 Trigger (PB3) remapping to PA4 00384 * @arg REMAP_Pin_TIM3TRIGPortA: TIM3 Trigger (PD1) remapping to PA5 00385 * @arg REMAP_Pin_TIM2TRIGLSE: TIM2 Trigger remapping to LSE 00386 * @arg REMAP_Pin_TIM3TRIGLSE: TIM3 Trigger remapping to LSE 00387 * @arg REMAP_Pin_SPI2Full: SPI2 MISO- MOSI- SCK- NSS(PG7- PG6- PG5- PG4) 00388 * remapping to PI3- PI2- PI1- PI0 00389 * @arg REMAP_Pin_TIM3TRIGPortG: TIM3 Trigger (PD1) remapping to PG3 00390 * @arg REMAP_Pin_TIM23BKIN: TIM2 Break Input (PA4) remapping to PG0 00391 * and TIM3 Break Input (PA5) remapping to PG1 00392 * @arg REMAP_Pin_SPI1PortF: SPI1 MISO- MOSI- SCK- NSS(PB7- PB6- PB5- PB4) 00393 * remapping to PF0- PF1- PF2- PF3 00394 * @arg REMAP_Pin_USART3TxRxPortF: USART3 Tx- Rx (PG1- PG0) remapping to PF0- PF1 00395 * @arg REMAP_Pin_USART3Clk: USART3 CK (PG2) remapping to PF2 00396 * @arg REMAP_Pin_TIM3Channel1: TIM3 Channel 1 (PB1) remapping to PI0 00397 * @arg REMAP_Pin_TIM3Channel2: TIM3 Channel 2 (PD0) remapping to PI3 00398 * @arg REMAP_Pin_CCO: CCO (PC4) remapping to PE2 00399 * @arg REMAP_Pin_TIM2Channel1: TIM2 Channel 1 (PB0) remapping to PC5 00400 * @arg REMAP_Pin_TIM2Channel2: TIM2 Channel 2 (PB2) remapping to PC6 00401 * @param NewState: This parameter can be ENABLE or DISABLE. 00402 * @retval None 00403 */ 00404 void SYSCFG_REMAPPinConfig(REMAP_Pin_TypeDef REMAP_Pin, FunctionalState NewState) 00405 { 00406 uint8_t regindex = 0; 00407 /* Check the parameters */ 00408 assert_param(IS_REMAP_PIN(REMAP_Pin)); 00409 assert_param(IS_FUNCTIONAL_STATE(NewState)); 00410 00411 /* Read register index */ 00412 regindex = (uint8_t) ((uint16_t) REMAP_Pin >> 8); 00413 00414 /* Check if REMAP_Pin is in RMPCR1 register */ 00415 if (regindex == 0x01) 00416 { 00417 SYSCFG->RMPCR1 &= (uint8_t)((uint8_t)((uint8_t)REMAP_Pin << 4) | (uint8_t)0x0F); 00418 if (NewState != DISABLE) 00419 { 00420 SYSCFG->RMPCR1 |= (uint8_t)((uint16_t)REMAP_Pin & (uint16_t)0x00F0); 00421 } 00422 } 00423 /* Check if REMAP_Pin is in RMPCR2 register */ 00424 else if (regindex == 0x02) 00425 { 00426 if (NewState != DISABLE) 00427 { 00428 SYSCFG->RMPCR2 |= (uint8_t) REMAP_Pin; 00429 } 00430 else 00431 { 00432 SYSCFG->RMPCR2 &= (uint8_t)((uint16_t)(~(uint16_t)REMAP_Pin)); 00433 } 00434 } 00435 /* REMAP_Pin is in RMPCR3 register */ 00436 else 00437 { 00438 if (NewState != DISABLE) 00439 { 00440 SYSCFG->RMPCR3 |= (uint8_t) REMAP_Pin; 00441 } 00442 else 00443 { 00444 SYSCFG->RMPCR3 &= (uint8_t)((uint16_t)(~(uint16_t)REMAP_Pin)); 00445 } 00446 } 00447 } 00448 00449 /** 00450 * @brief Remaps the DMA Channel to the specific peripheral (ADC or TIM4) 00451 * @param REMAP_DMAChannel: specifies the DMA Channel to remap. 00452 * This parameter can be one of the following values: 00453 * @arg REMAP_DMA1Channel_ADC1ToChannel0: ADC1 DMA1 req/ack mapped on DMA1 channel 0 00454 * @arg REMAP_DMA1Channel_ADC1ToChannel1: ADC1 DMA1 req/ack mapped on DMA1 channel 1 00455 * @arg REMAP_DMA1Channel_ADC1ToChannel2: ADC1 DMA1 req/ack mapped on DMA1 channel 2 00456 * @arg REMAP_DMA1Channel_ADC1ToChannel3: ADC1 DMA1 req/ack mapped on DMA1 channel 3 00457 * @arg REMAP_DMA1Channel_TIM4ToChannel0: TIM4 DMA1 req/ack mapped on DMA1 channel 0 00458 * @arg REMAP_DMA1Channel_TIM4ToChannel1: TIM4 DMA1 req/ack mapped on DMA1 channel 1 00459 * @arg REMAP_DMA1Channel_TIM4ToChannel2: TIM4 DMA1 req/ack mapped on DMA1 channel 2 00460 * @arg REMAP_DMA1Channel_TIM4ToChannel3: TIM4 DMA1 req/ack mapped on DMA1 channel 3 00461 * @retval None 00462 */ 00463 void SYSCFG_REMAPDMAChannelConfig(REMAP_DMAChannel_TypeDef REMAP_DMAChannel) 00464 { 00465 /* Check the parameters */ 00466 assert_param(IS_REMAP_DMACHANNEL(REMAP_DMAChannel)); 00467 00468 /* Check if the TIM4 DMA channel is selected: bits 4 --> 7 are set */ 00469 if ((REMAP_DMAChannel & 0xF0) != RESET) 00470 { 00471 /* Reset the TIM4 DMA channels */ 00472 SYSCFG->RMPCR1 &= (uint8_t) (~SYSCFG_RMPCR1_TIM4DMA_REMAP); 00473 } 00474 /* ADC DMA channel is selected: bits 4 --> 7 are reset */ 00475 else 00476 { 00477 /* Reset the ADC DMA channels */ 00478 SYSCFG->RMPCR1 &= (uint8_t) (~SYSCFG_RMPCR1_ADC1DMA_REMAP); 00479 } 00480 /* Set the DMA Channels remapping */ 00481 SYSCFG->RMPCR1 |= (uint8_t) ((uint8_t)0x0F & REMAP_DMAChannel); 00482 } 00483 00484 /** 00485 * @} 00486 */ 00487 00488 /** 00489 * @} 00490 */ 00491 00492 /** 00493 * @} 00494 */ 00495 00496 /** 00497 * @} 00498 */ 00499 00500 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/