Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 #include <stdio.h>
00013 #include <usb.h>
00014 #include <getopt.h>
00015 #include "hexfile.h"
00016 #include "flashprog.h"
00017 
00018 const unsigned short VID_NORDIC     = 0x1915;
00019 const unsigned short PID_LU1BOOT    = 0x0101;
00020 
00021 static unsigned char hex_buf[MAX_FLASH_SIZE];
00022 
00023 usb_dev_handle *find_and_open_usb(unsigned short vid, unsigned short pid)
00024 {
00025     struct usb_bus *bus;
00026     struct usb_device *dev;
00027     usb_dev_handle *hdev;
00028 
00029     usb_find_busses();      
00030     usb_find_devices();     
00031 
00032     for(bus = usb_busses; bus; bus = bus->next) 
00033     {
00034         for(dev = bus->devices; dev; dev = dev->next) 
00035         {
00036             if(dev->descriptor.idVendor == vid && dev->descriptor.idProduct == pid)
00037             {
00038                 
00039                 hdev = usb_open(dev);
00040                 if(usb_set_configuration(hdev, 1) < 0)
00041                 {
00042                     usb_close(hdev);
00043                     continue;
00044                 }
00045                 if(usb_claim_interface(hdev, 0) < 0)
00046                 {
00047                     usb_close(hdev);
00048                     continue;
00049                 }
00050                 return hdev;
00051             }
00052         }
00053     }
00054     return 0;
00055 }
00056 
00057 void print_usage(void)
00058 {
00059     fprintf(stderr, "usage: bootlu1 [options] <hex-file>\n");
00060     fprintf(stderr, "       options:\n");
00061     fprintf(stderr, "       -f 16 Flash size is 16K Bytes\n");
00062     fprintf(stderr, "       -f 32 Flash size is 32K Bytes\n");
00063 }
00064 
00065 int main(int argc, char* argv[])
00066 {   
00067     char c;
00068     unsigned flash_size, i, low_addr = 0, high_addr = 0;
00069     FILE *fp;
00070     usb_dev_handle *hdev;
00071 
00072     while((c = getopt(argc, argv, "f:")) != EOF)
00073     {
00074         switch(c)
00075         {
00076         case 'f':
00077             i = (unsigned)atoi(optarg);
00078             if ((i != 16) && (i != 32))
00079             {
00080                 print_usage();
00081                 exit(EXIT_FAILURE);
00082             }
00083             flash_size = i * 1024;
00084             break;
00085         default:
00086             print_usage();
00087             exit(EXIT_FAILURE);
00088         }
00089     }
00090     if ((argc - optind) != 1)
00091     {
00092         print_usage();
00093         exit(EXIT_FAILURE);
00094     }
00095 
00096     if ((fp = fopen(argv[optind], "rt")) == 0)
00097     {
00098         fprintf(stderr, "ERROR: Can't open input file <%s>\n", argv[optind]);
00099         return 1;
00100     }    
00101     usb_init();             
00102     hdev = find_and_open_usb(VID_NORDIC, PID_LU1BOOT);
00103     if (hdev == 0)
00104     {
00105         fprintf(stderr, "ERROR: nRF24LU1P Bootloader not found\n");
00106         exit(EXIT_FAILURE);
00107     }
00108     for(i=0;i<flash_size;i++)
00109         hex_buf[i] = 0xff;
00110     if (read_hex_file(fp, 1, hex_buf, flash_size, &low_addr, &high_addr) != NO_ERR)
00111     {
00112         exit(EXIT_FAILURE);
00113     }
00114     if (!flash_prog(hdev, low_addr, high_addr, flash_size, hex_buf))
00115     {
00116         fprintf(stderr, "ERROR: There was an error programming the flash\n");
00117         exit(EXIT_FAILURE);
00118     }
00119     usb_close(hdev);
00120     exit(EXIT_SUCCESS);
00121 }