@@ -165,6 +165,24 @@ pub enum Error {
165
165
RxOverrun ,
166
166
/// Transmit FIFO underrun.
167
167
TxUnderrun ,
168
+ /// Invalid input.
169
+ InvalidInput ,
170
+ }
171
+
172
+ impl fmt:: Display for Error {
173
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
174
+ match self {
175
+ Self :: NoCard => "no card" ,
176
+ Self :: CommandCrc => "command CRC check failed" ,
177
+ Self :: DataCrc => "data block CRC check failed" ,
178
+ Self :: Timeout => "timeout" ,
179
+ Self :: SoftwareTimeout => "software timeout" ,
180
+ Self :: RxOverrun => "receive FIFO overrun" ,
181
+ Self :: TxUnderrun => "transmit FIFO underrun" ,
182
+ Self :: InvalidInput => "invalid input" ,
183
+ }
184
+ . fmt ( f)
185
+ }
168
186
}
169
187
170
188
macro_rules! sta_rx_tx_err {
@@ -932,8 +950,8 @@ pub struct FatFsCursor<SDMMC> {
932
950
current_block : Option < u32 > ,
933
951
}
934
952
935
- impl FatFsCursor < Sdmmc > {
936
- pub fn new ( sdmmc : Sdmmc ) -> Self {
953
+ impl < SDMMC > FatFsCursor < SDMMC > {
954
+ pub fn new ( sdmmc : SDMMC ) -> Self {
937
955
Self {
938
956
sdmmc,
939
957
pos : 0 ,
@@ -942,14 +960,19 @@ impl FatFsCursor<Sdmmc> {
942
960
current_block : None ,
943
961
}
944
962
}
963
+ }
945
964
965
+ impl < SDMMC > FatFsCursor < SDMMC >
966
+ where
967
+ SDMMC : AsMut < Sdmmc > ,
968
+ {
946
969
pub fn partition_info ( & mut self ) -> Result < ( u32 , u32 ) , Error > {
947
970
if let Some ( partition_info) = self . partition_info {
948
971
return Ok ( partition_info) ;
949
972
}
950
973
951
974
let mut block = [ 0 ; 512 ] ;
952
- self . sdmmc . read_block ( 0 , & mut block) ?;
975
+ self . sdmmc . as_mut ( ) . read_block ( 0 , & mut block) ?;
953
976
954
977
// TODO: Support other partitions.
955
978
let partition1_info = & block[ 446 ..] [ ..16 ] ;
@@ -971,20 +994,33 @@ impl FatFsCursor<Sdmmc> {
971
994
}
972
995
}
973
996
997
+ impl AsMut < Sdmmc > for Sdmmc {
998
+ fn as_mut ( & mut self ) -> & mut Self {
999
+ self
1000
+ }
1001
+ }
1002
+
974
1003
#[ cfg( feature = "fatfs" ) ]
975
- impl IntoStorage < FatFsCursor < Sdmmc > > for Sdmmc {
976
- fn into_storage ( self ) -> FatFsCursor < Sdmmc > {
1004
+ impl < ' sdmmc > IntoStorage < FatFsCursor < & ' sdmmc mut Sdmmc > > for & ' sdmmc mut Sdmmc {
1005
+ fn into_storage ( self ) -> FatFsCursor < & ' sdmmc mut Sdmmc > {
977
1006
FatFsCursor :: new ( self )
978
1007
}
979
1008
}
980
1009
981
1010
#[ cfg( feature = "fatfs" ) ]
982
1011
impl < SDMMC > IoBase for FatFsCursor < SDMMC > {
983
- type Error = fatfs :: Error < Error > ;
1012
+ type Error = Error ;
984
1013
}
985
1014
986
1015
#[ cfg( feature = "fatfs" ) ]
987
- impl Seek for FatFsCursor < Sdmmc > {
1016
+ impl < SDMMC > IoBase for & mut FatFsCursor < SDMMC > {
1017
+ type Error = Error ;
1018
+ }
1019
+
1020
+ impl < SDMMC > Seek for FatFsCursor < SDMMC >
1021
+ where
1022
+ SDMMC : AsMut < Sdmmc > ,
1023
+ {
988
1024
fn seek ( & mut self , pos : SeekFrom ) -> Result < u64 , Self :: Error > {
989
1025
// TODO: Use `checked_add_signed` when stable.
990
1026
let new_pos = match pos {
@@ -1008,21 +1044,37 @@ impl Seek for FatFsCursor<Sdmmc> {
1008
1044
}
1009
1045
1010
1046
#[ cfg( feature = "fatfs" ) ]
1011
- impl Read for FatFsCursor < Sdmmc > {
1012
- #[ track_caller]
1047
+ impl < SDMMC > Seek for & mut FatFsCursor < SDMMC >
1048
+ where
1049
+ SDMMC : AsMut < Sdmmc > ,
1050
+ {
1051
+ fn seek ( & mut self , pos : SeekFrom ) -> Result < u64 , Self :: Error > {
1052
+ ( * self ) . seek ( pos)
1053
+ }
1054
+ }
1055
+
1056
+ #[ cfg( feature = "fatfs" ) ]
1057
+ impl < SDMMC > Read for FatFsCursor < SDMMC >
1058
+ where
1059
+ SDMMC : AsMut < Sdmmc > ,
1060
+ {
1013
1061
fn read ( & mut self , buf : & mut [ u8 ] ) -> Result < usize , Self :: Error > {
1014
1062
let ( start, end) = self . partition_info ( ) ?;
1015
1063
1016
1064
let end = end as u64 * 512 ;
1017
- let pos = self . pos . min ( end) ;
1065
+ let pos = self . pos ;
1066
+
1067
+ if pos >= end {
1068
+ return Ok ( 0 ) ;
1069
+ }
1018
1070
1019
1071
let addr = start + ( pos / 512 ) as u32 ;
1020
1072
let offset = ( pos % 512 ) as usize ;
1021
1073
let len = buf. len ( ) . min ( 512 - offset) ;
1022
1074
1023
1075
// Only read the block if we have not already read it.
1024
1076
if self . current_block != Some ( addr) {
1025
- self . sdmmc . read_block ( addr, & mut self . block ) ?;
1077
+ self . sdmmc . as_mut ( ) . read_block ( addr, & mut self . block ) ?;
1026
1078
self . current_block = Some ( addr) ;
1027
1079
}
1028
1080
@@ -1037,29 +1089,42 @@ impl Read for FatFsCursor<Sdmmc> {
1037
1089
}
1038
1090
1039
1091
#[ cfg( feature = "fatfs" ) ]
1040
- impl Write for FatFsCursor < Sdmmc > {
1092
+ impl < SDMMC > Read for & mut FatFsCursor < SDMMC >
1093
+ where
1094
+ SDMMC : AsMut < Sdmmc > ,
1095
+ {
1096
+ fn read ( & mut self , buf : & mut [ u8 ] ) -> Result < usize , Self :: Error > {
1097
+ ( * self ) . read ( buf)
1098
+ }
1099
+ }
1100
+
1101
+ #[ cfg( feature = "fatfs" ) ]
1102
+ impl < SDMMC > Write for FatFsCursor < SDMMC >
1103
+ where
1104
+ SDMMC : AsMut < Sdmmc > ,
1105
+ {
1041
1106
fn write ( & mut self , buf : & [ u8 ] ) -> Result < usize , Self :: Error > {
1042
1107
let ( start, end) = self . partition_info ( ) ?;
1043
1108
1044
1109
let end = end as u64 * 512 ;
1045
1110
let pos = self . pos ;
1046
1111
1047
- if pos + buf . len ( ) as u64 >= end {
1048
- return Err ( Self :: Error :: NotEnoughSpace ) ;
1112
+ if pos >= end {
1113
+ return Ok ( 0 ) ;
1049
1114
}
1050
1115
1051
1116
let addr = start + ( pos / 512 ) as u32 ;
1052
1117
let offset = ( pos % 512 ) as usize ;
1053
1118
let len = buf. len ( ) . min ( 512 - offset) ;
1054
1119
1055
1120
// Only read the block if we have not already read it.
1056
- if self . current_block != Some ( addr) {
1121
+ if self . current_block != Some ( addr) && len != 512 {
1057
1122
self . current_block = None ;
1058
- self . sdmmc . read_block ( addr, & mut self . block ) ?;
1123
+ self . sdmmc . as_mut ( ) . read_block ( addr, & mut self . block ) ?;
1059
1124
}
1060
1125
1061
1126
self . block [ offset..( offset + len) ] . copy_from_slice ( & buf[ 0 ..len] ) ;
1062
- self . sdmmc . write_block ( addr, & self . block ) ?;
1127
+ self . sdmmc . as_mut ( ) . write_block ( addr, & self . block ) ?;
1063
1128
self . current_block = Some ( addr) ;
1064
1129
1065
1130
self . pos += len as u64 ;
@@ -1073,3 +1138,17 @@ impl Write for FatFsCursor<Sdmmc> {
1073
1138
Ok ( ( ) )
1074
1139
}
1075
1140
}
1141
+
1142
+ #[ cfg( feature = "fatfs" ) ]
1143
+ impl < SDMMC > Write for & mut FatFsCursor < SDMMC >
1144
+ where
1145
+ SDMMC : AsMut < Sdmmc > ,
1146
+ {
1147
+ fn write ( & mut self , buf : & [ u8 ] ) -> Result < usize , Self :: Error > {
1148
+ ( * self ) . write ( buf)
1149
+ }
1150
+
1151
+ fn flush ( & mut self ) -> Result < ( ) , Self :: Error > {
1152
+ ( * self ) . flush ( )
1153
+ }
1154
+ }
0 commit comments