Skip to content

Commit

Permalink
multi-board support
Browse files Browse the repository at this point in the history
  • Loading branch information
dword1511 committed Dec 28, 2015
1 parent 55ce9ec commit bb3cfc1
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
*.map
*.out
*.html
board.h
48 changes: 29 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
# Makefile for stm32-vserprog
# * Simply make: will make the firmware for default board.
# * Override toochain: make CROSS=/path/to/arm-none-eabi
# * Override UART for downloading: make SERIAL=/dev/ttyS1
# * Override hardware config: make BOARD=some_board_in_boards_folder

###############################################################################

PROGRAM = stm32-vserprog
CROSS = arm-none-eabi
LDSCRIPT = libopencm3/lib/stm32/f1/stm32f103x8.ld
SERIAL = /dev/ttyUSB0
CROSS ?= arm-none-eabi
SERIAL ?= /dev/ttyUSB0
BOARD ?= stm32-vserprog-v2
OBJS = vserprog.o \
usbcdc.o \
spi.o \


DOCS = README.html
DOCS = README.html \



###############################################################################
Expand All @@ -24,29 +33,25 @@ HEX = $(PROGRAM).hex
MAP = $(PROGRAM).map
DMP = $(PROGRAM).out

DEFS = -DSTM32F1
INCS = -Ilibopencm3/include/
FP_FLAGS = -msoft-float
ARCH_FLAGS = -mthumb -mcpu=cortex-m3 $(FP_FLAGS) -mfix-cortex-m3-ldrd
include boards/$(BOARD).mk

CFLAGS += -O3 -Wall -g
#CFLAGS += -Os -Wall -g
#CFLAGS += -Wextra -fprofile-generate -fprofile-use
CFLAGS += -fno-common -ffunction-sections -fdata-sections
CFLAGS += $(ARCH_FLAGS) $(DEFS) $(INCS)

LDPATH = libopencm3/lib/
CFLAGS += $(ARCH_FLAGS) -Ilibopencm3/include/

LIBM = $(shell $(CC) $(CFLAGS) --print-file-name=libm.a)
LIBC = $(shell $(CC) $(CFLAGS) --print-file-name=libc.a)
LIBNOSYS = $(shell $(CC) $(CFLAGS) --print-file-name=libnosys.a)
LIBGCC = $(shell $(CC) $(CFLAGS) --print-libgcc-file-name)
LIBOPENCM3 = $(LDPATH)/libopencm3_stm32f1.a

# LDPATH is required for libopencm3 ld scripts to work.
LDPATH = libopencm3/lib/
LDFLAGS += -L$(LDPATH) -T$(LDSCRIPT) -Map $(MAP) --gc-sections
LDLIBS += $(LIBOPENCM3) $(LIBC) $(LIBNOSYS) $(LIBGCC)

all: $(LDPATH)$(LIBOPENCM3) $(BIN) $(HEX) $(DMP) size
all: $(LIBOPENCM3) $(BIN) $(HEX) $(DMP) size

$(ELF): $(LDSCRIPT) $(OBJS)
$(LD) -o $@ $(LDFLAGS) $(OBJS) $(LDLIBS)
Expand All @@ -56,29 +61,34 @@ $(DMP): $(ELF)

%.hex: %.elf
$(OBJCOPY) -S -O ihex $< $@
cp $@ $(PROGRAM)-$(BOARD).hex

%.bin: %.elf
$(OBJCOPY) -S -O binary $< $@

%.o: %.c
%.o: %.c board.h
$(CC) $(CFLAGS) -c $< -o $@

%.html: %.md
markdown $< > $@

$(LDPATH)$(LIBOPENCM3):
$(LIBOPENCM3):
git submodule init
git submodule update --remote
CFLAGS="$(CFLAGS)" make -C libopencm3 lib/stm32/f1 V=1
CFLAGS="$(CFLAGS)" make -C libopencm3 $(OPENCM3_MK) V=1


.PHONY: board.h clean distclean flash size

.PHONY: clean distclean flash size
board.h:
ln -sfT "boards/$(BOARD).h" $@

clean:
rm -f $(OBJS) $(DOCS) $(ELF) $(HEX) $(BIN) $(MAP) $(DMP)
rm -f $(OBJS) $(DOCS) $(ELF) $(HEX) $(BIN) $(MAP) $(DMP) board.h

distclean: clean
make -C libopencm3 clean
rm -f *~ *.swp
rm -f *~ *.swp *.hex

flash: $(HEX)
stm32flash -w $< -v $(SERIAL)
Expand Down
22 changes: 22 additions & 0 deletions boards/maple-mini.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef __BOARD_H__
#define __BOARD_H__

#include <stdbool.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>

#define BOARD_USE_DEBUG_PINS_AS_GPIO false

#define BOARD_RCC_LED RCC_GPIOB
#define BOARD_PORT_LED GPIOB
#define BOARD_PIN_LED GPIO1
#define BOARD_LED_HIGH_IS_BUSY false /* We only have IDLE LED on it, which is active high. */

#define BOARD_RCC_USB_PULLUP RCC_GPIOB
#define BOARD_PORT_USB_PULLUP GPIOB
#define BOARD_PIN_USB_PULLUP GPIO9
#define BOARD_USB_HIGH_IS_PULLUP false /* Active low */

/* Currently you can only use SPI1, since it has highest clock. */

#endif /* __BOARD_H__ */
1 change: 1 addition & 0 deletions boards/maple-mini.mk
22 changes: 22 additions & 0 deletions boards/stm32-vserprog-v2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef __BOARD_H__
#define __BOARD_H__

#include <stdbool.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>

#define BOARD_USE_DEBUG_PINS_AS_GPIO true /* We have to disable JTAG / SWD to access PB3 */

#define BOARD_RCC_LED RCC_GPIOA
#define BOARD_PORT_LED GPIOA
#define BOARD_PIN_LED GPIO0
#define BOARD_LED_HIGH_IS_BUSY true

#define BOARD_RCC_USB_PULLUP RCC_GPIOB
#define BOARD_PORT_USB_PULLUP GPIOB
#define BOARD_PIN_USB_PULLUP GPIO3
#define BOARD_USB_HIGH_IS_PULLUP true

/* Currently you can only use SPI1, since it has highest clock. */

#endif /* __BOARD_H__ */
1 change: 1 addition & 0 deletions boards/stm32-vserprog-v2.mk
22 changes: 22 additions & 0 deletions boards/stm32-vserprog-v3.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef __BOARD_H__
#define __BOARD_H__

#include <stdbool.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>

#define BOARD_USE_DEBUG_PINS_AS_GPIO true /* We have to disable JTAG / SWD to access PA13 */

#define BOARD_RCC_LED RCC_GPIOA
#define BOARD_PORT_LED GPIOA
#define BOARD_PIN_LED GPIO0
#define BOARD_LED_HIGH_IS_BUSY true

#define BOARD_RCC_USB_PULLUP RCC_GPIOA
#define BOARD_PORT_USB_PULLUP GPIOA
#define BOARD_PIN_USB_PULLUP GPIO13
#define BOARD_USB_HIGH_IS_PULLUP true

/* Currently you can only use SPI1, since it has highest clock. */

#endif /* __BOARD_H__ */
1 change: 1 addition & 0 deletions boards/stm32-vserprog-v3.mk
4 changes: 4 additions & 0 deletions boards/stm32f103.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ARCH_FLAGS = -DSTM32F1 -mthumb -mcpu=cortex-m3 -msoft-float -mfix-cortex-m3-ldrd
LDSCRIPT = libopencm3/lib/stm32/f1/stm32f103x8.ld
LIBOPENCM3 = libopencm3/lib/libopencm3_stm32f1.a
OPENCM3_MK = lib/stm32/f1
2 changes: 1 addition & 1 deletion libopencm3
4 changes: 2 additions & 2 deletions spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

#define SPI_DEFAULT_CLOCK 36000000

#define SPI_SELECT() gpio_clear(GPIOA, GPIO4)
#define SPI_UNSELECT() gpio_set(GPIOA, GPIO4)
#define SPI_SELECT() gpio_clear(GPIOA, GPIO_SPI1_NSS)
#define SPI_UNSELECT() gpio_set(GPIOA, GPIO_SPI1_NSS)

uint32_t spi_setup(uint32_t speed_hz);
void spi_bulk_read(uint32_t rlen);
Expand Down
1 change: 0 additions & 1 deletion usbcdc.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <stdlib.h>

#include <libopencm3/stm32/gpio.h>
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/usb/usbd.h>
#include <libopencm3/usb/cdc.h>
Expand Down
31 changes: 22 additions & 9 deletions vserprog.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "flashrom/serprog.h"
#include "flashrom/flash.h" /* For bus type */

#include "board.h"
#include "usbcdc.h"
#include "spi.h"

Expand All @@ -28,10 +29,15 @@
(1 << S_CMD_S_SPI_FREQ) \
)

#define LED_ENABLE() gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO0)
#define LED_DISABLE() gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO0)
#define LED_BUSY() gpio_set(GPIOA, GPIO0)
#define LED_IDLE() gpio_clear(GPIOA, GPIO0)
#define LED_ENABLE() gpio_set_mode(BOARD_PORT_LED, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, BOARD_PIN_LED)
#define LED_DISABLE() gpio_set_mode(BOARD_PORT_LED, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, BOARD_PIN_LED)
#if BOARD_LED_HIGH_IS_BUSY
#define LED_BUSY() gpio_set(BOARD_PORT_LED, BOARD_PIN_LED)
#define LED_IDLE() gpio_clear(BOARD_PORT_LED, BOARD_PIN_LED)
#else
#define LED_BUSY() gpio_clear(BOARD_PORT_LED, BOARD_PIN_LED)
#define LED_IDLE() gpio_set(BOARD_PORT_LED, BOARD_PIN_LED)
#endif

void handle_command(unsigned char command) {
static uint8_t i; /* Loop */
Expand Down Expand Up @@ -228,18 +234,25 @@ void handle_command(unsigned char command) {
int main(void) {
uint32_t i;

rcc_periph_clock_enable(RCC_GPIOA);
rcc_periph_clock_enable(BOARD_RCC_LED);
LED_ENABLE();
LED_BUSY();

rcc_clock_setup_in_hse_8mhz_out_72mhz();
rcc_periph_clock_enable(RCC_GPIOB);
rcc_periph_clock_enable(RCC_AFIO);
rcc_periph_clock_enable(RCC_GPIOA); /* For USB */
rcc_periph_clock_enable(BOARD_RCC_USB_PULLUP);
rcc_periph_clock_enable(RCC_AFIO); /* For SPI */
#if BOARD_USE_DEBUG_PINS_AS_GPIO
gpio_primary_remap(AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_OFF, AFIO_MAPR_TIM2_REMAP_FULL_REMAP);
#endif

/* Setup PB3 to pull up the D+ high. */
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO3);
gpio_set(GPIOB, GPIO3);
gpio_set_mode(BOARD_PORT_USB_PULLUP, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, BOARD_PIN_USB_PULLUP);
#if BOARD_USB_HIGH_IS_PULLUP
gpio_set(BOARD_PORT_USB_PULLUP, BOARD_PIN_USB_PULLUP);
#else
gpio_clear(BOARD_PORT_USB_PULLUP, BOARD_PIN_USB_PULLUP);
#endif

usbcdc_init();
spi_setup(SPI_DEFAULT_CLOCK);
Expand Down

0 comments on commit bb3cfc1

Please sign in to comment.