Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rouding down "pagesToWrite" #3

Closed
Donelook opened this issue Jan 30, 2024 · 3 comments
Closed

Rouding down "pagesToWrite" #3

Donelook opened this issue Jan 30, 2024 · 3 comments

Comments

@Donelook
Copy link

Donelook commented Jan 30, 2024

Hello,
i met issue in flashing w25q32 with FT232H when using your program. I have loaded program to my FLASH but it has not worked properly. I was searching so many possible mistake or reason. And i have got it - integer rouding down count of pages. Chatbot has written fix for it that you see below. Program with this fix work properly. Thank you for your program - is very helpful

private void btnWrite_Click(object sender, EventArgs e)
{
    OpenFileDialog diag = new() { Filter = "BIN files (*.bin)|*.bin|All files (*.*)|*.*" };
    if (diag.ShowDialog() != DialogResult.OK)
        return;
    byte[] fileBytes = File.ReadAllBytes(diag.FileName);

    SpiFlashManager? com = GetFlashMan();
    if (com is null)
        return;

    Progress($"Erasing chip...");
    com.Erase();

    int pagesToWrite = (int)Math.Ceiling((double)fileBytes.Length / 256);
    for (int i = 0; i < pagesToWrite; i++)
    {
        double percent = (double)i / pagesToWrite * 100;
        Progress($"Writing page {i} of {pagesToWrite} ({percent:0.00}%).", percent);
        byte[] pageBytes = new byte[256];
        int bytesRemaining = fileBytes.Length - i * 256;
        int bytesToCopy = Math.Min(256, bytesRemaining);
        Array.Copy(fileBytes, i * 256, pageBytes, 0, bytesToCopy);
        com.WritePage(i, pageBytes);
    }

    com.Disconnect();
    Progress($"Disconnected.");
}

image

@swharden
Copy link
Owner

Hi @Donelook, thanks for reporting this issue! It took me several weeks to get to this one 😅

Do you still have your setup available so if I make the change you can test whether it works? I don't have a w25q32 on hand myself...

@swharden
Copy link
Owner

Adding context, the error may be here:

int pagesToWrite = fileBytes.Length / 256;

And the suggested solution is changing that one line to be:

int pagesToWrite = (int)Math.Ceiling((double)fileBytes.Length / 256);

@swharden
Copy link
Owner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants