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