-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpmm.h
227 lines (165 loc) · 6.47 KB
/
pmm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright (c) 2020 Jan Brittenson
// See LICENSE for details.
#ifndef _PMM_H_
#define _PMM_H_
#include "common.h"
namespace PMM {
// Issue reset. Mainly for use by panic().
static inline void reset() {
PMMCTL0 |= PMMSWPOR;
}
// Set Vcore level. Shamelessly borrowed from the TI USB demo.
static inline bool vcore_up(uint8_t level) {
PMMCTL0_H = 0xa5;
//The code flow for increasing the Vcore has been altered to work around
//the erratum FLASH37.
//Please refer to the Errata sheet to know if a specific device is affected
// Initialize and save some previous values
uint16_t pmmrie_save = PMMRIE;
PMMRIE &= ~(SVMHVLRPE | SVSHPE | SVMLVLRPE | SVSLPE | SVMHVLRIE | SVMHIE |
SVSMHDLYIE | SVMLVLRIE | SVMLIE | SVSMLDLYIE);
uint16_t svsmhctl_save = SVSMHCTL;
uint16_t svsmlctl_save = SVSMLCTL;
PMMIFG = 0;
//Set SVM highside to new level and check if a VCore increase is possible
SVSMHCTL = SVMHE | SVSHE | (SVSMHRRL0 * level);
while (!(PMMIFG & SVSMHDLYIFG))
;
PMMIFG &= ~SVSMHDLYIFG;
//Check if a VCore increase is possible
if ((PMMIFG & SVMHIFG) == SVMHIFG) {
//-> Vcc is too low for a Vcore increase
//recover the previous settings
PMMIFG &= ~SVSMHDLYIFG;
SVSMHCTL = svsmhctl_save;
//Wait until SVM highside is settled
while (!(PMMIFG & SVSMHDLYIFG))
;
//Clear all Flags
PMMIFG &= ~(SVMHVLRIFG | SVMHIFG | SVSMHDLYIFG |
SVMLVLRIFG | SVMLIFG | SVSMLDLYIFG);
//Restore PMM interrupt enable register
PMMRIE = pmmrie_save;
//Lock PMM registers for write access
PMMCTL0_H = 0x00;
//return: voltage not set
return false;
}
//Set also SVS highside to new level
//Vcc is high enough for a Vcore increase
SVSMHCTL |= SVSHRVL0 * level;
//Wait until SVM highside is settled
while (!(PMMIFG & SVSMHDLYIFG))
;
//Clear flag
PMMIFG &= ~SVSMHDLYIFG;
//Set VCore to new level
PMMCTL0_L = PMMCOREV0 * level;
//Set SVM, SVS low side to new level
SVSMLCTL = SVMLE | (SVSMLRRL0 * level) | SVSLE | (SVSLRVL0 * level);
//Wait until SVM, SVS low side is settled
while (!(PMMIFG & SVSMLDLYIFG))
;
//Clear flag
PMMIFG &= ~SVSMLDLYIFG;
//SVS, SVM core and high side are now set to protect for the new core level
//Restore Low side settings
//Clear all other bits _except_ level settings
SVSMLCTL &= SVSLRVL0 | SVSLRVL1 | SVSMLRRL0 | SVSMLRRL1 | SVSMLRRL2;
//Clear level settings in the backup register,keep all other bits
svsmlctl_save &= ~(SVSLRVL0 | SVSLRVL1 | SVSMLRRL0 | SVSMLRRL1 | SVSMLRRL2);
//Restore low-side SVS monitor settings
SVSMLCTL |= svsmlctl_save;
//Restore High side settings
//Clear all other bits except level settings
SVSMHCTL &= SVSHRVL0 | SVSHRVL1 | SVSMHRRL0 | SVSMHRRL1 | SVSMHRRL2;
//Clear level settings in the backup register,keep all other bits
svsmhctl_save &= ~(SVSHRVL0 | SVSHRVL1 | SVSMHRRL0 | SVSMHRRL1 | SVSMHRRL2);
//Restore backup
SVSMHCTL |= svsmhctl_save;
//Wait until high side, low side settled
while (!(PMMIFG & SVSMLDLYIFG) || !(PMMIFG & SVSMHDLYIFG))
;
//Clear all Flags
PMMIFG &= ~(SVMHVLRIFG | SVMHIFG | SVSMHDLYIFG |
SVMLVLRIFG | SVMLIFG | SVSMLDLYIFG);
//Restore PMM interrupt enable register
PMMRIE = pmmrie_save;
//Lock PMM registers for write access
PMMCTL0_H = 0x00;
return true;
}
static bool vcore_down(uint8_t level) {
uint32_t PMMRIE_backup, SVSMHCTL_backup, SVSMLCTL_backup;
//The code flow for decreasing the Vcore has been altered to work around
//the erratum FLASH37.
//Please refer to the Errata sheet to know if a specific device is affected
//DO NOT ALTER THIS FUNCTION
//Open PMM registers for write access
PMMCTL0_H = 0xA5;
//Disable dedicated Interrupts
//Backup all registers
PMMRIE_backup = PMMRIE;
PMMRIE &= ~(SVMHVLRPE | SVSHPE | SVMLVLRPE | SVSLPE | SVMHVLRIE | SVMHIE |
SVSMHDLYIE | SVMLVLRIE | SVMLIE | SVSMLDLYIE);
SVSMHCTL_backup = SVSMHCTL;
SVSMLCTL_backup = SVSMLCTL;
//Clear flags
PMMIFG &= ~(SVMHIFG | SVSMHDLYIFG | SVMLIFG | SVSMLDLYIFG);
//Set SVM, SVS high & low side to new settings in normal mode
SVSMHCTL = SVMHE | (SVSMHRRL0 * level) | SVSHE | (SVSHRVL0 * level);
SVSMLCTL = SVMLE | (SVSMLRRL0 * level) | SVSLE | (SVSLRVL0 * level);
//Wait until SVM high side and SVM low side is settled
while (!(PMMIFG & SVSMHDLYIFG) || !(PMMIFG & SVSMLDLYIFG))
;
//Clear flags
PMMIFG &= ~(SVSMHDLYIFG + SVSMLDLYIFG);
//SVS, SVM core and high side are now set to protect for the new core level
//Set VCore to new level
PMMCTL0_L = PMMCOREV0 * level;
//Restore Low side settings
//Clear all other bits _except_ level settings
SVSMLCTL &= SVSLRVL0 | SVSLRVL1 | SVSMLRRL0 | SVSMLRRL1 | SVSMLRRL2;
//Clear level settings in the backup register,keep all other bits
SVSMLCTL_backup &= ~(SVSLRVL0 | SVSLRVL1 | SVSMLRRL0 | SVSMLRRL1 | SVSMLRRL2);
//Restore low-side SVS monitor settings
SVSMLCTL |= SVSMLCTL_backup;
//Restore High side settings
//Clear all other bits except level settings
SVSMHCTL &= SVSHRVL0 | SVSHRVL1 | SVSMHRRL0 | SVSMHRRL1 | SVSMHRRL2;
//Clear level settings in the backup register, keep all other bits
SVSMHCTL_backup &= ~(SVSHRVL0 | SVSHRVL1 | SVSMHRRL0 | SVSMHRRL1 | SVSMHRRL2);
//Restore backup
SVSMHCTL |= SVSMHCTL_backup;
//Wait until high side, low side settled
while(!(PMMIFG & SVSMLDLYIFG) || !(PMMIFG & SVSMHDLYIFG))
;
//Clear all Flags
PMMIFG &= ~(SVMHVLRIFG | SVMHIFG | SVSMHDLYIFG |
SVMLVLRIFG | SVMLIFG | SVSMLDLYIFG);
//Restore PMM interrupt enable register
PMMRIE = PMMRIE_backup;
//Lock PMM registers for write access
PMMCTL0_H = 0x00;
//Return: OK
return true;
}
static bool set_vcore(uint8_t level) {
uint8_t actlevel;
bool status = true;
//Set Mask for Max. level
level &= PMMCOREV_3;
//Get actual VCore
actlevel = PMMCTL0 & PMMCOREV_3;
//step by step increase or decrease
while ((level != actlevel) && status) {
if (level > actlevel) {
status = vcore_up(++actlevel);
} else {
status = vcore_down(--actlevel);
}
}
return status;
}
}; // namespace PMM
#endif // _PMM_H_