| 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 |
#include <stdbool.h> |
| 44 |
#include <stdint.h> |
| 45 |
#include <stdio.h> |
| 46 |
#include <stdlib.h> |
| 47 |
#include <sleep.h> |
| 48 |
#include <xil_io.h> |
| 49 |
#include <xscugic.h> |
| 50 |
#include <xparameters.h> |
| 51 |
#include "spi_engine.h" |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
#define SPI_ENGINE_INT_ID 88 |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
spi_engine spi; |
| 62 |
XScuGic_Config *gic_config; |
| 63 |
XScuGic gic; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
int32_t spi_engine_read(uint32_t reg_addr, |
| 69 |
uint32_t *reg_data) |
| 70 |
{ |
| 71 |
*reg_data = Xil_In32((spi.baseaddr + reg_addr)); |
| 72 |
|
| 73 |
return 0; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
int32_t spi_engine_write(uint32_t reg_addr, |
| 80 |
uint32_t reg_data) |
| 81 |
{ |
| 82 |
Xil_Out32((spi.baseaddr + reg_addr), reg_data); |
| 83 |
|
| 84 |
return 0; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
void spi_engine_program_add_cmd(spi_engine_program *p, |
| 91 |
uint8_t dry, |
| 92 |
uint16_t cmd) |
| 93 |
{ |
| 94 |
if (!dry) |
| 95 |
p->instructions[p->length] = cmd; |
| 96 |
p->length++; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
int32_t spi_engine_compile_message(spi_message *msg, |
| 103 |
uint8_t dry, |
| 104 |
spi_engine_program *p) |
| 105 |
{ |
| 106 |
uint32_t clk_div = 0; |
| 107 |
|
| 108 |
spi_engine_program_add_cmd(p, dry, |
| 109 |
SPI_ENGINE_CMD_WRITE(SPI_ENGINE_CMD_REG_CONFIG, msg->spi_config)); |
| 110 |
|
| 111 |
clk_div = (spi.spi_ref_clk / msg->spi_clk) - 1; |
| 112 |
spi_engine_program_add_cmd(p, dry, |
| 113 |
SPI_ENGINE_CMD_WRITE(SPI_ENGINE_CMD_REG_CLK_DIV, clk_div)); |
| 114 |
|
| 115 |
if (msg->cs_change) { |
| 116 |
spi_engine_program_add_cmd(p, dry, |
| 117 |
SPI_ENGINE_CMD_ASSERT(1, (0xff ^ BIT(msg->chip_select)))); |
| 118 |
} |
| 119 |
|
| 120 |
spi_engine_program_add_cmd(p, dry, |
| 121 |
SPI_ENGINE_CMD_TRANSFER(msg->tx_length ? 1 : 0, |
| 122 |
msg->rx_length ? 1 : 0, |
| 123 |
max(msg->tx_length, msg->rx_length) - 1)); |
| 124 |
|
| 125 |
if (msg->cs_change) { |
| 126 |
spi_engine_program_add_cmd(p, dry, |
| 127 |
SPI_ENGINE_CMD_ASSERT(1, 0xff)); |
| 128 |
} |
| 129 |
|
| 130 |
return 0; |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
int32_t spi_engine_write_cmd_fifo(void) |
| 137 |
{ |
| 138 |
uint32_t n, m, i; |
| 139 |
const uint16_t *buf; |
| 140 |
|
| 141 |
spi_engine_read(SPI_ENGINE_REG_CMD_FIFO_ROOM, &n); |
| 142 |
|
| 143 |
while (n && spi.cmd_length) { |
| 144 |
m = min(n, spi.cmd_length); |
| 145 |
buf = spi.cmd_buf; |
| 146 |
for (i = 0; i < m; i++) |
| 147 |
spi_engine_write(SPI_ENGINE_REG_CMD_FIFO, buf[i]); |
| 148 |
spi.cmd_length -= m; |
| 149 |
n -= m; |
| 150 |
} |
| 151 |
|
| 152 |
return spi.cmd_length != 0; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
int32_t spi_engine_write_tx_fifo(void) |
| 159 |
{ |
| 160 |
uint32_t n, m, i; |
| 161 |
const uint8_t *buf; |
| 162 |
|
| 163 |
spi_engine_read(SPI_ENGINE_REG_SDO_FIFO_ROOM, &n); |
| 164 |
|
| 165 |
while (n && spi.msg->tx_length) { |
| 166 |
m = min(n, spi.msg->tx_length); |
| 167 |
buf = spi.msg->tx_buf; |
| 168 |
for (i = 0; i < m; i++) |
| 169 |
spi_engine_write(SPI_ENGINE_REG_SDO_DATA_FIFO, buf[i]); |
| 170 |
spi.msg->tx_length -= m; |
| 171 |
n -= m; |
| 172 |
} |
| 173 |
|
| 174 |
return spi.msg->tx_length != 0; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
int32_t spi_engine_read_rx_fifo() |
| 181 |
{ |
| 182 |
uint32_t n, m, i; |
| 183 |
uint32_t data; |
| 184 |
uint8_t *buf; |
| 185 |
|
| 186 |
spi_engine_read(SPI_ENGINE_REG_SDI_FIFO_LEVEL, &n); |
| 187 |
|
| 188 |
while (n && spi.msg->rx_length) { |
| 189 |
m = min(n, spi.msg->rx_length); |
| 190 |
buf = spi.msg->rx_buf; |
| 191 |
for (i = 0; i < m; i++) { |
| 192 |
spi_engine_read(SPI_ENGINE_REG_SDI_DATA_FIFO, &data); |
| 193 |
buf[i] = (uint8_t) data; |
| 194 |
} |
| 195 |
spi.msg->rx_buf += m; |
| 196 |
spi.msg->rx_length -= m; |
| 197 |
n -= m; |
| 198 |
} |
| 199 |
|
| 200 |
return spi.msg->rx_length != 0; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
int32_t spi_engine_transfer_one_message(spi_message *msg) |
| 207 |
{ |
| 208 |
spi_engine_program p_dry, *p; |
| 209 |
size_t size; |
| 210 |
|
| 211 |
p_dry.length = 0; |
| 212 |
spi_engine_compile_message(msg, true, &p_dry); |
| 213 |
|
| 214 |
size = sizeof(*p->instructions) * (p_dry.length + 1); |
| 215 |
p = malloc(sizeof(*p) + size); |
| 216 |
if (!p) |
| 217 |
return -1; |
| 218 |
p->length = 0; |
| 219 |
spi_engine_compile_message(msg, false, p); |
| 220 |
|
| 221 |
spi.sync_id = (spi.sync_id + 1) & 0xff; |
| 222 |
spi_engine_program_add_cmd(p, false, |
| 223 |
SPI_ENGINE_CMD_SYNC(spi.sync_id)); |
| 224 |
|
| 225 |
spi.msg = msg; |
| 226 |
spi.p = p; |
| 227 |
|
| 228 |
spi.cmd_buf = p->instructions; |
| 229 |
spi.cmd_length = p->length; |
| 230 |
if (spi_engine_write_cmd_fifo()) |
| 231 |
spi.int_enable |= SPI_ENGINE_INT_CMD_ALMOST_EMPTY; |
| 232 |
|
| 233 |
if (spi_engine_write_tx_fifo()) |
| 234 |
spi.int_enable |= SPI_ENGINE_INT_SDO_ALMOST_EMPTY; |
| 235 |
|
| 236 |
if (spi.msg->rx_length != 0) |
| 237 |
spi.int_enable |= SPI_ENGINE_INT_SDI_ALMOST_FULL; |
| 238 |
|
| 239 |
spi.int_enable |= SPI_ENGINE_INT_SYNC; |
| 240 |
|
| 241 |
spi_engine_write(SPI_ENGINE_REG_INT_ENABLE, spi.int_enable); |
| 242 |
|
| 243 |
return 0; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
void spi_engine_isr(void *instance) |
| 250 |
{ |
| 251 |
uint32_t pending; |
| 252 |
uint32_t disable_int = 0; |
| 253 |
|
| 254 |
spi_engine_read(SPI_ENGINE_REG_INT_PENDING, &pending); |
| 255 |
|
| 256 |
if (pending & SPI_ENGINE_INT_SYNC) { |
| 257 |
spi_engine_write(SPI_ENGINE_REG_INT_PENDING, SPI_ENGINE_INT_SYNC); |
| 258 |
spi_engine_read(SPI_ENGINE_REG_SYNC_ID, &spi.completed_id); |
| 259 |
} |
| 260 |
|
| 261 |
if (pending & SPI_ENGINE_INT_CMD_ALMOST_EMPTY) { |
| 262 |
if (!spi_engine_write_cmd_fifo()) |
| 263 |
disable_int |= SPI_ENGINE_INT_CMD_ALMOST_EMPTY; |
| 264 |
} |
| 265 |
|
| 266 |
if (pending & SPI_ENGINE_INT_SDO_ALMOST_EMPTY) { |
| 267 |
if (!spi_engine_write_tx_fifo()) |
| 268 |
disable_int |= SPI_ENGINE_INT_SDO_ALMOST_EMPTY; |
| 269 |
} |
| 270 |
|
| 271 |
if (pending & (SPI_ENGINE_INT_SDI_ALMOST_FULL | SPI_ENGINE_INT_SYNC)) { |
| 272 |
if (!spi_engine_read_rx_fifo()) |
| 273 |
disable_int |= SPI_ENGINE_INT_SDI_ALMOST_FULL; |
| 274 |
} |
| 275 |
|
| 276 |
if (pending & SPI_ENGINE_INT_SYNC) { |
| 277 |
disable_int |= SPI_ENGINE_INT_SYNC; |
| 278 |
} |
| 279 |
|
| 280 |
if (disable_int) { |
| 281 |
spi.int_enable &= ~disable_int; |
| 282 |
spi_engine_write(SPI_ENGINE_REG_INT_ENABLE, spi.int_enable); |
| 283 |
} |
| 284 |
|
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
void spi_engine_offload_enable(uint8_t enable) |
| 291 |
{ |
| 292 |
uint32_t reg; |
| 293 |
|
| 294 |
spi_engine_read(SPI_ENGINE_REG_OFFLOAD_CTRL(0), ®); |
| 295 |
if (enable) |
| 296 |
reg |= SPI_ENGINE_OFFLOAD_CTRL_ENABLE; |
| 297 |
else |
| 298 |
reg &= ~SPI_ENGINE_OFFLOAD_CTRL_ENABLE; |
| 299 |
spi_engine_write(SPI_ENGINE_REG_OFFLOAD_CTRL(0), reg); |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
int32_t spi_engine_offload_load_msg(spi_message *msg) |
| 306 |
{ |
| 307 |
spi_engine_program p_dry, *p; |
| 308 |
size_t size; |
| 309 |
uint32_t i; |
| 310 |
|
| 311 |
p_dry.length = 0; |
| 312 |
spi_engine_compile_message(msg, true, &p_dry); |
| 313 |
|
| 314 |
size = sizeof(*p->instructions) * (p_dry.length + 2); |
| 315 |
p = malloc(sizeof(*p) + size); |
| 316 |
if (!p) |
| 317 |
return -1; |
| 318 |
p->length = 0; |
| 319 |
spi_engine_compile_message(msg, false, p); |
| 320 |
spi_engine_program_add_cmd(p, false, SPI_ENGINE_CMD_SLEEP(10)); |
| 321 |
spi_engine_program_add_cmd(p, false, SPI_ENGINE_CMD_SYNC(0)); |
| 322 |
|
| 323 |
spi_engine_write(SPI_ENGINE_REG_OFFLOAD_RESET(0), 1); |
| 324 |
for (i = 0; i < msg->tx_length; i++) |
| 325 |
spi_engine_write(SPI_ENGINE_REG_OFFLOAD_SDO_MEM(0), msg->tx_buf[i]); |
| 326 |
|
| 327 |
for (i = 0; i < p->length; i++) |
| 328 |
spi_engine_write(SPI_ENGINE_REG_OFFLOAD_CMD_MEM(0), p->instructions[i]); |
| 329 |
|
| 330 |
return 0; |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
int32_t spi_engine_setup(uint32_t baseaddr) |
| 337 |
{ |
| 338 |
uint32_t version; |
| 339 |
int32_t status; |
| 340 |
|
| 341 |
spi.baseaddr = baseaddr; |
| 342 |
spi.spi_ref_clk = 100000000; |
| 343 |
spi.sync_id = 0; |
| 344 |
spi.int_enable = 0; |
| 345 |
|
| 346 |
spi_engine_read(SPI_ENGINE_REG_VERSION, &version); |
| 347 |
if (SPI_ENGINE_VERSION_MAJOR(version) != 1) { |
| 348 |
printf("Unsupported peripheral version %u.%u.%c\n", |
| 349 |
(unsigned int)SPI_ENGINE_VERSION_MAJOR(version), |
| 350 |
(unsigned int)SPI_ENGINE_VERSION_MINOR(version), |
| 351 |
(int)SPI_ENGINE_VERSION_PATCH(version)); |
| 352 |
return -1; |
| 353 |
} |
| 354 |
|
| 355 |
spi_engine_write(SPI_ENGINE_REG_RESET, 0x01); |
| 356 |
usleep(100000); |
| 357 |
spi_engine_write(SPI_ENGINE_REG_RESET, 0x00); |
| 358 |
spi_engine_write(SPI_ENGINE_REG_INT_PENDING, 0xff); |
| 359 |
spi_engine_write(SPI_ENGINE_REG_INT_ENABLE, 0x00); |
| 360 |
|
| 361 |
gic_config = XScuGic_LookupConfig(XPAR_PS7_SCUGIC_0_DEVICE_ID); |
| 362 |
if(gic_config == NULL) |
| 363 |
printf("Error\n"); |
| 364 |
|
| 365 |
status = XScuGic_CfgInitialize(&gic, gic_config, |
| 366 |
gic_config->CpuBaseAddress); |
| 367 |
if(status) |
| 368 |
printf("Error\n"); |
| 369 |
|
| 370 |
XScuGic_SetPriorityTriggerType(&gic, SPI_ENGINE_INT_ID, 0x0, 0x3); |
| 371 |
status = XScuGic_Connect(&gic, SPI_ENGINE_INT_ID, |
| 372 |
(Xil_ExceptionHandler)spi_engine_isr, NULL); |
| 373 |
if(status) |
| 374 |
printf("Error\n"); |
| 375 |
|
| 376 |
XScuGic_Enable(&gic, SPI_ENGINE_INT_ID); |
| 377 |
|
| 378 |
Xil_ExceptionInit(); |
| 379 |
|
| 380 |
Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT, |
| 381 |
(Xil_ExceptionHandler)XScuGic_InterruptHandler, (void *)&gic); |
| 382 |
Xil_ExceptionEnable(); |
| 383 |
|
| 384 |
printf("SPI Engine version %u.%u.%c successfully initialized\n", |
| 385 |
(unsigned int)SPI_ENGINE_VERSION_MAJOR(version), |
| 386 |
(unsigned int)SPI_ENGINE_VERSION_MINOR(version), |
| 387 |
(int)SPI_ENGINE_VERSION_PATCH(version)); |
| 388 |
|
| 389 |
return 0; |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
int32_t spi_engine_write_and_read(uint8_t ss, |
| 396 |
uint8_t *data, |
| 397 |
uint8_t bytes_number) |
| 398 |
{ |
| 399 |
uint32_t expected_id = 0; |
| 400 |
uint32_t i; |
| 401 |
spi_message message; |
| 402 |
uint8_t tx[8]; |
| 403 |
uint8_t rx[8]; |
| 404 |
|
| 405 |
for (i = 0; i < bytes_number; i++) |
| 406 |
tx[i] = data[i]; |
| 407 |
|
| 408 |
message.chip_select = ss; |
| 409 |
message.cs_change = true; |
| 410 |
message.spi_clk = 1000000; |
| 411 |
message.spi_config = SPI_ENGINE_CONFIG_CPHA; |
| 412 |
message.tx_buf = tx; |
| 413 |
message.rx_buf = rx; |
| 414 |
|
| 415 |
expected_id = spi.sync_id + 1; |
| 416 |
spi.completed_id = 0xFFFFFFFF; |
| 417 |
|
| 418 |
message.tx_length = bytes_number; |
| 419 |
message.rx_length = bytes_number; |
| 420 |
message.rx_buf = rx; |
| 421 |
spi_engine_transfer_one_message(&message); |
| 422 |
|
| 423 |
while (spi.completed_id != expected_id); |
| 424 |
|
| 425 |
for (i = 0; i < bytes_number; i++) |
| 426 |
data[i] = rx[i]; |
| 427 |
|
| 428 |
return 0; |
| 429 |
} |