diff options
Diffstat (limited to 'libmaple')
-rw-r--r-- | libmaple/i2c.c | 8 | ||||
-rw-r--r-- | libmaple/include/libmaple/i2c.h | 25 | ||||
-rw-r--r-- | libmaple/stm32f1/i2c.c | 16 |
3 files changed, 43 insertions, 6 deletions
diff --git a/libmaple/i2c.c b/libmaple/i2c.c index 1618586..f54be47 100644 --- a/libmaple/i2c.c +++ b/libmaple/i2c.c @@ -299,10 +299,7 @@ void _i2c_irq_error_handler(i2c_dev *dev) { */ void i2c_bus_reset(const i2c_dev *dev) { /* Release both lines */ - gpio_write_bit(dev->gpio_port, dev->scl_pin, 1); - gpio_write_bit(dev->gpio_port, dev->sda_pin, 1); - gpio_set_mode(dev->gpio_port, dev->scl_pin, GPIO_OUTPUT_OD); - gpio_set_mode(dev->gpio_port, dev->sda_pin, GPIO_OUTPUT_OD); + i2c_master_release_bus(dev); /* * Make sure the bus is free by clocking it until any slaves release the @@ -376,8 +373,7 @@ void i2c_master_enable(i2c_dev *dev, uint32 flags) { /* Turn on clock and set GPIO modes */ i2c_init(dev); - gpio_set_mode(dev->gpio_port, dev->sda_pin, GPIO_AF_OUTPUT_OD); - gpio_set_mode(dev->gpio_port, dev->scl_pin, GPIO_AF_OUTPUT_OD); + i2c_config_gpios(dev); /* I2C1 and I2C2 are fed from APB1, clocked at 36MHz */ i2c_set_input_clk(dev, I2C_CLK); diff --git a/libmaple/include/libmaple/i2c.h b/libmaple/include/libmaple/i2c.h index 9f65f13..8ce6674 100644 --- a/libmaple/include/libmaple/i2c.h +++ b/libmaple/include/libmaple/i2c.h @@ -288,6 +288,31 @@ static inline void i2c_disable_ack(i2c_dev *dev) { dev->regs->CR1 &= ~I2C_CR1_ACK; } +/* GPIO control */ + +/** + * @brief Configure device GPIOs. + * + * Configure GPIO bits dev->sda_pin and dev->scl_pin on GPIO device + * dev->gpio_port for use with I2C device dev. + * + * @param dev I2C Device + * @see i2c_release_gpios() + */ +extern void i2c_config_gpios(const i2c_dev *dev); + +/** + * @brief Release GPIOs controlling an I2C bus + * + * Releases the I2C bus controlled by dev as master, and disconnects + * GPIO bits dev->sda_pin and dev->scl_pin on GPIO device + * dev->gpio_port from I2C device dev. + * + * @param dev I2C device + * @see i2c_config_gpios() + */ +extern void i2c_master_release_bus(const i2c_dev *dev); + /* Miscellaneous low-level routines */ void i2c_init(i2c_dev *dev); diff --git a/libmaple/stm32f1/i2c.c b/libmaple/stm32f1/i2c.c index 9ebcb40..ed0ca97 100644 --- a/libmaple/stm32f1/i2c.c +++ b/libmaple/stm32f1/i2c.c @@ -40,6 +40,22 @@ i2c_dev* const I2C1 = &i2c1; i2c_dev* const I2C2 = &i2c2; /* + * Routines + */ + +void i2c_config_gpios(const i2c_dev *dev) { + gpio_set_mode(dev->gpio_port, dev->sda_pin, GPIO_AF_OUTPUT_OD); + gpio_set_mode(dev->gpio_port, dev->scl_pin, GPIO_AF_OUTPUT_OD); +} + +void i2c_master_release_bus(const i2c_dev *dev) { + gpio_write_bit(dev->gpio_port, dev->scl_pin, 1); + gpio_write_bit(dev->gpio_port, dev->sda_pin, 1); + gpio_set_mode(dev->gpio_port, dev->scl_pin, GPIO_OUTPUT_OD); + gpio_set_mode(dev->gpio_port, dev->sda_pin, GPIO_OUTPUT_OD); +} + +/* * IRQ handlers */ |