summaryrefslogtreecommitdiff
path: root/src/arch/stm32f746zg-nucleo/driver
diff options
context:
space:
mode:
authorBirte Kristina Friesel <derf@finalrewind.org>2024-02-12 17:33:44 +0100
committerBirte Kristina Friesel <derf@finalrewind.org>2024-02-12 17:33:44 +0100
commit190fef225ff4f8656cbf43348de34edc640b7112 (patch)
tree9be6808c4bb30347375d6237d5738b737cbcbaee /src/arch/stm32f746zg-nucleo/driver
parent5db1ecd3fc2ab0c7942476bae07e57be66e7689c (diff)
stm32f7: Add I²C driver. With fast mode!
Diffstat (limited to 'src/arch/stm32f746zg-nucleo/driver')
-rw-r--r--src/arch/stm32f746zg-nucleo/driver/i2c.cc38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/arch/stm32f746zg-nucleo/driver/i2c.cc b/src/arch/stm32f746zg-nucleo/driver/i2c.cc
new file mode 100644
index 0000000..035f273
--- /dev/null
+++ b/src/arch/stm32f746zg-nucleo/driver/i2c.cc
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2024 Birte Kristina Friesel
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+#include "driver/i2c.h"
+#include "arch.h"
+#include <libopencm3/stm32/rcc.h>
+#include <libopencm3/stm32/gpio.h>
+#include <libopencm3/stm32/i2c.h>
+
+signed char I2C::setup()
+{
+
+ rcc_periph_clock_enable(RCC_I2C1);
+ gpio_set_af(GPIOB, GPIO_AF4, GPIO8 | GPIO9);
+ gpio_set_output_options(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_2MHZ, GPIO8|GPIO9);
+ gpio_mode_setup(GPIOB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO8 | GPIO9);
+ i2c_peripheral_disable(I2C1);
+#ifdef CONFIG_arch_stm32f746zg_nucleo_driver_i2c_fast_mode
+ i2c_set_speed(I2C1, i2c_speed_fm_400k, rcc_apb1_frequency / 1000000);
+#else
+ i2c_set_speed(I2C1, i2c_speed_sm_100k, rcc_apb1_frequency / 1000000);
+#endif
+ i2c_peripheral_enable(I2C1);
+
+ return 0;
+}
+
+signed char I2C::xmit(unsigned char address,
+ unsigned char tx_len, unsigned char *tx_buf,
+ unsigned char rx_len, unsigned char *rx_buf)
+{
+ i2c_transfer7(I2C1, address, tx_buf, tx_len, rx_buf, rx_len);
+ return 0;
+}
+
+I2C i2c;