@@ -13,23 +13,19 @@ const RASR_RW_PRIVILEGED: u32 = 1 << 24;
1313const RASR_RO_PRIVILEGED : u32 = 0b101 << 24 ;
1414const RASR_ENABLED : u32 = 1 ;
1515
16- // not exactly sure if this is right or not
17- // TODO: verify which mode to use
18- //const UNCACHED_SHARED: u32 = 0b100100 << 16;
19- //const UNCACHED_SHARED: u32 = 0b000100 << 16;
20-
2116const MPU_CTRL_ENABLE : u32 = 1 ;
2217const MPU_CTRL_HARD_FAULT_ENABLE : u32 = 1 << 1 ;
2318
24- /// Represents permisions for mpu region
19+ /// Represents permisions for mpu region.
2520#[ derive( Debug , Clone , Copy ) ]
2621pub struct MpuPerms {
2722 pub read : bool ,
2823 pub write : bool ,
2924 pub execute : bool ,
3025}
3126
32- /// Possible sizes for mpu region
27+ /// Possible sizes for mpu region.
28+ // only some possible values are specified, add more later if more sizes are needed
3329#[ repr( u32 ) ]
3430#[ derive( Debug , Clone , Copy ) ]
3531pub enum MpuRegionSize {
@@ -41,6 +37,8 @@ pub enum MpuRegionSize {
4137 MibiByte512 = 0x1c ,
4238}
4339
40+ /// Represents caching behavior cpu will use when accessing a regin of memory.
41+ // there are more possible caching behaviors, but we don't need them so they aren't yet added
4442#[ derive( Debug , Clone , Copy ) ]
4543pub enum MemoryCacheType {
4644 StronglyOrdered ,
@@ -52,6 +50,7 @@ impl MemoryCacheType {
5250 ( tex & 0b111 ) << 3 | ( s & 1 ) << 2 | ( c & 1 ) << 1 | ( b & 1 )
5351 }
5452
53+ /// Convert MemoryCacheType to tex, c, b, and s bits in the rasr register.
5554 fn to_bits ( & self ) -> u32 {
5655 match self {
5756 Self :: StronglyOrdered => Self :: make_memory_type_bits ( 0 , 0 , 0 , 0 ) ,
@@ -78,6 +77,7 @@ impl Mpu {
7877 }
7978 }
8079
80+ /// Contruct base address register, which specifies region number and start address of memory region.
8181 fn construct_rbar ( region_number : u32 , base_address : u32 ) -> u32 {
8282 // cortex m4 apparently only have 8 slots
8383 assert ! ( region_number < 8 ) ;
@@ -88,6 +88,7 @@ impl Mpu {
8888 | RBAR_ENABLED
8989 }
9090
91+ // Construct rasr value, which specfies size of memory region, as well as access permissions and caching behavior.
9192 fn construct_rasr ( size : MpuRegionSize , disable_mask : u8 , permissions : MpuPerms , cache_type : MemoryCacheType ) -> u32 {
9293 let execute_disable = if permissions. execute { 0 } else { RASR_EXECUTE_DISABLE } ;
9394
@@ -105,6 +106,7 @@ impl Mpu {
105106 | RASR_ENABLED
106107 }
107108
109+ /// Set the entry corresponding to `region_number` to have all the specified attributes.
108110 pub unsafe fn set_region (
109111 & mut self ,
110112 region_number : u32 ,
@@ -124,6 +126,7 @@ impl Mpu {
124126 ( rbar, rasr)
125127 }
126128
129+ /// Clears the given region number from any memory protections.
127130 pub unsafe fn clear_region ( & mut self , region_number : u32 ) {
128131 assert ! ( region_number < 8 ) ;
129132
@@ -132,6 +135,7 @@ impl Mpu {
132135 }
133136 }
134137
138+ /// Enables the MPU.
135139 pub unsafe fn enable ( & mut self ) {
136140 unsafe {
137141 self . regs . ctrl . write ( MPU_CTRL_ENABLE | MPU_CTRL_HARD_FAULT_ENABLE ) ;
0 commit comments