esp.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #include "compiler.h"
  2. #include "common.h"
  3. #include "io.h"
  4. #include "console.h"
  5. #include "esplink.h"
  6. struct esplink_head __esplink_head esplink_head;
  7. static volatile __esplink struct esplink_timesync tsync;
  8. static volatile __esplink struct esplink_ota ota;
  9. IRQHANDLER(esp,0)
  10. {
  11. uint32_t irqstatus = ESP_CPU_IRQ;
  12. ESP_CPU_IRQ_CLR = irqstatus;
  13. if (irqstatus & (1 << EL_DIRQ_UNDERRUN)) {
  14. con_printf("[ESP] ESP link memory underrun!!\n");
  15. ESP_SPI_IRQ = (1 << EL_UIRQ_READY); /* Block writes, reinitialize! */
  16. return;
  17. }
  18. if (irqstatus & (1 << EL_DIRQ_TIME)) {
  19. if (tsync.set.update) {
  20. SYSCLOCK_TICK_HOLD = tsync.set.tick;
  21. SYSCLOCK_DATETIME = tsync.set.td;
  22. tsync.set.update = 0;
  23. do_write_rtc = true;
  24. } else {
  25. tsync.get.td = SYSCLOCK_DATETIME;
  26. tsync.get.tick = SYSCLOCK_TICK_HOLD;
  27. ESP_SPI_IRQ_SET = 1 << EL_UIRQ_TIME;
  28. }
  29. }
  30. if (irqstatus & (1 << EL_DIRQ_HELLO)) {
  31. con_printf("[ESP] Got hello, sending ready...\n");
  32. /* Hello, are you there? Yes, I'm here, and you can write data now */
  33. ESP_SPI_IRQ_SET = (1 << EL_UIRQ_READY)|(1 << EL_UIRQ_WREN);
  34. }
  35. /*
  36. * Check to see if we got hello after an OTA process was completed
  37. * or aborted; ESP will send EL_DIRQ_DONE to wake us up.
  38. */
  39. if (!(ESP_SPI_IRQ & (1 << EL_UIRQ_OTA))) {
  40. ota.data = NULL;
  41. ota.len = 0;
  42. }
  43. }
  44. void esp_ota(const void *data, size_t len)
  45. {
  46. mask_irq(ESP_IRQ);
  47. ota.data = data;
  48. ota.len = len;
  49. ESP_SPI_IRQ_SET = 1 << EL_UIRQ_OTA;
  50. unmask_irq(ESP_IRQ);
  51. while (ota.data)
  52. waitfor(ESP_IRQ);
  53. }
  54. void esp_init(void)
  55. {
  56. static char __dram_data esp_signature[] = "Hej tomtebuggar slå i glasen!";
  57. ESP_CPU_IRQ = 0;
  58. ESP_SPI_IRQ = 0;
  59. memset(&esplink_head, 0, sizeof esplink_head);
  60. esplink_head.hlen = sizeof esplink_head;
  61. esplink_head.board.cfg = SYS_BOARDCFG;
  62. memcpy(esplink_head.signature, esp_signature, sizeof esp_signature);
  63. esplink_head.tsync = &tsync;
  64. esplink_head.ota = &ota;
  65. esplink_head.magic = ESPLINK_HEAD_MAGIC;
  66. ESP_SPI_IRQ = (1 << EL_UIRQ_READY);
  67. unmask_irq(ESP_IRQ);
  68. }