mirror of
https://github.com/switchbrew/switch-examples.git
synced 2025-06-20 21:12:38 +02:00
Added usbdev example. Added comments to usbds example.
This commit is contained in:
parent
33f726fadf
commit
7e83f99fd6
138
usb/usbdev/Makefile
Normal file
138
usb/usbdev/Makefile
Normal file
@ -0,0 +1,138 @@
|
|||||||
|
#---------------------------------------------------------------------------------
|
||||||
|
.SUFFIXES:
|
||||||
|
#---------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
ifeq ($(strip $(DEVKITA64)),)
|
||||||
|
$(error "Please set DEVKITA64 in your environment. export DEVKITA64=<path to>DEVKITA64")
|
||||||
|
endif
|
||||||
|
|
||||||
|
TOPDIR ?= $(CURDIR)
|
||||||
|
include $(DEVKITA64)/switch_rules
|
||||||
|
|
||||||
|
#---------------------------------------------------------------------------------
|
||||||
|
# TARGET is the name of the output
|
||||||
|
# BUILD is the directory where object files & intermediate files will be placed
|
||||||
|
# SOURCES is a list of directories containing source code
|
||||||
|
# DATA is a list of directories containing data files
|
||||||
|
# INCLUDES is a list of directories containing header files
|
||||||
|
# EXEFS_SRC is the input directory containing data copied into exefs, normally should only contain "main.npdm".
|
||||||
|
#---------------------------------------------------------------------------------
|
||||||
|
TARGET := $(notdir $(CURDIR))
|
||||||
|
BUILD := build
|
||||||
|
SOURCES := source
|
||||||
|
DATA := data
|
||||||
|
INCLUDES := include
|
||||||
|
EXEFS_SRC := exefs_src
|
||||||
|
|
||||||
|
#---------------------------------------------------------------------------------
|
||||||
|
# options for code generation
|
||||||
|
#---------------------------------------------------------------------------------
|
||||||
|
ARCH := -march=armv8-a -fPIE
|
||||||
|
|
||||||
|
CFLAGS := -g -Wall -O2 \
|
||||||
|
-ffast-math \
|
||||||
|
$(ARCH) $(DEFINES)
|
||||||
|
|
||||||
|
CFLAGS += $(INCLUDE)
|
||||||
|
|
||||||
|
CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu++11
|
||||||
|
|
||||||
|
ASFLAGS := -g $(ARCH)
|
||||||
|
LDFLAGS = -specs=switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map)
|
||||||
|
|
||||||
|
LIBS := -lnx
|
||||||
|
|
||||||
|
#---------------------------------------------------------------------------------
|
||||||
|
# list of directories containing libraries, this must be the top level containing
|
||||||
|
# include and lib
|
||||||
|
#---------------------------------------------------------------------------------
|
||||||
|
LIBDIRS := $(PORTLIBS) $(LIBNX)
|
||||||
|
|
||||||
|
|
||||||
|
#---------------------------------------------------------------------------------
|
||||||
|
# no real need to edit anything past this point unless you need to add additional
|
||||||
|
# rules for different file extensions
|
||||||
|
#---------------------------------------------------------------------------------
|
||||||
|
ifneq ($(BUILD),$(notdir $(CURDIR)))
|
||||||
|
#---------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
export OUTPUT := $(CURDIR)/$(TARGET)
|
||||||
|
export TOPDIR := $(CURDIR)
|
||||||
|
|
||||||
|
export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
|
||||||
|
$(foreach dir,$(DATA),$(CURDIR)/$(dir))
|
||||||
|
|
||||||
|
export DEPSDIR := $(CURDIR)/$(BUILD)
|
||||||
|
|
||||||
|
CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
|
||||||
|
CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
|
||||||
|
SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
|
||||||
|
BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))
|
||||||
|
|
||||||
|
#---------------------------------------------------------------------------------
|
||||||
|
# use CXX for linking C++ projects, CC for standard C
|
||||||
|
#---------------------------------------------------------------------------------
|
||||||
|
ifeq ($(strip $(CPPFILES)),)
|
||||||
|
#---------------------------------------------------------------------------------
|
||||||
|
export LD := $(CC)
|
||||||
|
#---------------------------------------------------------------------------------
|
||||||
|
else
|
||||||
|
#---------------------------------------------------------------------------------
|
||||||
|
export LD := $(CXX)
|
||||||
|
#---------------------------------------------------------------------------------
|
||||||
|
endif
|
||||||
|
#---------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
export OFILES := $(addsuffix .o,$(BINFILES)) \
|
||||||
|
$(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
|
||||||
|
|
||||||
|
export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
|
||||||
|
$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
|
||||||
|
-I$(CURDIR)/$(BUILD)
|
||||||
|
|
||||||
|
export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
|
||||||
|
|
||||||
|
export BUILD_EXEFS_SRC := $(TOPDIR)/$(EXEFS_SRC)
|
||||||
|
|
||||||
|
.PHONY: $(BUILD) clean all
|
||||||
|
|
||||||
|
#---------------------------------------------------------------------------------
|
||||||
|
all: $(BUILD)
|
||||||
|
|
||||||
|
$(BUILD):
|
||||||
|
@[ -d $@ ] || mkdir -p $@
|
||||||
|
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
|
||||||
|
|
||||||
|
#---------------------------------------------------------------------------------
|
||||||
|
clean:
|
||||||
|
@echo clean ...
|
||||||
|
@rm -fr $(BUILD) $(TARGET).pfs0 $(TARGET).nso $(TARGET).elf
|
||||||
|
|
||||||
|
|
||||||
|
#---------------------------------------------------------------------------------
|
||||||
|
else
|
||||||
|
|
||||||
|
DEPENDS := $(OFILES:.o=.d)
|
||||||
|
|
||||||
|
#---------------------------------------------------------------------------------
|
||||||
|
# main targets
|
||||||
|
#---------------------------------------------------------------------------------
|
||||||
|
$(OUTPUT).pfs0 : $(OUTPUT).nso
|
||||||
|
|
||||||
|
$(OUTPUT).nso : $(OUTPUT).elf
|
||||||
|
|
||||||
|
$(OUTPUT).elf : $(OFILES)
|
||||||
|
|
||||||
|
#---------------------------------------------------------------------------------
|
||||||
|
# you need a rule like this for each extension you use as binary data
|
||||||
|
#---------------------------------------------------------------------------------
|
||||||
|
%.bin.o : %.bin
|
||||||
|
#---------------------------------------------------------------------------------
|
||||||
|
@echo $(notdir $<)
|
||||||
|
@$(bin2o)
|
||||||
|
|
||||||
|
-include $(DEPENDS)
|
||||||
|
|
||||||
|
#---------------------------------------------------------------------------------------
|
||||||
|
endif
|
||||||
|
#---------------------------------------------------------------------------------------
|
86
usb/usbdev/source/main.c
Normal file
86
usb/usbdev/source/main.c
Normal file
@ -0,0 +1,86 @@
|
|||||||
|
#include <string.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <malloc.h>
|
||||||
|
|
||||||
|
#include <switch.h>
|
||||||
|
|
||||||
|
//Example for usbdev, see libnx usb_dev.h. Switch-as-device<>host USB comms for serial.
|
||||||
|
|
||||||
|
Result usbdev_test(u8 *tmpbuf)
|
||||||
|
{
|
||||||
|
u32 somepos;
|
||||||
|
size_t transfer_sizes[10];
|
||||||
|
u32 *tmpbuf32 = (u32*)tmpbuf;
|
||||||
|
|
||||||
|
//Note that usbDevRead/usbDevWrite return the actual-transfer-size.
|
||||||
|
|
||||||
|
memset(transfer_sizes, 0, sizeof(transfer_sizes));
|
||||||
|
|
||||||
|
for(somepos=0; somepos<0x101; somepos++)
|
||||||
|
{
|
||||||
|
memset(tmpbuf, 0, 0x200);
|
||||||
|
char *strptr = "Hello World!\n";
|
||||||
|
|
||||||
|
if(somepos==0 || somepos==0x101)
|
||||||
|
{
|
||||||
|
transfer_sizes[0] = usbDevWrite(strptr, strlen(strptr));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
tmpbuf[0] = somepos-1;
|
||||||
|
usbDevWrite(tmpbuf, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
memset(tmpbuf, 0, 0x210);
|
||||||
|
strncpy((char*)tmpbuf, "Hey: ", 4);
|
||||||
|
|
||||||
|
transfer_sizes[1] = usbDevRead(&tmpbuf[4], 0x200);
|
||||||
|
tmpbuf32[(4+0x200+0)>>2] = transfer_sizes[0];
|
||||||
|
tmpbuf32[(4+0x200+4)>>2] = transfer_sizes[1];
|
||||||
|
|
||||||
|
usbDevWrite(tmpbuf, 4+0x200+8);
|
||||||
|
|
||||||
|
for(somepos=0; somepos<(0x10000>>2); somepos++) {
|
||||||
|
tmpbuf32[somepos] = 0x41414141 + somepos;
|
||||||
|
}
|
||||||
|
|
||||||
|
transfer_sizes[0] = usbDevWrite(tmpbuf, 0x1fe);
|
||||||
|
transfer_sizes[1] = usbDevWrite(tmpbuf, 0x300);
|
||||||
|
transfer_sizes[2] = usbDevWrite(tmpbuf, 0x800);
|
||||||
|
transfer_sizes[3] = usbDevWrite(tmpbuf, 0x1000);
|
||||||
|
transfer_sizes[4] = usbDevWrite(tmpbuf, 0x2200);
|
||||||
|
transfer_sizes[5] = usbDevWrite(tmpbuf, 0x10000);
|
||||||
|
|
||||||
|
usbDevWrite(transfer_sizes, sizeof(transfer_sizes));
|
||||||
|
|
||||||
|
svcSleepThread(5000000000);//Delay 5s
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
Result ret;
|
||||||
|
|
||||||
|
ret = usbDevInitialize();
|
||||||
|
|
||||||
|
if (R_SUCCEEDED(ret)) {
|
||||||
|
u8 *tmpbuf = malloc(0x10000);
|
||||||
|
if (tmpbuf==NULL) ret = -4;
|
||||||
|
|
||||||
|
if (R_SUCCEEDED(ret)) {
|
||||||
|
memset(tmpbuf, 0, 0x10000);
|
||||||
|
ret = usbdev_test(tmpbuf);
|
||||||
|
}
|
||||||
|
|
||||||
|
usbDevExit();
|
||||||
|
}
|
||||||
|
|
||||||
|
if(R_FAILED(ret))fatalSimple(ret);
|
||||||
|
|
||||||
|
svcSleepThread(5000000000);//Delay 5s
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
@ -6,6 +6,8 @@
|
|||||||
|
|
||||||
//Example for usbds, see libnx usb.h. Switch-as-device<>host USB comms.
|
//Example for usbds, see libnx usb.h. Switch-as-device<>host USB comms.
|
||||||
//Linux detects this as a serial device.
|
//Linux detects this as a serial device.
|
||||||
|
//For using serial via usbds in general, see the usbdev example.
|
||||||
|
//See libnx usbdev.c/usb.h for getting actual-transfer-size after using usbDsEndpoint_PostBufferAsync.
|
||||||
|
|
||||||
Result usbds_test(u8 *tmpbuf)
|
Result usbds_test(u8 *tmpbuf)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user