From 09e89c867c8dc14adee298176a0a4c72d8f32245 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Tue, 7 Aug 2012 16:54:36 +0200 Subject: Split out reusable parts into the laks project. --- .gitmodules | 3 + SConstruct | 31 ++------ drivers/gps.h | 10 +-- drivers/l3gd20.h | 4 +- drivers/lsm303dlm.h | 2 +- drivers/ppmsum.cpp | 2 +- drivers/ppmsum.h | 4 +- drivers/xbee.cpp | 4 +- entry.cpp | 63 --------------- hal/fault.cpp | 41 ---------- hal/flash.cpp | 17 ---- hal/flash.h | 30 -------- hal/gpio.h | 43 ----------- hal/hal.h | 10 --- hal/i2c.cpp | 142 ---------------------------------- hal/i2c.h | 62 --------------- hal/interrupt.cpp | 187 -------------------------------------------- hal/interrupt.h | 121 ----------------------------- hal/pin.h | 137 --------------------------------- hal/rcc.cpp | 51 ------------ hal/rcc.h | 217 ---------------------------------------------------- hal/spi.h | 31 -------- hal/stm32.h | 89 --------------------- hal/timer.h | 40 ---------- hal/usart.cpp | 7 -- hal/usart.h | 69 ----------------- hal/usb.h | 93 ---------------------- laks | 1 + main.cpp | 15 ++-- openocd.cfg | 23 ------ os/mutex.h | 42 ---------- os/pool.cpp | 5 -- os/pool.h | 146 ----------------------------------- os/thread.cpp | 4 - os/thread.h | 63 --------------- os/time.cpp | 3 - os/time.h | 23 ------ suzumebachi.ld | 83 -------------------- 38 files changed, 30 insertions(+), 1888 deletions(-) create mode 100644 .gitmodules delete mode 100644 entry.cpp delete mode 100644 hal/fault.cpp delete mode 100644 hal/flash.cpp delete mode 100644 hal/flash.h delete mode 100644 hal/gpio.h delete mode 100644 hal/hal.h delete mode 100644 hal/i2c.cpp delete mode 100644 hal/i2c.h delete mode 100644 hal/interrupt.cpp delete mode 100644 hal/interrupt.h delete mode 100644 hal/pin.h delete mode 100644 hal/rcc.cpp delete mode 100644 hal/rcc.h delete mode 100644 hal/spi.h delete mode 100644 hal/stm32.h delete mode 100644 hal/timer.h delete mode 100644 hal/usart.cpp delete mode 100644 hal/usart.h delete mode 100644 hal/usb.h create mode 160000 laks delete mode 100644 openocd.cfg delete mode 100644 os/mutex.h delete mode 100644 os/pool.cpp delete mode 100644 os/pool.h delete mode 100644 os/thread.cpp delete mode 100644 os/thread.h delete mode 100644 os/time.cpp delete mode 100644 os/time.h delete mode 100644 suzumebachi.ld diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..d59e290 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "laks"] + path = laks + url = git://git.jvnv.net/laks diff --git a/SConstruct b/SConstruct index 9c906e0..d67ac16 100644 --- a/SConstruct +++ b/SConstruct @@ -2,33 +2,14 @@ import os env = Environment( ENV = os.environ, - - CC = 'arm-none-eabi-gcc', - CXX = 'arm-none-eabi-g++', - AS = 'arm-none-eabi-gcc', - CCFLAGS = '-O2 -Wall -ggdb -mcpu=cortex-m4 -mthumb -mhard-float -ffunction-sections -Wno-pmf-conversions', - CXXFLAGS = '-fno-exceptions -fno-rtti', - ASFLAGS = '-c -x assembler-with-cpp -mcpu=cortex-m4 -mthumb -mhard-float', - - CPPDEFINES = ['STM32F4'], - - LINK = 'arm-none-eabi-gcc', - LINKFLAGS = '-Wall -mcpu=cortex-m4 -mthumb -mhard-float -nostartfiles -Wl,-Tsuzumebachi.ld', # -Wl,--gc-sections - - AR = 'arm-none-eabi-ar', - RANLIB = 'arm-none-eabi-ranlib', - - CPPPATH = ['os', 'hal', 'drivers'], - #LIBPATH = [], - - LIBS = ['m'], ) -sources = Glob('os/*.cpp') + Glob('hal/*.cpp') + Glob('drivers/*.cpp') + Glob('*.cpp') +SConscript('laks/build_rules') -firmware = env.Program('suzumebachi.elf', sources) -env.Depends(firmware, 'suzumebachi.ld') +env.SelectMCU('stm32f405rg') -env.Command('prog', ['suzumebachi.elf'], 'openocd -f openocd.cfg -c flash_chip') +env.Append( + CPPPATH = ['drivers'], +) -Default('suzumebachi.elf') +env.Firmware('suzumebachi.elf', Glob('*.cpp') + Glob('drivers/*.cpp')) diff --git a/drivers/gps.h b/drivers/gps.h index 16a971b..2fcbf6d 100644 --- a/drivers/gps.h +++ b/drivers/gps.h @@ -1,12 +1,12 @@ #ifndef GPS_H #define GPS_H -#include "rcc.h" -#include "usart.h" -#include "interrupt.h" -#include "thread.h" +#include +#include +#include +#include -#include "pool.h" +#include struct GPSMsg { unsigned int n; diff --git a/drivers/l3gd20.h b/drivers/l3gd20.h index c7e8935..56dff08 100644 --- a/drivers/l3gd20.h +++ b/drivers/l3gd20.h @@ -1,8 +1,8 @@ #ifndef L3GD20_H #define L3GD20_H -#include "pin.h" -#include "spi.h" +#include +#include class L3GD20 { private: diff --git a/drivers/lsm303dlm.h b/drivers/lsm303dlm.h index 1c4956d..8d25b38 100644 --- a/drivers/lsm303dlm.h +++ b/drivers/lsm303dlm.h @@ -1,7 +1,7 @@ #ifndef LSM303DLM_H #define LSM303DLM_H -#include "i2c.h" +#include class LSM303DLM_A { private: diff --git a/drivers/ppmsum.cpp b/drivers/ppmsum.cpp index c1bd101..24d62ac 100644 --- a/drivers/ppmsum.cpp +++ b/drivers/ppmsum.cpp @@ -1,6 +1,6 @@ #include "ppmsum.h" -#include "rcc.h" +#include PPMSum* PPMSum::self = 0; diff --git a/drivers/ppmsum.h b/drivers/ppmsum.h index a522b57..98c2984 100644 --- a/drivers/ppmsum.h +++ b/drivers/ppmsum.h @@ -2,8 +2,8 @@ #define PPMSUM_H #include -#include "timer.h" -#include "interrupt.h" +#include +#include class PPMSum { friend void interrupt(); diff --git a/drivers/xbee.cpp b/drivers/xbee.cpp index efee619..d23903b 100644 --- a/drivers/xbee.cpp +++ b/drivers/xbee.cpp @@ -1,7 +1,7 @@ #include "xbee.h" -#include "usart.h" -#include "mutex.h" +#include +#include Mutex xbee_mutex; diff --git a/entry.cpp b/entry.cpp deleted file mode 100644 index ae58404..0000000 --- a/entry.cpp +++ /dev/null @@ -1,63 +0,0 @@ -#include -#include - -int main(); - -typedef void (*funcp_t)(); - -// Symbols from linker script. -extern uint32_t _data_rom; -extern uint32_t _data_start; -extern uint32_t _data_end; -extern uint32_t _bss_start; -extern uint32_t _bss_end; -extern funcp_t _init_array_start; -extern funcp_t _init_array_end; -extern funcp_t _fini_array_start; -extern funcp_t _fini_array_end; - -void __attribute__((naked)) entry() { - // Initialize HAL. - hal_init(); - - // Load .data from rom image. - uint32_t* rp = &_data_rom; - uint32_t* wp = &_data_start; - - while(wp < &_data_end) { - *wp++ = *rp++; - } - - // Clear .bss. - wp = &_bss_start; - - while(wp < &_bss_end) { - *wp++ = 0; - } - - // Call constructors. - funcp_t* fp = &_init_array_start; - - while(fp < &_init_array_end) { - (*fp++)(); - } - - // Call main(). - main(); - - // Call destructors. - fp = &_fini_array_start; - - while(fp < &_fini_array_end) { - (*fp++)(); - } - - // Halt. - while(1); -} - -extern "C" void __cxa_pure_virtual() { - while (1); -} - -void* __dso_handle = 0; diff --git a/hal/fault.cpp b/hal/fault.cpp deleted file mode 100644 index a176ae6..0000000 --- a/hal/fault.cpp +++ /dev/null @@ -1,41 +0,0 @@ -#include "interrupt.h" -#include "thread.h" -#include "time.h" - -inline void __attribute__((naked)) switch_context() { - asm volatile ("cpsid i"); - - // Save unsaved registers. - asm volatile ("push {r4, r5, r6, r7, r8, r9, r10, r11, lr}" ::: "memory"); - - // Store stack pointer for old thread. - asm volatile ("str sp, [%0]" :: "r" (&Thread::active_thread->sp)); - - // Update running thread. - Thread::active_thread = Thread::active_thread->next; - - // Fetch stack pointer for new thread. - asm volatile ("ldr sp, [%0]" :: "r" (&Thread::active_thread->sp)); - - asm volatile ("cpsie i"); - - // Load registers and return. - asm volatile ("pop {r4, r5, r6, r7, r8, r9, r10, r11, pc}" ::: "memory"); -} - -template<> -void interrupt() { - switch_context(); -} - -template<> -void interrupt() { - Time::tick(); -} - -template<> void interrupt() { while(1); } -template<> void interrupt() { while(1); } -template<> void interrupt() { while(1); } -template<> void interrupt() { while(1); } -template<> void interrupt() { while(1); } -template<> void interrupt() { while(1); } diff --git a/hal/flash.cpp b/hal/flash.cpp deleted file mode 100644 index 2b0fb69..0000000 --- a/hal/flash.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include "flash.h" - -void flash_init() { - #if defined(STM32F1) - - // Set flash latency. - FLASH.ACR = 0x12; - - #elif defined(STM32F4) - - // Set flash latency. - FLASH.ACR = 0x105; - - while(FLASH.ACR != 0x105); - - #endif -} diff --git a/hal/flash.h b/hal/flash.h deleted file mode 100644 index 30d30a5..0000000 --- a/hal/flash.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef FLASH_H -#define FLASH_H - -#include - -struct FLASH_t { - volatile uint32_t ACR; - volatile uint32_t KEYR; - volatile uint32_t OPTKEYR; - volatile uint32_t SR; - volatile uint32_t CR; - #if defined(STM32F1) - volatile uint32_t AR; - volatile uint32_t RESERVED; - volatile uint32_t OBR; - volatile uint32_t WRPR; - #elif defined(STM32F4) - volatile uint32_t OPTCR; - #endif -}; - -#if defined(STM32F1) -static FLASH_t& FLASH = *(FLASH_t*)0x40022000; -#elif defined(STM32F4) -static FLASH_t& FLASH = *(FLASH_t*)0x40023c00; -#endif - -void flash_init(); - -#endif diff --git a/hal/gpio.h b/hal/gpio.h deleted file mode 100644 index 324950e..0000000 --- a/hal/gpio.h +++ /dev/null @@ -1,43 +0,0 @@ -#ifndef GPIO_H -#define GPIO_H - -struct GPIO_t { - #if defined(STM32F1) - volatile uint32_t CRL; - volatile uint32_t CRH; - volatile uint32_t IDR; - volatile uint32_t ODR; - volatile uint32_t BSRR; - volatile uint32_t BRR; - volatile uint32_t LCKR; - #elif defined(STM32F4) - volatile uint32_t MODER; - volatile uint32_t OTYPER; - volatile uint32_t OSPEEDER; - volatile uint32_t PUPDR; - volatile uint32_t IDR; - volatile uint32_t ODR; - volatile uint32_t BSRR; - volatile uint32_t LCKR; - volatile uint32_t AFRL; - volatile uint32_t AFRH; - #endif -}; - -#if defined(STM32F1) -static GPIO_t& GPIOA = *(GPIO_t*)0x40010800; -static GPIO_t& GPIOB = *(GPIO_t*)0x40010c00; -static GPIO_t& GPIOC = *(GPIO_t*)0x40011000; -#elif defined(STM32F4) -static GPIO_t& GPIOA = *(GPIO_t*)0x40020000; -static GPIO_t& GPIOB = *(GPIO_t*)0x40020400; -static GPIO_t& GPIOC = *(GPIO_t*)0x40020800; -static GPIO_t& GPIOD = *(GPIO_t*)0x40020c00; -static GPIO_t& GPIOE = *(GPIO_t*)0x40021000; -static GPIO_t& GPIOF = *(GPIO_t*)0x40021400; -static GPIO_t& GPIOG = *(GPIO_t*)0x40021800; -static GPIO_t& GPIOH = *(GPIO_t*)0x40021c00; -static GPIO_t& GPIOI = *(GPIO_t*)0x40022000; -#endif - -#endif diff --git a/hal/hal.h b/hal/hal.h deleted file mode 100644 index 4076df3..0000000 --- a/hal/hal.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef HAL_H -#define HAL_H - -#include "rcc.h" - -void hal_init() { - rcc_init(); -} - -#endif diff --git a/hal/i2c.cpp b/hal/i2c.cpp deleted file mode 100644 index db9c919..0000000 --- a/hal/i2c.cpp +++ /dev/null @@ -1,142 +0,0 @@ -#include "i2c.h" - -#include "rcc.h" -#include "thread.h" - -#if defined(STM32F1) -I2C_t I2C1(0x40005400, 36000000, Interrupt::I2C1_EV, Interrupt::I2C1_ER); -I2C_t I2C2(0x40005800, 36000000, Interrupt::I2C2_EV, Interrupt::I2C2_ER); -#elif defined(STM32F4) -I2C_t I2C1(0x40005400, 42000000, Interrupt::I2C1_EV, Interrupt::I2C1_ER); -I2C_t I2C2(0x40005800, 42000000, Interrupt::I2C2_EV, Interrupt::I2C2_ER); -//I2C_t I2C3(0x40005c00, 42000000, Interrupt::I2C3_EV, Interrupt::I2C3_ER); -#endif - -void I2C_t::irq_ev() { - uint32_t sr1 = reg.SR1; - reg.SR2; - - // EV5, SB = 1: Start condition sent. - if(sr1 & 0x01) { - // Send address. - reg.DR = (addr << 1) | (writing ? 0 : 1); - } - - // EV6, ADDR = 1: Address sent. - if(sr1 & 0x02) { - if(writing) { - reg.DR = *write_p++; - writing--; - } else { - if(reading > 1) { - reg.CR1 |= 0x400; // Set ACK. - } else { - reg.CR1 |= 0x200; // Set STOP. - } - } - } - - // EV7, RxNE = 1: Receive buffer not empty. - if(sr1 & 0x40) { - *read_p++ = reg.DR; - reading--; - - if(reading == 1) { - // Unset ACK, set STOP. - reg.CR1 = (reg.CR1 & ~0x400) | 0x200; - } - - if(reading == 0) { - busy = 0; - } - } - - //reg.CR1 &= ~0x400; - - // EV8, TxE = 1, BTF = 0: Transmit buffer empty, still writing. - if(sr1 & 0x80 && !(sr1 & 0x04)) { - if(writing) { - // Send data. - reg.DR = *write_p++; - writing--; - } else { - // All data sent. - - if(reading) { - // Send repeat start. - reg.CR1 |= 0x100; - } else { - // Send stop. - reg.CR1 |= 0x200; - busy = 0; - } - } - } -} - -void I2C_t::irq_er() { - handle_error(); -} - -void I2C_t::handle_error() { - reg.SR1; - reg.SR2; - - //while(1); - reg.CR1 |= 0x200; - busy = 0; -} - -void I2C_t::enable(Pin& scl, Pin& sda) { - RCC.enable(RCC.I2C1); - asm volatile("nop"); - - scl.set_af(4); - sda.set_af(4); - scl.set_type(Pin::OpenDrain); - sda.set_type(Pin::OpenDrain); - scl.set_mode(Pin::AF); - sda.set_mode(Pin::AF); - - reg.CR1 = 0x8000; - reg.CR1 = 0; - - reg.CR2 = 0x700 | (clk / 1000000); - reg.TRISE = clk / 1000000 + 1; - reg.CCR = clk / 2 / 100000; - - Interrupt::enable(irq_ev_n, &I2C_t::irq_ev, this); - Interrupt::enable(irq_er_n, &I2C_t::irq_er, this); - - reg.CR1 = 1; -} - -void I2C_t::write_reg(uint8_t addr_, uint8_t reg_, uint8_t data) { - addr = addr_; - writing = 2; - reading = 0; - volatile uint8_t buf[] = {reg_, data}; - write_p = buf; - busy = 1; - - reg.CR1 |= 0x100; - - while(busy) { - Thread::yield(); - } -} - -void I2C_t::read_reg(uint8_t addr_, uint8_t reg_, uint8_t len, uint8_t* buf) { - addr = addr_; - writing = 1; - reading = len; - write_p = ®_; - read_p = buf; - busy = 1; - - reg.CR1 |= 0x100; - - while(busy) { - Thread::yield(); - } -} diff --git a/hal/i2c.h b/hal/i2c.h deleted file mode 100644 index a61950c..0000000 --- a/hal/i2c.h +++ /dev/null @@ -1,62 +0,0 @@ -#ifndef I2C_H -#define I2C_H - -#include -#include "interrupt.h" -#include "pin.h" - -struct I2C_reg_t { - volatile uint32_t CR1; - volatile uint32_t CR2; - volatile uint32_t OAR1; - volatile uint32_t OAR2; - volatile uint32_t DR; - volatile uint32_t SR1; - volatile uint32_t SR2; - volatile uint32_t CCR; - volatile uint32_t TRISE; -}; - -class I2C_t { - private: - volatile uint8_t addr; - volatile uint8_t writing; - volatile uint8_t reading; - volatile uint8_t* write_p; - volatile uint8_t* read_p; - - volatile bool busy; - - public: - I2C_reg_t& reg; - const uint32_t clk; - Interrupt::IRQ irq_ev_n; - Interrupt::IRQ irq_er_n; - - I2C_t(uint32_t reg_addr, uint32_t bus_clk, Interrupt::IRQ ev_n, Interrupt::IRQ er_n) : reg(*(I2C_reg_t*)reg_addr), clk(bus_clk), irq_ev_n(ev_n), irq_er_n(er_n) { - reading = writing = 0; - } - - void irq_ev(); - void irq_er(); - - void handle_error(); - - void enable(Pin& scl, Pin& sda); - - void write_reg(uint8_t addr_, uint8_t reg_, uint8_t data); - void read_reg(uint8_t addr_, uint8_t reg_, uint8_t len, uint8_t* buf); -}; - -#if defined(STM32F1) -extern I2C_t I2C1; -extern I2C_t I2C2; -#elif defined(STM32F4) -extern I2C_t I2C1; -extern I2C_t I2C2; -extern I2C_t I2C3; -#endif - -typedef I2C_t I2C; - -#endif diff --git a/hal/interrupt.cpp b/hal/interrupt.cpp deleted file mode 100644 index cebfed7..0000000 --- a/hal/interrupt.cpp +++ /dev/null @@ -1,187 +0,0 @@ -#include "interrupt.h" - -namespace Interrupt { - MFP mf_vectors[16 + NUM_IRQs]; -}; - -void entry(); - -void member_function_interrupt_gate() { - uint32_t interrupt_num; - asm ("mrs %0, ipsr" : "=r" (interrupt_num)); - - Interrupt::mf_vectors[interrupt_num].func_p(Interrupt::mf_vectors[interrupt_num].instance_p); -} - -extern "C" void unused_interrupt() { - member_function_interrupt_gate(); - //while(1); -} - -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt() __attribute__ ((weak, alias ("unused_interrupt"))); - -typedef void (*vector_t)(); - -vector_t vectors[] __attribute__((section(".vectors"))) = { - (vector_t)0x20004ffc, - entry, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - 0, - 0, - 0, - 0, - interrupt, - 0, - 0, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - interrupt, - 0, // 60 - 0, // 61 - 0, // 62 - 0, // 63 - 0, // 64 - 0, // 65 - 0, // 66 - 0, // 67 - 0, // 68 - 0, // 69 - 0, // 70 - 0, // 71 - 0, // 72 - 0, // 73 - 0, // 74 - 0, // 75 - 0, // 76 - interrupt<(Interrupt::IRQ)77>, // 77 -}; diff --git a/hal/interrupt.h b/hal/interrupt.h deleted file mode 100644 index 34651f2..0000000 --- a/hal/interrupt.h +++ /dev/null @@ -1,121 +0,0 @@ -#ifndef INTERRUPT_H -#define INTERRUPT_H - -#include "stm32.h" - -namespace Interrupt { - enum Exception { - NMI = 2, - HardFault = 3, - MemManage = 4, - BusFault = 5, - UsageFault = 6, - SVCall = 11, - PendSV = 14, - SysTick = 15 - }; - - enum IRQ { - WWDG, - PVD, - TAMPER, - RTC, - FLASH, - RCC, - EXTI0, - EXTI1, - EXTI2, - EXTI3, - EXTI4, - DMA1_Channel1, - DMA1_Channel2, - DMA1_Channel3, - DMA1_Channel4, - DMA1_Channel5, - DMA1_Channel6, - DMA1_Channel7, - ADC1_2, - USB_HP_CAN_TX, - USB_LP_CAN_RX0, - CAN_RX1, - CAN_SCE, - EXTI9_5, - TIM1_BRK, - TIM1_UP, - TIM1_TRG_COM, - TIM1_CC, - TIM2, - TIM3, - TIM4, - I2C1_EV, - I2C1_ER, - I2C2_EV, - I2C2_ER, - SPI1, - SPI2, - USART1, - USART2, - USART3, - EXTI15_10, - RTCAlarm, - USBWakeup, - TIM8_BRK, - TIM8_UP, - TIM8_TRG_COM, - TIM8_CC, - ADC3, - FSMC, - SDIO, - TIM5, - SPI3, - UART4, - UART5, - TIM6, - TIM7, - DMA2_Channel1, - DMA2_Channel2, - DMA2_Channel3, - DMA2_Channel4_5, - NUM_IRQs - }; - - inline void enable(IRQ n) { - NVIC.ISER[n >> 5] = 1 << (n & 0x1f); - } - - inline void set_priority(Exception n, uint8_t priority) { - SCB.SHPR[n - 4] = priority; - } - - inline void set_priority(IRQ n, uint8_t priority) { - NVIC.IPR[n] = priority; - } - - struct MFP { - void (*func_p)(void*); - void* instance_p; - }; - - extern MFP mf_vectors[]; - - template - inline void set_handler(IRQ n, void (T::*f)(), T* i) { - MFP& mfp = mf_vectors[16 + n]; - mfp.func_p = reinterpret_cast(f); - mfp.instance_p = i; - } - - template - inline void enable(IRQ n, void (T::*f)(), T* i) { - set_handler(n, f, i); - enable(n); - } -}; - -template -void interrupt(); - -template -void interrupt(); - -#endif diff --git a/hal/pin.h b/hal/pin.h deleted file mode 100644 index 1ad3ca2..0000000 --- a/hal/pin.h +++ /dev/null @@ -1,137 +0,0 @@ -#ifndef PIN_H -#define PIN_H - -#include "gpio.h" - -class Pin { - private: - GPIO_t& g; - int n; - - public: - Pin(GPIO_t& gpio, int pin) : g(gpio), n(pin) {} - - enum Mode { - Input, - Output, - AF, - Analog, - }; - - enum Type { - PushPull, - OpenDrain, - }; - - enum Pull { - PullNone, - PullUp, - PullDown, - }; - - void set_mode(Mode m) { - g.MODER = (g.MODER & ~(3 << (n * 2))) | m << (n * 2); - } - - void set_type(Type t) { - if(t) { - g.OTYPER |= 1 << n; - } else { - g.OTYPER &= ~(1 << n); - } - } - - void set_pull(Pull p) { - g.PUPDR = (g.PUPDR & ~(3 << (n * 2))) | p << (n * 2); - } - - void set_af(int af) { - if(n < 8) { - g.AFRL = (g.AFRL & ~(0xf << (n * 4))) | af << (n * 4); - } else { - g.AFRH = (g.AFRH & ~(0xf << (n * 4 - 32))) | af << (n * 4 - 32); - } - } - - void on() { - g.BSRR = 1 << n; - } - - void off() { - g.BSRR = 1 << 16 << n; - } - - void set(bool value) { - if(value) { - on(); - } else { - off(); - } - } - - bool get() { - return g.IDR & (1 << n); - } - - void toggle() { - set(!(g.ODR & (1 << n))); - } -}; - -static Pin PA0(GPIOA, 0); -static Pin PA1(GPIOA, 1); -static Pin PA2(GPIOA, 2); -static Pin PA3(GPIOA, 3); -static Pin PA4(GPIOA, 4); -static Pin PA5(GPIOA, 5); -static Pin PA6(GPIOA, 6); -static Pin PA7(GPIOA, 7); -static Pin PA8(GPIOA, 8); -static Pin PA9(GPIOA, 9); -static Pin PA10(GPIOA, 10); -static Pin PA11(GPIOA, 11); -static Pin PA12(GPIOA, 12); -static Pin PA13(GPIOA, 13); -static Pin PA14(GPIOA, 14); -static Pin PA15(GPIOA, 15); - -static Pin PB0(GPIOB, 0); -static Pin PB1(GPIOB, 1); -static Pin PB2(GPIOB, 2); -static Pin PB3(GPIOB, 3); -static Pin PB4(GPIOB, 4); -static Pin PB5(GPIOB, 5); -static Pin PB6(GPIOB, 6); -static Pin PB7(GPIOB, 7); -static Pin PB8(GPIOB, 8); -static Pin PB9(GPIOB, 9); -static Pin PB10(GPIOB, 10); -static Pin PB11(GPIOB, 11); -static Pin PB12(GPIOB, 12); -static Pin PB13(GPIOB, 13); -static Pin PB14(GPIOB, 14); -static Pin PB15(GPIOB, 15); - -static Pin PC0(GPIOC, 0); -static Pin PC1(GPIOC, 1); -static Pin PC2(GPIOC, 2); -static Pin PC3(GPIOC, 3); -static Pin PC4(GPIOC, 4); -static Pin PC5(GPIOC, 5); -static Pin PC6(GPIOC, 6); -static Pin PC7(GPIOC, 7); -static Pin PC8(GPIOC, 8); -static Pin PC9(GPIOC, 9); -static Pin PC10(GPIOC, 10); -static Pin PC11(GPIOC, 11); -static Pin PC12(GPIOC, 12); -static Pin PC13(GPIOC, 13); -static Pin PC14(GPIOC, 14); -static Pin PC15(GPIOC, 15); - -static Pin PD12(GPIOD, 12); -static Pin PD13(GPIOD, 13); -static Pin PD14(GPIOD, 14); -static Pin PD15(GPIOD, 15); - -#endif diff --git a/hal/rcc.cpp b/hal/rcc.cpp deleted file mode 100644 index 57b8f7d..0000000 --- a/hal/rcc.cpp +++ /dev/null @@ -1,51 +0,0 @@ -#include "rcc.h" -#include "flash.h" - -void rcc_init() { - // Initialize flash. - flash_init(); - - #if defined(STM32F1) - - // Enable HSE. - RCC.CR |= 0x10000; - while(RCC.CR & 0x20000); - - // Configure and enable PLL. - RCC.CFGR = 0x1d0000; - RCC.CR |= 0x1000000; - while(!(RCC.CR & 0x2000000)); - - // Switch to PLL. - RCC.CFGR |= 0x2; - while(!(RCC.CFGR & 0x8)); - - // Set APB1 prescaler to /2. - RCC.CFGR |= 0x400; - - // Set ADCCLK prescaler to /6. - RCC.CFGR |= 0x8000; - - #elif defined(STM32F4) - - // Enable HSE. - RCC.CR |= 0x10000; - while(!(RCC.CR & 0x20000)); - - // Configure and enable PLL. - RCC.PLLCFGR = 0x20400000 | (7 << 24) | (2 * 168 << 6) | 8; - RCC.CR |= 0x1000000; - while(!(RCC.CR & 0x2000000)); - - // Switch to PLL. - RCC.CFGR |= 0x2; - while(!(RCC.CFGR & 0x8)); - - // Set APB1 prescaler to /4. - RCC.CFGR |= 5 << 10; - - // Set APB2 prescaler to /2. - RCC.CFGR |= 4 << 13; - - #endif -} diff --git a/hal/rcc.h b/hal/rcc.h deleted file mode 100644 index 4b8d99f..0000000 --- a/hal/rcc.h +++ /dev/null @@ -1,217 +0,0 @@ -#ifndef RCC_H -#define RCC_H - -#include - -struct RCC_t { - #if defined(STM32F1) - volatile uint32_t CR; - volatile uint32_t CFGR; - volatile uint32_t CIR; - volatile uint32_t APB2RSTR; - volatile uint32_t APB1RSTR; - volatile uint32_t AHBENR; - volatile uint32_t APB2ENR; - volatile uint32_t APB1ENR; - volatile uint32_t BDCR; - volatile uint32_t CSR; - #elif defined(STM32F4) - volatile uint32_t CR; - volatile uint32_t PLLCFGR; - volatile uint32_t CFGR; - volatile uint32_t CIR; - volatile uint32_t AHB1RSTR; - volatile uint32_t AHB2RSTR; - volatile uint32_t AHB3RSTR; - volatile uint32_t AHB4RSTR; // Reserved - volatile uint32_t APB1RSTR; - volatile uint32_t APB2RSTR; - volatile uint32_t APB3RSTR; // Reserved - volatile uint32_t APB4RSTR; // Reserved - volatile uint32_t AHB1ENR; - volatile uint32_t AHB2ENR; - volatile uint32_t AHB3ENR; - volatile uint32_t AHB4ENR; // Reserved - volatile uint32_t APB1ENR; - volatile uint32_t APB2ENR; - volatile uint32_t APB3ENR; // Reserved - volatile uint32_t APB4ENR; // Reserved - volatile uint32_t AHB1LPENR; - volatile uint32_t AHB2LPENR; - volatile uint32_t AHB3LPENR; - volatile uint32_t AHB4LPENR; // Reserved - volatile uint32_t APB1LPENR; - volatile uint32_t APB2LPENR; - volatile uint32_t APB3LPENR; // Reserved - volatile uint32_t APB4LPENR; // Reserved - volatile uint32_t BDCR; - volatile uint32_t CSR; - volatile uint32_t _1; - volatile uint32_t _2; - volatile uint32_t SSCGR; - volatile uint32_t PLLI2SCFGR; - #endif - - #if defined(STM32F1) - enum AHB_dev { - DMA1 = 1 << 0, - DMA2 = 1 << 1, - SRAM = 1 << 2, - FLITF = 1 << 4, - CRC = 1 << 6, - FSMC = 1 << 8, - SDIO = 1 << 10 - }; - - enum APB1_dev { - TIM2 = 1 << 0, - TIM3 = 1 << 1, - TIM4 = 1 << 2, - TIM5 = 1 << 3, - TIM6 = 1 << 4, - TIM7 = 1 << 5, - TIM12 = 1 << 6, - TIM13 = 1 << 7, - TIM14 = 1 << 8, - WWDG = 1 << 11, - SPI2 = 1 << 14, - SPI3 = 1 << 15, - USART2 = 1 << 17, - USART3 = 1 << 18, - UART4 = 1 << 19, - UART5 = 1 << 20, - I2C1 = 1 << 21, - I2C2 = 1 << 22, - USB = 1 << 23, - CAN = 1 << 25, - BKP = 1 << 27, - PWR = 1 << 28, - DAC = 1 << 29 - }; - - enum APB2_dev { - AFIO = 1 << 0, - IOPA = 1 << 2, - IOPB = 1 << 3, - IOPC = 1 << 4, - IOPD = 1 << 5, - IOPE = 1 << 6, - IOPF = 1 << 7, - IOPG = 1 << 8, - ADC1 = 1 << 9, - ADC2 = 1 << 10, - TIM1 = 1 << 11, - SPI1 = 1 << 12, - TIM8 = 1 << 13, - USART1 = 1 << 14, - ADC3 = 1 << 15, - TIM9 = 1 << 19, - TIM10 = 1 << 20, - TIM11 = 1 << 21 - }; - #elif defined(STM32F4) - enum AHB1_dev { - GPIOA = 1 << 0, - GPIOB = 1 << 1, - GPIOC = 1 << 2, - GPIOD = 1 << 3, - GPIOE = 1 << 4, - GPIOF = 1 << 5, - GPIOG = 1 << 6, - GPIOH = 1 << 7, - GPIOI = 1 << 8, - CRC = 1 << 12, - DMA1 = 1 << 21, - DMA2 = 1 << 22, - ETHMAC = 1 << 25, - OTGHS = 1 << 29, - }; - - enum AHB2_dev { - DCMI = 1 << 0, - CRYP = 1 << 4, - HASH = 1 << 5, - RNG = 1 << 6, - OTGFS = 1 << 7, - }; - - enum AHB3_dev { - FSMC = 1 << 0, - }; - - enum APB1_dev { - TIM2 = 1 << 0, - TIM3 = 1 << 1, - TIM4 = 1 << 2, - TIM5 = 1 << 3, - TIM6 = 1 << 4, - TIM7 = 1 << 5, - TIM12 = 1 << 6, - TIM13 = 1 << 7, - TIM14 = 1 << 8, - WWDG = 1 << 11, - SPI2 = 1 << 14, - SPI3 = 1 << 15, - USART2 = 1 << 17, - USART3 = 1 << 18, - UART4 = 1 << 19, - UART5 = 1 << 20, - I2C1 = 1 << 21, - I2C2 = 1 << 22, - I2C3 = 1 << 23, - CAN1 = 1 << 25, - CAN2 = 1 << 26, - PWR = 1 << 28, - DAC = 1 << 29, - }; - - enum APB2_dev { - TIM1 = 1 << 0, - TIM8 = 1 << 1, - USART1 = 1 << 4, - USART6 = 1 << 5, - ADC = 1 << 8, - SDIO = 1 << 11, - SPI1 = 1 << 12, - SYSCFG = 1 << 14, - TIM9 = 1 << 16, - TIM10 = 1 << 17, - TIM11 = 1 << 18, - }; - #endif - - #if defined(STM32F1) - inline void enable(AHB_dev dev) { - AHBENR |= dev; - } - #elif defined(STM32F4) - inline void enable(AHB1_dev dev) { - AHB1ENR |= dev; - } - - inline void enable(AHB2_dev dev) { - AHB2ENR |= dev; - } - - inline void enable(AHB3_dev dev) { - AHB3ENR |= dev; - } - #endif - inline void enable(APB1_dev dev) { - APB1ENR |= dev; - } - - inline void enable(APB2_dev dev) { - APB2ENR |= dev; - } -}; - -#if defined(STM32F1) -static RCC_t& RCC = *(RCC_t*)0x40021000; -#elif defined(STM32F4) -static RCC_t& RCC = *(RCC_t*)0x40023800; -#endif - -void rcc_init(); - -#endif diff --git a/hal/spi.h b/hal/spi.h deleted file mode 100644 index 9f9cf63..0000000 --- a/hal/spi.h +++ /dev/null @@ -1,31 +0,0 @@ -#ifndef SPI_H -#define SPI_H - -struct SPI_reg_t { - volatile uint32_t CR1; - volatile uint32_t CR2; - volatile uint32_t SR; - volatile uint32_t DR; - volatile uint32_t CRCPR; - volatile uint32_t RXCRCR; - volatile uint32_t TXCRCR; - volatile uint32_t I2SCFGR; - volatile uint32_t I2SPR; -}; - -class SPI_t { - public: - SPI_reg_t& reg; - - SPI_t(uint32_t reg_addr) : reg(*(SPI_reg_t*)reg_addr) {} -}; - -#if defined(STM32F1) - -#elif defined(STM32F4) -static SPI_t SPI1(0x40013000); -static SPI_t SPI2(0x40003800); -static SPI_t SPI3(0x40003c00); -#endif - -#endif diff --git a/hal/stm32.h b/hal/stm32.h deleted file mode 100644 index 68848d8..0000000 --- a/hal/stm32.h +++ /dev/null @@ -1,89 +0,0 @@ -#ifndef STM32_H -#define STM32_H - -#include - -struct NVIC_t { - volatile uint32_t ISER[32]; - volatile uint32_t ICER[32]; - volatile uint32_t ISPR[32]; - volatile uint32_t ICPR[32]; - volatile uint32_t IABR[64]; - volatile uint8_t IPR[2816]; - volatile uint32_t STIR; -}; - -static NVIC_t& NVIC = *(NVIC_t*)0xe000e100; - -struct SCB_t { - volatile uint32_t CPUID; - volatile uint32_t ICSR; - volatile uint32_t VTOR; - volatile uint32_t AIRCR; - volatile uint32_t SCR; - volatile uint32_t CCR; - volatile uint8_t SHPR[12]; - volatile uint32_t SHCSR; - volatile uint32_t CFSR; - volatile uint32_t HFSR; - volatile uint32_t DFSR; - volatile uint32_t MMAR; - volatile uint32_t BFAR; -}; - -static SCB_t& SCB = *(SCB_t*)0xe000ed00; - -struct STK_t { - volatile uint32_t CTRL; - volatile uint32_t LOAD; - volatile uint32_t VAL; - volatile uint32_t CALIB; -}; - -static STK_t& STK = *(STK_t*)0xe000e010; - -struct ADC_t { - volatile uint32_t SR; - volatile uint32_t CR1; - volatile uint32_t CR2; - volatile uint32_t SMPR1; - volatile uint32_t SMPR2; - volatile uint32_t JOFR1; - volatile uint32_t JOFR2; - volatile uint32_t JOFR3; - volatile uint32_t JOFR4; - volatile uint32_t HTR; - volatile uint32_t LTR; - volatile uint32_t SQR1; - volatile uint32_t SQR2; - volatile uint32_t SQR3; - volatile uint32_t JSQR; - volatile uint32_t JDR1; - volatile uint32_t JDR2; - volatile uint32_t JDR3; - volatile uint32_t JDR4; - volatile uint32_t DR; -}; - -static ADC_t& ADC1 = *(ADC_t*)0x40012400; -static ADC_t& ADC2 = *(ADC_t*)0x40012800; -static ADC_t& ADC3 = *(ADC_t*)0x40013c00; - -struct DMA_t { - struct CH_t { - volatile uint32_t CCR; - volatile uint32_t CNDTR; - volatile uint32_t CPAR; - volatile uint32_t CMAR; - uint32_t _reserved; - }; - - volatile uint32_t ISR; - volatile uint32_t IFCR; - CH_t CH[7]; -}; - -static DMA_t& DMA1 = *(DMA_t*)0x40020000; -static DMA_t& DMA2 = *(DMA_t*)0x40020400; - -#endif diff --git a/hal/timer.h b/hal/timer.h deleted file mode 100644 index f9305de..0000000 --- a/hal/timer.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef TIMER_H -#define TIMER_H - -struct TIM_t { - volatile uint32_t CR1; - volatile uint32_t CR2; - volatile uint32_t SMCR; - volatile uint32_t DIER; - volatile uint32_t SR; - volatile uint32_t EGR; - volatile uint32_t CCMR1; - volatile uint32_t CCMR2; - volatile uint32_t CCER; - volatile uint32_t CNT; - volatile uint32_t PSC; - volatile uint32_t ARR; - volatile uint32_t RCR; - volatile uint32_t CCR1; - volatile uint32_t CCR2; - volatile uint32_t CCR3; - volatile uint32_t CCR4; - volatile uint32_t BDTR; - volatile uint32_t DCR; - volatile uint32_t DMAR; -}; - -#if defined(STM32F1) -static TIM_t& TIM1 = *(TIM_t*)0x40012c00; -static TIM_t& TIM2 = *(TIM_t*)0x40000000; -static TIM_t& TIM3 = *(TIM_t*)0x40000400; -static TIM_t& TIM4 = *(TIM_t*)0x40000800; -static TIM_t& TIM5 = *(TIM_t*)0x40000c00; -static TIM_t& TIM6 = *(TIM_t*)0x40001000; -static TIM_t& TIM7 = *(TIM_t*)0x40001400; -static TIM_t& TIM8 = *(TIM_t*)0x40013400; -#elif defined(STM32F4) -static TIM_t& TIM2 = *(TIM_t*)0x40000000; -#endif - -#endif diff --git a/hal/usart.cpp b/hal/usart.cpp deleted file mode 100644 index 61fe393..0000000 --- a/hal/usart.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include "usart.h" - -template<> -void interrupt() { - USART1.recv(); - //GPIOB.ODR ^= 1 << 1; -} diff --git a/hal/usart.h b/hal/usart.h deleted file mode 100644 index 24618fd..0000000 --- a/hal/usart.h +++ /dev/null @@ -1,69 +0,0 @@ -#ifndef USART_H -#define USART_H - -#include "rcc.h" -#include "stm32.h" -#include "interrupt.h" -#include "thread.h" - -struct USART_reg_t { - volatile uint32_t SR; - volatile uint32_t DR; - volatile uint32_t BRR; - volatile uint32_t CR1; - volatile uint32_t CR2; - volatile uint32_t CR3; - volatile uint32_t GTPR; -}; - -class USART_t { - public: - USART_reg_t& reg; - const uint32_t clk; - - USART_t(uint32_t reg_addr, uint32_t bus_clk) : reg(*(USART_reg_t*)reg_addr), clk(bus_clk) {} - - inline void set_baudrate(uint32_t baudrate) { - reg.BRR = clk / baudrate; - } - - inline void enable() { - reg.CR1 = 0x202c; - } - - inline void send(uint8_t data) { - while(!(reg.SR & 0x80)) { - Thread::yield(); - } // Wait for TXE. - - reg.DR = data; - } - - inline uint8_t recv() { - return reg.DR; - } -}; - -#if defined(STM32F1) -static USART_t USART1(0x40013800, 72000000); -static USART_t USART2(0x40004400, 36000000); -static USART_t USART3(0x40004800, 36000000); -#elif defined(STM32F4) -static USART_t USART1(0x40011000, 84000000); -static USART_t USART2(0x40004400, 42000000); -static USART_t USART3(0x40004800, 42000000); -#endif - -inline void usart_enable() { - RCC.enable(RCC.USART1); - USART1.set_baudrate(115200); - USART1.enable(); - - //Interrupt::enable(Interrupt::USART1); -} - -inline void usart_send(uint8_t data) { - USART1.send(data); -} - -#endif diff --git a/hal/usb.h b/hal/usb.h deleted file mode 100644 index c8c550f..0000000 --- a/hal/usb.h +++ /dev/null @@ -1,93 +0,0 @@ -#ifndef USB_H -#define USB_H - -#include - -struct USB_reg_t { - volatile uint32_t GOTGCTL; - volatile uint32_t GOTGINT; - volatile uint32_t GAHBCFG; - volatile uint32_t GUSBCFG; - volatile uint32_t GRSTCTL; - volatile uint32_t GINTSTS; - volatile uint32_t GINTMSK; - volatile uint32_t GRXSTSR; - volatile uint32_t GRXSTSP; - volatile uint32_t GRXFSIZ; - volatile uint32_t DIEPTXF0; - volatile uint32_t HNPTXSTS; - uint32_t _reserved[2]; - volatile uint32_t GCCFG; - volatile uint32_t CID; - uint32_t _reserved1[48]; - volatile uint32_t HPTXFSIZ; - volatile uint32_t DIEPTXF1; - volatile uint32_t DIEPTXF2; - volatile uint32_t DIEPTXF3; -}; - -struct USB_dev_reg_t { - volatile uint32_t DCFG; - volatile uint32_t DCTL; - volatile uint32_t DSTS; - uint32_t _reserved; - volatile uint32_t DIEPMSK; - volatile uint32_t DOEPMSK; - volatile uint32_t DAINT; - volatile uint32_t DAINTMSK; - uint32_t _reserved1[2]; - volatile uint32_t DVBUSDIS; - volatile uint32_t DVBUSPULSE; - uint32_t _reserved2; - volatile uint32_t DIEPEMPMSK; -}; - -struct USB_dev_iep_reg_t { - volatile uint32_t DIEPCTL; - uint32_t _reserved; - volatile uint32_t DIEPINT; - uint32_t _reserved1; - volatile uint32_t DIEPTSIZ; - uint32_t _reserved2; - volatile uint32_t DTXFSTS; - uint32_t _reserved3; -}; - -struct USB_dev_oep_reg_t { - volatile uint32_t DOEPCTL; - uint32_t _reserved; - volatile uint32_t DOEPINT; - uint32_t _reserved1; - volatile uint32_t DOEPTSIZ; - uint32_t _reserved2[3]; -}; - -union USB_fifo_reg_t { - volatile uint32_t reg; - volatile uint32_t buf[1024]; -}; - -class USB_t { - public: - USB_reg_t& reg; - USB_dev_reg_t& dev_reg; - USB_dev_iep_reg_t* const dev_iep_reg; - USB_dev_oep_reg_t* const dev_oep_reg; - USB_fifo_reg_t* const fifo; - - USB_t(uint32_t reg_addr) : - reg(*(USB_reg_t*)reg_addr), - dev_reg(*(USB_dev_reg_t*)(reg_addr + 0x800)), - dev_iep_reg((USB_dev_iep_reg_t*)(reg_addr + 0x900)), - dev_oep_reg((USB_dev_oep_reg_t*)(reg_addr + 0xb00)), - fifo((USB_fifo_reg_t*)(reg_addr + 0x1000)) {} -}; - -#if defined(STM32F1) - -#elif defined(STM32F4) -static USB_t OTG_FS(0x50000000); -static USB_t OTG_HS(0x40040000); -#endif - -#endif diff --git a/laks b/laks new file mode 160000 index 0000000..e586c17 --- /dev/null +++ b/laks @@ -0,0 +1 @@ +Subproject commit e586c178073b9a0fee90d5fc8e795d266ebd7b7d diff --git a/main.cpp b/main.cpp index 723714a..51d4c15 100644 --- a/main.cpp +++ b/main.cpp @@ -1,13 +1,12 @@ -#include "stm32.h" -#include "rcc.h" -#include "interrupt.h" -#include "thread.h" -#include "time.h" +#include +#include +#include +#include -#include "pin.h" +#include -#include "usb.h" -#include "i2c.h" +#include +#include #include "lsm303dlm.h" #include "l3gd20.h" diff --git a/openocd.cfg b/openocd.cfg deleted file mode 100644 index 1321413..0000000 --- a/openocd.cfg +++ /dev/null @@ -1,23 +0,0 @@ -telnet_port 4444 -gdb_port 3333 - -interface ft2232 - -ft2232_vid_pid 0x1457 0x5118 -ft2232_layout "jtagkey_prototype_v1" -ft2232_device_desc "USB<=>JTAG&RS232" - -reset_config trst_and_srst - -source [find target/stm32f1x.cfg] - -proc flash_chip {} { - halt - stm32x mass_erase 0 - reset halt - flash write_image suzumebachi.elf 0 elf - reset - shutdown -} - -init diff --git a/os/mutex.h b/os/mutex.h deleted file mode 100644 index d12331d..0000000 --- a/os/mutex.h +++ /dev/null @@ -1,42 +0,0 @@ -#ifndef MUTEX_H -#define MUTEX_H - -class Mutex { - private: - uint8_t locked; - public: - Mutex() : locked(0) {} - Mutex(uint8_t l) : locked(l) {} - - bool trylock() { - uint8_t val; - - // Check if mutex is locked. - asm volatile ("ldrexb %0, [%1]" : "=r" (val) : "r" (&locked)); - if(val) { - return false; - } - - // Try taking the lock. - asm volatile ("strexb %0, %1, [%2]" : "=r" (val) : "r" (1), "r" (&locked)); - if(val) { - return false; - } - - asm volatile("dmb"); - return true; - } - - void lock() { - while(!trylock()) { - Thread::yield(); - } - } - - void unlock() { - asm volatile("dmb"); - locked = 0; - } -}; - -#endif diff --git a/os/pool.cpp b/os/pool.cpp deleted file mode 100644 index f059b05..0000000 --- a/os/pool.cpp +++ /dev/null @@ -1,5 +0,0 @@ -#include "pool.h" - -void* operator new(unsigned int, char* buf) { - return (void*)buf; -} diff --git a/os/pool.h b/os/pool.h deleted file mode 100644 index 1cb0a71..0000000 --- a/os/pool.h +++ /dev/null @@ -1,146 +0,0 @@ -#ifndef POOL_H -#define POOL_H - -#include "stdint.h" - -template -class BasePool { - public: - struct Element { - unsigned int use_count; - BasePool* pool; - - char data[sizeof(T)]; - }; - - virtual void free(Element* e) = 0; -}; - -template -class P { - private: - typedef typename BasePool::Element Element; - - Element* e; - - void inc() { - e->use_count++; - } - - void dec() { - e->use_count--; - if(!e->use_count) { - T* p = (T*)e->data; - p->~T(); - e->pool->free(e); - } - } - - public: - P() : e(0) {} - - explicit P(Element* ep) : e(ep) { - inc(); - } - - P(const P& p) : e(p.e) { - inc(); - } - - ~P() { - if(e) { - dec(); - } - } - - void operator=(const P& p) { - if(e) { - dec(); - } - - e = p.e; - - if(e) { - inc(); - } - } - - void reset() { - if(e) { - dec(); - } - - e = 0; - } - - T* operator->() { - return (T*)e->data; - } - - T* operator*() { - return (T*)e->data; - } - - operator bool() { - return bool(e); - } -}; - -template -class Pool : public BasePool { - private: - typedef typename BasePool::Element Element; - - union Node { - Element e; - Node* next; - }; - - Node elements[size]; - - Node* next_free; - - void free(Element* e) { - Node* n = (Node*)e; - - n->next = next_free; - next_free = n; - } - - Element* alloc() { - if(!next_free) { - return 0; - } - - Element* e = &next_free->e; - next_free = next_free->next; - - e->use_count = 0; - e->pool = this; - - return e; - } - - public: - Pool() : next_free(0) { - for(unsigned int i = 0; i < size; i++) { - free(&elements[i].e); - } - } - - P create() { - Element* e = alloc(); - - if(!e) { - return P(); - } - - new (e->data) T; - - return P(e); - } -}; - -void* operator new(unsigned int, char* buf); - -#endif diff --git a/os/thread.cpp b/os/thread.cpp deleted file mode 100644 index 426fffd..0000000 --- a/os/thread.cpp +++ /dev/null @@ -1,4 +0,0 @@ -#include "thread.h" - -Thread Thread::main_thread __attribute__ ((init_priority (1000))); -Thread* Thread::active_thread = &Thread::main_thread; diff --git a/os/thread.h b/os/thread.h deleted file mode 100644 index 2213d6f..0000000 --- a/os/thread.h +++ /dev/null @@ -1,63 +0,0 @@ -#ifndef THREAD_H -#define THREAD_H - -#include - -class Thread { - friend void switch_context(); - - private: - struct int_frame_t { - // Software saved. - uint32_t r4; - uint32_t r5; - uint32_t r6; - uint32_t r7; - uint32_t r8; - uint32_t r9; - uint32_t r10; - uint32_t r11; - uint32_t lr_ex; - - // Hardware saved. - uint32_t r0; - uint32_t r1; - uint32_t r2; - uint32_t r3; - uint32_t r12; - uint32_t lr; - uint32_t pc; - uint32_t psr; - }; - - int_frame_t* sp; - - Thread* next; - - static Thread* active_thread; - static Thread main_thread; - - Thread() : next(this) {} - - public: - Thread(void* stack, uint32_t stack_size, void (*func)()) { - sp = (int_frame_t*)((uint8_t*)stack + stack_size - sizeof(int_frame_t)); - - sp->lr_ex = 0xfffffff9; - - // frame->lr = thread exit handler - sp->pc = (uint32_t)func; - sp->psr = 0x01000000; - } - - void start() { - next = active_thread->next; - active_thread->next = this; - } - - static inline void yield() { - asm volatile("svc 0"); - } -}; - -#endif diff --git a/os/time.cpp b/os/time.cpp deleted file mode 100644 index 6c96027..0000000 --- a/os/time.cpp +++ /dev/null @@ -1,3 +0,0 @@ -#include "time.h" - -volatile uint32_t Time::systime; diff --git a/os/time.h b/os/time.h deleted file mode 100644 index f4706e0..0000000 --- a/os/time.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef TIME_H -#define TIME_H - -#include "thread.h" - -class Time { - private: - static volatile uint32_t systime; - - public: - inline static void tick() { - systime++; - } - - inline static void sleep(uint32_t ms) { - ms += systime; - while(systime < ms) { - Thread::yield(); - } - } -}; - -#endif diff --git a/suzumebachi.ld b/suzumebachi.ld deleted file mode 100644 index 4903bd9..0000000 --- a/suzumebachi.ld +++ /dev/null @@ -1,83 +0,0 @@ -MEMORY { - flash (rx) : org = 0x08000000, len = 128k - ram (rwx) : org = 0x20000000, len = 20k -} - -_ram_start = ORIGIN(ram); -_ram_size = LENGTH(ram); -_ram_end = _ram_start + _ram_size; - -SECTIONS { - . = 0; - - .vectors : ALIGN(16) SUBALIGN(16) { - KEEP(*(.vectors)) - } > flash - - .init_array : ALIGN(4) SUBALIGN(4) { - PROVIDE(_init_array_start = .); - KEEP(*(SORT(.init_array.*))) - KEEP(*(.init_array)) - PROVIDE(_init_array_end = .); - } > flash - - .fini_array : ALIGN(4) SUBALIGN(4) { - PROVIDE(_fini_array_start = .); - KEEP(*(.fini_array)) - KEEP(*(SORT(.fini_array.*))) - PROVIDE(_fini_array_end = .); - } > flash - - .text : ALIGN(16) SUBALIGN(16) { - *(.text.startup.*) - *(.text) - *(.text.*) - *(.rodata) - *(.rodata.*) - *(.glue_7t) - *(.glue_7) - *(.gcc*) - } > flash - - .ARM.extab : { - *(.ARM.extab* .gnu.linkonce.armextab.*) - } > flash - - .ARM.exidx : { - *(.ARM.exidx* .gnu.linkonce.armexidx.*) - } > flash - - .eh_frame_hdr : { - *(.eh_frame_hdr) - } > flash - - .eh_frame : ONLY_IF_RO { - *(.eh_frame) - } > flash - - . = ALIGN(4); - - PROVIDE(_data_rom = .); - - .data : { - PROVIDE(_data_start = .); - *(.data) - . = ALIGN(4); - *(.data.*) - . = ALIGN(4); - *(.ramtext) - . = ALIGN(4); - PROVIDE(_data_end = .); - } > ram AT > flash - - .bss : { - PROVIDE(_bss_start = .); - *(.bss) - . = ALIGN(4); - *(.bss.*) - . = ALIGN(4); - *(COMMON) - . = ALIGN(4); - PROVIDE(_bss_end = .); - } > ram -} -- cgit v1.2.3