-
Notifications
You must be signed in to change notification settings - Fork 120
feat(PeriphDrivers): MAX32657 clock calibration #1468
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -580,4 +580,44 @@ uint32_t MXC_SYS_ClockMeasure(mxc_sys_compare_clock_t clock, uint32_t compareClo | |
| return MXC_SYS_GetClockMeasure(); | ||
| } | ||
|
|
||
| /* ************************************************************************** */ | ||
| int MXC_SYS_ClockCalibrate(mxc_sys_system_clock_t clock) | ||
| { | ||
| static const int CAL_MS = 10; | ||
| /* IPO_FREQ / ERTCOCC_FREQ, integer divide, rounded */ | ||
| static const int AUTOCAL2_DIV = (IPO_FREQ + (ERTCO_FREQ - 1)) / ERTCO_FREQ; | ||
| int err; | ||
|
|
||
| /* Only the IPO supports calibration */ | ||
| if (clock != MXC_SYS_CLOCK_IPO) { | ||
| return E_BAD_PARAM; | ||
| } | ||
|
|
||
| /* Make sure the ERTCO is enabled */ | ||
| if ((err = MXC_SYS_ClockSourceEnable(MXC_SYS_CLOCK_ERTCO))) { | ||
| return err; | ||
| } | ||
|
|
||
| /* The following section implements section 4.1.2.1 of the MAX32657 UG */ | ||
| MXC_FCR->autocal0 &= ~(MXC_F_FCR_AUTOCAL0_EN); | ||
| MXC_SETFIELD(MXC_FCR->autocal2, MXC_F_FCR_AUTOCAL2_DIV, | ||
| AUTOCAL2_DIV << MXC_F_FCR_AUTOCAL2_DIV_POS); | ||
| MXC_SETFIELD(MXC_FCR->autocal2, MXC_F_FCR_AUTOCAL2_RUNTIME, | ||
| CAL_MS << MXC_F_FCR_AUTOCAL2_RUNTIME_POS); | ||
| MXC_SETFIELD(MXC_FCR->autocal1, MXC_F_FCR_AUTOCAL1_INIT_TRIM, | ||
| 0x64 << MXC_F_FCR_AUTOCAL1_INIT_TRIM_POS); | ||
| MXC_SETFIELD(MXC_FCR->autocal0, MXC_F_FCR_AUTOCAL0_MU, 4 << MXC_F_FCR_AUTOCAL0_MU_POS); | ||
| MXC_FCR->autocal0 |= MXC_F_FCR_AUTOCAL0_LOAD_TRIM | MXC_F_FCR_AUTOCAL0_EN | | ||
| MXC_F_FCR_AUTOCAL0_RUN; | ||
|
|
||
| MXC_Delay(MXC_DELAY_MSEC(CAL_MS)); | ||
|
|
||
| /* Leaving the calibration hardware running will result in a more accurate frequency on average. | ||
| * Trim trim settings will oscillate around the ideal frequency. | ||
| */ | ||
| MXC_FCR->autocal0 &= ~(MXC_F_FCR_AUTOCAL0_RUN); | ||
|
Comment on lines
+615
to
+618
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any scenario where we might want a one-shot calibration and then shutdown the hardware, e.g. for power efficiency? If we want to leave the calibration hardware running, then should at least be documented in the header file for consumers, IMHO.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Clearing the run bit here makes this a one-shot calibration. |
||
|
|
||
| return E_NO_ERROR; | ||
| } | ||
|
|
||
| /**@} end of mxc_sys */ | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comes out to 1526 if my math is correct, should it not be 3662? Even if 1526 was left shifted it would come out to 3052.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3662 and 3051 are based on different IPO frequencies. There is a typo in the documentation. 1526 = 50 MHz / 32768