Bus Pirate: Entering binary mode

From DP

Revision as of 07:47, 25 August 2010 by Ian (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation , search

How to enter Bus Pirate binary mode in various languages.

<syntaxhighlight lang="C">void BP_EnableMode(int fd, char bbmode) {

       int ret;
       char tmp[100] = { [0 ... 20] = 0x00 };
       int done = 0;
       int tries=0;


       printf(" Entering binary mode...\n");
       if (fd==-1)   //added because the fd has already returned null
       {
           printf("Port does not exist!");
           return;
       }
       while (!done) {
               tmp[0]=0x00;
               //printf("Sending 0X%X to port\n",tmp[0]);
               serial_write(fd, tmp, 1);
               tries++;
               //printf("tries: %i Ret %i\n",tries,ret);
               usleep(1);
               ret = serial_read(fd, tmp, 5);
               if (ret != 5 && tries>20) {
                     fprintf(stderr, "Buspirate did not respond correctly :( %i \n", ret );
                     exit(-1);
               }else if (strncmp(tmp, "BBIO1", 5) == 0){
                     done=1;
               }
               if (tries>25){
                    printf("Buspirate:Too many tries in serial read! -exiting \n - chip not detected, or not readable/writable\n");
                    exit(-1);
               }
       }
       done=0;
       //
       //Now send the command to select a mode
       //
       tmp[0] = bbmode; //the mode to select
       //printf("Sending 0X%X to port\n",tmp[0]);
       serial_write(fd, tmp, 1);
       tries++;
       usleep(1);
       ret = serial_read(fd, tmp, 4);
       if ( (ret==4) && (strncmp(tmp, "SPI1", 4) == 0)) {//check the reply
       } else{
            fprintf(stderr, "Buspirate did not respond correctly :( %i \n", ret );
            exit(-1);
       }

}</syntaxhighlight>

This C example enters binary mode with a loop that sends 0x00 once, then pauses and checks for BBIO1. If it doesn't get BBIO1 after a short delay, it sends 0x00 again, up to 25 times.

This function has a second section that configures a mode (for example SPI) after entering binary access mode.