Beispiel #1
0
uint8_t target_unlock_sequence(void)
{
    uint32_t val;

    // read the device ID
    if (!swd_read_ap(MDM_IDR, &val)) {
        return 0;
    }

    // verify the result
    if (val != MDM_ID) {
        return 0;
    }

    if (!swd_read_ap(MDM_STATUS, &val)) {
        return 0;
    }

    // flash in secured mode
    if (val & (1 << 2)) {
        // hold the device in reset
        swd_set_target_reset(1);

        // write the mass-erase enable bit
        if (!swd_write_ap(MDM_CTRL, 1)) {
            return 0;
        }

        while (1) {
            // wait until mass erase is started
            if (!swd_read_ap(MDM_STATUS, &val)) {
                return 0;
            }

            if (val & 1) {
                break;
            }
        }

        // mass erase in progress
        while (1) {
            // keep reading until procedure is complete
            if (!swd_read_ap(MDM_CTRL, &val)) {
                return 0;
            }

            if (val == 0) {
                break;
            }
        }
    }

    return 1;
}
uint8_t target_unlock_sequence(void) {
    uint32_t val;

    if (!swd_read_ap(MDM_IDR, &val)) {
        return 0;
    }
    // Read-only identification register that always reads as 0x001C_0000
    if (val != 0x001c0000) {
        return 0;
    }

    if (!swd_read_ap(MDM_CTRL, &val)) {
        return 0;
    }

    if (!swd_read_ap(MDM_STATUS, &val)) {
        return 0;
    }

    // flash in secured mode
    if (val & (1 << 2)) {
        target_set_state(RESET_HOLD);
        while (1) {
            if (!swd_write_ap(MDM_CTRL, 1)) {
                return 0;
            }

            if (!swd_read_ap(MDM_STATUS, &val)) {
                return 0;
            }

            if (val & 1) {
                break;
            }
        }

        while (1) {
            if (!swd_write_ap(MDM_CTRL, 0)) {
                return 0;
            }

            if (!swd_read_ap(MDM_STATUS, &val)) {
                return 0;
            }

            if (!swd_read_ap(MDM_CTRL, &val)) {
                return 0;
            }

            if (val == 0) {
                break;
            }
        }
    }

    return 1;
}