Author Topic: VHDL: How to send a sequence via SPI?  (Read 1255 times)

0 Members and 1 Guest are viewing this topic.

Offline ivonenandTopic starter

  • Contributor
  • Posts: 38
VHDL: How to send a sequence via SPI?
« on: October 19, 2017, 05:43:43 pm »
Hi Guys,
I've written a fairly simple 8-bit SPI master controller in VHDL. It's nothing special, it has a read and write port, a strobe signal and a status flag. Easy. Now I need to use this peripheral to send a sequence of bytes. This is where I need help.

Let's say I need to send 10 bytes to initialize an external ADC that's connected to this SPI. What's the easiest way of doing this? One way is to write a FSM, but it seems I need three states, just to send one byte:

1. Put byte on write port
2. Set strobe signal to 1
3. Wait for status flag
...

So if I program the FSM this way, I need 30 states just to send 10 bytes. Is there an easier way of doing this?

Thanks,
Ivo
 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2803
  • Country: nz
Re: VHDL: How to send a sequence via SPI?
« Reply #1 on: October 19, 2017, 06:38:14 pm »
Add a counter, and hold your values in an array indexed by the counter. That way you only need a few states.

Without the counter you don't really need 30 states.... You could most likely get away with 11.
- waiting to send value 1
- waiting to send value 2
- waiting to send value 3
.....
- all values sent.

The setting of the value and the strobe is what you do when you  transition between states when you see that 'busy' isn't asserted (unless your SPI core lags in setting the 'busy' output, in which case it is a little bit broken)

Oh, here is another idea:

Code: [Select]
if rising_edge(clk) then
  if count = max_count then
     -- All values sent
     strobe = '0';
  elsif strobe = '1' then
     -- dead cycle while waiting for SPI core to react to registered outputs
     strobe = '0';
  elsif spi_busy = '0' then
    -- Send new value to SPI core, next cycle (as "strobe" and "data" are registered)
    strobe <= '1';
    data   <= values(to_integer(count));
    count  <= count + 1;
  else
    -- spi core is busy
    strobe <= '0';
  end if;
end if;
« Last Edit: October 19, 2017, 10:07:42 pm by hamster_nz »
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf