Add SPI FLash to B_L072Z_LRWAN1

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
Post Reply
lejibxl
Posts: 3
Joined: Thu Nov 14, 2019 8:53 am

Add SPI FLash to B_L072Z_LRWAN1

Post by lejibxl » Tue Apr 14, 2020 2:16 pm

Hello,
*sorry for the quality of my English, it's not my mother language.*

as the B_L072Z_LRWAN1 card (µC stm32l072) has too little internal flash memory to make the micropython work,
I was thinking of adding an SPI FLASH (W25Q64) to get around this problem.
If followed the advice of "mcauser" on this forum and I added the info as in "https://github.com/mcauser/WEACT_F411CE ... figboard.h".
But during compilation I get the following error message.
storage.c: In function 'storage_systick_callback':
storage.c:86:13: error: 'NVIC_Type {aka struct <anonymous>}' has no member named 'STIR'
NVIC->STIR = FLASH_IRQn;
^~
../../py/mkrules.mk:64: recipe for target 'build-DILDO/storage.o' failed
make: *** [build-DILDO/storage.o] Error 1
I can't find anything about it.
Can you help me ?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Add SPI FLash to B_L072Z_LRWAN1

Post by jimmo » Tue Apr 14, 2020 11:48 pm

The storage driver triggers the flash IRQ from a periodic systick which it uses to do things like flush the cache. See how BDEV_IOCTL_IRQ_HANDLER is used in (e.g. spibdev.c)

On the STM32FL0 (i.e. Cortex M0), the way you trigger the flash IRQ is different. On the other families (e.g. M4 and higher) you use NVIC->STIR (interrupt controller -> software trigger interrupt register). Unfortunately don't know off the top of my head what the M0 equivalent is... perhaps try using NVIC_SetPendingIRQ(FLASH_IRQn) (which uses ISPR instead or STIR).

lejibxl
Posts: 3
Joined: Thu Nov 14, 2019 8:53 am

Re: Add SPI FLash to B_L072Z_LRWAN1

Post by lejibxl » Wed Apr 15, 2020 12:03 pm

jimmo ,
thank you very much for your answer as well as the other posts you wrote on this forum which helped me to understand how micropython works.
But after all this the question I ask myself:
  • has anyone ever managed to add a SPI FLASH to a µC stm32l072?
  • Is it simply possible?
:roll:

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Add SPI FLash to B_L072Z_LRWAN1

Post by jimmo » Wed Apr 15, 2020 1:00 pm

lejibxl wrote:
Wed Apr 15, 2020 12:03 pm
thank you very much for your answer as well as the other posts you wrote on this forum which helped me to understand how micropython works.
No worries :)
lejibxl wrote:
Wed Apr 15, 2020 12:03 pm
has anyone ever managed to add a SPI FLASH to a µC stm32l072?
If they have, then I haven't seen any PRs for it. Note that the internal flash is not supported either at the moment (although fixing that should be very similar to what you're doing to support external flash).
lejibxl wrote:
Wed Apr 15, 2020 12:03 pm
Is it simply possible?
I assume you're doing this because you want to support a filesystem on the external spiflash. There's no reason why this shouldn't work, it's just making a block device around the spiflash, no CPU support necessary other than the SPI peripheral -- in fact you could implement all of this entirely in Python (it's just implemented in the firmware for efficiency).

But if what you want is to add additional program space (i.e. to load larger firmware), then I don't think that's possible (the L072 doesn't have XIP).

lejibxl
Posts: 3
Joined: Thu Nov 14, 2019 8:53 am

Re: Add SPI FLash to B_L072Z_LRWAN1

Post by lejibxl » Fri Apr 24, 2020 9:24 am

Hello Jimmo, thank you so much for this information.

But I still have a lot of open questions.
  1. The first one is why Damien P. George created a repository for the card "B-L072Z-LRWAN1".While it seems impossible to make the micropython work (you still have to add a LoRaWAN stack with all dependencies (float, ...) and a phyton program for the application). I understand that the internal flash is too small, but why didn't you provide a possibility to add an SPI Flash? When I see the quality of this whole project, there is certainly a goal, but this is something I don't understand.
  2. I saw that CircuitPython has finalized the use of a Flash SPI with a Cortex-M0: https://github.com/adafruit/circuitpyth ... m0_express
    but they don't seem to use storage.c which seems to be a combination of internal flash and external SPI flash.
  3. Your proposal to do this in python, does this mean I have to modify the boot.py to include the SPI mount command (https://docs.micropython.org/en/latest/ ... re-spi-bus) and da run main.py on this SPI-FLASH?
  4. Your other proposal to use NVIC_SetPendingIRQ(FLASH_IRQn) do I have to do this in the bdev.c file? ??
Sorry for all the questions, but as you can see, I'm a little confused. If you could just get me on track

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Add SPI FLash to B_L072Z_LRWAN1

Post by jimmo » Fri Apr 24, 2020 12:39 pm

lejibxl wrote:
Fri Apr 24, 2020 9:24 am
The first one is why Damien P. George created a repository for the card "B-L072Z-LRWAN1".While it seems impossible to make the micropython work (you still have to add a LoRaWAN stack with all dependencies (float, ...) and a phyton program for the application). I understand that the internal flash is too small, but why didn't you provide a possibility to add an SPI Flash? When I see the quality of this whole project, there is certainly a goal, but this is something I don't understand.
Perhaps that was (or is) on the TODO list, but there are lots of other priorities. Compared to getting MicroPython running at all on the STM32L0 family, these are all relatively minor things that need to be done. At least what's there is a good base for open source contributors to build on (who might not have been as comfortable doing the initial L0 port themselves). And I guess it's just a case of "it's valuable as a base for other people to work on, so might as well share it".

Edited to add: Another interesting piece of background -- MicroPython works on a lot of different devices, which means a lot of development boards for the maintainers to own. And so if you're going to buy development boards (which are almost as expensive to ship as the boards themselves) you might as well get interesting ones (which this one certainly is). I believe this board was used to develop the L0 port (which is useful for far more devices than just the LRWAN1 board).

The other thing (that already works on this board) is to use frozen modules. It means you can still write your code (and the LoRa driver) in Python, but the bytecode is compiled into the firmware.

I actually have one of these boards, and would be keen to get it working myself but have too many other things to do! Would be interested to hear if you make any progress.
lejibxl wrote:
Fri Apr 24, 2020 9:24 am
I saw that CircuitPython has finalized the use of a Flash SPI with a Cortex-M0: https://github.com/adafruit/circuitpyth ... m0_express
but they don't seem to use storage.c which seems to be a combination of internal flash and external SPI flash.
CircuitPython and MicroPython only really share the core VM -- all the hardware drivers and peripheral support is completely reimplemented from scratch in CircuitPython.

The background here about what's happening in storage.c is that, in MicroPython, it periodically flushes the block cache out to the flash. It uses the flash IRQ to do this at low priority. This is why it's using the NVIC->STIR register to do a software triggered interrupt. (This works on everything except M0, but I think the alternative I suggested might work).

I imagine CircuitPython have done something similar, but I'm not familiar enough with their hardware drivers to know off the top of my head.
lejibxl wrote:
Fri Apr 24, 2020 9:24 am
Your proposal to do this in python, does this mean I have to modify the boot.py to include the SPI mount command (https://docs.micropython.org/en/latest/ ... re-spi-bus) and da run main.py on this SPI-FLASH?
You could write a block device in Python (that uses machine.SPI, yes), and then mount it with os.mount.
See https://docs.micropython.org/en/latest/ ... ystem.html for all the info about this.
lejibxl wrote:
Fri Apr 24, 2020 9:24 am
Your other proposal to use NVIC_SetPendingIRQ(FLASH_IRQn) do I have to do this in the bdev.c file? ??
Nope, this replaces the line that you had the compile error on in storage.c:

NVIC->STIR = FLASH_IRQn;

Post Reply