fpgasvc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. #include "common.h"
  2. #include "config.h"
  3. #include "fpga.h"
  4. #include "esplink.h"
  5. #include "xmalloc.h"
  6. #include <driver/gpio.h>
  7. #include <driver/spi_common.h>
  8. #include <driver/spi_master.h>
  9. #define PIN_FPGA_INT 9
  10. #define PIN_FPGA_CS 10
  11. #define PIN_FPGA_IO0 11
  12. #define PIN_FPGA_CLK 12
  13. #define PIN_FPGA_IO1 13
  14. #define FPGA_SPI_HOST FSPI /* SPI2 */
  15. #define FPGA_PRIORITY 3
  16. #define FPGA_SVC_STACK 4096
  17. static spi_bus_config_t spi_bus_config = {
  18. .data0_io_num = PIN_FPGA_IO0,
  19. .data1_io_num = PIN_FPGA_IO1,
  20. .sclk_io_num = PIN_FPGA_CLK,
  21. .data2_io_num = -1,
  22. .data3_io_num = -1,
  23. .data4_io_num = -1,
  24. .data5_io_num = -1,
  25. .data6_io_num = -1,
  26. .data7_io_num = -1,
  27. .max_transfer_sz = 4096,
  28. .flags = SPICOMMON_BUSFLAG_MASTER | SPICOMMON_BUSFLAG_DUAL
  29. };
  30. #define FPGA_IOV_MAX 4
  31. static void ARDUINO_ISR_ATTR spi_callback(spi_transaction_t *);
  32. static const spi_device_interface_config_t spi_device_interface_config = {
  33. .command_bits = 8,
  34. .address_bits = 32,
  35. .dummy_bits = 0,
  36. .mode = 0,
  37. .cs_ena_pretrans = 0,
  38. .cs_ena_posttrans = 0,
  39. .clock_speed_hz = SPI_MASTER_FREQ_40M,
  40. .spics_io_num = PIN_FPGA_CS,
  41. .flags = SPI_DEVICE_HALFDUPLEX,
  42. .queue_size = FPGA_IOV_MAX,
  43. .post_cb = spi_callback
  44. };
  45. static spi_device_handle_t spi_handle;
  46. static TaskHandle_t fpga_task;
  47. static SemaphoreHandle_t spi_mutex;
  48. static EventGroupHandle_t spi_done_evgroup;
  49. static volatile bool spi_abort_all;
  50. #define NOTIFY_INDEX 0
  51. #define NOTIFY_FPGA (1 << 0)
  52. #define NOTIFY_ENABLE (1 << 1)
  53. #define NOTIFY_DISABLE (1 << 2)
  54. #if 0
  55. #define NOTIFY_SPI (1 << 3)
  56. #define NOTIFY_RINGBUF (1 << 4)
  57. #endif
  58. static uint32_t notify_poll_for(uint32_t flags)
  59. {
  60. return ulTaskNotifyValueClearIndexed(NULL, NOTIFY_INDEX, flags);
  61. }
  62. /* This supports multiple flags set */
  63. static uint32_t notify_wait_for(uint32_t flags)
  64. {
  65. uint32_t notify_value;
  66. /* Already received? Might already have been waited for... */
  67. notify_value = notify_poll_for(flags);
  68. while (!(notify_value & flags)) {
  69. xTaskNotifyWaitIndexed(NOTIFY_INDEX, 0, flags,
  70. &notify_value, portMAX_DELAY);
  71. }
  72. return notify_value;
  73. }
  74. static void ARDUINO_ISR_ATTR fpga_notify_from_isr(uint32_t flags)
  75. {
  76. BaseType_t wakeup = pdFALSE;
  77. if (xTaskNotifyIndexedFromISR(fpga_task, NOTIFY_INDEX, flags, eSetBits,
  78. &wakeup) != pdFAIL)
  79. portYIELD_FROM_ISR(wakeup);
  80. }
  81. static void fpga_notify_from_task(uint32_t flags)
  82. {
  83. xTaskNotifyIndexed(fpga_task, NOTIFY_INDEX, flags, eSetBits);
  84. }
  85. static void ARDUINO_ISR_ATTR fpga_interrupt(void)
  86. {
  87. fpga_notify_from_isr(NOTIFY_FPGA);
  88. }
  89. static void ARDUINO_ISR_ATTR spi_callback(spi_transaction_t *t)
  90. {
  91. size_t flags = (size_t)t->user;
  92. if (!flags)
  93. return;
  94. BaseType_t wakeup = pdFALSE;
  95. if (xEventGroupSetBitsFromISR(spi_done_evgroup, (size_t)t->user,
  96. &wakeup) != pdFAIL)
  97. portYIELD_FROM_ISR(wakeup);
  98. }
  99. static void fpga_service_task(void *);
  100. static EventGroupHandle_t fpga_service_evgroup;
  101. void fpga_service_enable(bool on)
  102. {
  103. uint32_t flag = on ? NOTIFY_ENABLE : NOTIFY_DISABLE;
  104. fpga_notify_from_task(flag);
  105. xEventGroupWaitBits(fpga_service_evgroup, flag, 0, pdTRUE, portMAX_DELAY);
  106. }
  107. esp_err_t fpga_service_init(void)
  108. {
  109. esp_err_t err;
  110. pinMode(PIN_FPGA_INT, INPUT_PULLUP);
  111. fpga_service_evgroup = null_check(xEventGroupCreate());
  112. spi_mutex = null_check(xSemaphoreCreateRecursiveMutex());
  113. spi_done_evgroup = null_check(xEventGroupCreate());
  114. /* The ordering here attempts to avoid race conditions... */
  115. if (xTaskCreate(fpga_service_task, "fpga_svc", FPGA_SVC_STACK, NULL,
  116. FPGA_PRIORITY, &fpga_task) != pdPASS)
  117. return ESP_FAIL;
  118. esplink_init();
  119. xEventGroupSetBits(fpga_service_evgroup, NOTIFY_DISABLE);
  120. return ESP_OK;
  121. }
  122. static bool fpga_link_enable(void)
  123. {
  124. esp_err_t err;
  125. if (spi_handle)
  126. return true; /* Already started */
  127. xEventGroupClearBits(fpga_service_evgroup, NOTIFY_DISABLE);
  128. err = spi_bus_initialize(FPGA_SPI_HOST, &spi_bus_config, SPI_DMA_CH_AUTO);
  129. if (err)
  130. goto init_fail;
  131. err = spi_bus_add_device(FPGA_SPI_HOST, &spi_device_interface_config,
  132. &spi_handle);
  133. if (err)
  134. goto free_bus_fail;
  135. /* Only device on this bus, so acquire it permanently */
  136. err = spi_device_acquire_bus(spi_handle, portMAX_DELAY);
  137. if (err)
  138. goto release_bus_fail;
  139. xEventGroupClearBits(spi_done_evgroup, -1);
  140. pinMode(PIN_FPGA_INT, INPUT_PULLUP);
  141. attachInterrupt(PIN_FPGA_INT, fpga_interrupt, FALLING);
  142. xEventGroupSetBits(fpga_service_evgroup, NOTIFY_ENABLE);
  143. xSemaphoreGiveRecursive(spi_mutex);
  144. goto done;
  145. release_bus_fail:
  146. spi_bus_remove_device(spi_handle);
  147. spi_handle = NULL;
  148. free_bus_fail:
  149. spi_bus_free(FPGA_SPI_HOST);
  150. init_fail:
  151. xEventGroupSetBits(fpga_service_evgroup, NOTIFY_DISABLE);
  152. done:
  153. return !err;
  154. }
  155. static void fpga_link_disable(void)
  156. {
  157. if (!spi_handle)
  158. return; /* Already stopped */
  159. xEventGroupClearBits(fpga_service_evgroup, NOTIFY_ENABLE);
  160. xSemaphoreTakeRecursive(spi_mutex, portMAX_DELAY);
  161. detachInterrupt(PIN_FPGA_INT);
  162. spi_device_release_bus(spi_handle);
  163. spi_bus_remove_device(spi_handle);
  164. spi_bus_free(FPGA_SPI_HOST);
  165. spi_handle = NULL;
  166. xEventGroupSetBits(fpga_service_evgroup, NOTIFY_DISABLE);
  167. }
  168. static bool fpga_online(void)
  169. {
  170. struct esplink_head head;
  171. fpga_io_read(FPGA_CMD_ACK(EL_UIRQ_READY), ESPLINK_HDR_ADDR,
  172. &head, sizeof head);
  173. if (head.magic != ESPLINK_HEAD_MAGIC || head.hlen <= 8)
  174. return false;
  175. if (unlikely(head.hlen < sizeof head)) {
  176. /* Clear any fields not provided */
  177. memset((char *)&head + head.hlen, 0, sizeof head - head.hlen);
  178. }
  179. printf("[FPGA] Ready, board = %u.%u fixes %02x fpga %u\n",
  180. head.board.major, head.board.minor,
  181. head.board.fixes, head.board.fpga);
  182. if (((size_t)head.signature_len - 1) >= 127)
  183. return false;
  184. char signature_string[head.signature_len+1];
  185. fpga_io_read(0, head.signature,
  186. signature_string, head.signature_len);
  187. signature_string[head.signature_len] = '\0';
  188. fpga_io_write(0, (char *)head.signature + 9, "GUBBAR", 6);
  189. printf("[FPGA] online, signature \"%s\"\n", signature_string);
  190. esplink_start(&head);
  191. xSemaphoreGiveRecursive(spi_mutex);
  192. return true;
  193. }
  194. static void fpga_offline(void)
  195. {
  196. xSemaphoreTakeRecursive(spi_mutex, portMAX_DELAY);
  197. esplink_start(NULL); /* Stop esplink */
  198. }
  199. esp_err_t fpga_iov(const struct fpga_iov *iov, size_t niov)
  200. {
  201. spi_transaction_ext_t trans[FPGA_IOV_MAX];
  202. size_t ntrans = 0;
  203. if (niov > FPGA_IOV_MAX)
  204. return ESP_ERR_INVALID_ARG;
  205. for (size_t i = 0; i < niov; i++) {
  206. const struct fpga_iov *iv = &iov[i];
  207. if (!iv->len && !(iv->cmd & FPGA_CMD_NULL))
  208. continue;
  209. spi_transaction_ext_t *t = &trans[ntrans];
  210. memset(t, 0, sizeof *t);
  211. t->base.flags =
  212. SPI_TRANS_MODE_DIO |
  213. SPI_TRANS_VARIABLE_DUMMY |
  214. SPI_TRANS_MULTILINE_CMD |
  215. SPI_TRANS_MULTILINE_ADDR;
  216. t->base.cmd = iv->cmd;
  217. t->base.addr = iv->iaddr;
  218. if (iv->cmd & FPGA_CMD_RD) {
  219. t->base.rxlength = iv->len << 3;
  220. t->base.rx_buffer = iv->rdata;
  221. /* Emulate partial word read by adding dummy bits for offset */
  222. t->dummy_bits = (iv->iaddr & 3) << 2;
  223. if (iv->cmd & FPGA_CMD_STATUS) {
  224. /*
  225. * Include the status "dummy" bits
  226. * THIS REQUIRES THE REMOTE ADDRESS TO BE 32-BIT ALIGNED
  227. */
  228. t->base.rxlength += 32;
  229. t->dummy_bits -= 16;
  230. }
  231. } else {
  232. t->base.length = iv->len << 3;
  233. t->base.tx_buffer = iv->wdata;
  234. }
  235. ntrans++;
  236. }
  237. if (!ntrans)
  238. return ESP_OK;
  239. esp_err_t err = ESP_OK;
  240. xSemaphoreTakeRecursive(spi_mutex, portMAX_DELAY);
  241. if (!spi_handle) {
  242. err = ESP_FAIL;
  243. goto fail;
  244. }
  245. xEventGroupClearBits(spi_done_evgroup, ~(EventBits_t)0);
  246. size_t tbit = 1;
  247. for (size_t i = 0; i < ntrans; i++) {
  248. spi_transaction_ext_t *t = &trans[i];
  249. t->base.user = (void *)tbit;
  250. err = spi_device_queue_trans(spi_handle, &t->base, portMAX_DELAY);
  251. if (err) {
  252. ntrans = i;
  253. break;
  254. }
  255. tbit <<= 1;
  256. }
  257. if (likely(ntrans)) {
  258. xEventGroupWaitBits(spi_done_evgroup, tbit-1, pdTRUE, pdTRUE,
  259. portMAX_DELAY);
  260. while (ntrans--) {
  261. /* This is insanely stupid to have to do when not needed */
  262. spi_transaction_t *tp;
  263. spi_device_get_trans_result(spi_handle, &tp, 0);
  264. }
  265. }
  266. fail:
  267. xSemaphoreGiveRecursive(spi_mutex);
  268. return err;
  269. }
  270. esp_err_t fpga_io_write(unsigned int cmd, const void *addr,
  271. const void *data, size_t len)
  272. {
  273. struct fpga_iov iov;
  274. iov.cmd = cmd | ~FPGA_CMD_RD;
  275. iov.addr = addr;
  276. iov.wdata = data;
  277. iov.len = len;
  278. return fpga_iov(&iov, 1);
  279. }
  280. esp_err_t fpga_io_read(unsigned int cmd, const void *addr,
  281. void *data, size_t len)
  282. {
  283. struct fpga_iov iov;
  284. iov.cmd = cmd | FPGA_CMD_RD;
  285. iov.addr = addr;
  286. iov.rdata = data;
  287. iov.len = len;
  288. return fpga_iov(&iov, 1);
  289. }
  290. /*
  291. * Get status in polling mode (small transaction, < 256 CPU cycles).
  292. * cmd typically would be IRQ/ACK bits.
  293. */
  294. uint32_t fpga_io_status(unsigned int cmd)
  295. {
  296. spi_transaction_t trans;
  297. memset(&trans, 0, sizeof trans);
  298. trans.flags =
  299. SPI_TRANS_MODE_DIO |
  300. SPI_TRANS_MULTILINE_CMD |
  301. SPI_TRANS_MULTILINE_ADDR |
  302. SPI_TRANS_USE_RXDATA;
  303. trans.cmd = cmd | FPGA_CMD_RD;
  304. trans.addr = 0;
  305. trans.rxlength = 32;
  306. esp_err_t err = ESP_OK;
  307. xSemaphoreTakeRecursive(spi_mutex, portMAX_DELAY);
  308. err = spi_device_polling_transmit(spi_handle, &trans);
  309. xSemaphoreGiveRecursive(spi_mutex);
  310. return err ? 0 : *(const uint32_t *)trans.rx_data;
  311. }
  312. static void fpga_service_task(void *dummy)
  313. {
  314. (void)dummy;
  315. uint32_t status;
  316. bool fpga_initialized = false;
  317. enum fpga_state {
  318. FPGA_DISABLED, /* FPGA services disabled */
  319. FPGA_OFFLINE, /* FPGA services enabled, waiting for FPGA */
  320. FPGA_ONLINE /* FPGA services active */
  321. } fpga_state = FPGA_DISABLED;
  322. printf("[FPGA] Starting FPGA services task\n");
  323. while (1) {
  324. uint32_t notifiers, status;
  325. switch (fpga_state) {
  326. case FPGA_DISABLED:
  327. notifiers = notify_wait_for(NOTIFY_ENABLE);
  328. if ((notifiers & NOTIFY_ENABLE) && fpga_link_enable()) {
  329. fputs("[FPGA] FPGA services enabled\n", stdout);
  330. fpga_state = FPGA_OFFLINE;
  331. }
  332. break;
  333. case FPGA_OFFLINE:
  334. fpga_io_status(FPGA_CMD_IRQ(EL_DIRQ_HELLO));
  335. notifiers = notify_wait_for(NOTIFY_FPGA|NOTIFY_DISABLE);
  336. if (notifiers & NOTIFY_DISABLE)
  337. break;
  338. status = fpga_io_status(FPGA_CMD_ACK(EL_UIRQ_READY));
  339. if ((status & ~0xfce) == 0x9030 && fpga_online()) {
  340. fpga_state = FPGA_ONLINE;
  341. }
  342. break;
  343. case FPGA_ONLINE:
  344. notifiers = notify_wait_for(NOTIFY_FPGA|NOTIFY_DISABLE);
  345. if (notifiers & NOTIFY_DISABLE) {
  346. fpga_offline();
  347. break;
  348. }
  349. while (!digitalRead(PIN_FPGA_INT)) {
  350. status = fpga_io_status(0);
  351. if ((status & ~0xfce) != 0x9010) {
  352. fpga_offline();
  353. fputs("[FPGA] FPGA offline\n", stdout);
  354. fpga_state = FPGA_OFFLINE;
  355. break;
  356. }
  357. if (status & 0x40)
  358. esplink_poll();
  359. if (status & 0x80) {
  360. fputs("[FPGA] invalid upstream interrupt 3\n", stdout);
  361. fpga_io_status(FPGA_CMD_ACK(3));
  362. }
  363. }
  364. break;
  365. }
  366. if (notifiers & NOTIFY_DISABLE) {
  367. fputs("[FPGA] FPGA services disabled\n", stdout);
  368. fpga_link_disable();
  369. fpga_state = FPGA_DISABLED;
  370. }
  371. }
  372. }