From 3fffef85de0b780eb26beef5625242355053e872 Mon Sep 17 00:00:00 2001 From: Birte Kristina Friesel Date: Sun, 11 Feb 2024 21:21:28 +0100 Subject: stm32f446re: Add I²C driver MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/arch/stm32f446re-nucleo/driver/i2c.cc | 38 +++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/arch/stm32f446re-nucleo/driver/i2c.cc (limited to 'src/arch/stm32f446re-nucleo/driver') diff --git a/src/arch/stm32f446re-nucleo/driver/i2c.cc b/src/arch/stm32f446re-nucleo/driver/i2c.cc new file mode 100644 index 0000000..01b2623 --- /dev/null +++ b/src/arch/stm32f446re-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 +#include +#include + +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_stm32f446re_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_set_standard_mode(I2C1); + 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; -- cgit v1.2.3