@@ -33,7 +33,7 @@ pub fn render(
3333 let rty = rsize. to_ty ( ) ?;
3434 let description = util:: respace ( & register. description ) ;
3535
36- let unsafety = :: generate :: unsafety ( register. write_constraint . as_ref ( ) , rsize) ;
36+ let unsafety = unsafety ( register. write_constraint . as_ref ( ) , rsize) ;
3737
3838 let mut mod_items = vec ! [ ] ;
3939 let mut reg_impl_items = vec ! [ ] ;
@@ -577,7 +577,7 @@ pub fn fields(
577577
578578 let mut proxy_items = vec ! [ ] ;
579579
580- let mut unsafety = :: generate :: unsafety ( f. write_constraint , f. width ) ;
580+ let mut unsafety = unsafety ( f. write_constraint , f. width ) ;
581581 let bits = & f. bits ;
582582 let fty = & f. ty ;
583583 let offset = & f. offset ;
@@ -804,6 +804,25 @@ pub fn fields(
804804 Ok ( ( ) )
805805}
806806
807+ fn unsafety ( write_constraint : Option < & WriteConstraint > , width : u32 ) -> Option < Ident > {
808+ match write_constraint {
809+ Some ( & WriteConstraint :: Range ( ref range) )
810+ if range. min as u64 == 0 && range. max as u64 == ( 1u64 << width) - 1 =>
811+ {
812+ // the SVD has acknowledged that it's safe to write
813+ // any value that can fit in the field
814+ None
815+ }
816+ None if width == 1 => {
817+ // the field is one bit wide, so we assume it's legal to write
818+ // either value into it or it wouldn't exist; despite that
819+ // if a writeConstraint exists then respect it
820+ None
821+ }
822+ _ => Some ( Ident :: new ( "unsafe" ) ) ,
823+ }
824+ }
825+
807826#[ derive( Clone , Debug ) ]
808827pub struct Base < ' a > {
809828 pub peripheral : Option < & ' a str > ,
0 commit comments