-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathspi.cc
268 lines (216 loc) · 7.84 KB
/
spi.cc
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include "spi.h"
#include <cstring>
#include "hardware/gpio.h"
#include "hardware/irq.h"
#include "hardware/spi.h"
#include "utils.h"
#define GPIO_DEBUG_PIN_0 3
#define GPIO_DEBUG_PIN_1 4
#define GPIO_DEBUG_PIN_2 5
namespace {
constexpr size_t kTXTicksToWait = 1;
extern "C" {
static IBPIRQData __not_in_flash("irq") irq_data[2];
void SPIDeviceTask(void* parameter) {
reinterpret_cast<IBPSPIDevice*>(parameter)->DeviceTask();
}
void SPIHostTask(void* parameter) {
reinterpret_cast<IBPSPIHost*>(parameter)->HostTask();
}
void __no_inline_not_in_flash_func(SPIDeviceRXIRQ)(IBPIRQData* irq_data) {
gpio_put(GPIO_DEBUG_PIN_0, 1);
// Local copy to reduce pointer dereference.
IBPIRQData irq_data_local = *irq_data;
spi_get_hw(irq_data_local.spi_port)->imsc &= 0b1011; // Mask RX IRQ
while (spi_is_readable(irq_data_local.spi_port)) {
const uint8_t read = (uint8_t)spi_get_hw(irq_data_local.spi_port)->dr;
if (irq_data_local.rx_buf_idx == 0) {
irq_data_local.rx_packet_size = GetTransactionTotalSize(read);
}
(*irq_data_local.rx_buffer)[irq_data_local.rx_buf_idx++] = read;
if (irq_data_local.rx_packet_size < 0) {
*irq_data = irq_data_local;
xSemaphoreGiveFromISR(irq_data_local.rx_handle,
/*pxHigherPriorityTaskWoken=*/NULL);
return;
}
if (irq_data_local.rx_buf_idx >= irq_data_local.rx_packet_size) {
while (spi_is_writable(irq_data_local.spi_port) &&
irq_data_local.tx_buf_idx < irq_data_local.tx_packet_size) {
spi_get_hw(irq_data_local.spi_port)->dr =
(*irq_data_local.tx_buffer)[irq_data_local.tx_buf_idx++];
}
*irq_data = irq_data_local;
spi_get_hw(irq_data_local.spi_port)->imsc |= 0b1000; // Enable TX IRQ
xSemaphoreGiveFromISR(irq_data_local.rx_handle,
/*pxHigherPriorityTaskWoken=*/NULL);
return;
}
}
*irq_data = irq_data_local;
spi_get_hw(irq_data_local.spi_port)->imsc |= 0b0100; // Reenable IRQ
}
void SPIDeviceTXIRQ(IBPIRQData* irq_data) {
gpio_put(GPIO_DEBUG_PIN_1, 1);
IBPIRQData irq_data_local = *irq_data;
spi_get_hw(irq_data_local.spi_port)->imsc &= 0b0111; // Mask IRQ
while (spi_is_writable(irq_data_local.spi_port) &&
irq_data_local.tx_buf_idx < irq_data_local.tx_packet_size) {
spi_get_hw(irq_data_local.spi_port)->dr =
(*irq_data_local.tx_buffer)[irq_data_local.tx_buf_idx++];
}
*irq_data = irq_data_local;
if (irq_data_local.tx_buf_idx >= irq_data_local.tx_packet_size) {
// Only wake up the task when the queue is empty.
xSemaphoreGiveFromISR(irq_data_local.tx_handle,
/*pxHigherPriorityTaskWoken=*/NULL);
// Leave TX IRQ disabled.
} else {
spi_get_hw(irq_data_local.spi_port)->imsc |= 0b1000; // Reenable IRQ
}
}
void SPI0DeviceIRQ() {
if (spi_get_hw(spi0)->mis & 0b0100) {
SPIDeviceRXIRQ(&irq_data[0]);
} else if (spi_get_hw(spi0)->mis & 0b1000) {
SPIDeviceTXIRQ(&irq_data[0]);
}
}
void SPI0HostIRQ() {}
void SPI1DeviceIRQ() {
if (spi_get_hw(spi1)->mis & 0b0100) {
SPIDeviceRXIRQ(&irq_data[1]);
} else if (spi_get_hw(spi1)->mis & 0b1000) {
SPIDeviceTXIRQ(&irq_data[1]);
}
}
void SPI1HostIRQ() {}
}
} // namespace
void IBPIRQData::Clear() {
rx_buf_idx = 0;
rx_packet_size = -1;
tx_buf_idx = 0;
tx_packet_size = 0;
xSemaphoreGive(rx_handle);
xSemaphoreGive(tx_handle);
}
IBPSPIBase::IBPSPIBase(IBPSPIArgs args)
: task_handle_(NULL),
spi_port_(args.spi_port),
baud_rate_(args.baud_rate),
rx_pin_(args.rx_pin),
tx_pin_(args.tx_pin),
cs_pin_(args.cs_pin),
sck_pin_(args.sck_pin),
irq_data_(NULL) {}
bool IBPSPIBase::TXEmpty() {
return (spi_get_const_hw(spi_port_)->sr & SPI_SSPSR_TFE_BITS);
}
void IBPSPIBase::InitSPI(bool slave) {
spi_init(spi_port_, baud_rate_);
spi_set_slave(spi_port_, slave);
gpio_set_function(rx_pin_, GPIO_FUNC_SPI);
gpio_set_function(tx_pin_, GPIO_FUNC_SPI);
gpio_set_function(cs_pin_, GPIO_FUNC_SPI);
gpio_set_function(sck_pin_, GPIO_FUNC_SPI);
}
void IBPSPIBase::InitIRQData(irq_handler_t irq_handler) {
const uint8_t spi_idx = spi_get_index(spi_port_);
irq_data_ = &irq_data[spi_idx];
irq_data_->rx_buffer =
std::make_shared<std::array<uint8_t, IBP_MAX_PACKET_LEN>>();
irq_data_->tx_buffer =
std::make_shared<std::array<uint8_t, IBP_MAX_PACKET_LEN>>();
irq_data_->spi_port = spi_port_;
irq_data_->rx_handle = xSemaphoreCreateBinary();
irq_data_->tx_handle = xSemaphoreCreateBinary();
irq_data_->Clear();
const int irq_nums[2] = {SPI0_IRQ, SPI1_IRQ};
irq_set_exclusive_handler(irq_nums[spi_idx], irq_handler);
}
IBPSPIDevice::IBPSPIDevice(IBPSPIArgs args) : IBPSPIBase(args) {}
Status IBPSPIDevice::IBPInitialize() {
InitSPI(/*slave=*/true);
spi_get_hw(spi_port_)->imsc = 0; // Disable the interrupt for now.
if (spi_port_ == spi0) {
irq_set_enabled(SPI0_IRQ, true);
} else {
irq_set_enabled(SPI1_IRQ, true);
}
BaseType_t status =
xTaskCreate(&SPIDeviceTask, "spi_device_task", CONFIG_TASK_STACK_SIZE,
this, CONFIG_TASK_PRIORITY, &task_handle_);
if (status != pdPASS || task_handle_ == NULL) {
return ERROR;
}
const irq_handler_t irq_handlers[2] = {SPI0DeviceIRQ, SPI1DeviceIRQ};
InitIRQData(irq_handlers[spi_get_index(spi_port_)]);
gpio_init(GPIO_DEBUG_PIN_0);
gpio_set_dir(GPIO_DEBUG_PIN_0, GPIO_OUT);
gpio_init(GPIO_DEBUG_PIN_1);
gpio_set_dir(GPIO_DEBUG_PIN_1, GPIO_OUT);
gpio_init(GPIO_DEBUG_PIN_2);
gpio_set_dir(GPIO_DEBUG_PIN_2, GPIO_OUT);
return OK;
}
void IBPSPIDevice::DeviceTask() {
if (irq_data_ == NULL) {
LOG_ERROR("irq_data_ is NULL. Terminate the IBP device task");
return;
}
while (true) {
irq_data_->Clear();
// Get the new out-bound packet.
const std::string out_packet = GetOutPacket();
if (out_packet.size() > IBP_MAX_PACKET_LEN || out_packet.empty()) {
LOG_ERROR("Invalid out bound packet");
vTaskDelay(pdMS_TO_TICKS(IBP_INVALID_PACKET_DELAY_MS));
continue;
}
std::memcpy(irq_data_->tx_buffer->data(), out_packet.data(),
out_packet.size());
irq_data_->tx_packet_size = out_packet.size();
gpio_put(GPIO_DEBUG_PIN_0, 0);
gpio_put(GPIO_DEBUG_PIN_1, 0);
gpio_put(GPIO_DEBUG_PIN_2, 0);
// Clear RX FIFO
while (spi_is_readable(spi_port_)) {
(void)spi_get_hw(spi_port_)->dr;
}
// If there are still data stuck in TX FIFO, reset the SPI controller. Seems
// like the only way to clear up the TX FIFO.
if (!TXEmpty()) {
gpio_put(GPIO_DEBUG_PIN_1, 1);
InitSPI(/*slave=*/true);
gpio_put(GPIO_DEBUG_PIN_1, 0);
}
xSemaphoreTake(irq_data->rx_handle, /*xTicksToWait=*/0);
xSemaphoreTake(irq_data->tx_handle, /*xTicksToWait=*/0);
spi_get_hw(spi_port_)->imsc &= 0b0111; // Mask TX IRQ
spi_get_hw(spi_port_)->imsc |= 0b0100; // Enable RX IRQ
xSemaphoreTake(irq_data->rx_handle, portMAX_DELAY);
gpio_put(GPIO_DEBUG_PIN_0, 0);
// At this point RX should be disabled. Remask just to be sure.
spi_get_hw(spi_port_)->imsc &= 0b1011;
if (irq_data_->rx_packet_size < 0) {
LOG_ERROR("Invalid in bound packet");
vTaskDelay(pdMS_TO_TICKS(IBP_INVALID_PACKET_DELAY_MS));
continue;
}
SetInPacket(
std::string(irq_data->rx_buffer->data(),
irq_data->rx_buffer->data() + irq_data_->rx_packet_size));
const bool tx_done = xSemaphoreTake(irq_data->tx_handle, kTXTicksToWait);
gpio_put(GPIO_DEBUG_PIN_1, 0);
// At this point both RX and TX IRQs should be masked. Remask just to be
// sure.
spi_get_hw(spi_port_)->imsc = 0;
if (tx_done && !TXEmpty()) {
// If the semaphore is successfully acquired, then we know that TX IRQ has
// pushed all the bytes. If the TX FIFO is still not empty then let's wait
// a bit for it to clear up.
vTaskDelay(kTXTicksToWait);
}
}
}