-
Notifications
You must be signed in to change notification settings - Fork 51
/
spi.c
166 lines (128 loc) · 4.67 KB
/
spi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
spi.c - SPI support for SD card, Trinamic & networking (WizNet) plugins
Part of grblHAL driver for RP2040
Copyright (c) 2020-2024 Terje Io
grblHAL is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
grblHAL is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with grblHAL. If not, see <http://www.gnu.org/licenses/>.
*/
#include "driver.h"
#if SPI_ENABLE
#include "hardware/dma.h"
#include "hardware/spi.h"
#include "pico/stdlib.h"
#define SPIport(p) SPIportI(p)
#define SPIportI(p) spi ## p
#define SPIdreqRX(p) SPIdreqrxI(p)
#define SPIdreqrxI(p) DREQ_SPI ## p ## _RX
#define SPIdreqTX(p) SPIdreqtxI(p)
#define SPIdreqtxI(p) DREQ_SPI ## p ## _TX
#define SPIPORT SPIport(SPI_PORT)
#define SPIDMARX SPIdreqRX(SPI_PORT)
#define SPIDMATX SPIdreqTX(SPI_PORT)
static uint32_t spi_freq = 400000;
typedef struct
{
int channel;
dma_channel_config config;
io_rw_32 *port;
uint8_t dummy;
} spi_dma_t;
static spi_dma_t dma_tx;
static spi_dma_t dma_rx;
void spi_start (void)
{
static const periph_pin_t sck = {
.function = Output_SPICLK,
.group = PinGroup_SPI,
.pin = SPI_SCK_PIN,
.mode = { .mask = PINMODE_OUTPUT }
};
static const periph_pin_t sdo = {
.function = Input_MISO,
.group = PinGroup_SPI,
.pin = SPI_MISO_PIN,
.mode = { .mask = PINMODE_NONE }
};
static const periph_pin_t sdi = {
.function = Output_MOSI,
.group = PinGroup_SPI,
.pin = SPI_MOSI_PIN,
.mode = { .mask = PINMODE_NONE }
};
static bool init = false;
if(!init) {
spi_init(SPIPORT, spi_freq);
gpio_set_function(SPI_SCK_PIN, GPIO_FUNC_SPI);
gpio_set_function(SPI_MISO_PIN, GPIO_FUNC_SPI);
gpio_set_function(SPI_MOSI_PIN, GPIO_FUNC_SPI);
hal.periph_port.register_pin(&sck);
hal.periph_port.register_pin(&sdi);
hal.periph_port.register_pin(&sdo);
dma_rx.port = &spi_get_hw(SPIPORT)->dr;
dma_rx.channel = dma_claim_unused_channel(true);
dma_rx.config = dma_channel_get_default_config(dma_rx.channel);
channel_config_set_transfer_data_size(&dma_rx.config, DMA_SIZE_8);
channel_config_set_dreq(&dma_rx.config, SPIDMARX);
channel_config_set_read_increment(&dma_rx.config, false);
dma_tx.dummy = 0xFF;
dma_tx.port = &spi_get_hw(SPIPORT)->dr;
dma_tx.channel = dma_claim_unused_channel(true);
dma_tx.config = dma_channel_get_default_config(dma_tx.channel);
channel_config_set_transfer_data_size(&dma_tx.config, DMA_SIZE_8);
channel_config_set_dreq(&dma_tx.config, SPIDMATX);
channel_config_set_write_increment(&dma_tx.config, false);
init = true;
}
}
uint32_t spi_set_speed (uint32_t freq_hz)
{
uint32_t cur = spi_freq;
if(freq_hz != 0)
spi_set_baudrate(SPIPORT, spi_freq = freq_hz);
return cur;
}
uint8_t spi_get_byte (void)
{
uint8_t byte;
spi_read_blocking(SPIPORT, 0xFF, &byte, 1);
return byte;
}
void spi_put_byte (uint8_t byte)
{
spi_write_blocking(SPIPORT, &byte, 1);
}
void spi_write (uint8_t *data, uint16_t len)
{
if(len <= 2)
spi_write_blocking(SPIPORT, data, len);
else {
channel_config_set_read_increment(&dma_tx.config, true);
dma_channel_configure(dma_tx.channel, &dma_tx.config, dma_tx.port, data, len, false);
channel_config_set_write_increment(&dma_rx.config, false);
dma_channel_configure(dma_rx.channel, &dma_rx.config, &dma_rx.dummy, dma_rx.port, len, false);
dma_start_channel_mask((1 << dma_tx.channel) | (1 << dma_rx.channel));
dma_channel_wait_for_finish_blocking(dma_rx.channel);
}
}
void spi_read (uint8_t *data, uint16_t len)
{
if(len <= 2) {
spi_read_blocking(SPIPORT, 0xFF, data, len);
} else {
channel_config_set_read_increment(&dma_tx.config, false);
dma_channel_configure(dma_tx.channel, &dma_tx.config, dma_tx.port, &dma_tx.dummy, len, false);
channel_config_set_write_increment(&dma_rx.config, true);
dma_channel_configure(dma_rx.channel, &dma_rx.config, data, dma_rx.port, len, false);
dma_start_channel_mask((1 << dma_tx.channel) | (1 << dma_rx.channel));
dma_channel_wait_for_finish_blocking(dma_rx.channel);
}
}
#endif // SPI_ENABLE