]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4F_MSP432_LaunchPad_IAR_CCS_Keil/driverlib/sysctl.c
commit 9f316c246baafa15c542a5aea81a94f26e3d6507
[freertos] / FreeRTOS / Demo / CORTEX_M4F_MSP432_LaunchPad_IAR_CCS_Keil / driverlib / sysctl.c
1 /*
2  * -------------------------------------------
3  *    MSP432 DriverLib - v3_10_00_09 
4  * -------------------------------------------
5  *
6  * --COPYRIGHT--,BSD,BSD
7  * Copyright (c) 2014, Texas Instruments Incorporated
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * *  Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  *
17  * *  Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * *  Neither the name of Texas Instruments Incorporated nor the names of
22  *    its contributors may be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
32  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
35  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  * --/COPYRIGHT--*/
37 /* Standard Includes */
38 #include <stdint.h>
39 #include <stdbool.h>
40
41 /* DriverLib Includes */
42 #include <sysctl.h>
43 #include <debug.h>
44
45 #ifdef DEBUG
46
47 static bool SysCtlSRAMBankValid(uint8_t sramBank)
48 {
49     return(
50             sramBank == SYSCTL_SRAM_BANK7 ||
51             sramBank == SYSCTL_SRAM_BANK6 ||
52             sramBank == SYSCTL_SRAM_BANK5 ||
53             sramBank == SYSCTL_SRAM_BANK4 ||
54             sramBank == SYSCTL_SRAM_BANK3 ||
55             sramBank == SYSCTL_SRAM_BANK2 ||
56             sramBank == SYSCTL_SRAM_BANK1
57     );
58 }
59
60 static bool SysCtlSRAMBankValidRet(uint8_t sramBank)
61 {
62     sramBank &= ~(SYSCTL_SRAM_BANK7 & SYSCTL_SRAM_BANK6 &
63             SYSCTL_SRAM_BANK5 & SYSCTL_SRAM_BANK4 &
64             SYSCTL_SRAM_BANK3 & SYSCTL_SRAM_BANK2 &
65             SYSCTL_SRAM_BANK1);
66
67     return (sramBank == 0);
68 }
69
70 static bool SysCtlPeripheralIsValid (uint16_t hwPeripheral)
71 {
72     hwPeripheral &= ~(SYSCTL_PERIPH_DMA & SYSCTL_PERIPH_WDT &
73             SYSCTL_PERIPH_ADC & SYSCTL_PERIPH_EUSCIB3 &
74             SYSCTL_PERIPH_EUSCIB2 & SYSCTL_PERIPH_EUSCIB1 &
75             SYSCTL_PERIPH_EUSCIB0 & SYSCTL_PERIPH_EUSCIA3 &
76             SYSCTL_PERIPH_EUSCIA2 & SYSCTL_PERIPH_EUSCIA1 &
77             SYSCTL_PERIPH_EUSCIA0 & SYSCTL_PERIPH_TIMER32_0_MODULE &
78             SYSCTL_PERIPH_TIMER16_3 & SYSCTL_PERIPH_TIMER16_2 &
79             SYSCTL_PERIPH_TIMER16_2 & SYSCTL_PERIPH_TIMER16_1 &
80             SYSCTL_PERIPH_TIMER16_0);
81
82     return (hwPeripheral == 0);
83 }
84 #endif
85
86 void SysCtl_getTLVInfo(uint_fast8_t tag, uint_fast8_t instance,
87         uint_fast8_t *length, uint32_t **data_address)
88 {
89     /* TLV Structure Start Address */
90     uint32_t *TLV_address = (uint32_t *) TLV_START;
91
92     while (((*TLV_address != tag)) // check for tag and instance
93             && (*TLV_address != TLV_TAGEND))         // do range check first
94     {
95         if (*TLV_address == tag)
96         {
97             if(instance == 0)
98             {
99                 break;
100             }
101
102             /* Repeat until requested instance is reached */
103             instance--;
104         }
105
106         TLV_address += (*(TLV_address + 1)) + 2;
107     }
108
109     /* Check if Tag match happened... */
110     if (*TLV_address == tag)
111     {
112         /* Return length = Address + 1 */
113         *length = (*(TLV_address + 1))*4;
114         /* Return address of first data/value info = Address + 2 */
115         *data_address = (uint32_t *) (TLV_address + 2);
116     }
117     // If there was no tag match and the end of TLV structure was reached..
118     else
119     {
120         // Return 0 for TAG not found
121         *length = 0;
122         // Return 0 for TAG not found
123         *data_address = 0;
124     }
125 }
126
127 uint_least32_t SysCtl_getSRAMSize(void)
128 {
129     return SYSCTL->SRAM_SIZE;
130 }
131
132 uint_least32_t SysCtl_getFlashSize(void)
133 {
134     return SYSCTL->FLASH_SIZE;
135 }
136
137 void SysCtl_disableNMISource(uint_fast8_t flags)
138 {
139     SYSCTL->NMI_CTLSTAT &= ~(flags);
140 }
141
142 void SysCtl_enableNMISource(uint_fast8_t flags)
143 {
144     SYSCTL->NMI_CTLSTAT |= flags;
145 }
146
147 uint_fast8_t SysCtl_getNMISourceStatus(void)
148 {
149     return SYSCTL->NMI_CTLSTAT;
150 }
151
152 void SysCtl_enableSRAMBank(uint_fast8_t sramBank)
153 {
154     ASSERT(SysCtlSRAMBankValid(sramBank));
155
156     /* Waiting for SRAM Ready Bit to be set */
157     while (!(SYSCTL->SRAM_BANKEN & SYSCTL_SRAM_BANKEN_SRAM_RDY))
158         ;
159
160     SYSCTL->SRAM_BANKEN = (sramBank | SYSCTL_SRAM_BANKEN_BNK0_EN);
161 }
162
163 void SysCtl_disableSRAMBank(uint_fast8_t sramBank)
164 {
165     ASSERT(SysCtlSRAMBankValid(sramBank));
166
167     /* Waiting for SRAM Ready Bit to be set */
168     while (!(SYSCTL->SRAM_BANKEN & SYSCTL_SRAM_BANKEN_SRAM_RDY))
169        ;
170
171     switch (sramBank)
172     {
173     case SYSCTL_SRAM_BANK7:
174         sramBank = SYSCTL_SRAM_BANK6 +  SYSCTL_SRAM_BANK5 + SYSCTL_SRAM_BANK4
175                         + SYSCTL_SRAM_BANK3 + SYSCTL_SRAM_BANK2 
176                         + SYSCTL_SRAM_BANK1;
177         break;
178     case SYSCTL_SRAM_BANK6:
179         sramBank =   SYSCTL_SRAM_BANK5 + SYSCTL_SRAM_BANK4
180                         + SYSCTL_SRAM_BANK3 + SYSCTL_SRAM_BANK2 
181                         + SYSCTL_SRAM_BANK1;
182         break;
183     case SYSCTL_SRAM_BANK5:
184         sramBank =   SYSCTL_SRAM_BANK4 + SYSCTL_SRAM_BANK3  
185                 + SYSCTL_SRAM_BANK2 + SYSCTL_SRAM_BANK1;
186         break;
187     case SYSCTL_SRAM_BANK4:
188         sramBank =   SYSCTL_SRAM_BANK3 + SYSCTL_SRAM_BANK2 
189                         + SYSCTL_SRAM_BANK1;
190         break;
191     case SYSCTL_SRAM_BANK3:
192         sramBank =   SYSCTL_SRAM_BANK2 + SYSCTL_SRAM_BANK1;
193         break;
194     case SYSCTL_SRAM_BANK2:
195         sramBank = SYSCTL_SRAM_BANK1;
196         break;
197     case SYSCTL_SRAM_BANK1:
198         sramBank = 0;
199         break;
200     default:
201         return;
202     }
203
204     SYSCTL->SRAM_BANKEN = (sramBank | SYSCTL_SRAM_BANKEN_BNK0_EN);
205 }
206
207 void SysCtl_enableSRAMBankRetention(uint_fast8_t sramBank)
208 {
209     ASSERT(SysCtlSRAMBankValidRet(sramBank));
210
211     /* Waiting for SRAM Ready Bit to be set */
212     while (!(SYSCTL->SRAM_BANKRET & SYSCTL_SRAM_BANKRET_SRAM_RDY))
213             ;
214
215     SYSCTL->SRAM_BANKRET |= sramBank;
216 }
217
218 void SysCtl_disableSRAMBankRetention(uint_fast8_t sramBank)
219 {
220     ASSERT(SysCtlSRAMBankValidRet(sramBank));
221
222     /* Waiting for SRAM Ready Bit to be set */
223     while (!(SYSCTL->SRAM_BANKRET & SYSCTL_SRAM_BANKRET_SRAM_RDY))
224             ;
225
226     SYSCTL->SRAM_BANKRET &= ~sramBank;
227 }
228
229 void SysCtl_rebootDevice(void)
230 {
231     SYSCTL->REBOOT_CTL = (SYSCTL_REBOOT_CTL_REBOOT | SYSCTL_REBOOT_KEY);
232 }
233
234 void SysCtl_enablePeripheralAtCPUHalt(uint_fast16_t devices)
235 {
236     ASSERT(SysCtlPeripheralIsValid(devices));
237     SYSCTL->PERIHALT_CTL &= ~devices;
238 }
239
240 void SysCtl_disablePeripheralAtCPUHalt(uint_fast16_t devices)
241 {
242     ASSERT(SysCtlPeripheralIsValid(devices));
243     SYSCTL->PERIHALT_CTL |= devices;
244 }
245
246 void SysCtl_setWDTTimeoutResetType(uint_fast8_t resetType)
247 {
248     if (resetType)
249         SYSCTL->WDTRESET_CTL |=
250                 SYSCTL_WDTRESET_CTL_TIMEOUT;
251     else
252         SYSCTL->WDTRESET_CTL &= ~SYSCTL_WDTRESET_CTL_TIMEOUT;
253 }
254
255 void SysCtl_setWDTPasswordViolationResetType(uint_fast8_t resetType)
256 {
257     ASSERT(resetType <= SYSCTL_HARD_RESET);
258
259     if (resetType)
260         SYSCTL->WDTRESET_CTL |=
261                 SYSCTL_WDTRESET_CTL_VIOLATION;
262     else
263         SYSCTL->WDTRESET_CTL &= ~SYSCTL_WDTRESET_CTL_VIOLATION;
264 }
265
266 void SysCtl_enableGlitchFilter(void)
267 {
268     SYSCTL->DIO_GLTFLT_CTL |= SYSCTL_DIO_GLTFLT_CTL_GLTCH_EN;
269 }
270
271 void SysCtl_disableGlitchFilter(void)
272 {
273     SYSCTL->DIO_GLTFLT_CTL &= ~SYSCTL_DIO_GLTFLT_CTL_GLTCH_EN;
274 }
275
276 uint_fast16_t SysCtl_getTempCalibrationConstant(uint32_t refVoltage,
277         uint32_t temperature)
278 {
279     return HWREG16(TLV_BASE + refVoltage + temperature);
280 }