From faff80689da243d38946691cb2df492d57beaaca Mon Sep 17 00:00:00 2001 From: Ethan Date: Wed, 20 Jul 2022 16:41:47 -0400 Subject: [PATCH 1/4] runs the table from flash using dma --- ddssweeper/dds-sweeper.c | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/ddssweeper/dds-sweeper.c b/ddssweeper/dds-sweeper.c index 7ee6c61..61503f8 100644 --- a/ddssweeper/dds-sweeper.c +++ b/ddssweeper/dds-sweeper.c @@ -425,10 +425,18 @@ void background() { wait(0); triggers = 0; + uint8_t *ins = (uint8_t *)(XIP_BASE + FLASH_TARGET_OFFSET); + + uint spi = dma_claim_unused_channel(true); + dma_channel_config c = dma_channel_get_default_config(spi); + channel_config_set_transfer_data_size(&c, DMA_SIZE_8); + channel_config_set_dreq(&c, spi_get_dreq(spi1, true)); + dma_channel_configure(spi, &c, &spi_get_hw(spi1)->dr, NULL, 8, false); + while (status != ABORTING) { // If an instruction is empty that means to stop - if (instructions[offset] == 0x00) { - if (instructions[offset + 1]) { + if (ins[offset] == 0x00) { + if (ins[offset + 1]) { i = 0; offset = ((INS_SIZE + csrs) * ad9959.channels + 1) * (i++); } else @@ -436,13 +444,17 @@ void background() { } if (ad9959.channels > 1) { - spi_write_blocking(spi1, instructions + offset + 1, - (INS_SIZE + 2) * ad9959.channels); + dma_channel_transfer_from_buffer_now( + spi, ins + offset + 1, (INS_SIZE + 2) * ad9959.channels); + // spi_write_blocking(spi1, ins + offset + 1, + // (INS_SIZE + 2) * ad9959.channels); } else { - spi_write_blocking(spi1, instructions + offset + 1, INS_SIZE); + dma_channel_transfer_from_buffer_now(spi, ins + offset + 1, + INS_SIZE); + // spi_write_blocking(spi1, ins + offset + 1, INS_SIZE); } - pio_sm_put(PIO_TRIG, 0, instructions[offset]); + pio_sm_put(PIO_TRIG, 0, ins[offset]); if (i == 1 && timing) { multicore_fifo_push_blocking(1); From 67759dff776e76fa2b950826cbaf9e9be8eeedf6 Mon Sep 17 00:00:00 2001 From: Ethan Date: Thu, 21 Jul 2022 12:15:38 -0400 Subject: [PATCH 2/4] broken attempt to write to the flash all the time --- ddssweeper/dds-sweeper.c | 58 +++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 16 deletions(-) diff --git a/ddssweeper/dds-sweeper.c b/ddssweeper/dds-sweeper.c index 61503f8..eacf335 100644 --- a/ddssweeper/dds-sweeper.c +++ b/ddssweeper/dds-sweeper.c @@ -33,7 +33,7 @@ static mutex_t status_mutex; static mutex_t wait_mutex; -#define FLASH_TARGET_OFFSET (256 * 1024) +#define FLASH_TABLE_OFFSET (256 * 1024) // STATUS flag #define STOPPED 0 @@ -163,21 +163,45 @@ void abort_run() { // Set Table Instructions // ============================================================================ +void save_to_flash(uint offset, uint8_t *buf, uint len) { + // grab 2 pages + uint prev_pages = offset / FLASH_PAGE_SIZE; + uint8_t pages[FLASH_PAGE_SIZE * 2]; + memcpy(pages, + (uint8_t *)(XIP_BASE + FLASH_TABLE_OFFSET + + prev_pages * FLASH_PAGE_SIZE), + FLASH_PAGE_SIZE * 2); + + // write instructino there + uint page_offset = offset - prev_pages * FLASH_PAGE_SIZE; + memcpy(pages + page_offset, buf, len); + + // write pages to flash + + uint32_t ints = save_and_disable_interrupts(); + // flash_range_erase(FLASH_TABLE_OFFSET, MAX_SIZE); + flash_range_program(FLASH_TABLE_OFFSET + prev_pages * FLASH_PAGE_SIZE, + pages, FLASH_PAGE_SIZE * 2); + restore_interrupts(ints); + printf("ok\n"); +} + void set_ins(uint type, uint channel, uint addr, double s0, double e0, double rate, uint div) { - uint8_t ins[30]; + uint8_t buf[30]; + uint8_t *ins = buf + 1; uint csrs = ad9959.channels == 1 ? 0 : ad9959.channels; uint offset = ((INS_SIZE + csrs) * ad9959.channels + 1) * addr + 1; uint channel_offset = (INS_SIZE + (csrs ? 2 : 0)) * channel; if (channel == 4 || channel == 5) { - instructions[offset - 1] = 0x00; + ins[-1] = 0x00; if (channel == 5) - instructions[offset] = 0xff; + ins[0] = 0xff; else - instructions[offset] = 0x00; - return; + ins[0] = 0x00; + save_to_flash(offset, ins, 2); } if (type == 0) { @@ -366,24 +390,26 @@ void set_ins(uint type, uint channel, uint addr, double s0, double e0, // setting profile pin trigger bits if (s0 <= e0 && ad9959.channels == 1) { - instructions[offset - 1] |= 0xff; + ins[-1] |= 0xff; } else if (s0 <= e0) { - instructions[offset - 1] |= (1u << channel) | (1u << (channel + 4)); + ins[-1] |= (1u << channel) | (1u << (channel + 4)); } else if (ad9959.channels == 1) { - instructions[offset - 1] |= 0x0f; + ins[-1] |= 0x0f; } else { instructions[offset - 1] &= ~(1u << (channel + 4)); - instructions[offset - 1] |= 1u << channel; + ins[-1] |= 1u << channel; } } - memcpy(instructions + offset + channel_offset, ins, INS_SIZE); + // memcpy(instructions + offset + channel_offset, ins, INS_SIZE); + save_to_flash(offset + channel_offset - 1, ins - 1, INS_SIZE); if (ad9959.channels > 1) { uint8_t csr[] = { 0x00, 0x02 | (1u << (((channel + 1) % ad9959.channels) + 4))}; - memcpy(instructions + offset + channel_offset + INS_SIZE, csr, 2); + // memcpy(instructions + offset + channel_offset + INS_SIZE, csr, 2); + save_to_flash(offset + channel_offset + INS_SIZE - 1, csr, 2); } // printf("Instruction #%d - offset %u - channel_offset %d: ", addr, offset, @@ -425,7 +451,7 @@ void background() { wait(0); triggers = 0; - uint8_t *ins = (uint8_t *)(XIP_BASE + FLASH_TARGET_OFFSET); + uint8_t *ins = (uint8_t *)(XIP_BASE + FLASH_TABLE_OFFSET); uint spi = dma_claim_unused_channel(true); dma_channel_config c = dma_channel_get_default_config(spi); @@ -504,9 +530,9 @@ void loop() { } else if (strncmp(readstring, "save", 4) == 0) { uint32_t ints = save_and_disable_interrupts(); printf("Erasing...\n"); - flash_range_erase(FLASH_TARGET_OFFSET, MAX_SIZE); + flash_range_erase(FLASH_TABLE_OFFSET, MAX_SIZE); printf("Programming...\n"); - flash_range_program(FLASH_TARGET_OFFSET, instructions, MAX_SIZE); + flash_range_program(FLASH_TABLE_OFFSET, instructions, MAX_SIZE); restore_interrupts(ints); printf("ok\n"); } @@ -524,7 +550,7 @@ void loop() { printf("ok\n"); } else if (strncmp(readstring, "load", 4) == 0) { for (int j = 0; j < MAX_SIZE; j++) { - instructions[j] = ((uint8_t *)(XIP_BASE + FLASH_TARGET_OFFSET))[j]; + instructions[j] = ((uint8_t *)(XIP_BASE + FLASH_TABLE_OFFSET))[j]; } for (int j = 0; j < 100; j++) { printf("%02x ", instructions[j]); From 4839982cba2eec80c35a5f336d528e255b637c8c Mon Sep 17 00:00:00 2001 From: Ethan Date: Thu, 21 Jul 2022 14:53:48 -0400 Subject: [PATCH 3/4] correctly save to RAMand play back --- ddssweeper/dds-sweeper.c | 69 +++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 32 deletions(-) diff --git a/ddssweeper/dds-sweeper.c b/ddssweeper/dds-sweeper.c index eacf335..676c79e 100644 --- a/ddssweeper/dds-sweeper.c +++ b/ddssweeper/dds-sweeper.c @@ -164,44 +164,45 @@ void abort_run() { // ============================================================================ void save_to_flash(uint offset, uint8_t *buf, uint len) { - // grab 2 pages - uint prev_pages = offset / FLASH_PAGE_SIZE; - uint8_t pages[FLASH_PAGE_SIZE * 2]; - memcpy(pages, + // grab 2 sectors + uint prev_sectors = offset / FLASH_SECTOR_SIZE; + uint8_t sectors[FLASH_SECTOR_SIZE * 2]; + memcpy(sectors, (uint8_t *)(XIP_BASE + FLASH_TABLE_OFFSET + - prev_pages * FLASH_PAGE_SIZE), - FLASH_PAGE_SIZE * 2); + prev_sectors * FLASH_SECTOR_SIZE), + FLASH_SECTOR_SIZE * 2); // write instructino there - uint page_offset = offset - prev_pages * FLASH_PAGE_SIZE; - memcpy(pages + page_offset, buf, len); - - // write pages to flash + uint sector_offset = offset - prev_sectors * FLASH_SECTOR_SIZE; + memcpy(sectors + sector_offset, buf, len); + // write sectors to flash uint32_t ints = save_and_disable_interrupts(); - // flash_range_erase(FLASH_TABLE_OFFSET, MAX_SIZE); - flash_range_program(FLASH_TABLE_OFFSET + prev_pages * FLASH_PAGE_SIZE, - pages, FLASH_PAGE_SIZE * 2); + flash_range_erase(FLASH_TABLE_OFFSET + prev_sectors, FLASH_SECTOR_SIZE * 2); + flash_range_program(FLASH_TABLE_OFFSET + prev_sectors * FLASH_SECTOR_SIZE, + sectors, FLASH_SECTOR_SIZE * 2); restore_interrupts(ints); - printf("ok\n"); } void set_ins(uint type, uint channel, uint addr, double s0, double e0, double rate, uint div) { - uint8_t buf[30]; - uint8_t *ins = buf + 1; + uint8_t ins[30]; uint csrs = ad9959.channels == 1 ? 0 : ad9959.channels; - uint offset = ((INS_SIZE + csrs) * ad9959.channels + 1) * addr + 1; + uint offset = (INS_SIZE * ad9959.channels + csrs * 2 + 1) * addr + 1; uint channel_offset = (INS_SIZE + (csrs ? 2 : 0)) * channel; + uint8_t pmask = *((uint8_t *)(XIP_BASE + FLASH_TABLE_OFFSET + offset - 1)); + if (channel == 4 || channel == 5) { - ins[-1] = 0x00; + pmask = 0x00; + ins[0] = 0x00; if (channel == 5) - ins[0] = 0xff; + ins[1] = 0xff; else - ins[0] = 0x00; - save_to_flash(offset, ins, 2); + ins[1] = 0x00; + save_to_flash(offset - 1, ins, 2); + return; } if (type == 0) { @@ -390,26 +391,29 @@ void set_ins(uint type, uint channel, uint addr, double s0, double e0, // setting profile pin trigger bits if (s0 <= e0 && ad9959.channels == 1) { - ins[-1] |= 0xff; + pmask |= 0xff; } else if (s0 <= e0) { - ins[-1] |= (1u << channel) | (1u << (channel + 4)); - + pmask |= (1u << channel) | (1u << (channel + 4)); } else if (ad9959.channels == 1) { - ins[-1] |= 0x0f; + pmask |= 0x0f; } else { - instructions[offset - 1] &= ~(1u << (channel + 4)); - ins[-1] |= 1u << channel; + pmask &= ~(1u << (channel + 4)); + pmask |= 1u << channel; } } + // printf("ADDR: %u - CHANNEL: %u - OFFSET: %u - COFFSET: %u - PMASK: %u\n", + // addr, channel, offset, channel_offset, offset - 1); + // memcpy(instructions + offset + channel_offset, ins, INS_SIZE); - save_to_flash(offset + channel_offset - 1, ins - 1, INS_SIZE); + save_to_flash(offset + channel_offset, ins, INS_SIZE); + save_to_flash(offset - 1, &pmask, 1); if (ad9959.channels > 1) { uint8_t csr[] = { 0x00, 0x02 | (1u << (((channel + 1) % ad9959.channels) + 4))}; // memcpy(instructions + offset + channel_offset + INS_SIZE, csr, 2); - save_to_flash(offset + channel_offset + INS_SIZE - 1, csr, 2); + save_to_flash(offset + channel_offset + INS_SIZE, csr, 2); } // printf("Instruction #%d - offset %u - channel_offset %d: ", addr, offset, @@ -480,13 +484,14 @@ void background() { // spi_write_blocking(spi1, ins + offset + 1, INS_SIZE); } - pio_sm_put(PIO_TRIG, 0, ins[offset]); + pio_sm_put(PIO_TRIG, 0, + *(uint8_t *)(XIP_BASE + FLASH_TABLE_OFFSET + offset)); if (i == 1 && timing) { multicore_fifo_push_blocking(1); } - offset = ((INS_SIZE + csrs) * ad9959.channels + 1) * (i++); + offset = (INS_SIZE * ad9959.channels + csrs * 2 + 1) * (i++); wait(0); } @@ -552,7 +557,7 @@ void loop() { for (int j = 0; j < MAX_SIZE; j++) { instructions[j] = ((uint8_t *)(XIP_BASE + FLASH_TABLE_OFFSET))[j]; } - for (int j = 0; j < 100; j++) { + for (int j = 0; j < 500; j++) { printf("%02x ", instructions[j]); } printf("\n"); From cf7472f26696e875fba6ac757f9566e89dc0ebf7 Mon Sep 17 00:00:00 2001 From: Ethan Date: Thu, 21 Jul 2022 14:59:38 -0400 Subject: [PATCH 4/4] reduce number of flash writes --- ddssweeper/dds-sweeper.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/ddssweeper/dds-sweeper.c b/ddssweeper/dds-sweeper.c index 676c79e..551b33a 100644 --- a/ddssweeper/dds-sweeper.c +++ b/ddssweeper/dds-sweeper.c @@ -186,7 +186,7 @@ void save_to_flash(uint offset, uint8_t *buf, uint len) { void set_ins(uint type, uint channel, uint addr, double s0, double e0, double rate, uint div) { - uint8_t ins[30]; + uint8_t ins[35]; uint csrs = ad9959.channels == 1 ? 0 : ad9959.channels; uint offset = (INS_SIZE * ad9959.channels + csrs * 2 + 1) * addr + 1; @@ -406,14 +406,16 @@ void set_ins(uint type, uint channel, uint addr, double s0, double e0, // addr, channel, offset, channel_offset, offset - 1); // memcpy(instructions + offset + channel_offset, ins, INS_SIZE); - save_to_flash(offset + channel_offset, ins, INS_SIZE); save_to_flash(offset - 1, &pmask, 1); if (ad9959.channels > 1) { uint8_t csr[] = { 0x00, 0x02 | (1u << (((channel + 1) % ad9959.channels) + 4))}; // memcpy(instructions + offset + channel_offset + INS_SIZE, csr, 2); - save_to_flash(offset + channel_offset + INS_SIZE, csr, 2); + memcpy(ins + INS_SIZE, csr, 2); + save_to_flash(offset + channel_offset, ins, INS_SIZE + 2); + } else { + save_to_flash(offset + channel_offset, ins, INS_SIZE); } // printf("Instruction #%d - offset %u - channel_offset %d: ", addr, offset,