summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--SConscript7
-rw-r--r--build_rules80
-rw-r--r--gpio/gpio.h43
-rw-r--r--gpio/pin.h137
-rw-r--r--i2c/i2c.cpp145
-rw-r--r--i2c/i2c.h62
-rw-r--r--interrupt/fault.cpp41
-rw-r--r--interrupt/interrupt.cpp187
-rw-r--r--interrupt/interrupt.h151
-rw-r--r--ld_scripts/arm_flash_ram.ld78
-rw-r--r--ld_scripts/stm32_f1_8.ld6
-rw-r--r--ld_scripts/stm32_f1_b.ld6
-rw-r--r--ld_scripts/stm32_f4_e.ld7
-rw-r--r--ld_scripts/stm32_f4_g.ld7
-rw-r--r--os/mutex.h42
-rw-r--r--os/pool.cpp5
-rw-r--r--os/pool.h146
-rw-r--r--os/thread.cpp4
-rw-r--r--os/thread.h63
-rw-r--r--os/time.cpp3
-rw-r--r--os/time.h32
-rw-r--r--rcc/flash.cpp17
-rw-r--r--rcc/flash.h30
-rw-r--r--rcc/rcc.cpp51
-rw-r--r--rcc/rcc.h217
-rw-r--r--spi/spi.h31
-rw-r--r--startup/entry.cpp63
-rw-r--r--timer/timer.h40
-rw-r--r--usart/usart.cpp7
-rw-r--r--usart/usart.h68
-rw-r--r--usb/usb.h93
32 files changed, 1870 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..5761abc
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+*.o
diff --git a/SConscript b/SConscript
new file mode 100644
index 0000000..5d706af
--- /dev/null
+++ b/SConscript
@@ -0,0 +1,7 @@
+# Don't call this file explicitly, it is called implicitly by the build rules.
+
+Import('env')
+
+env.Append(
+ LIB_SOURCES = Glob('*.cpp') + Glob('*/*.cpp'),
+)
diff --git a/build_rules b/build_rules
new file mode 100644
index 0000000..0040cd7
--- /dev/null
+++ b/build_rules
@@ -0,0 +1,80 @@
+laks_dir = Dir('.')
+ld_dir = Dir('ld_scripts')
+main_sconscript = File('SConscript')
+
+def select_arm(env, family):
+ env.SetDefault(
+ TOOLCHAIN = 'arm-none-eabi-',
+ )
+
+ env.Replace(
+ CC = '${TOOLCHAIN}gcc',
+ CXX = '${TOOLCHAIN}g++',
+ AS = '${TOOLCHAIN}gcc',
+ LINK = '${TOOLCHAIN}gcc',
+ AR = '${TOOLCHAIN}ar',
+ RANLIB = '${TOOLCHAIN}ranlib',
+
+ CCFLAGS = '-O2 -Wall -ggdb -mcpu=${CPU_FAMILY} -mthumb -ffunction-sections',
+ CXXFLAGS = '-fno-exceptions -fno-rtti -Wno-pmf-conversions',
+ ASFLAGS = '-c -x assembler-with-cpp -mcpu=${CPU_FAMILY} -mthumb',
+ LINKFLAGS = '-Wall -mcpu=${CPU_FAMILY} -mthumb -mhard-float -nostartfiles -Wl,-T${LINK_SCRIPT}', # -Wl,--gc-sections
+
+ CPPPATH = [laks_dir],
+ LIBPATH = [ld_dir],
+
+ LIB_SOURCES = [],
+
+ CPU_FAMILY = family,
+ )
+
+ if family == 'cortex-m4':
+ env.Append(CCFLAGS = ' -mhard-float')
+
+def select_stm32(env, variant):
+ family = variant[5:9]
+ pin_count = variant[9]
+ flash = variant[10]
+
+ if family == 'f103':
+ select_arm(env, 'cortex-m3')
+ env.Append(CPPDEFINES = ['STM32F1'])
+
+ env['LINK_SCRIPT'] = {
+ '8': 'stm32_f1_8.ld',
+ 'b': 'stm32_f1_b.ld',
+ }[flash]
+
+ elif family in ('f405', 'f407'):
+ select_arm(env, 'cortex-m4')
+ env.Append(CPPDEFINES = ['STM32F4'])
+
+ env['LINK_SCRIPT'] = {
+ 'e': 'stm32_f4_e.ld',
+ 'g': 'stm32_f4_g.ld',
+ }[flash]
+
+ else:
+ print 'Unknown stm32 family: %s' % mcu
+ Exit(1)
+
+def SelectMCU(env, mcu):
+ mcu = mcu.lower()
+
+ if mcu.startswith('stm32'):
+ select_stm32(env, mcu)
+
+ else:
+ print 'Unknown MCU: %s' % mcu
+ Exit(1)
+
+ SConscript(main_sconscript, exports = 'env')
+
+AddMethod(Environment, SelectMCU)
+
+def Firmware(env, target, sources):
+ firmware = env.Program(target, [sources, env['LIB_SOURCES']])
+ #env.Depends(firmware, env['LINK_SCRIPT']) # TODO
+ return firmware
+
+AddMethod(Environment, Firmware)
diff --git a/gpio/gpio.h b/gpio/gpio.h
new file mode 100644
index 0000000..324950e
--- /dev/null
+++ b/gpio/gpio.h
@@ -0,0 +1,43 @@
+#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/gpio/pin.h b/gpio/pin.h
new file mode 100644
index 0000000..1ad3ca2
--- /dev/null
+++ b/gpio/pin.h
@@ -0,0 +1,137 @@
+#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/i2c/i2c.cpp b/i2c/i2c.cpp
new file mode 100644
index 0000000..a395b13
--- /dev/null
+++ b/i2c/i2c.cpp
@@ -0,0 +1,145 @@
+#include "i2c.h"
+
+#include <rcc/rcc.h>
+#include <os/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.SR1 = 0;
+
+ 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();
+ }