From 799f2b3b8a42a180879260dff910d682e3a2d104 Mon Sep 17 00:00:00 2001 From: victorpv Date: Mon, 11 Sep 2017 22:01:55 -0500 Subject: [PATCH 1/2] Correct issue with port speed not changing. The current implementation will not change speed if Wire.setClock is called after begin() since the flags are only applied when the port is enabled from being disabled. Corrected that by adding 2 lines to disable the port, and then enable it again with the new settings. Tested and confirmed the new speed is applied. --- STM32F1/libraries/Wire/Wire.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/STM32F1/libraries/Wire/Wire.cpp b/STM32F1/libraries/Wire/Wire.cpp index c4e9bef..3f5f161 100644 --- a/STM32F1/libraries/Wire/Wire.cpp +++ b/STM32F1/libraries/Wire/Wire.cpp @@ -97,7 +97,8 @@ void HardWire::setClock(uint32_t frequencyHz) dev_flags &= ~I2C_FAST_MODE;// clear FAST_MODE bit break; } - + i2c_disable(sel_hard); + i2c_master_enable(sel_hard, dev_flags); } HardWire Wire(1); From a530b6a3d46385586e6ece1202ee505c09157150 Mon Sep 17 00:00:00 2001 From: victorpv Date: Mon, 11 Sep 2017 22:49:03 -0500 Subject: [PATCH 2/2] Update Wire.cpp Adds a check to verify if the port had been already enabled, and only in that case disables/re-enables the port. This also solves the issue of setClock being called after begin(), but doesn't not enable the port if begin() had not been called yet. --- STM32F1/libraries/Wire/Wire.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/STM32F1/libraries/Wire/Wire.cpp b/STM32F1/libraries/Wire/Wire.cpp index 3f5f161..a026e2e 100644 --- a/STM32F1/libraries/Wire/Wire.cpp +++ b/STM32F1/libraries/Wire/Wire.cpp @@ -97,8 +97,10 @@ void HardWire::setClock(uint32_t frequencyHz) dev_flags &= ~I2C_FAST_MODE;// clear FAST_MODE bit break; } - i2c_disable(sel_hard); - i2c_master_enable(sel_hard, dev_flags); + if (sel_hard->regs->CR1 & I2C_CR1_PE){ + i2c_disable(sel_hard); + i2c_master_enable(sel_hard, dev_flags); + } } HardWire Wire(1);