pins_arduino.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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-10-14 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(A,3), "uart2"}, /* Serial-RX */
  26. {D1, GET_PIN(A,2), "uart2"}, /* Serial-TX */
  27. {D2, GET_PIN(A,10)},
  28. {D3, GET_PIN(B,3), "pwm2", 2}, /* PWM */
  29. {D4, GET_PIN(B,5)},
  30. {D5, GET_PIN(B,4), "pwm3", 1}, /* PWM */
  31. {D6, GET_PIN(B,10), "pwm2", 3}, /* PWM */
  32. {D7, GET_PIN(A,8)},
  33. {D8, GET_PIN(A,9)},
  34. {D9, GET_PIN(C,7), "pwm3", 2}, /* PWM */
  35. {D10, GET_PIN(B,6), "pwm4", 1}, /* PWM */
  36. {D11, GET_PIN(A,7), "pwm1", -1}, /* PWM */
  37. {D12, GET_PIN(A,6)},
  38. {D13, GET_PIN(A,5)}, /* 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(C,13)},
  42. {A0, GET_PIN(A,0), "adc1", 0}, /* ADC */
  43. {A1, GET_PIN(A,1), "adc1", 1}, /* ADC */
  44. {A2, GET_PIN(A,4), "adc1", 4}, /* ADC */
  45. {A3, GET_PIN(B,0), "adc1", 8}, /* ADC */
  46. {A4, GET_PIN(C,1), "adc1", 11}, /* ADC */
  47. {A5, GET_PIN(C,0), "adc1", 10}, /* ADC */
  48. {A6, RT_NULL, "adc1", RT_ADC_INTERN_CH_VREF}, /* ADC, On-Chip: internal reference voltage */
  49. {A7, RT_NULL, "adc1", RT_ADC_INTERN_CH_TEMPER}, /* ADC, On-Chip: internal temperature sensor */
  50. };
  51. #ifdef RTDUINO_USING_SPI
  52. void switchToSPI(const char *bus_name)
  53. {
  54. GPIO_InitTypeDef GPIO_InitStruct = {0};
  55. if(!rt_strcmp(bus_name, "spi1"))
  56. {
  57. __HAL_RCC_SPI1_CLK_ENABLE();
  58. __HAL_RCC_GPIOA_CLK_ENABLE();
  59. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5);
  60. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_6);
  61. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_7);
  62. /**SPI1 GPIO Configuration
  63. PA5 ------> SPI1_SCK
  64. PA6 ------> SPI1_MISO
  65. PA7 ------> SPI1_MOSI
  66. */
  67. GPIO_InitStruct.Pin = GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
  68. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  69. GPIO_InitStruct.Pull = GPIO_NOPULL;
  70. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  71. GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
  72. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  73. LOG_W("D11, D12 and D13 will switch from PWM to SPI");
  74. }
  75. }
  76. #endif /* RTDUINO_USING_SPI */