1 |
60 |
zero_gravi |
<<<
|
2 |
|
|
:sectnums:
|
3 |
|
|
==== Serial Peripheral Interface Controller (SPI)
|
4 |
|
|
|
5 |
|
|
[cols="<3,<3,<4"]
|
6 |
|
|
[frame="topbot",grid="none"]
|
7 |
|
|
|=======================
|
8 |
|
|
| Hardware source file(s): | neorv32_spi.vhd |
|
9 |
|
|
| Software driver file(s): | neorv32_spi.c |
|
10 |
|
|
| | neorv32_spi.h |
|
11 |
|
|
| Top entity port: | `spi_sck_o` | 1-bit serial clock output
|
12 |
|
|
| | `spi_sdo_i` | 1-bit serial data output
|
13 |
|
|
| | `spi_sdi_o` | 1-bit serial data input
|
14 |
|
|
| | `spi_csn_i` | 8-bit dedicated chip select (low-active)
|
15 |
|
|
| Configuration generics: | _IO_SPI_EN_ | implement SPI controller when _true_
|
16 |
|
|
| CPU interrupts: | fast IRQ channel 6 | transmission done interrupt (see <<_processor_interrupts>>)
|
17 |
|
|
|=======================
|
18 |
|
|
|
19 |
|
|
**Theory of Operation**
|
20 |
|
|
|
21 |
|
|
SPI is a synchronous serial transmission interface. The NEORV32 SPI transceiver allows 8-, 16-, 24- and 32-
|
22 |
|
|
bit long transmissions. The unit provides 8 dedicated chip select signals via the top entity's `spi_csn_o`
|
23 |
|
|
signal.
|
24 |
|
|
|
25 |
64 |
zero_gravi |
The SPI unit is enabled via the _SPI_CTRL_EN_ bit in the `CTRL` control register. The idle clock polarity is configured via the _SPI_CTRL_CPHA_
|
26 |
60 |
zero_gravi |
bit and can be low (`0`) or high (`1`) during idle. The data quantity to be transferred within a
|
27 |
64 |
zero_gravi |
single transmission is defined via the _SPI_CTRL_SIZEx bits_. The unit supports 8-bit (`00`), 16-bit (`01`), 24-
|
28 |
60 |
zero_gravi |
bit (`10`) and 32-bit (`11`) transfers. Whenever a transfer is completed, the "transmission done interrupt" is triggered.
|
29 |
64 |
zero_gravi |
A transmission is still in progress as long as the _SPI_CTRL_BUSY_ flag is set.
|
30 |
60 |
zero_gravi |
|
31 |
64 |
zero_gravi |
The SPI controller features 8 dedicated chip-select lines. These lines are controlled via the control register's _SPI_CTRL_CSx_ bits. When
|
32 |
|
|
a specifc _SPI_CTRL_CSx_ bit is **set**, the according chip select line `spi_csn_o(x)` goes **low** (low-active chip select lines).
|
33 |
60 |
zero_gravi |
|
34 |
64 |
zero_gravi |
The SPI clock frequency is defined via the 3-bit _SPI_CTRL_PRSCx_ clock prescaler. The following prescalers
|
35 |
60 |
zero_gravi |
are available:
|
36 |
|
|
|
37 |
|
|
.SPI prescaler configuration
|
38 |
|
|
[cols="<4,^1,^1,^1,^1,^1,^1,^1,^1"]
|
39 |
|
|
[options="header",grid="rows"]
|
40 |
|
|
|=======================
|
41 |
64 |
zero_gravi |
| **`SPI_CTRL_PRSCx`** | `0b000` | `0b001` | `0b010` | `0b011` | `0b100` | `0b101` | `0b110` | `0b111`
|
42 |
60 |
zero_gravi |
| Resulting `clock_prescaler` | 2 | 4 | 8 | 64 | 128 | 1024 | 2048 | 4096
|
43 |
|
|
|=======================
|
44 |
|
|
|
45 |
64 |
zero_gravi |
Based on the _SPI_CTRL_PRSCx_ configuration, the actual SPI clock frequency f~SPI~ is derived from the processor's main clock f~main~ and is determined by:
|
46 |
60 |
zero_gravi |
|
47 |
|
|
_**f~SPI~**_ = _f~main~[Hz]_ / (2 * `clock_prescaler`)
|
48 |
|
|
|
49 |
64 |
zero_gravi |
A transmission is started when writing data to the `DATA` register. The data must be LSB-aligned. So if
|
50 |
60 |
zero_gravi |
the SPI transceiver is configured for less than 32-bit transfers data quantity, the transmit data must be placed
|
51 |
64 |
zero_gravi |
into the lowest 8/16/24 bit of `DATA`. Vice versa, the received data is also always LSB-aligned.
|
52 |
60 |
zero_gravi |
|
53 |
64 |
zero_gravi |
.SPI register map (`struct NEORV32_SPI`)
|
54 |
60 |
zero_gravi |
[cols="<2,<2,<4,^1,<7"]
|
55 |
|
|
[options="header",grid="all"]
|
56 |
|
|
|=======================
|
57 |
|
|
| Address | Name [C] | Bit(s), Name [C] | R/W | Function
|
58 |
64 |
zero_gravi |
.16+<| `0xffffffa8` .16+<| `NEORV32_SPI.CTRL` <|`0` _SPI_CTRL_CS0_ ^| r/w .8+<| Direct chip-select 0..7; setting `spi_csn_o(x)` low when set
|
59 |
|
|
<|`1` _SPI_CTRL_CS1_ ^| r/w
|
60 |
|
|
<|`2` _SPI_CTRL_CS2_ ^| r/w
|
61 |
|
|
<|`3` _SPI_CTRL_CS3_ ^| r/w
|
62 |
|
|
<|`4` _SPI_CTRL_CS4_ ^| r/w
|
63 |
|
|
<|`5` _SPI_CTRL_CS5_ ^| r/w
|
64 |
|
|
<|`6` _SPI_CTRL_CS6_ ^| r/w
|
65 |
|
|
<|`7` _SPI_CTRL_CS7_ ^| r/w
|
66 |
|
|
<|`8` _SPI_CTRL_EN_ ^| r/w <| SPI enable
|
67 |
|
|
<|`9` _SPI_CTRL_CPHA_ ^| r/w <| polarity of `spi_sck_o` when idle
|
68 |
|
|
<|`10` _SPI_CTRL_PRSC0_ ^| r/w .3+| 3-bit clock prescaler select
|
69 |
|
|
<|`11` _SPI_CTRL_PRSC1_ ^| r/w
|
70 |
|
|
<|`12` _SPI_CTRL_PRSC2_ ^| r/w
|
71 |
|
|
<|`14` _SPI_CTRL_SIZE0_ ^| r/w .2+<| transfer size (`00`=8-bit, `01`=16-bit, `10`=24-bit, `11`=32-bit)
|
72 |
|
|
<|`15` _SPI_CTRL_SIZE1_ ^| r/w
|
73 |
|
|
<|`31` _SPI_CTRL_BUSY_ ^| r/- <| transmission in progress when set
|
74 |
|
|
| `0xffffffac` | `NEORV32_SPI.DATA` |`31:0` | r/w | receive/transmit data, LSB-aligned
|
75 |
60 |
zero_gravi |
|=======================
|