Parcourir la source

1、修改支持rtdm_gpio驱动 2、适配fuxi 开出量配置,注意打开宏:NW_AREA_MAIN_2021

ygl il y a 1 mois
Parent
commit
c90e256e92

+ 3 - 0
.gitignore

@@ -4,3 +4,6 @@ dtu/dtu_main_t536/ko
 .cache
 compile_commands.json
 dtu/dtu_main_t536/compile_commands.json
+/*.code-workspace
+/dtu/dtu_main_t536/*.o
+/dtu/dtu_main_t536/dtu_t536

+ 322 - 85
app_public/fuxi_public/fuxi_bsp/source/gpio.c

@@ -8,82 +8,23 @@
 其它说明:
 修改记录:
 
-[old]-->drop
-gpio0: a0111000.gpio -> gpiochip480 -> GPIO0_0
-gpio1: a0112000.gpio -> gpiochip448 -> GPIO1_0
-gpio2: a0387000.gpio -> gpiochip416 -> GPIO2_0
-gpio3: a0388000.gpio -> gpiochip384 -> GPIO3_0
-gpio4: a0919000.gpio -> gpiochip352 -> GPIO4_0
-gpio5: a091a000.gpio -> gpiochip320 -> GPIO5_0
-gpio6: a091b000.gpio -> gpiochip288 -> GPIO6_0
-gpio7: a091c000.gpio -> gpiochip256 -> GPIO7_0
-
-PDO0_1		--> GPIO5_22	-->  gpio342
-PDO1_1		--> GPIO5_23	-->  gpio343
-PDO2_1		--> GPIO5_24	-->  gpio344
-PDO3_1		--> GPIO5_25	-->  gpio345
-KOUT0		--> GPIO4_29	-->  gpio381
-KOUT1		--> GPIO4_30	-->  gpio382
-KOUT2		--> GPIO4_31	-->  gpio383
-KOUT3		--> GPIO5_0		-->  gpio320
-KOUT4		--> GPIO5_1		-->  gpio321
-KOUT5		--> GPIO5_2		-->  gpio322
-KOUT6		--> GPIO5_3		-->  gpio323
-KOUT7		--> GPIO5_4		-->  gpio324
-KOUT8		--> GPIO5_5		-->  gpio325
-KOUT9		--> GPIO5_6		-->  gpio326
-// sunxi 20220530 KOUT10		--> GPIO5_7		-->  gpio327
-// sunxi 20220530 KOUT_EN0	--> GPIO5_8		-->  gpio328
-// sunxi 20220530 KOUT_EN1	--> GPIO5_9		-->  gpio329
-KOUT10		--> GPIO2_21	-->  gpio437
-KOUT_EN0	--> GPIO3_18	-->  gpio402
-KOUT_EN1	--> GPIO3_19	-->  gpio403
-RUN led		--> GPIO3_17	-->  gpio401
-ERR led		--> GPIO3_20	-->  gpio404
-// sunxi 20220609 esam_pwr    --> GPIO0_10	-->	 gpio490
-esam_pwr    --> GPIO2_21	-->	 gpio437
-WTD			--> GPIO0_31	-->  gpio511
-
 [new]-->applay
-gpio0: a0111000.gpio -> gpiochip0 -> GPIO0_0
-gpio1: a0112000.gpio -> gpiochip32 -> GPIO1_0
-gpio2: a0387000.gpio -> gpiochip64 -> GPIO2_0
-gpio3: a0388000.gpio -> gpiochip96 -> GPIO3_0
-gpio4: a0919000.gpio -> gpiochip128 -> GPIO4_0
-gpio5: a091a000.gpio -> gpiochip160 -> GPIO5_0
-gpio6: a091b000.gpio -> gpiochip192 -> GPIO6_0
-gpio7: a091c000.gpio -> gpiochip224 -> GPIO7_0
-
-PDO0_1		--> GPIO5_22	-->  gpio182
-PDO1_1		--> GPIO5_23	-->  gpio183
-PDO2_1		--> GPIO5_24	-->  gpio184
-PDO3_1		--> GPIO5_25	-->  gpio185
-KOUT0		--> GPIO4_29	-->  gpio157
-KOUT1		--> GPIO4_30	-->  gpio158
-KOUT2		--> GPIO4_31	-->  gpio159
-KOUT3		--> GPIO5_0		-->  gpio160
-KOUT4		--> GPIO5_1		-->  gpio161
-KOUT5		--> GPIO5_2		-->  gpio162
-KOUT6		--> GPIO5_3		-->  gpio163
-KOUT7		--> GPIO5_4		-->  gpio164
-KOUT8		--> GPIO5_5		-->  gpio165
-KOUT9		--> GPIO5_6		-->  gpio166
-// zhaoyang 20231214 KOUT10		--> GPIO5_7		-->  gpio167
-// zhaoyang 20231214 KOUT_EN0	--> GPIO5_8		-->  gpio168
-// zhaoyang 20231214 KOUT_EN1	--> GPIO5_9		-->  gpio169
-KOUT10		--> GPIO2_21	-->  gpio85
-KOUT_EN0	--> GPIO3_18	-->  gpio114
-KOUT_EN1	--> GPIO3_19	-->  gpio115
-RUN led		--> GPIO3_17	-->  gpio113
-ERR led		--> GPIO3_20	-->  gpio116
-// zhaoyang 20231214 esam_pwr    --> GPIO0_10	-->	 gpio10
-esam_pwr    --> GPIO2_21	-->	 gpio85
-WTD			--> GPIO0_31	-->  gpio31
+B_DO_ADJ	--> GPIOD_17	-->  gpio113
+B_DO_0		--> GPIOD_03	-->  gpio99
+B_DO_1		--> GPIOD_19	-->  gpio115
+B_DO_2		--> GPIOD_02	-->  gpio98
+B_DO_3		--> GPIOD_09	-->  gpio105
+B_DO_4		--> GPIOD_18	-->  gpio114
+B_DO_5		--> GPIOD_16	-->  gpio112
+B_DO_6		--> GPIOD_07	-->  gpio103
+B_DO_7		--> GPIOD_08	-->  gpio104
+B_DO_8		--> GPIOD_06	-->  gpio102
+
 T536 start ↓↓↓↓↓↓
-RUN_LED			-->  PB7
-ERR_LED			-->  PF4
-ESAM_PWR		-->  PL7
-LINE_LOSS_SET	-->  PM5
+RUN_LED			-->  PB7  	-->  gpio39
+ERR_LED			-->  PF4	-->  gpio164
+ESAM_PWR		-->  PL7	-->  gpio159
+LINE_LOSS_SET	-->  PM5	-->  gpio389
 T536 end   ↑↑↑↑↑↑
 */
 
@@ -93,7 +34,7 @@ T536 end   
 #include "gpio.h"
 #include "head.h"
 
-
+#include <rtdm/gpio.h>
 /*------------------------------- 宏定义 --------------------------------------
 */
 #if CFG_BSP_DEBUG
@@ -102,9 +43,13 @@ T536 end   
 
 #endif
 
-
 /*------------------------------ 全局变量 -------------------------------------
 */
+typedef enum {
+    SYS_GPIO = 0,
+	RTDM_GPIO = 1,
+} GPIO_TYPE;
+
 unsigned short kc_pwm0_stu;
 unsigned short kc_pwm1_stu;
 
@@ -118,10 +63,11 @@ extern uint32_t g_yx_buf[2]; //预
 extern unsigned int change_di_ch(unsigned int di);
 
 int gpio_val_fd[DO_NUM];
-
-const int gpio_val[DO_NUM] = {96, 97, 98, 99, 100, 101, 102, 103, 104, 105};
+int gpio_dev_type[DO_NUM] = {SYS_GPIO,};	//0:系统GPIO, 1:RTDM GPIO
+const int gpio_val[DO_NUM] = {98, 99, 115, 98, 105, 114, 112, 103, 104, 102};
 
 int gpio_val_fd_other[GPIO_NUM_OTHER];
+int gpio_dev_type_other[GPIO_NUM_OTHER] = {SYS_GPIO,};	
 const int gpio_val_other[GPIO_NUM_OTHER] ={39,164,359,389};
 
 // static int gpio_watchdog_val = 0;
@@ -129,6 +75,296 @@ static int gpio_run_led_val = 0;
 
 /*------------------------------ 外部函数 -------------------------------------
 */
+#if 1
+int sysfs_gpio_unexport(int gpio) 
+{
+    char path[64];
+    int fd;
+    
+    snprintf(path, sizeof(path), "/sys/class/gpio/unexport");
+    fd = open(path, O_WRONLY);
+    if (fd < 0) {
+        perror("Failed to open unexport");
+        return -1;
+    }
+    
+    char buf[16];
+	snprintf(buf, sizeof(buf), "%d", gpio);
+	if (write(fd, buf, strlen(buf)) < 0) { 
+		perror("Failed to set GPIO");
+        close(fd);
+        return -1;
+    }
+	close(fd);
+	return 0;
+}
+
+int sysfs_gpio_export(int gpio) {
+    char path[64];
+    int fd;
+    
+	// 检查是否已导出
+    snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d", gpio);
+    if (access(path, F_OK) == 0) {
+        sysfs_gpio_unexport(gpio);	//	bugfix: 避免重复导出
+		msleep(50);				//短暂等待让内核处理
+    }
+
+    snprintf(path, sizeof(path), "/sys/class/gpio/export");
+    fd = open(path, O_WRONLY);
+    if (fd < 0) {
+        perror("Failed to open export");
+        return -1;
+    }
+    
+    char buf[16];
+    snprintf(buf, sizeof(buf), "%d", gpio);
+    if (write(fd, buf, strlen(buf)) < 0) {
+        perror("Failed to export GPIO");
+        close(fd);
+        return -1;
+    }
+    
+    close(fd);
+    return 0;
+}
+
+int sysfs_gpio_set_direction(int gpio, int is_output) {
+    char path[64];
+    int fd;
+    
+    snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/direction", gpio);
+    fd = open(path, O_WRONLY);
+    if (fd < 0) {
+        perror("Failed to open direction");
+        return -1;
+    }
+    
+    if (write(fd, is_output ? "out" : "in", is_output ? 3 : 2) < 0) {
+        perror("Failed to set direction");
+        close(fd);
+        return -1;
+    }
+    
+    close(fd);
+    return 0;
+}
+
+int sysfs_gpio_set_edge_detect(int gpio, int edge)
+{
+	char path[64];
+	int fd;
+	
+	snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/edge", gpio);
+	fd = open(path, O_WRONLY);
+	if (fd < 0) { 
+		perror("Failed to open edge");
+		close(fd);
+		return -1;
+    }
+
+	if (write(fd, edge, strlen(edge)) < 0) { 
+		perror("Failed to set edge");
+		close(fd);
+		return -1;
+    }
+
+	close(fd);
+	return 0;
+}
+int sysfs_gpio_set_value(int gpio, int value) {
+    char path[64];
+    int fd = -1;
+    
+    snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", gpio);
+    fd = open(path, O_WRONLY);
+    if (fd < 0) {
+        perror("Failed to open value");
+        return -1;
+    }
+    
+    if (write(fd, value ? "1" : "0", 1) < 0) {
+        perror("Failed to set value");
+        close(fd);
+        return -1;
+    }
+    
+    return fd;
+}
+
+int sysfs_gpio_init(int gpio, int direction)
+{
+	#define GPIO_LOW 0
+	int ret;
+	ret = sysfs_gpio_export(gpio);
+	if(ret < 0)
+	{
+		printf("sysfs_gpio_export error\n");
+		return -1;
+	}
+	ret = sysfs_gpio_set_direction(gpio, direction);
+	if(ret < 0)
+	{
+		printf("sysfs_gpio_set_direction error\n");
+	}
+
+	return sysfs_gpio_set_value(gpio, GPIO_LOW);	// 初始化IO为低电平
+}
+
+int is_rtdm_device(int fd) {
+    
+	if(gpio_dev_type[fd] > SYS_GPIO)
+		return 1;
+	else
+	if(gpio_dev_type[fd] == SYS_GPIO)
+    	return 0;
+	else
+		return -1;
+}
+int rt_write(int fd, char *buf, int len) {
+    int value;
+    
+    if(len <= 0 || buf == NULL) {
+        return -1;
+    }
+    
+    if (is_rtdm_device(fd) == 1) {
+        // Xenomai RTDM设备:需要转换为二进制值
+        value = atoi(buf);
+        if(value != 0 && value != 1) {
+            printf("value :%d ERR, Please enter '0' or '1'\n", value);
+            return -1;
+        }
+        return write(fd, &value, sizeof(value));
+    } 
+	else 
+	{
+        // 常规系统设备:直接传递字符串
+        return write(fd, buf, len);
+    }
+}
+int rtdm_gpio_init(int gpio, int direction)
+{
+	#define GPIO_LOW 0
+	char path[64];
+    int fd = -1, ret;
+    int value = 1;
+    snprintf(path, sizeof(path), "/dev/rtdm/3604000.pinctrl/gpio%d", gpio);
+    fd = open(path, O_RDWR);
+	if (fd < 0) {
+        perror("open");
+        return -1;
+    }
+
+	ret = ioctl(fd, GPIO_RTIOC_DIR_OUT, &value);
+	if (ret < 0) {
+        perror("ioctl DIR_OUT");
+        close(fd);
+        return -1;
+    }
+
+	rt_write(gpio_val_fd[DO_OUT0], "0", 2);	// 初始化IO为低电平
+	return fd;
+}
+
+int gpio_init(void)
+{
+	#define GPIO_OUTPUT 1
+    int i;
+
+#ifdef NW_AREA_MAIN_2021
+    for (i = 0; i < DO_NUM; i ++)
+    {
+		gpio_val_fd[i] = rtdm_gpio_init(gpio_val[i], GPIO_OUTPUT);
+		if(gpio_val_fd[i] < 0)
+		{
+			gpio_val_fd[i] = sysfs_gpio_init(gpio_val[i], GPIO_OUTPUT);
+			if(gpio_val_fd[i] < 0)
+			{
+				printf("gpio_val_fd[%d] error\n", i);
+				gpio_val_fd[i] = -1;
+				gpio_dev_type[i] = -1;
+				return -1;
+			}
+		}
+		else {
+			gpio_dev_type[i] = gpio_val_fd[i];	// 保存RTDM设备类型句柄,这里有风险,fd句柄可能为0
+		}
+    }
+#endif
+     for (i = 0; i < GPIO_NUM_OTHER; i ++)
+    {
+		gpio_val_fd_other[i] = -1;	//rtdm_gpio_init(gpio_val_other[i], GPIO_OUTPUT);
+		if(gpio_val_fd_other[i] < 0)
+		{
+			gpio_val_fd_other[i] = sysfs_gpio_init(gpio_val_other[i], GPIO_OUTPUT);
+			if(gpio_val_fd_other[i] < 0)
+			{
+				printf("gpio_val_fd_other[%d] error\n", i);
+				gpio_val_fd_other[i] = -1;
+				gpio_dev_type_other[i] = -1;
+				return -1;
+			}
+		}
+		else {
+			gpio_dev_type_other[i] = gpio_val_fd_other[i];	// 保存RTDM设备类型句柄,这里有风险,fd句柄可能为0
+		}
+    }
+
+	msleep(10);// 延时10ms
+
+	kc_pwm0_stu = 0;
+	kc_pwm1_stu = 0;
+    kc_kout0_stu=0;
+    kc_kout1_stu=0;
+
+    //其他与底层GPIO相关的配置再此处添加 比如 LED WATCHDOG                 
+    write(gpio_val_fd_other[RUN_LED], "0", 2);
+    gpio_run_led_val = 0;
+
+	//上电默认切换到外部232串口
+	//后续根据投退判断是否需要切换到线损的维护口
+	write(gpio_val_fd_other[LINE_LOSS_SET], "0", 2);	
+
+	//开启esam供电
+	write(gpio_val_fd_other[ESAM_PWR], "0", 2);
+#ifdef NW_AREA_MAIN_2021
+    for (i = 0; i < DO_NUM; i ++)
+    {
+		write(gpio_val_fd[i], "1", 2);
+    }
+#endif
+    return 0;
+}
+
+int gpio_exit(void)
+{
+	int i;
+#ifdef NW_AREA_MAIN_2021
+    for (i = 0; i < DO_NUM; i ++)
+    {
+    	if(gpio_val_fd[i] >= 0)
+    	{
+    		close(gpio_val_fd[i]);
+    		gpio_val_fd[i] = -1;
+    	}
+		sysfs_gpio_unexport(gpio_val[i]);
+    }
+#endif
+    for (i = 0; i < GPIO_NUM_OTHER; i ++)
+    {
+    	if(gpio_val_fd_other[i] >= 0)
+    	{
+    		close(gpio_val_fd_other[i]);
+    		gpio_val_fd_other[i] = -1;
+    	}
+		sysfs_gpio_unexport(gpio_val_other[i]);
+    }
+
+    return 0;
+}
+
+#else
 /******************************************************************************
 函数名称:	gpio_init
 函数版本:	01.01
@@ -153,7 +389,7 @@ int gpio_init(void)
         printf("Cannot open GPIO to export it\n");
         return -1;
     }
-#ifndef GW_AREA_MAIN_2021
+#ifdef NW_AREA_MAIN_2021
     for (i = 0; i < DO_NUM; i ++)
     {
         sprintf(num, "%d", gpio_val[i]);
@@ -167,9 +403,10 @@ int gpio_init(void)
         write(exportfd, num, 4);
         gpio_val_fd_other[i] = -1;
     }
-
+	
     close(exportfd);
-#ifndef GW_AREA_MAIN_2021
+
+#ifdef NW_AREA_MAIN_2021
     for (i = 0; i < DO_NUM; i ++)
     {
         sprintf(path, "/sys/class/gpio/gpio%d/value", gpio_val[i]);
@@ -219,7 +456,7 @@ int gpio_init(void)
             	close(gpio_val_fd_other[j]);
             	gpio_val_fd_other[j] = -1;
             }
-		#ifndef GW_AREA_MAIN_2021
+		#ifdef NW_AREA_MAIN_2021
             for(j = 0; j < DO_NUM; j ++)
             {
             	close(gpio_val_fd[j]);
@@ -264,7 +501,7 @@ int gpio_init(void)
 
 	//开启esam供电
 	write(gpio_val_fd_other[ESAM_PWR], "0", 2);
-#ifndef GW_AREA_MAIN_2021
+#ifdef NW_AREA_MAIN_2021
     for (i = 0; i < DO_NUM; i ++)
     {
 		write(gpio_val_fd[i], "1", 2);
@@ -285,7 +522,7 @@ int gpio_exit(void)
         printf("Cannot open GPIO to unexport it\n");
         return -1;
     }
-#ifndef GW_AREA_MAIN_2021
+#ifdef NW_AREA_MAIN_2021
     for (i = 0; i < DO_NUM; i ++)
     {
     	if(gpio_val_fd[i] >= 0)
@@ -312,7 +549,7 @@ int gpio_exit(void)
 
     return 0;
 }
-
+#endif
 /******************************************************************************
 函数名称:	gpio_get_kin
 函数版本:	01.01

+ 1 - 1
app_public/fuxi_public/fuxi_bsp/source/gps.c

@@ -131,7 +131,7 @@ int gps_init(void)
     fflush(stdout);
 
     g_gps_virt_addr = (char*)g_gps_map_base + offset_in_page;
-    // printf("g_ustimer_virt_addr: %p %p %x\n", g_ustimer_virt_addr, g_ustimer_map_base, offset_in_page);
+    // printf("g_gps_virt_addr: %p %p %x\n", g_gps_virt_addr, g_gps_map_base, offset_in_page);
 
     // Init timer ccu clock
     /* set TMR3 clock source to HOSC, 8 pre-division */

BIN
dtu/dtu_main_t536/dtu_t536


BIN
dtu/dtu_main_t536/main_mod.o