pins_arduino.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #define DBG_TAG "RTduino.pins_arduino"
  13. #define DBG_LVL DBG_INFO
  14. #include <rtdbg.h>
  15. /*
  16. * {Arduino Pin, RT-Thread Pin [, Device Name, Channel]}
  17. * [] means optional
  18. * Digital pins must NOT give the device name and channel.
  19. * Analog pins MUST give the device name and channel(ADC, PWM or DAC).
  20. * Arduino Pin must keep in sequence.
  21. */
  22. const pin_map_t pin_map_table[]=
  23. {
  24. {D0, GET_PIN(A,3), "uart2"}, /* Serial-RX */
  25. {D1, GET_PIN(A,2), "uart2"}, /* Serial-TX */
  26. {D2, GET_PIN(A,10)},
  27. {D3, GET_PIN(B,3), "pwm2", 2}, /* PWM */
  28. {D4, GET_PIN(B,5)},
  29. {D5, GET_PIN(B,4), "pwm3", 1}, /* PWM */
  30. {D6, GET_PIN(B,10), "pwm2", 3}, /* PWM */
  31. {D7, GET_PIN(A,8)},
  32. {D8, GET_PIN(A,9)},
  33. {D9, GET_PIN(C,7), "pwm3", 2}, /* PWM */
  34. {D10, GET_PIN(B,6), "pwm4", 1}, /* PWM */
  35. {D11, GET_PIN(A,7), "pwm1", -1}, /* PWM */
  36. {D12, GET_PIN(A,6)},
  37. {D13, GET_PIN(A,5)}, /* LED_BUILTIN */
  38. {D14, GET_PIN(B,9), "i2c1"}, /* I2C-SDA (Wire) */
  39. {D15, GET_PIN(B,8), "i2c1"}, /* I2C-SCL (Wire) */
  40. {D16, GET_PIN(C,13)},
  41. {A0, GET_PIN(A,0), "adc1", 0}, /* ADC */
  42. {A1, GET_PIN(A,1), "adc1", 1}, /* ADC */
  43. {A2, GET_PIN(A,4), "adc1", 4}, /* ADC */
  44. {A3, GET_PIN(B,0), "adc1", 8}, /* ADC */
  45. {A4, GET_PIN(C,1), "adc1", 11}, /* ADC */
  46. {A5, GET_PIN(C,0), "adc1", 10}, /* ADC */
  47. {A6, RT_NULL, "adc1", RT_ADC_INTERN_CH_VREF}, /* ADC, On-Chip: internal reference voltage */
  48. {A7, RT_NULL, "adc1", RT_ADC_INTERN_CH_TEMPER}, /* ADC, On-Chip: internal temperature sensor */
  49. };
  50. void pins_switch_to_spi(const char *bus_name)
  51. {
  52. if(!rt_strcmp(bus_name, "spi1"))
  53. {
  54. __HAL_RCC_TIM1_CLK_DISABLE();
  55. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_7);
  56. GPIO_InitTypeDef GPIO_InitStruct = {0};
  57. __HAL_RCC_SPI1_CLK_ENABLE();
  58. __HAL_RCC_GPIOA_CLK_ENABLE();
  59. /**SPI1 GPIO Configuration
  60. PA5 ------> SPI1_SCK
  61. PA6 ------> SPI1_MISO
  62. PA7 ------> SPI1_MOSI
  63. */
  64. GPIO_InitStruct.Pin = GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7;
  65. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  66. GPIO_InitStruct.Pull = GPIO_NOPULL;
  67. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  68. GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
  69. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  70. LOG_W("D11, D12 and D13 will switch from PWM to SPI");
  71. }
  72. }