diff options
Diffstat (limited to 'hal')
-rw-r--r-- | hal/fault.cpp | 41 | ||||
-rw-r--r-- | hal/flash.cpp | 17 | ||||
-rw-r--r-- | hal/flash.h | 30 | ||||
-rw-r--r-- | hal/gpio.h | 43 | ||||
-rw-r--r-- | hal/hal.h | 10 | ||||
-rw-r--r-- | hal/i2c.cpp | 142 | ||||
-rw-r--r-- | hal/i2c.h | 62 | ||||
-rw-r--r-- | hal/interrupt.cpp | 187 | ||||
-rw-r--r-- | hal/interrupt.h | 121 | ||||
-rw-r--r-- | hal/pin.h | 137 | ||||
-rw-r--r-- | hal/rcc.cpp | 51 | ||||
-rw-r--r-- | hal/rcc.h | 217 | ||||
-rw-r--r-- | hal/spi.h | 31 | ||||
-rw-r--r-- | hal/stm32.h | 89 | ||||
-rw-r--r-- | hal/timer.h | 40 | ||||
-rw-r--r-- | hal/usart.cpp | 7 | ||||
-rw-r--r-- | hal/usart.h | 69 | ||||
-rw-r--r-- | hal/usb.h | 93 |
18 files changed, 0 insertions, 1387 deletions
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<Interrupt::SVCall>() { - switch_context(); -} - -template<> -void interrupt<Interrupt::SysTick>() { - Time::tick(); -} - -template<> void interrupt<Interrupt::NMI>() { while(1); } -template<> void interrupt<Interrupt::HardFault>() { while(1); } -template<> void interrupt<Interrupt::MemManage>() { while(1); } -template<> void interrupt<Interrupt::BusFault>() { while(1); } -template<> void interrupt<Interrupt::UsageFault>() { while(1); } -template<> void interrupt<Interrupt::PendSV>() { 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 <stdint.h> - -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 <stdint.h> -#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<Interrupt::NMI>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::HardFault>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::MemManage>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::BusFault>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::UsageFault>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::SVCall>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::PendSV>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::SysTick>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::WWDG>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::PVD>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::TAMPER>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::RTC>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::FLASH>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::RCC>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::EXTI0>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::EXTI1>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::EXTI2>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::EXTI3>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::EXTI4>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::DMA1_Channel1>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::DMA1_Channel2>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::DMA1_Channel3>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::DMA1_Channel4>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::DMA1_Channel5>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::DMA1_Channel6>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::DMA1_Channel7>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::ADC1_2>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::USB_HP_CAN_TX>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::USB_LP_CAN_RX0>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::CAN_RX1>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::CAN_SCE>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::EXTI9_5>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::TIM1_BRK>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::TIM1_UP>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::TIM1_TRG_COM>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::TIM1_CC>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::TIM2>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::TIM3>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::TIM4>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::I2C1_EV>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::I2C1_ER>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::I2C2_EV>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::I2C2_ER>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::SPI1>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::SPI2>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::USART1>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::USART2>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::USART3>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::EXTI15_10>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::RTCAlarm>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::USBWakeup>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::TIM8_BRK>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::TIM8_UP>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::TIM8_TRG_COM>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::TIM8_CC>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::ADC3>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::FSMC>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::SDIO>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::TIM5>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::SPI3>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::UART4>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::UART5>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::TIM6>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::TIM7>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::DMA2_Channel1>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::DMA2_Channel2>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::DMA2_Channel3>() __attribute__ ((weak, alias ("unused_interrupt"))); -template<> void interrupt<Interrupt::DMA2_Channel4_5>() __attribute__ ((weak, alias ("unused_interrupt"))); - -typedef void (*vector_t)(); - -vector_t vectors[] __attribute__((section(".vectors"))) = { - (vector_t)0x20004ffc, - entry, - interrupt<Interrupt::NMI>, - interrupt<Interrupt::HardFault>, - interrupt<Interrupt::MemManage>, - interrupt<Interrupt::BusFault>, - interrupt<Interrupt::UsageFault>, - 0, - 0, - 0, - 0, - interrupt<Interrupt::SVCall>, - 0, - 0, - interrupt<Interrupt::PendSV>, - interrupt<Interrupt::SysTick>, - interrupt<Interrupt::WWDG>, - interrupt<Interrupt::PVD>, - interrupt<Interrupt::TAMPER>, - interrupt<Interrupt::RTC>, - interrupt<Interrupt::FLASH>, - interrupt<Interrupt::RCC>, - interrupt<Interrupt::EXTI0>, - interrupt<Interrupt::EXTI1>, - interrupt<Interrupt::EXTI2>, - interrupt<Interrupt::EXTI3>, - interrupt<Interrupt::EXTI4>, - interrupt<Interrupt::DMA1_Channel1>, - interrupt<Interrupt::DMA1_Channel2>, - interrupt<Interrupt::DMA1_Channel3>, - interrupt<Interrupt::DMA1_Channel4>, - interrupt<Interrupt::DMA1_Channel5>, - interrupt<Interrupt::DMA1_Channel6>, - interrupt<Interrupt::DMA1_Channel7>, - interrupt<Interrupt::ADC1_2>, - interrupt<Interrupt::USB_HP_CAN_TX>, - interrupt<Interrupt::USB_LP_CAN_RX0>, - interrupt<Interrupt::CAN_RX1>, - interrupt<Interrupt::CAN_SCE>, - interrupt<Interrupt::EXTI9_5>, - interrupt<Interrupt::TIM1_BRK>, - interrupt<Interrupt::TIM1_UP>, - interrupt<Interrupt::TIM1_TRG_COM>, - interrupt<Interrupt::TIM1_CC>, - interrupt<Interrupt::TIM2>, - interrupt<Interrupt::TIM3>, - interrupt<Interrupt::TIM4>, - interrupt<Interrupt::I2C1_EV>, - interrupt<Interrupt::I2C1_ER>, - interrupt<Interrupt::I2C2_EV>, - interrupt<Interrupt::I2C2_ER>, - interrupt<Interrupt::SPI1>, - interrupt<Interrupt::SPI2>, - interrupt<Interrupt::USART1>, - interrupt<Interrupt::USART2>, - interrupt<Interrupt::USART3>, - interrupt<Interrupt::EXTI15_10>, - interrupt<Interrupt::RTCAlarm>, - interrupt<Interrupt::USBWakeup>, - interrupt<Interrupt::TIM8_BRK>, - interrupt<Interrupt::TIM8_UP>, - interrupt<Interrupt::TIM8_TRG_COM>, - interrupt<Interrupt::TIM8_CC>, - interrupt<Interrupt::ADC3>, - interrupt<Interrupt::FSMC>, - interrupt<Interrupt::SDIO>, - interrupt<Interrupt::TIM5>, - interrupt<Interrupt::SPI3>, - interrupt<Interrupt::UART4>, - interrupt<Interrupt::UART5>, - interrupt<Interrupt::TIM6>, - interrupt<Interrupt::TIM7>, - interrupt<Interrupt::DMA2_Channel1>, - interrupt<Interrupt::DMA2_Channel2>, - interrupt<Interrupt::DMA2_Channel3>, - interrupt<Interrupt::DMA2_Channel4_5>, - 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<class T> - inline void set_handler(IRQ n, void (T::*f)(), T* i) { - MFP& mfp = mf_vectors[16 + n]; - mfp.func_p = reinterpret_cast<void (*)(void*)>(f); - mfp.instance_p = i; - } - - template<class T> - inline void enable(IRQ n, void (T::*f)(), T* i) { - set_handler(n, f, i); - enable(n); - } -}; - -template<Interrupt::Exception> -void interrupt(); - -template<Interrupt::IRQ> -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 <stdint.h> - -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 << |