From 57f2002ddea71b64c15c3b333a3dae486ad3e77f Mon Sep 17 00:00:00 2001
From: Alexis Foulon <alexis.foulon@polymtl.ca>
Date: Thu, 25 Nov 2021 23:58:09 +0000
Subject: [PATCH] I2c gpio

---
 .../dma/abstract-dma/abstract-dma.cpp         |  6 ++---
 .../gpio/i2c-gpio/i2c-gpio-init.hpp           | 12 ++++++++++
 .../stm32f072xb/gpio/i2c-gpio/i2c-gpio.hpp    | 23 +++++++++++++++++++
 3 files changed, 38 insertions(+), 3 deletions(-)
 create mode 100644 library/stm32f072xb/gpio/i2c-gpio/i2c-gpio-init.hpp
 create mode 100644 library/stm32f072xb/gpio/i2c-gpio/i2c-gpio.hpp

diff --git a/library/stm32f072xb/dma/abstract-dma/abstract-dma.cpp b/library/stm32f072xb/dma/abstract-dma/abstract-dma.cpp
index f4176d6..c5bb305 100644
--- a/library/stm32f072xb/dma/abstract-dma/abstract-dma.cpp
+++ b/library/stm32f072xb/dma/abstract-dma/abstract-dma.cpp
@@ -42,16 +42,16 @@ void AbstractDma::setupRegister() {
 void AbstractDma::setPeripheral(DmaPeripheral peripheral) {
 	switch (peripheral) {
 		case DmaPeripheral::USART2_RX:
-			WRITE_REG(_dmaChannel->CPAR, (uint32_t)&USART2->RDR);
+			_dmaChannel->CPAR = (uintptr_t)&USART2->RDR;
 			break;
 		case DmaPeripheral::USART2_TX:
-			WRITE_REG(_dmaChannel->CPAR, (uint32_t)&USART2->TDR);
+			_dmaChannel->CPAR = (uintptr_t)&USART2->TDR;
 			break;
 	}
 }
 
 void AbstractDma::setMemory(volatile const uint8_t* buffer) {
-	WRITE_REG(_dmaChannel->CMAR, (uint32_t)(buffer));
+	_dmaChannel->CMAR = (uintptr_t)(buffer);
 }
 
 void AbstractDma::setNumberOfDataTransfer(uint16_t numberOfDataTransfer) {
diff --git a/library/stm32f072xb/gpio/i2c-gpio/i2c-gpio-init.hpp b/library/stm32f072xb/gpio/i2c-gpio/i2c-gpio-init.hpp
new file mode 100644
index 0000000..5eebfc1
--- /dev/null
+++ b/library/stm32f072xb/gpio/i2c-gpio/i2c-gpio-init.hpp
@@ -0,0 +1,12 @@
+#include "alternate-gpio-init.hpp"
+
+#ifndef I2C_GPIO_INIT_H
+#define I2C_GPIO_INIT_H
+
+namespace stm32f072xb {
+
+struct I2cGpioInit : public AlternateGpioInit {};
+
+}  // namespace stm32f072xb
+
+#endif
diff --git a/library/stm32f072xb/gpio/i2c-gpio/i2c-gpio.hpp b/library/stm32f072xb/gpio/i2c-gpio/i2c-gpio.hpp
new file mode 100644
index 0000000..89f66a0
--- /dev/null
+++ b/library/stm32f072xb/gpio/i2c-gpio/i2c-gpio.hpp
@@ -0,0 +1,23 @@
+#include "alternate-gpio.hpp"
+#include "i2c-gpio-init.hpp"
+
+#ifndef I2C_GPIO_H
+#define I2C_GPIO_H
+
+namespace stm32f072xb {
+
+template <Port T>
+class I2cGpio : public Gpio<T> {
+   public:
+	I2cGpio(const I2cGpioInit& gpioInit) : AlternateGpio<T>(gpioInit) {
+		// Select open drain output type
+		SET_BIT(this->getGpio()->OTYPER, 1UL << (this->getPin()));
+
+		// Select high speed output
+		SET_BIT(this->getGpio()->OSPEEDR, 0x3UL << (this->getPin() * 2));
+	}
+};
+
+};  // namespace stm32f072xb
+
+#endif
-- 
GitLab