From c673bdca77ce7266e896d7caa4cc36ea4f5d5cf2 Mon Sep 17 00:00:00 2001 From: Tormod Volden Date: Thu, 8 May 2014 20:38:51 +0200 Subject: [PATCH 1/7] Use BRR register for resetting GPIO bits Other than shaving of a whopping 4 bytes from the binary, this is just the most straight-forward way of resetting bits. Signed-off-by: Tormod Volden --- hardware.c | 4 ++-- hardware.h | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/hardware.c b/hardware.c index d3d985e..06ce864 100644 --- a/hardware.c +++ b/hardware.c @@ -38,8 +38,8 @@ void setPin(u32 bank, u8 pin) { } void resetPin(u32 bank, u8 pin) { - u32 pinMask = 0x1 << (16 + pin); - SET_REG(GPIO_BSRR(bank), pinMask); + u32 pinMask = 0x1 << (pin); + SET_REG(GPIO_BRR(bank), pinMask); } bool readPin(u32 bank, u8 pin) { diff --git a/hardware.h b/hardware.h index 70f06c7..ec86767 100644 --- a/hardware.h +++ b/hardware.h @@ -64,6 +64,7 @@ #define GPIO_IDR(port) (port+0x08) #define GPIO_ODR(port) (port+0x0c) #define GPIO_BSRR(port) (port+0x10) +#define GPIO_BRR(port) (port+0x14) #define SCS_BASE ((u32)0xE000E000) #define NVIC_BASE (SCS_BASE + 0x0100) From 0d9a714b239b64dd721739c7dc69f5f0d8c2e137 Mon Sep 17 00:00:00 2001 From: Tormod Volden Date: Thu, 8 May 2014 22:14:30 +0200 Subject: [PATCH 2/7] config.h: Do not include common.h common.h itself is the only file to include config.h Signed-off-by: Tormod Volden --- config.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/config.h b/config.h index def4f99..b04964a 100644 --- a/config.h +++ b/config.h @@ -33,8 +33,6 @@ #ifndef __CONFIG_H #define __CONFIG_H -#include "common.h" - #define LED_BANK GPIOA #define LED 5 #define BLINK_FAST 0x50000 From c9dc339a3ecf5bfc916a03d685524d1f9320d4cc Mon Sep 17 00:00:00 2001 From: Tormod Volden Date: Thu, 8 May 2014 23:30:05 +0200 Subject: [PATCH 3/7] config.h: Reorder entries Signed-off-by: Tormod Volden --- config.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/config.h b/config.h index b04964a..52a1ea0 100644 --- a/config.h +++ b/config.h @@ -35,12 +35,13 @@ #define LED_BANK GPIOA #define LED 5 -#define BLINK_FAST 0x50000 -#define BLINK_SLOW 0x100000 #define BUTTON_BANK GPIOC #define BUTTON 9 +#define BLINK_FAST 0x50000 +#define BLINK_SLOW 0x100000 + #define STARTUP_BLINKS 5 #define BOOTLOADER_WAIT 6 From b05f59a21b603bd381643b65bb0948eba7819f28 Mon Sep 17 00:00:00 2001 From: Tormod Volden Date: Sun, 11 May 2014 15:12:24 +0200 Subject: [PATCH 4/7] Remove spurious configuration of whole lower GPIOA bank This is not much of an issue as long as SetupLED() is the first place the GPIOA bank is configured and the ports are inputs anyway, but it would cause trouble if something tried to configure any of the ports before SetupLED(). Note that ports are by default floating inputs after reset, but this configuration was setting them to analog inputs instead. Signed-off-by: Tormod Volden --- hardware.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/hardware.c b/hardware.c index 06ce864..877f2be 100644 --- a/hardware.c +++ b/hardware.c @@ -105,8 +105,6 @@ void setupLED(void) { SET_REG(RCC_APB2ENR, rwmVal); /* Setup GPIOA Pin 5 as PP Out */ - SET_REG(GPIO_CRL(GPIOA), 0x00100000); - rwmVal = GET_REG(GPIO_CRL(GPIOA)); rwmVal &= 0xFF0FFFFF; rwmVal |= 0x00100000; From 8089feb99bd44d56ff2984330f92624f12a6035c Mon Sep 17 00:00:00 2001 From: Tormod Volden Date: Thu, 8 May 2014 23:18:33 +0200 Subject: [PATCH 5/7] Avoid hardcoding of LED, Button and USB disconnect banks and pins Signed-off-by: Tormod Volden --- config.h | 6 ++++++ hardware.c | 53 +++++++++++++++++++++++++++++++++-------------------- hardware.h | 5 +++++ usb.c | 33 +++++++++++++++++++++------------ 4 files changed, 65 insertions(+), 32 deletions(-) diff --git a/config.h b/config.h index 52a1ea0..98313a2 100644 --- a/config.h +++ b/config.h @@ -34,11 +34,17 @@ #define __CONFIG_H #define LED_BANK GPIOA +#define LED_RCC_APB2ENR_GPIO RCC_APB2ENR_GPIOA #define LED 5 #define BUTTON_BANK GPIOC +#define BUTTON_RCC_APB2ENR_GPIO RCC_APB2ENR_GPIOC #define BUTTON 9 +#define USB_DISC_BANK GPIOC +#define USB_DISC_RCC_APB2ENR_GPIO RCC_APB2ENR_GPIOC +#define USB_DISC 12 + #define BLINK_FAST 0x50000 #define BLINK_SLOW 0x100000 diff --git a/hardware.c b/hardware.c index 877f2be..7b55b16 100644 --- a/hardware.c +++ b/hardware.c @@ -96,38 +96,51 @@ void setupCLK(void) { } void setupLED(void) { - // todo, swap out hardcoded pin/bank with macro u32 rwmVal; /* read-write-modify place holder var */ - /* Setup APB2 (GPIOA) */ + /* Setup APB2 for LED GPIO bank */ rwmVal = GET_REG(RCC_APB2ENR); - rwmVal |= 0x00000004; + rwmVal |= LED_RCC_APB2ENR_GPIO; SET_REG(RCC_APB2ENR, rwmVal); - /* Setup GPIOA Pin 5 as PP Out */ - rwmVal = GET_REG(GPIO_CRL(GPIOA)); - rwmVal &= 0xFF0FFFFF; - rwmVal |= 0x00100000; - SET_REG(GPIO_CRL(GPIOA), rwmVal); - - setPin(GPIOA, 5); +#if (LED < 8) +# define LED_GPIO_CR GPIO_CRL(LED_BANK) +# define LED_CR_PORT (LED) +#else +# define LED_GPIO_CR GPIO_CRH(LED_BANK) +# define LED_CR_PORT (LED - 8) +#endif + + /* Setup GPIO Pin as PP Out */ + rwmVal = GET_REG(LED_GPIO_CR); + rwmVal &= ~(0xF << (LED_CR_PORT * 4)); + rwmVal |= (0x1 << (LED_CR_PORT * 4)); + SET_REG(LED_GPIO_CR, rwmVal); + + setPin(LED_BANK, LED); } void setupBUTTON(void) { - // todo, swap out hardcoded pin/bank with macro u32 rwmVal; /* read-write-modify place holder var */ - /* Setup APB2 (GPIOC) */ + /* Setup APB2 for button GPIO bank */ rwmVal = GET_REG(RCC_APB2ENR); - rwmVal |= 0x00000010; + rwmVal |= BUTTON_RCC_APB2ENR_GPIO; SET_REG(RCC_APB2ENR, rwmVal); - /* Setup GPIOC Pin 9 as PP Out */ - rwmVal = GET_REG(GPIO_CRH(GPIOC)); - rwmVal &= 0xFFFFFF0F; - rwmVal |= 0x00000040; - SET_REG(GPIO_CRH(GPIOC), rwmVal); - +#if (BUTTON < 8) +# define BUTTON_GPIO_CR GPIO_CRL(BUTTON_BANK) +# define BUTTON_CR_PORT (BUTTON) +#else +# define BUTTON_GPIO_CR GPIO_CRH(BUTTON_BANK) +# define BUTTON_CR_PORT (BUTTON - 8) +#endif + + /* Setup button GPIO Pin as input */ + rwmVal = GET_REG(BUTTON_GPIO_CR); + rwmVal &= ~(0xF << (BUTTON_CR_PORT * 4)); + rwmVal |= (0x4 << (BUTTON_CR_PORT * 4)); + SET_REG(BUTTON_GPIO_CR, rwmVal); } void setupFLASH() { @@ -164,7 +177,7 @@ void jumpToUser(u32 usrAddr) { flashLock(); usbDsbISR(); nvicDisableInterrupts(); - setPin(GPIOC, 12); // disconnect usb from host. todo, macroize pin + setPin(USB_DISC_BANK, USB_DISC); // disconnect usb from host systemReset(); // resets clocks and periphs, not core regs diff --git a/hardware.h b/hardware.h index ec86767..2ec8329 100644 --- a/hardware.h +++ b/hardware.h @@ -33,6 +33,7 @@ #define RCC ((u32)0x40021000) #define FLASH ((u32)0x40022000) #define GPIOA ((u32)0x40010800) +#define GPIOB ((u32)0x40010C00) #define GPIOC ((u32)0x40011000) #define RCC_CR RCC @@ -41,6 +42,10 @@ #define RCC_AHBENR (RCC + 0x14) #define RCC_APB2ENR (RCC + 0x18) #define RCC_APB1ENR (RCC + 0x1C) +#define RCC_APB2ENR_GPIOA 0x00000004 +#define RCC_APB2ENR_GPIOB 0x00000008 +#define RCC_APB2ENR_GPIOC 0x00000010 +#define RCC_APB1ENR_USB 0x00800000 #define FLASH_ACR (FLASH + 0x00) #define FLASH_KEYR (FLASH + 0x04) diff --git a/usb.c b/usb.c index b6d2a67..57db187 100644 --- a/usb.c +++ b/usb.c @@ -36,25 +36,34 @@ void setupUSB(void) { u32 rwmVal; /* read-write-modify place holder var */ - /* Setup the USB DISC Pin */ + /* Setup APB2 for USB disconnect GPIO bank */ rwmVal = GET_REG(RCC_APB2ENR); - rwmVal |= 0x00000010; + rwmVal |= USB_DISC_RCC_APB2ENR_GPIO; SET_REG(RCC_APB2ENR, rwmVal); - // todo, macroize usb_disc pin - /* Setup GPIOC Pin 12 as OD out */ - rwmVal = GET_REG(GPIO_CRH(GPIOC)); - rwmVal &= 0xFFF0FFFF; - rwmVal |= 0x00050000; - setPin(GPIOC, 12); - SET_REG(GPIO_CRH(GPIOC), rwmVal); +#if (USB_DISC < 8) +# define USB_DISC_GPIO_CR GPIO_CRL(USB_DISC_BANK) +# define USB_DISC_CR_PORT (USB_DISC) +#else +# define USB_DISC_GPIO_CR GPIO_CRH(USB_DISC_BANK) +# define USB_DISC_CR_PORT (USB_DISC - 8) +#endif + + /* preset pin to high */ + setPin(USB_DISC_BANK, USB_DISC); - pRCC->APB1ENR |= 0x00800000; + /* Setup GPIO Pin as OD out */ + rwmVal = GET_REG(USB_DISC_GPIO_CR); + rwmVal &= ~(0xF << (USB_DISC_CR_PORT * 4)); + rwmVal |= (0x5 << (USB_DISC_CR_PORT * 4)); + SET_REG(USB_DISC_GPIO_CR, rwmVal); + + /* enable USB clock */ + pRCC->APB1ENR |= RCC_APB1ENR_USB; /* initialize the usb application */ - resetPin(GPIOC, 12); /* present ourselves to the host */ + resetPin(USB_DISC_BANK, USB_DISC); /* present ourselves to the host */ usbAppInit(); - } vu32 bDeviceState = UNCONNECTED; From a2c258fa4de0a60a2a49c0cd59cc1ceb5a575cb8 Mon Sep 17 00:00:00 2001 From: Tormod Volden Date: Sat, 10 May 2014 00:27:29 +0200 Subject: [PATCH 6/7] Allow passing PLATFORM to the build to specify target board type Example: make PLATFORM=MAPLE Signed-off-by: Tormod Volden --- Makefile | 4 ++++ config.h | 24 +++++++++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 4318bd9..9aa6ea6 100644 --- a/Makefile +++ b/Makefile @@ -32,6 +32,10 @@ CFLAGS += -Wpointer-arith -Wswitch CFLAGS += -Wredundant-decls -Wreturn-type -Wshadow -Wunused CFLAGS += -Wa,-adhlns=$(BUILDDIR)/$(subst $(suffix $<),.lst,$<) CFLAGS += $(patsubst %,-I%,$(INCDIRS)) +ifndef PLATFORM +PLATFORM = MAPLE +endif +CFLAGS += -D$(PLATFORM) # Aeembler Flags ASFLAGS = -Wa,-adhlns=$(BUILDDIR)/$(<:.s=.lst)#,--g$(DEBUG) diff --git a/config.h b/config.h index 98313a2..6cabc96 100644 --- a/config.h +++ b/config.h @@ -33,17 +33,23 @@ #ifndef __CONFIG_H #define __CONFIG_H -#define LED_BANK GPIOA -#define LED_RCC_APB2ENR_GPIO RCC_APB2ENR_GPIOA -#define LED 5 +#ifdef MAPLE -#define BUTTON_BANK GPIOC -#define BUTTON_RCC_APB2ENR_GPIO RCC_APB2ENR_GPIOC -#define BUTTON 9 +# define LED_BANK GPIOA +# define LED_RCC_APB2ENR_GPIO RCC_APB2ENR_GPIOA +# define LED 5 -#define USB_DISC_BANK GPIOC -#define USB_DISC_RCC_APB2ENR_GPIO RCC_APB2ENR_GPIOC -#define USB_DISC 12 +# define BUTTON_BANK GPIOC +# define BUTTON_RCC_APB2ENR_GPIO RCC_APB2ENR_GPIOC +# define BUTTON 9 + +# define USB_DISC_BANK GPIOC +# define USB_DISC_RCC_APB2ENR_GPIO RCC_APB2ENR_GPIOC +# define USB_DISC 12 + +#else +# error "Unknown platform" +#endif #define BLINK_FAST 0x50000 #define BLINK_SLOW 0x100000 From 9e3a8716af3cc927ad582aeb127d63bcf3a0888d Mon Sep 17 00:00:00 2001 From: Tormod Volden Date: Sat, 10 May 2014 00:46:27 +0200 Subject: [PATCH 7/7] Add MAPLE_MINI platform build option Example: make PLATFORM=MAPLE_MINI Signed-off-by: Tormod Volden --- config.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/config.h b/config.h index 6cabc96..f86968b 100644 --- a/config.h +++ b/config.h @@ -47,6 +47,20 @@ # define USB_DISC_RCC_APB2ENR_GPIO RCC_APB2ENR_GPIOC # define USB_DISC 12 +#elif defined MAPLE_MINI + +# define LED_BANK GPIOB +# define LED_RCC_APB2ENR_GPIO RCC_APB2ENR_GPIOB +# define LED 1 + +# define BUTTON_BANK GPIOB +# define BUTTON_RCC_APB2ENR_GPIO RCC_APB2ENR_GPIOB +# define BUTTON 8 + +# define USB_DISC_BANK GPIOB +# define USB_DISC_RCC_APB2ENR_GPIO RCC_APB2ENR_GPIOB +# define USB_DISC 9 + #else # error "Unknown platform" #endif