hal_gpio.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * BSD 2-Clause License
  3. * Copyright (c) 2022, LiteEMF
  4. * All rights reserved.
  5. * This software component is licensed by LiteEMF under BSD 2-Clause license,
  6. * the "License"; You may not use this file except in compliance with the
  7. * License. You may obtain a copy of the License at:
  8. * opensource.org/licenses/BSD-2-Clause
  9. *
  10. */
  11. /************************************************************************************************************
  12. ** Description:
  13. ************************************************************************************************************/
  14. #include "api/api_gpio.h"
  15. #include "py32f002b_ll_bus.h"
  16. /******************************************************************************************************
  17. ** Defined
  18. *******************************************************************************************************/
  19. /******************************************************************************************************
  20. ** static Parameters
  21. *******************************************************************************************************/
  22. /******************************************************************************************************
  23. ** public Parameters
  24. *******************************************************************************************************/
  25. /*****************************************************************************************************
  26. ** static Function
  27. ******************************************************************************************************/
  28. /*****************************************************************************************************
  29. ** Function
  30. ******************************************************************************************************/
  31. /*******************************************************************
  32. ** Parameters:
  33. ** Returns:
  34. ** Description:
  35. *******************************************************************/
  36. GPIO_TypeDef * get_gpio_port(pin_t io)
  37. {
  38. GPIO_TypeDef * base = NULL;
  39. switch (io&HW_PORT_MASK)
  40. {
  41. case HW_PORTA_BASE:
  42. base = GPIOA;
  43. break;
  44. case HW_PORTB_BASE:
  45. base = GPIOB;
  46. break;
  47. case HW_PORTC_BASE:
  48. base = GPIOC;
  49. break;
  50. default:
  51. break;
  52. }
  53. return base;
  54. }
  55. uint32_t get_gpio_rcc(pin_t io)
  56. {
  57. uint32_t rcc = 0;
  58. switch (io&HW_PORT_MASK)
  59. {
  60. case HW_PORTA_BASE:
  61. LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA);
  62. break;
  63. case HW_PORTB_BASE:
  64. LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB);
  65. break;
  66. case HW_PORTC_BASE:
  67. LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOC);
  68. break;
  69. default:
  70. break;
  71. }
  72. return rcc;
  73. }
  74. uint32_t get_gpio_pin(pin_t io)
  75. {
  76. return 1<<(io&HW_PIN_MASK);
  77. }
  78. void hal_gpio_mode(pin_t pin, uint8_t mode)
  79. {
  80. }
  81. void hal_gpio_dir(pin_t pin, pin_dir_t dir, pin_pull_t pull)
  82. {
  83. if((uint32_t)-1 == pin) return;
  84. GPIO_TypeDef * gpiox = get_gpio_port((pin_t)pin);
  85. LL_GPIO_InitTypeDef GPIO_InitStructure = {0};
  86. get_gpio_rcc((pin_t)pin);
  87. GPIO_InitStructure.Pin = get_gpio_pin(pin);
  88. GPIO_InitStructure.Mode = (dir == PIN_OUT ? LL_GPIO_MODE_OUTPUT : LL_GPIO_MODE_INPUT);
  89. GPIO_InitStructure.Speed = LL_GPIO_SPEED_FREQ_LOW;
  90. GPIO_InitStructure.OutputType = (pull == PIN_PULL_OD ? LL_GPIO_OUTPUT_OPENDRAIN : LL_GPIO_OUTPUT_PUSHPULL);
  91. GPIO_InitStructure.Pull = (pull & 0x2);
  92. GPIO_InitStructure.Alternate = LL_GPIO_AF_7;
  93. LL_GPIO_Init(get_gpio_port((pin_t)pin), &GPIO_InitStructure);
  94. }
  95. uint32_t hal_gpio_in(pin_t pin)
  96. {
  97. return LL_GPIO_IsInputPinSet(get_gpio_port(pin), get_gpio_pin(pin));
  98. }
  99. void hal_gpio_out(pin_t pin, uint8_t value)
  100. {
  101. if(value){
  102. LL_GPIO_SetOutputPin(get_gpio_port(pin), get_gpio_pin(pin));
  103. }else{
  104. LL_GPIO_ResetOutputPin(get_gpio_port(pin), get_gpio_pin(pin));
  105. }
  106. }