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

[sw/bootloader] add TWI boot option #1108

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

LukasP46
Copy link
Contributor

Add ability to read program from TWI (also known as I2C) memory like common EEPROMs with one address byte.

@LukasP46
Copy link
Contributor Author

The bootloader gets to big if all options are enables. How should we handle this? Maybe disable TWI by default but add an additional test where SPI is disabled instead?

(I'm gonna extend the documentation when we resolved this)

@stnolting
Copy link
Owner

Add ability to read program from TWI

Nice! Thank you very much!

The bootloader gets to big if all options are enables.

Oh I know this problem... 🙈
How much more ROM do you need for your modified bootloader?

How should we handle this? Maybe disable TWI by default but add an additional test where SPI is disabled instead?

I was thinking about rewriting the bootloader in plain assembly... But to be honest, I'm too lazy for that... 😅

So yes, we could disable the TWI option by default and let the user decide if this is a relevant feature.

@stnolting stnolting added enhancement New feature or request SW Software-related labels Dec 3, 2024
@LukasP46
Copy link
Contributor Author

LukasP46 commented Dec 4, 2024

How much more ROM do you need for your modified bootloader?

Memory utilization:
   text    data     bss     dec     hex filename
   4456       0       8    4464    1170 main.elf

I was thinking about rewriting the bootloader in plain assembly... But to be honest, I'm too lazy for that... 😅

too lazy not crazy
Surely it can be done, but it sounds really time consuming and rather hard to debug (and contribute to).

I actually use the C ISA extension so the bootloader is always small enough, but I guess I'll change the default and then the user can enable it if needed. I would actually suggest changing the warning as well and recommend recompiling the bootloader for each processor to enable a potentially smaller bootloader, what do you think?

@LukasP46
Copy link
Contributor Author

LukasP46 commented Dec 4, 2024

While writing to the TWI memory via the bootloader is not planned, I will probably upload a Python script to flash via an USB I2C adapter.

@LukasP46 LukasP46 marked this pull request as ready for review December 4, 2024 09:59
@stnolting
Copy link
Owner

I actually use the C ISA extension so the bootloader is always small enough

That's a good solution! But I would recommend to provide the pre-built bootloader image compiled with the minimal ISA options only - so it is compatible to any ISA configuration.

I would actually suggest changing the warning as well and recommend recompiling the bootloader for each processor to enable a potentially smaller bootloader, what do you think?

Which warning?

Sure, we could add a note/tip to the according section of the documentation for this.

Btw, we have already enabled link-time-optimization and Os as compile-time switches. I am not sure if there is anything more we could do to further shrink the bootloader.

However, maybe it is worth a try to compile the bootloader with the absolute minimal ISA configuration which is rv32e. The E ISA extension only provides 16 x registers. Maybe the overhead of register spilling could be reduced by this?! 🤔

While writing to the TWI memory via the bootloader is not planned, I will probably upload a Python script to flash via an USB I2C adapter.

I think we could re-use parts of the "SPI write" code?

I just scanned your code briefly, but I think you are using a fixed number of address bytes for accessing the TWI flash, right? Maybe we could also make this configurable just like we did for the SPI accesses.

@stnolting stnolting self-assigned this Dec 10, 2024
@LukasP46
Copy link
Contributor Author

That's a good solution! But I would recommend to provide the pre-built bootloader image compiled with the minimal ISA options only - so it is compatible to any ISA configuration.

I agree, the pre-built should be compatible with any (but E currently) ISA configuration. But we could encourage the user to build the bootloader to their needs and ISA.

Which warning?

This one in bootloader.c:

/**********************************************************************//**
 * Sanity check: Base RV32I ISA only!
 **************************************************************************/
#if defined __riscv_atomic || defined __riscv_a || __riscv_b || __riscv_compressed || defined __riscv_c || defined __riscv_mul || defined __riscv_m
  #warning In order to allow the bootloader to run on *any* CPU configuration it should be compiled using the base rv32i ISA only.
#endif

But actually I'm fine with it, because the first part says to allow, which actually implies if you need it...

Btw, we have already enabled link-time-optimization and Os as compile-time switches. I am not sure if there is anything more we could do to further shrink the bootloader.

Probably not? -Oz is even more aggressive, but is currently the same size as -Os.

However, maybe it is worth a try to compile the bootloader with the absolute minimal ISA configuration which is rv32e. The E ISA extension only provides 16 x registers. Maybe the overhead of register spilling could be reduced by this?!

I'll look into it. This would also have the advantage that the bootloader would actually work on all ISA configurations.

I think we could re-use parts of the "SPI write" code?

Probably yes? Unfortunately I don't have a use case at the moment, but I keep it in mind. Probably not in this PR though. Some other bootloader extensions have a higher priority, you'll see :)

I just scanned your code briefly, but I think you are using a fixed number of address bytes for accessing the TWI flash, right? Maybe we could also make this configurable just like we did for the SPI accesses.

I agree, I'll add that one!

@LukasP46 LukasP46 marked this pull request as draft December 11, 2024 08:54
@stnolting
Copy link
Owner

I agree, the pre-built should be compatible with any (but E currently) ISA configuration. But we could encourage the user to build the bootloader to their needs and ISA.

👍

This one in bootloader.c:

Oh, right. I forgot about that...

I think we could remove that. It does not capture all possible ISA extensions. So basically it is useless.

Probably not? -Oz is even more aggressive, but is currently the same size as -Os.

Interesting! I was not aware of that option. I'll look it up.

I'll look into it. This would also have the advantage that the bootloader would actually work on all ISA configurations.

I just tested that and it saves more than 100 bytes (yeay!).

-> #1118

I agree, I'll add that one!

Cool, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request SW Software-related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants