Skip to content

Commit

Permalink
All tests passing!
Browse files Browse the repository at this point in the history
  • Loading branch information
thegecko committed Jan 30, 2021
1 parent 1de40b0 commit 5cf1f56
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 94 deletions.
75 changes: 33 additions & 42 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
*/

#include <string.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/cm3/systick.h>
#include <libopencm3/usb/usbd.h>
#include <libopencm3/stm32/rcc.h>

#define BULK_EP_MAXPACKET 64
#define CONTROL_EP_PACKET 16

static const struct usb_device_descriptor dev = {
.bLength = USB_DT_DEVICE_SIZE,
Expand All @@ -19,7 +21,7 @@ static const struct usb_device_descriptor dev = {
.bMaxPacketSize0 = BULK_EP_MAXPACKET,
.idVendor = 0x59e3,
.idProduct = 0x0a23,
.bcdDevice = 0x0200, // Version
.bcdDevice = 0x0110, // Version
.iManufacturer = 1,
.iProduct = 2,
.iSerialNumber = 3,
Expand Down Expand Up @@ -88,45 +90,27 @@ static const struct usb_config_descriptor config = {

static const char *usb_strings[] = {
"Nonolith Labs",
"STM32F103 Example Device",
"DEMO",
"STM32F103 Test Device",
"TEST_DEVICE",
};

/* Buffer to be used for control requests. */
uint8_t usbd_control_buffer[128];
uint8_t testbuf[16];

static void ep_1_cb(usbd_device *usbd_dev, uint8_t ep) {
char buf[BULK_EP_MAXPACKET];
usbd_ep_write_packet(usbd_dev, ep, buf, BULK_EP_MAXPACKET);
}

static void ep_2_cb(usbd_device *usbd_dev, uint8_t ep) {
char buf[BULK_EP_MAXPACKET];
usbd_ep_read_packet(usbd_dev, ep, buf, BULK_EP_MAXPACKET);
}

static void ep_4_cb(usbd_device *usbd_dev, uint8_t ep)
{
(void)usbd_dev;
(void)ep;
// Do nothing
}
static usbd_device *test_dev;
static uint8_t control_buf[CONTROL_EP_PACKET];
static uint8_t poll_buf[BULK_EP_MAXPACKET];

static enum usbd_request_return_codes control_callback(usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf,
uint16_t *len, void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req)) {

(void)usbd_dev;
(void)complete;

switch(req->bRequest) {
case 0x81: {
if (req->bmRequestType & USB_REQ_TYPE_IN) {
memcpy(*buf, testbuf, 16);
*len = 16;
memcpy(*buf, control_buf, CONTROL_EP_PACKET);
*len = CONTROL_EP_PACKET;
} else {
if (*len > 16) *len = 16;
memcpy(testbuf, *buf, *len);
if (*len > CONTROL_EP_PACKET) *len = CONTROL_EP_PACKET;
memcpy(control_buf, *buf, *len);
}

return USBD_REQ_HANDLED;
Expand All @@ -139,27 +123,34 @@ static enum usbd_request_return_codes control_callback(usbd_device *usbd_dev, st
static void config_callback(usbd_device *usbd_dev, uint16_t wValue) {
(void)wValue;

usbd_ep_setup(usbd_dev, USB_REQ_TYPE_IN | 1, USB_ENDPOINT_ATTR_BULK, BULK_EP_MAXPACKET, ep_1_cb);
usbd_ep_setup(usbd_dev, USB_REQ_TYPE_OUT | 2, USB_ENDPOINT_ATTR_BULK, BULK_EP_MAXPACKET, ep_2_cb);
usbd_ep_setup(usbd_dev, USB_REQ_TYPE_IN | 1, USB_ENDPOINT_ATTR_BULK, BULK_EP_MAXPACKET, NULL);
usbd_ep_setup(usbd_dev, USB_REQ_TYPE_OUT | 2, USB_ENDPOINT_ATTR_BULK, BULK_EP_MAXPACKET, NULL);
usbd_ep_setup(usbd_dev, USB_REQ_TYPE_IN | 3, USB_ENDPOINT_ATTR_BULK, BULK_EP_MAXPACKET, NULL);
usbd_ep_setup(usbd_dev, USB_REQ_TYPE_OUT | 4, USB_ENDPOINT_ATTR_BULK, BULK_EP_MAXPACKET, ep_4_cb);
usbd_ep_setup(usbd_dev, USB_REQ_TYPE_OUT | 4, USB_ENDPOINT_ATTR_BULK, BULK_EP_MAXPACKET, NULL);

usbd_register_control_callback(usbd_dev, USB_REQ_TYPE_VENDOR, USB_REQ_TYPE_TYPE, control_callback);

// Preload data for polling
ep_1_cb(usbd_dev, USB_REQ_TYPE_IN | 1);

// Break ep 4 so it times out
// NAK endpoint 4 so it times out
usbd_ep_nak_set(usbd_dev, USB_REQ_TYPE_OUT | 4, 1);

// SysTick interrupt every N clock pulses: set reload to N-1
systick_set_reload(99999);
systick_interrupt_enable();
systick_counter_enable();
}

int main(void) {
usbd_device *usbd_dev;
rcc_clock_setup_pll(&rcc_hsi_configs[RCC_CLOCK_HSI_48MHZ]);

usbd_dev = usbd_init(&st_usbfs_v1_usb_driver, &dev, &config, usb_strings, 3, usbd_control_buffer, sizeof(usbd_control_buffer));
usbd_register_set_config_callback(usbd_dev, config_callback);

uint8_t usbd_control_buffer[128];
test_dev = usbd_init(&st_usbfs_v1_usb_driver, &dev, &config, usb_strings, 3, usbd_control_buffer, sizeof(usbd_control_buffer));
usbd_register_set_config_callback(test_dev, config_callback);

while (1)
usbd_poll(usbd_dev);
usbd_poll(test_dev);
}

void sys_tick_handler(void) {
usbd_ep_write_packet(test_dev, USB_REQ_TYPE_IN | 1, poll_buf, BULK_EP_MAXPACKET);
usbd_ep_read_packet(test_dev, USB_REQ_TYPE_OUT | 2, poll_buf, BULK_EP_MAXPACKET);
}
53 changes: 1 addition & 52 deletions rules.mk
Original file line number Diff line number Diff line change
@@ -1,36 +1,3 @@
# This version of rules.mk expects the following to be defined before
# inclusion..
### REQUIRED ###
# OPENCM3_DIR - duh
# PROJECT - will be the basename of the output elf, eg usb-gadget0-stm32f4disco
# CFILES - basenames only, eg main.c blah.c
# CXXFILES - same for C++ files. Must have cxx suffix!
# DEVICE - the full device name, eg stm32f405ret6
# _or_
# LDSCRIPT - full path, eg ../../examples/stm32/f4/stm32f4-discovery/stm32f4-discovery.ld
# OPENCM3_LIB - the basename, eg: opencm3_stm32f4
# OPENCM3_DEFS - the target define eg: -DSTM32F4
# ARCH_FLAGS - eg, -mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16
# (ie, the full set of cpu arch flags, _none_ are defined in this file)
#
### OPTIONAL ###
# INCLUDES - fully formed -I paths, if you want extra, eg -I../shared
# BUILD_DIR - defaults to bin, should set this if you are building multiarch
# OPT - full -O flag, defaults to -Os
# CSTD - defaults -std=c99
# CXXSTD - no default.
# OOCD_INTERFACE - eg stlink-v2
# OOCD_TARGET - eg stm32f4x
# both only used if you use the "make flash" target.
# OOCD_FILE - eg my.openocd.cfg
# This overrides interface/target above, and is used as just -f FILE
### TODO/FIXME/notes ###
# No support for stylecheck.
# No support for BMP/texane/random flash methods, no plans either
# No support for magically finding the library.
# C++ hasn't been actually tested with this..... sorry bout that. ;)
# Second expansion/secondary not set, add this if you need them.

BUILD_DIR ?= bin
OPT ?= -Os
CSTD ?= -std=c99
Expand All @@ -50,7 +17,6 @@ CXX = $(PREFIX)g++
LD = $(PREFIX)gcc
OBJCOPY = $(PREFIX)objcopy
OBJDUMP = $(PREFIX)objdump
OOCD ?= openocd

OPENCM3_INC = $(OPENCM3_DIR)/include

Expand Down Expand Up @@ -111,7 +77,6 @@ LDLIBS += -Wl,--start-group -lc -lgcc -lnosys -Wl,--end-group
%: SCCS/s.%

all: $(PROJECT).elf $(PROJECT).bin
flash: $(PROJECT).flash

# error if not using linker script generator
ifeq (,$(DEVICE))
Expand Down Expand Up @@ -154,24 +119,8 @@ $(PROJECT).elf: $(OBJS) $(LDSCRIPT) $(LIBDEPS)
%.list: %.elf
$(OBJDUMP) -S $< > $@

%.flash: %.elf
@printf " FLASH\t$<\n"
ifeq (,$(OOCD_FILE))
$(Q)(echo "halt; program $(realpath $(*).elf) verify reset" | nc -4 localhost 4444 2>/dev/null) || \
$(OOCD) -f interface/$(OOCD_INTERFACE).cfg \
-f target/$(OOCD_TARGET).cfg \
-c "program $(realpath $(*).elf) verify reset exit" \
$(NULL)
else
$(Q)(echo "halt; program $(realpath $(*).elf) verify reset" | nc -4 localhost 4444 2>/dev/null) || \
$(OOCD) -f $(OOCD_FILE) \
-c "program $(realpath $(*).elf) verify reset exit" \
$(NULL)
endif

clean:
rm -rf $(BUILD_DIR) $(GENERATED_BINS)

.PHONY: all clean flash
.PHONY: all clean
-include $(OBJS:.o=.d)

0 comments on commit 5cf1f56

Please sign in to comment.