diff --git a/gp2040ce_bintools/pico.py b/gp2040ce_bintools/pico.py index 301093a..ed0f9e2 100644 --- a/gp2040ce_bintools/pico.py +++ b/gp2040ce_bintools/pico.py @@ -13,6 +13,7 @@ PICO_VENDOR = 0x2e8a PICO_PRODUCT = 0x0003 PICOBOOT_CMD_STRUCT = ' None: + """Erase a section of flash memory on a Pico in BOOTSEL mode. + + Args: + out_endpoint: the out direction USB endpoint to write to + in_endpoint: the in direction USB endpoint to read from + location: memory address of where to start erasing from + size: number of bytes to erase + """ + # set up the data + pico_token = 1 + command_size = 8 + transfer_len = 0 + payload = struct.pack(PICOBOOT_CMD_STRUCT + PICOBOOT_CMD_ERASE_SUFFIX_STRUCT, + PICO_MAGIC, pico_token, PICO_COMMANDS['ERASE'], command_size, transfer_len, + location, size) + logger.debug("ERASE: %s", payload) + out_end.write(payload) + _ = in_end.read(256) + + def exit_xip(out_end: usb.core.Endpoint, in_end: usb.core.Endpoint) -> None: """Exit XIP on a Pico in BOOTSEL. diff --git a/tests/test_pico.py b/tests/test_pico.py index 1d8194f..481d076 100644 --- a/tests/test_pico.py +++ b/tests/test_pico.py @@ -52,6 +52,16 @@ def test_exit_xip(): end_in.read.assert_called_once() +def test_erase(): + """Test that we can send a command to erase a section of memory.""" + end_out, end_in = mock.MagicMock(), mock.MagicMock() + pico.erase(end_out, end_in, 0x101FE000, 8192) + + payload = struct.pack('