1 /**************************************************************************//**
2 *
3 * @copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Change Logs:
8 * Date            Author       Notes
9 * 2020-1-16       Wayne        First version
10 *
11 ******************************************************************************/
12 
13 #include <rtdevice.h>
14 #include <drv_gpio.h>
15 
16 
17 
18 #if defined(BOARD_USING_ESP8266)
rt_hw_esp8266_port(void)19 static int rt_hw_esp8266_port(void)
20 {
21     rt_base_t esp_rst_pin = NU_GET_PININDEX(NU_PC, 13);
22     rt_base_t esp_fwupdate_pin = NU_GET_PININDEX(NU_PD, 12);
23 
24     /* ESP8266 reset pin PC.13 */
25     rt_pin_mode(esp_rst_pin, PIN_MODE_OUTPUT);
26     rt_pin_write(esp_rst_pin, 1);
27 
28     /* ESP8266 reset pin PD.12 */
29     rt_pin_mode(esp_fwupdate_pin, PIN_MODE_OUTPUT);
30     rt_pin_write(esp_fwupdate_pin, 1);
31 
32     return 0;
33 }
34 INIT_APP_EXPORT(rt_hw_esp8266_port);
35 #endif /* BOARD_USING_ESP8266  */
36 
37 #if defined(BOARD_USING_LCD_ILI9341) && defined(NU_PKG_USING_ILI9341_SPI)
38 
39 #if defined(NU_PKG_USING_ADC_TOUCH_SW)
40 
41 #include "adc_touch.h"
42 #include "touch_sw.h"
43 #include "NuMicro.h"
44 
45 #define NU_MFP_POS(PIN)          ((PIN % 8) * 4)
46 #define NU_MFP_MSK(PIN)          (0xful << NU_MFP_POS(PIN))
47 
48 S_CALIBRATION_MATRIX g_sCalMat = { 52, 6247, -2804852, 4917, 47, -2309716, 65536 };
49 
nu_pin_func(rt_base_t pin,int data)50 static void nu_pin_func(rt_base_t pin, int data)
51 {
52     uint32_t pin_index      = NU_GET_PINS(pin);
53     uint32_t port_index     = NU_GET_PORT(pin);
54     __IO uint32_t *GPx_MFPx = ((__IO uint32_t *) &SYS->GPA_MFPL) + port_index * 2 + (pin_index / 8);
55     uint32_t MFP_Msk        = NU_MFP_MSK(pin_index);
56 
57     *GPx_MFPx  = (*GPx_MFPx & (~MFP_Msk)) | data;
58 }
59 
tp_switch_to_analog(rt_base_t pin)60 static void tp_switch_to_analog(rt_base_t pin)
61 {
62     GPIO_T *port = (GPIO_T *)(GPIOA_BASE + (0x40) * NU_GET_PORT(pin));
63 
64     if (pin == NU_GET_PININDEX(NU_PB, 11))
65         nu_pin_func(pin, SYS_GPB_MFPH_PB11MFP_EADC0_CH11);
66     else if (pin == NU_GET_PININDEX(NU_PB, 8))
67         nu_pin_func(pin, SYS_GPB_MFPH_PB8MFP_EADC0_CH8);
68 
69     GPIO_DISABLE_DIGITAL_PATH(port, NU_GET_PIN_MASK(NU_GET_PINS(pin)));
70 }
71 
tp_switch_to_digital(rt_base_t pin)72 static void tp_switch_to_digital(rt_base_t pin)
73 {
74     GPIO_T *port = (GPIO_T *)(GPIOA_BASE + (0x40) * NU_GET_PORT(pin));
75 
76     nu_pin_func(pin, 0);
77 
78     /* Enable digital path on these EADC pins */
79     GPIO_ENABLE_DIGITAL_PATH(port, NU_GET_PIN_MASK(NU_GET_PINS(pin)));
80 }
81 
82 static S_TOUCH_SW sADCTP =
83 {
84     .adc_name    = "eadc0",
85     .i32ADCChnYU = 11,
86     .i32ADCChnXR = 8,
87     .pin =
88     {
89         NU_GET_PININDEX(NU_PB, 10), // XL
90         NU_GET_PININDEX(NU_PB, 11), // YU
91         NU_GET_PININDEX(NU_PB, 8), // XR
92         NU_GET_PININDEX(NU_PB, 9), // YD
93     },
94     .switch_to_analog  = tp_switch_to_analog,
95     .switch_to_digital = tp_switch_to_digital,
96 };
97 
98 #endif
99 
100 #include <lcd_ili9341.h>
101 #if defined(PKG_USING_GUIENGINE)
102     #include <rtgui/driver.h>
103 #endif
rt_hw_ili9341_port(void)104 int rt_hw_ili9341_port(void)
105 {
106     if (rt_hw_lcd_ili9341_spi_init("spi1", RT_NULL) != RT_EOK)
107         return -1;
108 
109     rt_hw_lcd_ili9341_init();
110 
111 #if defined(PKG_USING_GUIENGINE)
112     rt_device_t lcd_ili9341;
113     lcd_ili9341 = rt_device_find("lcd");
114     if (lcd_ili9341)
115     {
116         rtgui_graphic_set_device(lcd_ili9341);
117     }
118 #endif
119 
120 #if defined(NU_PKG_USING_ADC_TOUCH_SW)
121     nu_adc_touch_sw_register(&sADCTP);
122 #endif
123 
124     return 0;
125 }
126 INIT_COMPONENT_EXPORT(rt_hw_ili9341_port);
127 #endif /* BOARD_USING_LCD_ILI9341 */
128