1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * *  Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * *  Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * *  Neither the name of Texas Instruments Incorporated nor the names of
17  *    its contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 #include <ti/devices/msp432p4xx/driverlib/debug.h>
33 #include <ti/devices/msp432p4xx/driverlib/interrupt.h>
34 #include <ti/devices/msp432p4xx/driverlib/mpu.h>
35 
MPU_enableModule(uint32_t mpuConfig)36 void MPU_enableModule(uint32_t mpuConfig)
37 {
38     //
39     // Check the arguments.
40     //
41     ASSERT(!(mpuConfig & ~(MPU_CONFIG_PRIV_DEFAULT | MPU_CONFIG_HARDFLT_NMI)));
42 
43     //
44     // Set the MPU control bits according to the flags passed by the user,
45     // and also set the enable bit.
46     //
47     MPU->CTRL = mpuConfig | MPU_CTRL_ENABLE_Msk;
48 }
49 
MPU_disableModule(void)50 void MPU_disableModule(void)
51 {
52     //
53     // Turn off the MPU enable bit.
54     //
55     MPU->CTRL &= ~MPU_CTRL_ENABLE_Msk;
56 
57 }
58 
MPU_getRegionCount(void)59 uint32_t MPU_getRegionCount(void)
60 {
61     //
62     // Read the DREGION field of the MPU type register and mask off
63     // the bits of interest to get the count of regions.
64     //
65     return ((MPU->TYPE & MPU_TYPE_DREGION_Msk) >> NVIC_MPU_TYPE_DREGION_S);
66 }
67 
MPU_enableRegion(uint32_t region)68 void MPU_enableRegion(uint32_t region)
69 {
70     //
71     // Check the arguments.
72     //
73     ASSERT(region < 8);
74 
75     //
76     // Select the region to modify.
77     //
78     MPU->RNR = region;
79 
80     //
81     // Modify the enable bit in the region attributes.
82     //
83     MPU->RASR |= MPU_RASR_ENABLE_Msk;
84 }
85 
MPU_disableRegion(uint32_t region)86 void MPU_disableRegion(uint32_t region)
87 {
88     //
89     // Check the arguments.
90     //
91     ASSERT(region < 8);
92 
93     //
94     // Select the region to modify.
95     //
96     MPU->RNR = region;
97 
98     //
99     // Modify the enable bit in the region attributes.
100     //
101     MPU->RASR &= ~MPU_RASR_ENABLE_Msk;
102 }
103 
MPU_setRegion(uint32_t region,uint32_t addr,uint32_t flags)104 void MPU_setRegion(uint32_t region, uint32_t addr, uint32_t flags)
105 {
106     //
107     // Check the arguments.
108     //
109     ASSERT(region < 8);
110 
111     //
112     // Program the base address, use the region field to select the
113     // region at the same time.
114     //
115     MPU->RBAR = addr | region | MPU_RBAR_VALID_Msk;
116 
117     //
118     // Program the region attributes.  Set the TEX field and the S, C,
119     // and B bits to fixed values that are suitable for all Stellaris
120     // memory.
121     //
122     MPU->RASR = (flags & ~(MPU_RASR_TEX_Msk | MPU_RASR_C_Msk)) | MPU_RASR_S_Msk
123             | MPU_RASR_B_Msk;
124 }
125 
MPU_getRegion(uint32_t region,uint32_t * addr,uint32_t * pflags)126 void MPU_getRegion(uint32_t region, uint32_t *addr, uint32_t *pflags)
127 {
128     //
129     // Check the arguments.
130     //
131     ASSERT(region < 8);
132     ASSERT(addr);
133     ASSERT(pflags);
134 
135     //
136     // Select the region to get.
137     //
138     MPU->RNR = region;
139 
140     //
141     // Read and store the base address for the region.
142     //
143     *addr = MPU->RBAR & MPU_RBAR_ADDR_Msk;
144 
145     //
146     // Read and store the region attributes.
147     //
148     *pflags = MPU->RASR;
149 }
150 
MPU_registerInterrupt(void (* intHandler)(void))151 void MPU_registerInterrupt(void (*intHandler)(void))
152 {
153     //
154     // Check the arguments.
155     //
156     ASSERT(intHandler);
157 
158     //
159     // Register the interrupt handler.
160     //
161     Interrupt_registerInterrupt(FAULT_MPU, intHandler);
162 
163 }
164 
MPU_unregisterInterrupt(void)165 void MPU_unregisterInterrupt(void)
166 {
167     //
168     // Unregister the interrupt handler.
169     //
170     Interrupt_unregisterInterrupt(FAULT_MPU);
171 }
172 
MPU_enableInterrupt(void)173 void MPU_enableInterrupt(void)
174 {
175 
176     //
177     // Enable the memory management fault.
178     //
179     Interrupt_enableInterrupt(FAULT_MPU);
180 
181 }
182 
MPU_disableInterrupt(void)183 void MPU_disableInterrupt(void)
184 {
185     //
186     // Disable the interrupt.
187     //
188     Interrupt_disableInterrupt(FAULT_MPU);
189 }
190