Skip to content

Unify platforms #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
37 changes: 31 additions & 6 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,41 @@
#ifndef __CONFIG_H
#define __CONFIG_H

#include "common.h"
#ifdef MAPLE

# 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

#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

#define LED_BANK GPIOA
#define LED 5
#define BLINK_FAST 0x50000
#define BLINK_SLOW 0x100000

#define BUTTON_BANK GPIOC
#define BUTTON 9

#define STARTUP_BLINKS 5
#define BOOTLOADER_WAIT 6

Expand Down
59 changes: 35 additions & 24 deletions hardware.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -96,40 +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 */
SET_REG(GPIO_CRL(GPIOA), 0x00100000);

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() {
Expand Down Expand Up @@ -166,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


Expand Down
6 changes: 6 additions & 0 deletions hardware.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -64,6 +69,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)
Expand Down
33 changes: 21 additions & 12 deletions usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down