libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
spi_common_v2.c
Go to the documentation of this file.
1/** @addtogroup spi_file SPI peripheral API
2 * @ingroup peripheral_apis
3
4@author @htmlonly © @endhtmlonly 2009
5Uwe Hermann <uwe@hermann-uwe.de>
6@author @htmlonly &copy; @endhtmlonly 2012
7Ken Sarkies <ksarkies@internode.on.net>
8
9Devices can have up to three SPI peripherals. The common 4-wire full-duplex
10mode of operation is supported, along with 3-wire variants using unidirectional
11communication modes or half-duplex bidirectional communication. A variety of
12options allows many of the SPI variants to be supported. Multimaster operation
13is also supported. A CRC can be generated and checked in hardware.
14
15@note Some JTAG pins need to be remapped if SPI is to be used.
16
17@note The I2S protocol shares the SPI hardware so the two protocols cannot be
18used at the same time on the same peripheral.
19
20Example: Clk/4, positive clock polarity, leading edge trigger, 8-bit words,
21LSB first.
22@code
23 spi_init_master(SPI1, SPI_CR1_BAUDRATE_FPCLK_DIV_4, SPI_CR1_CPOL_CLK_TO_0_WHEN_IDLE,
24 SPI_CR1_CPHA_CLK_TRANSITION_1, SPI_CR1_CRCL_8BIT, SPI_CR1_LSBFIRST);
25 spi_write(SPI1, 0x55); // 8-bit write
26 spi_write(SPI1, 0xaa88); // 16-bit write
27 reg8 = spi_read(SPI1); // 8-bit read
28 reg16 = spi_read(SPI1); // 16-bit read
29@endcode
30
31@todo need additional functions to aid ISRs in retrieving status
32
33*/
34
35/*
36 * This file is part of the libopencm3 project.
37 *
38 * Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
39 *
40 * This library is free software: you can redistribute it and/or modify
41 * it under the terms of the GNU Lesser General Public License as published by
42 * the Free Software Foundation, either version 3 of the License, or
43 * (at your option) any later version.
44 *
45 * This library is distributed in the hope that it will be useful,
46 * but WITHOUT ANY WARRANTY; without even the implied warranty of
47 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48 * GNU Lesser General Public License for more details.
49 *
50 * You should have received a copy of the GNU Lesser General Public License
51 * along with this library. If not, see <http://www.gnu.org/licenses/>.
52 */
53
56
57/**@{*/
58
59/*---------------------------------------------------------------------------*/
60/** @brief Configure the SPI as Master.
61
62The SPI peripheral is configured as a master with communication parameters
63baudrate, frame format lsb/msb first, clock polarity and phase. The SPI
64enable, CRC enable, CRC next CRC length controls are not affected.
65These must be controlled separately.
66
67@param[in] spi Unsigned int32. SPI peripheral identifier @ref spi_reg_base.
68@param[in] br Unsigned int32. Baudrate @ref spi_baudrate.
69@param[in] cpol Unsigned int32. Clock polarity @ref spi_cpol.
70@param[in] cpha Unsigned int32. Clock Phase @ref spi_cpha.
71@param[in] lsbfirst Unsigned int32. Frame format lsb/msb first @ref
72spi_lsbfirst.
73@returns int. Error code.
74*/
75
76int spi_init_master(uint32_t spi, uint32_t br, uint32_t cpol, uint32_t cpha,
77 uint32_t lsbfirst)
78{
79 uint32_t reg32 = SPI_CR1(spi);
80
81 /* Reset all bits omitting SPE, CRCEN, CRCNEXT and CRCL bits. */
83
84 reg32 |= SPI_CR1_MSTR; /* Configure SPI as master. */
85
86 reg32 |= br; /* Set baud rate bits. */
87 reg32 |= cpol; /* Set CPOL value. */
88 reg32 |= cpha; /* Set CPHA value. */
89 reg32 |= lsbfirst; /* Set frame format (LSB- or MSB-first). */
90
91 SPI_CR2(spi) |= SPI_CR2_SSOE; /* common case */
92 SPI_CR1(spi) = reg32;
93
94 return 0; /* TODO */
95}
96
97void spi_send8(uint32_t spi, uint8_t data)
98{
99 /* Wait for transfer finished. */
100 while (!(SPI_SR(spi) & SPI_SR_TXE));
101
102 SPI_DR8(spi) = data;
103}
104
105uint8_t spi_read8(uint32_t spi)
106{
107 /* Wait for transfer finished. */
108 while (!(SPI_SR(spi) & SPI_SR_RXNE));
109
110 return SPI_DR8(spi);
111}
112
113/*---------------------------------------------------------------------------*/
114/** @brief SPI Set CRC length to 8 bits
115
116@param[in] spi Unsigned int32. SPI peripheral identifier @ref spi_reg_base.
117*/
118
119void spi_set_crcl_8bit(uint32_t spi)
120{
121 SPI_CR1(spi) &= ~SPI_CR1_CRCL;
122}
123
124/*---------------------------------------------------------------------------*/
125/** @brief SPI Set CRC length to 16 bits
126
127@param[in] spi Unsigned int32. SPI peripheral identifier @ref spi_reg_base.
128*/
129
130void spi_set_crcl_16bit(uint32_t spi)
131{
132 SPI_CR1(spi) |= SPI_CR1_CRCL;
133}
134
135/** @brief SPI Set data size
136
137@param[in] spi Unsigned int32. SPI peripheral identifier @ref spi_reg_base.
138@param[in] data_s Unsigned int16. data size @ref spi_ds.
139*/
140
141void spi_set_data_size(uint32_t spi, uint16_t data_s)
142{
143 SPI_CR2(spi) = (SPI_CR2(spi) & ~SPI_CR2_DS_MASK) |
144 (data_s & SPI_CR2_DS_MASK);
145}
146
147/*---------------------------------------------------------------------------*/
148/** @brief SPI Set reception threshold to 8 bits
149
150@param[in] spi Unsigned int32. SPI peripheral identifier @ref spi_reg_base.
151*/
152
154{
155 SPI_CR2(spi) |= SPI_CR2_FRXTH;
156}
157
158/*---------------------------------------------------------------------------*/
159/** @brief SPI Set reception threshold to 16 bits
160
161@param[in] spi Unsigned int32. SPI peripheral identifier @ref spi_reg_base.
162*/
163
165{
166 SPI_CR2(spi) &= ~SPI_CR2_FRXTH;
167}
168
169/**@}*/
#define SPI_CR2_DS_MASK
Definition: spi_common_v2.h:88
#define SPI_CR1_CRCL
Definition: spi_common_v2.h:49
#define SPI_SR_RXNE
#define SPI_CR1_CRCNEXT
#define SPI_CR1(spi_base)
#define SPI_CR1_MSTR
#define SPI_SR_TXE
#define SPI_CR2(spi_base)
#define SPI_CR2_FRXTH
Definition: spi_common_v2.h:60
#define SPI_CR1_SPE
#define SPI_CR1_CRCEN
#define SPI_SR(spi_base)
#define SPI_DR8(spi_base)
Definition: spi_common_v2.h:34
#define SPI_CR2_SSOE
void spi_set_data_size(uint32_t spi, uint16_t data_s)
SPI Set data size.
void spi_set_crcl_16bit(uint32_t spi)
SPI Set CRC length to 16 bits.
void spi_send8(uint32_t spi, uint8_t data)
Definition: spi_common_v2.c:97
int spi_init_master(uint32_t spi, uint32_t br, uint32_t cpol, uint32_t cpha, uint32_t lsbfirst)
Configure the SPI as Master.
Definition: spi_common_v2.c:76
void spi_set_crcl_8bit(uint32_t spi)
SPI Set CRC length to 8 bits.
uint8_t spi_read8(uint32_t spi)
void spi_fifo_reception_threshold_16bit(uint32_t spi)
SPI Set reception threshold to 16 bits.
void spi_fifo_reception_threshold_8bit(uint32_t spi)
SPI Set reception threshold to 8 bits.