Skip to content
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

sdGetClockDivider() implementation conflicts with function description. #31

Open
ptrk8 opened this issue Mar 2, 2023 · 0 comments
Open

Comments

@ptrk8
Copy link

ptrk8 commented Mar 2, 2023

The function signature below states that 3 - 0x3FF are only possible answers for the divisor; however, in the following function located here, if divisor < 3, divisor is set to 4.

Should divisor be set to 3 or 4 if divisor < 3?

Thanks in advance!

/*-[INTERNAL: sdGetClockDivider]--------------------------------------------}
. Get clock divider for the given requested frequency. This is calculated
. relative to the SD base clock of 41.66667Mhz
. RETURN: 3 - 0x3FF are only possible answers for the divisor
. 10Aug17 LdB
.--------------------------------------------------------------------------*/
static uint32_t sdGetClockDivider(uint32_t freq) {
    uint32_t divisor =
            (41666667 + freq - 1) / freq;                // Pi SD frequency is always 41.66667Mhz on baremetal
    if (divisor > 0x3FF) divisor = 0x3FF;                            // Constrain divisor to max 0x3FF
    if (EMMC_SLOTISR_VER->SDVERSION <
        2) {                            // Any version less than HOST SPECIFICATION 3 (Aka numeric 2)
        uint_fast8_t shiftcount = fls_uint32_t(divisor);            // Only 8 bits and set pwr2 div on Hosts specs 1 & 2
        if (shiftcount > 0) shiftcount--;                            // Note the offset of shift by 1 (look at the spec)
        if (shiftcount > 7) shiftcount = 7;                            // It's only 8 bits maximum on HOST_SPEC_V2
        divisor = ((uint32_t) 1 << shiftcount);                        // Version 1,2 take power 2
    } else if (divisor < 3) divisor = 4;                            // Set minimum divisor limit
    LOG_DEBUG("Divisor = %i, Freq Set = %i\n", (int) divisor, (int) (41666667 / divisor));
    return divisor;                                                    // Return divisor that would be required
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant