TinyFPGA-BX Won't Program Anymore


#10

I have been thinking about what I might have done to cause this issue. I was wondering if pushing the button on the board while the board is being programmed might have caused some of the metadata to be overwritten.

This is just a theory, but I hope it may help you figuring this out.


#11

Your theory can make sense.
I don’t think that I’ve done exactly that, but it is possible that I have started tinyprog before releasing the reset button.


#12

Hi Luke,
I refresh this page every couple of hours to see if the utility to re-write the metadata is available.
I hope you will find some spare time to write it.
Cheers!


#13

Hi,

Perhaps we can try to fix this as community. It should be not be very hard and will empower us!
-There is a tool to read the data
-There is a tool to write (generic) data
-We can try and figure out the small changed needed for this.


#14

I did start having a look at this. I previously made some changes to tinyprog to allow it to read data - see Experiments with BX programmer and bootloader

I was thinking of adding to that, the ability to read and dump the metadata to a file. That would be useful in diagnosing the problem as the -m flag only currently works when the metadata is valid json. It would also help in developing the utility.

To do that looks easy - it would be like the --read_image flag I added, but would use read_security_register_page rather than read.

There are already erase_security_register_page and and program_security_register_page methods in init.py.

So adding a --write_metadata flag to tinyprog shouldn’t be too hard. We would need to format the json and write it to the three security pages. There seems to be optional metadata in normal pages as well, but I am not sure that that is used.

Most of the metadata is fixed, other than a uuid and the version numbers.


#15

So, I implemented the flag (-z, --read_security_image) to dump the security data, see -

And here is the result:

python __main__.py -ztest.bin

TinyProg CLI
------------
Using device id 1d50:6130
Only one board with active bootloader, using it.
Reading security page image to file test.bin

od -Ax -c test.bin

000000 { " b o a r d m e t a " : { " n
000010 a m e " : " T i n y F P G A B
000020 X " , " f p g a " : " i c e 4 0
000030 l p 8 k - c m 8 1 " , " h v e r
000040 " : " 1 . 0 . 0 " , " u u i d "
000050 : " 5 e 2 0 5 4 0 7 - 2 f 6 d -
000060 4 a 7 a - b 7 d f - 1 b 1 d 2 2
000070 8 6 f a 8 d " } } \0
000080
*
0000f0 {
000100 " b o o t m e t a " : { " b o o
000110 t l o a d e r " : " T i n y F P
000120 G A U S B B o o t l o a d e
000130 r " , " b v e r " : " 1 . 0 . 1
000140 " , " u p d a t e " : " h t t p
000150 s : / / t i n y f p g a . c o m
000160 / u p d a t e / t i n y f p g a
000170 - b x " , " a d d r m a p " : {
000180 " b o o t l o a d e r " : " 0 x
000190 0 0 0 a 0 - 0 x 2 8 0 0 0 " , "
0001a0 u s e r i m a g e " : " 0 x 2 8
0001b0 0 0 0 - 0 x 5 0 0 0 0 " , " u s
0001c0 e r d a t a " : " 0 x 5 0 0 0 0
0001d0 - 0 x 1 0 0 0 0 0 " } } } \0
0001e0
0001f0 377 377
000200 377 377 377 377 377 377 377 377 377 377 377 377 377 377 377 377
*
0002f0 377 377 377 377 377 377 377 377 377 377 377 377 377
0002fd


#16

It looks like just security register pages 1 and 2 are used and are filled with binary zero after the json data. The opening “{” for the second page is at address 0x100 not 0xf0 (i.e the stat of the second page) as you can see if you read the pages one by one. The third page is filled with 0xff, possibly because it has never been written.

I don’t know how the uuid is formed.


#17

Another solution that should work is to change tinyprog so that if it fails to read the metadata, it returns default metadata. That should make the device usable without writing to it, and would be at least a temporary solution until @lukevalenty writes the metadata utility.


#18

I have investigated this a bit more and I don’t think the problem is the metadata in the security pages.

As the update process does not touch the security pages, it seemed unlikely that that was the problem.

The code to get the metadata does:

    > meta_roots = (
        [self._parse_json(self.prog.read_security_register_page(p).replace(b"\x00", b"").replace(b"\xff", b"")) for p in [1, 2, 3]] +
        [self._parse_json(self.prog.read(int(math.pow(2, p) - (4 * 1024)), (4 * 1024)).replace(b"\x00", b"").replace(b"\xff", b"")) for p in [17, 18, 19, 20, 21, 22, 23, 24]]
    )

This concatenates roots from security pages and normal pages. As the normal pages are not currently used, they are not needed and you can comment them out like:

      > meta_roots = (
        [self._parse_json(self.prog.read_security_register_page(p).replace(b"\x00", b"").replace(b"\xff", b"")) for p in [1, 2, 3]] # +
       # [self._parse_json(self.prog.read(int(math.pow(2, p) - (4 * 1024)), (4 * 1024)).replace(b"\x00", b"").replace(b"\xff", b"")) for p in [17, 18, 19, 20, 21, 22, 23, 24]]
    )roots = (
        [self._parse_json(self.prog.read_security_register_page(p).replace(b"\x00", b"").replace(b"\xff", b"")) for p in [1, 2, 3]] +
        [self._parse_json(self.prog.read(int(math.pow(2, p) - (4 * 1024)), (4 * 1024)).replace(b"\x00", b"").replace(b"\xff", b"")) for p in [17, 18, 19, 20, 21, 22, 23, 24]]
    )

and metadata is still returned fine.

However if any of those pages have numeric data that is not 0x00 or 0xff, you will get the error you reported. You can test this by just inserting " + [0] ", and you get the error you reported.

So it looks like one of those pages got data written to it during the failed update. If you comment out the read of those normal pages, as above, you should avoid the error and it should return metadata, unless your failed update caused other problems.


Simple first code for BX
#19

Hi Lawrie,

Thank you for the info.
However, I’m still not able to bring back to life my TinyFPGA BX.
I’m using the tinyprog from windows.
If I use the tinyfpgab script, it returns “No port was specified and no active bootloaders found”, and if I use it with the “-c” option it returns “No active bootloaders found”.
Also, I cannot find where the the lines of code that you propose to comment are.


#20

I did not think tinyfpgab worked with the BX.

The code I mentioned is at https://github.com/lawrie/TinyFPGA-Bootloader/blob/master/programmer/tinyprog/__init__.py

You should be add to run that on Windows. Put __init__.py in a directory called tinyprog and comment out the code as shown. Put main.py in the directory above tinyprog and then run:

python __main__.py -m


#21

I just did this on Windows are got:

PS C:\Users\lawri> python __main__.py -m
[
{
“boardmeta”: {
“name”: “TinyFPGA BX”,
“fpga”: “ice40lp8k-cm81”,
“hver”: “1.0.0”,
“uuid”: “5e205407-2f6d-4a7a-b7df-1b1d2286fa8d”
},
“bootmeta”: {
“bootloader”: “TinyFPGA USB Bootloader”,
“bver”: “1.0.1”,
“update”: “https://tinyfpga.com/update/tinyfpga-bx”,
“addrmap”: {
“bootloader”: “0x000a0-0x28000”,
“userimage”: “0x28000-0x50000”,
“userdata”: “0x50000-0x100000”
}
},
“port”: “COM5”
}
]

I then added “+ [0]” to the meta_roots and got:

PS C:\Users\lawri> python __main__.py -m
Traceback (most recent call last):
File “main.py”, line 417, in
main()
File “main.py”, line 259, in main
m[“port”] = str(port)
TypeError: ‘int’ object does not support item assignment

And

python __main__.py -l

TinyProg CLI
------------
Using device id 1d50:6130
Only one board with active bootloader, using it.
Boards with active bootloaders:

    COM5: No metadata

#22

Hi Lawrie,

Thanks for your help.
I was able to run the script with the new “-z” function that you have added.
The result is this: (I don’t have “od” installed)

image

Then, I have commented the line 170 of the init.py.
However, the result is exactly the same when I try to program the FPGA.


#23

Would it be possible to modify the lines 179, 182 and 185, and write there the data that the script was supposed to read correctly?


#24

That confirms you still have the metadata in security pages, as I thought.

You may not be using the init.py that you edited - I hit that problem. Put a print statement in it to make sure you are getting the edited version. Python picks up the system version of tinyprog not the one in the current directory. To fix that, I put __init__.py in a tinyprog directory below __main__.py and then it finds the edited version.


#25

Yes, you could do that and avoid using the metadata at all, but I am still convinced the problem is what I said it was, until I get evidence to the contrary.


#26

Your hypothesis that I was running a different file than the one that I was editing was correct!
Now it returns other errors in lines 182 and 188.
“TypeError: ‘NoneType’ object is not subscriptable”


#27

Actually I have done a mistake, and have commented the 2 lines 169 and 170.
Commenting only the 170 it works perfectly!


#28

Thank you very much Lawrie!
I was stuck with this for a few days and your help was very useful!
Finally I managed to program again the TinyFPGA-BX!
Have a nice day.


#29

I suspect that code on line 170 is incorrect. I could not work out what it was doing when I first saw it. I suspected it was trying to find the last page on flash memory for different flash memory sizes. But it might just be wrong and if it accidentally finds a page with non-zero data, it can cause the problem.