hal_gpio.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include <stdint.h>
  2. #include <mk20dx128.h>
  3. #include "teensy_hal.h"
  4. #define GPIO_NUMBER 32
  5. void HAL_GPIO_Init(GPIO_TypeDef *GPIOx, GPIO_InitTypeDef *GPIO_Init)
  6. {
  7. /* Check the parameters */
  8. assert_param(IS_GPIO_PIN(GPIO_Init->Pin));
  9. assert_param(IS_GPIO_MODE(GPIO_Init->Mode));
  10. assert_param(IS_GPIO_PULL(GPIO_Init->Pull));
  11. /* Configure the port pins */
  12. for (uint32_t position = 0; position < GPIO_NUMBER; position++) {
  13. uint32_t bitmask = 1 << position;
  14. if ((GPIO_Init->Pin & bitmask) == 0) {
  15. continue;
  16. }
  17. volatile uint32_t *port_pcr = GPIO_PIN_TO_PORT_PCR(GPIOx, position);
  18. /*--------------------- GPIO Mode Configuration ------------------------*/
  19. /* In case of Alternate function mode selection */
  20. if ((GPIO_Init->Mode == GPIO_MODE_AF_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_OD)) {
  21. /* Check the Alternate function parameter */
  22. assert_param(IS_GPIO_AF(GPIO_Init->Alternate));
  23. }
  24. else if (GPIO_Init->Mode == GPIO_MODE_ANALOG) {
  25. GPIO_Init->Alternate = 0;
  26. }
  27. else {
  28. GPIO_Init->Alternate = 1;
  29. }
  30. /* Configure Alternate function mapped with the current IO */
  31. *port_pcr &= ~PORT_PCR_MUX_MASK;
  32. *port_pcr |= PORT_PCR_MUX(GPIO_Init->Alternate);
  33. /* Configure IO Direction mode (Input, Output, Alternate or Analog) */
  34. if (GPIO_Init->Mode == GPIO_MODE_INPUT || GPIO_Init->Mode == GPIO_MODE_ANALOG) {
  35. GPIOx->PDDR &= ~bitmask;
  36. } else {
  37. GPIOx->PDDR |= bitmask;
  38. }
  39. /* In case of Output or Alternate function mode selection */
  40. if ((GPIO_Init->Mode == GPIO_MODE_OUTPUT_PP) || (GPIO_Init->Mode == GPIO_MODE_AF_PP) ||
  41. (GPIO_Init->Mode == GPIO_MODE_OUTPUT_OD) || (GPIO_Init->Mode == GPIO_MODE_AF_OD)) {
  42. /* Check the Speed parameter */
  43. assert_param(IS_GPIO_SPEED(GPIO_Init->Speed));
  44. *port_pcr |= PORT_PCR_DSE;
  45. /* Configure the IO Speed */
  46. if (GPIO_Init->Speed > GPIO_SPEED_FREQ_MEDIUM) {
  47. *port_pcr &= ~PORT_PCR_SRE;
  48. } else {
  49. *port_pcr |= PORT_PCR_SRE;
  50. }
  51. /* Configure the IO Output Type */
  52. if (GPIO_Init->Mode & GPIO_OUTPUT_TYPE) {
  53. *port_pcr |= PORT_PCR_ODE; // OD
  54. } else {
  55. *port_pcr &= ~PORT_PCR_ODE; // PP
  56. }
  57. } else {
  58. *port_pcr &= ~PORT_PCR_DSE;
  59. }
  60. /* Activate the Pull-up or Pull down resistor for the current IO */
  61. if (GPIO_Init->Pull == GPIO_NOPULL) {
  62. *port_pcr &= ~PORT_PCR_PE;
  63. } else {
  64. *port_pcr |= PORT_PCR_PE;
  65. if (GPIO_Init->Pull == GPIO_PULLDOWN) {
  66. *port_pcr &= ~PORT_PCR_PS;
  67. } else {
  68. *port_pcr |= PORT_PCR_PS;
  69. }
  70. }
  71. #if 0
  72. /*--------------------- EXTI Mode Configuration ------------------------*/
  73. /* Configure the External Interrupt or event for the current IO */
  74. if((GPIO_Init->Mode & EXTI_MODE) == EXTI_MODE)
  75. {
  76. /* Enable SYSCFG Clock */
  77. __SYSCFG_CLK_ENABLE();
  78. temp = ((uint32_t)0x0F) << (4 * (position & 0x03));
  79. SYSCFG->EXTICR[position >> 2] &= ~temp;
  80. SYSCFG->EXTICR[position >> 2] |= ((uint32_t)(__HAL_GET_GPIO_SOURCE(GPIOx)) << (4 * (position & 0x03)));
  81. /* Clear EXTI line configuration */
  82. EXTI->IMR &= ~((uint32_t)iocurrent);
  83. EXTI->EMR &= ~((uint32_t)iocurrent);
  84. if((GPIO_Init->Mode & GPIO_MODE_IT) == GPIO_MODE_IT)
  85. {
  86. EXTI->IMR |= iocurrent;
  87. }
  88. if((GPIO_Init->Mode & GPIO_MODE_EVT) == GPIO_MODE_EVT)
  89. {
  90. EXTI->EMR |= iocurrent;
  91. }
  92. /* Clear Rising Falling edge configuration */
  93. EXTI->RTSR &= ~((uint32_t)iocurrent);
  94. EXTI->FTSR &= ~((uint32_t)iocurrent);
  95. if((GPIO_Init->Mode & RISING_EDGE) == RISING_EDGE)
  96. {
  97. EXTI->RTSR |= iocurrent;
  98. }
  99. if((GPIO_Init->Mode & FALLING_EDGE) == FALLING_EDGE)
  100. {
  101. EXTI->FTSR |= iocurrent;
  102. }
  103. }
  104. #endif
  105. }
  106. }