Bus Pirate: Entering binary mode

From DP

Jump to: navigation , search

How to enter Bus Pirate binary mode in various languages.

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");
 
        //
        //check that the passed serial port exists and is working
        //
        if (fd==-1)   //added because the fd has already returned null
        {
            printf("Port does not exist!");
            return;
 
        }
 
        //
        //loop up to 25 times, send 0x00 each time and pause briefly for a reply (BBIO1)
        //
        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);
                }
        }
 
        //
        //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);
        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);
        }
}

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.