Changeset 1120 for trunk/buspirate_spi.c


Ignore:
Timestamp:
07/29/10 18:24:09 (22 months ago)
Author:
hailfinger
Message:

If we violate the raw SPI communication protocol requirements of the Bus
Pirate (namely, waiting for the completion of one command before sending
the next one), we can reduce the number of round trips by a factor of 3.
The FT2232 chip present in the Bus Pirate has a big enough buffer (at
least 128 bytes IIRC) to avoid overflows in the tiny buffer of the Bus
Pirate PIC.

Thanks to Daniel Flinkmann for sponsoring development of this patch.

Signed-off-by: Carl-Daniel Hailfinger <c-d.hailfinger.devel.2006@…>
Tested-by: Daniel Flinkmann <DFlinkmann@…>
Acked-by: Daniel Flinkmann <dflinkmann@…>

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/buspirate_spi.c

    r1112 r1120  
    261261                return SPI_INVALID_LENGTH; 
    262262 
    263         /* +2 is pretty arbitrary. */ 
    264         buf = realloc(buf, writecnt + readcnt + 2); 
     263        /* 3 bytes extra for CS#, len, CS#. */ 
     264        buf = realloc(buf, writecnt + readcnt + 3); 
    265265        if (!buf) { 
    266266                msg_perr("Out of memory!\n"); 
     
    270270        /* Assert CS# */ 
    271271        buf[i++] = 0x02; 
    272         ret = buspirate_sendrecv(buf, 1, 1); 
    273         if (ret) 
    274                 return SPI_GENERIC_ERROR; 
    275         if (buf[0] != 0x01) { 
    276                 msg_perr("Protocol error while lowering CS#!\n"); 
    277                 return SPI_GENERIC_ERROR; 
    278         } 
    279  
    280         i = 0; 
     272 
    281273        buf[i++] = 0x10 | (writecnt + readcnt - 1); 
    282274        memcpy(buf + i, writearr, writecnt); 
    283275        i += writecnt; 
    284276        memset(buf + i, 0, readcnt); 
    285         ret = buspirate_sendrecv(buf, i + readcnt, i + readcnt); 
    286         if (ret) 
     277 
     278        i += readcnt; 
     279        /* De-assert CS# */ 
     280        buf[i++] = 0x03; 
     281 
     282        ret = buspirate_sendrecv(buf, i, i); 
     283 
     284        if (ret) { 
     285                msg_perr("Bus Pirate communication error!\n"); 
    287286                return SPI_GENERIC_ERROR; 
    288         if (buf[0] != 0x01) { 
     287        } 
     288 
     289        if (buf[0] != 0x01) { 
     290                msg_perr("Protocol error while lowering CS#!\n"); 
     291                return SPI_GENERIC_ERROR; 
     292        } 
     293 
     294        if (buf[1] != 0x01) { 
    289295                msg_perr("Protocol error while reading/writing SPI!\n"); 
    290296                return SPI_GENERIC_ERROR; 
    291297        } 
    292         memcpy(readarr, buf + i, readcnt); 
    293  
    294         i = 0; 
    295         /* De-assert CS# */ 
    296         buf[i++] = 0x03; 
    297         ret = buspirate_sendrecv(buf, 1, 1); 
    298         if (ret) 
    299                 return SPI_GENERIC_ERROR; 
    300         if (buf[0] != 0x01) { 
     298 
     299        if (buf[i - 1] != 0x01) { 
    301300                msg_perr("Protocol error while raising CS#!\n"); 
    302301                return SPI_GENERIC_ERROR; 
    303302        } 
    304303 
     304        /* Skip CS#, length, writearr. */ 
     305        memcpy(readarr, buf + 2 + writecnt, readcnt); 
     306 
    305307        return ret; 
    306308} 
Note: See TracChangeset for help on using the changeset viewer.