Can't get correct SPI clock frequency on BeagleBone

Hi all,

I’m trying to control SPI bus on the Beagle Bone.

One thing I noticed is that I can’t set SPI clock speed properly. For example, if I configure the clock to “go” at 500kHz, on the scope I measure more or less a frequency of 350kHz.
If I configure it for 1MHz, I get a frequency of (roughly) 650kHz. With 2MHz setting, I get 1.15MHz frequency.

Do you have some ideas to fix this issue?

In order to talk SPI, I created a simple .c program, based on spidev_test.c. Please take a look at it, maybe it’s something stupid I did in the code.

Inside the archive you will find also the Makefile I used. Compile it with “make”. Then you can run the program with “sudo ./spilight /dev/spidev2.0”

Thank you in advance,

Daniele

spilight.tar.gz (2.14 KB)

Hi!

thank you for having shared your experience. From the time I found this problem I understood the spi speed-related settings far better.

Btw it works like you say. This is what I found:

for beaglebone the spi controller driver is found at driver/spi/spi-omap2-mcspi.c (this path refers obviously to the linux kernel sources). There there is a function whose job is to calculate the divider used to establish the real spi clock frequency. The function name is omap2_mcspi_calc_divisor(). Here follows function’s body:

`
static u32 omap2_mcspi_calc_divisor(u32 speed_hz)
{
u32 div;

for (div = 0; div < 15; div++)
if (speed_hz >= (OMAP2_MCSPI_MAX_FREQ >> div))
return div;

return 15;
}

`

OMAP2_MCSPI_MAX_FREQ is 48000000 Hz.

I found also a table describing the possible spi clock speed with the relative divider:

CLKD | Divide by | SPI_CLK [Hz] |

  • | - | - |
    0 | 1 | 48.000.000 |
    1 | 2 | 24.000.000 |
    2 | 4 | 12.000.000 |
    3 | 8 | 6.000.000 |
    4 | 16 | 3.000.000 |
    5 | 32 | 1.500.000 |
    6 | 64 | 750.000 |
    7 | 128 | 375.000 |
    8 | 256 | 187.500 |
    9 | 512 | 93.750 |
    10 | 1024 | 46.875 |
    11 | 2048 | 23.438 |
    12 | 4096 | 11.719 |
    13 | 8192 | 5.859 |
    14 | 16384 | 2.930 |
    15 | 32768 | 1.465 |

but I didn’t verify if the speed you can get is exactly the one reported in this table. For example with my scope I saw a speed of (roughly) 650kHz which is not reported in the table. I think I should have seen 750kHz instead. I will investigate further and report here.

Best regards!