OpenCores
URL https://opencores.org/ocsvn/neorv32/neorv32/trunk

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [docs/] [datasheet/] [soc_spi.adoc] - Blame information for rev 65

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
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 65 zero_gravi
|                          | `spi_sdo_o` | 1-bit serial data output
13
|                          | `spi_sdi_i` | 1-bit serial data input
14 60 zero_gravi
|                          | `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 65 zero_gravi
 
20 60 zero_gravi
**Theory of Operation**
21
 
22 65 zero_gravi
SPI is a synchronous serial transmission interface for fast on-board communications.
23
The NEORV32 SPI transceiver supports 8-, 16-, 24- and 32-bit wide transmissions.
24
The unit provides 8 dedicated chip select signals via the top entity's `spi_csn_o` signal, which are
25
directly controlled by the SPI module (no additional GPIO required).
26 60 zero_gravi
 
27 65 zero_gravi
The SPI unit is enabled by setting the _SPI_CTRL_EN_ bit in the `CTRL` control register. No transfer can be initiated
28
and no interrupt request will be triggered if this bit is cleared. Furthermore, a transfer being in process
29
can be terminated at any time by clearing this bit.
30 60 zero_gravi
 
31 65 zero_gravi
The data quantity to be transferred within a single transmission is defined via the _SPI_CTRL_SIZEx_ bits.
32
The SPI module supports 8-bit (`00`), 16-bit (`01`), 24-
33
bit (`10`) and 32-bit (`11`) transfers.
34 60 zero_gravi
 
35 65 zero_gravi
A transmission is started when writing data to the `DATA` register. The data must be LSB-aligned. So if
36
the SPI transceiver is configured for less than 32-bit transfers data quantity, the transmit data must be placed
37
into the lowest 8/16/24 bit of `DATA`. Vice versa, the received data is also always LSB-aligned. Application
38
software should only actually process the amount of bits that were configured using _SPI_CTRL_SIZEx_ when
39
reading `DATA`.
40 60 zero_gravi
 
41 65 zero_gravi
The SPI controller features 8 dedicated chip-select lines. These lines are controlled via the control register's
42
_SPI_CTRL_CSx_ bits. When a specific _SPI_CTRL_CSx_ bit is **set**, the according chip-select line `spi_csn_o(x)`
43
goes **low** (low-active chip-select lines).
44
 
45
[IMPORTANT]
46
Changes to the `CTRL` control register should be made only when the SPI module is idle as they directly effect
47
transmissions being in-progress.
48
 
49
[TIP]
50
The actual transmission length is left to the user: after asserting chip-select an arbitrary amount of
51
transmission with arbitrary data quantity (_SPI_CTRL_SIZEx_) can be made before de-asserting chip-select again.
52
 
53
[NOTE]
54
The NEORV32 SPI module only supports _host mode_. Transmission are initiated only by the processor's SPI module
55
(and not by an external SPI module).
56
 
57
[NOTE]
58
The NEORV32 SPI module only support MSB-first mode. Data can be reversed before writing `DATA` (for TX) / after
59
reading `DATA` (for RX) to provide LSB-first transmissions.
60
 
61
 
62
**SPI Clock Configuration**
63
 
64
The SPI module supports all _standard SPI clock modes_ (0, 1, 2, 3), which is via the two control register bits
65
_SPI_CTRL_CPHA_ and _SPI_CTRL_CPOL_. The _SPI_CTRL_CPHA_ bit defines the _clock phase_ and the _SPI_CTRL_CPOL_
66
bit defines the _clock polarity_.
67
 
68
.SPI clock modes; image from https://en.wikipedia.org/wiki/File:SPI_timing_diagram2.svg (license: (Wikimedia) https://en.wikipedia.org/wiki/Creative_Commons[Creative Commons] https://creativecommons.org/licenses/by-sa/3.0/deed.en[Attribution-Share Alike 3.0 Unported])
69
image::SPI_timing_diagram2.wikimedia.png[]
70
 
71
.SPI standard clock modes
72
[cols="<2,^1,^1,^1,^1"]
73
[options="header",grid="rows"]
74
|=======================
75
|                 | Mode 0 | Mode 1 | Mode 2 | Mode 4
76
| _SPI_CTRL_CPOL_ |    `0` |    `0` |    `1` |    `1`
77
| _SPI_CTRL_CPHA_ |    `0` |    `1` |    `0` |    `1`
78
|=======================
79
 
80
The SPI clock frequency (`spi_sck_o`) is programmed by the 3-bit _SPI_CTRL_PRSCx_ clock prescaler.
81
The following prescalers are available:
82
 
83 60 zero_gravi
.SPI prescaler configuration
84
[cols="<4,^1,^1,^1,^1,^1,^1,^1,^1"]
85
[options="header",grid="rows"]
86
|=======================
87 64 zero_gravi
| **`SPI_CTRL_PRSCx`**        | `0b000` | `0b001` | `0b010` | `0b011` | `0b100` | `0b101` | `0b110` | `0b111`
88 60 zero_gravi
| Resulting `clock_prescaler` |       2 |       4 |       8 |      64 |     128 |    1024 |    2048 |    4096
89
|=======================
90
 
91 65 zero_gravi
Based on the _SPI_CTRL_PRSCx_ configuration, the actual SPI clock frequency f~SPI~ is derived from the processor's
92
main clock f~main~ and is determined by:
93 60 zero_gravi
 
94
_**f~SPI~**_ = _f~main~[Hz]_ / (2 * `clock_prescaler`)
95
 
96 65 zero_gravi
Hence, the maximum SPI clock is f~main~ / 4.
97 60 zero_gravi
 
98 65 zero_gravi
 
99
**SPI Interrupt**
100
 
101
The SPI module provides a single interrupt to signal "ready for new transmission" to the CPU. Whenever the SPI
102
module is currently idle (and enabled), the interrupt request is active. A pending interrupt request is cleared
103
by triggering a new SPI transmission or by disabling the SPI module.
104
 
105
 
106 64 zero_gravi
.SPI register map (`struct NEORV32_SPI`)
107 60 zero_gravi
[cols="<2,<2,<4,^1,<7"]
108
[options="header",grid="all"]
109
|=======================
110
| Address | Name [C] | Bit(s), Name [C] | R/W | Function
111 65 zero_gravi
.18+<| `0xffffffa8` .18+<| `NEORV32_SPI.CTRL` <|`0` _SPI_CTRL_CS0_    ^| r/w .8+<| Direct chip-select 0..7; setting `spi_csn_o(x)` low when set
112
                                              <|`1` _SPI_CTRL_CS1_    ^| r/w
113
                                              <|`2` _SPI_CTRL_CS2_    ^| r/w
114
                                              <|`3` _SPI_CTRL_CS3_    ^| r/w
115
                                              <|`4` _SPI_CTRL_CS4_    ^| r/w
116
                                              <|`5` _SPI_CTRL_CS5_    ^| r/w
117
                                              <|`6` _SPI_CTRL_CS6_    ^| r/w
118
                                              <|`7` _SPI_CTRL_CS7_    ^| r/w
119
                                              <|`8` _SPI_CTRL_EN_     ^| r/w <| SPI enable
120
                                              <|`9` _SPI_CTRL_CPHA_   ^| r/w <| clock phase (`0`=sample RX on rising edge & update TX on falling edge; `1`=sample RX on falling edge & update TX on rising edge)
121
                                              <|`10` _SPI_CTRL_PRSC0_ ^| r/w .3+| 3-bit clock prescaler select
122
                                              <|`11` _SPI_CTRL_PRSC1_ ^| r/w
123
                                              <|`12` _SPI_CTRL_PRSC2_ ^| r/w
124
                                              <|`13` _SPI_CTRL_SIZE0_ ^| r/w .2+<| transfer size (`00`=8-bit, `01`=16-bit, `10`=24-bit, `11`=32-bit)
125
                                              <|`14` _SPI_CTRL_SIZE1_ ^| r/w
126
                                              <|`15` _SPI_CTRL_CPOL_  ^| r/w <| clock polarity
127
                                              <|`16` .. `30`          ^| r/- <| _reserved, read as zero
128
                                              <|`31` _SPI_CTRL_BUSY_  ^| r/- <| transmission in progress when set
129 64 zero_gravi
| `0xffffffac` | `NEORV32_SPI.DATA` |`31:0` | r/w | receive/transmit data, LSB-aligned
130 60 zero_gravi
|=======================

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.