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 
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(G,9), "uart3"},        /* Serial-Rx */
29     {D1, GET_PIN(G,14), "uart3"},       /* Serial-Tx */
30     {D2, GET_PIN(G,13)},
31     {D3, GET_PIN(A,1), "pwm2", 2},      /* PWM */
32     {D4, GET_PIN(G,12)},
33     {D5, GET_PIN(A,2), "pwm2", 3},      /* PWM */
34     {D6, GET_PIN(A,6), "pwm3", 1},      /* PWM */
35     {D7, GET_PIN(G,11)},
36     {D8, GET_PIN(G,10)},
37     {D9, GET_PIN(A,7), "pwm3", 2},      /* PWM */
38     {D10, GET_PIN(H,6), "pwm12", 1},    /* PWM */
39     {D11, GET_PIN(B,15), "pwm12", 2},   /* PWM */
40     {D12, GET_PIN(B,14)},
41     {D13, GET_PIN(D,3)},                /* 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(A,0)},                /* user button */
45     {D17, GET_PIN(G,6)},                /* user LED */
46     {D18, GET_PIN(D,4)},                /* user LED */
47     {D19, GET_PIN(D,5)},                /* user LED */
48     {D20, GET_PIN(K,3)},                /* user LED */
49     {A0, GET_PIN(B,1), "adc1", 9},      /* ADC */
50     {A1, GET_PIN(C,2), "adc1", 12},     /* ADC */
51     {A2, GET_PIN(C,3), "adc1", 13},     /* ADC */
52     {A3, GET_PIN(C,4), "adc1", 14},     /* ADC */
53     {A4, GET_PIN(C,5), "adc1", 15},     /* ADC */
54     {A5, GET_PIN(A,4), "adc1", 4},      /* ADC */
55     {A6, RT_NULL, "adc1", RT_ADC_INTERN_CH_VREF},   /* ADC, On-Chip: internal reference voltage */
56     {A7, RT_NULL, "adc1", RT_ADC_INTERN_CH_TEMPER}, /* ADC, On-Chip: internal temperature sensor */
57 };
58 
59 #ifdef RTDUINO_USING_SPI
switchToSPI(const char * bus_name)60 void switchToSPI(const char *bus_name)
61 {
62     GPIO_InitTypeDef GPIO_InitStruct = {0};
63 
64     if(!rt_strcmp(bus_name, "spi2"))
65     {
66         __HAL_RCC_SPI2_CLK_ENABLE();
67 
68         __HAL_RCC_GPIOD_CLK_ENABLE();
69         __HAL_RCC_GPIOB_CLK_ENABLE();
70 
71         HAL_GPIO_DeInit(GPIOD, GPIO_PIN_3);
72         HAL_GPIO_DeInit(GPIOB, GPIO_PIN_14 | GPIO_PIN_15);
73 
74         /**SPI2 GPIO Configuration
75         PD3     ------> SPI2_SCK
76         PB14     ------> SPI2_MISO
77         PB15     ------> SPI2_MOSI
78         */
79         GPIO_InitStruct.Pin = GPIO_PIN_3;
80         GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
81         GPIO_InitStruct.Pull = GPIO_NOPULL;
82         GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
83         GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
84         HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
85 
86         GPIO_InitStruct.Pin = GPIO_PIN_14|GPIO_PIN_15;
87         GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
88         GPIO_InitStruct.Pull = GPIO_NOPULL;
89         GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
90         GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
91         HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
92 
93         LOG_I("D11, D12 and D13 will switch from PWM to SPI");
94     }
95 }
96 #endif /* RTDUINO_USING_SPI */
97