2021-06-08 13:28:02 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
#include <avr/wdt.h>
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
|
|
|
|
#include "xflash_dump.h"
|
2021-06-08 15:17:18 +00:00
|
|
|
#ifdef XFLASH_DUMP
|
2021-06-16 21:58:00 +00:00
|
|
|
#include "asm.h"
|
2021-06-08 13:28:02 +00:00
|
|
|
#include "xflash.h"
|
2021-06-10 15:02:41 +00:00
|
|
|
#include "Marlin.h" // for softReset
|
2021-06-08 13:28:02 +00:00
|
|
|
|
2021-06-11 17:21:51 +00:00
|
|
|
bool xfdump_check_state(dump_crash_reason* reason)
|
2021-06-08 13:28:02 +00:00
|
|
|
{
|
|
|
|
uint32_t magic;
|
|
|
|
|
|
|
|
XFLASH_SPI_ENTER();
|
|
|
|
xflash_rd_data(DUMP_OFFSET + offsetof(dump_t, header.magic),
|
|
|
|
(uint8_t*)&magic, sizeof(magic));
|
2021-06-11 17:21:51 +00:00
|
|
|
if (magic != DUMP_MAGIC)
|
2021-06-08 13:28:02 +00:00
|
|
|
return false;
|
|
|
|
|
2021-06-11 17:21:51 +00:00
|
|
|
if (reason)
|
|
|
|
{
|
|
|
|
xflash_rd_data(DUMP_OFFSET + offsetof(dump_t, header.crash_reason),
|
|
|
|
(uint8_t*)reason, sizeof(*reason));
|
|
|
|
}
|
|
|
|
return true;
|
2021-06-08 13:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void xfdump_reset()
|
|
|
|
{
|
|
|
|
XFLASH_SPI_ENTER();
|
|
|
|
xflash_enable_wr();
|
|
|
|
xflash_sector_erase(DUMP_OFFSET + offsetof(dump_t, header.magic));
|
|
|
|
xflash_wait_busy();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void xfdump_erase()
|
|
|
|
{
|
|
|
|
for(uint32_t addr = DUMP_OFFSET;
|
|
|
|
addr < DUMP_OFFSET + DUMP_SIZE;
|
|
|
|
addr += 4096)
|
|
|
|
{
|
|
|
|
xflash_enable_wr();
|
2021-06-22 13:54:22 +00:00
|
|
|
xflash_sector_erase(addr);
|
2021-06-08 13:28:02 +00:00
|
|
|
xflash_wait_busy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-16 21:58:00 +00:00
|
|
|
static void __attribute__((noinline)) xfdump_dump_core(dump_header_t& hdr, uint32_t addr, uint8_t* buf, uint16_t cnt)
|
2021-06-08 13:28:02 +00:00
|
|
|
{
|
|
|
|
XFLASH_SPI_ENTER();
|
|
|
|
|
|
|
|
// start by clearing all sectors (we need all of them in any case)
|
|
|
|
xfdump_erase();
|
|
|
|
|
2021-06-16 21:58:00 +00:00
|
|
|
// sample SP/PC
|
|
|
|
hdr.sp = SP;
|
|
|
|
GETPC(&hdr.pc);
|
|
|
|
|
2021-06-08 13:28:02 +00:00
|
|
|
// write header
|
2021-06-08 17:08:03 +00:00
|
|
|
static_assert(sizeof(hdr) <= 256, "header is larger than a single page write");
|
2021-06-08 13:28:02 +00:00
|
|
|
xflash_enable_wr();
|
|
|
|
xflash_page_program(DUMP_OFFSET, (uint8_t*)&hdr, sizeof(hdr));
|
|
|
|
xflash_wait_busy();
|
|
|
|
|
|
|
|
// write data
|
2021-06-12 11:16:12 +00:00
|
|
|
static_assert(sizeof(dump_t::data) <= RAMEND+1, "dump area size insufficient");
|
2021-06-08 13:28:02 +00:00
|
|
|
xflash_multipage_program(addr, buf, cnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-16 21:58:00 +00:00
|
|
|
void xfdump_dump()
|
2021-06-08 13:28:02 +00:00
|
|
|
{
|
|
|
|
dump_header_t buf;
|
|
|
|
buf.magic = DUMP_MAGIC;
|
|
|
|
buf.regs_present = false;
|
2021-06-10 15:35:49 +00:00
|
|
|
buf.crash_reason = (uint8_t)dump_crash_reason::manual;
|
2021-06-08 13:28:02 +00:00
|
|
|
|
|
|
|
// write sram only
|
|
|
|
xfdump_dump_core(buf, DUMP_OFFSET + offsetof(dump_t, data.sram),
|
2021-06-08 15:53:06 +00:00
|
|
|
(uint8_t*)RAMSTART, RAMSIZE);
|
2021-06-08 13:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-06-16 21:58:00 +00:00
|
|
|
void xfdump_full_dump_and_reset(dump_crash_reason reason)
|
2021-06-08 13:28:02 +00:00
|
|
|
{
|
|
|
|
dump_header_t buf;
|
|
|
|
buf.magic = DUMP_MAGIC;
|
|
|
|
buf.regs_present = true;
|
2021-06-10 15:35:49 +00:00
|
|
|
buf.crash_reason = (uint8_t)reason;
|
2021-06-08 13:28:02 +00:00
|
|
|
|
|
|
|
// disable interrupts for a cleaner register dump
|
|
|
|
cli();
|
|
|
|
|
2021-06-10 14:24:52 +00:00
|
|
|
// ensure there's always enough time (with some margin) to dump
|
|
|
|
// dump time on w25x20cl: ~150ms
|
|
|
|
wdt_enable(WDTO_500MS);
|
|
|
|
|
2021-06-08 13:28:02 +00:00
|
|
|
// write all addressable ranges (this will trash bidirectional registers)
|
2021-06-12 11:16:12 +00:00
|
|
|
xfdump_dump_core(buf, DUMP_OFFSET + offsetof(dump_t, data), 0, RAMEND+1);
|
2021-06-08 13:28:02 +00:00
|
|
|
|
2021-06-10 14:24:52 +00:00
|
|
|
// force a reset even sooner
|
2021-06-09 10:59:26 +00:00
|
|
|
softReset();
|
2021-06-08 13:28:02 +00:00
|
|
|
}
|
2021-06-08 15:17:18 +00:00
|
|
|
#endif
|