From bcce98da54b047a15ef0abf41e0c7794f29f0439 Mon Sep 17 00:00:00 2001 From: Uwe Hermann Date: Thu, 30 Dec 2010 01:52:44 +0100 Subject: Move STM32 examples to examples/stm32 subdir. --- examples/stm32/stm32-h103/Makefile | 68 +++++ examples/stm32/stm32-h103/fancyblink/Makefile | 23 ++ examples/stm32/stm32-h103/fancyblink/README | 10 + examples/stm32/stm32-h103/fancyblink/fancyblink.c | 54 ++++ examples/stm32/stm32-h103/fancyblink/fancyblink.ld | 31 +++ examples/stm32/stm32-h103/miniblink/Makefile | 23 ++ examples/stm32/stm32-h103/miniblink/README | 10 + examples/stm32/stm32-h103/miniblink/miniblink.c | 66 +++++ examples/stm32/stm32-h103/miniblink/miniblink.ld | 31 +++ examples/stm32/stm32-h103/spi/Makefile | 23 ++ examples/stm32/stm32-h103/spi/README | 8 + examples/stm32/stm32-h103/spi/spi.c | 50 ++++ examples/stm32/stm32-h103/spi/spi.ld | 31 +++ examples/stm32/stm32-h103/usart/Makefile | 23 ++ examples/stm32/stm32-h103/usart/README | 13 + examples/stm32/stm32-h103/usart/usart.c | 82 ++++++ examples/stm32/stm32-h103/usart/usart.ld | 31 +++ examples/stm32/stm32-h103/usb_cdcacm/Makefile | 23 ++ examples/stm32/stm32-h103/usb_cdcacm/README | 7 + examples/stm32/stm32-h103/usb_cdcacm/cdcacm.c | 244 +++++++++++++++++ examples/stm32/stm32-h103/usb_cdcacm/cdcacm.ld | 32 +++ examples/stm32/stm32-h103/usb_dfu/Makefile | 23 ++ examples/stm32/stm32-h103/usb_dfu/README | 7 + examples/stm32/stm32-h103/usb_dfu/usbdfu.c | 295 +++++++++++++++++++++ examples/stm32/stm32-h103/usb_dfu/usbdfu.ld | 29 ++ examples/stm32/stm32-h103/usb_hid/Makefile | 23 ++ examples/stm32/stm32-h103/usb_hid/README | 7 + examples/stm32/stm32-h103/usb_hid/usbhid.c | 273 +++++++++++++++++++ examples/stm32/stm32-h103/usb_hid/usbhid.ld | 32 +++ examples/stm32/stm32-h103/usb_iap/Makefile | 23 ++ examples/stm32/stm32-h103/usb_iap/README | 8 + examples/stm32/stm32-h103/usb_iap/usbiap.c | 295 +++++++++++++++++++++ examples/stm32/stm32-h103/usb_iap/usbiap.ld | 32 +++ 33 files changed, 1930 insertions(+) create mode 100644 examples/stm32/stm32-h103/Makefile create mode 100644 examples/stm32/stm32-h103/fancyblink/Makefile create mode 100644 examples/stm32/stm32-h103/fancyblink/README create mode 100644 examples/stm32/stm32-h103/fancyblink/fancyblink.c create mode 100644 examples/stm32/stm32-h103/fancyblink/fancyblink.ld create mode 100644 examples/stm32/stm32-h103/miniblink/Makefile create mode 100644 examples/stm32/stm32-h103/miniblink/README create mode 100644 examples/stm32/stm32-h103/miniblink/miniblink.c create mode 100644 examples/stm32/stm32-h103/miniblink/miniblink.ld create mode 100644 examples/stm32/stm32-h103/spi/Makefile create mode 100644 examples/stm32/stm32-h103/spi/README create mode 100644 examples/stm32/stm32-h103/spi/spi.c create mode 100644 examples/stm32/stm32-h103/spi/spi.ld create mode 100644 examples/stm32/stm32-h103/usart/Makefile create mode 100644 examples/stm32/stm32-h103/usart/README create mode 100644 examples/stm32/stm32-h103/usart/usart.c create mode 100644 examples/stm32/stm32-h103/usart/usart.ld create mode 100644 examples/stm32/stm32-h103/usb_cdcacm/Makefile create mode 100644 examples/stm32/stm32-h103/usb_cdcacm/README create mode 100644 examples/stm32/stm32-h103/usb_cdcacm/cdcacm.c create mode 100644 examples/stm32/stm32-h103/usb_cdcacm/cdcacm.ld create mode 100644 examples/stm32/stm32-h103/usb_dfu/Makefile create mode 100644 examples/stm32/stm32-h103/usb_dfu/README create mode 100644 examples/stm32/stm32-h103/usb_dfu/usbdfu.c create mode 100644 examples/stm32/stm32-h103/usb_dfu/usbdfu.ld create mode 100644 examples/stm32/stm32-h103/usb_hid/Makefile create mode 100644 examples/stm32/stm32-h103/usb_hid/README create mode 100644 examples/stm32/stm32-h103/usb_hid/usbhid.c create mode 100644 examples/stm32/stm32-h103/usb_hid/usbhid.ld create mode 100644 examples/stm32/stm32-h103/usb_iap/Makefile create mode 100644 examples/stm32/stm32-h103/usb_iap/README create mode 100644 examples/stm32/stm32-h103/usb_iap/usbiap.c create mode 100644 examples/stm32/stm32-h103/usb_iap/usbiap.ld (limited to 'examples/stm32/stm32-h103') diff --git a/examples/stm32/stm32-h103/Makefile b/examples/stm32/stm32-h103/Makefile new file mode 100644 index 0000000..bd3a2d8 --- /dev/null +++ b/examples/stm32/stm32-h103/Makefile @@ -0,0 +1,68 @@ +## +## This file is part of the libopenstm32 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 usb_cdcacm usb_hid + +miniblink: + @printf " BUILD examples/stm32-h103/miniblink\n" + $(Q)$(MAKE) -C miniblink + +fancyblink: + @printf " BUILD examples/stm32-h103/fancyblink\n" + $(Q)$(MAKE) -C fancyblink + +usart: + @printf " BUILD examples/stm32-h103/usart\n" + $(Q)$(MAKE) -C usart + +spi: + @printf " BUILD examples/stm32-h103/spi\n" + $(Q)$(MAKE) -C spi + +usb_cdcacm: + @printf " BUILD examples/stm32-h103/usb_cdcacm\n" + $(Q)$(MAKE) -C usb_cdcacm + +usb_hid: + @printf " BUILD examples/stm32-h103/usb_hid\n" + $(Q)$(MAKE) -C usb_hid + +clean: + @printf " CLEAN examples/stm32-h103/miniblink\n" + $(Q)$(MAKE) -C miniblink clean + @printf " CLEAN examples/stm32-h103/fancyblink\n" + $(Q)$(MAKE) -C fancyblink clean + @printf " CLEAN examples/stm32-h103/usart\n" + $(Q)$(MAKE) -C usart clean + @printf " CLEAN examples/stm32-h103/spi\n" + $(Q)$(MAKE) -C spi clean + @printf " CLEAN examples/stm32-h103/usb_cdcacm\n" + $(Q)$(MAKE) -C usb_cdcacm clean + @printf " CLEAN examples/stm32-h103/usb_hid\n" + $(Q)$(MAKE) -C usb_hid clean + +.PHONY: miniblink fancyblink usart spi usb_cdcacm usb_hid clean + diff --git a/examples/stm32/stm32-h103/fancyblink/Makefile b/examples/stm32/stm32-h103/fancyblink/Makefile new file mode 100644 index 0000000..09aff6f --- /dev/null +++ b/examples/stm32/stm32-h103/fancyblink/Makefile @@ -0,0 +1,23 @@ +## +## This file is part of the libopenstm32 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 = fancyblink + +include ../../Makefile.include + diff --git a/examples/stm32/stm32-h103/fancyblink/README b/examples/stm32/stm32-h103/fancyblink/README new file mode 100644 index 0000000..791af39 --- /dev/null +++ b/examples/stm32/stm32-h103/fancyblink/README @@ -0,0 +1,10 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This is small LED blinking example program using libopenstm32. + +It's intended for the ST STM32-based Olimex STM32-H103 eval board (see +http://olimex.com/dev/stm32-h103.html for details). It should blink +the LED on the board. + diff --git a/examples/stm32/stm32-h103/fancyblink/fancyblink.c b/examples/stm32/stm32-h103/fancyblink/fancyblink.c new file mode 100644 index 0000000..014ae90 --- /dev/null +++ b/examples/stm32/stm32-h103/fancyblink/fancyblink.c @@ -0,0 +1,54 @@ +/* + * This file is part of the libopenstm32 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 . + */ + +#include +#include + +/* Set STM32 to 72 MHz. */ +void clock_setup(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + + /* Enable GPIOC clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + +} + +void gpio_setup(void) +{ + /* Set GPIO12 (in GPIO port C) to 'output push-pull'. */ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO12); +} + +int main(void) +{ + int i; + + clock_setup(); + gpio_setup(); + + /* Blink the LED (PC12) on the board. */ + while (1) { + gpio_toggle(GPIOC, GPIO12); /* LED on/off */ + for (i = 0; i < 80000; i++); /* Wait (needs -O0 CFLAGS). */ + } + + return 0; +} diff --git a/examples/stm32/stm32-h103/fancyblink/fancyblink.ld b/examples/stm32/stm32-h103/fancyblink/fancyblink.ld new file mode 100644 index 0000000..b3bb9a4 --- /dev/null +++ b/examples/stm32/stm32-h103/fancyblink/fancyblink.ld @@ -0,0 +1,31 @@ +/* + * This file is part of the libopenstm32 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-H103 (STM32F103RBT6, 128K flash, 20K RAM). */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K +} + +/* Include the common ld script from libopenstm32. */ +INCLUDE libopenstm32.ld + diff --git a/examples/stm32/stm32-h103/miniblink/Makefile b/examples/stm32/stm32-h103/miniblink/Makefile new file mode 100644 index 0000000..cbbded6 --- /dev/null +++ b/examples/stm32/stm32-h103/miniblink/Makefile @@ -0,0 +1,23 @@ +## +## This file is part of the libopenstm32 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 = miniblink + +include ../../Makefile.include + diff --git a/examples/stm32/stm32-h103/miniblink/README b/examples/stm32/stm32-h103/miniblink/README new file mode 100644 index 0000000..66359f4 --- /dev/null +++ b/examples/stm32/stm32-h103/miniblink/README @@ -0,0 +1,10 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This is the smallest-possible example program using libopenstm32. + +It's intended for the ST STM32-based Olimex STM32-H103 eval board (see +http://olimex.com/dev/stm32-h103.html for details). It should blink +the LED on the board. + diff --git a/examples/stm32/stm32-h103/miniblink/miniblink.c b/examples/stm32/stm32-h103/miniblink/miniblink.c new file mode 100644 index 0000000..dc48200 --- /dev/null +++ b/examples/stm32/stm32-h103/miniblink/miniblink.c @@ -0,0 +1,66 @@ +/* + * This file is part of the libopenstm32 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 . + */ + +#include +#include + +void gpio_setup(void) +{ + /* Enable GPIOC clock. */ + /* Manually: */ + // RCC_APB2ENR |= IOPCEN; + /* Using API functions: */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + /* Set GPIO12 (in GPIO port C) to 'output push-pull'. */ + /* Manually: */ + // GPIOC_CRH = (GPIO_CNF_OUTPUT_PUSHPULL << (((12 - 8) * 4) + 2)); + // GPIOC_CRH |= (GPIO_MODE_OUTPUT_2_MHZ << ((12 - 8) * 4)); + /* Using API functions: */ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO12); +} + +int main(void) +{ + int i; + + gpio_setup(); + + /* Blink the LED (PC12) on the board. */ + while (1) { + /* Manually: */ + // GPIOC_BSRR = GPIO12; /* LED off */ + // for (i = 0; i < 80000; i++); /* Wait (needs -O0 CFLAGS). */ + // GPIOC_BRR = GPIO12; /* LED on */ + // for (i = 0; i < 80000; i++); /* Wait (needs -O0 CFLAGS). */ + + /* Using API functions gpio_set()/gpio_clear(): */ + // gpio_set(GPIOC, GPIO12); /* LED off */ + // for (i = 0; i < 80000; i++); /* Wait (needs -O0 CFLAGS). */ + // gpio_clear(GPIOC, GPIO12); /* LED on */ + // for (i = 0; i < 80000; i++); /* Wait (needs -O0 CFLAGS). */ + + /* Using API function gpio_toggle(): */ + gpio_toggle(GPIOC, GPIO12); /* LED on/off */ + for (i = 0; i < 80000; i++); /* Wait (needs -O0 CFLAGS). */ + } + + return 0; +} diff --git a/examples/stm32/stm32-h103/miniblink/miniblink.ld b/examples/stm32/stm32-h103/miniblink/miniblink.ld new file mode 100644 index 0000000..b3bb9a4 --- /dev/null +++ b/examples/stm32/stm32-h103/miniblink/miniblink.ld @@ -0,0 +1,31 @@ +/* + * This file is part of the libopenstm32 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-H103 (STM32F103RBT6, 128K flash, 20K RAM). */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K +} + +/* Include the common ld script from libopenstm32. */ +INCLUDE libopenstm32.ld + diff --git a/examples/stm32/stm32-h103/spi/Makefile b/examples/stm32/stm32-h103/spi/Makefile new file mode 100644 index 0000000..a63e92d --- /dev/null +++ b/examples/stm32/stm32-h103/spi/Makefile @@ -0,0 +1,23 @@ +## +## This file is part of the libopenstm32 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 . +## + +BINARY = spi + +include ../../Makefile.include + diff --git a/examples/stm32/stm32-h103/spi/README b/examples/stm32/stm32-h103/spi/README new file mode 100644 index 0000000..d4714c2 --- /dev/null +++ b/examples/stm32/stm32-h103/spi/README @@ -0,0 +1,8 @@ +------------------------------------------------------------------------------- +README +------------------------------------------------------------------------------- + +This example program repeatedly sends characters on SPI1 on the ST STM32-based +Olimex STM32-H103 eval board (see http://olimex.com/dev/stm32-h103.html +for details). + diff --git a/examples/stm32/stm32-h103/spi/spi.c b/examples/stm32/stm32-h103/spi/spi.c new file mode 100644 index 0000000..b174130 --- /dev/null +++ b/examples/stm32/stm32-h103/spi/spi.c @@ -0,0 +1,50 @@ +/* + * This file is part of the libopenstm32 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 . + */ + +#include +#include + +void clock_setup(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); +} + +void spi_setup(void) +{ + /* TODO */ +} + +void gpio_setup(void) +{ + /* TODO */ +} + +int main(void) +{ + clock_setup(); + gpio_setup(); + spi_setup(); + + while (1) { + spi_write(SPI1, 0xaa); + spi_write(SPI1, 0x55); + } + + return 0; +} diff --git a/examples/stm32/stm32-h103/spi/spi.ld b/examples/stm32/stm32-h103/spi/spi.ld new file mode 100644 index 0000000..5ae60b8 --- /dev/null +++ b/examples/stm32/stm32-h103/spi/spi.ld @@ -0,0 +1,31 @@ +/* + * This file is part of the libopenstm32 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 . + */ + +/* Linker script for Olimex STM32-H103 (STM32F103RBT6, 128K flash, 20K RAM). */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K +} + +/* Include the common ld script from libopenstm32. */ +INCLUDE libopenstm32.ld + diff --git a/examples/stm32/stm32-h103/usart/Makefile b/examples/stm32/stm32-h103/usart/Makefile new file mode 100644 index 0000000..75050cd --- /dev/null +++ b/examples/stm32/stm32-h103/usart/Makefile @@ -0,0 +1,23 @@ +## +## This file is part of the libopenstm32 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 = usart + +include ../../Makefile.include + diff --git a/examples/stm32/stm32-h103/usart/README b/examples/stm32/stm32-h103/usart/README new file mode 100644 index 0000000..ecd7269 --- /dev/null +++ b/examples/stm32/stm32-h103/usart/README @@ -0,0 +1,13 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example program sends some characters on USART3 on the ST STM32-based +Olimex STM32-H103 eval board (see http://olimex.com/dev/stm32-h103.html +for details). + +The terminal settings for the receiving device/PC are 38400 8n1. + +The sending is done in a blocking way in the code, see the usart_irq example +for a more elaborate USART example. + diff --git a/examples/stm32/stm32-h103/usart/usart.c b/examples/stm32/stm32-h103/usart/usart.c new file mode 100644 index 0000000..4470348 --- /dev/null +++ b/examples/stm32/stm32-h103/usart/usart.c @@ -0,0 +1,82 @@ +/* + * This file is part of the libopenstm32 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 . + */ + +#include +#include +#include + +void clock_setup(void) +{ + rcc_clock_setup_in_hse_8mhz_out_72mhz(); + + /* Enable GPIOC clock. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + /* Enable clocks for GPIO port B (for GPIO_USART3_TX) and USART3. */ + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN); + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USART3EN); +} + +void usart_setup(void) +{ + /* Setup GPIO pin GPIO_USART3_TX/GPIO10 on GPIO port B for transmit. */ + gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART3_TX); + + /* Setup UART parameters. */ + usart_set_baudrate(USART3, 38400); + usart_set_databits(USART3, 8); + usart_set_stopbits(USART3, USART_STOPBITS_1); + usart_set_mode(USART3, USART_MODE_TX); + usart_set_parity(USART3, USART_PARITY_NONE); + usart_set_flow_control(USART3, USART_FLOWCONTROL_NONE); + + /* Finally enable the USART. */ + usart_enable(USART3); +} + +void gpio_setup(void) +{ + /* Set GPIO12 (in GPIO port C) to 'output push-pull'. */ + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO12); +} + +int main(void) +{ + int i, j = 0, c = 0; + + clock_setup(); + gpio_setup(); + usart_setup(); + + /* Blink the LED (PC12) on the board with every transmitted byte. */ + while (1) { + gpio_toggle(GPIOC, GPIO12); /* LED on/off */ + usart_send_blocking(USART3, c + '0'); /* Send one byte on USART3. */ + c = (c == 9) ? 0 : c + 1; /* Increment c. */ + if ((j++ % 80) == 0) { /* Newline after line full. */ + usart_send_blocking(USART3, '\r'); + usart_send_blocking(USART3, '\n'); + } + for (i = 0; i < 80000; i++); /* Wait (needs -O0 CFLAGS). */ + } + + return 0; +} diff --git a/examples/stm32/stm32-h103/usart/usart.ld b/examples/stm32/stm32-h103/usart/usart.ld new file mode 100644 index 0000000..b3bb9a4 --- /dev/null +++ b/examples/stm32/stm32-h103/usart/usart.ld @@ -0,0 +1,31 @@ +/* + * This file is part of the libopenstm32 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-H103 (STM32F103RBT6, 128K flash, 20K RAM). */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K +} + +/* Include the common ld script from libopenstm32. */ +INCLUDE libopenstm32.ld + diff --git a/examples/stm32/stm32-h103/usb_cdcacm/Makefile b/examples/stm32/stm32-h103/usb_cdcacm/Makefile new file mode 100644 index 0000000..ab55ba2 --- /dev/null +++ b/examples/stm32/stm32-h103/usb_cdcacm/Makefile @@ -0,0 +1,23 @@ +## +## This file is part of the libopenstm32 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 = cdcacm + +include ../../Makefile.include + diff --git a/examples/stm32/stm32-h103/usb_cdcacm/README b/examples/stm32/stm32-h103/usb_cdcacm/README new file mode 100644 index 0000000..2f1ee4b --- /dev/null +++ b/examples/stm32/stm32-h103/usb_cdcacm/README @@ -0,0 +1,7 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example implements a USB CDC-ACM device (aka Virtual Serial Port) +to demonstrate the use of the USB device stack. + diff --git a/examples/stm32/stm32-h103/usb_cdcacm/cdcacm.c b/examples/stm32/stm32-h103/usb_cdcacm/cdcacm.c new file mode 100644 index 0000000..305ad43 --- /dev/null +++ b/examples/stm32/stm32-h103/usb_cdcacm/cdcacm.c @@ -0,0 +1,244 @@ +/* + * This file is part of the libopenstm32 project. + * + * Copyright (C) 2010 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 +#include +#include + +static const struct usb_device_descriptor dev = { + .bLength = USB_DT_DEVICE_SIZE, + .bDescriptorType = USB_DT_DEVICE, + .bcdUSB = 0x0200, + .bDeviceClass = USB_CLASS_CDC, + .bDeviceSubClass = 0, + .bDeviceProtocol = 0, + .bMaxPacketSize0 = 64, + .idVendor = 0x0483, + .idProduct = 0x5740, + .bcdDevice = 0x0200, + .iManufacturer = 1, + .iProduct = 2, + .iSerialNumber = 3, + .bNumConfigurations = 1, +}; + +/* This notification endpoint isn't implemented. According to CDC spec its + * optional, but its absence causes a NULL pointer dereference in Linux cdc_acm + * driver. */ +static const struct usb_endpoint_descriptor comm_endp[] = {{ + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x83, + .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, + .wMaxPacketSize = 16, + .bInterval = 255, +}}; + +static const struct usb_endpoint_descriptor data_endp[] = {{ + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x01, + .bmAttributes = USB_ENDPOINT_ATTR_BULK, + .wMaxPacketSize = 64, + .bInterval = 1, +}, { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x82, + .bmAttributes = USB_ENDPOINT_ATTR_BULK, + .wMaxPacketSize = 64, + .bInterval = 1, +}}; + +static const struct { + struct usb_cdc_header_descriptor header; + struct usb_cdc_call_management_descriptor call_mgmt; + struct usb_cdc_acm_descriptor acm; + struct usb_cdc_union_descriptor cdc_union; +} __attribute__((packed)) cdcacm_functional_descriptors = { + .header = { + .bFunctionLength = sizeof(struct usb_cdc_header_descriptor), + .bDescriptorType = CS_INTERFACE, + .bDescriptorSubtype = USB_CDC_TYPE_HEADER, + .bcdCDC = 0x0110, + }, + .call_mgmt = { + .bFunctionLength = + sizeof(struct usb_cdc_call_management_descriptor), + .bDescriptorType = CS_INTERFACE, + .bDescriptorSubtype = USB_CDC_TYPE_CALL_MANAGEMENT, + .bmCapabilities = 0, + .bDataInterface = 1, + }, + .acm = { + .bFunctionLength = sizeof(struct usb_cdc_acm_descriptor), + .bDescriptorType = CS_INTERFACE, + .bDescriptorSubtype = USB_CDC_TYPE_ACM, + .bmCapabilities = 0, + }, + .cdc_union = { + .bFunctionLength = sizeof(struct usb_cdc_union_descriptor), + .bDescriptorType = CS_INTERFACE, + .bDescriptorSubtype = USB_CDC_TYPE_UNION, + .bControlInterface = 0, + .bSubordinateInterface0 = 1, + } +}; + +static const struct usb_interface_descriptor comm_iface[] = {{ + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 0, + .bAlternateSetting = 0, + .bNumEndpoints = 1, + .bInterfaceClass = USB_CLASS_CDC, + .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM, + .bInterfaceProtocol = USB_CDC_PROTOCOL_AT, + .iInterface = 0, + + .endpoint = comm_endp, + + .extra = &cdcacm_functional_descriptors, + .extralen = sizeof(cdcacm_functional_descriptors) +}}; + +static const struct usb_interface_descriptor data_iface[] = {{ + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 1, + .bAlternateSetting = 0, + .bNumEndpoints = 2, + .bInterfaceClass = USB_CLASS_DATA, + .bInterfaceSubClass = 0, + .bInterfaceProtocol = 0, + .iInterface = 0, + + .endpoint = data_endp, +}}; + +static const struct usb_interface ifaces[] = {{ + .num_altsetting = 1, + .altsetting = comm_iface, +}, { + .num_altsetting = 1, + .altsetting = data_iface, +}}; + +static const struct usb_config_descriptor config = { + .bLength = USB_DT_CONFIGURATION_SIZE, + .bDescriptorType = USB_DT_CONFIGURATION, + .wTotalLength = 0, + .bNumInterfaces = 2, + .bConfigurationValue = 1, + .iConfiguration = 0, + .bmAttributes = 0x80, + .bMaxPower = 0x32, + + .interface = ifaces, +}; + +static const char *usb_strings[] = { + "x", + "Black Sphere Technologies", + "CDC-ACM Demo", + "DEMO" +}; + +static int cdcacm_control_request(struct usb_setup_data *req, u8 **buf, + u16 *len, void (**complete)(struct usb_setup_data *req)) +{ + (void)complete; + (void)buf; + + switch(req->bRequest) { + case USB_CDC_REQ_SET_CONTROL_LINE_STATE: { + /* This Linux cdc_acm driver requires this to be implemented + * even though it's optional in the CDC spec, and we don't + * advertise it in the ACM functional descriptor. */ + char buf[10]; + struct usb_cdc_notification *notif = (void*)buf; + + /* We echo signals back to host as notification */ + notif->bmRequestType = 0xA1; + notif->bNotification = USB_CDC_NOTIFY_SERIAL_STATE; + notif->wValue = 0; + notif->wIndex = 0; + notif->wLength = 2; + buf[8] = req->wValue & 3; + buf[9] = 0; + //usbd_ep_write_packet(0x83, buf, 10); + return 1; + } + case USB_CDC_REQ_SET_LINE_CODING: + if(*len < sizeof(struct usb_cdc_line_coding)) + return 0; + + return 1; + } + return 0; +} + +static void cdcacm_data_rx_cb(u8 ep) +{ + (void)ep; + + char buf[64]; + int len = usbd_ep_read_packet(0x01, buf, 64); + if(len) { + usbd_ep_write_packet(0x82, buf, len); + buf[len] = 0; + } +} + +static void cdcacm_set_config(u16 wValue) +{ + (void)wValue; + + usbd_ep_setup(0x01, USB_ENDPOINT_ATTR_BULK, 64, cdcacm_data_rx_cb); + usbd_ep_setup(0x82, USB_ENDPOINT_ATTR_BULK, 64, NULL); + usbd_ep_setup(0x83, USB_ENDPOINT_ATTR_INTERRUPT, 16, NULL); + + usbd_register_control_callback( + USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE, + USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, + cdcacm_control_request); +} + +int main(void) +{ + rcc_clock_setup_in_hsi_out_48mhz(); + + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USBEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + gpio_set(GPIOC, GPIO11); + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO11); + + usbd_init(&dev, &config, usb_strings); + usbd_register_set_config_callback(cdcacm_set_config); + + {int i; for (i=0;i<0x80000;i++);} + gpio_clear(GPIOC, GPIO11); + + while (1) + usbd_poll(); +} diff --git a/examples/stm32/stm32-h103/usb_cdcacm/cdcacm.ld b/examples/stm32/stm32-h103/usb_cdcacm/cdcacm.ld new file mode 100644 index 0000000..a393e0b --- /dev/null +++ b/examples/stm32/stm32-h103/usb_cdcacm/cdcacm.ld @@ -0,0 +1,32 @@ +/* + * This file is part of the libopenstm32 project. + * + * Copyright (C) 2010 Thomas Otto + * + * 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-H103 (STM32F103RBT6, 128K flash, 20K RAM). */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K +} + + +/* Include the common ld script from libopenstm32. */ +INCLUDE libopenstm32.ld + diff --git a/examples/stm32/stm32-h103/usb_dfu/Makefile b/examples/stm32/stm32-h103/usb_dfu/Makefile new file mode 100644 index 0000000..183b7c8 --- /dev/null +++ b/examples/stm32/stm32-h103/usb_dfu/Makefile @@ -0,0 +1,23 @@ +## +## This file is part of the libopenstm32 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 = usbdfu + +include ../../Makefile.include + diff --git a/examples/stm32/stm32-h103/usb_dfu/README b/examples/stm32/stm32-h103/usb_dfu/README new file mode 100644 index 0000000..9c0169a --- /dev/null +++ b/examples/stm32/stm32-h103/usb_dfu/README @@ -0,0 +1,7 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example implements a USB Device Firmware Upgrade (DFU) bootloader +to demonstrate the use of the USB device stack. + diff --git a/examples/stm32/stm32-h103/usb_dfu/usbdfu.c b/examples/stm32/stm32-h103/usb_dfu/usbdfu.c new file mode 100644 index 0000000..0a6789d --- /dev/null +++ b/examples/stm32/stm32-h103/usb_dfu/usbdfu.c @@ -0,0 +1,295 @@ +/* + * This file is part of the libopenstm32 project. + * + * Copyright (C) 2010 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 +#include +#include +#include +#include + +#define APP_ADDRESS 0x08002000 + +/* Commands sent with wBlockNum == 0 as per ST implementation. */ +#define CMD_SETADDR 0x21 +#define CMD_ERASE 0x41 + +/* We need a special large control buffer for this device: */ +u8 usbd_control_buffer[1024]; + +static enum dfu_state usbdfu_state = STATE_DFU_IDLE; + +static struct { + u8 buf[sizeof(usbd_control_buffer)]; + u16 len; + u32 addr; + u16 blocknum; +} prog; + +const struct usb_device_descriptor dev = { + .bLength = USB_DT_DEVICE_SIZE, + .bDescriptorType = USB_DT_DEVICE, + .bcdUSB = 0x0200, + .bDeviceClass = 0, + .bDeviceSubClass = 0, + .bDeviceProtocol = 0, + .bMaxPacketSize0 = 64, + .idVendor = 0x0483, + .idProduct = 0xDF11, + .bcdDevice = 0x0200, + .iManufacturer = 1, + .iProduct = 2, + .iSerialNumber = 3, + .bNumConfigurations = 1, +}; + +const struct usb_dfu_descriptor dfu_function = { + .bLength = sizeof(struct usb_dfu_descriptor), + .bDescriptorType = DFU_FUNCTIONAL, + .bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH, + .wDetachTimeout = 255, + .wTransferSize = 1024, + .bcdDFUVersion = 0x011A, +}; + +const struct usb_interface_descriptor iface = { + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 0, + .bAlternateSetting = 0, + .bNumEndpoints = 0, + .bInterfaceClass = 0xFE, /* Device Firmware Upgrade */ + .bInterfaceSubClass = 1, + .bInterfaceProtocol = 2, + + /* The ST Microelectronics DfuSe application needs this string. + * The format isn't documented... */ + .iInterface = 4, + + .extra = &dfu_function, + .extralen = sizeof(dfu_function), +}; + +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 = 0xC0, + .bMaxPower = 0x32, + + .interface = ifaces, +}; + +static const char *usb_strings[] = { + "x", + "Black Sphere Technologies", + "DFU Demo", + "DEMO", + /* This string is used by ST Microelectronics' DfuSe utility */ + "@Internal Flash /0x08000000/8*001Ka,56*001Kg" +}; + +static u8 usbdfu_getstatus(u32 *bwPollTimeout) +{ + switch(usbdfu_state) { + case STATE_DFU_DNLOAD_SYNC: + usbdfu_state = STATE_DFU_DNBUSY; + *bwPollTimeout = 100; + return DFU_STATUS_OK; + + case STATE_DFU_MANIFEST_SYNC: + /* Device will reset when read is complete */ + usbdfu_state = STATE_DFU_MANIFEST; + return DFU_STATUS_OK; + + default: + return DFU_STATUS_OK; + } +} + +static void usbdfu_getstatus_complete(struct usb_setup_data *req) +{ + int i; + (void)req; + + switch(usbdfu_state) { + case STATE_DFU_DNBUSY: + + flash_unlock(); + if(prog.blocknum == 0) { + switch(prog.buf[0]) { + case CMD_ERASE: + flash_erase_page(*(u32*)(prog.buf+1)); + case CMD_SETADDR: + prog.addr = *(u32*)(prog.buf+1); + } + } else { + u32 baseaddr = prog.addr + + ((prog.blocknum - 2) * + dfu_function.wTransferSize); + for(i = 0; i < prog.len; i += 2) + flash_program_half_word(baseaddr + i, + *(u16*)(prog.buf+i)); + } + flash_lock(); + + /* We jump straight to dfuDNLOAD-IDLE, + * skipping dfuDNLOAD-SYNC + */ + usbdfu_state = STATE_DFU_DNLOAD_IDLE; + return; + + case STATE_DFU_MANIFEST: + /* USB device must detach, we just reset... */ + scb_reset_system(); + return; /* Will never return */ + default: + return; + } +} + +static int usbdfu_control_command(struct usb_setup_data *req, + void (**complete)(struct usb_setup_data *req)) +{ + (void)complete; + + if(req->bmRequestType != 0x21) + return 0; /* Only accept class request */ + + switch(req->bRequest) { + case DFU_DNLOAD: + usbdfu_state = STATE_DFU_MANIFEST_SYNC; + return 1; + case DFU_CLRSTATUS: + /* Clear error and return to dfuIDLE */ + if(usbdfu_state == STATE_DFU_ERROR) + usbdfu_state = STATE_DFU_IDLE; + return 1; + case DFU_ABORT: + /* 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; + case DFU_GETSTATUS: { + u32 bwPollTimeout = 0; /* 24-bit integer in DFU class spec */ + + (*buf)[0] = usbdfu_getstatus(&bwPollTimeout); + (*buf)[1] = bwPollTimeout & 0xFF; + (*buf)[2] = (bwPollTimeout >> 8) & 0xFF; + (*buf)[3] = (bwPollTimeout >> 16) & 0xFF; + (*buf)[4] = usbdfu_state; + (*buf)[5] = 0; /* iString not used here */ + *len = 6; + + *complete = usbdfu_getstatus_complete; + + return 1; + } + case DFU_GETSTATE: + /* Return state with no state transision */ + *buf[0] = usbdfu_state; + *len = 1; + return 1; + } + + 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); + if(!gpio_get(GPIOA, GPIO10)) { + /* Boot the application if it's valid */ + if((*(volatile u32*)APP_ADDRESS & 0x2FFE0000) == 0x20000000) { + /* Set vector table base address */ + SCB_VTOR = APP_ADDRESS & 0xFFFF; + /* Initialise master stack pointer */ + asm volatile ("msr msp, %0"::"g" + (*(volatile u32*)APP_ADDRESS)); + /* Jump to application */ + (*(void(**)())(APP_ADDRESS + 4))(); + } + } + + rcc_clock_setup_in_hsi_out_48mhz(); + + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USBEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); + + 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_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); + + gpio_set(GPIOA, GPIO15); + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO15); + + while (1) + usbd_poll(); +} diff --git a/examples/stm32/stm32-h103/usb_dfu/usbdfu.ld b/examples/stm32/stm32-h103/usb_dfu/usbdfu.ld new file mode 100644 index 0000000..d7cd0c5 --- /dev/null +++ b/examples/stm32/stm32-h103/usb_dfu/usbdfu.ld @@ -0,0 +1,29 @@ +/* + * This file is part of the libopenstm32 project. + * + * Copyright (C) 2010 Thomas Otto + * + * 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 . + */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH =8K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 4K +} + +/* Include the common ld script from libopenstm32. */ +INCLUDE libopenstm32.ld + diff --git a/examples/stm32/stm32-h103/usb_hid/Makefile b/examples/stm32/stm32-h103/usb_hid/Makefile new file mode 100644 index 0000000..7967467 --- /dev/null +++ b/examples/stm32/stm32-h103/usb_hid/Makefile @@ -0,0 +1,23 @@ +## +## This file is part of the libopenstm32 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 = usbhid + +include ../../Makefile.include + diff --git a/examples/stm32/stm32-h103/usb_hid/README b/examples/stm32/stm32-h103/usb_hid/README new file mode 100644 index 0000000..48a9b39 --- /dev/null +++ b/examples/stm32/stm32-h103/usb_hid/README @@ -0,0 +1,7 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example implements a USB Human Interface Device (HID) +to demonstrate the use of the USB device stack. + diff --git a/examples/stm32/stm32-h103/usb_hid/usbhid.c b/examples/stm32/stm32-h103/usb_hid/usbhid.c new file mode 100644 index 0000000..e326dd0 --- /dev/null +++ b/examples/stm32/stm32-h103/usb_hid/usbhid.c @@ -0,0 +1,273 @@ +/* + * This file is part of the libopenstm32 project. + * + * Copyright (C) 2010 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 +#include +#include +#include + +/* Define this to include the DFU APP interface. */ +#define INCLUDE_DFU_INTERFACE + +#ifdef INCLUDE_DFU_INTERFACE +#include +#include +#endif + +const struct usb_device_descriptor dev = { + .bLength = USB_DT_DEVICE_SIZE, + .bDescriptorType = USB_DT_DEVICE, + .bcdUSB = 0x0200, + .bDeviceClass = 0, + .bDeviceSubClass = 0, + .bDeviceProtocol = 0, + .bMaxPacketSize0 = 64, + .idVendor = 0x0483, + .idProduct = 0x5710, + .bcdDevice = 0x0200, + .iManufacturer = 1, + .iProduct = 2, + .iSerialNumber = 3, + .bNumConfigurations = 1, +}; + +/* I have no idea what this means. I haven't read the HID spec. */ +static const u8 hid_report_descriptor[] = { + 0x05, 0x01, 0x09, 0x02, 0xA1, 0x01, 0x09, 0x01, + 0xA1, 0x00, 0x05, 0x09, 0x19, 0x01, 0x29, 0x03, + 0x15, 0x00, 0x25, 0x01, 0x95, 0x03, 0x75, 0x01, + 0x81, 0x02, 0x95, 0x01, 0x75, 0x05, 0x81, 0x01, + 0x05, 0x01, 0x09, 0x30, 0x09, 0x31, 0x09, 0x38, + 0x15, 0x81, 0x25, 0x7F, 0x75, 0x08, 0x95, 0x03, + 0x81, 0x06, 0xC0, 0x09, 0x3c, 0x05, 0xff, 0x09, + 0x01, 0x15, 0x00, 0x25, 0x01, 0x75, 0x01, 0x95, + 0x02, 0xb1, 0x22, 0x75, 0x06, 0x95, 0x01, 0xb1, + 0x01, 0xc0 +}; + +static const struct { + struct usb_hid_descriptor hid_descriptor; + struct { + u8 bReportDescriptorType; + u16 wDescriptorLength; + } __attribute__((packed)) hid_report; +} __attribute__((packed)) hid_function = { + .hid_descriptor = { + .bLength = sizeof(hid_function), + .bDescriptorType = USB_DT_HID, + .bcdHID = 0x0100, + .bCountryCode = 0, + .bNumDescriptors = 1, + }, + .hid_report = { + .bReportDescriptorType = USB_DT_REPORT, + .wDescriptorLength = sizeof(hid_report_descriptor), + } +}; + +const struct usb_endpoint_descriptor hid_endpoint = { + .bLength = USB_DT_ENDPOINT_SIZE, + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 0x81, + .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, + .wMaxPacketSize = 4, + .bInterval = 0x20, +}; + +const struct usb_interface_descriptor hid_iface = { + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 0, + .bAlternateSetting = 0, + .bNumEndpoints = 1, + .bInterfaceClass = USB_CLASS_HID, + .bInterfaceSubClass = 1, /* boot */ + .bInterfaceProtocol = 2, /* mouse */ + .iInterface = 0, + + .endpoint = &hid_endpoint, + + .extra = &hid_function, + .extralen = sizeof(hid_function), +}; + +#ifdef INCLUDE_DFU_INTERFACE +const struct usb_dfu_descriptor dfu_function = { + .bLength = sizeof(struct usb_dfu_descriptor), + .bDescriptorType = DFU_FUNCTIONAL, + .bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH, + .wDetachTimeout = 255, + .wTransferSize = 1024, + .bcdDFUVersion = 0x011A, +}; + +const struct usb_interface_descriptor dfu_iface = { + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 1, + .bAlternateSetting = 0, + .bNumEndpoints = 0, + .bInterfaceClass = 0xFE, + .bInterfaceSubClass = 1, + .bInterfaceProtocol = 1, + .iInterface = 0, + + .extra = &dfu_function, + .extralen = sizeof(dfu_function), +}; +#endif + +const struct usb_interface ifaces[] = {{ + .num_altsetting = 1, + .altsetting = &hid_iface, +#ifdef INCLUDE_DFU_INTERFACE +}, { + .num_altsetting = 1, + .altsetting = &dfu_iface, +#endif +}}; + +const struct usb_config_descriptor config = { + .bLength = USB_DT_CONFIGURATION_SIZE, + .bDescriptorType = USB_DT_CONFIGURATION, + .wTotalLength = 0, +#ifdef INCLUDE_DFU_INTERFACE + .bNumInterfaces = 2, +#else + .bNumInterfaces = 1, +#endif + .bConfigurationValue = 1, + .iConfiguration = 0, + .bmAttributes = 0xC0, + .bMaxPower = 0x32, + + .interface = ifaces, +}; + +static const char *usb_strings[] = { + "x", + "Black Sphere Technologies", + "HID Demo", + "DEMO", +}; + +static int hid_control_request(struct usb_setup_data *req, u8 **buf, u16 *len, + void (**complete)(struct usb_setup_data *req)) +{ + (void)complete; + + if((req->bmRequestType != 0x81) || + (req->bRequest != USB_REQ_GET_DESCRIPTOR) || + (req->wValue != 0x2200)) + return 0; + + /* Handle the HID report descriptor */ + *buf = (u8*)hid_report_descriptor; + *len = sizeof(hid_report_descriptor); + + return 1; +} + +#ifdef INCLUDE_DFU_INTERFACE +static void dfu_detach_complete(struct usb_setup_data *req) +{ + (void)req; + + gpio_set_mode(GPIOA, GPIO_MODE_INPUT, 0, GPIO15); + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO10); + gpio_set(GPIOA, GPIO10); + scb_reset_core(); +} + +static int dfu_control_request(struct usb_setup_data *req, u8 **buf, u16 *len, + void (**complete)(struct usb_setup_data *req)) +{ + (void)buf; + (void)len; + + if((req->bmRequestType != 0x21) || (req->bRequest != DFU_DETACH)) + return 0; /* Only accept class request */ + + *complete = dfu_detach_complete; + + return 1; +} +#endif + +static void hid_set_config(u16 wValue) +{ + (void)wValue; + + usbd_ep_setup(0x81, USB_ENDPOINT_ATTR_INTERRUPT, 4, NULL); + + usbd_register_control_callback( + USB_REQ_TYPE_STANDARD | USB_REQ_TYPE_INTERFACE, + USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, + hid_control_request); +#ifdef INCLUDE_DFU_INTERFACE + usbd_register_control_callback( + USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE, + USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, + dfu_control_request); +#endif + + systick_set_clocksource(STK_CTRL_CLKSOURCE_AHB_DIV8); + systick_set_reload(100000); + systick_interrupt_enable(); + systick_counter_enable(); +} + +int main(void) +{ + rcc_clock_setup_in_hsi_out_48mhz(); + + + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USBEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN); + + gpio_set(GPIOC, GPIO11); + gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO11); + + usbd_init(&dev, &config, usb_strings); + usbd_register_set_config_callback(hid_set_config); + + {int i; for (i=0;i<0x80000;i++);} + gpio_clear(GPIOC, GPIO11); + + while (1) + usbd_poll(); +} + +void sys_tick_handler(void) +{ + static int x = 0; + static int dir = 1; + u8 buf[4] = {0, 0, 0, 0}; + + buf[1] = dir; + x += dir; + if(x > 30) dir = -dir; + if(x < -30) dir = -dir; + + usbd_ep_write_packet(0x81, buf, 4); +} diff --git a/examples/stm32/stm32-h103/usb_hid/usbhid.ld b/examples/stm32/stm32-h103/usb_hid/usbhid.ld new file mode 100644 index 0000000..a393e0b --- /dev/null +++ b/examples/stm32/stm32-h103/usb_hid/usbhid.ld @@ -0,0 +1,32 @@ +/* + * This file is part of the libopenstm32 project. + * + * Copyright (C) 2010 Thomas Otto + * + * 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-H103 (STM32F103RBT6, 128K flash, 20K RAM). */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K +} + + +/* Include the common ld script from libopenstm32. */ +INCLUDE libopenstm32.ld + diff --git a/examples/stm32/stm32-h103/usb_iap/Makefile b/examples/stm32/stm32-h103/usb_iap/Makefile new file mode 100644 index 0000000..9c93f91 --- /dev/null +++ b/examples/stm32/stm32-h103/usb_iap/Makefile @@ -0,0 +1,23 @@ +## +## This file is part of the libopenstm32 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 = usbiap + +include ../../Makefile.includes + diff --git a/examples/stm32/stm32-h103/usb_iap/README b/examples/stm32/stm32-h103/usb_iap/README new file mode 100644 index 0000000..2966684 --- /dev/null +++ b/examples/stm32/stm32-h103/usb_iap/README @@ -0,0 +1,8 @@ +------------------------------------------------------------------------------ +README +------------------------------------------------------------------------------ + +This example implements a USB bootloader for the Paparazzi project. + +TODO: Move to examples/lisa-m? + diff --git a/examples/stm32/stm32-h103/usb_iap/usbiap.c b/examples/stm32/stm32-h103/usb_iap/usbiap.c new file mode 100644 index 0000000..0a6789d --- /dev/null +++ b/examples/stm32/stm32-h103/usb_iap/usbiap.c @@ -0,0 +1,295 @@ +/* + * This file is part of the libopenstm32 project. + * + * Copyright (C) 2010 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 +#include +#include +#include +#include + +#define APP_ADDRESS 0x08002000 + +/* Commands sent with wBlockNum == 0 as per ST implementation. */ +#define CMD_SETADDR 0x21 +#define CMD_ERASE 0x41 + +/* We need a special large control buffer for this device: */ +u8 usbd_control_buffer[1024]; + +static enum dfu_state usbdfu_state = STATE_DFU_IDLE; + +static struct { + u8 buf[sizeof(usbd_control_buffer)]; + u16 len; + u32 addr; + u16 blocknum; +} prog; + +const struct usb_device_descriptor dev = { + .bLength = USB_DT_DEVICE_SIZE, + .bDescriptorType = USB_DT_DEVICE, + .bcdUSB = 0x0200, + .bDeviceClass = 0, + .bDeviceSubClass = 0, + .bDeviceProtocol = 0, + .bMaxPacketSize0 = 64, + .idVendor = 0x0483, + .idProduct = 0xDF11, + .bcdDevice = 0x0200, + .iManufacturer = 1, + .iProduct = 2, + .iSerialNumber = 3, + .bNumConfigurations = 1, +}; + +const struct usb_dfu_descriptor dfu_function = { + .bLength = sizeof(struct usb_dfu_descriptor), + .bDescriptorType = DFU_FUNCTIONAL, + .bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH, + .wDetachTimeout = 255, + .wTransferSize = 1024, + .bcdDFUVersion = 0x011A, +}; + +const struct usb_interface_descriptor iface = { + .bLength = USB_DT_INTERFACE_SIZE, + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 0, + .bAlternateSetting = 0, + .bNumEndpoints = 0, + .bInterfaceClass = 0xFE, /* Device Firmware Upgrade */ + .bInterfaceSubClass = 1, + .bInterfaceProtocol = 2, + + /* The ST Microelectronics DfuSe application needs this string. + * The format isn't documented... */ + .iInterface = 4, + + .extra = &dfu_function, + .extralen = sizeof(dfu_function), +}; + +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 = 0xC0, + .bMaxPower = 0x32, + + .interface = ifaces, +}; + +static const char *usb_strings[] = { + "x", + "Black Sphere Technologies", + "DFU Demo", + "DEMO", + /* This string is used by ST Microelectronics' DfuSe utility */ + "@Internal Flash /0x08000000/8*001Ka,56*001Kg" +}; + +static u8 usbdfu_getstatus(u32 *bwPollTimeout) +{ + switch(usbdfu_state) { + case STATE_DFU_DNLOAD_SYNC: + usbdfu_state = STATE_DFU_DNBUSY; + *bwPollTimeout = 100; + return DFU_STATUS_OK; + + case STATE_DFU_MANIFEST_SYNC: + /* Device will reset when read is complete */ + usbdfu_state = STATE_DFU_MANIFEST; + return DFU_STATUS_OK; + + default: + return DFU_STATUS_OK; + } +} + +static void usbdfu_getstatus_complete(struct usb_setup_data *req) +{ + int i; + (void)req; + + switch(usbdfu_state) { + case STATE_DFU_DNBUSY: + + flash_unlock(); + if(prog.blocknum == 0) { + switch(prog.buf[0]) { + case CMD_ERASE: + flash_erase_page(*(u32*)(prog.buf+1)); + case CMD_SETADDR: + prog.addr = *(u32*)(prog.buf+1); + } + } else { + u32 baseaddr = prog.addr + + ((prog.blocknum - 2) * + dfu_function.wTransferSize); + for(i = 0; i < prog.len; i += 2) + flash_program_half_word(baseaddr + i, + *(u16*)(prog.buf+i)); + } + flash_lock(); + + /* We jump straight to dfuDNLOAD-IDLE, + * skipping dfuDNLOAD-SYNC + */ + usbdfu_state = STATE_DFU_DNLOAD_IDLE; + return; + + case STATE_DFU_MANIFEST: + /* USB device must detach, we just reset... */ + scb_reset_system(); + return; /* Will never return */ + default: + return; + } +} + +static int usbdfu_control_command(struct usb_setup_data *req, + void (**complete)(struct usb_setup_data *req)) +{ + (void)complete; + + if(req->bmRequestType != 0x21) + return 0; /* Only accept class request */ + + switch(req->bRequest) { + case DFU_DNLOAD: + usbdfu_state = STATE_DFU_MANIFEST_SYNC; + return 1; + case DFU_CLRSTATUS: + /* Clear error and return to dfuIDLE */ + if(usbdfu_state == STATE_DFU_ERROR) + usbdfu_state = STATE_DFU_IDLE; + return 1; + case DFU_ABORT: + /* 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; + case DFU_GETSTATUS: { + u32 bwPollTimeout = 0; /* 24-bit integer in DFU class spec */ + + (*buf)[0] = usbdfu_getstatus(&bwPollTimeout); + (*buf)[1] = bwPollTimeout & 0xFF; + (*buf)[2] = (bwPollTimeout >> 8) & 0xFF; + (*buf)[3] = (bwPollTimeout >> 16) & 0xFF; + (*buf)[4] = usbdfu_state; + (*buf)[5] = 0; /* iString not used here */ + *len = 6; + + *complete = usbdfu_getstatus_complete; + + return 1; + } + case DFU_GETSTATE: + /* Return state with no state transision */ + *buf[0] = usbdfu_state; + *len = 1; + return 1; + } + + 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); + if(!gpio_get(GPIOA, GPIO10)) { + /* Boot the application if it's valid */ + if((*(volatile u32*)APP_ADDRESS & 0x2FFE0000) == 0x20000000) { + /* Set vector table base address */ + SCB_VTOR = APP_ADDRESS & 0xFFFF; + /* Initialise master stack pointer */ + asm volatile ("msr msp, %0"::"g" + (*(volatile u32*)APP_ADDRESS)); + /* Jump to application */ + (*(void(**)())(APP_ADDRESS + 4))(); + } + } + + rcc_clock_setup_in_hsi_out_48mhz(); + + rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USBEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN); + rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_AFIOEN); + + 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_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); + + gpio_set(GPIOA, GPIO15); + gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_PUSHPULL, GPIO15); + + while (1) + usbd_poll(); +} diff --git a/examples/stm32/stm32-h103/usb_iap/usbiap.ld b/examples/stm32/stm32-h103/usb_iap/usbiap.ld new file mode 100644 index 0000000..a393e0b --- /dev/null +++ b/examples/stm32/stm32-h103/usb_iap/usbiap.ld @@ -0,0 +1,32 @@ +/* + * This file is part of the libopenstm32 project. + * + * Copyright (C) 2010 Thomas Otto + * + * 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-H103 (STM32F103RBT6, 128K flash, 20K RAM). */ + +/* Define memory regions. */ +MEMORY +{ + rom (rx) : ORIGIN = 0x08000000, LENGTH = 128K + ram (rwx) : ORIGIN = 0x20000000, LENGTH = 20K +} + + +/* Include the common ld script from libopenstm32. */ +INCLUDE libopenstm32.ld + -- cgit v1.2.3