tabemann / zeptoforth

A not-so-small Forth for Cortex-M

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RP2040 spi-baud! doesn't look right

pkoning2 opened this issue · comments

I was thinking about the RP2040 rules for the SPI clock, and looked at the logic in spi.fs. It looks strange and doesn't seem to work right.

The natural thing to do would be to divide the system clock by the desired SPI clock, rounded up, and then use that as the divisor. For slow SPI clocks that doesn't quite work because there are two dividers (7 and 8 bits respectively). So I see there is code to calculate the "prescaler" first, then the second divider from that.

But find-spi-prescaler doesn't work right. If I ask it for the prescaler for 20 MHz it tells me 4, which is an ok answer. But if I change sysclk to 120 MHz and ask it that same question I should get 2 or 6 but I still get 4 instead, and find-spi-postdiv gives 2 for a SPI clock of 15 MHz (120 MHz / 8) rather than 20. It turns out the reason is that find-spi-prescaler gets a 32 bit overflow so it doesn't find 2 as a valid prescale.

I'm curious why the code works the way it does.

I have fixed this issue, and the fix is now in the devel, master, and editor-devel (as I am working on a text editor named zeptoed, and it relies on SPI as it edits files in FAT32 filesystems on SDHC cards) branches.