Procházet zdrojové kódy

add adctool, gpiotool and i2ctool

zhangbo před 3 roky
rodič
revize
c40be2f1c0

+ 3 - 0
GD32F450_BMC_BaseCode.initramfs

@@ -163,6 +163,9 @@ file /usr/bin/ncp81111_cfg ${INSTALL_ROOT}/projects/${SAMPLE}/app/NCP81111_Confi
 file /usr/bin/damon.sh ${INSTALL_ROOT}/projects/${SAMPLE}/app/damon.sh 777 0 0
 file /usr/bin/ipmitool ${INSTALL_ROOT}/projects/${SAMPLE}/app/ipmitool-1.8.18/ipmitool 755 0 0
 file /usr/bin/authlicense ${INSTALL_ROOT}/projects/${SAMPLE}/app/AuthLicense/authlicense 755 0 0
+file /usr/bin/adctool ${INSTALL_ROOT}/projects/${SAMPLE}/app/adctool/adctool 755 0 0
+file /usr/bin/gpiotool ${INSTALL_ROOT}/projects/${SAMPLE}/app/gpiotool/gpiotool 755 0 0
+file /usr/bin/i2ctool ${INSTALL_ROOT}/projects/${SAMPLE}/app/i2ctool/i2ctool 755 0 0
 
 dir /etc/goahead 755 0 0 
 dir /var/www 755 0 0

+ 8 - 0
app/Makefile

@@ -21,12 +21,20 @@ all	:
 	make -C ./NCP81111_Config
 	make -C ./ipmitool-1.8.18
 	make -C ./AuthLicense
+	make -C ./adctool
+	make -C ./gpiotool
+	make -C ./i2ctool
+	make -C ./spitool
 clean	: 
 	make -C ./goahead-3.6.5 clean
 	make -C ./bmc clean
 	make -C ./test_app clean
 	make -C ./ipmitool-1.8.18 clean
 	make -C ./UpdateFirmware clean
+	make -C ./adctool clean
+	make -C ./gpiotool clean
+	make -C ./i2ctool clean
+	make -C ./spitool clean
 
 	make -C ./driver/Platform clean
 	make -C ./driver/GPIO clean

+ 49 - 0
app/adctool/Makefile

@@ -0,0 +1,49 @@
+EXEC		= adctool
+
+CFLAGS		:= -Os -mcpu=cortex-m3 -mthumb
+LDFLAGS		:= -mcpu=cortex-m3 -mthumb -L$(INSTALL_ROOT)/A2F/root/usr/lib
+#BUILD_TIME 	= $(shell date +"%Y-%m-%d %H:%M:%S")
+
+
+
+CC	= $(CROSS_COMPILE_APPS)gcc
+
+#source
+SRC	= adctool_main.c
+
+# hal_api
+SRC += $(wildcard ../hal_api/*.c) 
+
+#incldue
+CFLAGS	+= -I$(INSTALL_ROOT)/A2F/root/usr/include
+CFLAGS	+= -I ./
+CFLAGS	+= -I ../common_include
+CFLAGS	+= -I ../driver
+CFLAGS	+= -I ../hal_api
+CFLAGS	+= -I ../driver/STM32F4xx_HAL_Driver
+CFLAGS	+= -I ../driver/STM32F4xx_HAL_Driver/Inc
+CFLAGS	+= -I ../driver/STM32F4xx_HAL_Driver/Inc/Legacy
+
+
+#library
+
+#sub-directory
+#SUBDIR 	= ./sensor
+
+
+
+
+
+
+$(EXEC): $(SRC)
+	#echo $(BUILD_TIME)
+	$(CC) $(LDFLAGS) $(CFLAGS) -o $@ $^ $(LIBS) -lpthread
+
+
+#object
+OBJS	= $(SRC:%.c=%.o)
+
+clean:
+	rm -f $(EXEC) $(OBJS)
+	rm -f *.gdb 
+

binární
app/adctool/adctool


+ 42 - 0
app/adctool/adctool_main.c

@@ -0,0 +1,42 @@
+#include <stdio.h>
+#include <stdint.h>
+#include "driver.h"
+#include "hal_interface_api.h"
+#include <fcntl.h>
+
+/*
+adctool --get-all-channels
+*/
+
+void help()
+{
+	printf("adctool <cmd>\n");
+	printf("    --get-all-channels: read all channels raw data.\n");
+}
+
+int main(int argc, char* argv[])
+{
+	int i;
+	uint16_t buf;
+	if(argc < 2)
+	{
+		help();
+		return -1;
+	}
+
+	if((strcmp(argv[1], "--get-all-channels") == 0) && (argc == 2))
+	{
+		for(i=1;i<=7;i++)
+		{
+			buf = 0;
+			stm32_adc_get_value(i, &buf);
+			printf("ADC%d: %#04x\n", i, buf); 	
+		}
+		
+	}
+	else
+	{
+		help();
+		return -1;
+	}
+}

+ 1 - 1
app/bmc/main.h

@@ -116,7 +116,7 @@ extern int gPendActionIfc;
 extern TLS_T g_tls;
 extern PendingBridgedResTbl_T	m_PendingBridgedResTbl[MAX_PENDING_BRIDGE_TBL][MAX_PENDING_BRIDGE_RES];
 extern PendingSeqNoTbl_T		m_PendingSeqNoTbl[16][MAX_PENDING_SEQ_NO];
-extern KCSBridgeResInfo_T       m_KCSBridgeResInfo;
+//extern KCSBridgeResInfo_T       m_KCSBridgeResInfo;
 extern TimerTaskTbl_T    		m_TimerTaskTbl [20];
 
 extern BMCInfo_t 			g_BMCInfo;

+ 2 - 2
app/bmc/msghndlr/MsgHndlrTask.c

@@ -86,8 +86,8 @@ TimerTaskTbl_T    m_TimerTaskTbl [20] =
 //PendingBridgedResTbl_T	m_PendingBridgedResTbl[MAX_PENDING_BRIDGE_RES];
 
 PendingBridgedResTbl_T	m_PendingBridgedResTbl[MAX_PENDING_BRIDGE_TBL][MAX_PENDING_BRIDGE_RES];
-PendingSeqNoTbl_T	m_PendingSeqNoTbl[16][MAX_PENDING_SEQ_NO];
-KCSBridgeResInfo_T       m_KCSBridgeResInfo;
+PendingSeqNoTbl_T		m_PendingSeqNoTbl[16][MAX_PENDING_SEQ_NO];
+//KCSBridgeResInfo_T      m_KCSBridgeResInfo;
 int gFd_MsgHndlrIfc;
 TLS_T g_tls;
 /*!

+ 49 - 0
app/gpiotool/Makefile

@@ -0,0 +1,49 @@
+EXEC		= gpiotool
+
+CFLAGS		:= -Os -mcpu=cortex-m3 -mthumb
+LDFLAGS		:= -mcpu=cortex-m3 -mthumb -L$(INSTALL_ROOT)/A2F/root/usr/lib
+#BUILD_TIME 	= $(shell date +"%Y-%m-%d %H:%M:%S")
+
+
+
+CC	= $(CROSS_COMPILE_APPS)gcc
+
+#source
+SRC	= gpiotool_main.c
+
+# hal_api
+SRC += $(wildcard ../hal_api/*.c) 
+
+#incldue
+CFLAGS	+= -I$(INSTALL_ROOT)/A2F/root/usr/include
+CFLAGS	+= -I ./
+CFLAGS	+= -I ../common_include
+CFLAGS	+= -I ../driver
+CFLAGS	+= -I ../hal_api
+CFLAGS	+= -I ../driver/STM32F4xx_HAL_Driver
+CFLAGS	+= -I ../driver/STM32F4xx_HAL_Driver/Inc
+CFLAGS	+= -I ../driver/STM32F4xx_HAL_Driver/Inc/Legacy
+
+
+#library
+
+#sub-directory
+#SUBDIR 	= ./sensor
+
+
+
+
+
+
+$(EXEC): $(SRC)
+	#echo $(BUILD_TIME)
+	$(CC) $(LDFLAGS) $(CFLAGS) -o $@ $^ $(LIBS) -lpthread
+
+
+#object
+OBJS	= $(SRC:%.c=%.o)
+
+clean:
+	rm -f $(EXEC) $(OBJS)
+	rm -f *.gdb 
+

binární
app/gpiotool/adctool


binární
app/gpiotool/gpiotool


+ 280 - 0
app/gpiotool/gpiotool_main.c

@@ -0,0 +1,280 @@
+#include <stdio.h>
+#include <stdint.h>
+#include "driver.h"
+#include "hal_interface_api.h"
+#include <fcntl.h>
+
+
+
+
+typedef struct 
+{
+	char 			pinstr[5];
+	GPIO_TypeDef  	*gpioport;
+	uint16_t 		gpiopin;
+} GPIO_TABLE_T;
+
+const GPIO_TABLE_T gpio_map[] = {
+	/**** GPIOA ****/
+	{"PA0",	GPIOA, GPIO_PIN_0},
+	{"PA1",	GPIOA, GPIO_PIN_1},
+	{"PA2",	GPIOA, GPIO_PIN_2},
+	{"PA3",	GPIOA, GPIO_PIN_3},
+	{"PA4",	GPIOA, GPIO_PIN_4},
+	{"PA5",	GPIOA, GPIO_PIN_5},
+	{"PA6",	GPIOA, GPIO_PIN_6},
+	{"PA7",	GPIOA, GPIO_PIN_7},
+	{"PA8",	GPIOA, GPIO_PIN_8},
+	{"PA9",	GPIOA, GPIO_PIN_9},
+	{"PA10",	GPIOA, GPIO_PIN_10},
+	{"PA11",	GPIOA, GPIO_PIN_11},
+	{"PA12",	GPIOA, GPIO_PIN_12},
+	{"PA13",	GPIOA, GPIO_PIN_13},
+	{"PA14",	GPIOA, GPIO_PIN_14},
+	{"PA15",	GPIOA, GPIO_PIN_15},
+	/**** GPIOB ****/
+	{"PB0",	GPIOB, GPIO_PIN_0},
+	{"PB1",	GPIOB, GPIO_PIN_1},
+	{"PB2",	GPIOB, GPIO_PIN_2},
+	{"PB3",	GPIOB, GPIO_PIN_3},
+	{"PB4",	GPIOB, GPIO_PIN_4},
+	{"PB5",	GPIOB, GPIO_PIN_5},
+	{"PB6",	GPIOB, GPIO_PIN_6},
+	{"PB7",	GPIOB, GPIO_PIN_7},
+	{"PB8",	GPIOB, GPIO_PIN_8},
+	{"PB9",	GPIOB, GPIO_PIN_9},
+	{"PB10",	GPIOB, GPIO_PIN_10},
+	{"PB11",	GPIOB, GPIO_PIN_11},
+	{"PB12",	GPIOB, GPIO_PIN_12},
+	{"PB13",	GPIOB, GPIO_PIN_13},
+	{"PB14",	GPIOB, GPIO_PIN_14},
+	{"PB15",	GPIOB, GPIO_PIN_15},
+	/**** GPIOC ****/
+	{"PC0",	GPIOC, GPIO_PIN_0},
+	{"PC1",	GPIOC, GPIO_PIN_1},
+	{"PC2",	GPIOC, GPIO_PIN_2},
+	{"PC3",	GPIOC, GPIO_PIN_3},
+	{"PC4",	GPIOC, GPIO_PIN_4},
+	{"PC5",	GPIOC, GPIO_PIN_5},
+	{"PC6",	GPIOC, GPIO_PIN_6},
+	{"PC7",	GPIOC, GPIO_PIN_7},
+	{"PC8",	GPIOC, GPIO_PIN_8},
+	{"PC9",	GPIOC, GPIO_PIN_9},
+	{"PC10",	GPIOC, GPIO_PIN_10},
+	{"PC11",	GPIOC, GPIO_PIN_11},
+	{"PC12",	GPIOC, GPIO_PIN_12},
+	{"PC13",	GPIOC, GPIO_PIN_13},
+	{"PC14",	GPIOC, GPIO_PIN_14},
+	{"PC15",	GPIOC, GPIO_PIN_15},
+	/**** GPIOD ****/
+	{"PD0",	GPIOD, GPIO_PIN_0},
+	{"PD1",	GPIOD, GPIO_PIN_1},
+	{"PD2",	GPIOD, GPIO_PIN_2},
+	{"PD3",	GPIOD, GPIO_PIN_3},
+	{"PD4",	GPIOD, GPIO_PIN_4},
+	{"PD5",	GPIOD, GPIO_PIN_5},
+	{"PD6",	GPIOD, GPIO_PIN_6},
+	{"PD7",	GPIOD, GPIO_PIN_7},
+	{"PD8",	GPIOD, GPIO_PIN_8},
+	{"PD9",	GPIOD, GPIO_PIN_9},
+	{"PD10",	GPIOD, GPIO_PIN_10},
+	{"PD11",	GPIOD, GPIO_PIN_11},
+	{"PD12",	GPIOD, GPIO_PIN_12},
+	{"PD13",	GPIOD, GPIO_PIN_13},
+	{"PD14",	GPIOD, GPIO_PIN_14},
+	{"PD15",	GPIOD, GPIO_PIN_15},
+	/**** GPIOE ****/
+	{"PE0",	GPIOE, GPIO_PIN_0},
+	{"PE1",	GPIOE, GPIO_PIN_1},
+	{"PE2",	GPIOE, GPIO_PIN_2},
+	{"PE3",	GPIOE, GPIO_PIN_3},
+	{"PE4",	GPIOE, GPIO_PIN_4},
+	{"PE5",	GPIOE, GPIO_PIN_5},
+	{"PE6",	GPIOE, GPIO_PIN_6},
+	{"PE7",	GPIOE, GPIO_PIN_7},
+	{"PE8",	GPIOE, GPIO_PIN_8},
+	{"PE9",	GPIOE, GPIO_PIN_9},
+	{"PE10",	GPIOE, GPIO_PIN_10},
+	{"PE11",	GPIOE, GPIO_PIN_11},
+	{"PE12",	GPIOE, GPIO_PIN_12},
+	{"PE13",	GPIOE, GPIO_PIN_13},
+	{"PE14",	GPIOE, GPIO_PIN_14},
+	{"PE15",	GPIOE, GPIO_PIN_15},
+	/**** GPIOF ****/
+	{"PF0",	GPIOF, GPIO_PIN_0},
+	{"PF1",	GPIOF, GPIO_PIN_1},
+	{"PF2",	GPIOF, GPIO_PIN_2},
+	{"PF3",	GPIOF, GPIO_PIN_3},
+	{"PF4",	GPIOF, GPIO_PIN_4},
+	{"PF5",	GPIOF, GPIO_PIN_5},
+	{"PF6",	GPIOF, GPIO_PIN_6},
+	{"PF7",	GPIOF, GPIO_PIN_7},
+	{"PF8",	GPIOF, GPIO_PIN_8},
+	{"PF9",	GPIOF, GPIO_PIN_9},
+	{"PF10",	GPIOF, GPIO_PIN_10},
+	{"PF11",	GPIOF, GPIO_PIN_11},
+	{"PF12",	GPIOF, GPIO_PIN_12},
+	{"PF13",	GPIOF, GPIO_PIN_13},
+	{"PF14",	GPIOF, GPIO_PIN_14},
+	{"PF15",	GPIOF, GPIO_PIN_15},
+	/**** GPIOG ****/
+	{"PG0",	GPIOG, GPIO_PIN_0},
+	{"PG1",	GPIOG, GPIO_PIN_1},
+	{"PG2",	GPIOG, GPIO_PIN_2},
+	{"PG3",	GPIOG, GPIO_PIN_3},
+	{"PG4",	GPIOG, GPIO_PIN_4},
+	{"PG5",	GPIOG, GPIO_PIN_5},
+	{"PG6",	GPIOG, GPIO_PIN_6},
+	{"PG7",	GPIOG, GPIO_PIN_7},
+	{"PG8",	GPIOG, GPIO_PIN_8},
+	{"PG9",	GPIOG, GPIO_PIN_9},
+	{"PG10",	GPIOG, GPIO_PIN_10},
+	{"PG11",	GPIOG, GPIO_PIN_11},
+	{"PG12",	GPIOG, GPIO_PIN_12},
+	{"PG13",	GPIOG, GPIO_PIN_13},
+	{"PG14",	GPIOG, GPIO_PIN_14},
+	{"PG15",	GPIOG, GPIO_PIN_15},
+	/**** GPIOH ****/
+	{"PH0",	GPIOH, GPIO_PIN_0},
+	{"PH1",	GPIOH, GPIO_PIN_1},
+	{"PH2",	GPIOH, GPIO_PIN_2},
+	{"PH3",	GPIOH, GPIO_PIN_3},
+	{"PH4",	GPIOH, GPIO_PIN_4},
+	{"PH5",	GPIOH, GPIO_PIN_5},
+	{"PH6",	GPIOH, GPIO_PIN_6},
+	{"PH7",	GPIOH, GPIO_PIN_7},
+	{"PH8",	GPIOH, GPIO_PIN_8},
+	{"PH9",	GPIOH, GPIO_PIN_9},
+	{"PH10",	GPIOH, GPIO_PIN_10},
+	{"PH11",	GPIOH, GPIO_PIN_11},
+	{"PH12",	GPIOH, GPIO_PIN_12},
+	{"PH13",	GPIOH, GPIO_PIN_13},
+	{"PH14",	GPIOH, GPIO_PIN_14},
+	{"PH15",	GPIOH, GPIO_PIN_15},
+	/**** GPIOI ****/
+	{"PI0",	GPIOI, GPIO_PIN_0},
+	{"PI1",	GPIOI, GPIO_PIN_1},
+	{"PI2",	GPIOI, GPIO_PIN_2},
+	{"PI3",	GPIOI, GPIO_PIN_3},
+	{"PI4",	GPIOI, GPIO_PIN_4},
+	{"PI5",	GPIOI, GPIO_PIN_5},
+	{"PI6",	GPIOI, GPIO_PIN_6},
+	{"PI7",	GPIOI, GPIO_PIN_7},
+	{"PI8",	GPIOI, GPIO_PIN_8},
+	{"PI9",	GPIOI, GPIO_PIN_9},
+	{"PI10",	GPIOI, GPIO_PIN_10},
+	{"PI11",	GPIOI, GPIO_PIN_11},
+	{"PI12",	GPIOI, GPIO_PIN_12},
+	{"PI13",	GPIOI, GPIO_PIN_13},
+	{"PI14",	GPIOI, GPIO_PIN_14},
+	{"PI15",	GPIOI, GPIO_PIN_15},
+};
+
+void help()
+{
+	printf("gpiotool set <PA0~PI15> <outputpp/outputod/input> <pullup/pulldown/nopull>\n");
+	printf("gpiotool write <PA0~PI15> <high/low>\n");
+	printf("gpiotool read <PA0~PI15>\n");
+}
+
+int main(int argc, char* argv[])
+{
+	int i;
+	uint16_t buf;
+	GPIO_TABLE_T *gpioinfo;
+	GPIO_TypeDef  	*GPIO_PORT;
+	uint16_t		GPIO_PIN;
+	GPIO_InitTypeDef GPIO_InitStruct;
+	uint8_t 		pin_state;
+
+
+	if(argc < 3)
+	{
+		help();
+		return -1;
+	}
+
+	for(i=0;i<sizeof(gpio_map)/sizeof(GPIO_TABLE_T);i++)
+	{
+		if(strcmp(argv[2], gpio_map[i].pinstr) == 0)
+		{
+			GPIO_PORT = gpio_map[i].gpioport;
+			GPIO_PIN = gpio_map[i].gpiopin;
+			break;
+		}
+	}
+
+	if(i == sizeof(gpio_map)/sizeof(GPIO_TABLE_T))
+	{
+		printf("Invalid gpio pin %s \n", argv[2]);
+		help();
+		return -1;
+	}
+
+	if((strcmp(argv[1], "set") == 0) && (argc == 5))
+	{
+		GPIO_InitStruct.Pin = GPIO_PIN;
+		GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
+
+		if(strcmp(argv[3], "outputpp") == 0)
+			GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
+		else if(strcmp(argv[3], "outputod") == 0)
+			GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
+		else if(strcmp(argv[3], "input") == 0)
+			GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
+		else
+		{
+			printf("Invalid dir %s\n", argv[3]);
+			help();
+			return -1;
+		}
+
+		if(strcmp(argv[4], "pullup") == 0)
+			GPIO_InitStruct.Pull = GPIO_PULLUP;
+		else if(strcmp(argv[4], "pulldown") == 0)
+			GPIO_InitStruct.Pull = GPIO_PULLDOWN;
+		else if(strcmp(argv[4], "nopull") == 0)
+			GPIO_InitStruct.Pull = GPIO_NOPULL;
+		else
+		{
+			printf("Invalid pull %s\n", argv[4]);
+			help();
+			return -1;
+		}
+
+		stm32_gpio_init(GPIO_PORT, &GPIO_InitStruct);
+		printf("Set %s %s %s\n", argv[2], argv[3], argv[4]);
+	}
+	else if((strcmp(argv[1], "write") == 0) && (argc == 4))
+	{
+		if(strcmp(argv[3], "high") == 0)
+		{
+			stm32_gpio_write(GPIO_PORT, GPIO_PIN, 1);
+			printf("Set %s high\n", argv[2]);
+		}
+		else if(strcmp(argv[3], "low") == 0)
+		{
+			stm32_gpio_write(GPIO_PORT, GPIO_PIN, 0);
+			printf("Set %s low\n", argv[2]);
+		}
+		else
+		{
+			printf("Invalid pin state %s\n", argv[3]);
+			help();
+			return -1;
+		}
+	}
+	else if((strcmp(argv[1], "read") == 0) && (argc == 3))
+	{
+		pin_state = stm32_gpio_read(GPIO_PORT, GPIO_PIN);
+		if(pin_state)
+			printf("Read %s high\n", argv[2]);
+		else
+			printf("Read %s low\n", argv[2]);
+	}	
+	else
+	{
+		help();
+		return -1;
+	}
+}

+ 49 - 0
app/i2ctool/Makefile

@@ -0,0 +1,49 @@
+EXEC		= i2ctool
+
+CFLAGS		:= -Os -mcpu=cortex-m3 -mthumb
+LDFLAGS		:= -mcpu=cortex-m3 -mthumb -L$(INSTALL_ROOT)/A2F/root/usr/lib
+#BUILD_TIME 	= $(shell date +"%Y-%m-%d %H:%M:%S")
+
+
+
+CC	= $(CROSS_COMPILE_APPS)gcc
+
+#source
+SRC	= i2ctool_main.c
+
+# hal_api
+SRC += $(wildcard ../hal_api/*.c) 
+
+#incldue
+CFLAGS	+= -I$(INSTALL_ROOT)/A2F/root/usr/include
+CFLAGS	+= -I ./
+CFLAGS	+= -I ../common_include
+CFLAGS	+= -I ../driver
+CFLAGS	+= -I ../hal_api
+CFLAGS	+= -I ../driver/STM32F4xx_HAL_Driver
+CFLAGS	+= -I ../driver/STM32F4xx_HAL_Driver/Inc
+CFLAGS	+= -I ../driver/STM32F4xx_HAL_Driver/Inc/Legacy
+
+
+#library
+
+#sub-directory
+#SUBDIR 	= ./sensor
+
+
+
+
+
+
+$(EXEC): $(SRC)
+	#echo $(BUILD_TIME)
+	$(CC) $(LDFLAGS) $(CFLAGS) -o $@ $^ $(LIBS) -lpthread
+
+
+#object
+OBJS	= $(SRC:%.c=%.o)
+
+clean:
+	rm -f $(EXEC) $(OBJS)
+	rm -f *.gdb 
+

binární
app/i2ctool/i2ctool


+ 197 - 0
app/i2ctool/i2ctool_main.c

@@ -0,0 +1,197 @@
+#include <stdio.h>
+#include <stdint.h>
+#include "driver.h"
+#include "hal_interface_api.h"
+#include <fcntl.h>
+#include <string.h>
+
+
+
+void help()
+{
+	printf("i2ctool <cmd>\n");
+	printf("    --scan [channel]: scan i2c bus\n");
+	printf("    --write [channel] -t [target address] -d [data...]: write i2c data.\n");
+}
+
+int str2uint8(char* str, uint8_t *value)
+{
+	uint8_t val = 0;
+	char chr;
+	if(sizeof(str) > 2)		//maybe hex
+	{
+		if((strncmp(str, "0x", 2) == 0) || (strncmp(str, "0X", 2) == 0))
+		{			
+			if(strlen(str) > 4)
+			{
+				printf("Invalid hex %s\n", str);
+				return -1;
+			}
+
+			chr = str[2];
+			if((chr >= '0') && (chr <= '9'))
+			{
+				val = chr - '0';
+			}
+			else if((chr >= 'a') && (chr <= 'f'))
+			{
+				val = chr - 'a' + 10;
+			}
+			else if((chr >= 'A') && (chr <= 'F'))
+			{
+				val = chr - 'A' + 10;
+			}
+			else
+			{
+				printf("Invalid hex %s\n", str);
+				return -1;
+			}
+
+			if(str[3] != '\0')
+			{
+				chr = str[3];
+				val <<= 4; 
+				if((chr >= '0') && (chr <= '9'))
+					val += chr - '0';
+				else if((chr >= 'a') && (chr <= 'f'))
+					val += chr - 'a' + 10;
+				else if((chr >= 'A') && (chr <= 'F'))
+					val += chr - 'A' + 10;
+				else
+				{
+					printf("Invalid hex %s\n", str);
+					return -1;
+				}
+			}
+		}
+		else	//dec
+		{
+			val = atoi(str);
+		}
+	}
+	else	//dec
+	{
+		val = atoi(str);
+	}
+
+	*value = val;
+	return 0;
+}
+
+int main(int argc, char* argv[])
+{
+	int fd;
+	int i;
+	int ret;
+	uint8_t buf[100];
+	uint8_t channel;
+	uint8_t target_addr, start_addr = 2, end_addr = 254;
+	char devname[20] = {0};
+	uint8_t val;
+
+
+	if(argc < 3)
+	{
+		help();
+		return -1;
+	}
+
+	if(strcmp(argv[1], "--scan") == 0)
+	{
+		//Get Channel
+		if(str2uint8(argv[2], &channel) != 0)
+		{
+			printf("Get Channel failed %s\n", argv[2]);
+			return -1;
+		}
+		sprintf(devname, "/dev/i2c%d", channel);
+		printf("\n");
+	 	printf("Find Slave I2C device:\n");
+		for(i=start_addr;i<=end_addr;i+=2)
+		{
+			fd = open(devname, O_RDWR);
+			ret = stm32_i2c_master_write(fd, i, buf, 0);
+			if(ret == 0)
+			{
+				printf("  %#02x\n", i);
+			}
+			close(fd);
+		}
+		printf("\n");
+
+	}
+	else if(strcmp(argv[1], "--write") == 0)
+	{
+		//Get Channel
+		if(str2uint8(argv[2], &channel) != 0)
+		{
+			printf("Get Channel failed %s\n", argv[2]);
+			return -1;
+		}
+		sprintf(devname, "/dev/i2c%d", channel);
+
+		//Get target address
+		if((strcmp(argv[3], "-t") == 0) && (argc >= 5))
+		{
+			if(str2uint8(argv[4], &target_addr) != 0)
+			{
+				printf("Invalid target address %s\n", argv[4]);
+				help();
+				return -1;
+			}
+
+			if(target_addr & 0x01)
+			{
+				printf("target_addr must be write!\n");
+				return -1;
+			}
+		}
+		else
+		{
+			printf("Need -t \n");
+			help();
+			return -1;
+		}
+
+		//Get Datas
+		if((strcmp(argv[5], "-d") == 0) && (argc >= 7)) 
+		{
+			for(i=0;i<argc-6;i++)
+			{
+				if(str2uint8(argv[i+6], &val) != 0)
+				{
+					printf("Invalid data[%d] = %s\n", i, argv[i+6]);
+					return -1;
+				}
+				buf[i] = val;
+			}
+		}
+		else
+		{
+			printf("Need -d\n");
+			help();
+			return -1;
+		}
+		printf("i2c_%d write %#02x: ", channel, target_addr);
+		for(i=0;i<argc-6;i++)
+			printf("%02x ", buf[i]);
+		printf("\n");
+		
+		fd = open(devname, O_RDWR);
+		ret = stm32_i2c_master_write(fd, target_addr, buf, argc-6);
+		if(ret != 0)
+		{
+			printf("i2ctool write %#02x failed!\n", target_addr);
+		}
+		close(fd);
+	}
+	else
+	{
+		printf("Invalid param %s\n", argv[1]);
+		help();
+		return -1;
+	}
+
+	
+	return 0;
+}

+ 49 - 0
app/spitool/Makefile

@@ -0,0 +1,49 @@
+EXEC		= spitool
+
+CFLAGS		:= -Os -mcpu=cortex-m3 -mthumb
+LDFLAGS		:= -mcpu=cortex-m3 -mthumb -L$(INSTALL_ROOT)/A2F/root/usr/lib
+#BUILD_TIME 	= $(shell date +"%Y-%m-%d %H:%M:%S")
+
+
+
+CC	= $(CROSS_COMPILE_APPS)gcc
+
+#source
+SRC	= spitool_main.c
+
+# hal_api
+SRC += $(wildcard ../hal_api/*.c) 
+
+#incldue
+CFLAGS	+= -I$(INSTALL_ROOT)/A2F/root/usr/include
+CFLAGS	+= -I ./
+CFLAGS	+= -I ../common_include
+CFLAGS	+= -I ../driver
+CFLAGS	+= -I ../hal_api
+CFLAGS	+= -I ../driver/STM32F4xx_HAL_Driver
+CFLAGS	+= -I ../driver/STM32F4xx_HAL_Driver/Inc
+CFLAGS	+= -I ../driver/STM32F4xx_HAL_Driver/Inc/Legacy
+
+
+#library
+
+#sub-directory
+#SUBDIR 	= ./sensor
+
+
+
+
+
+
+$(EXEC): $(SRC)
+	#echo $(BUILD_TIME)
+	$(CC) $(LDFLAGS) $(CFLAGS) -o $@ $^ $(LIBS) -lpthread
+
+
+#object
+OBJS	= $(SRC:%.c=%.o)
+
+clean:
+	rm -f $(EXEC) $(OBJS)
+	rm -f *.gdb 
+

binární
app/spitool/spitool


+ 42 - 0
app/spitool/spitool_main.c

@@ -0,0 +1,42 @@
+#include <stdio.h>
+#include <stdint.h>
+#include "driver.h"
+#include "hal_interface_api.h"
+#include <fcntl.h>
+
+/*
+adctool --get-all-channels
+*/
+
+void help()
+{
+	printf("spitool <cmd>\n");
+	printf("    --.\n");
+}
+
+int main(int argc, char* argv[])
+{
+	int i;
+	uint16_t buf;
+	if(argc < 2)
+	{
+		help();
+		return -1;
+	}
+
+	if((strcmp(argv[1], "--get-all-channels") == 0) && (argc == 2))
+	{
+		for(i=1;i<=7;i++)
+		{
+			buf = 0;
+			stm32_adc_get_value(i, &buf);
+			printf("ADC%d: %#04x\n", i, buf); 	
+		}
+		
+	}
+	else
+	{
+		help();
+		return -1;
+	}
+}