Skip to content

stm32 / spiflash: Support defining exact flash chip(s) connected. #12722

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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

andrewleech
Copy link
Contributor

Different spiflash brands / models can require slightly different settings to use.
This PR allows a board profile to declare the model spiflash being used directly.

It's re-using a header of spiflash definitions already available in drivers/memory/external_flash_device.h, however new ones could be added inline in mpconfigboard.h if needed.

This started as an additional commit in #12512 and also provides functionality first presented in #11932

@andrewleech andrewleech force-pushed the stm32_qspi_cr_write branch 3 times, most recently from 01d336f to b3e145b Compare October 19, 2023 00:54
mp_soft_qspi_qread(self, len, dest);
if (cmd == CMD_RD_SFDP) {
// cmd, address and data on 1 line.
// 3 addr bytes, 1 dummy byte (8 dummy cycles x 1 line)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit strange, the function here is read_cmd_qaddr_qdata which explicitly asks for 4-lines of addr and data. But then this special case is using 1-line for addr and data.

So either:

  • add a new function called read_cmd_addr_data which is used for commands like this one
  • change this function to be called read_cmd_addr_data and make it select the width of addr and data based on the cmd value, which is what you've done here

The first option feels more correct, because the bus shouldn't care about the values going over it, it should just do as instructed and write the data over the requested number of data lines.

The first option also means softqspi.c and qspi.c don't need to know about (or have a definition of) CMD_RD_SFDP.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did start going down a path of passing in args to read_cmd_qaddr_qdata to select the required settings... the problem I ran into was that it's not just the number of lines, it's also the dummy cycles. And if both those settings are exposed, the alternate-bytes should be as well arguably. And it all got quite messy and I reverted back to this.

If I ignore the other settings and just base the difference on the number of lines used, passing that in as a function arg is less code space use than duplicating the function and adding the extra entry to the mp_qspi_proto_t.
To be fair these existing functions have dummy/AB settings all hard-coded apparently to suit the commands that just happen to be used by spiflash, so continuing down that path doesn't seem too bad until someone needs them to be more flexible for a new reason.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, I also noticed last time that the new octospi driver in stm only support 1 and 2 line modes currently, auto-selected by which pins are defined...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I agree it's not all consistent and could be more flexible. You're right that adding a new function takes up more code space, compared to reusing an existing one.

How about calling it read_cmd_addr_data() and passing in the number of lines to use? It could just be an enum with only 2 options to start with, eg:

enum qspi_tranfer_mode {
    QSPI_TRANSFER_CMD_ADDR_DATA,
    QSPI_TRANSFER_CMD_QADDR_QDATA,
};

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm just finishing up implementing it as an uint8_t lines arg which gets passed 1 or 4 so far.... the enum approach gives better flexibility in future to make a labelled mode that changes different settings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The enum approach has been added and looks like it works well for me thanks.

if (ret != 0 || sfdp[0] != *(uint32_t *)sfdp_header) {
printf("mp_spiflash: sfdp not supported\n");
printf("Set MICROPY_HW_SPIFLASH_DEVICES or MICROPY_HW_BDEV_SPIFLASH_SIZE_BYTES");
printf("jedec ids: 0x%x 0x%x 0x%x\n", jedec_ids[0], jedec_ids[1], jedec_ids[2]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use mp_printf(MICROPY_ERROR_PRINTER, ...).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I did initially think that but saw printf had already been used in this file. I'll change it everywhere to use the preferred approach.

@andrewleech andrewleech force-pushed the stm32_qspi_cr_write branch 5 times, most recently from f5bd1e3 to 8003704 Compare November 23, 2023 22:13
@andrewleech andrewleech force-pushed the stm32_qspi_cr_write branch 3 times, most recently from 860cfba to 66c719d Compare January 29, 2024 04:07
@projectgus
Copy link
Contributor

This is an automated heads-up that we've just merged a Pull Request
that removes the STATIC macro from MicroPython's C API.

See #13763

A search suggests this PR might apply the STATIC macro to some C code. If it
does, then next time you rebase the PR (or merge from master) then you should
please replace all the STATIC keywords with static.

Although this is an automated message, feel free to @-reply to me directly if
you have any questions about this.

Andrew Leech and others added 12 commits April 4, 2024 14:24
Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Add some more details to the struct definition.

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Fixes support for 4 line communication with chip, but
maintains fallback to 2 or 1 line if hardware needs it.

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
The Serial Flash Discoverable Parameters (SFDP) is a jedec standard
interface on (most/newer) spiflash chips that allows querying the
flash size (amongst other things).

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
@andrewleech andrewleech force-pushed the stm32_qspi_cr_write branch from a4456ee to 3033161 Compare April 5, 2024 00:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants