pins_arduino.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-09-20 liYony first version
  9. */
  10. #include <Arduino.h>
  11. #include "pins_arduino.h"
  12. #include <drv_gpio.h>
  13. #define DBG_TAG "RTduino.pins_arduino"
  14. #define DBG_LVL DBG_INFO
  15. #include <rtdbg.h>
  16. /*
  17. * {Arduino Pin, RT-Thread Pin [, Device Name, Channel]}
  18. * [] means optional
  19. * Digital pins must NOT give the device name and channel.
  20. * Analog pins MUST give the device name and channel(ADC, PWM or DAC).
  21. * Arduino Pin must keep in sequence.
  22. */
  23. const pin_map_t pin_map_table[]=
  24. {
  25. {D0, GET_PIN(G,9), "uart3"}, /* Serial-Rx */
  26. {D1, GET_PIN(G,14), "uart3"}, /* Serial-Tx */
  27. {D2, GET_PIN(G,13)},
  28. {D3, GET_PIN(A,1), "pwm2", 2}, /* PWM */
  29. {D4, GET_PIN(G,12)},
  30. {D5, GET_PIN(A,2), "pwm2", 3}, /* PWM */
  31. {D6, GET_PIN(A,6), "pwm3", 1}, /* PWM */
  32. {D7, GET_PIN(G,11)},
  33. {D8, GET_PIN(G,10)},
  34. {D9, GET_PIN(A,7), "pwm3", 2}, /* PWM */
  35. {D10, GET_PIN(H,6), "pwm12", 1}, /* PWM */
  36. {D11, GET_PIN(B,15), "pwm12", 2}, /* PWM */
  37. {D12, GET_PIN(B,14)},
  38. {D13, GET_PIN(D,3)}, /* LED_BUILTIN */
  39. {D14, GET_PIN(B,9), "i2c1"}, /* I2C-SDA (Wire) */
  40. {D15, GET_PIN(B,8), "i2c1"}, /* I2C-SCL (Wire) */
  41. {D16, GET_PIN(A,0)}, /* user button */
  42. {D17, GET_PIN(G,6)}, /* user LED */
  43. {D18, GET_PIN(D,4)}, /* user LED */
  44. {D19, GET_PIN(D,5)}, /* user LED */
  45. {D20, GET_PIN(K,3)}, /* user LED */
  46. {A0, GET_PIN(B,1), "adc1", 9}, /* ADC */
  47. {A1, GET_PIN(C,2), "adc1", 12}, /* ADC */
  48. {A2, GET_PIN(C,3), "adc1", 13}, /* ADC */
  49. {A3, GET_PIN(C,4), "adc1", 14}, /* ADC */
  50. {A4, GET_PIN(C,5), "adc1", 15}, /* ADC */
  51. {A5, GET_PIN(A,4), "adc1", 4}, /* ADC */
  52. {A6, RT_NULL, "adc1", RT_ADC_INTERN_CH_VREF}, /* ADC, On-Chip: internal reference voltage */
  53. {A7, RT_NULL, "adc1", RT_ADC_INTERN_CH_TEMPER}, /* ADC, On-Chip: internal temperature sensor */
  54. };
  55. #ifdef RTDUINO_USING_SPI
  56. void switchToSPI(const char *bus_name)
  57. {
  58. GPIO_InitTypeDef GPIO_InitStruct = {0};
  59. if(!rt_strcmp(bus_name, "spi2"))
  60. {
  61. __HAL_RCC_SPI2_CLK_ENABLE();
  62. __HAL_RCC_GPIOD_CLK_ENABLE();
  63. __HAL_RCC_GPIOB_CLK_ENABLE();
  64. HAL_GPIO_DeInit(GPIOD, GPIO_PIN_3);
  65. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_14 | GPIO_PIN_15);
  66. /**SPI2 GPIO Configuration
  67. PD3 ------> SPI2_SCK
  68. PB14 ------> SPI2_MISO
  69. PB15 ------> SPI2_MOSI
  70. */
  71. GPIO_InitStruct.Pin = GPIO_PIN_3;
  72. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  73. GPIO_InitStruct.Pull = GPIO_NOPULL;
  74. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  75. GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
  76. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  77. GPIO_InitStruct.Pin = GPIO_PIN_14|GPIO_PIN_15;
  78. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  79. GPIO_InitStruct.Pull = GPIO_NOPULL;
  80. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  81. GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
  82. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  83. LOG_I("D11, D12 and D13 will switch from PWM to SPI");
  84. }
  85. }
  86. #endif /* RTDUINO_USING_SPI */