n32g45x_gpio.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file n32g45x_gpio.c
  29. * @author Nations
  30. * @version v1.0.2
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #include "n32g45x_gpio.h"
  35. #include "n32g45x_rcc.h"
  36. /** @addtogroup N32G45X_StdPeriph_Driver
  37. * @{
  38. */
  39. /** @addtogroup GPIO
  40. * @brief GPIO driver modules
  41. * @{
  42. */
  43. /** @addtogroup GPIO_Private_TypesDefinitions
  44. * @{
  45. */
  46. /**
  47. * @}
  48. */
  49. /** @addtogroup GPIO_Private_Defines
  50. * @{
  51. */
  52. /* ------------ RCC registers bit address in the alias region ----------------*/
  53. #define AFIO_OFFSET (AFIO_BASE - PERIPH_BASE)
  54. /* --- Event control register -----*/
  55. /* Alias word address of EVOE bit */
  56. #define EVCR_OFFSET (AFIO_OFFSET + 0x00)
  57. #define EVOE_BitNumber ((uint8_t)0x07)
  58. #define EVCR_EVOE_BB (PERIPH_BB_BASE + (EVCR_OFFSET * 32) + (EVOE_BitNumber * 4))
  59. /* --- RMP_CFG Register ---*/
  60. /* Alias word address of MII_RMII_SEL bit */
  61. #define MAPR_OFFSET (AFIO_OFFSET + 0x04)
  62. #define MII_RMII_SEL_BitNumber ((u8)0x17)
  63. #define MAPR_MII_RMII_SEL_BB (PERIPH_BB_BASE + (MAPR_OFFSET * 32) + (MII_RMII_SEL_BitNumber * 4))
  64. #define EVCR_PORTPINCONFIG_MASK ((uint16_t)0xFF80)
  65. #define LSB_MASK ((uint16_t)0xFFFF)
  66. #define DBGAFR_POSITION_MASK ((uint32_t)0x000F0000)
  67. #define DBGAFR_SWJCFG_MASK ((uint32_t)0xF0FFFFFF)
  68. #define DBGAFR_LOCATION_MASK ((uint32_t)0x00200000)
  69. #define DBGAFR_NUMBITS_MASK ((uint32_t)0x00100000)
  70. #define DBGAFR_NUMBITS_MAPR3_MASK ((uint32_t)0x40000000)
  71. #define DBGAFR_NUMBITS_MAPR4_MASK ((uint32_t)0x20000000)
  72. #define DBGAFR_NUMBITS_MAPR5_MASK ((uint32_t)0x10000000)
  73. #define DBGAFR_NUMBITS_SPI1_MASK ((uint32_t)0x01000000)
  74. #define DBGAFR_NUMBITS_USART2_MASK ((uint32_t)0x04000000)
  75. /**
  76. * @}
  77. */
  78. /** @addtogroup GPIO_Private_Macros
  79. * @{
  80. */
  81. /**
  82. * @}
  83. */
  84. /** @addtogroup GPIO_Private_Variables
  85. * @{
  86. */
  87. /**
  88. * @}
  89. */
  90. /** @addtogroup GPIO_Private_FunctionPrototypes
  91. * @{
  92. */
  93. /**
  94. * @}
  95. */
  96. /** @addtogroup GPIO_Private_Functions
  97. * @{
  98. */
  99. /**
  100. * @brief Deinitializes the GPIOx peripheral registers to their default reset values.
  101. * @param GPIOx where x can be (A..G) to select the GPIO peripheral.
  102. */
  103. void GPIO_DeInit(GPIO_Module* GPIOx)
  104. {
  105. /* Check the parameters */
  106. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  107. if (GPIOx == GPIOA)
  108. {
  109. RCC_EnableAPB2PeriphReset(RCC_APB2_PERIPH_GPIOA, ENABLE);
  110. RCC_EnableAPB2PeriphReset(RCC_APB2_PERIPH_GPIOA, DISABLE);
  111. }
  112. else if (GPIOx == GPIOB)
  113. {
  114. RCC_EnableAPB2PeriphReset(RCC_APB2_PERIPH_GPIOB, ENABLE);
  115. RCC_EnableAPB2PeriphReset(RCC_APB2_PERIPH_GPIOB, DISABLE);
  116. }
  117. else if (GPIOx == GPIOC)
  118. {
  119. RCC_EnableAPB2PeriphReset(RCC_APB2_PERIPH_GPIOC, ENABLE);
  120. RCC_EnableAPB2PeriphReset(RCC_APB2_PERIPH_GPIOC, DISABLE);
  121. }
  122. else if (GPIOx == GPIOD)
  123. {
  124. RCC_EnableAPB2PeriphReset(RCC_APB2_PERIPH_GPIOD, ENABLE);
  125. RCC_EnableAPB2PeriphReset(RCC_APB2_PERIPH_GPIOD, DISABLE);
  126. }
  127. else if (GPIOx == GPIOE)
  128. {
  129. RCC_EnableAPB2PeriphReset(RCC_APB2_PERIPH_GPIOE, ENABLE);
  130. RCC_EnableAPB2PeriphReset(RCC_APB2_PERIPH_GPIOE, DISABLE);
  131. }
  132. else if (GPIOx == GPIOF)
  133. {
  134. RCC_EnableAPB2PeriphReset(RCC_APB2_PERIPH_GPIOF, ENABLE);
  135. RCC_EnableAPB2PeriphReset(RCC_APB2_PERIPH_GPIOF, DISABLE);
  136. }
  137. else if (GPIOx == GPIOG)
  138. {
  139. RCC_EnableAPB2PeriphReset(RCC_APB2_PERIPH_GPIOG, ENABLE);
  140. RCC_EnableAPB2PeriphReset(RCC_APB2_PERIPH_GPIOG, DISABLE);
  141. }
  142. else
  143. {
  144. }
  145. }
  146. /**
  147. * @brief Deinitializes the Alternate Functions (remap, event control
  148. * and EXTI configuration) registers to their default reset values.
  149. */
  150. void GPIO_AFIOInitDefault(void)
  151. {
  152. RCC_EnableAPB2PeriphReset(RCC_APB2_PERIPH_AFIO, ENABLE);
  153. RCC_EnableAPB2PeriphReset(RCC_APB2_PERIPH_AFIO, DISABLE);
  154. }
  155. /**
  156. * @brief Initializes the GPIOx peripheral according to the specified
  157. * parameters in the GPIO_InitStruct.
  158. * @param GPIOx where x can be (A..G) to select the GPIO peripheral.
  159. * @param GPIO_InitStruct pointer to a GPIO_InitType structure that
  160. * contains the configuration information for the specified GPIO peripheral.
  161. */
  162. void GPIO_InitPeripheral(GPIO_Module* GPIOx, GPIO_InitType* GPIO_InitStruct)
  163. {
  164. uint32_t currentmode = 0x00, currentpin = 0x00, pinpos = 0x00, pos = 0x00;
  165. uint32_t tmpregister = 0x00, pinmask = 0x00;
  166. /* Check the parameters */
  167. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  168. assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
  169. assert_param(IS_GPIO_PIN(GPIO_InitStruct->Pin));
  170. /*---------------------------- GPIO Mode Configuration -----------------------*/
  171. currentmode = ((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x0F);
  172. if ((((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x10)) != 0x00)
  173. {
  174. /* Check the parameters */
  175. assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));
  176. /* Output mode */
  177. currentmode |= (uint32_t)GPIO_InitStruct->GPIO_Speed;
  178. }
  179. /*---------------------------- GPIO PL_CFG Configuration ------------------------*/
  180. /* Configure the eight low port pins */
  181. if (((uint32_t)GPIO_InitStruct->Pin & ((uint32_t)0x00FF)) != 0x00)
  182. {
  183. tmpregister = GPIOx->PL_CFG;
  184. for (pinpos = 0x00; pinpos < 0x08; pinpos++)
  185. {
  186. pos = ((uint32_t)0x01) << pinpos;
  187. /* Get the port pins position */
  188. currentpin = (GPIO_InitStruct->Pin) & pos;
  189. if (currentpin == pos)
  190. {
  191. pos = pinpos << 2;
  192. /* Clear the corresponding low control register bits */
  193. pinmask = ((uint32_t)0x0F) << pos;
  194. tmpregister &= ~pinmask;
  195. /* Write the mode configuration in the corresponding bits */
  196. tmpregister |= (currentmode << pos);
  197. /* Reset the corresponding POD bit */
  198. if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)
  199. {
  200. GPIOx->PBC = (((uint32_t)0x01) << pinpos);
  201. }
  202. else
  203. {
  204. /* Set the corresponding POD bit */
  205. if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)
  206. {
  207. GPIOx->PBSC = (((uint32_t)0x01) << pinpos);
  208. }
  209. }
  210. }
  211. }
  212. GPIOx->PL_CFG = tmpregister;
  213. }
  214. /*---------------------------- GPIO PH_CFG Configuration ------------------------*/
  215. /* Configure the eight high port pins */
  216. if (GPIO_InitStruct->Pin > 0x00FF)
  217. {
  218. tmpregister = GPIOx->PH_CFG;
  219. for (pinpos = 0x00; pinpos < 0x08; pinpos++)
  220. {
  221. pos = (((uint32_t)0x01) << (pinpos + 0x08));
  222. /* Get the port pins position */
  223. currentpin = ((GPIO_InitStruct->Pin) & pos);
  224. if (currentpin == pos)
  225. {
  226. pos = pinpos << 2;
  227. /* Clear the corresponding high control register bits */
  228. pinmask = ((uint32_t)0x0F) << pos;
  229. tmpregister &= ~pinmask;
  230. /* Write the mode configuration in the corresponding bits */
  231. tmpregister |= (currentmode << pos);
  232. /* Reset the corresponding POD bit */
  233. if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)
  234. {
  235. GPIOx->PBC = (((uint32_t)0x01) << (pinpos + 0x08));
  236. }
  237. /* Set the corresponding POD bit */
  238. if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)
  239. {
  240. GPIOx->PBSC = (((uint32_t)0x01) << (pinpos + 0x08));
  241. }
  242. }
  243. }
  244. GPIOx->PH_CFG = tmpregister;
  245. }
  246. }
  247. /**
  248. * @brief Fills each GPIO_InitStruct member with its default value.
  249. * @param GPIO_InitStruct pointer to a GPIO_InitType structure which will
  250. * be initialized.
  251. */
  252. void GPIO_InitStruct(GPIO_InitType* GPIO_InitStruct)
  253. {
  254. /* Reset GPIO init structure parameters values */
  255. GPIO_InitStruct->Pin = GPIO_PIN_ALL;
  256. GPIO_InitStruct->GPIO_Speed = GPIO_Speed_2MHz;
  257. GPIO_InitStruct->GPIO_Mode = GPIO_Mode_IN_FLOATING;
  258. }
  259. /**
  260. * @brief Reads the specified input port pin.
  261. * @param GPIOx where x can be (A..G) to select the GPIO peripheral.
  262. * @param Pin specifies the port bit to read.
  263. * This parameter can be GPIO_Pin_x where x can be (0..15).
  264. * @return The input port pin value.
  265. */
  266. uint8_t GPIO_ReadInputDataBit(GPIO_Module* GPIOx, uint16_t Pin)
  267. {
  268. uint8_t bitstatus = 0x00;
  269. /* Check the parameters */
  270. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  271. assert_param(IS_GET_GPIO_PIN(Pin));
  272. if ((GPIOx->PID & Pin) != (uint32_t)Bit_RESET)
  273. {
  274. bitstatus = (uint8_t)Bit_SET;
  275. }
  276. else
  277. {
  278. bitstatus = (uint8_t)Bit_RESET;
  279. }
  280. return bitstatus;
  281. }
  282. /**
  283. * @brief Reads the specified GPIO input data port.
  284. * @param GPIOx where x can be (A..G) to select the GPIO peripheral.
  285. * @return GPIO input data port value.
  286. */
  287. uint16_t GPIO_ReadInputData(GPIO_Module* GPIOx)
  288. {
  289. /* Check the parameters */
  290. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  291. return ((uint16_t)GPIOx->PID);
  292. }
  293. /**
  294. * @brief Reads the specified output data port bit.
  295. * @param GPIOx where x can be (A..G) to select the GPIO peripheral.
  296. * @param Pin specifies the port bit to read.
  297. * This parameter can be GPIO_Pin_x where x can be (0..15).
  298. * @return The output port pin value.
  299. */
  300. uint8_t GPIO_ReadOutputDataBit(GPIO_Module* GPIOx, uint16_t Pin)
  301. {
  302. uint8_t bitstatus = 0x00;
  303. /* Check the parameters */
  304. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  305. assert_param(IS_GET_GPIO_PIN(Pin));
  306. if ((GPIOx->POD & Pin) != (uint32_t)Bit_RESET)
  307. {
  308. bitstatus = (uint8_t)Bit_SET;
  309. }
  310. else
  311. {
  312. bitstatus = (uint8_t)Bit_RESET;
  313. }
  314. return bitstatus;
  315. }
  316. /**
  317. * @brief Reads the specified GPIO output data port.
  318. * @param GPIOx where x can be (A..G) to select the GPIO peripheral.
  319. * @return GPIO output data port value.
  320. */
  321. uint16_t GPIO_ReadOutputData(GPIO_Module* GPIOx)
  322. {
  323. /* Check the parameters */
  324. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  325. return ((uint16_t)GPIOx->POD);
  326. }
  327. /**
  328. * @brief Sets the selected data port bits.
  329. * @param GPIOx where x can be (A..G) to select the GPIO peripheral.
  330. * @param Pin specifies the port bits to be written.
  331. * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
  332. */
  333. void GPIO_SetBits(GPIO_Module* GPIOx, uint16_t Pin)
  334. {
  335. /* Check the parameters */
  336. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  337. assert_param(IS_GPIO_PIN(Pin));
  338. GPIOx->PBSC = Pin;
  339. }
  340. void GPIO_SetBitsHigh16(GPIO_Module* GPIOx, uint32_t Pin)
  341. {
  342. /* Check the parameters */
  343. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  344. // assert_param(IS_GPIO_PIN(Pin));
  345. GPIOx->PBSC = Pin;
  346. }
  347. /**
  348. * @brief Clears the selected data port bits.
  349. * @param GPIOx where x can be (A..G) to select the GPIO peripheral.
  350. * @param Pin specifies the port bits to be written.
  351. * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
  352. */
  353. void GPIO_ResetBits(GPIO_Module* GPIOx, uint16_t Pin)
  354. {
  355. /* Check the parameters */
  356. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  357. assert_param(IS_GPIO_PIN(Pin));
  358. GPIOx->PBC = Pin;
  359. }
  360. /**
  361. * @brief Sets or clears the selected data port bit.
  362. * @param GPIOx where x can be (A..G) to select the GPIO peripheral.
  363. * @param Pin specifies the port bit to be written.
  364. * This parameter can be one of GPIO_Pin_x where x can be (0..15).
  365. * @param BitCmd specifies the value to be written to the selected bit.
  366. * This parameter can be one of the Bit_OperateType enum values:
  367. * @arg Bit_RESET to clear the port pin
  368. * @arg Bit_SET to set the port pin
  369. */
  370. void GPIO_WriteBit(GPIO_Module* GPIOx, uint16_t Pin, Bit_OperateType BitCmd)
  371. {
  372. /* Check the parameters */
  373. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  374. assert_param(IS_GET_GPIO_PIN(Pin));
  375. assert_param(IS_GPIO_BIT_OPERATE(BitCmd));
  376. if (BitCmd != Bit_RESET)
  377. {
  378. GPIOx->PBSC = Pin;
  379. }
  380. else
  381. {
  382. GPIOx->PBC = Pin;
  383. }
  384. }
  385. /**
  386. * @brief Writes data to the specified GPIO data port.
  387. * @param GPIOx where x can be (A..G) to select the GPIO peripheral.
  388. * @param PortVal specifies the value to be written to the port output data register.
  389. */
  390. void GPIO_Write(GPIO_Module* GPIOx, uint16_t PortVal)
  391. {
  392. /* Check the parameters */
  393. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  394. GPIOx->POD = PortVal;
  395. }
  396. /**
  397. * @brief Locks GPIO Pins configuration registers.
  398. * @param GPIOx where x can be (A..G) to select the GPIO peripheral.
  399. * @param Pin specifies the port bit to be written.
  400. * This parameter can be any combination of GPIO_Pin_x where x can be (0..15).
  401. */
  402. void GPIO_ConfigPinLock(GPIO_Module* GPIOx, uint16_t Pin)
  403. {
  404. uint32_t tmp = 0x00010000;
  405. /* Check the parameters */
  406. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  407. assert_param(IS_GPIO_PIN(Pin));
  408. tmp |= Pin;
  409. /* Set LCKK bit */
  410. GPIOx->PLOCK_CFG = tmp;
  411. /* Reset LCKK bit */
  412. GPIOx->PLOCK_CFG = Pin;
  413. /* Set LCKK bit */
  414. GPIOx->PLOCK_CFG = tmp;
  415. /* Read LCKK bit*/
  416. tmp = GPIOx->PLOCK_CFG;
  417. /* Read LCKK bit*/
  418. tmp = GPIOx->PLOCK_CFG;
  419. }
  420. /**
  421. * @brief Selects the GPIO pin used as Event output.
  422. * @param PortSource selects the GPIO port to be used as source
  423. * for Event output.
  424. * This parameter can be GPIO_PortSourceGPIOx where x can be (A..E).
  425. * @param PinSource specifies the pin for the Event output.
  426. * This parameter can be GPIO_PinSourcex where x can be (0..15).
  427. */
  428. void GPIO_ConfigEventOutput(uint8_t PortSource, uint8_t PinSource)
  429. {
  430. uint32_t tmpregister = 0x00;
  431. /* Check the parameters */
  432. assert_param(IS_GPIO_EVENTOUT_PORT_SOURCE(PortSource));
  433. assert_param(IS_GPIO_PIN_SOURCE(PinSource));
  434. tmpregister = AFIO->ECTRL;
  435. /* Clear the PORT[6:4] and PIN[3:0] bits */
  436. tmpregister &= EVCR_PORTPINCONFIG_MASK;
  437. tmpregister |= (uint32_t)PortSource << 0x04;
  438. tmpregister |= PinSource;
  439. AFIO->ECTRL = tmpregister;
  440. }
  441. /**
  442. * @brief Enables or disables the Event Output.
  443. * @param Cmd new state of the Event output.
  444. * This parameter can be: ENABLE or DISABLE.
  445. */
  446. void GPIO_CtrlEventOutput(FunctionalState Cmd)
  447. {
  448. /* Check the parameters */
  449. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  450. *(__IO uint32_t*)EVCR_EVOE_BB = (uint32_t)Cmd;
  451. }
  452. /**
  453. * @brief Changes the mapping of the specified pin.
  454. * @param RmpPin selects the pin to remap.
  455. * This parameter can be one of the following values:
  456. * @arg GPIO_RMP_SPI1 SPI1 Alternate Function mapping
  457. * @arg GPIO_RMP_I2C1 I2C1 Alternate Function mapping
  458. * @arg GPIO_RMP_USART1 USART1 Alternate Function mapping
  459. * @arg GPIO_RMP_USART2 USART2 Alternate Function mapping
  460. * @arg GPIO_PART_RMP_USART3 USART3 Partial Alternate Function mapping
  461. * @arg GPIO_ALL_RMP_USART3 USART3 Full Alternate Function mapping
  462. * @arg GPIO_PART1_RMP_TIM1 TIM1 Partial Alternate Function mapping
  463. * @arg GPIO_PART2_RMP_TIM1 TIM1 Partial Alternate Function mapping
  464. * @arg GPIO_ALL_RMP_TIM1 TIM1 Full Alternate Function mapping
  465. * @arg GPIO_PartialRemap1_TIM2 TIM2 Partial1 Alternate Function mapping
  466. * @arg GPIO_PART2_RMP_TIM2 TIM2 Partial2 Alternate Function mapping
  467. * @arg GPIO_ALL_RMP_TIM2 TIM2 Full Alternate Function mapping
  468. * @arg GPIO_PART1_RMP_TIM3 TIM3 Partial Alternate Function mapping
  469. * @arg GPIO_ALL_RMP_TIM3 TIM3 Full Alternate Function mapping
  470. * @arg GPIO_RMP_TIM4 TIM4 Alternate Function mapping
  471. * @arg GPIO_RMP1_CAN1 CAN1 Alternate Function mapping
  472. * @arg GPIO_RMP2_CAN1 CAN1 Alternate Function mapping
  473. * @arg GPIO_RMP3_CAN1 CAN1 Alternate Function mapping
  474. * @arg GPIO_RMP_PD01 PD01 Alternate Function mapping
  475. * @arg GPIO_RMP_TIM5CH4 LSI connected to TIM5 Channel4 input capture for calibration
  476. * @arg GPIO_RMP_ADC1_ETRI ADC1 External Trigger Injected Conversion remapping
  477. * @arg GPIO_RMP_ADC1_ETRR ADC1 External Trigger Regular Conversion remapping
  478. * @arg GPIO_RMP_ADC2_ETRI ADC2 External Trigger Injected Conversion remapping
  479. * @arg GPIO_RMP_ADC2_ETRR ADC2 External Trigger Regular Conversion remapping
  480. * @arg GPIO_RMP_SW_JTAG_NO_NJTRST Full SWJ Enabled (JTAG-DP + SW-DP) but without JTRST
  481. * @arg GPIO_RMP_SW_JTAG_SW_ENABLE JTAG-DP Disabled and SW-DP Enabled
  482. * @arg GPIO_RMP_SW_JTAG_DISABLE Full SWJ Disabled (JTAG-DP + SW-DP)
  483. * @arg GPIO_RMP_SDIO SDIO Alternate Function mapping
  484. * @arg GPIO_RMP1_CAN2 CAN2 Alternate Function mapping
  485. * @arg GPIO_RMP3_CAN2 CAN2 Alternate Function mapping
  486. * @arg GPIO_RMP1_QSPI QSPI Alternate Function mapping
  487. * @arg GPIO_RMP3_QSPI QSPI Alternate Function mapping
  488. * @arg GPIO_RMP1_I2C2 I2C2 Alternate Function mapping
  489. * @arg GPIO_RMP3_I2C2 I2C2 Alternate Function mapping
  490. * @arg GPIO_RMP2_I2C3 I2C3 Alternate Function mapping
  491. * @arg GPIO_RMP3_I2C3 I2C3 Alternate Function mapping
  492. * @arg GPIO_RMP1_I2C4 I2C4 Alternate Function mapping
  493. * @arg GPIO_RMP3_I2C4 I2C4 Alternate Function mapping
  494. * @arg GPIO_RMP1_SPI2 SPI2 Alternate Function mapping
  495. * @arg GPIO_RMP2_SPI2 SPI2 Alternate Function mapping
  496. * @arg GPIO_RMP1_SPI3 SPI3 Alternate Function mapping
  497. * @arg GPIO_RMP2_SPI3 SPI3 Alternate Function mapping
  498. * @arg GPIO_RMP1_ETH ETH Alternate Function mapping
  499. * @arg GPIO_RMP2_ETH ETH Alternate Function mapping
  500. * @arg GPIO_RMP3_ETH ETH Alternate Function mapping
  501. * @arg GPIO_RMP1_SPI1 SPI1 Alternate Function mapping
  502. * @arg GPIO_RMP2_SPI1 SPI1 Alternate Function mapping
  503. * @arg GPIO_RMP3_SPI1 SPI1 Alternate Function mapping
  504. * @arg GPIO_RMP1_USART2 USART2 Alternate Function mapping
  505. * @arg GPIO_RMP2_USART2 USART2 Alternate Function mapping
  506. * @arg GPIO_RMP3_USART2 USART2 Alternate Function mapping
  507. * @arg GPIO_RMP1_UART4 UART4 Alternate Function mapping
  508. * @arg GPIO_RMP2_UART4 UART4 Alternate Function mapping
  509. * @arg GPIO_RMP3_UART4 UART4 Alternate Function mapping
  510. * @arg GPIO_RMP1_UART5 UART5 Alternate Function mapping
  511. * @arg GPIO_RMP2_UART5 UART5 Alternate Function mapping
  512. * @arg GPIO_RMP3_UART5 UART5 Alternate Function mapping
  513. * @arg GPIO_RMP2_UART6 UART6 Alternate Function mapping
  514. * @arg GPIO_RMP3_UART6 UART6 Alternate Function mapping
  515. * @arg GPIO_RMP1_UART7 UART7 Alternate Function mapping
  516. * @arg GPIO_RMP3_UART7 UART7 Alternate Function mapping
  517. * @arg GPIO_RMP1_TIM8 TIM8 Alternate Function mapping
  518. * @arg GPIO_RMP3_TIM8 TIM8 Alternate Function mapping
  519. * @arg GPIO_RMP1_COMP1 COMP1 Alternate Function mapping
  520. * @arg GPIO_RMP2_COMP1 COMP1 Alternate Function mapping
  521. * @arg GPIO_RMP3_COMP1 COMP1 Alternate Function mapping
  522. * @arg GPIO_RMP1_COMP2 COMP2 Alternate Function mapping
  523. * @arg GPIO_RMP2_COMP2 COMP2 Alternate Function mapping
  524. * @arg GPIO_RMP3_COMP2 COMP2 Alternate Function mapping
  525. * @arg GPIO_RMP1_COMP3 COMP3 Alternate Function mapping
  526. * @arg GPIO_RMP3_COMP3 COMP3 Alternate Function mapping
  527. * @arg GPIO_RMP1_COMP4 COMP4 Alternate Function mapping
  528. * @arg GPIO_RMP3_COMP4 COMP4 Alternate Function mapping
  529. * @arg GPIO_RMP1_COMP5 COMP5 Alternate Function mapping
  530. * @arg GPIO_RMP2_COMP5 COMP5 Alternate Function mapping
  531. * @arg GPIO_RMP3_COMP5 COMP5 Alternate Function mapping
  532. * @arg GPIO_RMP3_UART5 UART5 Alternate Function mapping
  533. * @arg GPIO_RMP1_COMP6 COMP6 Alternate Function mapping
  534. * @arg GPIO_RMP3_COMP6 COMP6 Alternate Function mapping
  535. * @arg GPIO_RMP_COMP7 COMP7 Alternate Function mapping
  536. * @arg GPIO_RMP_ADC3_ETRI ADC3_ETRGINJ Alternate Function mapping
  537. * @arg GPIO_RMP_ADC3_ETRR ADC3_ETRGREG Alternate Function mapping
  538. * @arg GPIO_RMP_ADC4_ETRI ADC4_ETRGINJ Alternate Function mapping
  539. * @arg GPIO_RMP_ADC4_ETRR ADC4_ETRGREG Alternate Function mapping
  540. * @arg GPIO_RMP_TSC_OUT_CTRL TSC_OUT_CTRL Alternate Function mapping
  541. * @arg GPIO_RMP_QSPI_XIP_EN QSPI_XIP_EN Alternate Function mapping
  542. * @arg GPIO_RMP1_DVP DVP Alternate Function mapping
  543. * @arg GPIO_RMP3_DVP DVP Alternate Function mapping
  544. * @arg GPIO_Remap_SPI1_NSS SPI1 NSS Alternate Function mapping
  545. * @arg GPIO_Remap_SPI2_NSS SPI2 NSS Alternate Function mapping
  546. * @arg GPIO_Remap_SPI3_NSS SPI3 NSS Alternate Function mapping
  547. * @arg GPIO_Remap_QSPI_MISO QSPI MISO Alternate Function mapping
  548. * @arg GPIO_Remap_DET_EN_EGB4 EGB4 Detect Alternate Function mapping
  549. * @arg GPIO_Remap_DET_EN_EGB3 EGB3 Detect Alternate Function mapping
  550. * @arg GPIO_Remap_DET_EN_EGB2 EGB2 Detect Alternate Function mapping
  551. * @arg GPIO_Remap_DET_EN_EGB1 EGB1 Detect Alternate Function mapping
  552. * @arg GPIO_Remap_DET_EN_EGBN4 EGBN4 Detect Alternate Function mapping
  553. * @arg GPIO_Remap_DET_EN_EGBN3 EGBN3 Detect Alternate Function mapping
  554. * @arg GPIO_Remap_DET_EN_EGBN2 EGBN2 Detect Alternate Function mapping
  555. * @arg GPIO_Remap_DET_EN_EGBN1 EGBN1 Detect Alternate Function mapping
  556. * @arg GPIO_Remap_DET_EN_ECLAMP4 ECLAMP4 Detect Alternate Function mapping
  557. * @arg GPIO_Remap_DET_EN_ECLAMP3 ECLAMP3 Detect Alternate Function mapping
  558. * @arg GPIO_Remap_DET_EN_ECLAMP2 ECLAMP2 Detect Alternate Function mapping
  559. * @arg GPIO_Remap_DET_EN_ECLAMP1 ECLAMP1 Detect Alternate Function mapping
  560. * @arg GPIO_Remap_RST_EN_EGB4 EGB4 Reset Alternate Function mapping
  561. * @arg GPIO_Remap_RST_EN_EGB3 EGB3 Reset Alternate Function mapping
  562. * @arg GPIO_Remap_RST_EN_EGB2 EGB2 Reset Alternate Function mapping
  563. * @arg GPIO_Remap_RST_EN_EGB1 EGB1 Reset Alternate Function mapping
  564. * @arg GPIO_Remap_RST_EN_EGBN4 EGBN4 Reset Alternate Function mapping
  565. * @arg GPIO_Remap_RST_EN_EGBN3 EGBN3 Reset Alternate Function mapping
  566. * @arg GPIO_Remap_RST_EN_EGBN2 EGBN2 Reset Alternate Function mapping
  567. * @arg GPIO_Remap_RST_EN_EGBN1 EGBN1 Reset Alternate Function mapping
  568. * @arg GPIO_Remap_RST_EN_ECLAMP4 ECLAMP4 Reset Alternate Function mapping
  569. * @arg GPIO_Remap_RST_EN_ECLAMP3 ECLAMP3 Reset Alternate Function mapping
  570. * @arg GPIO_Remap_RST_EN_ECLAMP2 ECLAMP2 Reset Alternate Function mapping
  571. * @arg GPIO_Remap_RST_EN_ECLAMP1 ECLAMP1 Reset Alternate Function mapping
  572. * @param Cmd new state of the port pin remapping.
  573. * This parameter can be: ENABLE or DISABLE.
  574. */
  575. void GPIO_ConfigPinRemap(uint32_t RmpPin, FunctionalState Cmd)
  576. {
  577. uint32_t tmp = 0x00, tmp1 = 0x00, tmpregister = 0x00, tmpmask = 0x00, tmp2 = 0x00;
  578. /* Check the parameters */
  579. assert_param(IS_GPIO_REMAP(RmpPin));
  580. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  581. /* Check RmpPin relate AFIO RMP_CFG */
  582. if ((RmpPin & 0x40000000) == 0x40000000)
  583. {
  584. tmpregister = AFIO->RMP_CFG3;
  585. }
  586. else if ((RmpPin & 0x20000000) == 0x20000000)
  587. {
  588. tmpregister = AFIO->RMP_CFG4;
  589. }
  590. else if ((RmpPin & 0x10000000) == 0x10000000)
  591. {
  592. tmpregister = AFIO->RMP_CFG5;
  593. }
  594. else
  595. {
  596. tmpregister = AFIO->RMP_CFG;
  597. }
  598. tmpmask = (RmpPin & DBGAFR_POSITION_MASK) >> 16;
  599. tmp = RmpPin & LSB_MASK;
  600. if ((RmpPin
  601. & (DBGAFR_NUMBITS_MAPR5_MASK | DBGAFR_NUMBITS_MAPR4_MASK | DBGAFR_NUMBITS_MAPR3_MASK | DBGAFR_LOCATION_MASK
  602. | DBGAFR_NUMBITS_MASK))
  603. == (DBGAFR_LOCATION_MASK | DBGAFR_NUMBITS_MASK))
  604. {
  605. tmpregister &= DBGAFR_SWJCFG_MASK;
  606. AFIO->RMP_CFG &= DBGAFR_SWJCFG_MASK;
  607. }
  608. else if ((RmpPin & DBGAFR_NUMBITS_MASK) == DBGAFR_NUMBITS_MASK)
  609. {
  610. if ((RmpPin & DBGAFR_LOCATION_MASK) == DBGAFR_LOCATION_MASK)
  611. {
  612. tmp1 = (((uint32_t)0x03) << tmpmask) << 16;
  613. }
  614. else
  615. {
  616. tmp1 = ((uint32_t)0x03) << tmpmask;
  617. }
  618. tmpregister &= ~tmp1;
  619. if ((RmpPin & 0x70000000) == 0x00000000)
  620. {
  621. tmpregister |= ~DBGAFR_SWJCFG_MASK;
  622. }
  623. }
  624. else
  625. {/*configuration AFIO RMP_CFG*/
  626. if ((RmpPin & DBGAFR_NUMBITS_SPI1_MASK) == DBGAFR_NUMBITS_SPI1_MASK)
  627. {
  628. if ((RmpPin & 0x00000004) == 0x00000004)
  629. {
  630. if ((RmpPin & 0x02000000) == 0x02000000) // GPIO_RMP3_SPI1
  631. {
  632. tmpregister &= ~(tmp << (((RmpPin & 0x00200000) >> 21) * 16));
  633. if (Cmd != DISABLE)
  634. {
  635. tmp2 = AFIO->RMP_CFG;
  636. tmp2 |= 0x00000001;
  637. tmp2 |= ~DBGAFR_SWJCFG_MASK;
  638. AFIO->RMP_CFG = tmp2; // Remap_SPI1 ENABLE
  639. }
  640. else
  641. {
  642. tmp2 = AFIO->RMP_CFG;
  643. tmp2 &= 0xFFFFFFFE;
  644. tmp2 |= ~DBGAFR_SWJCFG_MASK;
  645. AFIO->RMP_CFG = tmp2; // Remap_SPI1 DISABLE
  646. }
  647. }
  648. else
  649. {
  650. tmpregister &= ~(tmp << (((RmpPin & 0x00200000) >> 21) * 16)); // GPIO_RMP2_SPI1
  651. tmp2 = AFIO->RMP_CFG;
  652. tmp2 &= 0xFFFFFFFE;
  653. tmp2 |= ~DBGAFR_SWJCFG_MASK;
  654. AFIO->RMP_CFG = tmp2; // Remap_SPI1 DISABLE
  655. }
  656. }
  657. else
  658. {
  659. tmpregister &= ~((tmp | 0x00000004) << (((RmpPin & 0x00200000) >> 21) * 16)); // clear
  660. if (Cmd != DISABLE) // GPIO_RMP1_SPI1
  661. {
  662. tmp2 = AFIO->RMP_CFG;
  663. tmp2 |= 0x00000001;
  664. tmp2 |= ~DBGAFR_SWJCFG_MASK;
  665. AFIO->RMP_CFG = tmp2; // Remap_SPI1 ENABLE
  666. }
  667. else
  668. {
  669. tmp2 = AFIO->RMP_CFG;
  670. tmp2 &= 0xFFFFFFFE;
  671. tmp2 |= ~DBGAFR_SWJCFG_MASK;
  672. AFIO->RMP_CFG = tmp2; // Remap_SPI1 DISABLE
  673. }
  674. }
  675. }
  676. else if ((RmpPin & DBGAFR_NUMBITS_USART2_MASK) == DBGAFR_NUMBITS_USART2_MASK)
  677. {
  678. if ((RmpPin & 0x00000008) == 0x00000008)
  679. {
  680. if ((RmpPin & 0x02000000) == 0x02000000) // GPIO_RMP3_USART2
  681. {
  682. tmpregister &= ~(tmp << (((RmpPin & 0x00200000) >> 21) * 16));
  683. if (Cmd != DISABLE)
  684. {
  685. tmp2 = AFIO->RMP_CFG;
  686. tmp2 |= 0x00000008;
  687. tmp2 |= ~DBGAFR_SWJCFG_MASK;
  688. AFIO->RMP_CFG = tmp2; // Remap_USART2 ENABLE
  689. }
  690. else
  691. {
  692. tmp2 = AFIO->RMP_CFG;
  693. tmp2 &= 0xFFFFFFF7;
  694. tmp2 |= ~DBGAFR_SWJCFG_MASK;
  695. AFIO->RMP_CFG = tmp2; // Remap_USART2 DISABLE
  696. }
  697. }
  698. else
  699. {
  700. tmpregister &= ~(tmp << (((RmpPin & 0x00200000) >> 21) * 16)); // GPIO_RMP2_USART2
  701. tmp2 = AFIO->RMP_CFG;
  702. tmp2 &= 0xFFFFFFF7;
  703. tmp2 |= ~DBGAFR_SWJCFG_MASK;
  704. AFIO->RMP_CFG = tmp2; // Remap_USART2 DISABLE
  705. }
  706. }
  707. else // GPIO_RMP1_USART2
  708. {
  709. tmpregister &= ~((tmp | 0x00000008) << (((RmpPin & 0x00200000) >> 21) * 16)); // clear
  710. if (Cmd != DISABLE)
  711. {
  712. tmp2 = AFIO->RMP_CFG;
  713. tmp2 |= 0x00000008;
  714. tmp2 |= ~DBGAFR_SWJCFG_MASK;
  715. AFIO->RMP_CFG = tmp2; // Remap_USART2 ENABLE
  716. }
  717. else
  718. {
  719. tmp2 = AFIO->RMP_CFG;
  720. tmp2 &= 0xFFFFFFF7;
  721. tmp2 |= ~DBGAFR_SWJCFG_MASK;
  722. AFIO->RMP_CFG = tmp2; // Remap_USART2 DISABLE
  723. }
  724. }
  725. }
  726. else
  727. {
  728. tmpregister &= ~(tmp << (((RmpPin & 0x00200000) >> 21) * 16));
  729. if ((RmpPin & 0x70000000) == 0x00000000)
  730. {
  731. tmpregister |= ~DBGAFR_SWJCFG_MASK;
  732. }
  733. }
  734. }
  735. /*configuration AFIO RMP_CFG~RMP_CFG5*/
  736. if (Cmd != DISABLE)
  737. {
  738. tmpregister |= (tmp << (((RmpPin & 0x00200000) >> 21) * 16));
  739. }
  740. if ((RmpPin & 0x40000000) == 0x40000000)
  741. {
  742. AFIO->RMP_CFG3 = tmpregister;
  743. }
  744. else if ((RmpPin & 0x20000000) == 0x20000000)
  745. {
  746. AFIO->RMP_CFG4 = tmpregister;
  747. }
  748. else if ((RmpPin & 0x10000000) == 0x10000000)
  749. {
  750. AFIO->RMP_CFG5 = tmpregister;
  751. }
  752. else
  753. {
  754. AFIO->RMP_CFG = tmpregister;
  755. }
  756. }
  757. /**
  758. * @brief Selects the GPIO pin used as EXTI Line.
  759. * @param PortSource selects the GPIO port to be used as source for EXTI lines.
  760. * This parameter can be GPIO_PortSourceGPIOx where x can be (A..G).
  761. * @param PinSource specifies the EXTI line to be configured.
  762. * This parameter can be GPIO_PinSourcex where x can be (0..15).
  763. */
  764. void GPIO_ConfigEXTILine(uint8_t PortSource, uint8_t PinSource)
  765. {
  766. uint32_t tmp = 0x00;
  767. /* Check the parameters */
  768. assert_param(IS_GPIO_EXTI_PORT_SOURCE(PortSource));
  769. assert_param(IS_GPIO_PIN_SOURCE(PinSource));
  770. tmp = ((uint32_t)0x0F) << (0x04 * (PinSource & (uint8_t)0x03));
  771. AFIO->EXTI_CFG[PinSource >> 0x02] &= ~tmp;
  772. AFIO->EXTI_CFG[PinSource >> 0x02] |= (((uint32_t)PortSource) << (0x04 * (PinSource & (uint8_t)0x03)));
  773. }
  774. /**
  775. * @brief Selects the Ethernet media interface.
  776. * @note This function applies only to N32G45x Connectivity line devices.
  777. * @param ETH_ConfigSel specifies the Media Interface mode.
  778. * This parameter can be one of the following values:
  779. * @arg GPIO_ETH_MII_CFG MII mode
  780. * @arg GPIO_ETH_RMII_CFG RMII mode
  781. */
  782. void GPIO_ETH_ConfigMediaInterface(uint32_t ETH_ConfigSel)
  783. {
  784. /* Check the parameters */
  785. assert_param(IS_GPIO_ETH_MEDIA_INTERFACE(ETH_ConfigSel));
  786. if (ETH_ConfigSel == GPIO_ETH_RMII_CFG)
  787. {
  788. *(__IO uint32_t*)MAPR_MII_RMII_SEL_BB = (uint32_t)1;
  789. }
  790. else
  791. {
  792. *(__IO uint32_t*)MAPR_MII_RMII_SEL_BB = (uint32_t)0;
  793. }
  794. }
  795. /**
  796. * @}
  797. */
  798. /**
  799. * @}
  800. */
  801. /**
  802. * @}
  803. */