xflash_dump.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include <stddef.h>
  2. #include <avr/wdt.h>
  3. #include <avr/interrupt.h>
  4. #include "xflash_dump.h"
  5. #ifdef XFLASH_DUMP
  6. #include "xflash.h"
  7. #include "Marlin.h" // for softReset
  8. bool xfdump_check_state(dump_crash_reason* reason)
  9. {
  10. uint32_t magic;
  11. XFLASH_SPI_ENTER();
  12. xflash_rd_data(DUMP_OFFSET + offsetof(dump_t, header.magic),
  13. (uint8_t*)&magic, sizeof(magic));
  14. if (magic != DUMP_MAGIC)
  15. return false;
  16. if (reason)
  17. {
  18. xflash_rd_data(DUMP_OFFSET + offsetof(dump_t, header.crash_reason),
  19. (uint8_t*)reason, sizeof(*reason));
  20. }
  21. return true;
  22. }
  23. void xfdump_reset()
  24. {
  25. XFLASH_SPI_ENTER();
  26. xflash_enable_wr();
  27. xflash_sector_erase(DUMP_OFFSET + offsetof(dump_t, header.magic));
  28. xflash_wait_busy();
  29. }
  30. static void xfdump_erase()
  31. {
  32. for(uint32_t addr = DUMP_OFFSET;
  33. addr < DUMP_OFFSET + DUMP_SIZE;
  34. addr += 4096)
  35. {
  36. xflash_enable_wr();
  37. xflash_sector_erase(DUMP_OFFSET);
  38. xflash_wait_busy();
  39. }
  40. }
  41. static void xfdump_dump_core(dump_header_t& hdr, uint32_t addr, uint8_t* buf, uint16_t cnt)
  42. {
  43. XFLASH_SPI_ENTER();
  44. // start by clearing all sectors (we need all of them in any case)
  45. xfdump_erase();
  46. // write header
  47. static_assert(sizeof(hdr) <= 256, "header is larger than a single page write");
  48. xflash_enable_wr();
  49. xflash_page_program(DUMP_OFFSET, (uint8_t*)&hdr, sizeof(hdr));
  50. xflash_wait_busy();
  51. // write data
  52. static_assert(sizeof(dump_t::data) <= RAMEND, "dump area size insufficient");
  53. xflash_multipage_program(addr, buf, cnt);
  54. }
  55. void xfdump_dump()
  56. {
  57. dump_header_t buf;
  58. buf.magic = DUMP_MAGIC;
  59. buf.regs_present = false;
  60. buf.crash_reason = (uint8_t)dump_crash_reason::manual;
  61. // write sram only
  62. xfdump_dump_core(buf, DUMP_OFFSET + offsetof(dump_t, data.sram),
  63. (uint8_t*)RAMSTART, RAMSIZE);
  64. }
  65. void xfdump_full_dump_and_reset(dump_crash_reason reason)
  66. {
  67. dump_header_t buf;
  68. buf.magic = DUMP_MAGIC;
  69. buf.regs_present = true;
  70. buf.crash_reason = (uint8_t)reason;
  71. // disable interrupts for a cleaner register dump
  72. cli();
  73. // ensure there's always enough time (with some margin) to dump
  74. // dump time on w25x20cl: ~150ms
  75. wdt_enable(WDTO_500MS);
  76. // write all addressable ranges (this will trash bidirectional registers)
  77. xfdump_dump_core(buf, DUMP_OFFSET + offsetof(dump_t, data), 0, RAMEND);
  78. // force a reset even sooner
  79. softReset();
  80. }
  81. #endif