libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
spi_common_all.h
Go to the documentation of this file.
1/** @addtogroup spi_defines
2 *
3 * @author @htmlonly &copy; @endhtmlonly 2009 Uwe Hermann <uwe@hermann-uwe.de>
4 *
5 */
6/*
7 * This file is part of the libopencm3 project.
8 *
9 * Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
10 *
11 * This library is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this library. If not, see <http://www.gnu.org/licenses/>.
23 */
24
25/* THIS FILE SHOULD NOT BE INCLUDED DIRECTLY, BUT ONLY VIA SPI.H
26The order of header inclusion is important. spi.h includes the device
27specific memorymap.h header before including this header file.*/
28
29/** @cond */
30#if defined(LIBOPENCM3_SPI_H)
31/** @endcond */
32#ifndef LIBOPENCM3_SPI_COMMON_ALL_H
33#define LIBOPENCM3_SPI_COMMON_ALL_H
34
35/**@{*/
36
37/* Registers can be accessed as 16bit or 32bit values. */
38
39/* --- Convenience macros -------------------------------------------------- */
40
41/****************************************************************************/
42/** @defgroup spi_reg_base SPI Register base address
43@ingroup spi_defines
44
45@{*/
46#define SPI1 SPI1_BASE
47#define SPI2 SPI2_BASE
48#define SPI3 SPI3_BASE
49#define SPI4 SPI4_BASE
50#define SPI5 SPI5_BASE
51#define SPI6 SPI6_BASE
52/**@}*/
53
54/* --- SPI registers ------------------------------------------------------- */
55
56/* Control register 1 (SPIx_CR1) */
57/* Note: Not used in I2S mode. */
58#define SPI_CR1(spi_base) MMIO32((spi_base) + 0x00)
59#define SPI1_CR1 SPI_CR1(SPI1_BASE)
60#define SPI2_CR1 SPI_CR1(SPI2_BASE)
61#define SPI3_CR1 SPI_CR1(SPI3_BASE)
62
63/* Control register 2 (SPIx_CR2) */
64#define SPI_CR2(spi_base) MMIO32((spi_base) + 0x04)
65#define SPI1_CR2 SPI_CR2(SPI1_BASE)
66#define SPI2_CR2 SPI_CR2(SPI2_BASE)
67#define SPI3_CR2 SPI_CR2(SPI3_BASE)
68
69/* Status register (SPIx_SR) */
70#define SPI_SR(spi_base) MMIO32((spi_base) + 0x08)
71#define SPI1_SR SPI_SR(SPI1_BASE)
72#define SPI2_SR SPI_SR(SPI2_BASE)
73#define SPI3_SR SPI_SR(SPI3_BASE)
74
75/* Data register (SPIx_DR) */
76#define SPI_DR(spi_base) MMIO32((spi_base) + 0x0c)
77#define SPI1_DR SPI_DR(SPI1_BASE)
78#define SPI2_DR SPI_DR(SPI2_BASE)
79#define SPI3_DR SPI_DR(SPI3_BASE)
80
81/* CRC polynomial register (SPIx_CRCPR) */
82/* Note: Not used in I2S mode. */
83#define SPI_CRCPR(spi_base) MMIO32((spi_base) + 0x10)
84#define SPI1_CRCPR SPI_CRCPR(SPI1_BASE)
85#define SPI2_CRCPR SPI_CRCPR(SPI2_BASE)
86#define SPI3_CRCPR SPI_CRCPR(SPI3_BASE)
87
88/* RX CRC register (SPIx_RXCRCR) */
89/* Note: Not used in I2S mode. */
90#define SPI_RXCRCR(spi_base) MMIO32((spi_base) + 0x14)
91#define SPI1_RXCRCR SPI_RXCRCR(SPI1_BASE)
92#define SPI2_RXCRCR SPI_RXCRCR(SPI2_BASE)
93#define SPI3_RXCRCR SPI_RXCRCR(SPI3_BASE)
94
95/* TX CRC register (SPIx_RXCRCR) */
96/* Note: Not used in I2S mode. */
97#define SPI_TXCRCR(spi_base) MMIO32((spi_base) + 0x18)
98#define SPI1_TXCRCR SPI_TXCRCR(SPI1_BASE)
99#define SPI2_TXCRCR SPI_TXCRCR(SPI2_BASE)
100#define SPI3_TXCRCR SPI_TXCRCR(SPI3_BASE)
101
102/* I2S configuration register (SPIx_I2SCFGR) */
103#define SPI_I2SCFGR(spi_base) MMIO32((spi_base) + 0x1c)
104#define SPI1_I2SCFGR SPI_I2SCFGR(SPI1_BASE)
105#define SPI2_I2SCFGR SPI_I2SCFGR(SPI2_BASE)
106#define SPI3_I2SCFGR SPI_I2SCFGR(SPI3_BASE)
107
108/* I2S prescaler register (SPIx_I2SPR) */
109#define SPI_I2SPR(spi_base) MMIO32((spi_base) + 0x20)
110#define SPI1_I2SPR SPI_I2SPR(SPI1_BASE)
111#define SPI2_I2SPR SPI_I2SPR(SPI2_BASE)
112#define SPI3_I2SPR SPI_I2SPR(SPI3_BASE)
113
114/* --- SPI_CR1 values ------------------------------------------------------ */
115
116/* Note: None of the CR1 bits are used in I2S mode. */
117
118/* BIDIMODE: Bidirectional data mode enable */
119#define SPI_CR1_BIDIMODE_2LINE_UNIDIR (0 << 15)
120#define SPI_CR1_BIDIMODE_1LINE_BIDIR (1 << 15)
121#define SPI_CR1_BIDIMODE (1 << 15)
122
123/* BIDIOE: Output enable in bidirectional mode */
124#define SPI_CR1_BIDIOE (1 << 14)
125
126/* CRCEN: Hardware CRC calculation enable */
127#define SPI_CR1_CRCEN (1 << 13)
128
129/* CRCNEXT: Transmit CRC next */
130#define SPI_CR1_CRCNEXT (1 << 12)
131
132/* RXONLY: Receive only */
133#define SPI_CR1_RXONLY (1 << 10)
134
135/* SSM: Software slave management */
136#define SPI_CR1_SSM (1 << 9)
137
138/* SSI: Internal slave select */
139#define SPI_CR1_SSI (1 << 8)
140
141/* LSBFIRST: Frame format */
142/****************************************************************************/
143/** @defgroup spi_lsbfirst SPI lsb/msb first
144@ingroup spi_defines
145
146@{*/
147#define SPI_CR1_MSBFIRST (0 << 7)
148#define SPI_CR1_LSBFIRST (1 << 7)
149/**@}*/
150
151/* SPE: SPI enable */
152#define SPI_CR1_SPE (1 << 6)
153
154/* BR[2:0]: Baud rate control */
155/****************************************************************************/
156/** @defgroup spi_baudrate SPI peripheral baud rates
157@ingroup spi_defines
158
159@{*/
160#define SPI_CR1_BAUDRATE_FPCLK_DIV_2 (0x00 << 3)
161#define SPI_CR1_BAUDRATE_FPCLK_DIV_4 (0x01 << 3)
162#define SPI_CR1_BAUDRATE_FPCLK_DIV_8 (0x02 << 3)
163#define SPI_CR1_BAUDRATE_FPCLK_DIV_16 (0x03 << 3)
164#define SPI_CR1_BAUDRATE_FPCLK_DIV_32 (0x04 << 3)
165#define SPI_CR1_BAUDRATE_FPCLK_DIV_64 (0x05 << 3)
166#define SPI_CR1_BAUDRATE_FPCLK_DIV_128 (0x06 << 3)
167#define SPI_CR1_BAUDRATE_FPCLK_DIV_256 (0x07 << 3)
168/**@}*/
169/****************************************************************************/
170/** @defgroup spi_br_pre SPI peripheral baud rate prescale values
171@ingroup spi_defines
172
173@{*/
174#define SPI_CR1_BR_FPCLK_DIV_2 0x0
175#define SPI_CR1_BR_FPCLK_DIV_4 0x1
176#define SPI_CR1_BR_FPCLK_DIV_8 0x2
177#define SPI_CR1_BR_FPCLK_DIV_16 0x3
178#define SPI_CR1_BR_FPCLK_DIV_32 0x4
179#define SPI_CR1_BR_FPCLK_DIV_64 0x5
180#define SPI_CR1_BR_FPCLK_DIV_128 0x6
181#define SPI_CR1_BR_FPCLK_DIV_256 0x7
182/**@}*/
183
184/* MSTR: Master selection */
185#define SPI_CR1_MSTR (1 << 2)
186
187/* CPOL: Clock polarity */
188/****************************************************************************/
189/** @defgroup spi_cpol SPI clock polarity
190@ingroup spi_defines
191
192@{*/
193#define SPI_CR1_CPOL_CLK_TO_0_WHEN_IDLE (0 << 1)
194#define SPI_CR1_CPOL_CLK_TO_1_WHEN_IDLE (1 << 1)
195/**@}*/
196#define SPI_CR1_CPOL (1 << 1)
197
198/* CPHA: Clock phase */
199/****************************************************************************/
200/** @defgroup spi_cpha SPI clock phase
201@ingroup spi_defines
202
203@{*/
204#define SPI_CR1_CPHA_CLK_TRANSITION_1 (0 << 0)
205#define SPI_CR1_CPHA_CLK_TRANSITION_2 (1 << 0)
206/**@}*/
207#define SPI_CR1_CPHA (1 << 0)
208
209/* --- SPI_CR2 values ------------------------------------------------------ */
210
211/* Bits [15:8]: Reserved. Forced to 0 by hardware. Used on F3. */
212
213/* TXEIE: Tx buffer empty interrupt enable */
214#define SPI_CR2_TXEIE (1 << 7)
215
216/* RXNEIE: Rx buffer not empty interrupt enable */
217#define SPI_CR2_RXNEIE (1 << 6)
218
219/* ERRIE: Error interrupt enable */
220#define SPI_CR2_ERRIE (1 << 5)
221
222/* Bits [4:3]: Reserved. Forced to 0 by hardware. */
223
224/* SSOE: SS output enable */
225/* Note: Not used in I2S mode. */
226#define SPI_CR2_SSOE (1 << 2)
227
228/* TXDMAEN: Tx buffer DMA enable */
229#define SPI_CR2_TXDMAEN (1 << 1)
230
231/* RXDMAEN: Rx buffer DMA enable */
232#define SPI_CR2_RXDMAEN (1 << 0)
233
234/* --- SPI_SR values ------------------------------------------------------- */
235
236/* Bits [15:8]: Reserved. Forced to 0 by hardware. Used on F3. */
237
238/* BSY: Busy flag */
239#define SPI_SR_BSY (1 << 7)
240
241/* OVR: Overrun flag */
242#define SPI_SR_OVR (1 << 6)
243
244/* MODF: Mode fault */
245/* Note: Not used in I2S mode. */
246#define SPI_SR_MODF (1 << 5)
247
248/* CRCERR: CRC error flag */
249/* Note: Not used in I2S mode. */
250#define SPI_SR_CRCERR (1 << 4)
251
252/* UDR: Underrun flag */
253/* Note: Not used in SPI mode. */
254#define SPI_SR_UDR (1 << 3)
255
256/* CHSIDE: Channel side */
257/* Note: Not used in SPI mode. No meaning in PCM mode. */
258#define SPI_SR_CHSIDE (1 << 2)
259
260/* TXE: Transmit buffer empty */
261#define SPI_SR_TXE (1 << 1)
262
263/* RXNE: Receive buffer not empty */
264#define SPI_SR_RXNE (1 << 0)
265
266/* --- SPI_DR values ------------------------------------------------------- */
267
268/* SPI_DR[15:0]: Data Register. */
269
270/* --- SPI_CRCPR values ---------------------------------------------------- */
271
272/* Note: Not used in I2S mode. */
273/* SPI_CRCPR [15:0]: CRC Polynomial Register. */
274
275/* --- SPI_RXCRCR values --------------------------------------------------- */
276
277/* Note: Not used in I2S mode. */
278/* SPI_RXCRCR [15:0]: RX CRC Register. */
279
280/* --- SPI_TXCRCR values --------------------------------------------------- */
281
282/* Note: Not used in I2S mode. */
283/* SPI_TXCRCR [15:0]: TX CRC Register. */
284
285/* --- SPI_I2SCFGR values -------------------------------------------------- */
286
287/* Note: None of these bits are used in SPI mode. */
288
289/* Bits [15:12]: Reserved. Forced to 0 by hardware. */
290
291/* I2SMOD: I2S mode selection */
292#define SPI_I2SCFGR_I2SMOD (1 << 11)
293
294/* I2SE: I2S enable */
295#define SPI_I2SCFGR_I2SE (1 << 10)
296
297/* I2SCFG[9:8]: I2S configuration mode */
298#define SPI_I2SCFGR_I2SCFG_LSB 8
299#define SPI_I2SCFGR_I2SCFG_SLAVE_TRANSMIT 0x0
300#define SPI_I2SCFGR_I2SCFG_SLAVE_RECEIVE 0x1
301#define SPI_I2SCFGR_I2SCFG_MASTER_TRANSMIT 0x2
302#define SPI_I2SCFGR_I2SCFG_MASTER_RECEIVE 0x3
303
304/* PCMSYNC: PCM frame synchronization */
305#define SPI_I2SCFGR_PCMSYNC (1 << 7)
306
307/* Bit 6: Reserved. Forced to 0 by hardware. */
308
309/* I2SSTD[5:4]: I2S standard selection */
310#define SPI_I2SCFGR_I2SSTD_LSB 4
311#define SPI_I2SCFGR_I2SSTD_I2S_PHILIPS 0x0
312#define SPI_I2SCFGR_I2SSTD_MSB_JUSTIFIED 0x1
313#define SPI_I2SCFGR_I2SSTD_LSB_JUSTIFIED 0x2
314#define SPI_I2SCFGR_I2SSTD_PCM 0x3
315
316/* CKPOL: Steady state clock polarity */
317#define SPI_I2SCFGR_CKPOL (1 << 3)
318
319/* DATLEN[2:1]: Data length to be transferred */
320#define SPI_I2SCFGR_DATLEN_LSB 1
321#define SPI_I2SCFGR_DATLEN_16BIT 0x0
322#define SPI_I2SCFGR_DATLEN_24BIT 0x1
323#define SPI_I2SCFGR_DATLEN_32BIT 0x2
324
325/* CHLEN: Channel length */
326#define SPI_I2SCFGR_CHLEN (1 << 0)
327
328/* --- SPI_I2SPR values ---------------------------------------------------- */
329
330/* Note: None of these bits are used in SPI mode. */
331
332/* Bits [15:10]: Reserved. Forced to 0 by hardware. */
333
334/* MCKOE: Master clock output enable */
335#define SPI_I2SPR_MCKOE (1 << 9)
336
337/* ODD: Odd factor for the prescaler */
338#define SPI_I2SPR_ODD (1 << 8)
339
340/* I2SDIV[7:0]: I2S linear prescaler */
341/* 0 and 1 are forbidden values */
342
343/* --- Function prototypes ------------------------------------------------- */
344
346
347void spi_enable(uint32_t spi);
348void spi_disable(uint32_t spi);
349uint16_t spi_clean_disable(uint32_t spi);
350void spi_write(uint32_t spi, uint16_t data);
351void spi_send(uint32_t spi, uint16_t data);
352uint16_t spi_read(uint32_t spi);
353uint16_t spi_xfer(uint32_t spi, uint16_t data);
354void spi_set_bidirectional_mode(uint32_t spi);
355void spi_set_unidirectional_mode(uint32_t spi);
358void spi_enable_crc(uint32_t spi);
359void spi_disable_crc(uint32_t spi);
360void spi_set_next_tx_from_buffer(uint32_t spi);
361void spi_set_next_tx_from_crc(uint32_t spi);
362void spi_set_full_duplex_mode(uint32_t spi);
363void spi_set_receive_only_mode(uint32_t spi);
365void spi_enable_software_slave_management(uint32_t spi);
366void spi_set_nss_high(uint32_t spi);
367void spi_set_nss_low(uint32_t spi);
368void spi_send_lsb_first(uint32_t spi);
369void spi_send_msb_first(uint32_t spi);
370void spi_set_baudrate_prescaler(uint32_t spi, uint8_t baudrate);
371void spi_set_master_mode(uint32_t spi);
372void spi_set_slave_mode(uint32_t spi);
373void spi_set_clock_polarity_1(uint32_t spi);
374void spi_set_clock_polarity_0(uint32_t spi);
375void spi_set_clock_phase_1(uint32_t spi);
376void spi_set_clock_phase_0(uint32_t spi);
377void spi_enable_tx_buffer_empty_interrupt(uint32_t spi);
381void spi_enable_error_interrupt(uint32_t spi);
382void spi_disable_error_interrupt(uint32_t spi);
383void spi_enable_ss_output(uint32_t spi);
384void spi_disable_ss_output(uint32_t spi);
385void spi_enable_tx_dma(uint32_t spi);
386void spi_disable_tx_dma(uint32_t spi);
387void spi_enable_rx_dma(uint32_t spi);
388void spi_disable_rx_dma(uint32_t spi);
389void spi_set_standard_mode(uint32_t spi, uint8_t mode);
390
392
393/**@}*/
394
395#endif
396/** @cond */
397#else
398#warning "spi_common_all.h should not be included explicitly, only via spi.h"
399#endif
400/** @endcond */
401
#define END_DECLS
Definition: common.h:34
#define BEGIN_DECLS
Definition: common.h:33
void spi_disable_rx_dma(uint32_t spi)
SPI Disable Receive Transfers via DMA.
void spi_set_next_tx_from_buffer(uint32_t spi)
SPI Next Transmit is a Data Word.
void spi_disable_crc(uint32_t spi)
SPI Disable the CRC.
uint16_t spi_read(uint32_t spi)
SPI Data Read.
void spi_send(uint32_t spi, uint16_t data)
SPI Data Write with Blocking.
void spi_set_unidirectional_mode(uint32_t spi)
SPI Set Unidirectional Mode.
void spi_enable(uint32_t spi)
SPI Enable.
void spi_set_clock_polarity_1(uint32_t spi)
SPI Set the Clock Polarity to High when Idle.
void spi_enable_crc(uint32_t spi)
SPI Enable the CRC.
void spi_disable(uint32_t spi)
SPI Disable.
void spi_set_nss_low(uint32_t spi)
SPI Set the Software NSS Signal Low.
void spi_enable_tx_buffer_empty_interrupt(uint32_t spi)
SPI Enable the Transmit Buffer Empty Interrupt.
void spi_disable_software_slave_management(uint32_t spi)
SPI Disable Slave Management by Hardware.
void spi_set_clock_polarity_0(uint32_t spi)
SPI Set the Clock Polarity to Low when Idle.
void spi_set_baudrate_prescaler(uint32_t spi, uint8_t baudrate)
SPI Set the Baudrate Prescaler.
void spi_write(uint32_t spi, uint16_t data)
SPI Data Write.
void spi_set_full_duplex_mode(uint32_t spi)
SPI Set Full Duplex (3-wire) Mode.
void spi_enable_tx_dma(uint32_t spi)
SPI Enable Transmit Transfers via DMA.
void spi_set_bidirectional_transmit_only_mode(uint32_t spi)
SPI Set Bidirectional Simplex Receive Only Mode.
void spi_disable_ss_output(uint32_t spi)
SPI Set the NSS Pin as an Input.
void spi_send_lsb_first(uint32_t spi)
SPI Set to Send LSB First.
void spi_disable_error_interrupt(uint32_t spi)
SPI Disable the Error Interrupt.
void spi_set_next_tx_from_crc(uint32_t spi)
SPI Next Transmit is a CRC Word.
void spi_set_receive_only_mode(uint32_t spi)
SPI Set Receive Only Mode for Simplex (2-wire) Unidirectional Transfers.
void spi_enable_software_slave_management(uint32_t spi)
SPI Enable Slave Management by Software.
void spi_set_clock_phase_0(uint32_t spi)
SPI Set the Clock Phase to Capture on Leading Edge.
void spi_disable_tx_buffer_empty_interrupt(uint32_t spi)
SPI Disable the Transmit Buffer Empty Interrupt.
void spi_enable_rx_dma(uint32_t spi)
SPI Enable Receive Transfers via DMA.
void spi_set_clock_phase_1(uint32_t spi)
SPI Set the Clock Phase to Capture on Trailing Edge.
void spi_set_standard_mode(uint32_t spi, uint8_t mode)
SPI Standard Mode selection.
void spi_enable_rx_buffer_not_empty_interrupt(uint32_t spi)
SPI Enable the Receive Buffer Ready Interrupt.
void spi_set_nss_high(uint32_t spi)
SPI Set the Software NSS Signal High.
void spi_enable_ss_output(uint32_t spi)
SPI Set the NSS Pin as an Output.
void spi_disable_rx_buffer_not_empty_interrupt(uint32_t spi)
SPI Disable the Receive Buffer Ready Interrupt.
void spi_send_msb_first(uint32_t spi)
SPI Set to Send MSB First.
uint16_t spi_xfer(uint32_t spi, uint16_t data)
SPI Data Write and Read Exchange.
void spi_set_slave_mode(uint32_t spi)
SPI Set to Slave Mode.
void spi_enable_error_interrupt(uint32_t spi)
SPI Enable the Error Interrupt.
void spi_set_bidirectional_mode(uint32_t spi)
SPI Set Bidirectional Simplex Mode.
void spi_set_bidirectional_receive_only_mode(uint32_t spi)
SPI Set Bidirectional Simplex Receive Only Mode.
uint16_t spi_clean_disable(uint32_t spi)
SPI Clean Disable.
void spi_disable_tx_dma(uint32_t spi)
SPI Disable Transmit Transfers via DMA.
void spi_set_master_mode(uint32_t spi)
SPI Set to Master Mode.