This commit is contained in:
eddyem 2020-01-25 13:44:43 +03:00
parent 36a215be1f
commit e7b8dcace7
9 changed files with 344 additions and 3 deletions

Binary file not shown.

39
USB_reset.c Normal file
View File

@ -0,0 +1,39 @@
// https://askubuntu.com/a/661/501233
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/usbdevice_fs.h>
int main(int argc, char **argv)
{
const char *filename;
int fd;
int rc;
if (argc != 2) {
fprintf(stderr, "Usage: usbreset device-filename\n");
return 1;
}
filename = argv[1];
fd = open(filename, O_WRONLY);
if (fd < 0) {
perror("Error opening output file");
return 1;
}
printf("Resetting USB device %s\n", filename);
rc = ioctl(fd, USBDEVFS_RESET, 0);
if (rc < 0) {
perror("Error in ioctl");
return 1;
}
printf("Reset successful\n");
close(fd);
return 0;
}

View File

@ -34,6 +34,7 @@ if(DEFINED EBUG)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wextra -Wall -Werror -W")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wextra -Wall -Werror -W")
set(CMAKE_BUILD_TYPE DEBUG)
set(CMAKE_VERBOSE_MAKEFILE "ON")
add_definitions(-DEBUG)
else()
set(CMAKE_BUILD_TYPE RELEASE)

View File

@ -39,6 +39,7 @@ if(DEFINED EBUG)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wextra -Wall -Werror -W")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wextra -Wall -Werror -W")
set(CMAKE_BUILD_TYPE DEBUG)
set(CMAKE_VERBOSE_MAKEFILE "ON")
add_definitions(-DEBUG)
else()
set(CMAKE_BUILD_TYPE RELEASE)

45
cmakelists_/Makefile Normal file
View File

@ -0,0 +1,45 @@
# run `make DEF=...` to add extra defines
PROGRAM :=
LDFLAGS := -fdata-sections -ffunction-sections -Wl,--gc-sections -Wl,--discard-all
SRCS := $(wildcard *.c)
DEFINES := $(DEF) -D_GNU_SOURCE -D_XOPEN_SOURCE=1111
OBJDIR := mk
CFLAGS += -O2 -Wall -Wextra -Wno-trampolines -std=gnu99
OBJS := $(addprefix $(OBJDIR)/, $(SRCS:%.c=%.o))
DEPS := $(OBJS:.o=.d)
CC = gcc
#CXX = g++
all : $(OBJDIR) $(PROGRAM)
debug: CFLAGS += -DEBUG -Werror
debug: all
$(PROGRAM) : $(OBJS)
@echo -e "\t\tLD $(PROGRAM)"
$(CC) $(LDFLAGS) $(OBJS) -o $(PROGRAM)
$(OBJDIR):
mkdir $(OBJDIR)
ifneq ($(MAKECMDGOALS),clean)
-include $(DEPS)
endif
$(OBJDIR)/%.o: %.c
@echo -e "\t\tCC $<"
$(CC) -MD -c $(LDFLAGS) $(CFLAGS) $(DEFINES) -o $@ $<
clean:
@echo -e "\t\tCLEAN"
@rm -f $(OBJS) $(DEPS)
@rmdir $(OBJDIR) 2>/dev/null || true
xclean: clean
@rm -f $(PROGRAM)
gentags:
CFLAGS="$(CFLAGS) $(DEFINES)" geany -g $(PROGRAM).c.tags *[hc] 2>/dev/null
.PHONY: gentags clean xclean

View File

@ -0,0 +1,157 @@
# make debug adds -DEBUG -Werror
# make ADDEFS="additional defs"
BINARY = chrono
BOOTPORT ?= /dev/ttyUSB0
BOOTSPEED ?= 115200
# MCU FAMILY
FAMILY ?= F1
# MCU code
MCU ?= F103x8
# density (stm32f10x.h, lines 70-84)
DENSITY ?= MD
# change this linking script depending on particular MCU model,
LDSCRIPT ?= stm32F103xB.ld
DEFS = ${ADDEFS} -DVERSION=\"0.1.0\"
TARGET := RELEASE
# proxy GPS output over USART1
#DEFS += -DUSART1PROXY
INDEPENDENT_HEADERS=
FP_FLAGS ?= -msoft-float -mfloat-abi=soft
ASM_FLAGS ?= -mthumb -mcpu=cortex-m3 -mfix-cortex-m3-ldrd
ARCH_FLAGS = $(ASM_FLAGS) $(FP_FLAGS)
###############################################################################
# Executables
#PREFIX ?= arm-none-eabi
# gcc from arm web site
PREFIX ?= /opt/bin/arm-none-eabi
TOOLCHLIB ?= /opt/arm-none-eabi/lib
RM := rm -f
RMDIR := rmdir
CC := $(PREFIX)-gcc
# don't replace ld with gcc: the binary size would be much greater!!
LD := $(PREFIX)-ld
AR := $(PREFIX)-ar
AS := $(PREFIX)-as
SIZE := $(PREFIX)-size
OBJCOPY := $(PREFIX)-objcopy
OBJDUMP := $(PREFIX)-objdump
GDB := $(PREFIX)-gdb
STFLASH := $(shell which st-flash)
STBOOT := $(shell which stm32flash)
DFUUTIL := $(shell which dfu-util)
###############################################################################
# Source files
OBJDIR = mk
SRC := $(wildcard *.c)
OBJS := $(addprefix $(OBJDIR)/, $(SRC:%.c=%.o))
STARTUP = $(OBJDIR)/startup.o
OBJS += $(STARTUP)
# dependencies: we need them to recompile files if their headers-dependencies changed
DEPS := $(OBJS:.o=.d)
INC_DIR ?= ../inc
INCLUDE := -I$(INC_DIR)/Fx -I$(INC_DIR)/cm
LIB_DIR := $(INC_DIR)/ld
###############################################################################
# C flags
CFLAGS += -O2 -g -D__thumb2__=1 -MD
CFLAGS += -Wall -Wextra -Wshadow
CFLAGS += -fno-common -ffunction-sections -fdata-sections -fno-stack-protector
CFLAGS += $(ARCH_FLAGS)
###############################################################################
# Linker flags
LDFLAGS += --static -nostartfiles -nostdlibs
LDFLAGS += -L$(LIB_DIR) -L$(TOOLCHLIB)
LDFLAGS += -T$(LDSCRIPT)
###############################################################################
# Used libraries
LDLIBS += -lc $(shell $(CC) $(CFLAGS) -print-libgcc-file-name)
DEFS += -DSTM32$(FAMILY) -DSTM32$(MCU) -DSTM32F10X_$(DENSITY)
ELF := $(OBJDIR)/$(BINARY).elf
LIST := $(OBJDIR)/$(BINARY).list
BIN := $(BINARY).bin
HEX := $(BINARY).hex
all: $(OBJDIR)/RELEASE
all: bin list size
release: all
debug: CFLAGS += -DEBUG -Werror
debug: TARGET := DEBUG
debug: $(OBJDIR)/DEBUG
debug: bin list size
$(OBJDIR)/DEBUG:
make clean
$(OBJDIR)/RELEASE:
make clean
elf: $(ELF)
bin: $(BIN)
hex: $(HEX)
list: $(LIST)
ifneq ($(MAKECMDGOALS),clean)
-include $(DEPS)
endif
$(OBJDIR):
mkdir $(OBJDIR)
$(STARTUP): $(INC_DIR)/startup/vector.c
$(CC) $(CFLAGS) $(DEFS) $(INCLUDE) -o $@ -c $<
$(OBJDIR)/%.o: %.c
@echo " CC $<"
$(CC) $(CFLAGS) $(DEFS) $(INCLUDE) -o $@ -c $<
$(BIN): $(ELF)
@echo "TARGET: $(TARGET)"
@> $(OBJDIR)/$(TARGET)
@echo " OBJCOPY $(BIN)"
$(OBJCOPY) -Obinary $(ELF) $(BIN)
$(HEX): $(ELF)
@echo " OBJCOPY $(HEX)"
$(OBJCOPY) -Oihex $(ELF) $(HEX)
$(LIST): $(ELF)
@echo " OBJDUMP $(LIST)"
$(OBJDUMP) -S $(ELF) > $(LIST)
$(ELF): $(OBJDIR) $(OBJS) $(LDSCRIPT)
@echo " LD $(ELF)"
$(LD) $(LDFLAGS) $(OBJS) $(LDLIBS) -o $(ELF)
size: $(ELF)
$(SIZE) $(ELF)
clean:
@echo " CLEAN"
@$(RM) $(HEX)
@$(RM) -rf $(OBJDIR) 2>/dev/null || true
flash: $(BIN)
@echo " FLASH $(BIN)"
$(STFLASH) --reset write $(BIN) 0x8000000
boot: $(BIN)
@echo " LOAD $(BIN) through bootloader"
$(STBOOT) -b$(BOOTSPEED) $(BOOTPORT) -w $(BIN)
dfuboot: $(BIN)
@echo " LOAD $(BIN) THROUGH DFU"
$(DFUUTIL) -a0 -D $(BIN) -s 0x08000000
.PHONY: clean flash boot DEBUG

34
cryptpass.c Normal file
View File

@ -0,0 +1,34 @@
#define _GNU_SOURCE
#include <crypt.h> // gcc -lcrypt
#include <errno.h>
#include <stdio.h>
int main(int argc, char **argv){
if(argc != 3){
printf("USAGE:\n\t%s <password> <salt>\n", argv[0]);
printf("Example:\n\t%s password \\$6\\$salt -> SHA512 with given salt (see man 3 crypt)\n", argv[0]);
return 1;
}
struct crypt_data x;
x.initialized = 0;
char *c = crypt_r(argv[1], argv[2], &x);
int e = errno;
if(!c){
switch(e){
case EINVAL:
fprintf(stderr, "Wrong SALT format!\n");
break;
case ENOSYS:
fprintf(stderr, "crypt() not realized!\n");
break;
case EPERM:
fprintf(stderr, "/proc/sys/crypto/fips_enabled prohibits this encryption method!\n");
default:
fprintf(stderr, "Unknown error!\n");
}
return e;
}
printf("Result of crypt_r():\n%s\n", c);
return 0;
}

View File

@ -1,6 +1,7 @@
cmake_minimum_required(VERSION 3.9)
project(myapp)
add_executable(myapp helloworld.c)
project(examples)
link_libraries(usefull_macros)
include_directories(../)
target_link_libraries(myapp usefull_macros)
#target_link_libraries(hello -lm)
add_executable(hello helloworld.c)

63
usb_reset/reset.c Normal file
View File

@ -0,0 +1,63 @@
#include <stdio.h>
#include <string.h>
#include <usb.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <linux/usbdevice_fs.h>
#include <errno.h>
#include <stdlib.h>
#define ERR(...) fprintf(stderr, __VA_ARGS__)
#define DBG(...)
int main(int argc, char **argv){
int pid, vid;
int fd, rc;
char buf[256], *d = NULL, *f = NULL, *eptr;
struct usb_bus *bus;
struct usb_device *dev;
int found = 0;
if(argc != 2 || !(d = strchr(argv[1], ':'))){
printf("Usage: %s vid:pid\n", argv[0]);
return 1;
}
vid = (int)strtol(argv[1], &eptr, 16);
pid = (int)strtol(d+1, &eptr, 16);
printf("pid: %x, vid: %x\n", pid, vid);
d = NULL;
usb_init();
usb_find_busses();
usb_find_devices();
for(bus = usb_busses; bus && !found; bus = bus->next) {
for(dev = bus->devices; dev && !found; dev = dev->next) {
if (dev->descriptor.idVendor == vid && dev->descriptor.idProduct == pid){
found = 1;
d = bus->dirname;
f = dev->filename;
}
}
}
if(!found){
// "õÓÔÒÏÊÓÔ×Ï ÎÅ ÎÁÊÄÅÎÏ"
ERR("Device not found");
return 1;
}
snprintf(buf, 255, "/dev/bus/usb/%s/%s", d,f);
fd = open(buf, O_WRONLY);
if (fd < 0) {
// "îÅ ÍÏÇÕ ÏÔËÒÙÔØ ÆÁÊÌ ÕÓÔÒÏÊÓÔ×Á %s: %s"
ERR("Can't open device file %s: %s", buf, strerror(errno));
return 1;
}
printf("Resetting USB device %s", buf);
rc = ioctl(fd, USBDEVFS_RESET, 0);
if (rc < 0) {
// "îÅ ÍÏÇÕ ×ÙÚÙ×ÁÔØ ioctl"
perror("Error in ioctl");
return 1;
}
close(fd);
return 0;
}