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