-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathice40.c
279 lines (234 loc) · 8.17 KB
/
ice40.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
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
269
270
271
272
273
274
275
276
277
278
279
#include <sdkconfig.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <esp_system.h>
#include <soc/gpio_reg.h>
#include <soc/gpio_sig_map.h>
#include <soc/gpio_struct.h>
#include <soc/spi_reg.h>
#include <esp_err.h>
#include <esp_log.h>
#include <driver/gpio.h>
#include <driver/spi_master.h>
#include <freertos/FreeRTOS.h>
#include <freertos/semphr.h>
#include <freertos/task.h>
#include <driver/gpio.h>
#include "include/ice40.h"
static const char *TAG = "ice40";
// SPI communication
static void IRAM_ATTR ice40_pre_transaction_cb(spi_transaction_t* transaction) {
ICE40* device = (ICE40*) transaction->user;
if (device->cs_enabled) {
gpio_set_level(device->pin_cs, false);
}
}
static void IRAM_ATTR ice40_post_transaction_cb(spi_transaction_t* transaction) {
ICE40* device = (ICE40*) transaction->user;
if (device->cs_enabled) {
gpio_set_level(device->pin_cs, (transaction->flags & SPI_TRANS_CS_KEEP_ACTIVE) ? false : true);
}
}
esp_err_t ice40_send_turbo(ICE40* device, const uint8_t* data, uint32_t length) {
if (device->_spi_device_turbo == NULL) return ESP_FAIL;
spi_transaction_t transaction = {
.user = (void*) device,
.length = length * 8,
.tx_buffer = data,
.rx_buffer = NULL
};
return spi_device_transmit(device->_spi_device_turbo, &transaction);
}
esp_err_t ice40_send(ICE40* device, const uint8_t* data, uint32_t length) {
if (device->_spi_device_hd == NULL) return ESP_FAIL;
spi_transaction_t transaction = {
.user = (void*) device,
.length = length * 8,
.tx_buffer = data,
.rx_buffer = NULL
};
return spi_device_transmit(device->_spi_device_hd, &transaction);
}
esp_err_t ice40_receive(ICE40* device, uint8_t* data, uint32_t length) {
if (device->_spi_device_hd == NULL) return ESP_FAIL;
spi_transaction_t transaction = {
.user = (void*) device,
.length = 0,
.rxlength = length * 8,
.tx_buffer = NULL,
.rx_buffer = data
};
return spi_device_transmit(device->_spi_device_hd, &transaction);
}
esp_err_t ice40_transaction(ICE40* device, uint8_t* data_out, uint32_t out_length, uint8_t* data_in, uint32_t in_length) {
if (device->_spi_device_fd == NULL) return ESP_FAIL;
spi_transaction_t transaction = {
.user = (void*) device,
.length = out_length * 8,
.rxlength = in_length * 8,
.tx_buffer = data_out,
.rx_buffer = data_in
};
return spi_device_transmit(device->_spi_device_fd, &transaction);
}
// Device state management
esp_err_t ice40_enable(ICE40* device) {
esp_err_t res = ESP_FAIL;
if (device->pin_reset >= 0) {
res = gpio_set_level(device->pin_reset, true);
if (res != ESP_OK) return res;
}
if (device->set_reset != NULL) {
res = device->set_reset(true);
if (res != ESP_OK) {
ESP_LOGE(TAG, "Failed to set reset pin (true)");
return res;
}
}
return res;
}
esp_err_t ice40_disable(ICE40* device) {
esp_err_t res;
device->cs_enabled = false;
if (device->pin_reset >= 0) {
res = gpio_set_level(device->pin_reset, false);
if (res != ESP_OK) return res;
}
if (device->set_reset != NULL) {
res = device->set_reset(false);
if (res != ESP_OK) {
ESP_LOGE(TAG, "Failed to set reset pin (false)");
return res;
}
}
return gpio_set_level(device->pin_cs, true);
}
esp_err_t ice40_get_done(ICE40* device, bool* done) {
if (device->pin_done >= 0) {
*done = gpio_get_level(device->pin_done);
return ESP_OK;
}
if (device->get_done != NULL) {
return device->get_done(done);
}
return ESP_FAIL;
}
esp_err_t ice40_load_bitstream(ICE40* device, const uint8_t* bitstream, uint32_t length) {
const uint8_t dummy[20] = {0};
bool done;
// Put ICE40 in reset state
esp_err_t res = ice40_disable(device);
if (res != ESP_OK) return res;
// Set CS pin low
res = gpio_set_level(device->pin_cs, false);
if (res != ESP_OK) return res;
// Put ICE40 in SPI slave download state
res = ice40_enable(device);
if (res != ESP_OK) return res;
// Wait 2 ms
vTaskDelay(2 / portTICK_PERIOD_MS);
// Check iCE40 is reset
res = ice40_get_done(device, &done);
if (res != ESP_OK) return res;
if (done) {
ESP_LOGE(TAG, "FPGA SIGNALS DONE AFTER RESET, HARDWARE ERROR");
return ESP_FAIL;
}
// Set CS pin high
res = gpio_set_level(device->pin_cs, true);
if (res != ESP_OK) return res;
// Send 8 dummy clocks
ice40_send(device, dummy, 1);
// Set CS pin low
res = gpio_set_level(device->pin_cs, false);
if (res != ESP_OK) return res;
// Send the whole bitstream
uint32_t position = 0;
while (position < length) {
uint32_t remaining = length - position;
uint32_t transferSize = (remaining < device->spi_max_transfer_size) ? remaining : device->spi_max_transfer_size;
ice40_send(device, bitstream + position, transferSize);
position += transferSize;
}
// Set CS pin high
res = gpio_set_level(device->pin_cs, true);
if (res != ESP_OK) return res;
// Send > 149 dummy cycles
ice40_send(device, dummy, sizeof(dummy));
// Check configuration worked
res = ice40_get_done(device, &done);
if (res != ESP_OK) return res;
// Enable the CS pin (to allow normal SPI transfers)
device->cs_enabled = true;
// Done !
return done ? ESP_OK : ESP_FAIL;
}
esp_err_t ice40_init(ICE40* device) {
esp_err_t res;
if (device->pin_reset >= 0) {
res = gpio_set_direction(device->pin_reset, GPIO_MODE_OUTPUT);
if (res != ESP_OK) return res;
}
if (device->pin_done >= 0) {
res = gpio_set_direction(device->pin_done, GPIO_MODE_INPUT);
if (res != ESP_OK) return res;
}
if (device->pin_int >= 0) {
res = gpio_set_direction(device->pin_int, GPIO_MODE_INPUT);
if (res != ESP_OK) return res;
}
if (device->pin_cs < 0) return ESP_FAIL;
res = gpio_set_direction(device->pin_cs, GPIO_MODE_OUTPUT);
if (res != ESP_OK) return res;
res = gpio_set_level(device->pin_cs, true);
if (res != ESP_OK) return res;
device->cs_enabled = false;
spi_device_interface_config_t device_config_fd = {
.clock_speed_hz = device->spi_speed_full_duplex,
.input_delay_ns = device->spi_input_delay_ns,
.mode = 0,
.spics_io_num = -1,
.queue_size = 2,
.flags = 0,
.pre_cb = ice40_pre_transaction_cb,
.post_cb = ice40_post_transaction_cb,
.command_bits = 0,
.address_bits = 0,
.dummy_bits = 0
};
res = spi_bus_add_device(device->spi_bus, &device_config_fd, &device->_spi_device_fd);
if (res != ESP_OK) return res;
spi_device_interface_config_t device_config_hd = {
.clock_speed_hz = device->spi_speed_half_duplex,
.input_delay_ns = device->spi_input_delay_ns,
.mode = 0,
.spics_io_num = -1,
.queue_size = 2,
.flags = SPI_DEVICE_HALFDUPLEX,
.pre_cb = ice40_pre_transaction_cb,
.post_cb = ice40_post_transaction_cb,
.command_bits = 0,
.address_bits = 0,
.dummy_bits = 0
};
res = spi_bus_add_device(device->spi_bus, &device_config_hd, &device->_spi_device_hd);
if (res != ESP_OK) return res;
spi_device_interface_config_t device_config_turbo = {
.clock_speed_hz = device->spi_speed_turbo,
.input_delay_ns = device->spi_input_delay_ns,
.mode = 0,
.spics_io_num = -1,
.queue_size = 2,
.flags = SPI_DEVICE_HALFDUPLEX,
.pre_cb = ice40_pre_transaction_cb,
.post_cb = ice40_post_transaction_cb,
.command_bits = 0,
.address_bits = 0,
.dummy_bits = 0
};
res = spi_bus_add_device(device->spi_bus, &device_config_turbo, &device->_spi_device_turbo);
if (res != ESP_OK) return res;
return ice40_disable(device);
}