summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--SConscript1
-rw-r--r--platforms/stm32/f4.yaml4
-rw-r--r--uart/SConscript21
-rw-r--r--uart/stm32_uart.h33
-rw-r--r--uart/uart.h12
-rw-r--r--usart/usart.h58
6 files changed, 70 insertions, 59 deletions
diff --git a/SConscript b/SConscript
index 26a0bda..f9d4021 100644
--- a/SConscript
+++ b/SConscript
@@ -9,6 +9,7 @@ env.Append(
LIB_SOURCES = [
env.SConscript('interrupt/SConscript'),
env.SConscript('rcc/SConscript'),
+ env.SConscript('uart/SConscript'),
env.SConscript('usb/SConscript'),
Glob('startup/*.cpp'),
Glob('i2c/*.cpp'),
diff --git a/platforms/stm32/f4.yaml b/platforms/stm32/f4.yaml
index dc5717d..5325d92 100644
--- a/platforms/stm32/f4.yaml
+++ b/platforms/stm32/f4.yaml
@@ -22,11 +22,13 @@
size: 64k
periph:
- uart_stm32:
+ stm32_uart:
USART1:
offset: 0x40011000
+ type: v1
USART2:
offset: 0x40004400
+ type: v1
dwc_otg:
OTG_FS:
diff --git a/uart/SConscript b/uart/SConscript
new file mode 100644
index 0000000..dcf6d3e
--- /dev/null
+++ b/uart/SConscript
@@ -0,0 +1,21 @@
+Import('env')
+
+headers = []
+instances = []
+sources = []
+aliases = {}
+
+periph = env['PLATFORM_SPEC'].get('periph', {})
+
+if 'stm32_uart' in periph:
+ headers.append('stm32_uart.h')
+ for name, data in periph['stm32_uart'].items():
+ instances.append({
+ 'type': 'STM32_UART_t<STM32_UART_reg_%s_t>' % data['type'],
+ 'name': name,
+ 'args': [data['offset']],
+ })
+
+env.Jinja2('uart.h', '../templates/periph_instances.h.j2', headers = headers, instances = instances, aliases = aliases)
+
+Return('sources')
diff --git a/uart/stm32_uart.h b/uart/stm32_uart.h
new file mode 100644
index 0000000..e4902a5
--- /dev/null
+++ b/uart/stm32_uart.h
@@ -0,0 +1,33 @@
+#pragma once
+
+#include <mmio/mmio.h>
+
+struct STM32_UART_reg_v1_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;
+};
+
+struct STM32_UART_reg_v2_t {
+ volatile uint32_t CR1;
+ volatile uint32_t CR2;
+ volatile uint32_t CR3;
+ volatile uint32_t BRR;
+ volatile uint32_t GTPR;
+ volatile uint32_t RTOR;
+ volatile uint32_t RQR;
+ volatile uint32_t ISR;
+ volatile uint32_t ICR;
+ volatile uint32_t RDR;
+ volatile uint32_t TDR;
+};
+
+template <typename T>
+class STM32_UART_t : public mmio_ptr<T> {
+ public:
+ using mmio_ptr<T>::ptr;
+};
diff --git a/uart/uart.h b/uart/uart.h
new file mode 100644
index 0000000..ed680ea
--- /dev/null
+++ b/uart/uart.h
@@ -0,0 +1,12 @@
+#pragma once
+
+#include "stm32_uart.h"
+
+constexpr STM32_UART_t<STM32_UART_reg_v1_t> USART1{
+ 0x40011000,
+};
+constexpr STM32_UART_t<STM32_UART_reg_v1_t> USART2{
+ 0x40004400,
+};
+
+
diff --git a/usart/usart.h b/usart/usart.h
deleted file mode 100644
index 9f3d838..0000000
--- a/usart/usart.h
+++ /dev/null
@@ -1,58 +0,0 @@
-#ifndef USART_H
-#define USART_H
-
-#include <rcc/rcc.h>
-#include <interrupt/interrupt.h>
-
-#if defined(STM32F1) || defined(STM32F4)
-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;
-};
-#elif defined(STM32F3) || defined(STM32L0)
-struct USART_reg_t {
- volatile uint32_t CR1;
- volatile uint32_t CR2;
- volatile uint32_t CR3;
- volatile uint32_t BRR;
- volatile uint32_t GTPR;
- volatile uint32_t RTOR;
- volatile uint32_t RQR;
- volatile uint32_t ISR;
- volatile uint32_t ICR;
- volatile uint32_t RDR;
- volatile uint32_t TDR;
-};
-#endif
-
-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;
- }
-};
-
-#if defined(STM32F1) || defined(STM32F3)
-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);
-#elif defined(STM32L0)
-static USART_t USART1(0x40013800, 32000000);
-static USART_t USART2(0x40004400, 32000000);
-#endif
-
-#endif