/* Pass data to the CRC engine while ensuring that we don't attempt to read from an erased flash sector.  */
void crc_write_data_safely(const void *data, int count)
/* Pass data to the CRC engine while ensuring that we don't attempt to read from an erased flash sector.  */
void crc_write_data_safely(const void *data, int count)
{
    if (!((uint32_t)data >= DAPLINK_ROM_START && (uint32_t)data < (DAPLINK_ROM_START + DAPLINK_ROM_SIZE))) {
        CRC_WriteData(CRC_ENGINE, data, count);
        return;
    }
    uint32_t n = ROUND_UP((uint32_t)data, DAPLINK_SECTOR_SIZE) - (uint32_t)data;
    if (n) {
        crc_checked_write_data(data, n);
        data += n;
        count -= n;
    }
    while (count >= DAPLINK_SECTOR_SIZE) {
        n = MIN(count, DAPLINK_SECTOR_SIZE);
        crc_checked_write_data(data, n);
        data += n;
        count -= n;
    }
    if (count) {
        crc_checked_write_data(data, count);
        data += count;
        count -= count;
    }
}