Skip to main content
Topic: slow read M25P16 (Read 28175 times) previous topic - next topic

slow read M25P16

(Edited with pictures)

Hi,

new to buspirate and trying to figure out where the bottlenecks are, mostly out of curiosity. I did I read out from a ST M25P16 (2Mbyte) through flashrom. First attempts took 24minutes but it seems like my virtualbox under os x was the first low hanging fruit. After switching to a ubuntu machine I shaved that time to around 10minutes. I looked at cs and the clock with the scope and have some questions


First looking at chipselect, I see that it is only about a tenth of the time that we seem to enable the chip. What is the main bottleneck here?


Secondly I triggered on the clock (clock yellow, cs blue) and I see that it seems like we transfer 16bytes for each cs. I checked the code for flashrom and I see that the read instruction is 4 bytes and the we read 12 bytes. Why is the limits setup like that? could we extend the readcycles?

Flashrom seems to suggest that    30k,250k,1M,2M,2.6M,4M and 8M as possible clock speeds. I have tested with 8M but the clock seem to fall back to 125k. 1M seems to be 1M though.


Using buspirate v4 with latest firmware and the latest daily build of flashrom

HiZ>i
Bus Pirate v4
Firmware v6.1 r1676
DEVID:0x1019 REVID:0x0003 (24FJ256GB106 A5)
dangerousprototypes.com
HiZ>
Thanks for using picocom
thommy@kelly:~$ flashrom --version
flashrom v0.9.5.2-runknown on Linux 3.0.0-17-generic-pae (i686), built with libpci 3.1.7, GCC 4.6.1, little endian
flashrom is free software, get the source code at ___

Re: slow read M25P16

Reply #1
Found you SPI page which had the write then read command listed. So I did a quick hack (see below) where I used that mode instead of Bulk SPI. Seems to be down to a couple of seconds above 1minute, which kind of also puzzles me. I'm calculating the max speed in my case at 115200,1 start and 1 stop to 2048*1024/(115200/10)/60~3minutes (what do I miss??). Here is the run:

Code: [Select]
date;./flashrom -p buspirate_spi:dev=/dev/ttyACM0,spispeed=1M   -r spidmp10;date
Wed Apr  4 08:47:47 CEST 2012
flashrom v0.9.5-r1504 on Linux 3.0.0-17-generic-pae (i686), built with libpci 3.0.0, GCC 4.4.3, little endian
flashrom is free software, get the source code at

Calibrating delay loop... OK.
Found ST flash chip "M25P16" (2048 kB, SPI) on buspirate_spi.
===
This flash part has status UNTESTED for operations: ERASE WRITE
The test status of this chip may have been updated in the latest development
version of flashrom. If you are running the latest development version,
please email a report to flashrom@flashrom.org if any of the above operations
work correctly for you with this flash part. Please include the flashrom
output with the additional -V option for all operations you tested (-V, -Vr,
-VE, -Vw), and mention which mainboard or programmer you tested.
Please mention your board in the subject line. Thanks for your help!
Reading flash... done.
Wed Apr  4 08:48:54 CEST 2012

I also did a printout from flashrom about the clock setting and it seems to be correct (8M->0x67 and 4M->0x66). My scope indicates that 4M and 8M doesn't work though, so it seems to be a problem in the firmware.

hack:
Code: [Select]
--- buspirate_spi.c_org	2012-04-04 07:47:03.695354455 +0200
+++ buspirate_spi.c 2012-04-04 08:45:31.538215459 +0200
@@ -94,8 +94,8 @@
 
 static const struct spi_programmer spi_programmer_buspirate = {
  .type = SPI_CONTROLLER_BUSPIRATE,
- .max_data_read = 12,
- .max_data_write = 12,
+ .max_data_read = 4096,
+ .max_data_write = 4096,
  .command = buspirate_spi_send_command,
  .multicommand = default_spi_send_multicommand,
  .read = default_spi_read,
@@ -304,29 +304,36 @@
  unsigned int i = 0;
  int ret = 0;
 
- if (writecnt > 16 || readcnt > 16 || (readcnt + writecnt) > 16)
+ if (writecnt > 4096 || readcnt > 4096)
  return SPI_INVALID_LENGTH;
+       
+        msg_pdbg("Reading %d bytes and writing %d bytesn", readcnt,writecnt);
 
- /* 3 bytes extra for CS#, len, CS#. */
- buf = realloc(buf, writecnt + readcnt + 3);
+ /* 5 bytes extra for cmd, wr length, re length */
+ buf = realloc(buf, max(writecnt,readcnt) + 5);
  if (!buf) {
  msg_perr("Out of memory!n");
  exit(1); // -1
  }

+ /* Write then read command */
+ buf[i++] = 0x04;
+       
+        /*write bytes*/
+ buf[i++] = (writecnt >> 8) & 0xFF;
+        buf[i++] = (writecnt) & 0xFF;
+
+        /*read bytes*/
+ buf[i++] = (readcnt >> 8) & 0xFF;
+        buf[i++] = (readcnt) & 0xFF;
 
- /* Assert CS# */
- buf[i++] = 0x02;
 
- buf[i++] = 0x10 | (writecnt + readcnt - 1);
+     
  memcpy(buf + i, writearr, writecnt);
  i += writecnt;
- memset(buf + i, 0, readcnt);
-
- i += readcnt;
- /* De-assert CS# */
- buf[i++] = 0x03;
-
- ret = buspirate_sendrecv(buf, i, i);
+       
+        /*read one extra byte for status*/
+ ret = buspirate_sendrecv(buf,i ,readcnt+1 );
 
  if (ret) {
  msg_perr("Bus Pirate communication error!n");
@@ -334,22 +341,13 @@
  }
 
  if (buf[0] != 0x01) {
- msg_perr("Protocol error while lowering CS#!n");
+ msg_perr("Protocol error!n");
  return SPI_GENERIC_ERROR;
  }
 
- if (buf[1] != 0x01) {
- msg_perr("Protocol error while reading/writing SPI!n");
- return SPI_GENERIC_ERROR;
- }
-
- if (buf[i - 1] != 0x01) {
- msg_perr("Protocol error while raising CS#!n");
- return SPI_GENERIC_ERROR;
- }
 
- /* Skip CS#, length, writearr. */
- memcpy(readarr, buf + 2 + writecnt, readcnt);
+ /* Skip status byte */
+ memcpy(readarr, buf + 1, readcnt);
 
  return ret;
 }

Re: slow read M25P16

Reply #2
Looking further into the clock issue it seems like the following line in SPI.c

Code: [Select]
static unsigned char binSPIspeed[]={0b00000,0b11000,0b11100,0b11101,0b00011,0b01000,0b10000,0b11000};
really should be
Code: [Select]
static unsigned char binSPIspeed[]={0b00000,0b11000,0b11100,0b11101,0b00011,0b01011,0b10011,0b11011}

So it seems like the primary prescaler bits are wrong for the three highest frequencies. I'm correct? Maybe this is all intentional?.

Unfortunately I dont have working toolchain for PIC on my computer (and google tells me that it is a pain to get up and running), so I can't test it.

Re: slow read M25P16

Reply #3
Hi dturpin,

Thanks for the report and thorough investigation. If you are working with v4 there could be a number of issues, from incorrect settings to status polling problems. I marked this thread and will investigate further on the v4 hardware.
Got a question? Please ask in the forum for the fastest answers.

Re: slow read M25P16

Reply #4
The MPLAB X toolchain is just two installs, though it is not available as a simple apt-get or yum install.
Got a question? Please ask in the forum for the fastest answers.

Re: slow read M25P16

Reply #5
Ah ok, maybe the page I googled was old then, because it looked more painful than two packages. Anyway I downloaded the IDE and C30 PIC24 compiler from microchips homepage. When I build I get the following

../../hardwarev4a.h:220:22: error: Compiler.h: No such file or directory
../../hardwarev4a.h:224:23: error: ./USB/usb.h: No such file or directory
../../hardwarev4a.h:225:36: error: ./USB/usb_function_cdc.h: No such file or directory
../../hardwarev4a.h:226:30: error: ./USB/usb_device.h: No such file or directory

I cant find any of those files, neither in the buspirate dir or in my microchip dir. Do I miss some lib?

Re: slow read M25P16

Reply #6
Should probably learn how to google, found the library at microchips homepage. Next problem was that the compiler couldn't find the definition for TRISDbits, I changed the CPU to PIC24FJ256GB106 (which I believe is correct from the chip marking) but then I run into the following:
/opt/microchip/mplabc30/v3.30c/bin/bin/../bin/pic30-elf-ld: Warning: target processor '24FJ256GB106' does not match linker script
After that I got a whole lot of unresolved references

Any ideas?

Edit: seems like unresolved references didn't have anything todo with the warning, they are still there after I fix the warning. They all seem related to the usb-serial bridge. I just included the h-files, do I need to build and include some library aswell?

Edit2: Seems like I downloaded the wrong version, the read only zip-file. With the correct one I get a bunch of errors when trying to open the file under linux. Seems like there is a lot of windows paths in the project settings

Edit3: Ok, changing the paths manually and Im getting
make[2]: *** No rule to make target `../m_1wire_213.c', needed by `build/default/production/_ext/1472/m_1wire_213.o'.  Stop.

Any on how to move on?

Re: slow read M25P16

Reply #7
Having a monologue here now but looking into the errors a bit more I realize that what was missing was to include
onboardEEPROM.*
1wire_lib.*
in the project. That together with changing linker script and removing references to m_1wire_213.c made the project compile on my Linux machine. All the windows paths doesn't seem to matter, a grep on "Program Files" give me a bunch of hits. Haven't tried on the target yet though.

This should really be fixed, released packages should build without trouble.

Re: slow read M25P16

Reply #8
After testing the new values for the SPI prescalers 8M,4M and 2M seems to produce the correct clock:
8MHz


4MHz


2Mhz



So to summarize my findings:
-There are some build errors (at least on Linux) when downloading 6.1 zipfile
-You can gain a lot of speed by using the write then read command instead f bulk in flashrom
-SPI prescaler values seems to be one bit off for 2,4 and 8Mhz

All these have been tested on v4 and a Linux machine

Still have one question left; According to my calculations above I'm breaking the 115200 baudrate when dumping the content. How is that possible??

Repeated calculation:
I'm calculating the max speed in my case at 115200,1 start and 1 stop to 2048*1024/(115200/10)/60~3minutes (what do I miss??). My time was just a little bit over 1minute

Re: slow read M25P16

Reply #9
the binmode has a way to set the serial speed to 1Mbaud. I think flahrom is using this.

Re: slow read M25P16

Reply #10
It doesn't look like that in the flashrom code. All I can find about opening the serial device is
Code: [Select]
static int buspirate_serialport_setup(char *dev)
{
/* 115200bps, 8 databits, no parity, 1 stopbit */
sp_fd = sp_openserport(dev, 115200);
return 0;
}

But I could have missed something

Re: slow read M25P16

Reply #11
Hi dturpin,

It seems like you may have opened the Bus Pirate v3 project file instead of v4. The v4 firmware has it's own project file (instead of a define) so the extra includes are already taken care of for the new platform. It could also be a problem with the released version too of course.

You can exceed the synthetic USB->serial baud rate on most operating systems. Some will enforce it, but usually it is just a formality, the data will come and go as fast as the slave can process it.
Got a question? Please ask in the forum for the fastest answers.

Re: slow read M25P16

Reply #12
[quote author="ian"]Hi dturpin,

It seems like you may have opened the Bus Pirate v3 project file instead of v4. The v4 firmware has it's own project file (instead of a define) so the extra includes are already taken care of for the new platform. It could also be a problem with the released version too of course.
[/quote]
Ok, maybe I have. A stupid question then, for me thats usually more of an AVR-guy how do I chose which project to open? I just opened the project by going to the firmware dir and buspirate.X, mplab displays this:

As I said the build work on target (v4 target that is) after I had removed references to m_1wire_213.c in some projects-files and adding onboardEEPROM.* and 1wire_lib.* to the project. I also changed the target processor and changed the linker-script, both those seemed to be v3 (which seems to support your theory about me opening the wrong project).

[quote author="ian"]
You can exceed the synthetic USB->serial baud rate on most operating systems. Some will enforce it, but usually it is just a formality, the data will come and go as fast as the slave can process it.[/quote]

Ah that explains it. How will the buspirate firmware know that it can exceed the speed? Does it get an ACK for each packet sent? or could one end up in a situation where some buffer is overflowing on host since it only allows 115200 out to the application?

Re: slow read M25P16

Reply #13
I found a Bus Pirate firmware bug which caused an extreme slowdown of flashrom operations.
Code: [Select]
static unsigned char binSPIspeed[] = {0b00000, 0b11000, 0b11100, 0b11101, 0b00011, 0b01000, 0b10000, 0b11000};
//00=30,01=125,10=250,11=1000khz, 100=2mhz,101=2.667mhz,  110=4mhz, 111=8mhz; datasheet pg 142
Unless I'm looking at the wrong datasheet, the clock divisors for these values are:
0b00000 8:1 64:1 512:1
0b11000 2:1 64:1 128:1
0b11100 1:1 64:1 64:1
0b11101 1:1 16:1 16:1
0b00011 8:1 1:1  8:1
0b01000 6:1 64:1 384:1
0b10000 4:1 64:1 256:1
0b11000 2:1 64:1 128:1
This means people who selected values above 2 MHz got extremely slow rates instead: 24kHz instead of 2.667MHz, 62kHz instead of 4MHz, 125kHz instead of 8MHz. It also explains why many people complained that using speeds >2MHz slowed things down a lot.

Until there is a fixed firmware, use the following flashrom command line to get 2 MHz (best possible option until the Bus Pirate firmware bug is fixed):
Code: [Select]
flashrom -p buspirate_spi:dev=/dev/ttyUSB0,spispeed=2M
If you have any flashrom problems, please mail flashrom@flashrom.org and include "Bus Pirate" in the subject line. As an alternative, you can send me a private message.
It is recommended to use at least flashrom 0.9.2 or later, and latest flashrom from svn if you want a 3x speedup.

Re: slow read M25P16

Reply #14
[quote author="Sjaak"]the binmode has a way to set the serial speed to 1Mbaud. I think flashrom is using this.[/quote]

I can't find that command anywhere in the docs. Are you sure it is there?

The FT232R documentation states that you can go as high as 3 Mbaud, and the PIC24FJ64GA004 datasheet says the PIC can autodetect and work with up to 1 Mbaud, and that makes me say "huh?" when I read the Bus Pirate firmware which apparently reserves UART rate autodetection for BPv4.
If you have any flashrom problems, please mail flashrom@flashrom.org and include "Bus Pirate" in the subject line. As an alternative, you can send me a private message.
It is recommended to use at least flashrom 0.9.2 or later, and latest flashrom from svn if you want a 3x speedup.