@@ -2,8 +2,8 @@ use crate::error::Error;
22use crate :: error:: ErrorKind :: I2cNoAck ;
33use crate :: { FtInner , PinUse } ;
44use ftdi_mpsse:: { ClockBitsIn , ClockBitsOut , MpsseCmdBuilder , MpsseCmdExecutor } ;
5- use std:: result :: Result ;
6- use std:: { cell :: RefCell , sync :: Mutex } ;
5+ use std:: borrow :: BorrowMut ;
6+ use std:: sync :: { Arc , Mutex } ;
77
88/// SCL bitmask
99const SCL : u8 = 1 << 0 ;
@@ -21,7 +21,7 @@ const BITS_OUT: ClockBitsOut = ClockBitsOut::MsbNeg;
2121#[ derive( Debug ) ]
2222pub struct I2c < ' a , Device : MpsseCmdExecutor > {
2323 /// Parent FTDI device.
24- mtx : & ' a Mutex < RefCell < FtInner < Device > > > ,
24+ mtx : & ' a Arc < Mutex < FtInner < Device > > > ,
2525 /// Length of the start, repeated start, and stop conditions.
2626 ///
2727 /// The units for these are dimensionless number of MPSSE commands.
3737 E : std:: error:: Error ,
3838 Error < E > : From < E > ,
3939{
40- pub ( crate ) fn new ( mtx : & Mutex < RefCell < FtInner < Device > > > ) -> Result < I2c < Device > , Error < E > > {
41- let lock = mtx. lock ( ) . expect ( "Failed to aquire FTDI mutex" ) ;
42- let mut inner = lock. borrow_mut ( ) ;
40+ pub ( crate ) fn new ( mtx : & Arc < Mutex < FtInner < Device > > > ) -> Result < I2c < Device > , Error < E > > {
41+ let mut lock = mtx. lock ( ) . expect ( "Failed to aquire FTDI mutex" ) ;
42+ let inner = lock. borrow_mut ( ) ;
4343 inner. allocate_pin ( 0 , PinUse :: I2c ) ;
4444 inner. allocate_pin ( 1 , PinUse :: I2c ) ;
4545 inner. allocate_pin ( 2 , PinUse :: I2c ) ;
@@ -127,8 +127,8 @@ where
127127 fn read_fast ( & mut self , address : u8 , buffer : & mut [ u8 ] ) -> Result < ( ) , Error < E > > {
128128 assert ! ( !buffer. is_empty( ) , "buffer must be a non-empty slice" ) ;
129129
130- let lock = self . mtx . lock ( ) . expect ( "Failed to aquire FTDI mutex" ) ;
131- let mut inner = lock. borrow_mut ( ) ;
130+ let mut lock = self . mtx . lock ( ) . expect ( "Failed to aquire FTDI mutex" ) ;
131+ let inner = lock. borrow_mut ( ) ;
132132
133133 // ST
134134 let mut mpsse_cmd: MpsseCmdBuilder = MpsseCmdBuilder :: new ( ) ;
@@ -198,8 +198,8 @@ where
198198 fn read_slow ( & mut self , address : u8 , buffer : & mut [ u8 ] ) -> Result < ( ) , Error < E > > {
199199 assert ! ( !buffer. is_empty( ) , "buffer must be a non-empty slice" ) ;
200200
201- let lock = self . mtx . lock ( ) . expect ( "Failed to aquire FTDI mutex" ) ;
202- let mut inner = lock. borrow_mut ( ) ;
201+ let mut lock = self . mtx . lock ( ) . expect ( "Failed to aquire FTDI mutex" ) ;
202+ let inner = lock. borrow_mut ( ) ;
203203
204204 // ST
205205 let mut mpsse_cmd: MpsseCmdBuilder = MpsseCmdBuilder :: new ( ) ;
@@ -272,8 +272,8 @@ where
272272 fn write_fast ( & mut self , addr : u8 , bytes : & [ u8 ] ) -> Result < ( ) , Error < E > > {
273273 assert ! ( !bytes. is_empty( ) , "bytes must be a non-empty slice" ) ;
274274
275- let lock = self . mtx . lock ( ) . expect ( "Failed to aquire FTDI mutex" ) ;
276- let mut inner = lock. borrow_mut ( ) ;
275+ let mut lock = self . mtx . lock ( ) . expect ( "Failed to aquire FTDI mutex" ) ;
276+ let inner = lock. borrow_mut ( ) ;
277277 let mut mpsse_cmd: MpsseCmdBuilder = MpsseCmdBuilder :: new ( ) ;
278278
279279 // ST
@@ -333,8 +333,8 @@ where
333333 fn write_slow ( & mut self , addr : u8 , bytes : & [ u8 ] ) -> Result < ( ) , Error < E > > {
334334 assert ! ( !bytes. is_empty( ) , "bytes must be a non-empty slice" ) ;
335335
336- let lock = self . mtx . lock ( ) . expect ( "Failed to aquire FTDI mutex" ) ;
337- let mut inner = lock. borrow_mut ( ) ;
336+ let mut lock = self . mtx . lock ( ) . expect ( "Failed to aquire FTDI mutex" ) ;
337+ let inner = lock. borrow_mut ( ) ;
338338
339339 // ST
340340 let mut mpsse_cmd: MpsseCmdBuilder = MpsseCmdBuilder :: new ( ) ;
@@ -414,8 +414,8 @@ where
414414
415415 // lock at the start to prevent GPIO from being modified while we build
416416 // the MPSSE command
417- let lock = self . mtx . lock ( ) . expect ( "Failed to aquire FTDI mutex" ) ;
418- let mut inner = lock. borrow_mut ( ) ;
417+ let mut lock = self . mtx . lock ( ) . expect ( "Failed to aquire FTDI mutex" ) ;
418+ let inner = lock. borrow_mut ( ) ;
419419
420420 let mut mpsse_cmd: MpsseCmdBuilder = MpsseCmdBuilder :: new ( ) ;
421421
@@ -522,8 +522,8 @@ where
522522
523523 // lock at the start to prevent GPIO from being modified while we build
524524 // the MPSSE command
525- let lock = self . mtx . lock ( ) . expect ( "Failed to aquire FTDI mutex" ) ;
526- let mut inner = lock. borrow_mut ( ) ;
525+ let mut lock = self . mtx . lock ( ) . expect ( "Failed to aquire FTDI mutex" ) ;
526+ let inner = lock. borrow_mut ( ) ;
527527
528528 // ST
529529 let mut mpsse_cmd: MpsseCmdBuilder = MpsseCmdBuilder :: new ( ) ;
0 commit comments