blob: 880b53431591933caa5f200ddf6145155d91b02d [file] [log] [blame]
Austin Schuh208337d2022-01-01 14:29:11 -08001/*
2 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include "hardware/resets.h"
8#include "hardware/clocks.h"
9#include "hardware/spi.h"
10
11static inline void spi_reset(spi_inst_t *spi) {
12 invalid_params_if(SPI, spi != spi0 && spi != spi1);
13 reset_block(spi == spi0 ? RESETS_RESET_SPI0_BITS : RESETS_RESET_SPI1_BITS);
14}
15
16static inline void spi_unreset(spi_inst_t *spi) {
17 invalid_params_if(SPI, spi != spi0 && spi != spi1);
18 unreset_block_wait(spi == spi0 ? RESETS_RESET_SPI0_BITS : RESETS_RESET_SPI1_BITS);
19}
20
21uint spi_init(spi_inst_t *spi, uint baudrate) {
22 spi_reset(spi);
23 spi_unreset(spi);
24
25 uint baud = spi_set_baudrate(spi, baudrate);
26 spi_set_format(spi, 8, SPI_CPOL_0, SPI_CPHA_0, SPI_MSB_FIRST);
27 // Always enable DREQ signals -- harmless if DMA is not listening
28 hw_set_bits(&spi_get_hw(spi)->dmacr, SPI_SSPDMACR_TXDMAE_BITS | SPI_SSPDMACR_RXDMAE_BITS);
29
30 // Finally enable the SPI
31 hw_set_bits(&spi_get_hw(spi)->cr1, SPI_SSPCR1_SSE_BITS);
32 return baud;
33}
34
35void spi_deinit(spi_inst_t *spi) {
36 hw_clear_bits(&spi_get_hw(spi)->cr1, SPI_SSPCR1_SSE_BITS);
37 hw_clear_bits(&spi_get_hw(spi)->dmacr, SPI_SSPDMACR_TXDMAE_BITS | SPI_SSPDMACR_RXDMAE_BITS);
38 spi_reset(spi);
39}
40
41uint spi_set_baudrate(spi_inst_t *spi, uint baudrate) {
42 uint freq_in = clock_get_hz(clk_peri);
43 uint prescale, postdiv;
44 invalid_params_if(SPI, baudrate > freq_in);
45
46 // Find smallest prescale value which puts output frequency in range of
47 // post-divide. Prescale is an even number from 2 to 254 inclusive.
48 for (prescale = 2; prescale <= 254; prescale += 2) {
49 if (freq_in < (prescale + 2) * 256 * (uint64_t) baudrate)
50 break;
51 }
52 invalid_params_if(SPI, prescale > 254); // Frequency too low
53
54 // Find largest post-divide which makes output <= baudrate. Post-divide is
55 // an integer in the range 1 to 256 inclusive.
56 for (postdiv = 256; postdiv > 1; --postdiv) {
57 if (freq_in / (prescale * (postdiv - 1)) > baudrate)
58 break;
59 }
60
61 spi_get_hw(spi)->cpsr = prescale;
62 hw_write_masked(&spi_get_hw(spi)->cr0, (postdiv - 1) << SPI_SSPCR0_SCR_LSB, SPI_SSPCR0_SCR_BITS);
63
64 // Return the frequency we were able to achieve
65 return freq_in / (prescale * postdiv);
66}
67
68uint spi_get_baudrate(const spi_inst_t *spi) {
69 uint prescale = spi_get_const_hw(spi)->cpsr;
70 uint postdiv = ((spi_get_const_hw(spi)->cr0 & SPI_SSPCR0_SCR_BITS) >> SPI_SSPCR0_SCR_LSB) + 1;
71 return clock_get_hz(clk_peri) / (prescale * postdiv);
72}
73
74// Write len bytes from src to SPI. Simultaneously read len bytes from SPI to dst.
75// Note this function is guaranteed to exit in a known amount of time (bits sent * time per bit)
76int __not_in_flash_func(spi_write_read_blocking)(spi_inst_t *spi, const uint8_t *src, uint8_t *dst, size_t len) {
77 invalid_params_if(SPI, 0 > (int)len);
78
79 // Never have more transfers in flight than will fit into the RX FIFO,
80 // else FIFO will overflow if this code is heavily interrupted.
81 const size_t fifo_depth = 8;
82 size_t rx_remaining = len, tx_remaining = len;
83
84 while (rx_remaining || tx_remaining) {
85 if (tx_remaining && spi_is_writable(spi) && rx_remaining < tx_remaining + fifo_depth) {
86 spi_get_hw(spi)->dr = (uint32_t) *src++;
87 --tx_remaining;
88 }
89 if (rx_remaining && spi_is_readable(spi)) {
90 *dst++ = (uint8_t) spi_get_hw(spi)->dr;
91 --rx_remaining;
92 }
93 }
94
95 return (int)len;
96}
97
98// Write len bytes directly from src to the SPI, and discard any data received back
99int __not_in_flash_func(spi_write_blocking)(spi_inst_t *spi, const uint8_t *src, size_t len) {
100 invalid_params_if(SPI, 0 > (int)len);
101 // Write to TX FIFO whilst ignoring RX, then clean up afterward. When RX
102 // is full, PL022 inhibits RX pushes, and sets a sticky flag on
103 // push-on-full, but continues shifting. Safe if SSPIMSC_RORIM is not set.
104 for (size_t i = 0; i < len; ++i) {
105 while (!spi_is_writable(spi))
106 tight_loop_contents();
107 spi_get_hw(spi)->dr = (uint32_t)src[i];
108 }
109 // Drain RX FIFO, then wait for shifting to finish (which may be *after*
110 // TX FIFO drains), then drain RX FIFO again
111 while (spi_is_readable(spi))
112 (void)spi_get_hw(spi)->dr;
113 while (spi_get_hw(spi)->sr & SPI_SSPSR_BSY_BITS)
114 tight_loop_contents();
115 while (spi_is_readable(spi))
116 (void)spi_get_hw(spi)->dr;
117
118 // Don't leave overrun flag set
119 spi_get_hw(spi)->icr = SPI_SSPICR_RORIC_BITS;
120
121 return (int)len;
122}
123
124// Read len bytes directly from the SPI to dst.
125// repeated_tx_data is output repeatedly on SO as data is read in from SI.
126// Generally this can be 0, but some devices require a specific value here,
127// e.g. SD cards expect 0xff
128int __not_in_flash_func(spi_read_blocking)(spi_inst_t *spi, uint8_t repeated_tx_data, uint8_t *dst, size_t len) {
129 invalid_params_if(SPI, 0 > (int)len);
130 const size_t fifo_depth = 8;
131 size_t rx_remaining = len, tx_remaining = len;
132
133 while (rx_remaining || tx_remaining) {
134 if (tx_remaining && spi_is_writable(spi) && rx_remaining < tx_remaining + fifo_depth) {
135 spi_get_hw(spi)->dr = (uint32_t) repeated_tx_data;
136 --tx_remaining;
137 }
138 if (rx_remaining && spi_is_readable(spi)) {
139 *dst++ = (uint8_t) spi_get_hw(spi)->dr;
140 --rx_remaining;
141 }
142 }
143
144 return (int)len;
145}
146
147// Write len halfwords from src to SPI. Simultaneously read len halfwords from SPI to dst.
148int __not_in_flash_func(spi_write16_read16_blocking)(spi_inst_t *spi, const uint16_t *src, uint16_t *dst, size_t len) {
149 invalid_params_if(SPI, 0 > (int)len);
150 // Never have more transfers in flight than will fit into the RX FIFO,
151 // else FIFO will overflow if this code is heavily interrupted.
152 const size_t fifo_depth = 8;
153 size_t rx_remaining = len, tx_remaining = len;
154
155 while (rx_remaining || tx_remaining) {
156 if (tx_remaining && spi_is_writable(spi) && rx_remaining < tx_remaining + fifo_depth) {
157 spi_get_hw(spi)->dr = (uint32_t) *src++;
158 --tx_remaining;
159 }
160 if (rx_remaining && spi_is_readable(spi)) {
161 *dst++ = (uint16_t) spi_get_hw(spi)->dr;
162 --rx_remaining;
163 }
164 }
165
166 return (int)len;
167}
168
169// Write len bytes directly from src to the SPI, and discard any data received back
170int __not_in_flash_func(spi_write16_blocking)(spi_inst_t *spi, const uint16_t *src, size_t len) {
171 invalid_params_if(SPI, 0 > (int)len);
172 // Deliberately overflow FIFO, then clean up afterward, to minimise amount
173 // of APB polling required per halfword
174 for (size_t i = 0; i < len; ++i) {
175 while (!spi_is_writable(spi))
176 tight_loop_contents();
177 spi_get_hw(spi)->dr = (uint32_t)src[i];
178 }
179
180 while (spi_is_readable(spi))
181 (void)spi_get_hw(spi)->dr;
182 while (spi_get_hw(spi)->sr & SPI_SSPSR_BSY_BITS)
183 tight_loop_contents();
184 while (spi_is_readable(spi))
185 (void)spi_get_hw(spi)->dr;
186
187 // Don't leave overrun flag set
188 spi_get_hw(spi)->icr = SPI_SSPICR_RORIC_BITS;
189
190 return (int)len;
191}
192
193// Read len halfwords directly from the SPI to dst.
194// repeated_tx_data is output repeatedly on SO as data is read in from SI.
195int __not_in_flash_func(spi_read16_blocking)(spi_inst_t *spi, uint16_t repeated_tx_data, uint16_t *dst, size_t len) {
196 invalid_params_if(SPI, 0 > (int)len);
197 const size_t fifo_depth = 8;
198 size_t rx_remaining = len, tx_remaining = len;
199
200 while (rx_remaining || tx_remaining) {
201 if (tx_remaining && spi_is_writable(spi) && rx_remaining < tx_remaining + fifo_depth) {
202 spi_get_hw(spi)->dr = (uint32_t) repeated_tx_data;
203 --tx_remaining;
204 }
205 if (rx_remaining && spi_is_readable(spi)) {
206 *dst++ = (uint16_t) spi_get_hw(spi)->dr;
207 --rx_remaining;
208 }
209 }
210
211 return (int)len;
212}