Updated firmware for PCB Revision 3.0
[SCSI2SD-V6.git] / software / bootloaderhost / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 #include "hidapi.h"
5 #include "cybtldr_api.h"
6 #include "cybtldr_api2.h"
7
8 hid_device *handle = NULL;
9
10 static int OpenConnection(void)
11 {
12         return 0;
13 }
14
15 static int CloseConnection(void)
16 {
17         return 0;
18 }
19
20 static int ReadData(unsigned char* data, int count)
21 {
22 printf("read");
23         return (hid_read(handle, data, count) >= 0 ? 0 : -1);
24 }
25
26 static int WriteData(unsigned char* data, int count)
27 {
28         unsigned char buf[65];
29         buf[0] = 0; // report ID
30         int i;
31         for (i = 0; i < count; ++i)
32         {
33                 buf[i+1] = data[i];
34         }
35         //int result = hid_write(handle, buf, 65);
36         int result = hid_send_feature_report(handle, buf, count + 1);
37 printf("write %d, %d", count, result);
38         return (result >= 0) ? 0 : -1;
39 }
40
41
42 static void ProgressUpdate(unsigned char arrayId, unsigned short rowNum)
43 {
44         printf("Completed array %d, row %d\n", arrayId, rowNum);
45 }
46
47 int main(int argc, char* argv[])
48 {
49         int res;
50         unsigned char buf[65];
51         #define MAX_STR 255
52         wchar_t wstr[MAX_STR];
53         int i;
54
55         CyBtldr_CommunicationsData cyComms =
56         {
57                 &OpenConnection,
58                 &CloseConnection,
59                 &ReadData,
60                 &WriteData,
61                 64
62         };
63
64         // Enumerate and print the HID devices on the system
65         struct hid_device_info *dev = NULL;
66
67         printf("Waiting for a mate\n");
68         while (dev == NULL)
69         {
70                 dev = hid_enumerate(0x04B4, 0xB71D);
71                 usleep(10000); // 10ms
72         }
73         printf("Device Found\n  type: %04hx %04hx\n  path: %s\n  serial_number: %ls",
74                 dev->vendor_id, dev->product_id, dev->path, dev->serial_number);
75         printf("\n");
76         printf("  Manufacturer: %ls\n", dev->manufacturer_string);
77         printf("  Product:      %ls\n", dev->product_string);
78         printf("\n");
79         //hid_free_enumeration(devs);
80
81         // Open the device using the VID, PID,
82         // and optionally the Serial number.
83         handle = hid_open(0x04B4, 0xB71D, NULL);
84
85
86         printf("Tryng to program\n");
87         int result = CyBtldr_Program("/home/michael/projects/SCSI2SD/git/software/SCSI2SD/SCSI2SD.cydsn/CortexM3/ARM_GCC_473/Release/SCSI2SD.cyacd",
88                 &cyComms,
89                 &ProgressUpdate);
90         printf("Possibly successful ? %d\n", result);
91
92         return 0;
93 }
94