@@ -37,7 +37,7 @@ macro_rules! flush_txdr {
3737 ( $i2c: expr) => {
3838 // If a pending TXIS flag is set, write dummy data to TXDR
3939 if $i2c. isr( ) . read( ) . txis( ) . bit_is_set( ) {
40- $i2c. txdr( ) . write( |w| unsafe { w. txdata( ) . bits ( 0 ) } ) ;
40+ $i2c. txdr( ) . write( |w| w. txdata( ) . set ( 0 ) ) ;
4141 }
4242
4343 // If TXDR is not flagged as empty, write 1 to flush it
@@ -78,8 +78,9 @@ macro_rules! busy_wait {
7878 } else if isr. tcr( ) . bit_is_set( ) {
7979 // This condition Will only happen when reload == 1 and sbr == 1 (slave) and nbytes was written.
8080 // Send a NACK, set nbytes to clear tcr flag
81- $i2c. cr2( ) . modify( |_, w| unsafe {
82- w. nack( ) . set_bit( ) . nbytes( ) . bits( 1 as u8 )
81+ $i2c. cr2( ) . modify( |_, w| {
82+ w. nack( ) . set_bit( ) ;
83+ w. nbytes( ) . set( 1 as u8 )
8384 } ) ;
8485 // Make one extra loop here to wait on the stop condition
8586 } else if isr. addr( ) . bit_is_set( ) {
@@ -176,13 +177,10 @@ macro_rules! i2c {
176177 i2c. timingr( ) . write( |w| unsafe { w. bits( timing_bits) } ) ;
177178
178179 // Enable the I2C processing
179- i2c. cr1( ) . modify( |_, w| unsafe {
180- w. pe( )
181- . set_bit( )
182- . dnf( )
183- . bits( config. digital_filter)
184- . anfoff( )
185- . bit( !config. analog_filter)
180+ i2c. cr1( ) . modify( |_, w| {
181+ w. pe( ) . set_bit( ) ;
182+ w. dnf( ) . set( config. digital_filter) ;
183+ w. anfoff( ) . bit( !config. analog_filter)
186184 } ) ;
187185
188186 if config. slave_address_1 > 0 {
@@ -196,10 +194,10 @@ macro_rules! i2c {
196194 }
197195
198196 if config. slave_address_2 > 0 {
199- i2c. oar2( ) . write( |w| unsafe {
200- w. oa2msk( ) . bits ( config. slave_address_mask as u8 )
201- . oa2( ) . bits ( config. slave_address_2)
202- . oa2en( ) . set_bit( )
197+ i2c. oar2( ) . write( |w| {
198+ w. oa2msk( ) . set ( config. slave_address_mask as u8 ) ;
199+ w . oa2( ) . set ( config. slave_address_2) ;
200+ w . oa2en( ) . set_bit( )
203201 } ) ;
204202 // Enable acknowlidge control
205203 i2c. cr1( ) . modify( |_, w| w. sbc( ) . set_bit( ) ) ;
@@ -265,21 +263,20 @@ macro_rules! i2c {
265263 // Set START and prepare to send `bytes`.
266264 // The START bit can be set even if the bus is BUSY or
267265 // I2C is in slave mode.
268- self . i2c. cr2( ) . write( |w| unsafe {
269- w
270- // Set number of bytes to transfer
271- . nbytes( ) . bits( sndlen as u8 )
272- // Set address to transfer to/from
273- . sadd( ) . bits( ( addr << 1 ) as u16 )
274- // 7-bit addressing mode
275- . add10( ) . clear_bit( )
276- // Set transfer direction to write
277- . rd_wrn( ) . clear_bit( )
278- // Software end mode
279- . autoend( ) . clear_bit( )
280- . reload( ) . clear_bit( )
281- // Start transfer
282- . start( ) . set_bit( )
266+ self . i2c. cr2( ) . write( |w| {
267+ // Set number of bytes to transfer
268+ w. nbytes( ) . set( sndlen as u8 ) ;
269+ // Set address to transfer to/from
270+ w. sadd( ) . set( ( addr << 1 ) as u16 ) ;
271+ // 7-bit addressing mode
272+ w. add10( ) . clear_bit( ) ;
273+ // Set transfer direction to write
274+ w. rd_wrn( ) . clear_bit( ) ;
275+ // Software end mode
276+ w. autoend( ) . clear_bit( ) ;
277+ w. reload( ) . clear_bit( ) ;
278+ // Start transfer
279+ w. start( ) . set_bit( )
283280 } ) ;
284281 let mut idx = 0 ;
285282 // Wait until we are allowed to send data
@@ -288,29 +285,28 @@ macro_rules! i2c {
288285 for byte in snd_buffer {
289286 busy_wait!( self . i2c, txis, bit_is_set, idx, sndlen) ;
290287 // Put byte on the wire
291- self . i2c. txdr( ) . write( |w| unsafe { w. txdata( ) . bits ( * byte) } ) ;
288+ self . i2c. txdr( ) . write( |w| w. txdata( ) . set ( * byte) ) ;
292289 idx += 1 ;
293290 }
294291 // Wait until the write finishes before beginning to read.
295292 let dummy = 0xFE ;
296293 busy_wait!( self . i2c, tc, bit_is_set, idx, dummy ) ;
297294
298295 // reSTART and prepare to receive bytes into `rcv_buffer`
299- self . i2c. cr2( ) . write( |w| unsafe {
300- w
301- // Set number of bytes to transfer
302- . nbytes( ) . bits( rcvlen as u8 )
303- // Set address to transfer to/from
304- . sadd( ) . bits( ( addr << 1 ) as u16 )
305- // 7-bit addressing mode
306- . add10( ) . clear_bit( )
307- // Set transfer direction to read
308- . rd_wrn( ) . set_bit( )
309- // Automatic end mode
310- . autoend( ) . set_bit( )
311- . reload( ) . clear_bit( )
312- // Start transfer
313- . start( ) . set_bit( )
296+ self . i2c. cr2( ) . write( |w| {
297+ // Set number of bytes to transfer
298+ w. nbytes( ) . set( rcvlen as u8 ) ;
299+ // Set address to transfer to/from
300+ w. sadd( ) . set( ( addr << 1 ) as u16 ) ;
301+ // 7-bit addressing mode
302+ w. add10( ) . clear_bit( ) ;
303+ // Set transfer direction to read
304+ w. rd_wrn( ) . set_bit( ) ;
305+ // Automatic end mode
306+ w. autoend( ) . set_bit( ) ;
307+ w. reload( ) . clear_bit( ) ;
308+ // Start transfer
309+ w. start( ) . set_bit( )
314310 } ) ;
315311
316312 idx = 0 ;
@@ -336,19 +332,18 @@ macro_rules! i2c {
336332 // This could be up to 50% of a bus cycle (ie. up to 0.5/freq)
337333 while self . i2c. cr2( ) . read( ) . start( ) . bit_is_set( ) { } ;
338334
339- self . i2c. cr2( ) . modify( |_, w| unsafe {
340- w
341- // Start transfer
342- . start( ) . set_bit( )
343- // Set number of bytes to transfer
344- . nbytes( ) . bits( buflen as u8 )
345- // Set address to transfer to/from
346- . sadd( ) . bits( ( addr << 1 ) as u16 )
347- // Set transfer direction to write
348- . rd_wrn( ) . clear_bit( )
349- // Automatic end mode
350- . autoend( ) . set_bit( )
351- . reload( ) . clear_bit( )
335+ self . i2c. cr2( ) . modify( |_, w| {
336+ // Start transfer
337+ w. start( ) . set_bit( ) ;
338+ // Set number of bytes to transfer
339+ w. nbytes( ) . set( buflen as u8 ) ;
340+ // Set address to transfer to/from
341+ w. sadd( ) . set( ( addr << 1 ) as u16 ) ;
342+ // Set transfer direction to write
343+ w. rd_wrn( ) . clear_bit( ) ;
344+ // Automatic end mode
345+ w. autoend( ) . set_bit( ) ;
346+ w. reload( ) . clear_bit( )
352347 } ) ;
353348
354349 let mut idx = 0 ;
@@ -358,7 +353,7 @@ macro_rules! i2c {
358353
359354 // Put byte on the wire
360355 if idx < buflen {
361- self . i2c. txdr( ) . write( |w| unsafe { w. txdata( ) . bits ( bytes[ idx] ) } ) ;
356+ self . i2c. txdr( ) . write( |w| w. txdata( ) . set ( bytes[ idx] ) ) ;
362357 idx += 1 ;
363358 }
364359 }
@@ -382,19 +377,18 @@ macro_rules! i2c {
382377 // Set START and prepare to receive bytes into `buffer`.
383378 // The START bit can be set even if the bus
384379 // is BUSY or I2C is in slave mode.
385- self . i2c. cr2( ) . modify( |_, w| unsafe {
386- w
387- // Start transfer
388- . start( ) . set_bit( )
389- // Set number of bytes to transfer
390- . nbytes( ) . bits( buflen as u8 )
391- // Set address to transfer to/from
392- . sadd( ) . bits( ( addr << 1 ) as u16 )
393- // Set transfer direction to read
394- . rd_wrn( ) . set_bit( )
395- // automatic end mode
396- . autoend( ) . set_bit( )
397- . reload( ) . clear_bit( )
380+ self . i2c. cr2( ) . modify( |_, w| {
381+ // Start transfer
382+ w. start( ) . set_bit( ) ;
383+ // Set number of bytes to transfer
384+ w. nbytes( ) . set( buflen as u8 ) ;
385+ // Set address to transfer to/from
386+ w. sadd( ) . set( ( addr << 1 ) as u16 ) ;
387+ // Set transfer direction to read
388+ w. rd_wrn( ) . set_bit( ) ;
389+ // automatic end mode
390+ w. autoend( ) . set_bit( ) ;
391+ w. reload( ) . clear_bit( )
398392 } ) ;
399393 let mut idx = 0 ;
400394 loop {
@@ -449,9 +443,9 @@ macro_rules! i2c {
449443 assert!( buflen < 256 && buflen > 0 ) ;
450444
451445 // Set the nbytes and prepare to send bytes into `buffer`.
452- self . i2c. cr2( ) . modify( |_, w| unsafe {
453- w. nbytes( ) . bits ( buflen as u8 )
454- . reload( ) . clear_bit( )
446+ self . i2c. cr2( ) . modify( |_, w| {
447+ w. nbytes( ) . set ( buflen as u8 ) ;
448+ w . reload( ) . clear_bit( )
455449 } ) ;
456450 // flush i2c tx register
457451 self . i2c. isr( ) . write( |w| w. txe( ) . set_bit( ) ) ;
@@ -465,13 +459,13 @@ macro_rules! i2c {
465459
466460 // Put byte on the wire
467461 if idx < buflen {
468- self . i2c. txdr( ) . write( |w| unsafe { w. txdata( ) . bits ( bytes[ idx] ) } ) ;
462+ self . i2c. txdr( ) . write( |w| w. txdata( ) . set ( bytes[ idx] ) ) ;
469463 idx += 1 ;
470464 } else {
471465 // we will never reach here. In case the master wants to read more than buflen
472466 // the hardware will send 0xFF
473467 // Also means that on slave side we cannot detect this error case
474- self . i2c. txdr( ) . write( |w| unsafe { w. txdata( ) . bits ( 0x21 ) } ) ;
468+ self . i2c. txdr( ) . write( |w| w. txdata( ) . set ( 0x21 ) ) ;
475469 }
476470 }
477471 }
@@ -482,12 +476,11 @@ macro_rules! i2c {
482476 assert!( buflen < 256 && buflen > 0 ) ;
483477
484478 // Set the nbytes START and prepare to receive bytes into `buffer`.
485- self . i2c. cr2( ) . modify( |_, w| unsafe {
486- w
487- // Set number of bytes to transfer: maximum as all incoming bytes will be ACK'ed
488- . nbytes( ) . bits( buflen as u8 )
489- // during sending nbytes automatically send a ACK, stretch clock after last byte
490- . reload( ) . set_bit( )
479+ self . i2c. cr2( ) . modify( |_, w| {
480+ // Set number of bytes to transfer: maximum as all incoming bytes will be ACK'ed
481+ w. nbytes( ) . set( buflen as u8 ) ;
482+ // during sending nbytes automatically send a ACK, stretch clock after last byte
483+ w. reload( ) . set_bit( )
491484 } ) ;
492485 // end address phase, release clock stretching
493486 self . i2c. icr( ) . write( |w|
0 commit comments