|
33 | 33 |
|
34 | 34 | #include "mxc_device.h"
|
35 | 35 | #include "mxc_assert.h"
|
| 36 | +#include "mxc_errors.h" |
36 | 37 | #include "mxc_sys.h"
|
37 | 38 | #include "gcr_regs.h"
|
| 39 | +#include "flc_regs.h" |
38 | 40 | #include "lp.h"
|
39 | 41 |
|
40 | 42 | void MXC_LP_EnterSleepMode(void)
|
@@ -102,9 +104,94 @@ void MXC_LP_EnterShutDownMode(void)
|
102 | 104 | // Should never reach this line - device will reset on exit from shutdown mode.
|
103 | 105 | }
|
104 | 106 |
|
105 |
| -void MXC_LP_SetOVR(mxc_lp_ovr_t ovr) |
| 107 | +int MXC_LP_SetOVR(mxc_lp_ovr_t ovr) |
106 | 108 | {
|
107 |
| - //not supported yet |
| 109 | + uint32_t current_clock, div; |
| 110 | + int error; |
| 111 | + |
| 112 | + // Ensure part is operating from internal LDO for core power |
| 113 | + if (MXC_PWRSEQ->lpcn & MXC_F_PWRSEQ_LPCN_LDO_DIS) { |
| 114 | + return E_BAD_STATE; |
| 115 | + } |
| 116 | + |
| 117 | + // Select the 8KHz nanoring (no guarantee 32KHz is attached) as system clock source |
| 118 | + current_clock = MXC_GCR->clkctrl & MXC_F_GCR_CLKCTRL_SYSCLK_SEL; |
| 119 | + if (current_clock == MXC_SYS_CLOCK_IPO) { |
| 120 | + error = MXC_SYS_Clock_Select(MXC_SYS_CLOCK_INRO); |
| 121 | + if (error != E_NO_ERROR) { |
| 122 | + return error; |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // Set flash wait state for any clock so its not to low after clock changes. |
| 127 | + MXC_GCR->memctrl = (MXC_GCR->memctrl & ~(MXC_F_GCR_MEMCTRL_FWS)) | |
| 128 | + (0x5UL << MXC_F_GCR_MEMCTRL_FWS_POS); |
| 129 | + |
| 130 | + // Set the OVR bits |
| 131 | + // The OVR enums in mxc_lp_ovr_t equals to their appropriate register setting. |
| 132 | + MXC_SETFIELD(MXC_PWRSEQ->lpcn, MXC_F_PWRSEQ_LPCN_OVR, ovr); |
| 133 | + |
| 134 | + // Set LVE bit |
| 135 | + if (ovr == MXC_LP_OVR_0_9) { |
| 136 | + MXC_FLC0->ctrl |= MXC_F_FLC_CTRL_LVE; |
| 137 | + |
| 138 | + } else { |
| 139 | + MXC_FLC0->ctrl &= ~(MXC_F_FLC_CTRL_LVE); |
| 140 | + } |
| 141 | + |
| 142 | + // Revert the clock to original state if it was IPO |
| 143 | + if (current_clock == MXC_SYS_CLOCK_IPO) { |
| 144 | + error = MXC_SYS_Clock_Select(MXC_SYS_CLOCK_IPO); |
| 145 | + if (error != E_NO_ERROR) { |
| 146 | + return error; |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + // Update SystemCoreClock variable |
| 151 | + SystemCoreClockUpdate(); |
| 152 | + |
| 153 | + // Get the clock divider |
| 154 | + div = (MXC_GCR->clkctrl & MXC_F_GCR_CLKCTRL_SYSCLK_DIV) >> MXC_F_GCR_CLKCTRL_SYSCLK_DIV_POS; |
| 155 | + |
| 156 | + // Set Flash Wait States |
| 157 | + if (ovr == MXC_LP_OVR_0_9) { |
| 158 | + if (div == 0) { |
| 159 | + MXC_GCR->memctrl = (MXC_GCR->memctrl & ~(MXC_F_GCR_MEMCTRL_FWS)) | |
| 160 | + (0x2UL << MXC_F_GCR_MEMCTRL_FWS_POS); |
| 161 | + |
| 162 | + } else { |
| 163 | + MXC_GCR->memctrl = (MXC_GCR->memctrl & ~(MXC_F_GCR_MEMCTRL_FWS)) | |
| 164 | + (0x1UL << MXC_F_GCR_MEMCTRL_FWS_POS); |
| 165 | + } |
| 166 | + |
| 167 | + } else if (ovr == MXC_LP_OVR_1_0) { |
| 168 | + if (div == 0) { |
| 169 | + MXC_GCR->memctrl = (MXC_GCR->memctrl & ~(MXC_F_GCR_MEMCTRL_FWS)) | |
| 170 | + (0x2UL << MXC_F_GCR_MEMCTRL_FWS_POS); |
| 171 | + |
| 172 | + } else { |
| 173 | + MXC_GCR->memctrl = (MXC_GCR->memctrl & ~(MXC_F_GCR_MEMCTRL_FWS)) | |
| 174 | + (0x1UL << MXC_F_GCR_MEMCTRL_FWS_POS); |
| 175 | + } |
| 176 | + |
| 177 | + } else { |
| 178 | + if (div == 0) { |
| 179 | + MXC_GCR->memctrl = (MXC_GCR->memctrl & ~(MXC_F_GCR_MEMCTRL_FWS)) | |
| 180 | + (0x4UL << MXC_F_GCR_MEMCTRL_FWS_POS); |
| 181 | + |
| 182 | + } else if (div == 1) { |
| 183 | + MXC_GCR->memctrl = (MXC_GCR->memctrl & ~(MXC_F_GCR_MEMCTRL_FWS)) | |
| 184 | + (0x2UL << MXC_F_GCR_MEMCTRL_FWS_POS); |
| 185 | + |
| 186 | + } else { |
| 187 | + MXC_GCR->memctrl = (MXC_GCR->memctrl & ~(MXC_F_GCR_MEMCTRL_FWS)) | |
| 188 | + (0x1UL << MXC_F_GCR_MEMCTRL_FWS_POS); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + // Caller must perform peripheral reset |
| 193 | + |
| 194 | + return E_NO_ERROR; |
108 | 195 | }
|
109 | 196 |
|
110 | 197 | void MXC_LP_RetentionRegEnable(void)
|
|
0 commit comments