From 08990e5243de216a599add8d5c2b4b5740cf6a7b Mon Sep 17 00:00:00 2001 From: Gareth McMullin Date: Sun, 13 Mar 2011 13:58:19 +1300 Subject: [PATCH 01/12] Simplified build system for adding new examples. --- Makefile | 40 +++++++++++---------- examples/lm3s/Makefile | 38 -------------------- examples/lpc13xx/Makefile | 38 -------------------- examples/stm32/Makefile | 74 --------------------------------------- 4 files changed, 21 insertions(+), 169 deletions(-) delete mode 100644 examples/lm3s/Makefile delete mode 100644 examples/lpc13xx/Makefile delete mode 100644 examples/stm32/Makefile diff --git a/Makefile b/Makefile index 718ecd4c..aa4933c2 100644 --- a/Makefile +++ b/Makefile @@ -24,6 +24,8 @@ INCDIR = $(DESTDIR)/$(PREFIX)/include LIBDIR = $(DESTDIR)/$(PREFIX)/lib INSTALL = install +TARGETS = stm32 lpc13xx lm3s + # Be silent per default, but 'make V=1' will show all compiler calls. ifneq ($(V),1) Q := @ @@ -36,20 +38,20 @@ all: build build: lib examples lib: - @printf " BUILD lib/stm32\n" - $(Q)$(MAKE) -C lib/stm32 all - @printf " BUILD lib/lpc13xx\n" - $(Q)$(MAKE) -C lib/lpc13xx all - @printf " BUILD lib/lm3s\n" - $(Q)$(MAKE) -C lib/lm3s all + $(Q)for i in $(addprefix $@/,$(TARGETS)); do \ + if [ -d $$i ]; then \ + printf " BUILD $$i\n"; \ + $(MAKE) -C $$i; \ + fi; \ + done -examples: lib - @printf " BUILD examples/stm32\n" - $(Q)$(MAKE) -C examples/stm32 all - @printf " BUILD examples/lpc13xx\n" - $(Q)$(MAKE) -C examples/lpc13xx all - @printf " BUILD examples/lm3s\n" - $(Q)$(MAKE) -C examples/lm3s all +examples: + $(Q)for i in $(addsuffix /*,$(addprefix $@/,$(TARGETS))); do \ + if [ -d $$i ]; then \ + printf " BUILD $$i\n"; \ + $(MAKE) -C $$i; \ + fi; \ + done install: lib @printf " INSTALL headers\n" @@ -62,12 +64,12 @@ install: lib $(Q)$(INSTALL) -m 0644 lib/*/*.ld $(LIBDIR) clean: - $(Q)$(MAKE) -C examples/stm32 clean - $(Q)$(MAKE) -C lib/stm32 clean - $(Q)$(MAKE) -C examples/lpc13xx clean - $(Q)$(MAKE) -C lib/lpc13xx clean - $(Q)$(MAKE) -C examples/lm3s clean - $(Q)$(MAKE) -C lib/lm3s clean + $(Q)for i in $(addprefix lib/,$(TARGETS)) \ + $(addsuffix /*,$(addprefix examples/,$(TARGETS))); do \ + if [ -d $$i ]; then \ + $(MAKE) -C $$i clean; \ + fi; \ + done .PHONY: build lib examples install clean diff --git a/examples/lm3s/Makefile b/examples/lm3s/Makefile deleted file mode 100644 index 4f1704b7..00000000 --- a/examples/lm3s/Makefile +++ /dev/null @@ -1,38 +0,0 @@ -## -## This file is part of the libopencm3 project. -## -## Copyright (C) 2010 Piotr Esden-Tempski -## -## 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 . -## - -# Be silent per default, but 'make V=1' will show all compiler calls. -ifneq ($(V),1) -Q := @ -# Do not print "Entering directory ...". -MAKEFLAGS += --no-print-directory -endif - -all: lm3s3748-evb - -lm3s3748-evb: - @printf " BUILD examples/lm3s/lm3s3748-evb\n" - $(Q)$(MAKE) -C lm3s3748-evb - -clean: - @printf " CLEAN examples/lm3s/lm3s3748-evb\n" - $(Q)$(MAKE) -C lm3s3748-evb clean - -.PHONY: lm3s3748-evb clean - diff --git a/examples/lpc13xx/Makefile b/examples/lpc13xx/Makefile deleted file mode 100644 index 486b4ba4..00000000 --- a/examples/lpc13xx/Makefile +++ /dev/null @@ -1,38 +0,0 @@ -## -## This file is part of the libopencm3 project. -## -## Copyright (C) 2010 Piotr Esden-Tempski -## -## 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 . -## - -# Be silent per default, but 'make V=1' will show all compiler calls. -ifneq ($(V),1) -Q := @ -# Do not print "Entering directory ...". -MAKEFLAGS += --no-print-directory -endif - -all: lpc-p1343 - -lpc-p1343: - @printf " BUILD examples/lpc13xx/lpc-p1343\n" - $(Q)$(MAKE) -C lpc-p1343 - -clean: - @printf " CLEAN examples/lpc13xx/lpc-p1343\n" - $(Q)$(MAKE) -C lpc-p1343 clean - -.PHONY: lpc-p1343 clean - diff --git a/examples/stm32/Makefile b/examples/stm32/Makefile deleted file mode 100644 index 6a218fc9..00000000 --- a/examples/stm32/Makefile +++ /dev/null @@ -1,74 +0,0 @@ -## -## This file is part of the libopencm3 project. -## -## Copyright (C) 2010 Piotr Esden-Tempski -## -## 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 . -## - -# Be silent per default, but 'make V=1' will show all compiler calls. -ifneq ($(V),1) -Q := @ -# Do not print "Entering directory ...". -MAKEFLAGS += --no-print-directory -endif - -all: stm32-discovery stm32-h103 stm32-h107 mb525 lisa-m obldc other - -stm32-discovery: - @printf " BUILD examples/stm32/stm32-discovery\n" - $(Q)$(MAKE) -C stm32-discovery - -stm32-h103: - @printf " BUILD examples/stm32/stm32-h103\n" - $(Q)$(MAKE) -C stm32-h103 - -stm32-h107: - @printf " BUILD examples/stm32/stm32-h107\n" - $(Q)$(MAKE) -C stm32-h107 - -mb525: - @printf " BUILD examples/stm32/mb525\n" - $(Q)$(MAKE) -C mb525 - -lisa-m: - @printf " BUILD examples/stm32/lisa-m\n" - $(Q)$(MAKE) -C lisa-m - -other: - @printf " BUILD examples/stm32/other\n" - $(Q)$(MAKE) -C other - -obldc: - @printf " BUILD examples/stm32/obldc\n" - $(Q)$(MAKE) -C obldc - -clean: - @printf " CLEAN examples/stm32/stm32-discovery\n" - $(Q)$(MAKE) -C stm32-discovery clean - @printf " CLEAN examples/stm32/stm32-h103\n" - $(Q)$(MAKE) -C stm32-h103 clean - @printf " CLEAN examples/stm32/stm32-h107\n" - $(Q)$(MAKE) -C stm32-h107 clean - @printf " CLEAN examples/stm32/mb525\n" - $(Q)$(MAKE) -C mb525 clean - @printf " CLEAN examples/stm32/lisa-m\n" - $(Q)$(MAKE) -C lisa-m clean - @printf " CLEAN examples/stm32/other\n" - $(Q)$(MAKE) -C other clean - @printf " CLEAN examples/stm32/obldc\n" - $(Q)$(MAKE) -C obldc clean - -.PHONY: stm32-discovery stm32-h103 stm32-h107 mb525 lisa-m other obldc clean - From 95286a22f0bb6a822b4d2a62611bcc7b2c2daa8b Mon Sep 17 00:00:00 2001 From: Gareth McMullin Date: Sun, 13 Mar 2011 14:50:53 +1300 Subject: [PATCH 02/12] More build improvements and fixed broken examples. --- Makefile | 5 +- examples/lm3s/lm3s3748-evb/Makefile | 38 -------- examples/lpc13xx/lpc-p1343/Makefile | 38 -------- examples/stm32/lisa-m/Makefile | 43 -------- examples/stm32/lisa-m/usb_dfu/usbdfu.c | 64 ++++-------- examples/stm32/mb525/Makefile | 44 --------- examples/stm32/obldc/Makefile | 56 ----------- examples/stm32/obldc/usart_irq/usart_irq.c | 4 +- examples/stm32/other/Makefile | 92 ------------------ examples/stm32/stm32-discovery/Makefile | 62 ------------ examples/stm32/stm32-h103/Makefile | 108 --------------------- examples/stm32/stm32-h103/usb_dfu/usbdfu.c | 64 ++++-------- examples/stm32/stm32-h103/usb_iap/Makefile | 2 +- examples/stm32/stm32-h103/usb_iap/usbiap.c | 64 ++++-------- examples/stm32/stm32-h107/Makefile | 38 -------- 15 files changed, 66 insertions(+), 656 deletions(-) delete mode 100644 examples/lm3s/lm3s3748-evb/Makefile delete mode 100644 examples/lpc13xx/lpc-p1343/Makefile delete mode 100644 examples/stm32/lisa-m/Makefile delete mode 100644 examples/stm32/mb525/Makefile delete mode 100644 examples/stm32/obldc/Makefile delete mode 100644 examples/stm32/other/Makefile delete mode 100644 examples/stm32/stm32-discovery/Makefile delete mode 100644 examples/stm32/stm32-h103/Makefile delete mode 100644 examples/stm32/stm32-h107/Makefile diff --git a/Makefile b/Makefile index aa4933c2..9f3be8da 100644 --- a/Makefile +++ b/Makefile @@ -46,7 +46,7 @@ lib: done examples: - $(Q)for i in $(addsuffix /*,$(addprefix $@/,$(TARGETS))); do \ + $(Q)for i in $(addsuffix /*/*,$(addprefix $@/,$(TARGETS))); do \ if [ -d $$i ]; then \ printf " BUILD $$i\n"; \ $(MAKE) -C $$i; \ @@ -65,8 +65,9 @@ install: lib clean: $(Q)for i in $(addprefix lib/,$(TARGETS)) \ - $(addsuffix /*,$(addprefix examples/,$(TARGETS))); do \ + $(addsuffix /*/*,$(addprefix examples/,$(TARGETS))); do \ if [ -d $$i ]; then \ + printf " CLEAN $$i\n"; \ $(MAKE) -C $$i clean; \ fi; \ done diff --git a/examples/lm3s/lm3s3748-evb/Makefile b/examples/lm3s/lm3s3748-evb/Makefile deleted file mode 100644 index 5750afd2..00000000 --- a/examples/lm3s/lm3s3748-evb/Makefile +++ /dev/null @@ -1,38 +0,0 @@ -## -## This file is part of the libopencm3 project. -## -## Copyright (C) 2009 Uwe Hermann -## -## 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 . -## - -# Be silent per default, but 'make V=1' will show all compiler calls. -ifneq ($(V),1) -Q := @ -# Do not print "Entering directory ...". -MAKEFLAGS += --no-print-directory -endif - -all: miniblink - -miniblink: - @printf " BUILD examples/lm3s/lm3s3748-evb/miniblink\n" - $(Q)$(MAKE) -C miniblink - -clean: - @printf " CLEAN examples/lm3s/lm3s3748-evb/miniblink\n" - $(Q)$(MAKE) -C miniblink clean - -.PHONY: miniblink clean - diff --git a/examples/lpc13xx/lpc-p1343/Makefile b/examples/lpc13xx/lpc-p1343/Makefile deleted file mode 100644 index e1745acf..00000000 --- a/examples/lpc13xx/lpc-p1343/Makefile +++ /dev/null @@ -1,38 +0,0 @@ -## -## This file is part of the libopencm3 project. -## -## Copyright (C) 2009 Uwe Hermann -## -## 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 . -## - -# Be silent per default, but 'make V=1' will show all compiler calls. -ifneq ($(V),1) -Q := @ -# Do not print "Entering directory ...". -MAKEFLAGS += --no-print-directory -endif - -all: miniblink - -miniblink: - @printf " BUILD examples/lpc13xx/lpc-p1343/miniblink\n" - $(Q)$(MAKE) -C miniblink - -clean: - @printf " CLEAN examples/lpc13xx/lpc-p1343/miniblink\n" - $(Q)$(MAKE) -C miniblink clean - -.PHONY: miniblink clean - diff --git a/examples/stm32/lisa-m/Makefile b/examples/stm32/lisa-m/Makefile deleted file mode 100644 index fa554bca..00000000 --- a/examples/stm32/lisa-m/Makefile +++ /dev/null @@ -1,43 +0,0 @@ -## -## This file is part of the libopencm3 project. -## -## Copyright (C) 2009 Uwe Hermann -## -## 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 . -## - -# Be silent per default, but 'make V=1' will show all compiler calls. -ifneq ($(V),1) -Q := @ -# Do not print "Entering directory ...". -MAKEFLAGS += --no-print-directory -endif - -# TODO: Add usb_dfu later, doesn't build at the moment. -all: fancyblink usb_hid - -fancyblink: - @printf " BUILD examples/stm32/lisa-m/fancyblink\n" - $(Q)$(MAKE) -C usb_hid - -usb_hid: - @printf " BUILD examples/stm32/lisa-m/usb_hid\n" - $(Q)$(MAKE) -C usb_hid - -clean: - @printf " CLEAN examples/stm32/lisa-m/usb_hid\n" - $(Q)$(MAKE) -C usb_hid clean - -.PHONY: fancyblink usb_hid clean - diff --git a/examples/stm32/lisa-m/usb_dfu/usbdfu.c b/examples/stm32/lisa-m/usb_dfu/usbdfu.c index 2ae83746..4e83ce01 100644 --- a/examples/stm32/lisa-m/usb_dfu/usbdfu.c +++ b/examples/stm32/lisa-m/usb_dfu/usbdfu.c @@ -173,18 +173,26 @@ static void usbdfu_getstatus_complete(struct usb_setup_data *req) } } -static int usbdfu_control_command(struct usb_setup_data *req, - void (**complete)(struct usb_setup_data *req)) +static int usbdfu_control_request(struct usb_setup_data *req, u8 **buf, + u16 *len, void (**complete)(struct usb_setup_data *req)) { - (void)complete; - if(req->bmRequestType != 0x21) + if((req->bmRequestType & 0x7F) != 0x21) return 0; /* Only accept class request */ - + switch(req->bRequest) { case DFU_DNLOAD: - usbdfu_state = STATE_DFU_MANIFEST_SYNC; - return 1; + if((len == NULL) || (*len == 0)) { + usbdfu_state = STATE_DFU_MANIFEST_SYNC; + return 1; + } else { + /* Copy download data for use on GET_STATUS */ + prog.blocknum = req->wValue; + prog.len = *len; + memcpy(prog.buf, *buf, *len); + usbdfu_state = STATE_DFU_DNLOAD_SYNC; + return 1; + } case DFU_CLRSTATUS: /* Clear error and return to dfuIDLE */ if(usbdfu_state == STATE_DFU_ERROR) @@ -194,19 +202,6 @@ static int usbdfu_control_command(struct usb_setup_data *req, /* Abort returns to dfuIDLE state */ usbdfu_state = STATE_DFU_IDLE; return 1; - } - - return 0; -} - -static int usbdfu_control_read(struct usb_setup_data *req, u8 **buf, u16 *len, - void (**complete)(struct usb_setup_data *req)) -{ - - if(req->bmRequestType != 0xA1) - return 0; /* Only accept class request */ - - switch(req->bRequest) { case DFU_UPLOAD: /* Upload not supported for now */ return 0; @@ -235,26 +230,6 @@ static int usbdfu_control_read(struct usb_setup_data *req, u8 **buf, u16 *len, return 0; } -static int usbdfu_control_write(struct usb_setup_data *req, u8 *buf, u16 len, - void (**complete)(struct usb_setup_data *req)) -{ - (void)complete; - - if(req->bmRequestType != 0x21) - return 0; /* Only accept class request */ - - if(req->bRequest != DFU_DNLOAD) - return 0; - - /* Copy download data for use on GET_STATUS */ - prog.blocknum = req->wValue; - prog.len = len; - memcpy(prog.buf, buf, len); - usbdfu_state = STATE_DFU_DNLOAD_SYNC; - - return 1; -} - int main(void) { rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); @@ -280,11 +255,12 @@ int main(void) AFIO_MAPR |= AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_ON; gpio_set_mode(GPIOA, GPIO_MODE_INPUT, 0, GPIO15); - usbd_init(&dev, &config, usb_strings); + usbd_init(&stm32f103_usb_driver, &dev, &config, usb_strings); usbd_set_control_buffer_size(sizeof(usbd_control_buffer)); - usbd_register_control_command_callback(usbdfu_control_command); - usbd_register_control_write_callback(usbdfu_control_write); - usbd_register_control_read_callback(usbdfu_control_read); + usbd_register_control_callback( + USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE, + USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, + usbdfu_control_request); gpio_set(GPIOA, GPIO15); gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, diff --git a/examples/stm32/mb525/Makefile b/examples/stm32/mb525/Makefile deleted file mode 100644 index 78181e05..00000000 --- a/examples/stm32/mb525/Makefile +++ /dev/null @@ -1,44 +0,0 @@ -## -## This file is part of the libopencm3 project. -## -## Copyright (C) 2009 Uwe Hermann -## -## 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 . -## - -# Be silent per default, but 'make V=1' will show all compiler calls. -ifneq ($(V),1) -Q := @ -# Do not print "Entering directory ...". -MAKEFLAGS += --no-print-directory -endif - -all: fancyblink pwmleds - -fancyblink: - @printf " BUILD examples/stm32/mb525/fancyblink\n" - $(Q)$(MAKE) -C fancyblink - -pwmleds: - @printf " BUILD examples/stm32/mb525/pwmleds\n" - $(Q)$(MAKE) -C pwmleds - -clean: - @printf " CLEAN examples/stm32/mb525/fancyblink\n" - $(Q)$(MAKE) -C fancyblink clean - @printf " CLEAN examples/stm32/mb525/pwmleds\n" - $(Q)$(MAKE) -C pwmleds clean - -.PHONY: fancyblink pwmleds clean - diff --git a/examples/stm32/obldc/Makefile b/examples/stm32/obldc/Makefile deleted file mode 100644 index 626d889d..00000000 --- a/examples/stm32/obldc/Makefile +++ /dev/null @@ -1,56 +0,0 @@ -## -## This file is part of the libopencm3 project. -## -## Copyright (C) 2010 Piotr Esden-Tempski -## -## 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 . -## - -# Be silent per default, but 'make V=1' will show all compiler calls. -ifneq ($(V),1) -Q := @ -# Do not print "Entering directory ...". -MAKEFLAGS += --no-print-directory -endif - -all: led systick usart can - -led: - @printf " BUILD examples/stm32/obldc/led\n" - $(Q)$(MAKE) -C led - -systick: - @printf " BUILD examples/stm32/obldc/systick\n" - $(Q)$(MAKE) -C systick - -usart: - @printf " BUILD examples/stm32/obldc/usart\n" - $(Q)$(MAKE) -C usart - -can: - @printf " BUILD examples/stm32/obldc/can\n" - $(Q)$(MAKE) -C can - -clean: - @printf " CLEAN examples/stm32/obldc/led\n" - $(Q)$(MAKE) -C led clean - @printf " CLEAN examples/stm32/obldc/systick\n" - $(Q)$(MAKE) -C systick clean - @printf " CLEAN examples/stm32/obldc/usart\n" - $(Q)$(MAKE) -C usart clean - @printf " CLEAN examples/stm32/obldc/can\n" - $(Q)$(MAKE) -C can clean - -.PHONY: led systick usart can - diff --git a/examples/stm32/obldc/usart_irq/usart_irq.c b/examples/stm32/obldc/usart_irq/usart_irq.c index 7e5f9719..82320c1a 100644 --- a/examples/stm32/obldc/usart_irq/usart_irq.c +++ b/examples/stm32/obldc/usart_irq/usart_irq.c @@ -80,7 +80,7 @@ void usart1_isr(void) static u8 data = 'A'; /* Check if we were called because of RXNE. */ - if (((USART_CR1(USART1) & USART_SR_RXNEIE) != 0) && + if (((USART_CR1(USART1) & USART_CR1_RXNEIE) != 0) && ((USART_SR(USART1) & USART_SR_RXNE) != 0)) { /* Indicate that we got data. */ gpio_toggle(GPIOA, GPIO6); @@ -93,7 +93,7 @@ void usart1_isr(void) } /* Check if we were called because of TXE. */ - if ((USART_CR1(USART1) & USART_SR_TXEIE) != 0) && + if (((USART_CR1(USART1) & USART_CR1_TXEIE) != 0) && ((USART_SR(USART1) & USART_SR_TXE) != 0)) { /* Indicate that we are sending out data. */ gpio_toggle(GPIOA, GPIO7); diff --git a/examples/stm32/other/Makefile b/examples/stm32/other/Makefile deleted file mode 100644 index 995bec89..00000000 --- a/examples/stm32/other/Makefile +++ /dev/null @@ -1,92 +0,0 @@ -## -## This file is part of the libopencm3 project. -## -## Copyright (C) 2010 Uwe Hermann -## -## 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 . -## - -# Be silent per default, but 'make V=1' will show all compiler calls. -ifneq ($(V),1) -Q := @ -# Do not print "Entering directory ...". -MAKEFLAGS += --no-print-directory -endif - -all: i2c_stts75_sensor adc_temperature_sensor dma_mem2mem timer_interrupt systick dogm128 rtc usb_cdcacm usb_dfu usb_hid - -i2c_stts75_sensor: - @printf " BUILD examples/stm32/other/i2c_stts75_sensor\n" - $(Q)$(MAKE) -C i2c_stts75_sensor - -adc_temperature_sensor: - @printf " BUILD examples/stm32/other/adc_temperature_sensor\n" - $(Q)$(MAKE) -C adc_temperature_sensor - -dma_mem2mem: - @printf " BUILD examples/stm32/other/dma_mem2mem\n" - $(Q)$(MAKE) -C dma_mem2mem - -timer_interrupt: - @printf " BUILD examples/stm32/other/timer_interrupt\n" - $(Q)$(MAKE) -C timer_interrupt - -systick: - @printf " BUILD examples/stm32/other/systick\n" - $(Q)$(MAKE) -C systick - -dogm128: - @printf " BUILD examples/stm32/other/dogm128\n" - $(Q)$(MAKE) -C dogm128 - -rtc: - @printf " BUILD examples/stm32/other/rtc\n" - $(Q)$(MAKE) -C rtc - -usb_cdcacm: - @printf " BUILD examples/stm32/other/usb_cdcacm\n" - $(Q)$(MAKE) -C usb_cdcacm - -usb_dfu: - @printf " BUILD examples/stm32/other/usb_dfu\n" - $(Q)$(MAKE) -C usb_dfu - -usb_hid: - @printf " BUILD examples/stm32/other/usb_hid\n" - $(Q)$(MAKE) -C usb_hid - -clean: - @printf " CLEAN examples/stm32/other/i2c_stts75_sensor\n" - $(Q)$(MAKE) -C i2c_stts75_sensor clean - @printf " CLEAN examples/stm32/other/adc_temperature_sensor\n" - $(Q)$(MAKE) -C adc_temperature_sensor clean - @printf " CLEAN examples/stm32/other/dma_mem2mem\n" - $(Q)$(MAKE) -C dma_mem2mem clean - @printf " CLEAN examples/stm32/other/timer_interrupt\n" - $(Q)$(MAKE) -C timer_interrupt clean - @printf " CLEAN examples/stm32/other/systick\n" - $(Q)$(MAKE) -C systick clean - @printf " CLEAN examples/stm32/other/dogm128\n" - $(Q)$(MAKE) -C dogm128 clean - @printf " CLEAN examples/stm32/other/rtc\n" - $(Q)$(MAKE) -C rtc clean - @printf " CLEAN examples/stm32/other/usb_cdcacm\n" - $(Q)$(MAKE) -C usb_cdcacm clean - @printf " CLEAN examples/stm32/other/usb_dfu\n" - $(Q)$(MAKE) -C usb_dfu clean - @printf " CLEAN examples/stm32/other/usb_hid\n" - $(Q)$(MAKE) -C usb_hid clean - -.PHONY: i2c_stts75_sensor adc_temperature_sensor dma_mem2mem timer_interrupt systick dogm128 rtc clean usb_cdcacm usb_dfu usb_hid - diff --git a/examples/stm32/stm32-discovery/Makefile b/examples/stm32/stm32-discovery/Makefile deleted file mode 100644 index bbf96d80..00000000 --- a/examples/stm32/stm32-discovery/Makefile +++ /dev/null @@ -1,62 +0,0 @@ -## -## This file is part of the libopencm3 project. -## -## Copyright (C) 2009 Uwe Hermann -## -## 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 . -## - -# Be silent per default, but 'make V=1' will show all compiler calls. -ifneq ($(V),1) -Q := @ -# Do not print "Entering directory ...". -MAKEFLAGS += --no-print-directory -endif - -all: miniblink fancyblink usart button rtc - -miniblink: - @printf " BUILD examples/stm32/stm32-discovery/miniblink\n" - $(Q)$(MAKE) -C miniblink - -fancyblink: - @printf " BUILD examples/stm32/stm32-discovery/fancyblink\n" - $(Q)$(MAKE) -C fancyblink - -usart: - @printf " BUILD examples/stm32/stm32-discovery/usart\n" - $(Q)$(MAKE) -C usart - -button: - @printf " BUILD examples/stm32/stm32-discovery/button\n" - $(Q)$(MAKE) -C button - -rtc: - @printf " BUILD examples/stm32/stm32-discovery/rtc\n" - $(Q)$(MAKE) -C rtc - -clean: - @printf " CLEAN examples/stm32/stm32-discovery/miniblink\n" - $(Q)$(MAKE) -C miniblink clean - @printf " CLEAN examples/stm32/stm32-discovery/fancyblink\n" - $(Q)$(MAKE) -C fancyblink clean - @printf " CLEAN examples/stm32/stm32-discovery/usart\n" - $(Q)$(MAKE) -C usart clean - @printf " CLEAN examples/stm32/stm32-discovery/button\n" - $(Q)$(MAKE) -C button clean - @printf " CLEAN examples/stm32/stm32-discovery/rtc\n" - $(Q)$(MAKE) -C rtc clean - -.PHONY: miniblink fancyblink usart button rtc clean - diff --git a/examples/stm32/stm32-h103/Makefile b/examples/stm32/stm32-h103/Makefile deleted file mode 100644 index 85d0c45e..00000000 --- a/examples/stm32/stm32-h103/Makefile +++ /dev/null @@ -1,108 +0,0 @@ -## -## This file is part of the libopencm3 project. -## -## Copyright (C) 2009 Uwe Hermann -## -## 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 . -## - -# Be silent per default, but 'make V=1' will show all compiler calls. -ifneq ($(V),1) -Q := @ -# Do not print "Entering directory ...". -MAKEFLAGS += --no-print-directory -endif - -all: miniblink fancyblink usart usart_irq usart_printf usart_irq_printf timer usb_cdcacm usb_hid button exti_both traceswo - -miniblink: - @printf " BUILD examples/stm32/stm32-h103/miniblink\n" - $(Q)$(MAKE) -C miniblink - -fancyblink: - @printf " BUILD examples/stm32/stm32-h103/fancyblink\n" - $(Q)$(MAKE) -C fancyblink - -usart: - @printf " BUILD examples/stm32/stm32-h103/usart\n" - $(Q)$(MAKE) -C usart - -usart_irq: - @printf " BUILD examples/stm32/stm32-h103/usart_irq\n" - $(Q)$(MAKE) -C usart_irq - -usart_printf: - @printf " BUILD examples/stm32/stm32-h103/usart_printf\n" - $(Q)$(MAKE) -C usart_printf - -usart_irq_printf: - @printf " BUILD examples/stm32/stm32-h103/usart_irq_printf\n" - $(Q)$(MAKE) -C usart_irq_printf - -timer: - @printf " BUILD examples/stm32/stm32-h103/timer\n" - $(Q)$(MAKE) -C timer - -spi: - @printf " BUILD examples/stm32/stm32-h103/spi\n" - $(Q)$(MAKE) -C spi - -usb_cdcacm: - @printf " BUILD examples/stm32/stm32-h103/usb_cdcacm\n" - $(Q)$(MAKE) -C usb_cdcacm - -usb_hid: - @printf " BUILD examples/stm32/stm32-h103/usb_hid\n" - $(Q)$(MAKE) -C usb_hid - -button: - @printf " BUILD examples/stm32/stm32-h103/button\n" - $(Q)$(MAKE) -C button - -exti_both: - @printf " BUILD examples/stm32/stm32-h103/exti_both\n" - $(Q)$(MAKE) -C exti_both - -traceswo: - @printf " BUILD examples/stm32/stm32-h103/traceswo\n" - $(Q)$(MAKE) -C traceswo - -clean: - @printf " CLEAN examples/stm32/stm32-h103/miniblink\n" - $(Q)$(MAKE) -C miniblink clean - @printf " CLEAN examples/stm32/stm32-h103/fancyblink\n" - $(Q)$(MAKE) -C fancyblink clean - @printf " CLEAN examples/stm32/stm32-h103/usart\n" - $(Q)$(MAKE) -C usart clean - @printf " CLEAN examples/stm32/stm32-h103/usart_irq\n" - $(Q)$(MAKE) -C usart_irq clean - @printf " CLEAN examples/stm32/stm32-h103/usart_printf\n" - $(Q)$(MAKE) -C usart_printf clean - @printf " CLEAN examples/stm32/stm32-h103/usart_irq_printf\n" - $(Q)$(MAKE) -C usart_irq_printf clean - @printf " CLEAN examples/stm32/stm32-h103/spi\n" - $(Q)$(MAKE) -C spi clean - @printf " CLEAN examples/stm32/stm32-h103/usb_cdcacm\n" - $(Q)$(MAKE) -C usb_cdcacm clean - @printf " CLEAN examples/stm32/stm32-h103/usb_hid\n" - $(Q)$(MAKE) -C usb_hid clean - @printf " CLEAN examples/stm32/stm32-h103/button\n" - $(Q)$(MAKE) -C button clean - @printf " CLEAN examples/stm32/stm32-h103/exti_both\n" - $(Q)$(MAKE) -C exti_both clean - @printf " CLEAN examples/stm32/stm32-h103/traceswo\n" - $(Q)$(MAKE) -C traceswo clean - -.PHONY: miniblink fancyblink usart usart_irq usart_printf usart_irq_printf timer spi usb_cdcacm usb_hid button exti_both traceswo clean - diff --git a/examples/stm32/stm32-h103/usb_dfu/usbdfu.c b/examples/stm32/stm32-h103/usb_dfu/usbdfu.c index 2ae83746..4e83ce01 100644 --- a/examples/stm32/stm32-h103/usb_dfu/usbdfu.c +++ b/examples/stm32/stm32-h103/usb_dfu/usbdfu.c @@ -173,18 +173,26 @@ static void usbdfu_getstatus_complete(struct usb_setup_data *req) } } -static int usbdfu_control_command(struct usb_setup_data *req, - void (**complete)(struct usb_setup_data *req)) +static int usbdfu_control_request(struct usb_setup_data *req, u8 **buf, + u16 *len, void (**complete)(struct usb_setup_data *req)) { - (void)complete; - if(req->bmRequestType != 0x21) + if((req->bmRequestType & 0x7F) != 0x21) return 0; /* Only accept class request */ - + switch(req->bRequest) { case DFU_DNLOAD: - usbdfu_state = STATE_DFU_MANIFEST_SYNC; - return 1; + if((len == NULL) || (*len == 0)) { + usbdfu_state = STATE_DFU_MANIFEST_SYNC; + return 1; + } else { + /* Copy download data for use on GET_STATUS */ + prog.blocknum = req->wValue; + prog.len = *len; + memcpy(prog.buf, *buf, *len); + usbdfu_state = STATE_DFU_DNLOAD_SYNC; + return 1; + } case DFU_CLRSTATUS: /* Clear error and return to dfuIDLE */ if(usbdfu_state == STATE_DFU_ERROR) @@ -194,19 +202,6 @@ static int usbdfu_control_command(struct usb_setup_data *req, /* Abort returns to dfuIDLE state */ usbdfu_state = STATE_DFU_IDLE; return 1; - } - - return 0; -} - -static int usbdfu_control_read(struct usb_setup_data *req, u8 **buf, u16 *len, - void (**complete)(struct usb_setup_data *req)) -{ - - if(req->bmRequestType != 0xA1) - return 0; /* Only accept class request */ - - switch(req->bRequest) { case DFU_UPLOAD: /* Upload not supported for now */ return 0; @@ -235,26 +230,6 @@ static int usbdfu_control_read(struct usb_setup_data *req, u8 **buf, u16 *len, return 0; } -static int usbdfu_control_write(struct usb_setup_data *req, u8 *buf, u16 len, - void (**complete)(struct usb_setup_data *req)) -{ - (void)complete; - - if(req->bmRequestType != 0x21) - return 0; /* Only accept class request */ - - if(req->bRequest != DFU_DNLOAD) - return 0; - - /* Copy download data for use on GET_STATUS */ - prog.blocknum = req->wValue; - prog.len = len; - memcpy(prog.buf, buf, len); - usbdfu_state = STATE_DFU_DNLOAD_SYNC; - - return 1; -} - int main(void) { rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); @@ -280,11 +255,12 @@ int main(void) AFIO_MAPR |= AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_ON; gpio_set_mode(GPIOA, GPIO_MODE_INPUT, 0, GPIO15); - usbd_init(&dev, &config, usb_strings); + usbd_init(&stm32f103_usb_driver, &dev, &config, usb_strings); usbd_set_control_buffer_size(sizeof(usbd_control_buffer)); - usbd_register_control_command_callback(usbdfu_control_command); - usbd_register_control_write_callback(usbdfu_control_write); - usbd_register_control_read_callback(usbdfu_control_read); + usbd_register_control_callback( + USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE, + USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, + usbdfu_control_request); gpio_set(GPIOA, GPIO15); gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, diff --git a/examples/stm32/stm32-h103/usb_iap/Makefile b/examples/stm32/stm32-h103/usb_iap/Makefile index 0b517821..bed9908c 100644 --- a/examples/stm32/stm32-h103/usb_iap/Makefile +++ b/examples/stm32/stm32-h103/usb_iap/Makefile @@ -19,5 +19,5 @@ BINARY = usbiap -include ../../Makefile.includes +include ../../Makefile.include diff --git a/examples/stm32/stm32-h103/usb_iap/usbiap.c b/examples/stm32/stm32-h103/usb_iap/usbiap.c index 2ae83746..4e83ce01 100644 --- a/examples/stm32/stm32-h103/usb_iap/usbiap.c +++ b/examples/stm32/stm32-h103/usb_iap/usbiap.c @@ -173,18 +173,26 @@ static void usbdfu_getstatus_complete(struct usb_setup_data *req) } } -static int usbdfu_control_command(struct usb_setup_data *req, - void (**complete)(struct usb_setup_data *req)) +static int usbdfu_control_request(struct usb_setup_data *req, u8 **buf, + u16 *len, void (**complete)(struct usb_setup_data *req)) { - (void)complete; - if(req->bmRequestType != 0x21) + if((req->bmRequestType & 0x7F) != 0x21) return 0; /* Only accept class request */ - + switch(req->bRequest) { case DFU_DNLOAD: - usbdfu_state = STATE_DFU_MANIFEST_SYNC; - return 1; + if((len == NULL) || (*len == 0)) { + usbdfu_state = STATE_DFU_MANIFEST_SYNC; + return 1; + } else { + /* Copy download data for use on GET_STATUS */ + prog.blocknum = req->wValue; + prog.len = *len; + memcpy(prog.buf, *buf, *len); + usbdfu_state = STATE_DFU_DNLOAD_SYNC; + return 1; + } case DFU_CLRSTATUS: /* Clear error and return to dfuIDLE */ if(usbdfu_state == STATE_DFU_ERROR) @@ -194,19 +202,6 @@ static int usbdfu_control_command(struct usb_setup_data *req, /* Abort returns to dfuIDLE state */ usbdfu_state = STATE_DFU_IDLE; return 1; - } - - return 0; -} - -static int usbdfu_control_read(struct usb_setup_data *req, u8 **buf, u16 *len, - void (**complete)(struct usb_setup_data *req)) -{ - - if(req->bmRequestType != 0xA1) - return 0; /* Only accept class request */ - - switch(req->bRequest) { case DFU_UPLOAD: /* Upload not supported for now */ return 0; @@ -235,26 +230,6 @@ static int usbdfu_control_read(struct usb_setup_data *req, u8 **buf, u16 *len, return 0; } -static int usbdfu_control_write(struct usb_setup_data *req, u8 *buf, u16 len, - void (**complete)(struct usb_setup_data *req)) -{ - (void)complete; - - if(req->bmRequestType != 0x21) - return 0; /* Only accept class request */ - - if(req->bRequest != DFU_DNLOAD) - return 0; - - /* Copy download data for use on GET_STATUS */ - prog.blocknum = req->wValue; - prog.len = len; - memcpy(prog.buf, buf, len); - usbdfu_state = STATE_DFU_DNLOAD_SYNC; - - return 1; -} - int main(void) { rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); @@ -280,11 +255,12 @@ int main(void) AFIO_MAPR |= AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_ON; gpio_set_mode(GPIOA, GPIO_MODE_INPUT, 0, GPIO15); - usbd_init(&dev, &config, usb_strings); + usbd_init(&stm32f103_usb_driver, &dev, &config, usb_strings); usbd_set_control_buffer_size(sizeof(usbd_control_buffer)); - usbd_register_control_command_callback(usbdfu_control_command); - usbd_register_control_write_callback(usbdfu_control_write); - usbd_register_control_read_callback(usbdfu_control_read); + usbd_register_control_callback( + USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE, + USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, + usbdfu_control_request); gpio_set(GPIOA, GPIO15); gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, diff --git a/examples/stm32/stm32-h107/Makefile b/examples/stm32/stm32-h107/Makefile deleted file mode 100644 index a6d797d2..00000000 --- a/examples/stm32/stm32-h107/Makefile +++ /dev/null @@ -1,38 +0,0 @@ -## -## This file is part of the libopencm3 project. -## -## Copyright (C) 2009 Uwe Hermann -## -## 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 . -## - -# Be silent per default, but 'make V=1' will show all compiler calls. -ifneq ($(V),1) -Q := @ -# Do not print "Entering directory ...". -MAKEFLAGS += --no-print-directory -endif - -all: fancyblink - -fancyblink: - @printf " BUILD examples/stm32/stm32-h107/fancyblink\n" - $(Q)$(MAKE) -C fancyblink - -clean: - @printf " CLEAN examples/stm32/stm32-h107/fancyblink\n" - $(Q)$(MAKE) -C fancyblink clean - -.PHONY: fancyblink - From 7faea389e8ba002e1c3c7604eaaa916fad583179 Mon Sep 17 00:00:00 2001 From: Gareth McMullin Date: Mon, 14 Mar 2011 16:45:17 +1300 Subject: [PATCH 03/12] Added dependency generation. Abort loops on error. --- Makefile | 6 +++--- examples/lm3s/Makefile.include | 6 ++++-- examples/lpc13xx/Makefile.include | 6 ++++-- examples/stm32/Makefile.include | 6 ++++-- lib/lm3s/Makefile | 5 +++-- lib/lpc13xx/Makefile | 6 ++++-- lib/stm32/Makefile | 6 ++++-- 7 files changed, 26 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index 9f3be8da..fe8db6a0 100644 --- a/Makefile +++ b/Makefile @@ -41,7 +41,7 @@ lib: $(Q)for i in $(addprefix $@/,$(TARGETS)); do \ if [ -d $$i ]; then \ printf " BUILD $$i\n"; \ - $(MAKE) -C $$i; \ + $(MAKE) -C $$i || exit $?; \ fi; \ done @@ -49,7 +49,7 @@ examples: $(Q)for i in $(addsuffix /*/*,$(addprefix $@/,$(TARGETS))); do \ if [ -d $$i ]; then \ printf " BUILD $$i\n"; \ - $(MAKE) -C $$i; \ + $(MAKE) -C $$i || exit $?; \ fi; \ done @@ -68,7 +68,7 @@ clean: $(addsuffix /*/*,$(addprefix examples/,$(TARGETS))); do \ if [ -d $$i ]; then \ printf " CLEAN $$i\n"; \ - $(MAKE) -C $$i clean; \ + $(MAKE) -C $$i clean || exit $?; \ fi; \ done diff --git a/examples/lm3s/Makefile.include b/examples/lm3s/Makefile.include index 3420d1aa..89c12797 100644 --- a/examples/lm3s/Makefile.include +++ b/examples/lm3s/Makefile.include @@ -28,7 +28,7 @@ OBJDUMP = $(PREFIX)-objdump # TOOLCHAIN_DIR = `dirname \`which $(CC)\``/../$(PREFIX) TOOLCHAIN_DIR = ../../../.. CFLAGS += -O0 -g3 -Wall -Wextra -I$(TOOLCHAIN_DIR)/include -fno-common \ - -mcpu=cortex-m3 -mthumb + -mcpu=cortex-m3 -mthumb -MD LDSCRIPT = $(BINARY).ld LDFLAGS += -L$(TOOLCHAIN_DIR)/lib -L$(TOOLCHAIN_DIR)/lib/lm3s \ -T$(LDSCRIPT) -nostartfiles -Wl,--gc-sections @@ -75,7 +75,7 @@ flash: $(BINARY).flash @#printf " OBJDUMP $(*).list\n" $(Q)$(OBJDUMP) -S $(*).elf > $(*).list -%.elf: $(OBJS) $(LDSCRIPT) +%.elf: $(OBJS) $(LDSCRIPT) $(TOOLCHAIN_DIR)/lib/lm3s/libopencm3_lm3s.a @#printf " LD $(subst $(shell pwd)/,,$(@))\n" $(Q)$(LD) $(LDFLAGS) -o $(*).elf $(OBJS) -lopencm3_lm3s @@ -119,3 +119,5 @@ endif .PHONY: images clean +-include $(OBJS:.o=.d) + diff --git a/examples/lpc13xx/Makefile.include b/examples/lpc13xx/Makefile.include index 6e80fd56..1f2deba6 100644 --- a/examples/lpc13xx/Makefile.include +++ b/examples/lpc13xx/Makefile.include @@ -28,7 +28,7 @@ OBJDUMP = $(PREFIX)-objdump # TOOLCHAIN_DIR = `dirname \`which $(CC)\``/../$(PREFIX) TOOLCHAIN_DIR = ../../../.. CFLAGS += -Os -g -Wall -Wextra -I$(TOOLCHAIN_DIR)/include -fno-common \ - -mcpu=cortex-m3 -mthumb + -mcpu=cortex-m3 -mthumb -MD LDSCRIPT = $(BINARY).ld LDFLAGS += -L$(TOOLCHAIN_DIR)/lib -L$(TOOLCHAIN_DIR)/lib/lpc13xx \ -T$(LDSCRIPT) -nostartfiles -Wl,--gc-sections @@ -75,7 +75,7 @@ flash: $(BINARY).flash @#printf " OBJDUMP $(*).list\n" $(Q)$(OBJDUMP) -S $(*).elf > $(*).list -%.elf: $(OBJS) $(LDSCRIPT) +%.elf: $(OBJS) $(LDSCRIPT) $(TOOLCHAIN_DIR)/lib/lpc13xx/libopencm3_lpc13xx.a @#printf " LD $(subst $(shell pwd)/,,$(@))\n" $(Q)$(LD) $(LDFLAGS) -o $(*).elf $(OBJS) -lopencm3_lpc13xx @@ -119,3 +119,5 @@ endif .PHONY: images clean +-include $(OBJS:.o=.d) + diff --git a/examples/stm32/Makefile.include b/examples/stm32/Makefile.include index 3eb3d352..cdaff1d3 100644 --- a/examples/stm32/Makefile.include +++ b/examples/stm32/Makefile.include @@ -28,7 +28,7 @@ OBJDUMP = $(PREFIX)-objdump # TOOLCHAIN_DIR = `dirname \`which $(CC)\``/../$(PREFIX) TOOLCHAIN_DIR = ../../../.. CFLAGS += -Os -g -Wall -Wextra -I$(TOOLCHAIN_DIR)/include \ - -fno-common -mcpu=cortex-m3 -mthumb -msoft-float + -fno-common -mcpu=cortex-m3 -mthumb -msoft-float -MD LDSCRIPT = $(BINARY).ld LDFLAGS += -lc -lnosys -L$(TOOLCHAIN_DIR)/lib -L$(TOOLCHAIN_DIR)/lib/stm32 \ -T$(LDSCRIPT) -nostartfiles -Wl,--gc-sections \ @@ -75,7 +75,7 @@ flash: $(BINARY).flash @#printf " OBJDUMP $(*).list\n" $(Q)$(OBJDUMP) -S $(*).elf > $(*).list -%.elf: $(OBJS) $(LDSCRIPT) +%.elf: $(OBJS) $(LDSCRIPT) $(TOOLCHAIN_DIR)/lib/stm32/libopencm3_stm32.a @#printf " LD $(subst $(shell pwd)/,,$(@))\n" $(Q)$(LD) -o $(*).elf $(OBJS) -lopencm3_stm32 $(LDFLAGS) @@ -118,3 +118,5 @@ endif .PHONY: images clean +-include $(OBJS:.o=.d) + diff --git a/lib/lm3s/Makefile b/lib/lm3s/Makefile index 25c57ec7..2bdbd720 100644 --- a/lib/lm3s/Makefile +++ b/lib/lm3s/Makefile @@ -25,7 +25,7 @@ CC = $(PREFIX)-gcc AR = $(PREFIX)-ar CFLAGS = -Os -g -Wall -Wextra -I../../include -fno-common \ -mcpu=cortex-m3 -mthumb -Wstrict-prototypes \ - -ffunction-sections -fdata-sections + -ffunction-sections -fdata-sections -MD # ARFLAGS = rcsv ARFLAGS = rcs OBJS = gpio.o vector.o @@ -49,8 +49,9 @@ $(LIBNAME).a: $(OBJS) clean: @printf " CLEAN lib/lpc13xx\n" - $(Q)rm -f *.o + $(Q)rm -f *.o *.d $(Q)rm -f $(LIBNAME).a .PHONY: clean +-include $(OBJS:.o=.d) diff --git a/lib/lpc13xx/Makefile b/lib/lpc13xx/Makefile index 37889e66..7181a08b 100644 --- a/lib/lpc13xx/Makefile +++ b/lib/lpc13xx/Makefile @@ -25,7 +25,7 @@ CC = $(PREFIX)-gcc AR = $(PREFIX)-ar CFLAGS = -Os -g -Wall -Wextra -I../../include -fno-common \ -mcpu=cortex-m3 -mthumb -Wstrict-prototypes \ - -ffunction-sections -fdata-sections + -ffunction-sections -fdata-sections -MD # ARFLAGS = rcsv ARFLAGS = rcs OBJS = gpio.o @@ -49,8 +49,10 @@ $(LIBNAME).a: $(OBJS) clean: @printf " CLEAN lib/lpc13xx\n" - $(Q)rm -f *.o + $(Q)rm -f *.o *.d $(Q)rm -f $(LIBNAME).a .PHONY: clean +-include $(OBJS:.o=.d) + diff --git a/lib/stm32/Makefile b/lib/stm32/Makefile index 6ffcc4ad..095f5248 100644 --- a/lib/stm32/Makefile +++ b/lib/stm32/Makefile @@ -25,7 +25,7 @@ CC = $(PREFIX)-gcc AR = $(PREFIX)-ar CFLAGS = -Os -g -Wall -Wextra -I../../include -fno-common \ -mcpu=cortex-m3 -mthumb -Wstrict-prototypes \ - -ffunction-sections -fdata-sections + -ffunction-sections -fdata-sections -MD # ARFLAGS = rcsv ARFLAGS = rcs OBJS = vector.o rcc.o gpio.o usart.o adc.o spi.o flash.o nvic.o \ @@ -52,8 +52,10 @@ $(LIBNAME).a: $(OBJS) clean: @printf " CLEAN lib/stm32\n" - $(Q)rm -f *.o + $(Q)rm -f *.o *.d $(Q)rm -f $(LIBNAME).a .PHONY: clean +-include $(OBJS:.o=.d) + From bf0d1a7b4114ca35a2000bd47d9c6d73c6e92ce2 Mon Sep 17 00:00:00 2001 From: Gareth McMullin Date: Mon, 14 Mar 2011 20:31:32 +1300 Subject: [PATCH 04/12] Connectivity line USB driver now enumerating correctly. --- include/libopencm3/stm32/otg_fs.h | 10 ++++++++++ lib/usb/usb_f107.c | 21 ++++++++------------- lib/usb/usb_standard.c | 6 ++++++ 3 files changed, 24 insertions(+), 13 deletions(-) diff --git a/include/libopencm3/stm32/otg_fs.h b/include/libopencm3/stm32/otg_fs.h index d39593e2..34a7c852 100644 --- a/include/libopencm3/stm32/otg_fs.h +++ b/include/libopencm3/stm32/otg_fs.h @@ -297,6 +297,16 @@ #define OTG_FS_DIEPINTX_EPDISD (1 << 1) #define OTG_FS_DIEPINTX_XFRC (1 << 0) +/* OTG_FS Device IN Endpoint Interrupt Register (OTG_FS_DOEPINTx) */ +/* Bits 31:7 - Reserved */ +#define OTG_FS_DOEPINTX_B2BSTUP (1 << 6) +/* Bit 5 - Reserved */ +#define OTG_FS_DOEPINTX_OTEPDIS (1 << 4) +#define OTG_FS_DOEPINTX_STUP (1 << 3) +/* Bit 2 - Reserved */ +#define OTG_FS_DOEPINTX_EPDISD (1 << 1) +#define OTG_FS_DOEPINTX_XFRC (1 << 0) + /* OTG_FS Device OUT Endpoint 0 Transfer Size Regsiter (OTG_FS_DOEPTSIZ0) */ /* Bit 31 - Reserved */ #define OTG_FS_DIEPSIZ0_STUPCNT_1 (0x1 << 29) diff --git a/lib/usb/usb_f107.c b/lib/usb/usb_f107.c index a681b608..63574431 100644 --- a/lib/usb/usb_f107.c +++ b/lib/usb/usb_f107.c @@ -58,12 +58,13 @@ static void stm32f107_usbd_init(void) int i; /* TODO: Enable interrupts on Reset, Transfer, Suspend and Resume */ + OTG_FS_GINTSTS = OTG_FS_GINTSTS_MMIS; + /* WARNING: Undocumented! Select internal PHY */ OTG_FS_GUSBCFG |= OTG_FS_GUSBCFG_PHYSEL; /* Enable VBUS sensing in device mode and power down the phy */ OTG_FS_GCCFG |= OTG_FS_GCCFG_VBUSBSEN | OTG_FS_GCCFG_PWRDWN; - for(i = 0; i < 800000; i++) __asm__("nop"); /* Wait for AHB idle */ while(!(OTG_FS_GRSTCTL & OTG_FS_GRSTCTL_AHBIDL)); @@ -71,7 +72,6 @@ static void stm32f107_usbd_init(void) OTG_FS_GRSTCTL |= OTG_FS_GRSTCTL_CSRST; while(OTG_FS_GRSTCTL & OTG_FS_GRSTCTL_CSRST); - for(i = 0; i < 800000; i++) __asm__("nop"); /* Force peripheral only mode. */ OTG_FS_GUSBCFG |= OTG_FS_GUSBCFG_FDMOD; @@ -82,6 +82,9 @@ static void stm32f107_usbd_init(void) /* Restart the phy clock */ OTG_FS_PCGCCTL = 0; + OTG_FS_GRXFSIZ = 128; + OTG_FS_GNPTXFSIZ = (128 << 16) | 128; + /* Unmask interrupts for TX and RX */ OTG_FS_GINTMSK &= OTG_FS_GINTMSK_RXFLVLM; @@ -89,15 +92,7 @@ static void stm32f107_usbd_init(void) static void stm32f107_set_address(u8 addr) { - /* There is something badly wrong gere! */ - - /* TODO: Set device address and enable. */ - - /* This I think is correct, but doesn't work at all... */ - //OTG_FS_DCFG = (OTG_FS_DCFG & ~OTG_FS_DCFG_DAD) | (addr << 4); - - /* This is obviously incorrect, but sometimes works... */ - OTG_FS_DCFG |= addr << 4; + OTG_FS_DCFG = (OTG_FS_DCFG & ~OTG_FS_DCFG_DAD) | (addr << 4); } static void stm32f107_ep_setup(u8 addr, u8 type, u16 max_size, @@ -171,9 +166,9 @@ static void stm32f107_ep_stall_set(u8 addr, u8 stall) { if(addr == 0) { if(stall) - OTG_FS_DOEPCTL(addr) |= OTG_FS_DOEPCTL0_STALL; + OTG_FS_DIEPCTL(addr) |= OTG_FS_DIEPCTL0_STALL; else - OTG_FS_DOEPCTL(addr) &= ~OTG_FS_DOEPCTL0_STALL; + OTG_FS_DIEPCTL(addr) &= ~OTG_FS_DIEPCTL0_STALL; } if(addr & 0x80) { diff --git a/lib/usb/usb_standard.c b/lib/usb/usb_standard.c index 59953875..7bac2160 100644 --- a/lib/usb/usb_standard.c +++ b/lib/usb/usb_standard.c @@ -131,6 +131,12 @@ static int usb_standard_set_address(struct usb_setup_data *req, u8 **buf, _usbd_device.current_address = req->wValue; + /* Special workaround for STM32F10[57] that require the address + * to be set here. This is undocumented! + */ + if(_usbd_device.driver == &stm32f107_usb_driver) + _usbd_device.driver->set_address(req->wValue); + return 1; } From 67e3452855d8446bab1ebf9f07cb8891f250fb6c Mon Sep 17 00:00:00 2001 From: Gareth McMullin Date: Tue, 15 Mar 2011 07:12:46 +1300 Subject: [PATCH 05/12] Added simple USB example for STM32F107. --- examples/stm32/stm32-h107/usb_simple/Makefile | 23 ++++ examples/stm32/stm32-h107/usb_simple/README | 10 ++ .../stm32/stm32-h107/usb_simple/usb_simple.c | 118 ++++++++++++++++++ .../stm32/stm32-h107/usb_simple/usb_simple.ld | 31 +++++ 4 files changed, 182 insertions(+) create mode 100644 examples/stm32/stm32-h107/usb_simple/Makefile create mode 100644 examples/stm32/stm32-h107/usb_simple/README create mode 100644 examples/stm32/stm32-h107/usb_simple/usb_simple.c create mode 100644 examples/stm32/stm32-h107/usb_simple/usb_simple.ld diff --git a/examples/stm32/stm32-h107/usb_simple/Makefile b/examples/stm32/stm32-h107/usb_simple/Makefile new file mode 100644 index 00000000..2d954e28 --- /dev/null +++ b/examples/stm32/stm32-h107/usb_simple/Makefile @@ -0,0 +1,23 @@ +## +## This file is part of the libopencm3 project. +## +## Copyright (C) 2009 Uwe Hermann +## +## 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 . +## + +BINARY = usb_simple + +include ../../Makefile.include + diff --git a/examples/stm32/stm32-h107/usb_simple/README b/examples/stm32/stm32-h107/usb_simple/README new file mode 100644 index 00000000..93d4f03e --- /dev/null +++ b/examples/stm32/stm32-h107/usb_simple/README @@ -0,0 +1,10 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This is small USB controlled LED blinking example program using libopencm3. + +It's intended for the ST STM32-based Olimex STM32-H107 eval board (see +http://olimex.com/dev/stm32-h107.html for details). The usbtest.py +script in this directory maybe used to control the LED. + diff --git a/examples/stm32/stm32-h107/usb_simple/usb_simple.c b/examples/stm32/stm32-h107/usb_simple/usb_simple.c new file mode 100644 index 00000000..517f37e6 --- /dev/null +++ b/examples/stm32/stm32-h107/usb_simple/usb_simple.c @@ -0,0 +1,118 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2011 Gareth McMullin + * + * 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 . + */ + +#include +#include + +#include + +const struct usb_device_descriptor dev = { + .bLength = USB_DT_DEVICE_SIZE, + .bDescriptorType = USB_DT_DEVICE, + .bcdUSB = 0x0200, + .bDeviceClass = 0xFF, + .bDeviceSubClass = 0, + .bDeviceProtocol = 0, + .bMaxPacketSize0 = 64, + .idVendor = 0xCAFE, + .idProduct = 0xCAFE, + .bcdDevice = 0x0200, + .iManufacturer = 1, + .iProduct = 2, + .iSerialNumber = 3, + .bNumConfigurations = 1, +}; + +const struct usb_interface_descriptor iface = { + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 0, + .bAlternateSetting = 0, + .bNumEndpoints = 0, + .bInterfaceClass = 0xFF, + .bInterfaceSubClass = 0, + .bInterfaceProtocol = 0, + .iInterface = 0, +}; + +const struct usb_interface ifaces[] = {{ + .num_altsetting = 1, + .altsetting = &iface, +}}; + +const struct usb_config_descriptor config = { + .bLength = USB_DT_CONFIGURATION_SIZE, + .bDescriptorType = USB_DT_CONFIGURATION, + .wTotalLength = 0, + .bNumInterfaces = 1, + .bConfigurationValue = 1, + .iConfiguration = 0, + .bmAttributes = 0x80, + .bMaxPower = 0x32, + + .interface = ifaces, +}; + +const char *usb_strings[] = { + "x", + "Black Sphere Technologies", + "Simple Device", + "1001" +}; + +static int simple_control_callback(struct usb_setup_data *req, u8 **buf, + u16 *len, void (**complete)(struct usb_setup_data *req)) +{ + (void)buf; + (void)len; + (void)complete; + + if(req->bmRequestType != 0x40) + return 0; /* Only accept vendor request */ + + if(req->wValue & 1) + gpio_set(GPIOC, GPIO6); + else + gpio_clear(GPIOC, GPIO6); + + return 1; +} + +int main(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + + rcc_peripheral_enable_clock(&RCC_AHBENR, RCC_AHBENR_OTGFSEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + /* LED output */ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO6); + + usbd_init(&stm32f107_usb_driver, &dev, &config, usb_strings); + usbd_register_control_callback( + USB_REQ_TYPE_VENDOR, + USB_REQ_TYPE_TYPE, + simple_control_callback); + + while (1) + usbd_poll(); +} + diff --git a/examples/stm32/stm32-h107/usb_simple/usb_simple.ld b/examples/stm32/stm32-h107/usb_simple/usb_simple.ld new file mode 100644 index 00000000..6c9c7660 --- /dev/null +++ b/examples/stm32/stm32-h107/usb_simple/usb_simple.ld @@ -0,0 +1,31 @@ +/* + * This file is part of the libopencm3 project. + * + * Copyright (C) 2009 Uwe Hermann + * + * 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 . + */ + +/* Linker script for Olimex STM32-H107 (STM32F107VCT6, 256K flash, 64K RAM). */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 256K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 64K +} + +/* Include the common ld script. */ +INCLUDE libopencm3_stm32.ld + From 6bbc4c2f7cc17cf9cc18dc7110e7d9de2d4a8551 Mon Sep 17 00:00:00 2001 From: Gareth McMullin Date: Tue, 15 Mar 2011 20:33:02 +1300 Subject: [PATCH 06/12] Fixed non-control endpoints for connectivity line USB. --- include/libopencm3/stm32/otg_fs.h | 2 + lib/usb/usb_f107.c | 69 ++++++++++++++++++------------- 2 files changed, 43 insertions(+), 28 deletions(-) diff --git a/include/libopencm3/stm32/otg_fs.h b/include/libopencm3/stm32/otg_fs.h index 34a7c852..e1d7a6aa 100644 --- a/include/libopencm3/stm32/otg_fs.h +++ b/include/libopencm3/stm32/otg_fs.h @@ -250,6 +250,7 @@ #define OTG_FS_DIEPCTL0_EPENA (1 << 31) #define OTG_FS_DIEPCTL0_EPDIS (1 << 30) /* Bits 29:28 - Reserved */ +#define OTG_FS_DIEPCTLX_SD0PID (1 << 28) #define OTG_FS_DIEPCTL0_SNAK (1 << 27) #define OTG_FS_DIEPCTL0_CNAK (1 << 26) #define OTG_FS_DIEPCTL0_TXFNUM_MASK (0xf << 22) @@ -270,6 +271,7 @@ #define OTG_FS_DOEPCTL0_EPENA (1 << 31) #define OTG_FS_DOEPCTL0_EPDIS (1 << 30) /* Bits 29:28 - Reserved */ +#define OTG_FS_DOEPCTLX_SD0PID (1 << 28) #define OTG_FS_DOEPCTL0_SNAK (1 << 27) #define OTG_FS_DOEPCTL0_CNAK (1 << 26) /* Bits 25:22 - Reserved */ diff --git a/lib/usb/usb_f107.c b/lib/usb/usb_f107.c index 63574431..9a20b0b3 100644 --- a/lib/usb/usb_f107.c +++ b/lib/usb/usb_f107.c @@ -25,6 +25,10 @@ #include +/* Receive FIFO size in 32-bit words */ +#define RX_FIFO_SIZE 128 +static uint16_t fifo_mem_top; + static void stm32f107_usbd_init(void); static void stm32f107_set_address(u8 addr); static void stm32f107_ep_setup(u8 addr, u8 type, u16 max_size, @@ -55,7 +59,6 @@ const struct _usbd_driver stm32f107_usb_driver = { /** Initialize the USB device controller hardware of the STM32. */ static void stm32f107_usbd_init(void) { - int i; /* TODO: Enable interrupts on Reset, Transfer, Suspend and Resume */ OTG_FS_GINTSTS = OTG_FS_GINTSTS_MMIS; @@ -82,9 +85,8 @@ static void stm32f107_usbd_init(void) /* Restart the phy clock */ OTG_FS_PCGCCTL = 0; - OTG_FS_GRXFSIZ = 128; - OTG_FS_GNPTXFSIZ = (128 << 16) | 128; - + OTG_FS_GRXFSIZ = RX_FIFO_SIZE; + fifo_mem_top = RX_FIFO_SIZE; /* Unmask interrupts for TX and RX */ OTG_FS_GINTMSK &= OTG_FS_GINTMSK_RXFLVLM; @@ -125,14 +127,21 @@ static void stm32f107_ep_setup(u8 addr, u8 type, u16 max_size, OTG_FS_DOEPTSIZ(0) = doeptsiz[0]; OTG_FS_DOEPCTL(0) |= OTG_FS_DOEPCTL0_EPENA | OTG_FS_DIEPCTL0_SNAK; + OTG_FS_GNPTXFSIZ = ((max_size / 4) << 16) | RX_FIFO_SIZE; + fifo_mem_top += max_size / 4; + return; } /* TODO: Configuration for other endpoints */ if (dir) { + OTG_FS_DIEPTXF(addr) = ((max_size / 4) << 16) | fifo_mem_top; + fifo_mem_top += max_size / 4; + OTG_FS_DIEPTSIZ(addr) = (max_size & OTG_FS_DIEPSIZ0_XFRSIZ_MASK); OTG_FS_DIEPCTL(addr) |= OTG_FS_DIEPCTL0_EPENA | OTG_FS_DIEPCTL0_SNAK | (type << 18) | + OTG_FS_DIEPCTL0_USBAEP | (addr << 22) | max_size; if (callback) { @@ -147,7 +156,8 @@ static void stm32f107_ep_setup(u8 addr, u8 type, u16 max_size, (max_size & OTG_FS_DIEPSIZ0_XFRSIZ_MASK); OTG_FS_DOEPTSIZ(addr) = doeptsiz[addr]; OTG_FS_DOEPCTL(addr) |= OTG_FS_DOEPCTL0_EPENA | - OTG_FS_DIEPCTL0_CNAK | (type << 18) | max_size; + OTG_FS_DOEPCTL0_USBAEP | OTG_FS_DIEPCTL0_CNAK | + (type << 18) | max_size; if (callback) { _usbd_device. @@ -159,7 +169,8 @@ static void stm32f107_ep_setup(u8 addr, u8 type, u16 max_size, static void stm32f107_endpoints_reset(void) { - /* TODO: Reset all endpoints. */ + /* The core resets the endpoints automatically on reset */ + fifo_mem_top = RX_FIFO_SIZE; } static void stm32f107_ep_stall_set(u8 addr, u8 stall) @@ -174,26 +185,29 @@ static void stm32f107_ep_stall_set(u8 addr, u8 stall) if(addr & 0x80) { addr &= 0x7F; - if(stall) + if(stall) { OTG_FS_DIEPCTL(addr) |= OTG_FS_DIEPCTL0_STALL; - else + } else { OTG_FS_DIEPCTL(addr) &= ~OTG_FS_DIEPCTL0_STALL; - /* TODO: Reset to DATA0 */ + OTG_FS_DIEPCTL(addr) |= OTG_FS_DIEPCTLX_SD0PID; + } } else { - if(stall) + if(stall) { OTG_FS_DOEPCTL(addr) |= OTG_FS_DOEPCTL0_STALL; - else + } else { OTG_FS_DOEPCTL(addr) &= ~OTG_FS_DOEPCTL0_STALL; - /* TODO: Reset to DATA0 */ + OTG_FS_DOEPCTL(addr) |= OTG_FS_DOEPCTLX_SD0PID; + } } } static u8 stm32f107_ep_stall_get(u8 addr) { - /* TODO: return 1 if STALL set. */ - (void)addr; - - return 0; + /* return non-zero if STALL set. */ + if(addr & 0x80) + return (OTG_FS_DIEPCTL(addr&0x7f) & OTG_FS_DIEPCTL0_STALL)?1:0; + else + return (OTG_FS_DOEPCTL(addr) & OTG_FS_DOEPCTL0_STALL)?1:0; } static u16 stm32f107_ep_write_packet(u8 addr, const void *buf, u16 len) @@ -249,11 +263,12 @@ static u16 stm32f107_ep_read_packet(u8 addr, void *buf, u16 len) static void stm32f107_poll(void) { - /* TODO: Read interrupt status register */ + /* Read interrupt status register */ u32 intsts = OTG_FS_GINTSTS; + int i; if (intsts & OTG_FS_GINTSTS_ENUMDNE) { - /* TODO: Handle USB RESET condition */ + /* Handle USB RESET condition */ OTG_FS_GINTSTS = OTG_FS_GINTSTS_ENUMDNE; _usbd_reset(); return; @@ -266,7 +281,8 @@ static void stm32f107_poll(void) u32 rxstsp = OTG_FS_GRXSTSP; u32 pktsts = rxstsp & OTG_FS_GRXSTSP_PKTSTS_MASK; if((pktsts != OTG_FS_GRXSTSP_PKTSTS_OUT) && - (pktsts != OTG_FS_GRXSTSP_PKTSTS_SETUP)) return; + (pktsts != OTG_FS_GRXSTSP_PKTSTS_SETUP)) + return; u8 ep = rxstsp & OTG_FS_GRXSTSP_EPNUM_MASK; u8 type; @@ -287,15 +303,12 @@ static void stm32f107_poll(void) * the XFRC bit must be checked in each OTG_FS_DIEPINT(x) */ /* TODO: Check on endpoint interrupt... */ - { - int i; - for (i = 0; i < 4; i++) { /* Iterate over endpoints */ - if(OTG_FS_DIEPINT(i) & OTG_FS_DIEPINTX_XFRC) { - /* Transfer complete */ - if (_usbd_device.user_callback_ctr[i][USB_TRANSACTION_IN]) - _usbd_device.user_callback_ctr[i][USB_TRANSACTION_IN] (i); - OTG_FS_DIEPINT(i) = OTG_FS_DIEPINTX_XFRC; - } + for (i = 0; i < 4; i++) { /* Iterate over endpoints */ + if(OTG_FS_DIEPINT(i) & OTG_FS_DIEPINTX_XFRC) { + /* Transfer complete */ + if (_usbd_device.user_callback_ctr[i][USB_TRANSACTION_IN]) + _usbd_device.user_callback_ctr[i][USB_TRANSACTION_IN] (i); + OTG_FS_DIEPINT(i) = OTG_FS_DIEPINTX_XFRC; } } From 692266bf6734b0361151a713c5b602696637de63 Mon Sep 17 00:00:00 2001 From: Gareth McMullin Date: Wed, 23 Mar 2011 17:36:17 +1300 Subject: [PATCH 07/12] Fixes CAN identifiers on send and receive. --- lib/stm32/can.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/stm32/can.c b/lib/stm32/can.c index 32661339..e571f8a2 100644 --- a/lib/stm32/can.c +++ b/lib/stm32/can.c @@ -211,6 +211,8 @@ int can_transmit(u32 canport, u32 id, bool ext, bool rtr, u8 length, u8 *data) if (ret == -1) return ret; + /* Clear stale register bits */ + CAN_TIxR(canport, mailbox) = 0; if (ext) { /* Set extended ID. */ CAN_TIxR(canport, mailbox) |= id << CAN_TIxR_EXID_SHIFT; @@ -219,15 +221,11 @@ int can_transmit(u32 canport, u32 id, bool ext, bool rtr, u8 length, u8 *data) } else { /* Set standard ID. */ CAN_TIxR(canport, mailbox) |= id << CAN_TIxR_STID_SHIFT; - /* Unset extended ID indicator bit. */ - CAN_TIxR(canport, mailbox) &= ~CAN_TIxR_IDE; } /* Set/clear remote transmission request bit. */ if (rtr) CAN_TIxR(canport, mailbox) |= CAN_TIxR_RTR; /* Set */ - else - CAN_TIxR(canport, mailbox) &= ~CAN_TIxR_RTR; /* Clear */ /* Set the DLC. */ CAN_TDTxR(canport, mailbox) &= 0xFFFFFFFF0; @@ -270,12 +268,12 @@ void can_receive(u32 canport, u8 fifo, bool release, u32 *id, bool *ext, if (CAN_RIxR(canport, fifo_id) & CAN_RIxR_IDE) { *ext = true; /* Get extended CAN ID. */ - *id = ((CAN_RIxR(canport, fifo_id) & CAN_RIxR_EXID_MASK) > + *id = ((CAN_RIxR(canport, fifo_id) & CAN_RIxR_EXID_MASK) >> CAN_RIxR_EXID_SHIFT); } else { *ext = false; /* Get standard CAN ID. */ - *id = ((CAN_RIxR(canport, fifo_id) & CAN_RIxR_STID_MASK) > + *id = ((CAN_RIxR(canport, fifo_id) & CAN_RIxR_STID_MASK) >> CAN_RIxR_STID_SHIFT); } From ef0ff192f769d6e043b4fdfc89cfbade61b9db14 Mon Sep 17 00:00:00 2001 From: Gareth McMullin Date: Wed, 30 Mar 2011 07:17:02 +1300 Subject: [PATCH 08/12] Added usbd_ep_nak_set() to force flow control on OUT endpoints. --- include/libopencm3/usb/usbd.h | 2 ++ lib/usb/usb.c | 6 ++++++ lib/usb/usb_f103.c | 21 ++++++++++++++++++++- lib/usb/usb_f107.c | 21 ++++++++++++++++++++- lib/usb/usb_private.h | 1 + 5 files changed, 49 insertions(+), 2 deletions(-) diff --git a/include/libopencm3/usb/usbd.h b/include/libopencm3/usb/usbd.h index fab63046..c73de072 100644 --- a/include/libopencm3/usb/usbd.h +++ b/include/libopencm3/usb/usbd.h @@ -65,6 +65,8 @@ extern u16 usbd_ep_read_packet(u8 addr, void *buf, u16 len); extern void usbd_ep_stall_set(u8 addr, u8 stall); extern u8 usbd_ep_stall_get(u8 addr); +extern void usbd_ep_nak_set(u8 addr, u8 nak); + /* Optional */ extern void usbd_cable_connect(u8 on); diff --git a/lib/usb/usb.c b/lib/usb/usb.c index eb8e6d82..59c526dd 100644 --- a/lib/usb/usb.c +++ b/lib/usb/usb.c @@ -127,3 +127,9 @@ u8 usbd_ep_stall_get(u8 addr) { return _usbd_device.driver->ep_stall_get(addr); } + +void usbd_ep_nak_set(u8 addr, u8 nak) +{ + _usbd_device.driver->ep_nak_set(addr, nak); +} + diff --git a/lib/usb/usb_f103.c b/lib/usb/usb_f103.c index ef97670f..19f645d1 100644 --- a/lib/usb/usb_f103.c +++ b/lib/usb/usb_f103.c @@ -30,10 +30,13 @@ static void stm32f103_ep_setup(u8 addr, u8 type, u16 max_size, static void stm32f103_endpoints_reset(void); static void stm32f103_ep_stall_set(u8 addr, u8 stall); static u8 stm32f103_ep_stall_get(u8 addr); +static void stm32f103_ep_nak_set(u8 addr, u8 nak); static u16 stm32f103_ep_write_packet(u8 addr, const void *buf, u16 len); static u16 stm32f103_ep_read_packet(u8 addr, void *buf, u16 len); static void stm32f103_poll(void); +static u8 force_nak[8]; + const struct _usbd_driver stm32f103_usb_driver = { .init = stm32f103_usbd_init, .set_address = stm32f103_set_address, @@ -41,6 +44,7 @@ const struct _usbd_driver stm32f103_usb_driver = { .ep_reset = stm32f103_endpoints_reset, .ep_stall_set = stm32f103_ep_stall_set, .ep_stall_get = stm32f103_ep_stall_get, + .ep_nak_set = stm32f103_ep_nak_set, .ep_write_packet = stm32f103_ep_write_packet, .ep_read_packet = stm32f103_ep_read_packet, .poll = stm32f103_poll, @@ -177,6 +181,20 @@ static u8 stm32f103_ep_stall_get(u8 addr) return 0; } +static void stm32f103_ep_nak_set(u8 addr, u8 nak) +{ + /* It does not make sence to force NAK on IN endpoints */ + if(addr & 0x80) + return; + + force_nak[addr] = nak; + + if(nak) + USB_SET_EP_RX_STAT(addr, USB_EP_RX_STAT_NAK); + else + USB_SET_EP_RX_STAT(addr, USB_EP_RX_STAT_VALID); +} + /** * Copy a data buffer to packet memory. * @@ -236,7 +254,8 @@ static u16 stm32f103_ep_read_packet(u8 addr, void *buf, u16 len) usb_copy_from_pm(buf, USB_GET_EP_RX_BUFF(addr), len); USB_CLR_EP_RX_CTR(addr); - USB_SET_EP_RX_STAT(addr, USB_EP_RX_STAT_VALID); + if(!force_nak[addr]) + USB_SET_EP_RX_STAT(addr, USB_EP_RX_STAT_VALID); return len; } diff --git a/lib/usb/usb_f107.c b/lib/usb/usb_f107.c index 9a20b0b3..f106a053 100644 --- a/lib/usb/usb_f107.c +++ b/lib/usb/usb_f107.c @@ -29,6 +29,8 @@ #define RX_FIFO_SIZE 128 static uint16_t fifo_mem_top; +static u8 force_nak[4]; + static void stm32f107_usbd_init(void); static void stm32f107_set_address(u8 addr); static void stm32f107_ep_setup(u8 addr, u8 type, u16 max_size, @@ -36,6 +38,7 @@ static void stm32f107_ep_setup(u8 addr, u8 type, u16 max_size, static void stm32f107_endpoints_reset(void); static void stm32f107_ep_stall_set(u8 addr, u8 stall); static u8 stm32f107_ep_stall_get(u8 addr); +static void stm32f107_ep_nak_set(u8 addr, u8 nak); static u16 stm32f107_ep_write_packet(u8 addr, const void *buf, u16 len); static u16 stm32f107_ep_read_packet(u8 addr, void *buf, u16 len); static void stm32f107_poll(void); @@ -51,6 +54,7 @@ const struct _usbd_driver stm32f107_usb_driver = { .ep_reset = stm32f107_endpoints_reset, .ep_stall_set = stm32f107_ep_stall_set, .ep_stall_get = stm32f107_ep_stall_get, + .ep_nak_set = stm32f107_ep_nak_set, .ep_write_packet = stm32f107_ep_write_packet, .ep_read_packet = stm32f107_ep_read_packet, .poll = stm32f107_poll, @@ -210,6 +214,20 @@ static u8 stm32f107_ep_stall_get(u8 addr) return (OTG_FS_DOEPCTL(addr) & OTG_FS_DOEPCTL0_STALL)?1:0; } +static void stm32f107_ep_nak_set(u8 addr, u8 nak) +{ + /* It does not make sence to force NAK on IN endpoints */ + if(addr & 0x80) + return; + + force_nak[addr] = nak; + + if(nak) + OTG_FS_DOEPCTL(addr) |= OTG_FS_DOEPCTL0_SNAK; + else + OTG_FS_DOEPCTL(addr) |= OTG_FS_DOEPCTL0_CNAK; +} + static u16 stm32f107_ep_write_packet(u8 addr, const void *buf, u16 len) { const u32 *buf32 = buf; @@ -256,7 +274,8 @@ static u16 stm32f107_ep_read_packet(u8 addr, void *buf, u16 len) } OTG_FS_DOEPTSIZ(addr) = doeptsiz[addr]; - OTG_FS_DOEPCTL(addr) |= OTG_FS_DOEPCTL0_EPENA | OTG_FS_DOEPCTL0_CNAK; + OTG_FS_DOEPCTL(addr) |= OTG_FS_DOEPCTL0_EPENA | + (force_nak[addr] ? OTG_FS_DOEPCTL0_SNAK : OTG_FS_DOEPCTL0_CNAK); return len; } diff --git a/lib/usb/usb_private.h b/lib/usb/usb_private.h index 1bc6b3fc..40d59a11 100644 --- a/lib/usb/usb_private.h +++ b/lib/usb/usb_private.h @@ -78,6 +78,7 @@ struct _usbd_driver { void (*ep_setup)(u8 addr, u8 type, u16 max_size, void (*cb)(u8 ep)); void (*ep_reset)(void); void (*ep_stall_set)(u8 addr, u8 stall); + void (*ep_nak_set)(u8 addr, u8 nak); u8 (*ep_stall_get)(u8 addr); u16 (*ep_write_packet)(u8 addr, const void *buf, u16 len); u16 (*ep_read_packet)(u8 addr, void *buf, u16 len); From 3d820c6ed7f61d4cac40a5008f58a212c9a06860 Mon Sep 17 00:00:00 2001 From: Gareth McMullin Date: Mon, 4 Apr 2011 17:39:05 +1200 Subject: [PATCH 09/12] Fixed stm32f107 usb driver to allow calling usbd_poll from irq handler. --- lib/usb/usb_f107.c | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/lib/usb/usb_f107.c b/lib/usb/usb_f107.c index f106a053..ee83b652 100644 --- a/lib/usb/usb_f107.c +++ b/lib/usb/usb_f107.c @@ -63,8 +63,6 @@ const struct _usbd_driver stm32f107_usb_driver = { /** Initialize the USB device controller hardware of the STM32. */ static void stm32f107_usbd_init(void) { - /* TODO: Enable interrupts on Reset, Transfer, Suspend and Resume */ - OTG_FS_GINTSTS = OTG_FS_GINTSTS_MMIS; /* WARNING: Undocumented! Select internal PHY */ @@ -93,7 +91,15 @@ static void stm32f107_usbd_init(void) fifo_mem_top = RX_FIFO_SIZE; /* Unmask interrupts for TX and RX */ - OTG_FS_GINTMSK &= OTG_FS_GINTMSK_RXFLVLM; + OTG_FS_GAHBCFG |= OTG_FS_GAHBCFG_GINT; + OTG_FS_GINTMSK = OTG_FS_GINTMSK_ENUMDNEM | + OTG_FS_GINTMSK_RXFLVLM | + OTG_FS_GINTMSK_IEPINT | + OTG_FS_GINTMSK_USBSUSPM | + OTG_FS_GINTMSK_WUIM | + OTG_FS_GINTMSK_SOFM; + OTG_FS_DAINTMSK = 0xF; + OTG_FS_DIEPMSK = OTG_FS_DIEPMSK_XFRCM; } static void stm32f107_set_address(u8 addr) @@ -104,7 +110,7 @@ static void stm32f107_set_address(u8 addr) static void stm32f107_ep_setup(u8 addr, u8 type, u16 max_size, void (*callback) (u8 ep)) { - /* TODO: Configure endpoint address and type. + /* Configure endpoint address and type. * Allocate FIFO memory for endpoint. * Install callback funciton. */ @@ -137,7 +143,6 @@ static void stm32f107_ep_setup(u8 addr, u8 type, u16 max_size, return; } - /* TODO: Configuration for other endpoints */ if (dir) { OTG_FS_DIEPTXF(addr) = ((max_size / 4) << 16) | fifo_mem_top; fifo_mem_top += max_size / 4; @@ -293,7 +298,6 @@ static void stm32f107_poll(void) return; } - /* TODO: Handle transfer complete condition */ /* Note: RX and TX handled differently in this device. */ if (intsts & OTG_FS_GINTSTS_RXFLVL) { /* Receive FIFO non-empty */ @@ -315,13 +319,11 @@ static void stm32f107_poll(void) if (_usbd_device.user_callback_ctr[ep][type]) _usbd_device.user_callback_ctr[ep][type] (ep); - /* TODO: clear any interrupt flag */ } /* There is no global interrupt flag for transmit complete. * the XFRC bit must be checked in each OTG_FS_DIEPINT(x) */ - /* TODO: Check on endpoint interrupt... */ for (i = 0; i < 4; i++) { /* Iterate over endpoints */ if(OTG_FS_DIEPINT(i) & OTG_FS_DIEPINTX_XFRC) { /* Transfer complete */ @@ -331,20 +333,19 @@ static void stm32f107_poll(void) } } - /* TODO: Handle suspend condition */ - if (0) { - /* TODO: Clear suspend interrupt flag */ + if (intsts & OTG_FS_GINTSTS_USBSUSP) { if (_usbd_device.user_callback_suspend) _usbd_device.user_callback_suspend(); + OTG_FS_GINTSTS = OTG_FS_GINTSTS_USBSUSP; } - /* TODO: Handle wakeup condition */ - if (0) { - /* TODO: Clear wakeup interrupt flag */ + if (intsts & OTG_FS_GINTSTS_WKUPINT) { if (_usbd_device.user_callback_resume) _usbd_device.user_callback_resume(); + OTG_FS_GINTSTS = OTG_FS_GINTSTS_WKUPINT; } - /* TODO: Handle SOF condition */ + if (intsts & OTG_FS_GINTSTS_SOF) + OTG_FS_GINTSTS = OTG_FS_GINTSTS_SOF; } From cd88855d133c9286fd20caac2b562729258d6f7c Mon Sep 17 00:00:00 2001 From: Gareth McMullin Date: Sun, 24 Apr 2011 14:05:37 +1200 Subject: [PATCH 10/12] Delete example dependencies on 'make clean' --- examples/lm3s/Makefile.include | 1 + examples/lpc13xx/Makefile.include | 1 + examples/stm32/Makefile.include | 1 + 3 files changed, 3 insertions(+) diff --git a/examples/lm3s/Makefile.include b/examples/lm3s/Makefile.include index 89c12797..764a145a 100644 --- a/examples/lm3s/Makefile.include +++ b/examples/lm3s/Makefile.include @@ -85,6 +85,7 @@ flash: $(BINARY).flash clean: $(Q)rm -f *.o + $(Q)rm -f *.d $(Q)rm -f *.elf $(Q)rm -f *.bin $(Q)rm -f *.hex diff --git a/examples/lpc13xx/Makefile.include b/examples/lpc13xx/Makefile.include index 1f2deba6..c9ae180a 100644 --- a/examples/lpc13xx/Makefile.include +++ b/examples/lpc13xx/Makefile.include @@ -85,6 +85,7 @@ flash: $(BINARY).flash clean: $(Q)rm -f *.o + $(Q)rm -f *.d $(Q)rm -f *.elf $(Q)rm -f *.bin $(Q)rm -f *.hex diff --git a/examples/stm32/Makefile.include b/examples/stm32/Makefile.include index cdaff1d3..acb78e8e 100644 --- a/examples/stm32/Makefile.include +++ b/examples/stm32/Makefile.include @@ -85,6 +85,7 @@ flash: $(BINARY).flash clean: $(Q)rm -f *.o + $(Q)rm -f *.d $(Q)rm -f *.elf $(Q)rm -f *.bin $(Q)rm -f *.hex From 7434d50fda0ed9764056648319876e1032d1e7ba Mon Sep 17 00:00:00 2001 From: Gareth McMullin Date: Sun, 24 Apr 2011 14:28:16 +1200 Subject: [PATCH 11/12] Check index bounds on usb string descriptor request. --- lib/usb/usb_standard.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/usb/usb_standard.c b/lib/usb/usb_standard.c index 7bac2160..aa920109 100644 --- a/lib/usb/usb_standard.c +++ b/lib/usb/usb_standard.c @@ -98,6 +98,11 @@ static int usb_standard_get_descriptor(struct usb_setup_data *req, if (!_usbd_device.strings) return 0; /* Device doesn't support strings. */ + /* Check that string index is in range */ + for(i = 0; i <= (req->wValue & 0xff); i++) + if(_usbd_device.strings[i] == NULL) + return 0; + sd->bLength = strlen(_usbd_device.strings[req->wValue & 0xff]) * 2 + 2; sd->bDescriptorType = USB_DT_STRING; From 3ac0ae839d710de6552f617934bed2f5f9629bec Mon Sep 17 00:00:00 2001 From: Gareth McMullin Date: Sun, 24 Apr 2011 14:31:56 +1200 Subject: [PATCH 12/12] Added extra interrupt vectors for STM32 connectivity line. --- include/libopencm3/stm32/nvic.h | 8 ++++++++ lib/stm32/vector.c | 26 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/include/libopencm3/stm32/nvic.h b/include/libopencm3/stm32/nvic.h index ce8132fd..d29c425f 100644 --- a/include/libopencm3/stm32/nvic.h +++ b/include/libopencm3/stm32/nvic.h @@ -138,6 +138,14 @@ #define NVIC_DMA2_CHANNEL2_IRQ 57 #define NVIC_DMA2_CHANNEL3_IRQ 58 #define NVIC_DMA2_CHANNEL4_5_IRQ 59 +#define NVIC_DMA2_CHANNEL5_IRQ 60 +#define NVIC_ETH_IRQ 61 +#define NVIC_ETH_WKUP_IRQ 62 +#define NVIC_CAN2_TX_IRQ 63 +#define NVIC_CAN2_RX0_IRQ 64 +#define NVIC_CAN2_RX1_IRQ 65 +#define NVIC_CAN2_SCE_IRQ 66 +#define NVIC_OTG_FS_IRQ 67 /* --- NVIC functions ------------------------------------------------------ */ diff --git a/lib/stm32/vector.c b/lib/stm32/vector.c index 4d3d5103..39bd9a13 100644 --- a/lib/stm32/vector.c +++ b/lib/stm32/vector.c @@ -96,6 +96,15 @@ void WEAK dma2_channel1_isr(void); void WEAK dma2_channel2_isr(void); void WEAK dma2_channel3_isr(void); void WEAK dma2_channel4_5_isr(void); +void WEAK dma2_channel5_isr(void); +void WEAK eth_isr(void); +void WEAK eth_wkup_isr(void); +void WEAK can2_tx_isr(void); +void WEAK can2_rx0_isr(void); +void WEAK can2_rx1_isr(void); +void WEAK can2_sce_isr(void); +void WEAK otg_fs_isr(void); + __attribute__ ((section(".vectors"))) void (*const vector_table[]) (void) = { @@ -172,6 +181,14 @@ void (*const vector_table[]) (void) = { dma2_channel2_isr, dma2_channel3_isr, dma2_channel4_5_isr, + dma2_channel5_isr, + eth_isr, + eth_wkup_isr, + can2_tx_isr, + can2_rx0_isr, + can2_rx1_isr, + can2_sce_isr, + otg_fs_isr, }; void reset_handler(void) @@ -268,3 +285,12 @@ void null_handler(void) #pragma weak dma2_channel2_isr = null_handler #pragma weak dma2_channel3_isr = null_handler #pragma weak dma2_channel4_5_isr = null_handler +#pragma weak dma2_channel5_isr +#pragma weak eth_isr = null_handler +#pragma weak eth_wkup_isr = null_handler +#pragma weak can2_tx_isr = null_handler +#pragma weak can2_rx0_isr = null_handler +#pragma weak can2_rx1_isr = null_handler +#pragma weak can2_sce_isr = null_handler +#pragma weak otg_fs_isr = null_handler +