1 /*
2  * Copyright (c) 2006-2024, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2024-01-25     ShichengChu  first version
9  */
10 
11 #include <Arduino.h>
12 #include <board.h>
13 #include "pins_arduino.h"
14 #include <pico/stdlib.h>
15 #include <hardware/spi.h>
16 #include <hardware/gpio.h>
17 #include <pico/binary_info.h>
18 
19 #define DBG_TAG    "RTduino.pins_arduino"
20 #define DBG_LVL    DBG_INFO
21 #include <rtdbg.h>
22 
23 /*
24  * {Arduino Pin, RT-Thread Pin [, Device Name, Channel]}
25  * [] means optional
26  * Digital pins must NOT give the device name and channel.
27  * Analog pins MUST give the device name and channel(ADC, PWM or DAC).
28  * Arduino Pin must keep in sequence.
29  */
30 const pin_map_t pin_map_table[]=
31 {
32     {D0, 0, "uart0"},                                  /* Serial-TX */
33     {D1, 1, "uart0"},                                  /* Serial-RX */
34     {D2, 2},
35     {D3, 3},
36     {D4, 4, "i2c0"},                                   /* I2C-SDA (Wire) */
37     {D5, 5, "i2c0"},                                   /* I2C-SCL (Wire) */
38     {D6, 6},
39     {D7, 7},
40     {D8, 8, "uart1"},                                  /* Serial2-TX */
41     {D9, 9, "uart1"},                                  /* Serial2-RX */
42     {D10, 10, "pwm5", 0},                              /* PWM */
43     {D11, 11, "pwm5", 1},                              /* PWM */
44     {D12, 12, "pwm6", 0},                              /* PWM */
45     {D13, 13, "pwm6", 1},                              /* PWM */
46     {D14, 14, "pwm7", 0},                              /* PWM */
47     {D15, 15, "pwm7", 1},                              /* PWM */
48     {D16, 16, "pwm0", 0},                              /* PWM */
49     {D17, 17, "pwm0", 1},                              /* PWM */
50     {D18, 18, "pwm1", 0},                              /* PWM */
51     {D19, 19, "pwm1", 1},                              /* PWM */
52     {D20, 20, "pwm2", 0},                              /* PWM */
53     {D21, 21, "pwm2", 1},                              /* PWM */
54     {D22, 22, "pwm3", 0},                              /* PWM */
55     {D23, 23, "pwm3", 1},                              /* PWM */
56     {D24, 24, "pwm4", 0},                              /* PWM */
57     {D25, 25, "pwm4", 1},                              /* LED_BUILTIN */
58     {A0, 26, "adc0", 0},                               /* ADC */
59     {A1, 27, "adc1", 1},                               /* ADC */
60     {A2, 28, "adc2", 2},                               /* ADC */
61 };
62 
63 #ifdef RTDUINO_USING_SPI
switchToSPI(const char * bus_name)64 void switchToSPI(const char *bus_name)
65 {
66     if(!rt_strcmp(bus_name, "spi0"))
67     {
68         /**SPI0 GPIO Configuration
69         18u     ------> SPI0_SCK
70         16u     ------> SPI0_MISO
71         19u     ------> SPI0_MOSI
72         17u     ------> SPI0_CS
73         */
74         gpio_set_function(BSP_SPI0_SCK_PIN, GPIO_FUNC_SPI);
75         gpio_set_function(BSP_SPI0_MISO_PIN, GPIO_FUNC_SPI);
76         gpio_set_function(BSP_SPI0_MOSI_PIN, GPIO_FUNC_SPI);
77         gpio_init(BSP_SPI0_CS_PIN);
78         // Make the SPI pins available to picotool
79         bi_decl(bi_3pins_with_func(BSP_SPI0_MISO_PIN, BSP_SPI0_MOSI_PIN, BSP_SPI0_SCK_PIN, GPIO_FUNC_SPI));
80         // Make the CS pin available to picotool
81         bi_decl(bi_1pin_with_name(BSP_SPI0_CS_PIN, "SPI CS"));
82 
83         LOG_I("D16, D17, D18 and D19 will switch from PWM to SPI");
84     }
85 }
86 #endif /* RTDUINO_USING_SPI */
87