xflash_dump.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. XFLASH_SPI_ENTER();
  33. for(uint32_t addr = DUMP_OFFSET;
  34. addr < DUMP_OFFSET + DUMP_SIZE;
  35. addr += 4096)
  36. {
  37. xflash_enable_wr();
  38. xflash_sector_erase(DUMP_OFFSET);
  39. xflash_wait_busy();
  40. }
  41. }
  42. static void xfdump_dump_core(dump_header_t& hdr, uint32_t addr, uint8_t* buf, uint16_t cnt)
  43. {
  44. XFLASH_SPI_ENTER();
  45. // start by clearing all sectors (we need all of them in any case)
  46. xfdump_erase();
  47. // write header
  48. static_assert(sizeof(hdr) <= 256, "header is larger than a single page write");
  49. xflash_enable_wr();
  50. xflash_page_program(DUMP_OFFSET, (uint8_t*)&hdr, sizeof(hdr));
  51. xflash_wait_busy();
  52. // write data
  53. static_assert(sizeof(dump_t::data) <= RAMEND, "dump area size insufficient");
  54. xflash_multipage_program(addr, buf, cnt);
  55. }
  56. void xfdump_dump()
  57. {
  58. dump_header_t buf;
  59. buf.magic = DUMP_MAGIC;
  60. buf.regs_present = false;
  61. buf.crash_reason = (uint8_t)dump_crash_reason::manual;
  62. // write sram only
  63. xfdump_dump_core(buf, DUMP_OFFSET + offsetof(dump_t, data.sram),
  64. (uint8_t*)RAMSTART, RAMSIZE);
  65. }
  66. void xfdump_full_dump_and_reset(dump_crash_reason reason)
  67. {
  68. dump_header_t buf;
  69. buf.magic = DUMP_MAGIC;
  70. buf.regs_present = true;
  71. buf.crash_reason = (uint8_t)reason;
  72. // disable interrupts for a cleaner register dump
  73. cli();
  74. // ensure there's always enough time (with some margin) to dump
  75. // dump time on w25x20cl: ~150ms
  76. wdt_enable(WDTO_500MS);
  77. // write all addressable ranges (this will trash bidirectional registers)
  78. xfdump_dump_core(buf, DUMP_OFFSET + offsetof(dump_t, data), 0, RAMEND);
  79. // force a reset even sooner
  80. softReset();
  81. }
  82. #endif