diff options
author | Birte Kristina Friesel <derf@finalrewind.org> | 2024-02-11 21:21:28 +0100 |
---|---|---|
committer | Birte Kristina Friesel <derf@finalrewind.org> | 2024-02-11 21:21:28 +0100 |
commit | 3fffef85de0b780eb26beef5625242355053e872 (patch) | |
tree | 516fb36dbbd5b84475fd5b2c852aef18f16e8214 /src/arch/stm32f446re-nucleo/driver | |
parent | ae68513e744e2a7d99ee085c02de227b135b5a7d (diff) |
stm32f446re: Add I²C driver
Diffstat (limited to 'src/arch/stm32f446re-nucleo/driver')
-rw-r--r-- | src/arch/stm32f446re-nucleo/driver/i2c.cc | 38 |
1 files changed, 38 insertions, 0 deletions
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 <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_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; |