fpgasvc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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 10
  16. #define FPGA_SVC_STACK 4096
  17. #define RTC_TIMESYNC_PERIOD (511*configTICK_RATE_HZ)
  18. static spi_bus_config_t spi_bus_config = {
  19. .data0_io_num = PIN_FPGA_IO0,
  20. .data1_io_num = PIN_FPGA_IO1,
  21. .sclk_io_num = PIN_FPGA_CLK,
  22. .data2_io_num = -1,
  23. .data3_io_num = -1,
  24. .data4_io_num = -1,
  25. .data5_io_num = -1,
  26. .data6_io_num = -1,
  27. .data7_io_num = -1,
  28. .max_transfer_sz = 4096,
  29. .flags = SPICOMMON_BUSFLAG_MASTER | SPICOMMON_BUSFLAG_DUAL
  30. };
  31. #define FPGA_IOV_MAX 4
  32. static void ARDUINO_ISR_ATTR spi_callback(spi_transaction_t *);
  33. static const spi_device_interface_config_t spi_device_interface_config = {
  34. .command_bits = 8,
  35. .address_bits = 32,
  36. .dummy_bits = 0,
  37. .mode = 0,
  38. .cs_ena_pretrans = 0,
  39. .cs_ena_posttrans = 0,
  40. .clock_speed_hz = SPI_MASTER_FREQ_40M,
  41. .spics_io_num = PIN_FPGA_CS,
  42. .flags = SPI_DEVICE_HALFDUPLEX,
  43. .queue_size = FPGA_IOV_MAX,
  44. .post_cb = spi_callback
  45. };
  46. static spi_device_handle_t spi_handle;
  47. static TaskHandle_t fpga_task;
  48. static TimerHandle_t fpga_timesync_timer;
  49. static SemaphoreHandle_t spi_mutex;
  50. static EventGroupHandle_t spi_done_evgroup;
  51. static volatile bool spi_abort_all;
  52. static struct esplink_head head;
  53. #define NOTIFY_INDEX 0
  54. #define NOTIFY_FPGA (1 << 0)
  55. #define NOTIFY_ENABLE (1 << 1)
  56. #define NOTIFY_DISABLE (1 << 2)
  57. #define NOTIFY_TIMESYNC (1 << 3)
  58. #if 0
  59. #define NOTIFY_SPI (1 << 3)
  60. #define NOTIFY_RINGBUF (1 << 4)
  61. #endif
  62. static uint32_t notify_poll_for(uint32_t flags)
  63. {
  64. return ulTaskNotifyValueClearIndexed(NULL, NOTIFY_INDEX, flags);
  65. }
  66. /* This supports multiple flags set */
  67. static uint32_t notify_wait_for(uint32_t flags)
  68. {
  69. uint32_t notify_value;
  70. /* Already received? Might already have been waited for... */
  71. notify_value = notify_poll_for(flags);
  72. while (!(notify_value & flags)) {
  73. xTaskNotifyWaitIndexed(NOTIFY_INDEX, 0, flags,
  74. &notify_value, portMAX_DELAY);
  75. }
  76. return notify_value;
  77. }
  78. static void ARDUINO_ISR_ATTR fpga_notify_from_isr(uint32_t flags)
  79. {
  80. BaseType_t wakeup = pdFALSE;
  81. if (xTaskNotifyIndexedFromISR(fpga_task, NOTIFY_INDEX, flags, eSetBits,
  82. &wakeup) != pdFAIL)
  83. portYIELD_FROM_ISR(wakeup);
  84. }
  85. static void fpga_notify_from_task(uint32_t flags)
  86. {
  87. xTaskNotifyIndexed(fpga_task, NOTIFY_INDEX, flags, eSetBits);
  88. }
  89. static void ARDUINO_ISR_ATTR fpga_interrupt(void)
  90. {
  91. fpga_notify_from_isr(NOTIFY_FPGA);
  92. }
  93. void fpga_timesync_trigger(void)
  94. {
  95. fpga_notify_from_task(NOTIFY_TIMESYNC);
  96. }
  97. static void ARDUINO_ISR_ATTR spi_callback(spi_transaction_t *t)
  98. {
  99. size_t flags = (size_t)t->user;
  100. if (!flags)
  101. return;
  102. BaseType_t wakeup = pdFALSE;
  103. if (xEventGroupSetBitsFromISR(spi_done_evgroup, (size_t)t->user,
  104. &wakeup) != pdFAIL)
  105. portYIELD_FROM_ISR(wakeup);
  106. }
  107. static void fpga_service_task(void *);
  108. static EventGroupHandle_t fpga_service_evgroup;
  109. void fpga_service_enable(bool on)
  110. {
  111. uint32_t flag = on ? NOTIFY_ENABLE : NOTIFY_DISABLE;
  112. fpga_notify_from_task(flag);
  113. xEventGroupWaitBits(fpga_service_evgroup, flag, 0, pdTRUE, portMAX_DELAY);
  114. }
  115. esp_err_t fpga_service_init(void)
  116. {
  117. pinMode(PIN_FPGA_INT, INPUT);
  118. setvar_bool(status_max80_fpga, false);
  119. fpga_service_evgroup = null_check(xEventGroupCreate());
  120. spi_mutex = null_check(xSemaphoreCreateRecursiveMutex());
  121. spi_done_evgroup = null_check(xEventGroupCreate());
  122. /* The ordering here attempts to avoid race conditions... */
  123. if (xTaskCreate(fpga_service_task, "fpga_svc", FPGA_SVC_STACK, NULL,
  124. FPGA_PRIORITY, &fpga_task) != pdPASS)
  125. return ESP_FAIL;
  126. fpga_timesync_timer =
  127. null_check(xTimerCreate("rtc_sync", RTC_TIMESYNC_PERIOD, pdTRUE, NULL,
  128. (TimerCallbackFunction_t)fpga_timesync_trigger));
  129. esplink_init();
  130. xEventGroupSetBits(fpga_service_evgroup, NOTIFY_DISABLE);
  131. return ESP_OK;
  132. }
  133. static bool fpga_link_enable(void)
  134. {
  135. esp_err_t err;
  136. if (spi_handle)
  137. return true; /* Already started */
  138. xEventGroupClearBits(fpga_service_evgroup, NOTIFY_DISABLE);
  139. err = spi_bus_initialize(FPGA_SPI_HOST, &spi_bus_config, SPI_DMA_CH_AUTO);
  140. if (err)
  141. goto init_fail;
  142. err = spi_bus_add_device(FPGA_SPI_HOST, &spi_device_interface_config,
  143. &spi_handle);
  144. if (err)
  145. goto free_bus_fail;
  146. /* Only device on this bus, so acquire it permanently */
  147. err = spi_device_acquire_bus(spi_handle, portMAX_DELAY);
  148. if (err)
  149. goto release_bus_fail;
  150. xEventGroupClearBits(spi_done_evgroup, EVENT_ALL_BITS);
  151. pinMode(PIN_FPGA_INT, INPUT);
  152. attachInterrupt(PIN_FPGA_INT, fpga_interrupt, FALLING);
  153. xEventGroupSetBits(fpga_service_evgroup, NOTIFY_ENABLE);
  154. xSemaphoreGiveRecursive(spi_mutex);
  155. fpga_notify_from_task(NOTIFY_FPGA); /* In case FPGA_INT was already low */
  156. goto done;
  157. release_bus_fail:
  158. spi_bus_remove_device(spi_handle);
  159. spi_handle = NULL;
  160. free_bus_fail:
  161. spi_bus_free(FPGA_SPI_HOST);
  162. init_fail:
  163. xEventGroupSetBits(fpga_service_evgroup, NOTIFY_DISABLE);
  164. done:
  165. return !err;
  166. }
  167. static void fpga_link_disable(void)
  168. {
  169. if (!spi_handle)
  170. return; /* Already stopped */
  171. xEventGroupClearBits(fpga_service_evgroup, NOTIFY_ENABLE);
  172. xSemaphoreTakeRecursive(spi_mutex, portMAX_DELAY);
  173. detachInterrupt(PIN_FPGA_INT);
  174. spi_device_release_bus(spi_handle);
  175. spi_bus_remove_device(spi_handle);
  176. spi_bus_free(FPGA_SPI_HOST);
  177. spi_handle = NULL;
  178. xEventGroupSetBits(fpga_service_evgroup, NOTIFY_DISABLE);
  179. }
  180. static void fpga_poll_set_time(void);
  181. esp_err_t fpga_send_config(void)
  182. {
  183. char *buf = NULL;
  184. size_t bufsize = head.cfg.buflen;
  185. esp_err_t err = ESP_ERR_NO_MEM;
  186. buf = xmalloc_dma(bufsize);
  187. if (!buf)
  188. goto fail;
  189. if (sysvar_marshall(sysvar_null, sysvar_count, buf, &bufsize,
  190. (uintptr_t)head.cfg.buf) != sysvar_count)
  191. goto fail;
  192. bufsize = (bufsize + 3) & ~3; /* Round to dword */
  193. err = fpga_io_write(FPGA_CMD_IRQ(EL_DIRQ_CONFIG), head.cfg.buf,
  194. buf, bufsize);
  195. fail:
  196. if (buf)
  197. free(buf);
  198. printf("[FPGA] Configuration sent, %zu bytes, status = %u\n", bufsize, err);
  199. return err;
  200. }
  201. static bool fpga_online(void)
  202. {
  203. fpga_io_read(FPGA_CMD_ACK(EL_UIRQ_READY), ESPLINK_HDR_ADDR,
  204. &head, sizeof head);
  205. if (head.magic != ESPLINK_HEAD_MAGIC || head.hlen <= 8) {
  206. printf("[FPGA] Bad header received, magic = 0x%08x len = %u\n",
  207. head.magic, head.hlen);
  208. return false;
  209. }
  210. if (unlikely(head.hlen < sizeof head)) {
  211. /* Clear any fields not provided */
  212. memset((char *)&head + head.hlen, 0, sizeof head - head.hlen);
  213. }
  214. printf("[FPGA] Ready, board = %u.%u fixes %02x fpga %u\n",
  215. head.board.major, head.board.minor,
  216. head.board.fixes, head.board.fpga);
  217. printf("[FPGA] online, signature \"%.*s\"\n",
  218. (int)(sizeof head.signature - 1), head.signature);
  219. esplink_start(&head);
  220. setvar_bool(status_max80_fpga, true);
  221. xSemaphoreGiveRecursive(spi_mutex);
  222. fpga_send_config();
  223. xTimerStart(fpga_timesync_timer, portMAX_DELAY);
  224. fpga_poll_set_time();
  225. return true;
  226. }
  227. static void fpga_offline(void)
  228. {
  229. memset(&head, 0, sizeof head);
  230. xSemaphoreTakeRecursive(spi_mutex, portMAX_DELAY);
  231. xTimerStop(fpga_timesync_timer, portMAX_DELAY);
  232. setvar_bool(status_max80_fpga, false);
  233. esplink_start(NULL); /* Stop esplink */
  234. }
  235. esp_err_t fpga_iov(const struct fpga_iov *iov, size_t niov)
  236. {
  237. spi_transaction_ext_t trans[FPGA_IOV_MAX];
  238. size_t ntrans = 0;
  239. if (niov > FPGA_IOV_MAX)
  240. return ESP_ERR_INVALID_ARG;
  241. for (size_t i = 0; i < niov; i++) {
  242. const struct fpga_iov *iv = &iov[i];
  243. if (!iv->len && !(iv->cmd & FPGA_CMD_NULL))
  244. continue;
  245. spi_transaction_ext_t *t = &trans[ntrans];
  246. memset(t, 0, sizeof *t);
  247. t->base.flags =
  248. SPI_TRANS_MODE_DIO |
  249. SPI_TRANS_VARIABLE_DUMMY |
  250. SPI_TRANS_MULTILINE_CMD |
  251. SPI_TRANS_MULTILINE_ADDR;
  252. t->base.cmd = iv->cmd;
  253. t->base.addr = iv->iaddr;
  254. if (iv->cmd & FPGA_CMD_RD) {
  255. t->base.rxlength = iv->len << 3;
  256. t->base.rx_buffer = iv->rdata;
  257. /* Emulate partial word read by adding dummy bits for offset */
  258. t->dummy_bits = 16 + ((iv->iaddr & 3) << 2);
  259. if (iv->cmd & FPGA_CMD_STATUS) {
  260. /*
  261. * Include the status "dummy" bits
  262. * THIS REQUIRES THE REMOTE ADDRESS TO BE 32-BIT ALIGNED
  263. */
  264. t->base.rxlength += 32;
  265. t->dummy_bits -= 16;
  266. }
  267. } else {
  268. t->base.length = iv->len << 3;
  269. t->base.tx_buffer = iv->wdata;
  270. }
  271. ntrans++;
  272. }
  273. if (!ntrans)
  274. return ESP_OK;
  275. esp_err_t err = ESP_OK;
  276. xSemaphoreTakeRecursive(spi_mutex, portMAX_DELAY);
  277. if (!spi_handle) {
  278. err = ESP_FAIL;
  279. goto fail;
  280. }
  281. xEventGroupClearBits(spi_done_evgroup, EVENT_ALL_BITS);
  282. size_t tbit = 1;
  283. for (size_t i = 0; i < ntrans; i++) {
  284. spi_transaction_ext_t *t = &trans[i];
  285. t->base.user = (void *)tbit;
  286. err = spi_device_queue_trans(spi_handle, &t->base, portMAX_DELAY);
  287. if (err) {
  288. ntrans = i;
  289. break;
  290. }
  291. tbit <<= 1;
  292. }
  293. if (likely(ntrans)) {
  294. xEventGroupWaitBits(spi_done_evgroup, tbit-1, pdTRUE, pdTRUE,
  295. portMAX_DELAY);
  296. while (ntrans--) {
  297. /* This is insanely stupid to have to do when not needed */
  298. spi_transaction_t *tp;
  299. spi_device_get_trans_result(spi_handle, &tp, 0);
  300. }
  301. }
  302. fail:
  303. xSemaphoreGiveRecursive(spi_mutex);
  304. return err;
  305. }
  306. esp_err_t fpga_io_write(unsigned int cmd, const volatile void *addr,
  307. const void *data, size_t len)
  308. {
  309. struct fpga_iov iov;
  310. iov.cmd = cmd & ~FPGA_CMD_RD;
  311. iov.addr = addr;
  312. iov.wdata = data;
  313. iov.len = len;
  314. return fpga_iov(&iov, 1);
  315. }
  316. esp_err_t fpga_io_read(unsigned int cmd, const volatile void *addr,
  317. void *data, size_t len)
  318. {
  319. struct fpga_iov iov;
  320. iov.cmd = cmd | FPGA_CMD_RD;
  321. iov.addr = addr;
  322. iov.rdata = data;
  323. iov.len = len;
  324. return fpga_iov(&iov, 1);
  325. }
  326. /*
  327. * This should be executed after getting an EL_UIRQ_TIME notification;
  328. * do this in polling mode for best latency.
  329. */
  330. static void fpga_get_time(void)
  331. {
  332. esp_err_t err;
  333. struct tm tm;
  334. struct timeval tv;
  335. struct tsbuf {
  336. /* These two words are the status word normally considered "dummy" */
  337. uint16_t status;
  338. uint16_t tick;
  339. struct esplink_timesync_buf get;
  340. } tsbuf;
  341. if (!head.tsync) {
  342. fpga_io_status(FPGA_CMD_ACK(EL_UIRQ_TIME));
  343. return;
  344. }
  345. spi_transaction_ext_t trans;
  346. memset(&trans, 0, sizeof trans);
  347. trans.base.flags =
  348. SPI_TRANS_MODE_DIO |
  349. SPI_TRANS_VARIABLE_DUMMY |
  350. SPI_TRANS_MULTILINE_CMD |
  351. SPI_TRANS_MULTILINE_ADDR;
  352. trans.base.rxlength = sizeof tsbuf << 3;
  353. trans.base.rx_buffer = &tsbuf;
  354. trans.base.addr = (size_t)&head.tsync->get;
  355. trans.base.cmd = FPGA_CMD_RD | FPGA_CMD_ACK(EL_UIRQ_TIME);
  356. xSemaphoreTakeRecursive(spi_mutex, portMAX_DELAY);
  357. err = spi_device_polling_transmit(spi_handle, &trans.base);
  358. xSemaphoreGiveRecursive(spi_mutex);
  359. if (err)
  360. return;
  361. if (time_net_sync_status)
  362. return; /* Ignore time from RTC if SNTP active now */
  363. tm.tm_sec = tsbuf.get.tm.sec2 << 1;
  364. tm.tm_min = tsbuf.get.tm.min;
  365. tm.tm_hour = tsbuf.get.tm.hour;
  366. tm.tm_mday = tsbuf.get.tm.mday;
  367. tm.tm_mon = tsbuf.get.tm.mon - 1;
  368. tm.tm_year = tsbuf.get.tm.year + 80;
  369. tm.tm_isdst = -1; /* Unknown */
  370. /* The third term handles wraparounds due to delay in transit */
  371. tv.tv_sec = mktime(&tm) + (tsbuf.tick >> 15) +
  372. ((tsbuf.get.tick >= tsbuf.tick) << 1);
  373. tv.tv_usec = (((uint32_t)tsbuf.tick << 17) * UINT64_C(1000000)) >> 32;
  374. settimeofday(&tv, NULL);
  375. print_time("[FPGA] Time set from RTC: ", &tv);
  376. }
  377. static void fpga_poll_set_time(void)
  378. {
  379. if (!head.tsync)
  380. return;
  381. if (!time_net_sync_status) {
  382. /* Poll for current time; will call fpga_get_time() later */
  383. fpga_io_status(FPGA_CMD_IRQ(EL_DIRQ_TIME));
  384. return;
  385. }
  386. /* Otherwise transmit time to set the RTC */
  387. esp_err_t err;
  388. struct timeval tv;
  389. struct esplink_timesync_buf tset;
  390. spi_transaction_t trans;
  391. memset(&trans, 0, sizeof trans);
  392. tset.update = 1;
  393. xSemaphoreTakeRecursive(spi_mutex, portMAX_DELAY);
  394. gettimeofday(&tv, NULL);
  395. const struct tm *tm = localtime(&tv.tv_sec);
  396. tset.tick = ((tv.tv_usec * ((1ULL << (32+15))/1000000+1)) >> 32)
  397. + ((tv.tv_sec & 1) << 15);
  398. tset.tm.sec2 = tm->tm_sec >> 1;
  399. tset.tm.min = tm->tm_min;
  400. tset.tm.hour = tm->tm_hour;
  401. tset.tm.mday = tm->tm_mday;
  402. tset.tm.mon = tm->tm_mon + 1;
  403. tset.tm.year = tm->tm_year - 80;
  404. trans.flags =
  405. SPI_TRANS_MODE_DIO |
  406. SPI_TRANS_MULTILINE_CMD |
  407. SPI_TRANS_MULTILINE_ADDR;
  408. trans.length = sizeof tset << 3;
  409. trans.tx_buffer = &tset;
  410. trans.addr = (size_t)&head.tsync->set;
  411. trans.cmd = FPGA_CMD_WR | FPGA_CMD_IRQ(EL_DIRQ_TIME) |
  412. FPGA_CMD_ACK(EL_UIRQ_TIME);
  413. err = spi_device_polling_transmit(spi_handle, &trans);
  414. xSemaphoreGiveRecursive(spi_mutex);
  415. if (err)
  416. return;
  417. print_time("[FPGA] RTC update: ", &tv);
  418. }
  419. /*
  420. * Get status in polling mode (small transaction, < 256 CPU cycles).
  421. * cmd typically would be IRQ/ACK bits.
  422. */
  423. uint32_t fpga_io_status(unsigned int cmd)
  424. {
  425. spi_transaction_t trans;
  426. memset(&trans, 0, sizeof trans);
  427. trans.flags =
  428. SPI_TRANS_MODE_DIO |
  429. SPI_TRANS_MULTILINE_CMD |
  430. SPI_TRANS_MULTILINE_ADDR |
  431. SPI_TRANS_USE_RXDATA;
  432. trans.cmd = cmd | FPGA_CMD_RD;
  433. trans.addr = 0;
  434. trans.rxlength = 32;
  435. esp_err_t err = ESP_OK;
  436. xSemaphoreTakeRecursive(spi_mutex, portMAX_DELAY);
  437. err = spi_device_polling_transmit(spi_handle, &trans);
  438. xSemaphoreGiveRecursive(spi_mutex);
  439. return err ? 0 : *(const uint32_t *)trans.rx_data;
  440. }
  441. static int fpga_read_func(token_t token, void *buf, size_t len)
  442. {
  443. const void **pp = token;
  444. const char *p = *pp;
  445. esp_err_t err;
  446. err = fpga_io_read(0, p, buf, len);
  447. if (err)
  448. return -1;
  449. p += len;
  450. *pp = p;
  451. return len;
  452. }
  453. static void fpga_ota_update(void)
  454. {
  455. struct esplink_ota ota;
  456. fpga_io_read(0, head.ota, &ota, sizeof ota);
  457. if (!ota.data)
  458. return;
  459. esp_update(fpga_read_func, &ota.data, ota.len);
  460. fpga_io_status(FPGA_CMD_ACK(EL_UIRQ_OTA)|FPGA_CMD_IRQ(EL_DIRQ_DONE));
  461. reboot_delayed();
  462. }
  463. static void fpga_service_task(void *dummy)
  464. {
  465. (void)dummy;
  466. enum fpga_state {
  467. FPGA_DISABLED, /* FPGA services disabled */
  468. FPGA_OFFLINE, /* FPGA services enabled, waiting for FPGA */
  469. FPGA_ONLINE /* FPGA services active */
  470. } fpga_state = FPGA_DISABLED;
  471. fputs("[FPGA] Starting FPGA services task\n", stdout);
  472. while (1) {
  473. uint32_t notifiers = 0;
  474. uint32_t status;
  475. switch (fpga_state) {
  476. case FPGA_DISABLED:
  477. notifiers = notify_wait_for(NOTIFY_ENABLE);
  478. if ((notifiers & NOTIFY_ENABLE) && fpga_link_enable()) {
  479. fputs("[FPGA] Enabling FPGA services\n", stdout);
  480. fpga_state = FPGA_OFFLINE;
  481. }
  482. break;
  483. case FPGA_OFFLINE:
  484. status = fpga_io_status(FPGA_CMD_IRQ(EL_DIRQ_HELLO));
  485. printf("[FPGA] FPGA status flags = 0x%08x\n", status);
  486. if (!digitalRead(PIN_FPGA_INT)) {
  487. for (unsigned int i = 1; i < 8; i++) {
  488. if (status & (0x100 << i))
  489. status = fpga_io_status(FPGA_CMD_ACK(i));
  490. }
  491. if ((status & 0x301) == 0x300) {
  492. if (fpga_online()) {
  493. fpga_state = FPGA_ONLINE;
  494. break;
  495. }
  496. }
  497. }
  498. notifiers = notify_wait_for(NOTIFY_FPGA|NOTIFY_DISABLE);
  499. break;
  500. case FPGA_ONLINE:
  501. notifiers = notify_wait_for(NOTIFY_FPGA|NOTIFY_DISABLE|
  502. NOTIFY_TIMESYNC);
  503. if (notifiers & NOTIFY_DISABLE) {
  504. fpga_offline();
  505. break;
  506. }
  507. while (!digitalRead(PIN_FPGA_INT)) {
  508. status = fpga_io_status(0);
  509. if ((status & 0x301) != 0x100) {
  510. fpga_offline();
  511. printf("[FPGA] FPGA offline, status = 0x%08x\n", status);
  512. fpga_state = FPGA_OFFLINE;
  513. notifiers = 0;
  514. break;
  515. }
  516. if (status & (0x100 << EL_UIRQ_TIME))
  517. fpga_get_time();
  518. if (status & (0x100 << EL_UIRQ_RINGBUF))
  519. esplink_poll();
  520. if (status & (0x100 << EL_UIRQ_OTA))
  521. fpga_ota_update();
  522. for (unsigned int i = 5; i < 8; i++) {
  523. if (status & (0x100 << i)) {
  524. printf("[FPGA] Invalid interrupt %u received\n", i);
  525. status = fpga_io_status(FPGA_CMD_ACK(i));
  526. }
  527. }
  528. }
  529. if (notifiers & NOTIFY_TIMESYNC)
  530. fpga_poll_set_time();
  531. break;
  532. }
  533. if (notifiers & NOTIFY_DISABLE) {
  534. fputs("[FPGA] Disabling FPGA services\n", stdout);
  535. fpga_link_disable();
  536. fpga_state = FPGA_DISABLED;
  537. }
  538. }
  539. }