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

Is XOR operator an error on W25Qxx_ReadStatus? #1

Open
daguirrem opened this issue Apr 9, 2023 · 2 comments
Open

Is XOR operator an error on W25Qxx_ReadStatus? #1

daguirrem opened this issue Apr 9, 2023 · 2 comments

Comments

@daguirrem
Copy link
Contributor

Hello, first of all thanks for the lib!

I am implementing this lib on my project, but i have a question about W25Qxx_ReadStatus & W25Qxx_isStatus functions because i think at first it doesn't work as it should on my project. When i try to read or write the flash, it internally checks if the memory is at IDLE status, but it returns to me busy status when the status registers of W25Qxx are "idle".

The posible status of the memory are:

typedef enum {
    W25Qxx_STATUS_IDLE             = 0x01,
    W25Qxx_STATUS_BUSY             = 0x02,
    W25Qxx_STATUS_SUSPEND          = 0x04,
    W25Qxx_STATUS_BUSY_AND_SUSPEND = 0x08
} W25Qxx_STATUS;

and the W25Qxx_ReadStatus function is:

uint8_t W25Qxx_ReadStatus(void)   /* Read current chip running status */
{
    uint8_t ret = 0;

    ret |= W25Qxx_RBit_BUSY();
    ret |= W25Qxx_RBit_SUS()<<1;

    return (2 ^ ret);
}

So "ret" variable have 4 values:

  • 0: IDLE
  • 1: BUSY
  • 2: SUSPEND
  • 3: BUSY & SUSPEND

But if "ret" is 0 value (idle) the xor operator (^) returns 2 (2^0 = 2) and the status returned will be "W25Qxx_STATUS_BUSY".

If xor is remplaced with a pow operator the values will be:

  • 2 ** 0 = 1 (W25Qxx_STATUS_IDLE)
  • 2 ** 1 = 2 (W25Qxx_STATUS_BUSY)
  • 2 ** 2 = 4 (W25Qxx_STATUS_SUSPEND)
  • 2 ** 3 = 8 (W25Qxx_STATUS_BUSY_AND_SUSPEND)

Where ** is pow operator

iammingge added a commit that referenced this issue Apr 10, 2023
fix W25Qxx_ReadStatus xor operator #1
@iammingge
Copy link
Owner

iammingge commented Apr 10, 2023

Hi, as an open source author, thank you very much for using the library in your project and thanks again for the problems you found. You are right about the code "return (2 ^ ret); " in the function W25Qxx_ReadStatus. Not only can you use the pow function, but if you are using a microcontroller with a shift instruction you can also use the following :

uint8_t W25Qxx_ReadStatus(void)		/* Read current chip running status */																																
{
       uint8_t ret = 0;

       ret |= W25Qxx_RBit_BUSY();
       ret |= W25Qxx_RBit_SUS()<<1;

       return (1 << ret);              /* return (pow(2, ret)) */
}

Finally, thanks again, and if you find any other problems or have better ideas, I will try to reply and discuss them as soon as possible. 😊🤞

@daguirrem
Copy link
Contributor Author

My pleasure! and it's true, you should implement a shift version as default, it is more optimized!

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