xflash_dump.cpp 2.7 KB

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