aboutsummaryrefslogtreecommitdiff
path: root/embassy-stm32/src/i2c
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-stm32/src/i2c')
-rw-r--r--embassy-stm32/src/i2c/v1.rs52
-rw-r--r--embassy-stm32/src/i2c/v2.rs18
2 files changed, 27 insertions, 43 deletions
diff --git a/embassy-stm32/src/i2c/v1.rs b/embassy-stm32/src/i2c/v1.rs
index 922c1c7e8..5a9e82828 100644
--- a/embassy-stm32/src/i2c/v1.rs
+++ b/embassy-stm32/src/i2c/v1.rs
@@ -108,11 +108,11 @@ impl<'d, T: Instance> I2c<'d, T> {
108 // Send a START condition 108 // Send a START condition
109 109
110 T::regs().cr1().modify(|reg| { 110 T::regs().cr1().modify(|reg| {
111 reg.set_start(i2c::vals::Start::START); 111 reg.set_start(true);
112 }); 112 });
113 113
114 // Wait until START condition was generated 114 // Wait until START condition was generated
115 while self.check_and_clear_error_flags()?.sb() == i2c::vals::Sb::NOSTART {} 115 while !self.check_and_clear_error_flags()?.start() {}
116 116
117 // Also wait until signalled we're master and everything is waiting for us 117 // Also wait until signalled we're master and everything is waiting for us
118 while { 118 while {
@@ -126,13 +126,9 @@ impl<'d, T: Instance> I2c<'d, T> {
126 T::regs().dr().write(|reg| reg.set_dr(addr << 1)); 126 T::regs().dr().write(|reg| reg.set_dr(addr << 1));
127 127
128 // Wait until address was sent 128 // Wait until address was sent
129 while { 129 // Wait for the address to be acknowledged
130 // Check for any I2C errors. If a NACK occurs, the ADDR bit will never be set. 130 // Check for any I2C errors. If a NACK occurs, the ADDR bit will never be set.
131 let sr1 = self.check_and_clear_error_flags()?; 131 while !self.check_and_clear_error_flags()?.addr() {}
132
133 // Wait for the address to be acknowledged
134 !sr1.addr()
135 } {}
136 132
137 // Clear condition by reading SR2 133 // Clear condition by reading SR2
138 let _ = T::regs().sr2().read(); 134 let _ = T::regs().sr2().read();
@@ -150,7 +146,7 @@ impl<'d, T: Instance> I2c<'d, T> {
150 // Wait until we're ready for sending 146 // Wait until we're ready for sending
151 while { 147 while {
152 // Check for any I2C errors. If a NACK occurs, the ADDR bit will never be set. 148 // Check for any I2C errors. If a NACK occurs, the ADDR bit will never be set.
153 !self.check_and_clear_error_flags()?.tx_e() 149 !self.check_and_clear_error_flags()?.txe()
154 } {} 150 } {}
155 151
156 // Push out a byte of data 152 // Push out a byte of data
@@ -170,7 +166,7 @@ impl<'d, T: Instance> I2c<'d, T> {
170 // Check for any potential error conditions. 166 // Check for any potential error conditions.
171 self.check_and_clear_error_flags()?; 167 self.check_and_clear_error_flags()?;
172 168
173 !T::regs().sr1().read().rx_ne() 169 !T::regs().sr1().read().rxne()
174 } {} 170 } {}
175 171
176 let value = T::regs().dr().read().dr(); 172 let value = T::regs().dr().read().dr();
@@ -182,13 +178,13 @@ impl<'d, T: Instance> I2c<'d, T> {
182 // Send a START condition and set ACK bit 178 // Send a START condition and set ACK bit
183 unsafe { 179 unsafe {
184 T::regs().cr1().modify(|reg| { 180 T::regs().cr1().modify(|reg| {
185 reg.set_start(i2c::vals::Start::START); 181 reg.set_start(true);
186 reg.set_ack(true); 182 reg.set_ack(true);
187 }); 183 });
188 } 184 }
189 185
190 // Wait until START condition was generated 186 // Wait until START condition was generated
191 while unsafe { T::regs().sr1().read().sb() } == i2c::vals::Sb::NOSTART {} 187 while unsafe { !T::regs().sr1().read().start() } {}
192 188
193 // Also wait until signalled we're master and everything is waiting for us 189 // Also wait until signalled we're master and everything is waiting for us
194 while { 190 while {
@@ -197,24 +193,14 @@ impl<'d, T: Instance> I2c<'d, T> {
197 } {} 193 } {}
198 194
199 // Set up current address, we're trying to talk to 195 // Set up current address, we're trying to talk to
200 unsafe { 196 unsafe { T::regs().dr().write(|reg| reg.set_dr((addr << 1) + 1)) }
201 T::regs().dr().write(|reg| reg.set_dr((addr << 1) + 1));
202 }
203 197
204 // Wait until address was sent 198 // Wait until address was sent
205 while { 199 // Wait for the address to be acknowledged
206 unsafe { 200 while unsafe { !self.check_and_clear_error_flags()?.addr() } {}
207 let sr1 = self.check_and_clear_error_flags()?;
208
209 // Wait for the address to be acknowledged
210 !sr1.addr()
211 }
212 } {}
213 201
214 // Clear condition by reading SR2 202 // Clear condition by reading SR2
215 unsafe { 203 let _ = unsafe { T::regs().sr2().read() };
216 let _ = T::regs().sr2().read();
217 }
218 204
219 // Receive bytes into buffer 205 // Receive bytes into buffer
220 for c in buffer { 206 for c in buffer {
@@ -225,15 +211,15 @@ impl<'d, T: Instance> I2c<'d, T> {
225 unsafe { 211 unsafe {
226 T::regs().cr1().modify(|reg| { 212 T::regs().cr1().modify(|reg| {
227 reg.set_ack(false); 213 reg.set_ack(false);
228 reg.set_stop(i2c::vals::Stop::STOP); 214 reg.set_stop(true);
229 }); 215 })
230 } 216 }
231 217
232 // Receive last byte 218 // Receive last byte
233 *last = unsafe { self.recv_byte()? }; 219 *last = unsafe { self.recv_byte()? };
234 220
235 // Wait for the STOP to be sent. 221 // Wait for the STOP to be sent.
236 while unsafe { T::regs().cr1().read().stop() == i2c::vals::Stop::STOP } {} 222 while unsafe { T::regs().cr1().read().stop() } {}
237 223
238 // Fallthrough is success 224 // Fallthrough is success
239 Ok(()) 225 Ok(())
@@ -246,11 +232,9 @@ impl<'d, T: Instance> I2c<'d, T> {
246 unsafe { 232 unsafe {
247 self.write_bytes(addr, bytes)?; 233 self.write_bytes(addr, bytes)?;
248 // Send a STOP condition 234 // Send a STOP condition
249 T::regs() 235 T::regs().cr1().modify(|reg| reg.set_stop(true));
250 .cr1()
251 .modify(|reg| reg.set_stop(i2c::vals::Stop::STOP));
252 // Wait for STOP condition to transmit. 236 // Wait for STOP condition to transmit.
253 while T::regs().cr1().read().stop() == i2c::vals::Stop::STOP {} 237 while T::regs().cr1().read().stop() {}
254 }; 238 };
255 239
256 // Fallthrough is success 240 // Fallthrough is success
diff --git a/embassy-stm32/src/i2c/v2.rs b/embassy-stm32/src/i2c/v2.rs
index 58ab771b7..493aacb6d 100644
--- a/embassy-stm32/src/i2c/v2.rs
+++ b/embassy-stm32/src/i2c/v2.rs
@@ -132,7 +132,7 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> {
132 132
133 fn master_stop(&mut self) { 133 fn master_stop(&mut self) {
134 unsafe { 134 unsafe {
135 T::regs().cr2().write(|w| w.set_stop(i2c::vals::Stop::STOP)); 135 T::regs().cr2().write(|w| w.set_stop(true));
136 } 136 }
137 } 137 }
138 138
@@ -143,7 +143,7 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> {
143 // Wait for any previous address sequence to end 143 // Wait for any previous address sequence to end
144 // automatically. This could be up to 50% of a bus 144 // automatically. This could be up to 50% of a bus
145 // cycle (ie. up to 0.5/freq) 145 // cycle (ie. up to 0.5/freq)
146 while T::regs().cr2().read().start() == i2c::vals::Start::START {} 146 while T::regs().cr2().read().start() {}
147 } 147 }
148 148
149 // Set START and prepare to receive bytes into 149 // Set START and prepare to receive bytes into
@@ -158,10 +158,10 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> {
158 158
159 T::regs().cr2().modify(|w| { 159 T::regs().cr2().modify(|w| {
160 w.set_sadd((address << 1 | 0) as u16); 160 w.set_sadd((address << 1 | 0) as u16);
161 w.set_add10(i2c::vals::Add::BIT7); 161 w.set_add10(i2c::vals::Addmode::BIT7);
162 w.set_rd_wrn(i2c::vals::RdWrn::READ); 162 w.set_dir(i2c::vals::Dir::READ);
163 w.set_nbytes(length as u8); 163 w.set_nbytes(length as u8);
164 w.set_start(i2c::vals::Start::START); 164 w.set_start(true);
165 w.set_autoend(stop.autoend()); 165 w.set_autoend(stop.autoend());
166 w.set_reload(reload); 166 w.set_reload(reload);
167 }); 167 });
@@ -173,7 +173,7 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> {
173 // Wait for any previous address sequence to end 173 // Wait for any previous address sequence to end
174 // automatically. This could be up to 50% of a bus 174 // automatically. This could be up to 50% of a bus
175 // cycle (ie. up to 0.5/freq) 175 // cycle (ie. up to 0.5/freq)
176 while T::regs().cr2().read().start() == i2c::vals::Start::START {} 176 while T::regs().cr2().read().start() {}
177 177
178 let reload = if reload { 178 let reload = if reload {
179 i2c::vals::Reload::NOTCOMPLETED 179 i2c::vals::Reload::NOTCOMPLETED
@@ -186,10 +186,10 @@ impl<'d, T: Instance, TXDMA, RXDMA> I2c<'d, T, TXDMA, RXDMA> {
186 // I2C is in slave mode. 186 // I2C is in slave mode.
187 T::regs().cr2().modify(|w| { 187 T::regs().cr2().modify(|w| {
188 w.set_sadd((address << 1 | 0) as u16); 188 w.set_sadd((address << 1 | 0) as u16);
189 w.set_add10(i2c::vals::Add::BIT7); 189 w.set_add10(i2c::vals::Addmode::BIT7);
190 w.set_rd_wrn(i2c::vals::RdWrn::WRITE); 190 w.set_dir(i2c::vals::Dir::WRITE);
191 w.set_nbytes(length as u8); 191 w.set_nbytes(length as u8);
192 w.set_start(i2c::vals::Start::START); 192 w.set_start(true);
193 w.set_autoend(stop.autoend()); 193 w.set_autoend(stop.autoend());
194 w.set_reload(reload); 194 w.set_reload(reload);
195 }); 195 });