esp8266: Support up to 16 MB of flash
The ESP8266 supports up to 16 megabytes of SPI flash (source, page 12). NodeMCU has supported this much flash for about two months now, and I notice that MicroPython does not. I don't know all the nuances of the ESP8266, but from what I understand there are three things that should all encode the same size: the flash chip's ID, the SpiFlashChip structure from the SDK, and the bootloader size ID. They might not all agree about the flash chip's size, so we try to make them all agree in flashbdev.py. It looks like in the similar routine from NodeMCU, they use size IDs 8 and 9 for 8 MB and 16 MB, respectively. Setting these IDs in MicroPython seems to make mpy think that it has only 512 kB of flash, and I'm not sure why. Ignoring the bootloader size ID and only calculating flash size from the flash chip's ID seems to work, but somehow that seems like the wrong thing to do.
Long story short, I want to add support for 8 and 16 MB of flash to MicroPython, but I don't know what the right way to do it is. Any advice would be greatly appreciated.
esp8266: Making esp.flash_id() more useful
The esp8266 port exposes the SDK function that reads out the ID of the SPI flash module; it produces a result like this:
>>> esp.flash_id()
1261768
Unfortunately, that's not terribly helpful because it isn't the representation of the ID that people actually use. To get that we can use esptool:
./esptool.py -p /dev/tty.usbserial flash_id
Connecting...
Manufacturer: c8
Device: 4013
Or the code for the flash_id method could be enhanced to do some simple parsing and spit out the same thing. The question I have is whether this is useful. The different ESP8266 modules do have different sized flash, and it might be handy to know how big they are, but this result doesn't directly tell you that; instead, you have to search for the result, or know that you can go grab a big file of flash IDs (http://code.coreboot.org/svn/flashrom/trunk/flashchips.h) and grep through it, and then look up the manufacturer's datasheet. We could work around that by building in a table of the most-commonly-used SPI flash modules, but I could see that getting out of hand pretty quickly!
It would be nicer to have an SDK function that simply told you how big the flash is. There is a new SDK function in 1.1.0 called system_get_flash_size_map but it seems to report what was configured when the software was linked, not what is actually on the board. I added a simple wrapper for it and tested on modules with 512 KB, 2 MB and 4 MB, and it always reported map 0, which is 512 KB. Assuming I wrote the wrapper correctly, that is not helpful ;)
So the questions are these, IMO:
- Is it useful to be able to retrieve the flash ROM size from within MicroPython, or will people just look up the spec sheet or use esptool?
- Is the current flash_id method sufficient?
- If not, should it parse the answer to display the manufacturer and device codes?
- Or should it have a lookup table of known flash chips, so it can also report the size?