0

I want to have SPI master on my RPI Pico, and I require it to have exactly 4.8 MHz clock. But I don't know how to set it up properly, since SPI clock doesn't allow every value. I read about peripheral clocks and tried to meddle with them, and also changed main clock frequency, but didn't achieve anything. Is there a way to get it working in (preferably) hardware SPI interface?

Edit: I tried configuring my SPI the following way, but for given frequencies I got those in comments instead.

stdio_init_all();
//spi_init(spi0, 4800000);//~3.75MHz
//spi_init(spi0, 5000000);//~4MHz
//spi_init(spi0, 6000000);//~4.77MHz
spi_init(spi0, 6200000);//~4.77MHz
//spi_init(spi0, 6250000);//~5.24MHz
//spi_init(spi0, 6300000);//5.24?MHz
//spi_init(spi0, 6500000);//~5.24MHz
spi_set_format(spi0, 8, SPI_CPOL_0, SPI_CPHA_0, SPI_LSB_FIRST);
gpio_set_function(PIN_MOSI, GPIO_FUNC_SPI);
gpio_set_function(PIN_SCK, GPIO_FUNC_SPI);
gpio_set_function(PIN_MISO, GPIO_FUNC_SPI);
gpio_set_function(PIN_CS, GPIO_FUNC_SPI);

The closest being 4.77 MHz - which is pretty good, but I still scratch my head why is that I can't get exactly the frequency I want.

7
  • 1
    Why do you need exactly 4.8 MHz? Slave devices should not care.
    – joan
    Commented Aug 20, 2022 at 21:20
  • @joan sorry for my first response, it was kinda rude, so to answer you properly - I have MEMS microphone that gives me PDM signal. However before I plug it and make use of it, I wanted a "simulator" of a sort, so I figured I could use Pico for that. And AFAIK only SPI may handle such frequencies. And since microphone itself will be clocked by constant 4.8 MHz clock (to simplify further signal processing), I want "simulator" to use that frequency also.
    – PookyFan
    Commented Aug 21, 2022 at 14:14
  • 1
    I doubt you will get exactly 4800000. The nearest I can get (without doing anything special) is 4807692. I doubt anyone will put much effort into an answer without knowing what you have tried and how close you get.
    – joan
    Commented Aug 21, 2022 at 15:32
  • I’m voting to close this question because you've stated an arbitrary requirement with no background or justification, and you've provided no tolerances on your requirement. If you've arrived at this requirement for an exact clock frequency for an SPI application, I have to assume that you've missed the point somewhere along the way - SPI was never designed to require a specific, precise clock.
    – Seamus
    Commented Aug 25, 2022 at 21:15
  • I don't see a reason for closing my question, @Seamus. It just happens that from among various serial interfaces on RPI Pico, only SPI is fast enough to apply it in my project. I see no harm in asking question I've asked. I know SPI generally has other applications, but what am I supposed to use otherwise? Of course I may use some close to chosen frequency, but at the end of the day the final device (microphone) will have 4.8 MHz clock, period. Why should I test my dummy mic using any other frequency than the target one? But ok this is going off topic. Anyway, don't close my question please.
    – PookyFan
    Commented Aug 25, 2022 at 21:32

1 Answer 1

0

The SPI bus frequency is directly dependent on your clock frequency. Imagine a Byte where your SPI Clock is connected to any of the Bits. So now the Byte gets increased every clock cycle (everytime your clock is high) and now your clock signal is generated by the flipping of the Bit to which you connected it. If you connect it to the smallest bit (least significant bit LSB) you generate a signal which is half as fast as your system clock. The next bit is flipped every fourth cycle and so on. For every bit you go to the left your SPI frequency is divided by two.

To generate an exact clock signal like you want, you need to look for a Chip with a system clock equal to 4.8 MHz * 2^x (where x is 1 or greater and a natural number).

Edit: x must be 1 or greater.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.