fix Canon managing to new USB (CDC ACM)

This commit is contained in:
Edward Emelianov 2025-03-11 16:17:43 +03:00
parent f656b64eec
commit 40a22864df
24 changed files with 1412 additions and 1160 deletions

View File

@ -1,151 +0,0 @@
BINARY = canonusb
BOOTPORT ?= /dev/ttyUSB0
BOOTSPEED ?= 115200
# MCU FAMILY
FAMILY ?= F1
# MCU code
MCU ?= F103x8
# density (stm32f10x.h, lines 70-84)
DENSITY ?= LD
# change this linking script depending on particular MCU model,
LDSCRIPT ?= stm32f103x8.ld
# debug
#DEFS = -DEBUG
# autoincremental version & build date
VERSION_FILE = version.inc
NEXTVER := $(shell expr $$(awk '/#define BUILD_NUMBER/' $(VERSION_FILE) | tr -cd "[0-9]") + 1)
BUILDDATE := $(shell date +%Y-%m-%d)
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
LD := $(PREFIX)-gcc
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 -g -gdwarf-2
CFLAGS += -Wall -Werror -Wextra -Wshadow
CFLAGS += -fno-common -ffunction-sections -fdata-sections -fno-stack-protector -fshort-enums
CFLAGS += $(ARCH_FLAGS)
###############################################################################
# Linker flags
LDFLAGS += -nostartfiles -nostdlib --static -Wl,--gc-sections -Wl,--print-memory-usage
LDFLAGS += -Wl,-Map=$(OBJDIR)/$(BINARY).map
#LDFLAGS += --static --gc-sections --print-memory-usage
LDFLAGS += -L$(LIB_DIR) -L$(TOOLCHLIB)
LDFLAGS += -T$(LDSCRIPT)
###############################################################################
# Used libraries
LDLIBS += -lc -lgcc $(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: bin list size
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 $<
$(VERSION_FILE): *.[ch]
@echo " Generate version: $(NEXTVER) for date $(BUILDDATE)"
@sed -i "s/#define BUILD_NUMBER.*/#define BUILD_NUMBER \"$(NEXTVER)\"/" $(VERSION_FILE)
@sed -i "s/#define BUILD_DATE.*/#define BUILD_DATE \"$(BUILDDATE)\"/" $(VERSION_FILE)
$(OBJDIR)/proto.o: proto.c $(VERSION_FILE)
$(OBJDIR)/%.o: %.c
@echo " CC $<"
$(CC) $(CFLAGS) $(DEFS) $(INCLUDE) -o $@ -c $<
$(BIN): $(ELF)
@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)
@echo " LD $(ELF)"
$(LD) $(LDFLAGS) $(OBJS) $(LDLIBS) -o $(ELF)
size: $(ELF)
$(SIZE) $(ELF)
clean:
@echo " CLEAN"
@rm -rf $(OBJDIR) 2>/dev/null || true
flash: $(BIN)
@echo " FLASH $(BIN)"
$(STFLASH) 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

View File

@ -21,7 +21,7 @@
#include "hardware.h" #include "hardware.h"
#include "proto.h" #include "proto.h"
#include "spi.h" #include "spi.h"
#include "usb.h" #include "usb_dev.h"
#define CU(a) ((const uint8_t*)a) #define CU(a) ((const uint8_t*)a)
@ -99,7 +99,7 @@ typedef enum{
static f_movstate Fstopped(uint16_t *oldF){ static f_movstate Fstopped(uint16_t *oldF){
uint16_t f = getfocval(); uint16_t f = getfocval();
#ifdef EBUG #ifdef EBUG
USB_send("Curpos="); USB_send(u2str(f)); USB_send("\n"); USB_sendstr("Curpos="); USB_sendstr(u2str(f)); USB_sendstr("\n");
#endif #endif
if(BADFOCVAL == f) return F_ERR; if(BADFOCVAL == f) return F_ERR;
if(*oldF == f){ if(*oldF == f){
@ -355,10 +355,10 @@ int canon_diaphragm(char command){
int canon_focus(int16_t val){ int canon_focus(int16_t val){
if(!ready || inistate != INI_READY) return 1; if(!ready || inistate != INI_READY) return 1;
if(val < 0){ if(val < 0){
USB_send("Fsteps="); USB_send(u2str(getfocval())); USB_send("\n"); USB_sendstr("Fsteps="); USB_sendstr(u2str(getfocval())); USB_sendstr("\n");
}else{ }else{
if(val > Fmax){ if(val > Fmax){
USB_send("Fmax="); USB_send(u2str(Fmax)); USB_send("\n"); USB_sendstr("Fmax="); USB_sendstr(u2str(Fmax)); USB_sendstr("\n");
return 2; return 2;
} }
uint16_t curF = getfocval(); uint16_t curF = getfocval();
@ -383,20 +383,20 @@ int canon_sendcmd(uint8_t cmd){
// acquire 16bit value // acquire 16bit value
int canon_asku16(uint8_t cmd){ int canon_asku16(uint8_t cmd){
if(!ready || !canon_read(cmd, 3)) return 1; if(!ready || !canon_read(cmd, 3)) return 1;
USB_send("par="); USB_sendstr("par=");
USB_send(u2str((buf[1] << 8) | buf[2])); USB_sendstr(u2str((buf[1] << 8) | buf[2]));
USB_send("\n"); USB_sendstr("\n");
//canon_poll(); //canon_poll();
return 0; return 0;
} }
int canon_getinfo(){ int canon_getinfo(){
if(!ready || !canon_read(CANON_GETINFO, 6)) return 1; if(!ready || !canon_read(CANON_GETINFO, 6)) return 1;
USB_send("Info="); for(int i = 1; i < 7; ++i){ USB_sendstr("Info="); for(int i = 1; i < 7; ++i){
USB_send(u2hexstr(buf[i])); USB_send(" "); USB_sendstr(u2hexstr(buf[i])); USB_sendstr(" ");
} }
//canon_poll(); //canon_poll();
USB_send("\n"); USB_sendstr("\n");
return 0; return 0;
} }

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject> <!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 6.0.0, 2022-09-07T10:47:24. --> <!-- Written by QtCreator 15.0.1, 2025-03-11T16:16:01. -->
<qtcreator> <qtcreator>
<data> <data>
<variable>EnvironmentId</variable> <variable>EnvironmentId</variable>
@ -8,7 +8,7 @@
</data> </data>
<data> <data>
<variable>ProjectExplorer.Project.ActiveTarget</variable> <variable>ProjectExplorer.Project.ActiveTarget</variable>
<value type="int">0</value> <value type="qlonglong">0</value>
</data> </data>
<data> <data>
<variable>ProjectExplorer.Project.EditorSettings</variable> <variable>ProjectExplorer.Project.EditorSettings</variable>
@ -28,15 +28,17 @@
<value type="QByteArray" key="CurrentPreferences">QmlJSGlobal</value> <value type="QByteArray" key="CurrentPreferences">QmlJSGlobal</value>
</valuemap> </valuemap>
</valuemap> </valuemap>
<value type="int" key="EditorConfiguration.CodeStyle.Count">2</value> <value type="qlonglong" key="EditorConfiguration.CodeStyle.Count">2</value>
<value type="QByteArray" key="EditorConfiguration.Codec">KOI8-R</value> <value type="QByteArray" key="EditorConfiguration.Codec">KOI8-R</value>
<value type="bool" key="EditorConfiguration.ConstrainTooltips">false</value> <value type="bool" key="EditorConfiguration.ConstrainTooltips">false</value>
<value type="int" key="EditorConfiguration.IndentSize">4</value> <value type="int" key="EditorConfiguration.IndentSize">4</value>
<value type="bool" key="EditorConfiguration.KeyboardTooltips">false</value> <value type="bool" key="EditorConfiguration.KeyboardTooltips">false</value>
<value type="int" key="EditorConfiguration.LineEndingBehavior">0</value>
<value type="int" key="EditorConfiguration.MarginColumn">80</value> <value type="int" key="EditorConfiguration.MarginColumn">80</value>
<value type="bool" key="EditorConfiguration.MouseHiding">true</value> <value type="bool" key="EditorConfiguration.MouseHiding">true</value>
<value type="bool" key="EditorConfiguration.MouseNavigation">true</value> <value type="bool" key="EditorConfiguration.MouseNavigation">true</value>
<value type="int" key="EditorConfiguration.PaddingMode">1</value> <value type="int" key="EditorConfiguration.PaddingMode">1</value>
<value type="int" key="EditorConfiguration.PreferAfterWhitespaceComments">0</value>
<value type="bool" key="EditorConfiguration.PreferSingleLineComments">false</value> <value type="bool" key="EditorConfiguration.PreferSingleLineComments">false</value>
<value type="bool" key="EditorConfiguration.ScrollWheelZooming">false</value> <value type="bool" key="EditorConfiguration.ScrollWheelZooming">false</value>
<value type="bool" key="EditorConfiguration.ShowMargin">false</value> <value type="bool" key="EditorConfiguration.ShowMargin">false</value>
@ -54,16 +56,31 @@
<value type="QString" key="EditorConfiguration.ignoreFileTypes">*.md, *.MD, Makefile</value> <value type="QString" key="EditorConfiguration.ignoreFileTypes">*.md, *.MD, Makefile</value>
<value type="bool" key="EditorConfiguration.inEntireDocument">true</value> <value type="bool" key="EditorConfiguration.inEntireDocument">true</value>
<value type="bool" key="EditorConfiguration.skipTrailingWhitespace">true</value> <value type="bool" key="EditorConfiguration.skipTrailingWhitespace">true</value>
<value type="bool" key="EditorConfiguration.tintMarginArea">true</value>
</valuemap> </valuemap>
</data> </data>
<data> <data>
<variable>ProjectExplorer.Project.PluginSettings</variable> <variable>ProjectExplorer.Project.PluginSettings</variable>
<valuemap type="QVariantMap"> <valuemap type="QVariantMap">
<valuemap type="QVariantMap" key="AutoTest.ActiveFrameworks">
<value type="bool" key="AutoTest.Framework.Boost">true</value>
<value type="bool" key="AutoTest.Framework.CTest">false</value>
<value type="bool" key="AutoTest.Framework.Catch">true</value>
<value type="bool" key="AutoTest.Framework.GTest">true</value>
<value type="bool" key="AutoTest.Framework.QtQuickTest">true</value>
<value type="bool" key="AutoTest.Framework.QtTest">true</value>
</valuemap>
<value type="bool" key="AutoTest.ApplyFilter">false</value>
<valuemap type="QVariantMap" key="AutoTest.CheckStates"/>
<valuelist type="QVariantList" key="AutoTest.PathFilters"/>
<value type="int" key="AutoTest.RunAfterBuild">0</value>
<value type="bool" key="AutoTest.UseGlobal">true</value>
<valuemap type="QVariantMap" key="ClangTools"> <valuemap type="QVariantMap" key="ClangTools">
<value type="bool" key="ClangTools.AnalyzeOpenFiles">true</value> <value type="bool" key="ClangTools.AnalyzeOpenFiles">true</value>
<value type="bool" key="ClangTools.BuildBeforeAnalysis">true</value> <value type="bool" key="ClangTools.BuildBeforeAnalysis">true</value>
<value type="QString" key="ClangTools.DiagnosticConfig">Builtin.DefaultTidyAndClazy</value> <value type="QString" key="ClangTools.DiagnosticConfig">Builtin.DefaultTidyAndClazy</value>
<value type="int" key="ClangTools.ParallelJobs">4</value> <value type="int" key="ClangTools.ParallelJobs">4</value>
<value type="bool" key="ClangTools.PreferConfigFile">false</value>
<valuelist type="QVariantList" key="ClangTools.SelectedDirs"/> <valuelist type="QVariantList" key="ClangTools.SelectedDirs"/>
<valuelist type="QVariantList" key="ClangTools.SelectedFiles"/> <valuelist type="QVariantList" key="ClangTools.SelectedFiles"/>
<valuelist type="QVariantList" key="ClangTools.SuppressedDiagnostics"/> <valuelist type="QVariantList" key="ClangTools.SuppressedDiagnostics"/>
@ -78,9 +95,9 @@
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">{91347f2c-5221-46a7-80b1-0a054ca02f79}</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">{91347f2c-5221-46a7-80b1-0a054ca02f79}</value>
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value> <value type="qlonglong" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value> <value type="qlonglong" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value> <value type="qlonglong" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0"> <valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">
<value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">/home/eddy/Docs/SAO/ELECTRONICS/STM32/F1-srcs/USB_SPI</value> <value type="QString" key="ProjectExplorer.BuildConfiguration.BuildDirectory">/home/eddy/Docs/SAO/ELECTRONICS/STM32/F1-srcs/USB_SPI</value>
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0"> <valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
@ -91,7 +108,7 @@
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value> <value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">GenericProjectManager.GenericMakeStep</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">GenericProjectManager.GenericMakeStep</value>
</valuemap> </valuemap>
<value type="int" key="ProjectExplorer.BuildStepList.StepsCount">1</value> <value type="qlonglong" key="ProjectExplorer.BuildStepList.StepsCount">1</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Сборка</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Сборка</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Сборка</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Сборка</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Build</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Build</value>
@ -104,7 +121,7 @@
<value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value> <value type="bool" key="ProjectExplorer.BuildStep.Enabled">true</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">GenericProjectManager.GenericMakeStep</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">GenericProjectManager.GenericMakeStep</value>
</valuemap> </valuemap>
<value type="int" key="ProjectExplorer.BuildStepList.StepsCount">1</value> <value type="qlonglong" key="ProjectExplorer.BuildStepList.StepsCount">1</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Очистка</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Очистка</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Очистка</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Очистка</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Clean</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Clean</value>
@ -117,10 +134,10 @@
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">По умолчанию</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">По умолчанию</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">GenericProjectManager.GenericBuildConfiguration</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">GenericProjectManager.GenericBuildConfiguration</value>
</valuemap> </valuemap>
<value type="int" key="ProjectExplorer.Target.BuildConfigurationCount">1</value> <value type="qlonglong" key="ProjectExplorer.Target.BuildConfigurationCount">1</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.DeployConfiguration.0"> <valuemap type="QVariantMap" key="ProjectExplorer.Target.DeployConfiguration.0">
<valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0"> <valuemap type="QVariantMap" key="ProjectExplorer.BuildConfiguration.BuildStepList.0">
<value type="int" key="ProjectExplorer.BuildStepList.StepsCount">0</value> <value type="qlonglong" key="ProjectExplorer.BuildStepList.StepsCount">0</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Развёртывание</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Развёртывание</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Развёртывание</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Развёртывание</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Deploy</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.BuildSteps.Deploy</value>
@ -130,24 +147,29 @@
<value type="bool" key="ProjectExplorer.DeployConfiguration.CustomDataEnabled">false</value> <value type="bool" key="ProjectExplorer.DeployConfiguration.CustomDataEnabled">false</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.DefaultDeployConfiguration</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.DefaultDeployConfiguration</value>
</valuemap> </valuemap>
<value type="int" key="ProjectExplorer.Target.DeployConfigurationCount">1</value> <value type="qlonglong" key="ProjectExplorer.Target.DeployConfigurationCount">1</value>
<valuemap type="QVariantMap" key="ProjectExplorer.Target.RunConfiguration.0"> <valuemap type="QVariantMap" key="ProjectExplorer.Target.RunConfiguration.0">
<value type="bool" key="Analyzer.Perf.Settings.UseGlobalSettings">true</value>
<value type="bool" key="Analyzer.QmlProfiler.Settings.UseGlobalSettings">true</value>
<value type="bool" key="Analyzer.Valgrind.Settings.UseGlobalSettings">true</value>
<valuelist type="QVariantList" key="CustomOutputParsers"/> <valuelist type="QVariantList" key="CustomOutputParsers"/>
<value type="int" key="PE.EnvironmentAspect.Base">2</value> <value type="int" key="PE.EnvironmentAspect.Base">2</value>
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/> <valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
<value type="bool" key="PE.EnvironmentAspect.PrintOnRun">false</value>
<value type="QString" key="PerfRecordArgsId">-e cpu-cycles --call-graph dwarf,4096 -F 250</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.CustomExecutableRunConfiguration</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">ProjectExplorer.CustomExecutableRunConfiguration</value>
<value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey"></value> <value type="QString" key="ProjectExplorer.RunConfiguration.BuildKey"></value>
<value type="bool" key="RunConfiguration.UseCppDebugger">false</value> <value type="bool" key="ProjectExplorer.RunConfiguration.Customized">false</value>
<value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value> <value type="bool" key="RunConfiguration.UseCppDebuggerAuto">true</value>
<value type="bool" key="RunConfiguration.UseQmlDebugger">false</value>
<value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value> <value type="bool" key="RunConfiguration.UseQmlDebuggerAuto">true</value>
</valuemap> </valuemap>
<value type="int" key="ProjectExplorer.Target.RunConfigurationCount">1</value> <value type="qlonglong" key="ProjectExplorer.Target.RunConfigurationCount">1</value>
</valuemap> </valuemap>
</data> </data>
<data> <data>
<variable>ProjectExplorer.Project.TargetCount</variable> <variable>ProjectExplorer.Project.TargetCount</variable>
<value type="int">1</value> <value type="qlonglong">1</value>
</data> </data>
<data> <data>
<variable>ProjectExplorer.Project.Updater.FileVersion</variable> <variable>ProjectExplorer.Project.Updater.FileVersion</variable>

View File

@ -15,6 +15,10 @@ spi.c
spi.h spi.h
usb.c usb.c
usb.h usb.h
usb_descr.c
usb_descr.h
usb_dev.c
usb_dev.h
usb_lib.c usb_lib.c
usb_lib.h usb_lib.h
usbhw.c usbhw.c

View File

@ -25,7 +25,7 @@
#include "flash.h" #include "flash.h"
#include "proto.h" #include "proto.h"
#include "usb.h" // printout #include "usb_dev.h" // printout
#include <string.h> // memcpy #include <string.h> // memcpy
extern const uint32_t __varsstart, _BLOCKSIZE; extern const uint32_t __varsstart, _BLOCKSIZE;
@ -63,11 +63,11 @@ static int binarySearch(int r, const uint8_t *start, int stor_size){
while(r >= l){ while(r >= l){
int mid = l + (r - l) / 2; int mid = l + (r - l) / 2;
#ifdef EBUG #ifdef EBUG
USB_send("mid/l/r="); USB_sendstr("mid/l/r=");
USB_send(u2str(mid)); USB_send("/"); USB_sendstr(u2str(mid)); USB_sendstr("/");
USB_send(u2str(l)); USB_send("/"); USB_sendstr(u2str(l)); USB_sendstr("/");
USB_send(u2str(r)); USB_send("/"); USB_sendstr(u2str(r)); USB_sendstr("/");
USB_send("\n"); USB_sendstr("\n");
#endif #endif
const uint8_t *s = start + mid * stor_size; const uint8_t *s = start + mid * stor_size;
if(*((const uint16_t*)s) == stor_size){ if(*((const uint16_t*)s) == stor_size){
@ -94,7 +94,7 @@ void flashstorage_init(){
maxCnum = flsz / sizeof(user_conf); maxCnum = flsz / sizeof(user_conf);
} }
#ifdef EBUG #ifdef EBUG
USB_send("INIT\n"); USB_sendstr("INIT\n");
#endif #endif
// -1 if there's no data at all & flash is clear; maxnum-1 if flash is full // -1 if there's no data at all & flash is clear; maxnum-1 if flash is full
currentconfidx = binarySearch((int)maxCnum-2, (const uint8_t*)Flash_Data, sizeof(user_conf)); currentconfidx = binarySearch((int)maxCnum-2, (const uint8_t*)Flash_Data, sizeof(user_conf));
@ -102,7 +102,7 @@ void flashstorage_init(){
memcpy(&the_conf, &Flash_Data[currentconfidx], sizeof(user_conf)); memcpy(&the_conf, &Flash_Data[currentconfidx], sizeof(user_conf));
} }
#ifdef EBUG #ifdef EBUG
USB_send("currentconfidx="); USB_send(u2str(currentconfidx)); USB_send("\n"); USB_sendstr("currentconfidx="); USB_sendstr(u2str(currentconfidx)); USB_sendstr("\n");
#endif #endif
} }
@ -134,18 +134,18 @@ static int write2flash(const void *start, const void *wrdata, uint32_t stor_size
*(volatile uint16_t*)(address + i) = data[i]; *(volatile uint16_t*)(address + i) = data[i];
while(FLASH->SR & FLASH_SR_BSY) IWDG->KR = IWDG_REFRESH; while(FLASH->SR & FLASH_SR_BSY) IWDG->KR = IWDG_REFRESH;
if(*(volatile uint16_t*)(address + i) != data[i]){ if(*(volatile uint16_t*)(address + i) != data[i]){
USB_send("DON'T MATCH!\n"); USB_sendstr("DON'T MATCH!\n");
ret = 1; ret = 1;
break; break;
} }
if(FLASH->SR & FLASH_SR_PGERR){ if(FLASH->SR & FLASH_SR_PGERR){
USB_send("Prog err\n"); USB_sendstr("Prog err\n");
ret = 1; // program error - meet not 0xffff ret = 1; // program error - meet not 0xffff
break; break;
} }
#ifdef EBUG #ifdef EBUG
USB_send(u2str(stor_size)); USB_send("bytes stored @0x"); USB_sendstr(u2str(stor_size)); USB_sendstr("bytes stored @0x");
USB_send(u2hexstr((uint32_t)(address + i))); USB_send("\n"); USB_sendstr(u2hexstr((uint32_t)(address + i))); USB_sendstr("\n");
#endif #endif
FLASH->SR = FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPRTERR; FLASH->SR = FLASH_SR_EOP | FLASH_SR_PGERR | FLASH_SR_WRPRTERR;
} }
@ -157,7 +157,7 @@ static int write2flash(const void *start, const void *wrdata, uint32_t stor_size
static int erase_pageN(int N){ static int erase_pageN(int N){
int ret = 0; int ret = 0;
#ifdef EBUG #ifdef EBUG
USB_send("Erase page #"); USB_send(u2str(N)); USB_send("\n"); USB_sendstr("Erase page #"); USB_sendstr(u2str(N)); USB_sendstr("\n");
#endif #endif
FLASH->AR = (uint32_t)Flash_Data + N*FLASH_blocksize; FLASH->AR = (uint32_t)Flash_Data + N*FLASH_blocksize;
FLASH->CR |= FLASH_CR_STRT; FLASH->CR |= FLASH_CR_STRT;
@ -180,12 +180,12 @@ int erase_storage(int npage){
} }
end = flsz / FLASH_blocksize; end = flsz / FLASH_blocksize;
#ifdef EBUG #ifdef EBUG
USB_send("FLASH_SIZE="); USB_send(u2str(FLASH_SIZE)); USB_sendstr("FLASH_SIZE="); USB_sendstr(u2str(FLASH_SIZE));
USB_send("\nflsz="); USB_send(u2str(flsz)); USB_sendstr("\nflsz="); USB_sendstr(u2str(flsz));
USB_send("\nend="); USB_send(u2str(end)); USB_sendstr("\nend="); USB_sendstr(u2str(end));
USB_send("\ncurrentconfidx="); USB_send(u2str(currentconfidx)); USB_sendstr("\ncurrentconfidx="); USB_sendstr(u2str(currentconfidx));
USB_send("\nmaxCnum="); USB_send(u2str(maxCnum)); USB_sendstr("\nmaxCnum="); USB_sendstr(u2str(maxCnum));
USB_send("\n"); USB_sendstr("\n");
#endif #endif
if(end == 0 || end >= FLASH_SIZE) return 1; if(end == 0 || end >= FLASH_SIZE) return 1;
if(npage > -1){ // erase only one page if(npage > -1){ // erase only one page
@ -211,10 +211,12 @@ int erase_storage(int npage){
} }
void dump_userconf(){ void dump_userconf(){
USB_send("userconf_sz="); printu(the_conf.userconf_sz); USB_sendstr("userconf_sz="); printu(the_conf.userconf_sz);
USB_send("\ncurrentconfidx="); USB_send(u2str(currentconfidx)); USB_sendstr("\ncurrentconfidx=");
USB_send("\nCAN_speed="); printu(the_conf.canspeed); if(currentconfidx == -1) USB_sendstr("-1");
USB_send("\nCAN_ID="); printu(the_conf.canID); else USB_sendstr(u2str(currentconfidx));
USB_send("\nautoinit="); printu(the_conf.autoinit); USB_sendstr("\nCAN_speed="); printu(the_conf.canspeed);
USB_send("\n"); USB_sendstr("\nCAN_ID="); printu(the_conf.canID);
USB_sendstr("\nautoinit="); printu(the_conf.autoinit);
USB_sendstr("\n");
} }

View File

@ -22,7 +22,7 @@
#include "hardware.h" #include "hardware.h"
#include "proto.h" #include "proto.h"
#include "spi.h" #include "spi.h"
#include "usb.h" #include "usb_dev.h"
#define USBBUFSZ 127 #define USBBUFSZ 127
@ -32,47 +32,30 @@ void sys_tick_handler(void){
++Tms; ++Tms;
} }
// usb getline
char *get_USB(){
static char tmpbuf[USBBUFSZ+1], *curptr = tmpbuf;
static int rest = USBBUFSZ;
uint8_t x = USB_receive(curptr);
if(!x) return NULL;
curptr[x] = 0;
if(curptr[x-1] == '\n'){
curptr = tmpbuf;
rest = USBBUFSZ;
return tmpbuf;
}
curptr += x; rest -= x;
if(rest <= 0){ // buffer overflow
curptr = tmpbuf;
rest = USBBUFSZ;
}
return NULL;
}
int main(void){ int main(void){
char inbuff[256];
sysreset(); sysreset();
StartHSE(); StartHSE();
SysTick_Config(72000); SysTick_Config(72000);
flashstorage_init(); flashstorage_init();
hw_setup(); hw_setup();
if(ISUSB()) USB_setup(); if(ISUSB()){
else CAN_setup(the_conf.canspeed, the_conf.canID); USBPU_OFF();
USB_setup();
USBPU_ON();
}else CAN_setup(the_conf.canspeed, the_conf.canID);
spi_setup(); spi_setup();
uint32_t SPIctr = Tms; uint32_t SPIctr = Tms;
while(1){ while(1){
IWDG->KR = IWDG_REFRESH; IWDG->KR = IWDG_REFRESH;
char *txt = NULL; int l = USB_receivestr(inbuff, 255);
usb_proc(); if(l > 0){
txt = get_USB(); parse_cmd(inbuff); // call it even for NULL (if `flood` is running)
const char *ans = parse_cmd(txt); // call it even for NULL (if `flood` is running)
if(ans) USB_send(ans);
if(Tms - SPIctr > CANONPROC_INTERVAL){ // not more than once per 10ms if(Tms - SPIctr > CANONPROC_INTERVAL){ // not more than once per 10ms
SPIctr = Tms; SPIctr = Tms;
canon_proc(); canon_proc();
} }
} }
}
} }

View File

@ -23,7 +23,7 @@
#include "hardware.h" #include "hardware.h"
#include "proto.h" #include "proto.h"
#include "spi.h" #include "spi.h"
#include "usb.h" #include "usb_dev.h"
#include "version.inc" #include "version.inc"
static const char *OK = "OK", *FAIL = "FAIL"; static const char *OK = "OK", *FAIL = "FAIL";
@ -185,20 +185,6 @@ const char* helpmsg =
"X - save current config to flash\n" "X - save current config to flash\n"
; ;
#define STBUFSZ 255
static char stbuf[STBUFSZ+1], *bptr = NULL;
static int blen = 0;
static void initbuf(){bptr = stbuf; blen = STBUFSZ; *bptr = 0;}
#define newline() do{if(blen){ *bptr++ = '\n'; *bptr = 0; --blen; }}while(0)
static void add2buf(const char *s){
while(blen && *s){
*bptr++ = *s++;
--blen;
}
*bptr = 0;
}
#define SPIBUFSZ (64) #define SPIBUFSZ (64)
// buffer for SPI sending // buffer for SPI sending
static uint8_t spibuf[SPIBUFSZ]; static uint8_t spibuf[SPIBUFSZ];
@ -212,7 +198,7 @@ static int initspibuf(const char *buf){
if(buf == nxt) break; if(buf == nxt) break;
buf = nxt; buf = nxt;
if(D > 0xff){ if(D > 0xff){
USB_send("Number should be from 0 to 0xff\n"); USB_sendstr("Number should be from 0 to 0xff\n");
return 0; return 0;
} }
spibuf[spibuflen++] = (uint8_t)D; spibuf[spibuflen++] = (uint8_t)D;
@ -224,24 +210,23 @@ static void sendspibuf(){
uint8_t buf[SPIBUFSZ]; uint8_t buf[SPIBUFSZ];
memcpy(buf, spibuf, spibuflen); memcpy(buf, spibuf, spibuflen);
if(spibuflen == SPI_transmit((uint8_t*)buf, (uint8_t)spibuflen)){ if(spibuflen == SPI_transmit((uint8_t*)buf, (uint8_t)spibuflen)){
USB_send("Got SPI answer: "); USB_sendstr("Got SPI answer: ");
for(int i = 0; i < spibuflen; ++i){ for(int i = 0; i < spibuflen; ++i){
if(i) USB_send(", "); if(i) USB_sendstr(", ");
USB_send(u2hexstr(buf[i])); USB_sendstr(u2hexstr(buf[i]));
} }
USB_send("\n"); USB_sendstr("\n");
}else USB_send("Failed to send SPI buffer\n"); }else USB_sendstr("Failed to send SPI buffer\n");
} }
static void errw(int e){ static void errw(int e){
if(e){ if(e){
add2buf("Error with code "); USB_sendstr("Error with code ");
add2buf(u2str(e)); USB_sendstr(u2str(e));
if(e == 1) add2buf(" (busy or need initialization)"); if(e == 1) USB_sendstr(" (busy or need initialization)");
}else add2buf(OK); }else USB_sendstr(OK);
} }
extern uint8_t usbON;
const char *connmsgs[LENS_S_AMOUNT+1] = { const char *connmsgs[LENS_S_AMOUNT+1] = {
[LENS_DISCONNECTED] = "disconnected", [LENS_DISCONNECTED] = "disconnected",
[LENS_SLEEPING] = "sleeping, need init", [LENS_SLEEPING] = "sleeping, need init",
@ -262,15 +247,14 @@ const char *inimsgs[INI_S_AMOUNT+1] = {
[INI_ERR] = "error in init procedure", [INI_ERR] = "error in init procedure",
[INI_S_AMOUNT] = "wrong state" [INI_S_AMOUNT] = "wrong state"
}; };
const char *parse_cmd(const char *buf){ void parse_cmd(const char *buf){
static uint32_t lastFloodTime = 0; static uint32_t lastFloodTime = 0;
if(lastFloodTime && (Tms - lastFloodTime > FLOODING_INTERVAL)){ if(lastFloodTime && (Tms - lastFloodTime > FLOODING_INTERVAL)){
sendspibuf(); sendspibuf();
lastFloodTime = Tms ? Tms : 1; lastFloodTime = Tms ? Tms : 1;
} }
if(!buf || *buf == 0) return NULL; if(!buf || *buf == 0) return;
lastFloodTime= FALSE; lastFloodTime= FALSE;
initbuf();
if(buf[1] == '\n' || !buf[1]){ // one symbol commands if(buf[1] == '\n' || !buf[1]){ // one symbol commands
switch(*buf){ switch(*buf){
case 'a': case 'a':
@ -296,67 +280,67 @@ const char *parse_cmd(const char *buf){
errw(canon_asku16(CANON_GETREG)); errw(canon_asku16(CANON_GETREG));
break; break;
case 'E': case 'E':
if(erase_storage(-1)) add2buf(FAIL); if(erase_storage(-1)) USB_sendstr(FAIL);
add2buf(OK); USB_sendstr(OK);
break; break;
case 'F': // just watch SPI->CR1 value case 'F': // just watch SPI->CR1 value
add2buf("SPI1->CR1="); add2buf(u2hexstr(SPI_CR1)); USB_sendstr("SPI1->CR1="); USB_sendstr(u2hexstr(SPI_CR1));
break; break;
case 'G': case 'G':
add2buf("SPI "); USB_sendstr("SPI ");
switch(SPI_status){ switch(SPI_status){
case SPI_NOTREADY: case SPI_NOTREADY:
add2buf("not ready"); USB_sendstr("not ready");
break; break;
case SPI_READY: case SPI_READY:
add2buf("ready"); USB_sendstr("ready");
break; break;
case SPI_BUSY: case SPI_BUSY:
add2buf("busy"); USB_sendstr("busy");
break; break;
default: default:
add2buf("unknown"); USB_sendstr("unknown");
} }
add2buf("\nstate="); USB_sendstr("\nstate=");
uint16_t s = canon_getstate(); uint16_t s = canon_getstate();
uint8_t idx = s & 0xff; uint8_t idx = s & 0xff;
if(idx > LENS_S_AMOUNT) idx = LENS_S_AMOUNT; if(idx > LENS_S_AMOUNT) idx = LENS_S_AMOUNT;
add2buf(connmsgs[idx]); USB_sendstr(connmsgs[idx]);
idx = s >> 8; idx = s >> 8;
add2buf("\ninistate="); USB_sendstr("\ninistate=");
if(idx > INI_S_AMOUNT) idx = INI_S_AMOUNT; if(idx > INI_S_AMOUNT) idx = INI_S_AMOUNT;
add2buf(inimsgs[idx]); USB_sendstr(inimsgs[idx]);
break; break;
case 'h': case 'h':
errw(canon_sendcmd(CANON_FOCBYHANDS)); errw(canon_sendcmd(CANON_FOCBYHANDS));
break; break;
case 'I': case 'I':
USB_send("Reinit SPI\n"); USB_sendstr("Reinit SPI\n");
spi_setup(); spi_setup();
canon_init(); canon_init();
return NULL; return;
break; break;
case 'P': case 'P':
dump_userconf(); dump_userconf();
return NULL; return;
break; break;
case 'R': case 'R':
USB_send("Soft reset\n"); USB_sendstr("Soft reset\n");
NVIC_SystemReset(); NVIC_SystemReset();
break; break;
case 'T': case 'T':
add2buf("Tms="); USB_sendstr("Tms=");
add2buf(u2str(Tms)); USB_sendstr(u2str(Tms));
break; break;
case 'X': case 'X':
if(store_userconf()) add2buf(FAIL); if(store_userconf()) USB_sendstr(FAIL);
else add2buf(OK); else USB_sendstr(OK);
break; break;
default: default:
return helpmsg; USB_sendstr(helpmsg);
} }
newline(); newline();
return stbuf; return;
} }
uint32_t D = 0; uint32_t D = 0;
int16_t neg; int16_t neg;
@ -367,8 +351,8 @@ const char *parse_cmd(const char *buf){
neg = 1; neg = 1;
if(*buf == '-'){ ++buf; neg = -1; } if(*buf == '-'){ ++buf; neg = -1; }
nxt = getnum(buf, &D); nxt = getnum(buf, &D);
if(nxt == buf) add2buf("Need number"); if(nxt == buf) USB_sendstr("Need number");
else if(D > 0x7fff) add2buf("From -0x7fff to 0x7fff"); else if(D > 0x7fff) USB_sendstr("From -0x7fff to 0x7fff");
else errw(canon_focus(neg * (int16_t)D)); else errw(canon_focus(neg * (int16_t)D));
break; break;
case 'A': case 'A':
@ -377,7 +361,7 @@ const char *parse_cmd(const char *buf){
if(D) the_conf.autoinit = 1; if(D) the_conf.autoinit = 1;
else the_conf.autoinit = 0; else the_conf.autoinit = 0;
} }
USB_send("autoinit="); USB_send(u2str(the_conf.autoinit)); USB_send("\n"); USB_sendstr("autoinit="); USB_sendstr(u2str(the_conf.autoinit)); USB_sendstr("\n");
break; break;
case 'd': case 'd':
nxt = omit_spaces(buf); nxt = omit_spaces(buf);
@ -388,26 +372,26 @@ const char *parse_cmd(const char *buf){
neg = 1; neg = 1;
if(*buf == '-'){ ++buf; neg = -1; } if(*buf == '-'){ ++buf; neg = -1; }
nxt = getnum(buf, &D); nxt = getnum(buf, &D);
if(nxt == buf) add2buf("Need number"); if(nxt == buf) USB_sendstr("Need number");
else if(D > 0x7fff) add2buf("From -0x7fff to 0x7fff"); else if(D > 0x7fff) USB_sendstr("From -0x7fff to 0x7fff");
else{ else{
if(canon_writeu16(CANON_FOCMOVE, neg * (int16_t)D)) add2buf(OK); if(canon_writeu16(CANON_FOCMOVE, neg * (int16_t)D)) USB_sendstr(OK);
else add2buf(FAIL); else USB_sendstr(FAIL);
} }
break; break;
case 'C': case 'C':
nxt = getnum(buf, &D); nxt = getnum(buf, &D);
if(nxt != buf && D >= 25 && D <= 3000){ if(nxt != buf && D >= 25 && D <= 3000){
the_conf.canspeed = D; the_conf.canspeed = D;
add2buf("CAN_speed="); add2buf(u2str(the_conf.canspeed)); USB_sendstr("CAN_speed="); USB_sendstr(u2str(the_conf.canspeed));
}else add2buf(FAIL); }else USB_sendstr(FAIL);
break; break;
case 'D': case 'D':
nxt = getnum(buf, &D); nxt = getnum(buf, &D);
if(nxt != buf && D < 0x800){ if(nxt != buf && D < 0x800){
the_conf.canID = D; the_conf.canID = D;
add2buf("CAN_ID="); add2buf(u2str(the_conf.canID)); USB_sendstr("CAN_ID="); USB_sendstr(u2str(the_conf.canID));
}else add2buf(FAIL); }else USB_sendstr(FAIL);
break; break;
case 'F': // SPI flags case 'F': // SPI flags
nxt = omit_spaces(buf); nxt = omit_spaces(buf);
@ -415,7 +399,10 @@ const char *parse_cmd(const char *buf){
if(*nxt && *nxt != '\n'){ if(*nxt && *nxt != '\n'){
buf = nxt + 1; buf = nxt + 1;
nxt = getnum(buf, &D); nxt = getnum(buf, &D);
if(buf == nxt || D > 7) return helpmsg; if(buf == nxt || D > 7){
USB_sendstr(helpmsg);
return;
}
} }
switch(c){ switch(c){
case 'b': case 'b':
@ -435,39 +422,39 @@ const char *parse_cmd(const char *buf){
else SPI_CR1 &= ~SPI_CR1_CPOL; else SPI_CR1 &= ~SPI_CR1_CPOL;
break; break;
default: default:
return helpmsg; USB_sendstr(helpmsg);
return;
} }
add2buf("SPI_CR1="); add2buf(u2hexstr(SPI_CR1)); USB_sendstr("SPI_CR1="); USB_sendstr(u2hexstr(SPI_CR1));
break; break;
case 'L': case 'L':
if(0 == initspibuf(buf)){ if(0 == initspibuf(buf)){
USB_send("Enter data bytes\n"); USB_sendstr("Enter data bytes\n");
return NULL; return;
} }
USB_send("OK, activated\n"); USB_sendstr("OK, activated\n");
sendspibuf(); sendspibuf();
lastFloodTime = Tms ? Tms : 1; lastFloodTime = Tms ? Tms : 1;
return NULL; return;
break; break;
case 'S': // use stbuf here to store user data case 'S': // use stbuf here to store user data
if(0 == initspibuf(buf)){ if(0 == initspibuf(buf)){
USB_send("Enter data bytes\n"); USB_sendstr("Enter data bytes\n");
return NULL; return;
} }
USB_send("Send: "); USB_sendstr("Send: ");
for(int i = 0; i < spibuflen; ++i){ for(int i = 0; i < spibuflen; ++i){
if(i) USB_send(", "); if(i) USB_sendstr(", ");
USB_send(u2hexstr(spibuf[i])); USB_sendstr(u2hexstr(spibuf[i]));
} }
USB_send("\n"); USB_sendstr("\n");
sendspibuf(); sendspibuf();
return NULL; return;
break; break;
default: default:
return --buf; return;
} }
newline(); newline();
return stbuf;
} }

View File

@ -22,8 +22,8 @@
#include <stm32f1.h> #include <stm32f1.h>
#define printu(x) do{USB_send(u2str(x));}while(0) #define printu(x) do{USB_sendstr(u2str(x));}while(0)
#define printuhex(x) do{USB_send(uhex2str(x));}while(0) #define printuhex(x) do{USB_sendstr(uhex2str(x));}while(0)
#ifdef EBUG #ifdef EBUG
#define DBG(x) do{USB_send(x); USB_send("\n");}while(0) #define DBG(x) do{USB_send(x); USB_send("\n");}while(0)
@ -31,7 +31,7 @@
#define DBG(x) #define DBG(x)
#endif #endif
const char *parse_cmd(const char *buf); void parse_cmd(const char *buf);
char *omit_spaces(const char *buf); char *omit_spaces(const char *buf);
char *getnum(const char *buf, uint32_t *N); char *getnum(const char *buf, uint32_t *N);
char *u2str(uint32_t val); char *u2str(uint32_t val);

View File

@ -1,6 +1,5 @@
/* /*
* This file is part of the canonmanage project. * Copyright 2023 Edward V. Emelianov <edward.emelianoff@gmail.com>.
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -16,62 +15,152 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <stm32f1.h>
#include "ringbuffer.h" #include "ringbuffer.h"
// ring buffer static int datalen(ringbuffer *b){
static char ringbuffer[RBSIZE]; if(b->tail >= b->head) return (b->tail - b->head);
// head - position of first data byte else return (b->length - b->head + b->tail);
// tail - position of last data byte + 1
// head == tail - empty! So, buffer can't store more than RBSIZE-1 bytes of data!
static volatile int head = 0, tail = 0;
static int datalen(){
if(tail >= head) return (tail - head);
else return (RBSIZE - head + tail);
}
static int restlen(){
return (RBSIZE - 1 - datalen());
} }
static void mcpy(char *targ, const char *src, int l){ // stored data length
int RB_datalen(ringbuffer *b){
if(b->busy) return -1;
b->busy = 1;
int l = datalen(b);
b->busy = 0;
return l;
}
static int hasbyte(ringbuffer *b, uint8_t byte){
if(b->head == b->tail) return -1; // no data in buffer
int startidx = b->head;
if(b->head > b->tail){ //
for(int found = b->head; found < b->length; ++found)
if(b->data[found] == byte) return found;
startidx = 0;
}
for(int found = startidx; found < b->tail; ++found)
if(b->data[found] == byte) return found;
return -1;
}
/**
* @brief RB_hasbyte - check if buffer has given byte stored
* @param b - buffer
* @param byte - byte to find
* @return index if found, -1 if none or busy
*/
int RB_hasbyte(ringbuffer *b, uint8_t byte){
if(b->busy) return -1;
b->busy = 1;
int ret = hasbyte(b, byte);
b->busy = 0;
return ret;
}
// poor memcpy
static void mcpy(uint8_t *targ, const uint8_t *src, int l){
while(l--) *targ++ = *src++; while(l--) *targ++ = *src++;
} }
TRUE_INLINE void incr(volatile int *what, int n){ // increment head or tail
TRUE_INLINE void incr(ringbuffer *b, volatile int *what, int n){
*what += n; *what += n;
if(*what >= RBSIZE) *what -= RBSIZE; if(*what >= b->length) *what -= b->length;
} }
int RB_read(char s[BLOCKSIZE]){ static int read(ringbuffer *b, uint8_t *s, int len){
int l = datalen(); int l = datalen(b);
if(!l) return 0; if(!l) return 0;
if(l > BLOCKSIZE) l = BLOCKSIZE; if(l > len) l = len;
int _1st = RBSIZE - head; int _1st = b->length - b->head;
if(_1st > l) _1st = l; if(_1st > l) _1st = l;
if(_1st > BLOCKSIZE) _1st = BLOCKSIZE; if(_1st > len) _1st = len;
mcpy(s, ringbuffer+head, _1st); mcpy(s, b->data + b->head, _1st);
if(_1st < BLOCKSIZE && l > _1st){ if(_1st < len && l > _1st){
mcpy(s+_1st, ringbuffer, l-_1st); mcpy(s+_1st, b->data, l - _1st);
incr(&head, l); incr(b, &b->head, l);
return l; return l;
} }
incr(&head ,_1st); incr(b, &b->head, _1st);
return _1st; return _1st;
} }
int RB_write(const char *str, int l){ /**
int r = restlen(); * @brief RB_read - read data from ringbuffer
if(l > r) l = r; * @param b - buffer
if(!l) return 0; * @param s - array to write data
int _1st = RBSIZE - tail; * @param len - max len of `s`
* @return bytes read or -1 if busy
*/
int RB_read(ringbuffer *b, uint8_t *s, int len){
if(b->busy) return -1;
b->busy = 1;
int r = read(b, s, len);
b->busy = 0;
return r;
}
static int readto(ringbuffer *b, uint8_t byte, uint8_t *s, int len){
int idx = hasbyte(b, byte);
if(idx < 0) return 0;
int partlen = idx + 1 - b->head;
// now calculate length of new data portion
if(idx < b->head) partlen += b->length;
if(partlen > len) return -read(b, s, len);
return read(b, s, partlen);
}
/**
* @brief RB_readto fill array `s` with data until byte `byte` (with it)
* @param b - ringbuffer
* @param byte - check byte
* @param s - buffer to write data
* @param len - length of `s`
* @return amount of bytes written (negative, if len<data in buffer or buffer is busy)
*/
int RB_readto(ringbuffer *b, uint8_t byte, uint8_t *s, int len){
if(b->busy) return -1;
b->busy = 1;
int n = readto(b, byte, s, len);
b->busy = 0;
return n;
}
static int write(ringbuffer *b, const uint8_t *str, int l){
int r = b->length - 1 - datalen(b); // rest length
if(l > r || !l) return 0;
int _1st = b->length - b->tail;
if(_1st > l) _1st = l; if(_1st > l) _1st = l;
mcpy(ringbuffer+tail, str, _1st); mcpy(b->data + b->tail, str, _1st);
if(_1st < l){ // add another piece from start if(_1st < l){ // add another piece from start
mcpy(ringbuffer, str+_1st, l-_1st); mcpy(b->data, str+_1st, l-_1st);
} }
incr(&tail, l); incr(b, &b->tail, l);
return l; return l;
} }
/**
* @brief RB_write - write some data to ringbuffer
* @param b - buffer
* @param str - data
* @param l - length
* @return amount of bytes written or -1 if busy
*/
int RB_write(ringbuffer *b, const uint8_t *str, int l){
if(b->busy) return -1;
b->busy = 1;
int w = write(b, str, l);
b->busy = 0;
return w;
}
// just delete all information in buffer `b`
int RB_clearbuf(ringbuffer *b){
if(b->busy) return -1;
b->busy = 1;
b->head = 0;
b->tail = 0;
b->busy = 0;
return 1;
}

View File

@ -1,6 +1,5 @@
/* /*
* This file is part of the canonmanage project. * Copyright 2023 Edward V. Emelianov <edward.emelianoff@gmail.com>.
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -17,17 +16,26 @@
*/ */
#pragma once #pragma once
#ifndef RINGBUFFER_H__
#define RINGBUFFER_H__
#include "usbhw.h" // for USB_TXBUFSZ #if defined STM32F0
#include <stm32f0.h>
#elif defined STM32F1
#include <stm32f1.h>
#elif defined STM32F3
#include <stm32f3.h>
#endif
// ring buffer size in bytes typedef struct{
#define RBSIZE (512) uint8_t *data; // data buffer
// max reading portion size const int length; // its length
#define BLOCKSIZE (USB_TXBUFSZ) int head; // head index
int tail; // tail index
volatile int busy; // == TRUE if buffer is busy now
} ringbuffer;
int RB_read(char s[BLOCKSIZE]); int RB_read(ringbuffer *b, uint8_t *s, int len);
int RB_write(const char *str, int l); int RB_readto(ringbuffer *b, uint8_t byte, uint8_t *s, int len);
int RB_hasbyte(ringbuffer *b, uint8_t byte);
#endif // RINGBUFFER_H__ int RB_write(ringbuffer *b, const uint8_t *str, int l);
int RB_datalen(ringbuffer *b);
int RB_clearbuf(ringbuffer *b);

View File

@ -1,119 +0,0 @@
/*
* This file is part of the MLX90640 project.
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ringbuffer.h"
#include "usb.h"
#include "usb_lib.h"
static char usbbuff[USB_TXBUFSZ]; // temporary buffer for sending data
volatile uint8_t tx_succesfull = 1;
static volatile uint8_t rxNE = 0;
void send_next(){
//if(!tx_succesfull) return;
static int lastdsz = 0;
int buflen = RB_read(usbbuff);
if(!buflen){
if(lastdsz == 64) EP_Write(3, NULL, 0); // send ZLP after 64 bits packet when nothing more to send
lastdsz = 0;
return;
}
tx_succesfull = 0;
EP_Write(3, (uint8_t*)usbbuff, buflen);
lastdsz = buflen;
}
// put `buf` into queue to send
void USB_send(const char *buf){
if(!buf || !usbON) return;
int len = 0;
const char *b = buf;
while(*b++) ++len;
if(!usbON || !len) return;
int l = len;
while(l){
if(tx_succesfull) send_next();
int a = RB_write(buf, l);
l -= a;
buf += a;
}
}
// interrupt IN handler (never used?)
static void EP1_Handler(){
uint16_t epstatus = KEEP_DTOG(USB->EPnR[1]);
if(RX_FLAG(epstatus)) epstatus = (epstatus & ~USB_EPnR_STAT_TX) ^ USB_EPnR_STAT_RX; // set valid RX
else epstatus = epstatus & ~(USB_EPnR_STAT_TX|USB_EPnR_STAT_RX);
// clear CTR
epstatus = (epstatus & ~(USB_EPnR_CTR_RX|USB_EPnR_CTR_TX));
USB->EPnR[1] = epstatus;
}
// data IN/OUT handlers
static void transmit_Handler(){ // EP3IN
tx_succesfull = 1;
uint16_t epstatus = KEEP_DTOG_STAT(USB->EPnR[3]);
// clear CTR keep DTOGs & STATs
USB->EPnR[3] = (epstatus & ~(USB_EPnR_CTR_TX)); // clear TX ctr
}
static void receive_Handler(){ // EP2OUT
rxNE = 1;
uint16_t epstatus = KEEP_DTOG_STAT(USB->EPnR[2]);
USB->EPnR[2] = (epstatus & ~(USB_EPnR_CTR_RX)); // clear RX ctr
}
void usb_proc(){
if(CAN1->RF0R & CAN_RF0R_FOVR0){ // FIFO overrun
CAN1->RF0R &= ~CAN_RF0R_FOVR0;
}
switch(USB_Dev.USB_Status){
case USB_STATE_CONFIGURED:
// make new BULK endpoint
// Buffer have 1024 bytes, but last 256 we use for CAN bus (30.2 of RM: USB main features)
EP_Init(1, EP_TYPE_INTERRUPT, USB_EP1BUFSZ, 0, EP1_Handler); // IN1 - transmit
EP_Init(2, EP_TYPE_BULK, 0, USB_RXBUFSZ, receive_Handler); // OUT2 - receive data
EP_Init(3, EP_TYPE_BULK, USB_TXBUFSZ, 0, transmit_Handler); // IN3 - transmit data
USB_Dev.USB_Status = USB_STATE_CONNECTED;
break;
case USB_STATE_DEFAULT:
case USB_STATE_ADDRESSED:
if(usbON){
usbON = 0;
}
break;
default: // USB_STATE_CONNECTED - send next data portion
if(!usbON) return;
if(tx_succesfull) send_next();
}
}
/**
* @brief USB_receive
* @param buf (i) - buffer[64] for received data
* @return amount of received bytes
*/
uint8_t USB_receive(char *buf){
if(!usbON || !rxNE) return 0;
uint8_t sz = EP_Read(2, (uint16_t*)buf);
uint16_t epstatus = KEEP_DTOG(USB->EPnR[2]);
// keep stat_tx & set ACK rx
USB->EPnR[2] = (epstatus & ~(USB_EPnR_STAT_TX)) ^ USB_EPnR_STAT_RX;
rxNE = 0;
return sz;
}

View File

@ -1,34 +0,0 @@
/*
* This file is part of the MLX90640 project.
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef __USB_H__
#define __USB_H__
#include "usbhw.h"
#define BUFFSIZE (64)
extern volatile uint8_t tx_succesfull;
void usb_proc();
void send_next();
void USB_send(const char *buf);
uint8_t USB_receive(char *buf);
#endif // __USB_H__

View File

@ -0,0 +1,210 @@
/*
* Copyright 2024 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "usb_descr.h"
// low/high for uint16_t
#define L16(x) (x & 0xff)
#define H16(x) (x >> 8)
static const uint8_t USB_DeviceDescriptor[] = {
USB_DT_DEVICE_SIZE, // bLength
USB_DT_DEVICE, // bDescriptorType
L16(bcdUSB), // bcdUSB_L
H16(bcdUSB), // bcdUSB_H
USB_CLASS_MISC, // bDeviceClass
bDeviceSubClass, // bDeviceSubClass
bDeviceProtocol, // bDeviceProtocol
USB_EP0BUFSZ, // bMaxPacketSize
L16(idVendor), // idVendor_L
H16(idVendor), // idVendor_H
L16(idProduct), // idProduct_L
H16(idProduct), // idProduct_H
L16(bcdDevice_Ver), // bcdDevice_Ver_L
H16(bcdDevice_Ver), // bcdDevice_Ver_H
iMANUFACTURER_DESCR, // iManufacturer - indexes of string descriptors in array
iPRODUCT_DESCR, // iProduct
iSERIAL_DESCR, // iSerial
bNumConfigurations // bNumConfigurations
};
static const uint8_t USB_DeviceQualifierDescriptor[] = {
USB_DT_QUALIFIER_SIZE, //bLength
USB_DT_QUALIFIER, // bDescriptorType
L16(bcdUSB), // bcdUSB_L
H16(bcdUSB), // bcdUSB_H
USB_CLASS_PER_INTERFACE, // bDeviceClass
bDeviceSubClass, // bDeviceSubClass
bDeviceProtocol, // bDeviceProtocol
USB_EP0BUFSZ, // bMaxPacketSize0
bNumConfigurations, // bNumConfigurations
0 // Reserved
};
#define wTotalLength (USB_DT_CONFIG_SIZE + (bNumInterfaces * USB_DT_INTERFACE_SIZE) + (bTotNumEndpoints * USB_DT_ENDPOINT_SIZE) + (bNumCsInterfaces * USB_DT_CS_INTERFACE_SIZE) - 1)
static const uint8_t USB_ConfigDescriptor[] = {
// Configuration Descriptor
USB_DT_CONFIG_SIZE, // bLength: Configuration Descriptor size
USB_DT_CONFIG, // bDescriptorType: Configuration
L16(wTotalLength), // wTotalLength.L :no of returned bytes
H16(wTotalLength), // wTotalLength.H
bNumInterfaces, // bNumInterfaces
1, // bConfigurationValue: Current configuration value
0, // iConfiguration: Index of string descriptor describing the configuration or 0
BusPowered, // bmAttributes - Bus powered
50, // MaxPower in 2mA units
//---------------------------------------------------------------------------
// Virtual command Interface Descriptor
USB_DT_INTERFACE_SIZE, // bLength: Interface Descriptor size
USB_DT_INTERFACE, // bDescriptorType: Interface
0, // bInterfaceNumber: Number of Interface
0, // bAlternateSetting: Alternate setting
1, // bNumEndpoints: one for this
USB_CLASS_COMM, // bInterfaceClass
2, // bInterfaceSubClass: ACM
1, // bInterfaceProtocol: Common AT commands
iINTERFACE_DESCR1, // iInterface
// ---- CS Interfaces
USB_DT_CS_INTERFACE_SIZE, // bLength
USB_DT_CS_INTERFACE, // bDescriptorType: CS_INTERFACE
0, // bDescriptorSubtype: Header Func Desc
0x10, // bcdCDC: spec release number
1, // bDataInterface
USB_DT_CS_INTERFACE_SIZE, // bLength
USB_DT_CS_INTERFACE, // bDescriptorType: CS_INTERFACE
1, // bDescriptorSubtype: Call Management Func Desc
0, // bmCapabilities: D0+D1
1, // bDataInterface
USB_DT_CS_INTERFACE_SIZE-1, // bLength
USB_DT_CS_INTERFACE, // bDescriptorType: CS_INTERFACE
2, // bDescriptorSubtype: Abstract Control Management desc
2, // bmCapabilities
USB_DT_CS_INTERFACE_SIZE, // bLength
USB_DT_CS_INTERFACE, // bDescriptorType: CS_INTERFACE
6, // bDescriptorSubtype: Union func desc
0, // bMasterInterface: Communication class interface
1, // bSlaveInterface0: Data Class Interface
// Virtual endpoint 1 Descriptor
USB_DT_ENDPOINT_SIZE, // bLength: Endpoint Descriptor size
USB_DT_ENDPOINT, // bDescriptorType: Endpoint
0x8A, // bEndpointAddress IN10
USB_BM_ATTR_INTERRUPT, // bmAttributes: Interrupt
L16(USB_EP1BUFSZ), // wMaxPacketSize LO
H16(USB_EP1BUFSZ), // wMaxPacketSize HI
0x10, // bInterval: 16ms
//---------------------------------------------------------------------------
// Data interface
USB_DT_INTERFACE_SIZE, // bLength: Interface Descriptor size
USB_DT_INTERFACE, // bDescriptorType: Interface
1, // bInterfaceNumber: Number of Interface
0, // bAlternateSetting: Alternate setting
2, // bNumEndpoints: in and out
USB_CLASS_DATA, // bInterfaceClass
2, // bInterfaceSubClass: ACM
0, // bInterfaceProtocol
0, // iInterface
//Endpoint IN1 Descriptor
USB_DT_ENDPOINT_SIZE, // bLength: Endpoint Descriptor size
USB_DT_ENDPOINT, // bDescriptorType: Endpoint
0x81, // bEndpointAddress: IN1
USB_BM_ATTR_BULK, // bmAttributes: Bulk
L16(USB_TXBUFSZ), // wMaxPacketSize LO
H16(USB_TXBUFSZ), // wMaxPacketSize HI
0, // bInterval: ignore for Bulk transfer
// Endpoint OUT1 Descriptor
USB_DT_ENDPOINT_SIZE, // bLength: Endpoint Descriptor size
USB_DT_ENDPOINT, // bDescriptorType: Endpoint
0x01, // bEndpointAddress: OUT1
USB_BM_ATTR_BULK, // bmAttributes: Bulk
L16(USB_RXBUFSZ), // wMaxPacketSize LO
H16(USB_RXBUFSZ), // wMaxPacketSize HI
0, // bInterval: ignore for Bulk transfer
};
//const uint8_t HID_ReportDescriptor[];
_USB_LANG_ID_(LD, LANG_US);
_USB_STRING_(SD, u"0.1.0");
_USB_STRING_(MD, u"eddy@sao.ru");
_USB_STRING_(PD, u"USB/CAN Canon lens controller");
_USB_STRING_(ID, u"canonlens");
static const void* const StringDescriptor[iDESCR_AMOUNT] = {
[iLANGUAGE_DESCR] = &LD,
[iMANUFACTURER_DESCR] = &MD,
[iPRODUCT_DESCR] = &PD,
[iSERIAL_DESCR] = &SD,
[iINTERFACE_DESCR1] = &ID
};
static void wr0(const uint8_t *buf, uint16_t size, uint16_t askedsize){
if(askedsize < size) size = askedsize; // shortened request
if(size < USB_EP0BUFSZ){
EP_WriteIRQ(0, buf, size);
return;
}
while(size){
uint16_t l = size;
if(l > USB_EP0BUFSZ) l = USB_EP0BUFSZ;
EP_WriteIRQ(0, buf, l);
buf += l;
size -= l;
uint8_t needzlp = (l == USB_EP0BUFSZ) ? 1 : 0;
if(size || needzlp){ // send last data buffer
uint16_t epstatus = KEEP_DTOG(USB->EPnR[0]);
// keep DTOGs, clear CTR_RX,TX, set TX VALID, leave stat_Rx
USB->EPnR[0] = (epstatus & ~(USB_EPnR_CTR_RX|USB_EPnR_CTR_TX|USB_EPnR_STAT_RX))
^ USB_EPnR_STAT_TX;
uint32_t ctr = 1000000;
while(--ctr && (USB->ISTR & USB_ISTR_CTR) == 0){IWDG->KR = IWDG_REFRESH;};
if((USB->ISTR & USB_ISTR_CTR) == 0){
return;
}
if(needzlp) EP_WriteIRQ(0, NULL, 0);
}
}
}
void get_descriptor(config_pack_t *pack){
uint8_t descrtype = pack->wValue >> 8,
descridx = pack->wValue & 0xff;
switch(descrtype){
case DEVICE_DESCRIPTOR:
wr0(USB_DeviceDescriptor, sizeof(USB_DeviceDescriptor), pack->wLength);
break;
case CONFIGURATION_DESCRIPTOR:
wr0(USB_ConfigDescriptor, sizeof(USB_ConfigDescriptor), pack->wLength);
break;
case STRING_DESCRIPTOR:
if(descridx < iDESCR_AMOUNT){
wr0((const uint8_t *)StringDescriptor[descridx], *((uint8_t*)StringDescriptor[descridx]), pack->wLength);
}else{
EP_WriteIRQ(0, NULL, 0);
}
break;
case DEVICE_QUALIFIER_DESCRIPTOR:
wr0(USB_DeviceQualifierDescriptor, sizeof(USB_DeviceQualifierDescriptor), pack->wLength);
break;
/* case HID_REPORT_DESCRIPTOR:
wr0(HID_ReportDescriptor, sizeof(HID_ReportDescriptor), pack->wLength);
break;*/
default:
break;
}
}

View File

@ -0,0 +1,68 @@
/*
* Copyright 2024 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stdint.h>
#include "usb_lib.h"
// definition of parts common for USB_DeviceDescriptor & USB_DeviceQualifierDescriptor
// bcdUSB: 1.10
#define bcdUSB 0x0110
// Class - Misc (EF), subclass - common (2), protocol - interface association descr (1)
#define bDeviceSubClass 0x02
#define bDeviceProtocol 0x01
#define idVendor 0x0483
#define idProduct 0x5740
#define bcdDevice_Ver 0x0200
#define bNumConfigurations 1
// amount of interfaces and endpoints (except 0) used
#define bNumInterfaces 2
#define bTotNumEndpoints 3
#define bNumCsInterfaces 4
// powered
#define BusPowered (1<<7)
#define SelfPowered (1<<6)
#define RemoteWakeup (1<<5)
// buffer sizes
// for USB FS EP0 buffers are from 8 to 64 bytes long
#define USB_EP0BUFSZ 64
#define USB_EP1BUFSZ 10
// Rx/Tx EPs
#define USB_RXBUFSZ 64
#define USB_TXBUFSZ 64
// string descriptors
enum{
iLANGUAGE_DESCR,
iMANUFACTURER_DESCR,
iPRODUCT_DESCR,
iSERIAL_DESCR,
iINTERFACE_DESCR1,
/* iINTERFACE_DESCR2,
iINTERFACE_DESCR3,
iINTERFACE_DESCR4,
iINTERFACE_DESCR5,
iINTERFACE_DESCR6,
iINTERFACE_DESCR7,*/
iDESCR_AMOUNT
};
void get_descriptor(config_pack_t *pack);

View File

@ -0,0 +1,241 @@
/*
* Copyright 2024 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stm32f1.h>
#include <string.h>
#include "ringbuffer.h"
#include "usb_descr.h"
#include "usb_dev.h"
// Class-Specific Control Requests
#define SEND_ENCAPSULATED_COMMAND 0x00 // unused
#define GET_ENCAPSULATED_RESPONSE 0x01 // unused
#define SET_COMM_FEATURE 0x02 // unused
#define GET_COMM_FEATURE 0x03 // unused
#define CLEAR_COMM_FEATURE 0x04 // unused
#define SET_LINE_CODING 0x20
#define GET_LINE_CODING 0x21
#define SET_CONTROL_LINE_STATE 0x22
#define SEND_BREAK 0x23
// control line states
#define CONTROL_DTR 0x01
#define CONTROL_RTS 0x02
// inbuf overflow when receiving
static volatile uint8_t bufovrfl = 0;
// receive buffer: hold data until chkin() call
static uint8_t volatile rcvbuf[USB_RXBUFSZ];
static uint8_t volatile rcvbuflen = 0;
// line coding
usb_LineCoding WEAK lineCoding = {115200, 0, 0, 8};
// CDC configured and ready to use
volatile uint8_t CDCready = 0;
// ring buffers for incoming and outgoing data
static uint8_t obuf[RBOUTSZ], ibuf[RBINSZ];
static volatile ringbuffer rbout = {.data = obuf, .length = RBOUTSZ, .head = 0, .tail = 0};
static volatile ringbuffer rbin = {.data = ibuf, .length = RBINSZ, .head = 0, .tail = 0};
// last send data size
static volatile int lastdsz = 0;
static void chkin(){
if(bufovrfl) return; // allow user to know that previous buffer was overflowed and cleared
if(!rcvbuflen) return;
int w = RB_write((ringbuffer*)&rbin, (uint8_t*)rcvbuf, rcvbuflen);
if(w < 0){
return;
}
if(w != rcvbuflen) bufovrfl = 1;
rcvbuflen = 0;
uint16_t status = KEEP_DTOG(USB->EPnR[1]); // don't change DTOG
USB->EPnR[1] = (status & ~(USB_EPnR_STAT_TX|USB_EPnR_CTR_RX)) ^ USB_EPnR_STAT_RX; // prepare to get next data portion
}
// called from transmit EP to send next data portion or by user - when new transmission starts
static void send_next(){
uint8_t usbbuff[USB_TXBUFSZ];
int buflen = RB_read((ringbuffer*)&rbout, (uint8_t*)usbbuff, USB_TXBUFSZ);
if(buflen == 0){
if(lastdsz == 64) EP_Write(1, NULL, 0); // send ZLP after 64 bits packet when nothing more to send
lastdsz = 0;
return;
}else if(buflen < 0){
lastdsz = 0;
return;
}
EP_Write(1, (uint8_t*)usbbuff, buflen);
lastdsz = buflen;
}
// data IN/OUT handler
static void rxtx_handler(){
uint16_t epstatus = KEEP_DTOG(USB->EPnR[1]);
if(RX_FLAG(epstatus)){ // receive data
if(rcvbuflen){
bufovrfl = 1; // lost last data
rcvbuflen = 0;
}
rcvbuflen = EP_Read(1, (uint8_t*)rcvbuf);
USB->EPnR[1] = epstatus & ~(USB_EPnR_CTR_RX | USB_EPnR_STAT_RX | USB_EPnR_STAT_TX); // keep RX in STALL state until read data
chkin(); // try to write current data into RXbuf if it's not busy
}else{ // tx successfull
USB->EPnR[1] = (epstatus & ~(USB_EPnR_CTR_TX | USB_EPnR_STAT_TX)) ^ USB_EPnR_STAT_RX;
send_next();
}
}
// weak handlers: change them somewhere else if you want to setup USART
// SET_LINE_CODING
void WEAK linecoding_handler(usb_LineCoding *lc){
lineCoding = *lc;
}
// SET_CONTROL_LINE_STATE
void WEAK clstate_handler(uint16_t val){
CDCready = val; // CONTROL_DTR | CONTROL_RTS -> interface connected; 0 -> disconnected
}
// SEND_BREAK
void WEAK break_handler(){
CDCready = 0;
}
// USB is configured: setup endpoints
void set_configuration(){
EP_Init(1, EP_TYPE_BULK, USB_TXBUFSZ, USB_RXBUFSZ, rxtx_handler); // IN1 and OUT1
}
// PL2303 CLASS request
void usb_class_request(config_pack_t *req, uint8_t *data, uint16_t datalen){
uint8_t recipient = REQUEST_RECIPIENT(req->bmRequestType);
uint8_t dev2host = (req->bmRequestType & 0x80) ? 1 : 0;
switch(recipient){
case REQ_RECIPIENT_INTERFACE:
switch(req->bRequest){
case SET_LINE_CODING:
if(!data || !datalen) break; // wait for data
if(datalen == sizeof(usb_LineCoding))
linecoding_handler((usb_LineCoding*)data);
break;
case GET_LINE_CODING:
EP_WriteIRQ(0, (uint8_t*)&lineCoding, sizeof(lineCoding));
break;
case SET_CONTROL_LINE_STATE:
clstate_handler(req->wValue);
break;
case SEND_BREAK:
break_handler();
break;
default:
break;
}
break;
default:
if(dev2host) EP_WriteIRQ(0, NULL, 0);
}
if(!dev2host) EP_WriteIRQ(0, NULL, 0);
}
// blocking send full content of ring buffer
int USB_sendall(){
while(lastdsz > 0){
if(!CDCready) return FALSE;
}
return TRUE;
}
// put `buf` into queue to send
int USB_send(const uint8_t *buf, int len){
if(!buf || !CDCready || !len) return FALSE;
while(len){
int a = RB_write((ringbuffer*)&rbout, buf, len);
if(a > 0){
len -= a;
buf += a;
} else if (a < 0) continue; // do nothing if buffer is in reading state
if(lastdsz == 0) send_next(); // need to run manually - all data sent, so no IRQ on IN
}
return TRUE;
}
int USB_putbyte(uint8_t byte){
if(!CDCready) return FALSE;
int l = 0;
while((l = RB_write((ringbuffer*)&rbout, &byte, 1)) != 1){
if(l < 0) continue;
}
if(lastdsz == 0) send_next(); // need to run manually - all data sent, so no IRQ on IN
return TRUE;
}
int USB_sendstr(const char *string){
if(!string || !CDCready) return FALSE;
int len = 0;
const char *b = string;
while(*b++) ++len;
if(!len) return FALSE;
return USB_send((const uint8_t*)string, len);
}
/**
* @brief USB_receive - get binary data from receiving ring-buffer
* @param buf (i) - buffer for received data
* @param len - length of `buf`
* @return amount of received bytes (negative, if overfull happened)
*/
int USB_receive(uint8_t *buf, int len){
chkin();
if(bufovrfl){
while(1 != RB_clearbuf((ringbuffer*)&rbin));
bufovrfl = 0;
return -1;
}
int sz = RB_read((ringbuffer*)&rbin, buf, len);
if(sz < 0) return 0; // buffer in writting state
return sz;
}
/**
* @brief USB_receivestr - get string up to '\n' and replace '\n' with 0
* @param buf - receiving buffer
* @param len - its length
* @return strlen or negative value indicating overflow (if so, string won't be ends with 0 and buffer should be cleared)
*/
int USB_receivestr(char *buf, int len){
chkin();
if(bufovrfl){
while(1 != RB_clearbuf((ringbuffer*)&rbin));
bufovrfl = 0;
return -1;
}
int l = RB_readto((ringbuffer*)&rbin, '\n', (uint8_t*)buf, len);
if(l < 1){
if(rbin.length == RB_datalen((ringbuffer*)&rbin)){ // buffer is full but no '\n' found
while(1 != RB_clearbuf((ringbuffer*)&rbin));
return -1;
}
return 0;
}
if(l == 0) return 0;
buf[l-1] = 0; // replace '\n' with strend
return l;
}

View File

@ -0,0 +1,57 @@
/*
* Copyright 2024 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stm32f1.h>
#include "usb_lib.h"
typedef struct {
uint32_t dwDTERate;
uint8_t bCharFormat;
#define USB_CDC_1_STOP_BITS 0
#define USB_CDC_1_5_STOP_BITS 1
#define USB_CDC_2_STOP_BITS 2
uint8_t bParityType;
#define USB_CDC_NO_PARITY 0
#define USB_CDC_ODD_PARITY 1
#define USB_CDC_EVEN_PARITY 2
#define USB_CDC_MARK_PARITY 3
#define USB_CDC_SPACE_PARITY 4
uint8_t bDataBits;
} __attribute__ ((packed)) usb_LineCoding;
extern usb_LineCoding lineCoding;
extern volatile uint8_t CDCready;
void break_handler();
void clstate_handler(uint16_t val);
void linecoding_handler(usb_LineCoding *lc);
// sizes of ringbuffers for outgoing and incoming data
#define RBOUTSZ (1024)
#define RBINSZ (1024)
#define newline() USB_putbyte('\n')
#define USND(s) do{USB_sendstr(s); USB_putbyte('\n');}while(0)
int USB_sendall();
int USB_send(const uint8_t *buf, int len);
int USB_putbyte(uint8_t byte);
int USB_sendstr(const char *string);
int USB_receive(uint8_t *buf, int len);
int USB_receivestr(char *buf, int len);

View File

@ -1,6 +1,5 @@
/* /*
* This file is part of the MLX90640 project. * Copyright 2024 Edward V. Emelianov <edward.emelianoff@gmail.com>.
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -15,251 +14,100 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <stdint.h> #include <stdint.h>
#include "usb_lib.h" #include "usb_lib.h"
#include "usb_descr.h"
#include "usb_dev.h"
ep_t endpoints[STM32ENDPOINTS]; static ep_t endpoints[STM32ENDPOINTS];
usb_dev_t USB_Dev; static uint16_t USB_Addr = 0;
static usb_LineCoding lineCoding = {115200, 0, 0, 8}; static uint8_t setupdatabuf[EP0DATABUF_SIZE];
config_pack_t setup_packet; static config_pack_t *setup_packet = (config_pack_t*) setupdatabuf;
uint8_t ep0databuf[EP0DATABUF_SIZE]; volatile uint8_t usbON = 0; // device is configured and active
uint8_t ep0dbuflen = 0;
usb_LineCoding getLineCoding(){return lineCoding;} static uint16_t configuration = 0; // reply for GET_CONFIGURATION (==1 if configured)
uint8_t usbON = 0; // device disconnected from terminal
// definition of parts common for USB_DeviceDescriptor & USB_DeviceQualifierDescriptor
#define bcdUSB_L 0x10
#define bcdUSB_H 0x01
#define bDeviceClass 0
#define bDeviceSubClass 0
#define bDeviceProtocol 0
#define bNumConfigurations 1
static const uint8_t USB_DeviceDescriptor[] = {
18, // bLength
0x01, // bDescriptorType - Device descriptor
bcdUSB_L, // bcdUSB_L - 1.10
bcdUSB_H, // bcdUSB_H
bDeviceClass, // bDeviceClass - USB_COMM
bDeviceSubClass, // bDeviceSubClass
bDeviceProtocol, // bDeviceProtocol
USB_EP0_BUFSZ, // bMaxPacketSize
0x7b, // idVendor_L PL2303: VID=0x067b, PID=0x2303
0x06, // idVendor_H
0x03, // idProduct_L
0x23, // idProduct_H
0x00, // bcdDevice_Ver_L
0x03, // bcdDevice_Ver_H
0x01, // iManufacturer
0x02, // iProduct
0x00, // iSerialNumber
bNumConfigurations // bNumConfigurations
};
static const uint8_t USB_DeviceQualifierDescriptor[] = {
10, //bLength
0x06, // bDescriptorType - Device qualifier
bcdUSB_L, // bcdUSB_L
bcdUSB_H, // bcdUSB_H
bDeviceClass, // bDeviceClass
bDeviceSubClass, // bDeviceSubClass
bDeviceProtocol, // bDeviceProtocol
USB_EP0_BUFSZ, // bMaxPacketSize0
bNumConfigurations, // bNumConfigurations
0x00 // Reserved
};
static const uint8_t USB_ConfigDescriptor[] = {
/*Configuration Descriptor*/
0x09, /* bLength: Configuration Descriptor size */
0x02, /* bDescriptorType: Configuration */
39, /* wTotalLength:no of returned bytes */
0x00,
0x01, /* bNumInterfaces: 1 interface */
0x01, /* bConfigurationValue: Configuration value */
0x00, /* iConfiguration: Index of string descriptor describing the configuration */
0xa0, /* bmAttributes - Bus powered, Remote wakeup */
0x32, /* MaxPower 100 mA */
/*---------------------------------------------------------------------------*/
/*Interface Descriptor */
0x09, /* bLength: Interface Descriptor size */
0x04, /* bDescriptorType: Interface */
0x00, /* bInterfaceNumber: Number of Interface */
0x00, /* bAlternateSetting: Alternate setting */
0x03, /* bNumEndpoints: 3 endpoints used */
0xff, /* bInterfaceClass */
0x00, /* bInterfaceSubClass */
0x00, /* bInterfaceProtocol */
0x00, /* iInterface: */
///////////////////////////////////////////////////
/*Endpoint 1 Descriptor*/
0x07, /* bLength: Endpoint Descriptor size */
0x05, /* bDescriptorType: Endpoint */
0x81, /* bEndpointAddress IN1 */
0x03, /* bmAttributes: Interrupt */
0x0a, /* wMaxPacketSize LO: */
0x00, /* wMaxPacketSize HI: */
0x01, /* bInterval: */
/*Endpoint OUT2 Descriptor*/
0x07, /* bLength: Endpoint Descriptor size */
0x05, /* bDescriptorType: Endpoint */
0x02, /* bEndpointAddress: OUT2 */
0x02, /* bmAttributes: Bulk */
(USB_RXBUFSZ & 0xff), /* wMaxPacketSize: 64 */
(USB_RXBUFSZ >> 8),
0x00, /* bInterval: ignore for Bulk transfer */
/*Endpoint IN3 Descriptor*/
0x07, /* bLength: Endpoint Descriptor size */
0x05, /* bDescriptorType: Endpoint */
0x83, /* bEndpointAddress IN3 */
0x02, /* bmAttributes: Bulk */
(USB_TXBUFSZ & 0xff), /* wMaxPacketSize: 64 */
(USB_TXBUFSZ >> 8),
0x00, /* bInterval: ignore for Bulk transfer */
};
_USB_LANG_ID_(USB_StringLangDescriptor, LANG_US);
// these descriptors are not used in PL2303 emulator!
_USB_STRING_(USB_StringSerialDescriptor, u"0");
_USB_STRING_(USB_StringManufacturingDescriptor, u"Prolific Technology Inc.");
_USB_STRING_(USB_StringProdDescriptor, u"USB-Serial Controller");
/*
* default handlers
*/
// SET_LINE_CODING
void WEAK linecoding_handler(usb_LineCoding __attribute__((unused)) *lc){
}
// SET_CONTROL_LINE_STATE
void WEAK clstate_handler(uint16_t __attribute__((unused)) val){
}
// SEND_BREAK
void WEAK break_handler(){
}
// handler of vendor requests
void WEAK vendor_handler(config_pack_t *packet){
if(packet->bmRequestType & 0x80){ // read
uint8_t c;
switch(packet->wValue){
case 0x8484:
c = 2;
break;
case 0x0080:
c = 1;
break;
case 0x8686:
c = 0xaa;
break;
default:
c = 0;
}
EP_WriteIRQ(0, &c, 1);
}else{ // write ZLP
EP_WriteIRQ(0, (uint8_t *)0, 0);
}
}
static void wr0(const uint8_t *buf, uint16_t size){
if(setup_packet.wLength < size) size = setup_packet.wLength; // shortened request
if(size < endpoints[0].txbufsz){
EP_WriteIRQ(0, buf, size);
return;
}
while(size){
uint16_t l = size;
if(l > endpoints[0].txbufsz) l = endpoints[0].txbufsz;
EP_WriteIRQ(0, buf, l);
buf += l;
size -= l;
uint8_t needzlp = (l == endpoints[0].txbufsz) ? 1 : 0;
if(size || needzlp){ // send last data buffer
uint16_t status = KEEP_DTOG(USB->EPnR[0]);
// keep DTOGs, clear CTR_RX,TX, set TX VALID, leave stat_Rx
USB->EPnR[0] = (status & ~(USB_EPnR_CTR_RX|USB_EPnR_CTR_TX|USB_EPnR_STAT_RX))
^ USB_EPnR_STAT_TX;
uint32_t ctr = 1000000;
while(--ctr && (USB->ISTR & USB_ISTR_CTR) == 0){IWDG->KR = IWDG_REFRESH;};
if((USB->ISTR & USB_ISTR_CTR) == 0){
return;
}
if(needzlp) EP_WriteIRQ(0, (uint8_t*)0, 0);
}
}
}
static inline void get_descriptor(){
switch(setup_packet.wValue){
case DEVICE_DESCRIPTOR:
wr0(USB_DeviceDescriptor, sizeof(USB_DeviceDescriptor));
break;
case CONFIGURATION_DESCRIPTOR:
wr0(USB_ConfigDescriptor, sizeof(USB_ConfigDescriptor));
break;
case STRING_LANG_DESCRIPTOR:
wr0((const uint8_t *)&USB_StringLangDescriptor, STRING_LANG_DESCRIPTOR_SIZE_BYTE);
break;
case STRING_MAN_DESCRIPTOR:
wr0((const uint8_t *)&USB_StringManufacturingDescriptor, USB_StringManufacturingDescriptor.bLength);
break;
case STRING_PROD_DESCRIPTOR:
wr0((const uint8_t *)&USB_StringProdDescriptor, USB_StringProdDescriptor.bLength);
break;
case STRING_SN_DESCRIPTOR:
wr0((const uint8_t *)&USB_StringSerialDescriptor, USB_StringSerialDescriptor.bLength);
break;
case DEVICE_QUALIFIER_DESCRIPTOR:
wr0(USB_DeviceQualifierDescriptor, USB_DeviceQualifierDescriptor[0]);
break;
default:
break;
}
}
static uint8_t configuration = 0; // reply for GET_CONFIGURATION (==1 if configured)
static inline void std_d2h_req(){ static inline void std_d2h_req(){
uint16_t status = 0; // bus powered uint16_t st = 0;
switch(setup_packet.bRequest){ switch(setup_packet->bRequest){
case GET_DESCRIPTOR: case GET_DESCRIPTOR:
get_descriptor(); get_descriptor(setup_packet);
break; break;
case GET_STATUS: case GET_STATUS:
EP_WriteIRQ(0, (uint8_t *)&status, 2); // send status: Bus Powered EP_WriteIRQ(0, (uint8_t *)&st, 2); // send status: Bus Powered
break; break;
case GET_CONFIGURATION: case GET_CONFIGURATION:
EP_WriteIRQ(0, &configuration, 1); EP_WriteIRQ(0, (uint8_t*)&configuration, 1);
break; break;
default: default:
EP_WriteIRQ(0, NULL, 0);
break; break;
} }
} }
static inline void std_h2d_req(){ static inline void std_h2d_req(){
switch(setup_packet.bRequest){ switch(setup_packet->bRequest){
case SET_ADDRESS: case SET_ADDRESS:
// new address will be assigned later - after acknowlegement or request to host // new address will be assigned later - after acknowlegement or request to host
USB_Dev.USB_Addr = setup_packet.wValue; USB_Addr = setup_packet->wValue;
break; break;
case SET_CONFIGURATION: case SET_CONFIGURATION:
// Now device configured // Now device configured
USB_Dev.USB_Status = USB_STATE_CONFIGURED; configuration = setup_packet->wValue;
configuration = setup_packet.wValue; set_configuration();
usbON = 1;
break; break;
default: default:
break; break;
} }
} }
void WEAK usb_standard_request(){
uint8_t recipient = REQUEST_RECIPIENT(setup_packet->bmRequestType);
uint8_t dev2host = (setup_packet->bmRequestType & 0x80) ? 1 : 0;
switch(recipient){
case REQ_RECIPIENT_DEVICE:
if(dev2host){
std_d2h_req();
}else{
std_h2d_req();
}
break;
case REQ_RECIPIENT_INTERFACE:
if(dev2host && setup_packet->bRequest == GET_DESCRIPTOR){
get_descriptor(setup_packet);
}
break;
case REQ_RECIPIENT_ENDPOINT:
if(setup_packet->bRequest == CLEAR_FEATURE){
}else{
}
break;
default:
break;
}
if(!dev2host) EP_WriteIRQ(0, NULL, 0);
}
void WEAK usb_class_request(config_pack_t *req, uint8_t _U_ *data, uint16_t _U_ datalen){
switch(req->bRequest){
case GET_INTERFACE:
break;
case SET_CONFIGURATION: // set featuring by req->wValue
break;
default:
break;
}
if(0 == (setup_packet->bmRequestType & 0x80)) // host2dev
EP_WriteIRQ(0, NULL, 0);
}
void WEAK usb_vendor_request(config_pack_t _U_ *packet, uint8_t _U_ *data, uint16_t _U_ datalen){
if(0 == (setup_packet->bmRequestType & 0x80)) // host2dev
EP_WriteIRQ(0, NULL, 0);
}
/* /*
bmRequestType: 76543210 bmRequestType: 76543210
7 direction: 0 - host->device, 1 - device->host 7 direction: 0 - host->device, 1 - device->host
@ -269,68 +117,49 @@ bmRequestType: 76543210
/** /**
* Endpoint0 (control) handler * Endpoint0 (control) handler
*/ */
void EP0_Handler(){ static void EP0_Handler(){
uint16_t epstatus = USB->EPnR[0]; // EP0R on input -> return this value after modifications uint8_t ep0dbuflen = 0;
uint8_t reqtype = setup_packet.bmRequestType & 0x7f; uint8_t ep0databuf[EP0DATABUF_SIZE];
uint8_t dev2host = (setup_packet.bmRequestType & 0x80) ? 1 : 0; uint16_t epstatus = KEEP_DTOG(USB->EPnR[0]); // EP0R on input -> return this value after modifications
int rxflag = RX_FLAG(epstatus); int rxflag = RX_FLAG(epstatus);
if(rxflag && SETUP_FLAG(epstatus)){ // check direction
if(USB->ISTR & USB_ISTR_DIR){ // OUT interrupt - receive data, CTR_RX==1 (if CTR_TX == 1 - two pending transactions: receive following by transmit)
if(epstatus & USB_EPnR_SETUP){ // setup packet -> copy data to conf_pack
EP_Read(0, setupdatabuf);
// interrupt handler will be called later
}else if(epstatus & USB_EPnR_CTR_RX){ // data packet -> push received data to ep0databuf
ep0dbuflen = EP_Read(0, ep0databuf);
}
}
if(rxflag){
uint8_t reqtype = REQUEST_TYPE(setup_packet->bmRequestType);
switch(reqtype){ switch(reqtype){
case STANDARD_DEVICE_REQUEST_TYPE: // standard device request case REQ_TYPE_STANDARD:
if(dev2host){ if(SETUP_FLAG(epstatus)){
std_d2h_req(); usb_standard_request();
}else{ }else{
std_h2d_req();
EP_WriteIRQ(0, (uint8_t *)0, 0);
} }
break; break;
case STANDARD_ENDPOINT_REQUEST_TYPE: // standard endpoint request case REQ_TYPE_CLASS:
if(setup_packet.bRequest == CLEAR_FEATURE){ usb_class_request(setup_packet, ep0databuf, ep0dbuflen);
EP_WriteIRQ(0, (uint8_t *)0, 0);
}
break; break;
case VENDOR_REQUEST_TYPE: case REQ_TYPE_VENDOR:
vendor_handler(&setup_packet); usb_vendor_request(setup_packet, ep0databuf, ep0dbuflen);
break;
case CONTROL_REQUEST_TYPE:
switch(setup_packet.bRequest){
case GET_LINE_CODING:
EP_WriteIRQ(0, (uint8_t*)&lineCoding, sizeof(lineCoding));
break;
case SET_LINE_CODING: // omit this for next stage, when data will come
break;
case SET_CONTROL_LINE_STATE:
usbON = 1;
clstate_handler(setup_packet.wValue);
break;
case SEND_BREAK:
usbON = 0;
break_handler();
break; break;
default: default:
EP_WriteIRQ(0, NULL, 0);
break; break;
} }
if(setup_packet.bRequest != GET_LINE_CODING) EP_WriteIRQ(0, (uint8_t *)0, 0); // write acknowledgement
break;
default:
EP_WriteIRQ(0, (uint8_t *)0, 0);
} }
}else if(rxflag){ // got data over EP0 or host acknowlegement if(TX_FLAG(epstatus)){
if(endpoints[0].rx_cnt){
if(setup_packet.bRequest == SET_LINE_CODING){
linecoding_handler((usb_LineCoding*)ep0databuf);
}
}
} else if(TX_FLAG(epstatus)){ // package transmitted
// now we can change address after enumeration // now we can change address after enumeration
if ((USB->DADDR & USB_DADDR_ADD) != USB_Dev.USB_Addr){ if ((USB->DADDR & USB_DADDR_ADD) != USB_Addr){
USB->DADDR = USB_DADDR_EF | USB_Dev.USB_Addr; USB->DADDR = USB_DADDR_EF | USB_Addr;
// change state to ADRESSED usbON = 0;
USB_Dev.USB_Status = USB_STATE_ADDRESSED;
} }
} }
epstatus = KEEP_DTOG(USB->EPnR[0]); //epstatus = KEEP_DTOG(USB->EPnR[0]);
if(rxflag) epstatus ^= USB_EPnR_STAT_TX; // start ZLP/data transmission if(rxflag) epstatus ^= USB_EPnR_STAT_TX; // start ZLP or data transmission
else epstatus &= ~USB_EPnR_STAT_TX; // or leave unchanged else epstatus &= ~USB_EPnR_STAT_TX; // or leave unchanged
// keep DTOGs, clear CTR_RX,TX, set RX VALID // keep DTOGs, clear CTR_RX,TX, set RX VALID
USB->EPnR[0] = (epstatus & ~(USB_EPnR_CTR_RX|USB_EPnR_CTR_TX)) ^ USB_EPnR_STAT_RX; USB->EPnR[0] = (epstatus & ~(USB_EPnR_CTR_RX|USB_EPnR_CTR_TX)) ^ USB_EPnR_STAT_RX;
@ -347,10 +176,20 @@ void EP_WriteIRQ(uint8_t number, const uint8_t *buf, uint16_t size){
uint16_t N2 = (size + 1) >> 1; uint16_t N2 = (size + 1) >> 1;
// the buffer is 16-bit, so we should copy data as it would be uint16_t // the buffer is 16-bit, so we should copy data as it would be uint16_t
uint16_t *buf16 = (uint16_t *)buf; uint16_t *buf16 = (uint16_t *)buf;
#if defined USB1_16
// very bad: what if `size` is odd?
uint32_t *out = (uint32_t *)endpoints[number].tx_buf; uint32_t *out = (uint32_t *)endpoints[number].tx_buf;
for(int i = 0; i < N2; ++i, ++out){ for(int i = 0; i < N2; ++i, ++out){
*out = buf16[i]; *out = buf16[i];
} }
#elif defined USB2_16
// use memcpy instead?
for(int i = 0; i < N2; i++){
endpoints[number].tx_buf[i] = buf16[i];
}
#else
#error "Define USB1_16 or USB2_16"
#endif
USB_BTABLE->EP[number].USB_COUNT_TX = size; USB_BTABLE->EP[number].USB_COUNT_TX = size;
} }
@ -362,9 +201,9 @@ void EP_WriteIRQ(uint8_t number, const uint8_t *buf, uint16_t size){
*/ */
void EP_Write(uint8_t number, const uint8_t *buf, uint16_t size){ void EP_Write(uint8_t number, const uint8_t *buf, uint16_t size){
EP_WriteIRQ(number, buf, size); EP_WriteIRQ(number, buf, size);
uint16_t status = KEEP_DTOG(USB->EPnR[number]); uint16_t epstatus = KEEP_DTOG(USB->EPnR[number]);
// keep DTOGs, clear CTR_TX & set TX VALID to start transmission // keep DTOGs and RX stat, clear CTR_TX & set TX VALID to start transmission
USB->EPnR[number] = (status & ~(USB_EPnR_CTR_TX)) ^ USB_EPnR_STAT_TX; USB->EPnR[number] = (epstatus & ~(USB_EPnR_CTR_TX | USB_EPnR_STAT_RX)) ^ USB_EPnR_STAT_TX;
} }
/* /*
@ -372,16 +211,158 @@ void EP_Write(uint8_t number, const uint8_t *buf, uint16_t size){
* @param *buf - user array for data * @param *buf - user array for data
* @return amount of data read * @return amount of data read
*/ */
int EP_Read(uint8_t number, uint16_t *buf){ int EP_Read(uint8_t number, uint8_t *buf){
int sz = endpoints[number].rx_cnt; int sz = endpoints[number].rx_cnt;
if(!sz) return 0; if(!sz) return 0;
endpoints[number].rx_cnt = 0; endpoints[number].rx_cnt = 0;
#if defined USB1_16
int n = (sz + 1) >> 1; int n = (sz + 1) >> 1;
uint32_t *in = (uint32_t *)endpoints[number].rx_buf; uint32_t *in = (uint32_t*)endpoints[number].rx_buf;
if(n){ uint16_t *out = (uint16_t*)buf;
for(int i = 0; i < n; ++i, ++in) for(int i = 0; i < n; ++i, ++in)
buf[i] = *(uint16_t*)in; out[i] = *(uint16_t*)in;
} #elif defined USB2_16
// use memcpy instead?
for(int i = 0; i < sz; ++i)
buf[i] = endpoints[number].rx_buf[i];
#else
#error "Define USB1_16 or USB2_16"
#endif
return sz; return sz;
} }
static uint16_t lastaddr = LASTADDR_DEFAULT;
/**
* Endpoint initialisation
* @param number - EP num (0...7)
* @param type - EP type (EP_TYPE_BULK, EP_TYPE_CONTROL, EP_TYPE_ISO, EP_TYPE_INTERRUPT)
* @param txsz - transmission buffer size @ USB/CAN buffer
* @param rxsz - reception buffer size @ USB/CAN buffer
* @param uint16_t (*func)(ep_t *ep) - EP handler function
* @return 0 if all OK
*/
int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, void (*func)(ep_t ep)){
if(number >= STM32ENDPOINTS) return 4; // out of configured amount
if(txsz > USB_BTABLE_SIZE/ACCESSZ || rxsz > USB_BTABLE_SIZE/ACCESSZ) return 1; // buffer too large
if(lastaddr + txsz + rxsz >= USB_BTABLE_SIZE/ACCESSZ) return 2; // out of btable
USB->EPnR[number] = (type << 9) | (number & USB_EPnR_EA);
USB->EPnR[number] ^= USB_EPnR_STAT_RX | USB_EPnR_STAT_TX_1;
if(rxsz & 1) return 3; // wrong rx buffer size
uint16_t countrx = 0;
if(rxsz < 64) countrx = rxsz / 2;
else{
if(rxsz & 0x1f) return 3; // should be multiple of 32
countrx = 31 + rxsz / 32;
}
USB_BTABLE->EP[number].USB_ADDR_TX = lastaddr;
endpoints[number].tx_buf = (uint16_t *)(USB_BTABLE_BASE + lastaddr * ACCESSZ);
endpoints[number].txbufsz = txsz;
lastaddr += txsz;
USB_BTABLE->EP[number].USB_COUNT_TX = 0;
USB_BTABLE->EP[number].USB_ADDR_RX = lastaddr;
endpoints[number].rx_buf = (uint8_t *)(USB_BTABLE_BASE + lastaddr * ACCESSZ);
lastaddr += rxsz;
USB_BTABLE->EP[number].USB_COUNT_RX = countrx << 10;
endpoints[number].func = func;
return 0;
}
// standard IRQ handler
void USB_IRQ(){
uint32_t CNTR = USB->CNTR;
USB->CNTR = 0;
if(USB->ISTR & USB_ISTR_RESET){
usbON = 0;
// Reinit registers
CNTR = USB_CNTR_RESETM | USB_CNTR_CTRM | USB_CNTR_SUSPM;
// Endpoint 0 - CONTROL
// ON USB LS size of EP0 may be 8 bytes, but on FS it should be 64 bytes!
lastaddr = LASTADDR_DEFAULT;
// clear address, leave only enable bit
USB->DADDR = USB_DADDR_EF;
USB->ISTR = ~USB_ISTR_RESET;
if(EP_Init(0, EP_TYPE_CONTROL, USB_EP0BUFSZ, USB_EP0BUFSZ, EP0_Handler)){
return;
};
}
if(USB->ISTR & USB_ISTR_CTR){
// EP number
uint8_t n = USB->ISTR & USB_ISTR_EPID;
// copy received bytes amount
endpoints[n].rx_cnt = USB_BTABLE->EP[n].USB_COUNT_RX & 0x3FF; // low 10 bits is counter
// call EP handler
if(endpoints[n].func) endpoints[n].func();
}
if(USB->ISTR & USB_ISTR_WKUP){ // wakeup
#ifndef STM32F0
CNTR &= ~(USB_CNTR_FSUSP | USB_CNTR_LP_MODE | USB_CNTR_WKUPM); // clear suspend flags
#else
CNTR &= ~(USB_CNTR_FSUSP | USB_CNTR_LPMODE | USB_CNTR_WKUPM);
#endif
USB->ISTR = ~USB_ISTR_WKUP;
}
if(USB->ISTR & USB_ISTR_SUSP){ // suspend -> still no connection, may sleep
usbON = 0;
#ifndef STM32F0
CNTR |= USB_CNTR_FSUSP | USB_CNTR_LP_MODE | USB_CNTR_WKUPM;
#else
CNTR |= USB_CNTR_FSUSP | USB_CNTR_LPMODE | USB_CNTR_WKUPM;
#endif
CNTR &= ~(USB_CNTR_SUSPM);
USB->ISTR = ~USB_ISTR_SUSP;
}
USB->CNTR = CNTR; // rewoke interrupts
}
// here we suppose that all PIN settings done in hw_setup earlier
void USB_setup(){
#if defined STM32F3
NVIC_DisableIRQ(USB_LP_IRQn);
// remap USB LP & Wakeup interrupts to 75 and 76 - works only on pure F303
RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN; // enable tacting of SYSCFG
SYSCFG->CFGR1 |= SYSCFG_CFGR1_USB_IT_RMP;
#elif defined STM32F1
NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn);
NVIC_DisableIRQ(USB_HP_CAN1_TX_IRQn);
#elif defined STM32F0
NVIC_DisableIRQ(USB_IRQn);
RCC->APB1ENR |= RCC_APB1ENR_CRSEN;
RCC->CFGR3 &= ~RCC_CFGR3_USBSW; // reset USB
RCC->CR2 |= RCC_CR2_HSI48ON; // turn ON HSI48
uint32_t tmout = 16000000;
while(!(RCC->CR2 & RCC_CR2_HSI48RDY)){if(--tmout == 0) break;}
FLASH->ACR = FLASH_ACR_PRFTBE | FLASH_ACR_LATENCY;
CRS->CFGR &= ~CRS_CFGR_SYNCSRC;
CRS->CFGR |= CRS_CFGR_SYNCSRC_1; // USB SOF selected as sync source
CRS->CR |= CRS_CR_AUTOTRIMEN; // enable auto trim
CRS->CR |= CRS_CR_CEN; // enable freq counter & block CRS->CFGR as read-only
RCC->CFGR |= RCC_CFGR_SW;
#endif
RCC->APB1ENR |= RCC_APB1ENR_USBEN;
//??
USB->CNTR = USB_CNTR_FRES; // Force USB Reset
for(uint32_t ctr = 0; ctr < 72000; ++ctr) nop(); // wait >1ms
USB->CNTR = 0;
USB->BTABLE = 0;
USB->DADDR = 0;
USB->ISTR = 0;
USB->CNTR = USB_CNTR_RESETM; // allow only reset interrupts
#if defined STM32F3
NVIC_EnableIRQ(USB_LP_IRQn);
#elif defined STM32F1
NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
#elif defined STM32F0
USB->BCDR |= USB_BCDR_DPPU;
NVIC_EnableIRQ(USB_IRQn);
#endif
}
#if defined STM32F3
void usb_lp_isr() __attribute__ ((alias ("USB_IRQ")));
#elif defined STM32F1
void usb_lp_can_rx0_isr() __attribute__ ((alias ("USB_IRQ")));
#elif defined STM32F0
void usb_isr() __attribute__ ((alias ("USB_IRQ")));
#endif

View File

@ -1,6 +1,5 @@
/* /*
* This file is part of the MLX90640 project. * Copyright 2024 Edward V. Emelianov <edward.emelianoff@gmail.com>.
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
* *
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@ -15,61 +14,253 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#pragma once #pragma once
#ifndef __USB_LIB_H__
#define __USB_LIB_H__
#include <stdint.h>
#include <wchar.h> #include <wchar.h>
#include "usbhw.h"
/******************************************************************
* Hardware registers etc *
*****************************************************************/
#if defined STM32F0
#include <stm32f0.h>
#elif defined STM32F1
#include <stm32f1.h>
// there's no this define in standard header
#define USB_BASE ((uint32_t)0x40005C00)
#elif defined STM32F3
#include <stm32f3.h>
#endif
// max endpoints number
#define STM32ENDPOINTS 8
/**
* Buffers size definition
**/
// F0 - USB2_16; F1 - USB1_16; F3 - 1/2 depending on series
#if !defined USB1_16 && !defined USB2_16
#if defined STM32F0
#define USB2_16
#elif defined STM32F1
#define USB1_16
#else
#error "Can't determine USB1_16 or USB2_16, define by hands"
#endif
#endif
// BTABLE_SIZE FOR STM32F3:
// In STM32F303/302xB/C, 512 bytes SRAM is not shared with CAN.
// In STM32F302x6/x8 and STM32F30xxD/E, 726 bytes dedicated SRAM and 256 bytes shared SRAM with CAN i.e.
// 1Kbytes dedicated SRAM in case CAN is disabled.
// remember, that USB_BTABLE_SIZE will be divided by ACCESSZ, so don't divide it twice for 32-bit addressing
#ifdef NOCAN
#if defined STM32F0
#define USB_BTABLE_SIZE 1024
#elif defined STM32F3
#define USB_BTABLE_SIZE 512
#warning "Please, check real buffer size due to docs"
#else
#error "define STM32F0 or STM32F3"
#endif
#else // !NOCAN: F0/F3 with CAN or F1 (can't simultaneously run CAN and USB)
#if defined STM32F0
#define USB_BTABLE_SIZE 768
#elif defined STM32F3
#define USB_BTABLE_SIZE 512
#warning "Please, check real buffer size due to docs"
#else // STM32F103: 1024 bytes but with 32-bit addressing
#define USB_BTABLE_SIZE 1024
#endif
#endif // NOCAN
// first 64 bytes of USB_BTABLE are registers!
#define USB_BTABLE_BASE 0x40006000
#define USB ((USB_TypeDef *) USB_BASE)
#ifdef USB_BTABLE
#undef USB_BTABLE
#endif
#define USB_BTABLE ((USB_BtableDef *)(USB_BTABLE_BASE))
#define USB_ISTR_EPID 0x0000000F
#define USB_FNR_LSOF_0 0x00000800
#define USB_FNR_lSOF_1 0x00001000
#define USB_LPMCSR_BESL_0 0x00000010
#define USB_LPMCSR_BESL_1 0x00000020
#define USB_LPMCSR_BESL_2 0x00000040
#define USB_LPMCSR_BESL_3 0x00000080
#define USB_EPnR_CTR_RX 0x00008000
#define USB_EPnR_DTOG_RX 0x00004000
#define USB_EPnR_STAT_RX 0x00003000
#define USB_EPnR_STAT_RX_0 0x00001000
#define USB_EPnR_STAT_RX_1 0x00002000
#define USB_EPnR_SETUP 0x00000800
#define USB_EPnR_EP_TYPE 0x00000600
#define USB_EPnR_EP_TYPE_0 0x00000200
#define USB_EPnR_EP_TYPE_1 0x00000400
#define USB_EPnR_EP_KIND 0x00000100
#define USB_EPnR_CTR_TX 0x00000080
#define USB_EPnR_DTOG_TX 0x00000040
#define USB_EPnR_STAT_TX 0x00000030
#define USB_EPnR_STAT_TX_0 0x00000010
#define USB_EPnR_STAT_TX_1 0x00000020
#define USB_EPnR_EA 0x0000000F
#define USB_COUNTn_RX_BLSIZE 0x00008000
#define USB_COUNTn_NUM_BLOCK 0x00007C00
#define USB_COUNTn_RX 0x0000003F
#define USB_TypeDef USB_TypeDef_custom
typedef struct {
__IO uint32_t EPnR[STM32ENDPOINTS];
__IO uint32_t RESERVED[STM32ENDPOINTS];
__IO uint32_t CNTR;
__IO uint32_t ISTR;
__IO uint32_t FNR;
__IO uint32_t DADDR;
__IO uint32_t BTABLE;
#ifdef STM32F0
__IO uint32_t LPMCSR;
__IO uint32_t BCDR;
#endif
} USB_TypeDef;
// F303 D/E have 2x16 access scheme
typedef struct{
#if defined USB2_16
__IO uint16_t USB_ADDR_TX;
__IO uint16_t USB_COUNT_TX;
__IO uint16_t USB_ADDR_RX;
__IO uint16_t USB_COUNT_RX;
#define ACCESSZ (1)
#define BUFTYPE uint8_t
#elif defined USB1_16
__IO uint32_t USB_ADDR_TX;
__IO uint32_t USB_COUNT_TX;
__IO uint32_t USB_ADDR_RX;
__IO uint32_t USB_COUNT_RX;
#define ACCESSZ (2)
#define BUFTYPE uint16_t
#else
#error "Define USB1_16 or USB2_16"
#endif
} USB_EPDATA_TypeDef;
typedef struct{
__IO USB_EPDATA_TypeDef EP[STM32ENDPOINTS];
} USB_BtableDef;
#define EP0DATABUF_SIZE (64) #define EP0DATABUF_SIZE (64)
#define LASTADDR_DEFAULT (STM32ENDPOINTS * 8) #define LASTADDR_DEFAULT (STM32ENDPOINTS * 8)
// bmRequestType & 0x7f /******************************************************************
#define STANDARD_DEVICE_REQUEST_TYPE 0 * Defines from usb.h *
#define STANDARD_ENDPOINT_REQUEST_TYPE 2 *****************************************************************/
#define VENDOR_REQUEST_TYPE 0x40
#define CONTROL_REQUEST_TYPE 0x21 /*
// bRequest, standard; for bmRequestType == 0x80 * Device and/or Interface Class codes
*/
#define USB_CLASS_PER_INTERFACE 0
#define USB_CLASS_AUDIO 1
#define USB_CLASS_COMM 2
#define USB_CLASS_HID 3
#define USB_CLASS_PRINTER 7
#define USB_CLASS_PTP 6
#define USB_CLASS_MASS_STORAGE 8
#define USB_CLASS_HUB 9
#define USB_CLASS_DATA 10
#define USB_CLASS_MISC 0xef
#define USB_CLASS_VENDOR_SPEC 0xff
/*
* Descriptor types
*/
#define USB_DT_DEVICE 0x01
#define USB_DT_CONFIG 0x02
#define USB_DT_STRING 0x03
#define USB_DT_INTERFACE 0x04
#define USB_DT_ENDPOINT 0x05
#define USB_DT_QUALIFIER 0x06
#define USB_DT_IAD 0x0B
#define USB_DT_HID 0x21
#define USB_DT_REPORT 0x22
#define USB_DT_PHYSICAL 0x23
#define USB_DT_CS_INTERFACE 0x24
#define USB_DT_HUB 0x29
/*
* Descriptor sizes per descriptor type
*/
#define USB_DT_DEVICE_SIZE 18
#define USB_DT_CONFIG_SIZE 9
#define USB_DT_INTERFACE_SIZE 9
#define USB_DT_HID_SIZE 9
#define USB_DT_ENDPOINT_SIZE 7
#define USB_DT_QUALIFIER_SIZE 10
#define USB_DT_CS_INTERFACE_SIZE 5
#define USB_DT_IAD_SIZE 8
// bmRequestType & 0x80 == dev2host (1) or host2dev (0)
// recipient: bmRequestType & 0x1f
#define REQUEST_RECIPIENT(b) (b & 0x1f)
#define REQ_RECIPIENT_DEVICE 0
#define REQ_RECIPIENT_INTERFACE 1
#define REQ_RECIPIENT_ENDPOINT 2
#define REQ_RECIPIENT_OTHER 3
// type: [bmRequestType & 0x60 >> 5]
#define REQUEST_TYPE(b) ((b&0x60)>>5)
#define REQ_TYPE_STANDARD 0
#define REQ_TYPE_CLASS 1
#define REQ_TYPE_VENDOR 2
#define REQ_TYPE_RESERVED 3
//#define VENDOR_REQUEST 0x01
// standard device requests
#define GET_STATUS 0x00 #define GET_STATUS 0x00
#define GET_DESCRIPTOR 0x06
#define GET_CONFIGURATION 0x08
// for bmRequestType == 0
#define CLEAR_FEATURE 0x01 #define CLEAR_FEATURE 0x01
#define SET_FEATURE 0x03 // unused #define SET_FEATURE 0x03
#define SET_ADDRESS 0x05 #define SET_ADDRESS 0x05
#define SET_DESCRIPTOR 0x07 // unused #define GET_DESCRIPTOR 0x06
#define SET_DESCRIPTOR 0x07
#define GET_CONFIGURATION 0x08
#define SET_CONFIGURATION 0x09 #define SET_CONFIGURATION 0x09
// for bmRequestType == 0x81, 1 or 0xB2 // and some standard interface requests
#define GET_INTERFACE 0x0A // unused #define GET_INTERFACE 0x0A
#define SET_INTERFACE 0x0B // unused #define SET_INTERFACE 0x0B
#define SYNC_FRAME 0x0C // unused // and some standard endpoint requests
#define VENDOR_REQUEST 0x01 // unused #define SYNC_FRAME 0x0C
// Class-Specific Control Requests // Types of descriptors
#define SEND_ENCAPSULATED_COMMAND 0x00 // unused #define DEVICE_DESCRIPTOR 0x01
#define GET_ENCAPSULATED_RESPONSE 0x01 // unused #define CONFIGURATION_DESCRIPTOR 0x02
#define SET_COMM_FEATURE 0x02 // unused #define STRING_DESCRIPTOR 0x03
#define GET_COMM_FEATURE 0x03 // unused #define DEVICE_QUALIFIER_DESCRIPTOR 0x06
#define CLEAR_COMM_FEATURE 0x04 // unused #define DEBUG_DESCRIPTOR 0x0a
#define SET_LINE_CODING 0x20 #define HID_REPORT_DESCRIPTOR 0x22
#define GET_LINE_CODING 0x21
#define SET_CONTROL_LINE_STATE 0x22
#define SEND_BREAK 0x23
// control line states // EP types for EP_init
#define CONTROL_DTR 0x01 #define EP_TYPE_BULK 0x00
#define CONTROL_RTS 0x02 #define EP_TYPE_CONTROL 0x01
#define EP_TYPE_ISO 0x02
#define EP_TYPE_INTERRUPT 0x03
// wValue // EP types for descriptors
#define DEVICE_DESCRIPTOR 0x100 #define USB_BM_ATTR_CONTROL 0x00
#define CONFIGURATION_DESCRIPTOR 0x200 #define USB_BM_ATTR_ISO 0x01
#define STRING_LANG_DESCRIPTOR 0x300 #define USB_BM_ATTR_BULK 0x02
#define STRING_MAN_DESCRIPTOR 0x301 #define USB_BM_ATTR_INTERRUPT 0x03
#define STRING_PROD_DESCRIPTOR 0x302
#define STRING_SN_DESCRIPTOR 0x303
#define DEVICE_QUALIFIER_DESCRIPTOR 0x600 /******************************************************************
* Other stuff *
*****************************************************************/
#define RX_FLAG(epstat) (epstat & USB_EPnR_CTR_RX) #define RX_FLAG(epstat) (epstat & USB_EPnR_CTR_RX)
#define TX_FLAG(epstat) (epstat & USB_EPnR_CTR_TX) #define TX_FLAG(epstat) (epstat & USB_EPnR_CTR_TX)
@ -79,20 +270,6 @@
#define KEEP_DTOG_STAT(EPnR) (EPnR & ~(USB_EPnR_STAT_RX|USB_EPnR_STAT_TX|USB_EPnR_DTOG_RX|USB_EPnR_DTOG_TX)) #define KEEP_DTOG_STAT(EPnR) (EPnR & ~(USB_EPnR_STAT_RX|USB_EPnR_STAT_TX|USB_EPnR_DTOG_RX|USB_EPnR_DTOG_TX))
#define KEEP_DTOG(EPnR) (EPnR & ~(USB_EPnR_DTOG_RX|USB_EPnR_DTOG_TX)) #define KEEP_DTOG(EPnR) (EPnR & ~(USB_EPnR_DTOG_RX|USB_EPnR_DTOG_TX))
// USB state: uninitialized, addressed, ready for use
typedef enum{
USB_STATE_DEFAULT,
USB_STATE_ADDRESSED,
USB_STATE_CONFIGURED,
USB_STATE_CONNECTED
} USB_state;
// EP types
#define EP_TYPE_BULK 0x00
#define EP_TYPE_CONTROL 0x01
#define EP_TYPE_ISO 0x02
#define EP_TYPE_INTERRUPT 0x03
#define LANG_US (uint16_t)0x0409 #define LANG_US (uint16_t)0x0409
#define _USB_STRING_(name, str) \ #define _USB_STRING_(name, str) \
@ -106,7 +283,6 @@ static const struct name \
name = {sizeof(name), 0x03, str} name = {sizeof(name), 0x03, str}
#define _USB_LANG_ID_(name, lng_id) \ #define _USB_LANG_ID_(name, lng_id) \
\
static const struct name \ static const struct name \
{ \ { \
uint8_t bLength; \ uint8_t bLength; \
@ -115,7 +291,6 @@ static const struct name \
\ \
} \ } \
name = {0x04, 0x03, lng_id} name = {0x04, 0x03, lng_id}
#define STRING_LANG_DESCRIPTOR_SIZE_BYTE (4)
// EP0 configuration packet // EP0 configuration packet
typedef struct { typedef struct {
@ -130,57 +305,20 @@ typedef struct {
typedef struct{ typedef struct{
uint16_t *tx_buf; // transmission buffer address uint16_t *tx_buf; // transmission buffer address
uint16_t txbufsz; // transmission buffer size uint16_t txbufsz; // transmission buffer size
uint16_t *rx_buf; // reception buffer address uint8_t *rx_buf; // reception buffer address
void (*func)(); // endpoint action function void (*func)(); // endpoint action function
unsigned rx_cnt : 10; // received data counter unsigned rx_cnt : 10; // received data counter
} ep_t; } ep_t;
// USB status & its address extern volatile uint8_t usbON;
typedef struct {
uint8_t USB_Status;
uint16_t USB_Addr;
}usb_dev_t;
typedef struct {
uint32_t dwDTERate;
uint8_t bCharFormat;
#define USB_CDC_1_STOP_BITS 0
#define USB_CDC_1_5_STOP_BITS 1
#define USB_CDC_2_STOP_BITS 2
uint8_t bParityType;
#define USB_CDC_NO_PARITY 0
#define USB_CDC_ODD_PARITY 1
#define USB_CDC_EVEN_PARITY 2
#define USB_CDC_MARK_PARITY 3
#define USB_CDC_SPACE_PARITY 4
uint8_t bDataBits;
} __attribute__ ((packed)) usb_LineCoding;
typedef struct {
uint8_t bmRequestType;
uint8_t bNotificationType;
uint16_t wValue;
uint16_t wIndex;
uint16_t wLength;
} __attribute__ ((packed)) usb_cdc_notification;
extern ep_t endpoints[];
extern usb_dev_t USB_Dev;
extern uint8_t usbON;
extern config_pack_t setup_packet;
extern uint8_t ep0databuf[];
extern uint8_t ep0dbuflen;
void EP0_Handler();
void USB_setup();
int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, void (*func)());
void EP_WriteIRQ(uint8_t number, const uint8_t *buf, uint16_t size); void EP_WriteIRQ(uint8_t number, const uint8_t *buf, uint16_t size);
void EP_Write(uint8_t number, const uint8_t *buf, uint16_t size); void EP_Write(uint8_t number, const uint8_t *buf, uint16_t size);
int EP_Read(uint8_t number, uint16_t *buf); int EP_Read(uint8_t number, uint8_t *buf);
usb_LineCoding getLineCoding();
void linecoding_handler(usb_LineCoding *lc); // could be [re]defined in usb_dev.c
void clstate_handler(uint16_t val); extern void usb_class_request(config_pack_t *packet, uint8_t *data, uint16_t datalen);
void break_handler(); extern void usb_vendor_request(config_pack_t *packet, uint8_t *data, uint16_t datalen);
void vendor_handler(config_pack_t *packet); extern void set_configuration();
#endif // __USB_LIB_H__

View File

@ -1,129 +0,0 @@
/*
* This file is part of the MLX90640 project.
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "hardware.h"
#include "usb.h"
#include "usbhw.h"
#include "usb_lib.h"
void USB_setup(){
NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn);
NVIC_DisableIRQ(USB_HP_CAN1_TX_IRQn);
USBPU_OFF();
RCC->APB1ENR |= RCC_APB1ENR_USBEN;
RCC->APB2ENR |= USB_RCC;
USB->CNTR = USB_CNTR_FRES; // Force USB Reset
for(uint32_t ctr = 0; ctr < 72000; ++ctr) nop(); // wait >1ms
//uint32_t ctr = 0;
USB->CNTR = 0;
USB->BTABLE = 0;
USB->DADDR = 0;
USB->ISTR = 0;
USB->CNTR = USB_CNTR_RESETM | USB_CNTR_WKUPM; // allow only wakeup & reset interrupts
NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
USBPU_ON();
}
static uint16_t lastaddr = LASTADDR_DEFAULT;
/**
* Endpoint initialisation
* @param number - EP num (0...7)
* @param type - EP type (EP_TYPE_BULK, EP_TYPE_CONTROL, EP_TYPE_ISO, EP_TYPE_INTERRUPT)
* @param txsz - transmission buffer size @ USB/CAN buffer
* @param rxsz - reception buffer size @ USB/CAN buffer
* @param uint16_t (*func)(ep_t *ep) - EP handler function
* @return 0 if all OK
*/
int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, void (*func)(ep_t ep)){
if(number >= STM32ENDPOINTS) return 4; // out of configured amount
if(txsz > USB_BTABLE_SIZE || rxsz > USB_BTABLE_SIZE) return 1; // buffer too large
if(lastaddr + txsz + rxsz >= USB_BTABLE_SIZE) return 2; // out of btable
USB->EPnR[number] = (type << 9) | (number & USB_EPnR_EA);
USB->EPnR[number] ^= USB_EPnR_STAT_RX | USB_EPnR_STAT_TX_1;
if(rxsz & 1 || rxsz > 512) return 3; // wrong rx buffer size
uint16_t countrx = 0;
if(rxsz < 64) countrx = rxsz / 2;
else{
if(rxsz & 0x1f) return 3; // should be multiple of 32
countrx = 31 + rxsz / 32;
}
USB_BTABLE->EP[number].USB_ADDR_TX = lastaddr;
endpoints[number].tx_buf = (uint16_t *)(USB_BTABLE_BASE + lastaddr*2);
endpoints[number].txbufsz = txsz;
lastaddr += txsz;
USB_BTABLE->EP[number].USB_COUNT_TX = 0;
USB_BTABLE->EP[number].USB_ADDR_RX = lastaddr;
endpoints[number].rx_buf = (uint16_t *)(USB_BTABLE_BASE + lastaddr*2);
lastaddr += rxsz;
USB_BTABLE->EP[number].USB_COUNT_RX = countrx << 10;
endpoints[number].func = func;
return 0;
}
// standard IRQ handler
void usb_lp_can_rx0_isr(){
if(USB->ISTR & USB_ISTR_RESET){
usbON = 0;
// Reinit registers
USB->CNTR = USB_CNTR_RESETM | USB_CNTR_CTRM | USB_CNTR_SUSPM | USB_CNTR_WKUPM;
USB->ISTR = 0;
// Endpoint 0 - CONTROL
// ON USB LS size of EP0 may be 8 bytes, but on FS it should be 64 bytes!
lastaddr = LASTADDR_DEFAULT;
// clear address, leave only enable bit
USB->DADDR = USB_DADDR_EF;
USB_Dev.USB_Status = USB_STATE_DEFAULT;
USB->ISTR = ~USB_ISTR_RESET;
if(EP_Init(0, EP_TYPE_CONTROL, USB_EP0_BUFSZ, USB_EP0_BUFSZ, EP0_Handler)){
return;
}
}
if(USB->ISTR & USB_ISTR_CTR){
// EP number
uint8_t n = USB->ISTR & USB_ISTR_EPID;
// copy status register
uint16_t epstatus = USB->EPnR[n];
// copy received bytes amount
endpoints[n].rx_cnt = USB_BTABLE->EP[n].USB_COUNT_RX & 0x3FF; // low 10 bits is counter
// check direction
if(USB->ISTR & USB_ISTR_DIR){ // OUT interrupt - receive data, CTR_RX==1 (if CTR_TX == 1 - two pending transactions: receive following by transmit)
if(n == 0){ // control endpoint
if(epstatus & USB_EPnR_SETUP){ // setup packet -> copy data to conf_pack
EP_Read(0, (uint16_t*)&setup_packet);
ep0dbuflen = 0;
// interrupt handler will be called later
}else if(epstatus & USB_EPnR_CTR_RX){ // data packet -> push received data to ep0databuf
ep0dbuflen = endpoints[0].rx_cnt;
EP_Read(0, (uint16_t*)&ep0databuf);
}
}
}
// call EP handler
if(endpoints[n].func) endpoints[n].func(endpoints[n]);
}
if(USB->ISTR & USB_ISTR_SUSP){ // suspend -> still no connection, may sleep
usbON = 0;
USB->CNTR |= USB_CNTR_FSUSP | USB_CNTR_LP_MODE;
USB->ISTR = ~USB_ISTR_SUSP;
}
if(USB->ISTR & USB_ISTR_WKUP){ // wakeup
USB->CNTR &= ~(USB_CNTR_FSUSP | USB_CNTR_LP_MODE); // clear suspend flags
USB->ISTR = ~USB_ISTR_WKUP;
}
}

View File

@ -1,105 +0,0 @@
/*
* This file is part of the MLX90640 project.
* Copyright 2022 Edward V. Emelianov <edward.emelianoff@gmail.com>.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#ifndef USBHW_H__
#define USBHW_H__
#include <stm32f1.h>
#define USB_RCC RCC_APB2ENR_IOPAEN
// max endpoints number
#define STM32ENDPOINTS 8
/**
* Buffers size definition
**/
#define USB_BTABLE_SIZE 512
// first 64 bytes of USB_BTABLE are registers!
//#define USB_EP0_BASEADDR 64
// for USB FS EP0 buffers are from 8 to 64 bytes long (64 for PL2303)
#define USB_EP0_BUFSZ 64
// USB transmit buffer size (64 for PL2303)
#define USB_TXBUFSZ 64
// USB receive buffer size (64 for PL2303)
#define USB_RXBUFSZ 64
// EP1 - interrupt - buffer size
#define USB_EP1BUFSZ 8
#define USB_BTABLE_BASE 0x40006000
#define USB_BASE ((uint32_t)0x40005C00)
#define USB ((USB_TypeDef *) USB_BASE)
#ifdef USB_BTABLE
#undef USB_BTABLE
#endif
#define USB_BTABLE ((USB_BtableDef *)(USB_BTABLE_BASE))
#define USB_ISTR_EPID 0x0000000F
#define USB_FNR_LSOF_0 0x00000800
#define USB_FNR_lSOF_1 0x00001000
#define USB_LPMCSR_BESL_0 0x00000010
#define USB_LPMCSR_BESL_1 0x00000020
#define USB_LPMCSR_BESL_2 0x00000040
#define USB_LPMCSR_BESL_3 0x00000080
#define USB_EPnR_CTR_RX 0x00008000
#define USB_EPnR_DTOG_RX 0x00004000
#define USB_EPnR_STAT_RX 0x00003000
#define USB_EPnR_STAT_RX_0 0x00001000
#define USB_EPnR_STAT_RX_1 0x00002000
#define USB_EPnR_SETUP 0x00000800
#define USB_EPnR_EP_TYPE 0x00000600
#define USB_EPnR_EP_TYPE_0 0x00000200
#define USB_EPnR_EP_TYPE_1 0x00000400
#define USB_EPnR_EP_KIND 0x00000100
#define USB_EPnR_CTR_TX 0x00000080
#define USB_EPnR_DTOG_TX 0x00000040
#define USB_EPnR_STAT_TX 0x00000030
#define USB_EPnR_STAT_TX_0 0x00000010
#define USB_EPnR_STAT_TX_1 0x00000020
#define USB_EPnR_EA 0x0000000F
#define USB_COUNTn_RX_BLSIZE 0x00008000
#define USB_COUNTn_NUM_BLOCK 0x00007C00
#define USB_COUNTn_RX 0x0000003F
#define USB_TypeDef USB_TypeDef_custom
typedef struct {
__IO uint32_t EPnR[STM32ENDPOINTS];
__IO uint32_t RESERVED[STM32ENDPOINTS];
__IO uint32_t CNTR;
__IO uint32_t ISTR;
__IO uint32_t FNR;
__IO uint32_t DADDR;
__IO uint32_t BTABLE;
} USB_TypeDef;
typedef struct{
__IO uint32_t USB_ADDR_TX;
__IO uint32_t USB_COUNT_TX;
__IO uint32_t USB_ADDR_RX;
__IO uint32_t USB_COUNT_RX;
} USB_EPDATA_TypeDef;
typedef struct{
__IO USB_EPDATA_TypeDef EP[STM32ENDPOINTS];
} USB_BtableDef;
void USB_setup();
int EP_Init(uint8_t number, uint8_t type, uint16_t txsz, uint16_t rxsz, void (*func)());
#endif // USBHW_H__

View File

@ -1,2 +1,2 @@
#define BUILD_NUMBER "103" #define BUILD_NUMBER "112"
#define BUILD_DATE "2023-03-17" #define BUILD_DATE "2025-03-11"

View File

@ -41,7 +41,6 @@
11, 11,
12, 12,
13, 13,
14,
15, 15,
16, 16,
17, 17,

View File

@ -550,6 +550,7 @@
}, },
"schematic": { "schematic": {
"annotate_start_num": 0, "annotate_start_num": 0,
"bom_export_filename": "",
"bom_fmt_presets": [], "bom_fmt_presets": [],
"bom_fmt_settings": { "bom_fmt_settings": {
"field_delimiter": ",", "field_delimiter": ",",