Skip to content

Commit

Permalink
Linux and Windows fixes to hid-flash file
Browse files Browse the repository at this point in the history
  • Loading branch information
Serasidis committed Jun 1, 2018
1 parent c8c4a8a commit 20de981
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 27 deletions.
Binary file removed Arduino_STM32_patch_180517_01.zip
Binary file not shown.
Binary file added Arduino_STM32_patch_180601_01.zip
Binary file not shown.
Binary file modified cli/hid-flash.exe
Binary file not shown.
36 changes: 19 additions & 17 deletions cli/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ int main(int argc, char *argv[]) {
setbuf(stdout, NULL);

printf("\n+----------------------------------------------------------------------+\n");
printf ("| HID-Flash v1.4e - STM32 HID Bootloader Flash Tool |\n");
printf ("| HID-Flash v1.4f - STM32 HID Bootloader Flash Tool |\n");
printf ("| (c) 04/2018 - Bruno Freitas - http://www.brunofreitas.com/ |\n");
printf ("| (c) 04/2018 - Vassilis Serasidis - http://www.serasidis.gr/ |\n");
printf ("| Customized for STM32duino ecosystem - http://www.stm32duino.com/ |\n");
Expand All @@ -77,33 +77,34 @@ int main(int argc, char *argv[]) {
return 1;
}

serial_init(argv[2]);
serial_init(argv[2]);
hid_init();

printf("> Searching for F055:0001 HID device...\n");
handle = hid_open(0xF055, 0x0001, NULL);

if (!handle) {
printf("Unable to open the HID device.\n");
printf("> Unable to open the HID device.\n");
error = 1;
goto exit;
}

printf("> F055:0001 device is found !\n");

firmware_file = fopen(argv[1], "rb");
if(!firmware_file) {
printf("Error opening firmware file: %s\n", argv[1]);
printf("> Error opening firmware file: %s\n", argv[1]);
error = 1;
goto exit;
}

// Send RESET PAGES command to put HID bootloader in initial stage...
memset(hid_buffer, 0, sizeof(hid_buffer));
memset(hid_buffer, 0, sizeof(hid_buffer)); //Fill the hid_buffer with zeros.
memcpy(&hid_buffer[1], CMD_RESET_PAGES, sizeof(CMD_RESET_PAGES));

printf("Sending <reset pages> command...\n");
printf("> Sending <reset pages> command...\n");

// Flash is unavailable when writing to it, so USB interrupt may fail here
if(!usb_write(handle, hid_buffer, 129)) {
printf("Error while sending <reset pages> command.\n");
printf("> Error while sending <reset pages> command.\n");
error = 1;
goto exit;
}
Expand All @@ -113,7 +114,7 @@ int main(int argc, char *argv[]) {
fseek(firmware_file, 0, SEEK_SET);

// Send Firmware File data
printf("Flashing firmware...\n");
printf("> Flashing firmware...\n");

memset(page_data, 0, sizeof(page_data));
read_bytes = fread(page_data, 1, sizeof(page_data), firmware_file);
Expand All @@ -130,7 +131,7 @@ int main(int argc, char *argv[]) {
usleep(5000);
// Flash is unavailable when writing to it, so USB interrupt may fail here
if(!usb_write(handle, hid_buffer, 129)) {
printf("Error while flashing firmware data.\n");
printf("> Error while flashing firmware data.\n");
error = 1;
goto exit;
}
Expand All @@ -140,17 +141,17 @@ int main(int argc, char *argv[]) {
read_bytes = fread(page_data, 1, sizeof(page_data), firmware_file);
}

printf("\nDone!\n");
printf("\n> Done!\n");

// Send CMD_REBOOT_MCU command to reboot the microcontroller...
memset(hid_buffer, 0, sizeof(hid_buffer));
memcpy(&hid_buffer[1], CMD_REBOOT_MCU, sizeof(CMD_REBOOT_MCU));

printf("Sending <reboot mcu> command...\n");
printf("> Sending <reboot mcu> command...\n");

// Flash is unavailable when writing to it, so USB interrupt may fail here
if(!usb_write(handle, hid_buffer, 129)) {
printf("Error while sending <reboot mcu> command.\n");
printf("> Error while sending <reboot mcu> command.\n");
}

exit:
Expand All @@ -169,19 +170,20 @@ int main(int argc, char *argv[]) {
}

int serial_init(char *argument) {
printf("Trying to open the comport...\n");

printf("> Trying to open the comport...\n");
if(RS232_OpenComport(argument)){
return(0);
}
printf("Toggling DTR...\n");
printf("> Toggling DTR...\n");
RS232_disableRTS();
RS232_enableDTR();
usleep(50000L);
RS232_enableRTS();
usleep(50000L);
RS232_disableDTR();
usleep(50000L);
RS232_cputs("1EAF");
RS232_send_magic();
usleep(50000L);
RS232_disableRTS();

Expand Down
26 changes: 17 additions & 9 deletions cli/rs232.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
new_termios.c_cc[VLNEXT] = 0;
new_termios.c_cc[VEOL2] = 0;


if (cfsetispeed(&new_termios, B9600) != 0) {
fprintf(stderr, "cfsetispeed(&new_termios, B9600) failed: %s\n", strerror(errno));
return 1;
Expand All @@ -87,6 +88,7 @@
fprintf(stderr, "cfsetospeed(&new_termios, B9600) failed: %s\n", strerror(errno));
return 1;
}

if (tcsetattr(tty_fd, TCSANOW, &new_termios) != 0) {
fprintf(stderr, "tcsetattr(fd, TCSANOW, &new_termios) failed: %s\n", strerror(errno));
return 1;
Expand Down Expand Up @@ -191,6 +193,10 @@
}
}

void RS232_send_magic(){
write(tty_fd,"1EAF",4);
}


#else /* windows */

Expand All @@ -217,7 +223,7 @@ int RS232_OpenComport(char *comport)

if(Cport==INVALID_HANDLE_VALUE)
{
printf("unable to open comport\n");
printf("> unable to open comport\n");
return(1);
}

Expand All @@ -227,14 +233,14 @@ int RS232_OpenComport(char *comport)

if(!BuildCommDCBA(mode_str_2, &port_settings))
{
printf("unable to set comport dcb settings\n");
printf("> unable to set comport dcb settings\n");
CloseHandle(Cport);
return(1);
}

if(!SetCommState(Cport, &port_settings))
{
printf("unable to set comport cfg settings\n");
printf("> unable to set comport cfg settings\n");
CloseHandle(Cport);
return(1);
}
Expand All @@ -249,7 +255,7 @@ int RS232_OpenComport(char *comport)

if(!SetCommTimeouts(Cport, &Cptimeouts))
{
printf("unable to set comport time-out settings\n");
printf("> unable to set comport time-out settings\n");
CloseHandle(Cport);
return(1);
}
Expand Down Expand Up @@ -297,10 +303,12 @@ void RS232_disableRTS()
EscapeCommFunction(Cport, CLRRTS);
}

#endif


void RS232_cputs(const char *text) /* sends a string to serial port */
void RS232_send_magic()
{
while(*text != 0) RS232_SendByte(*(text++));
int n;

WriteFile(Cport, "1EAF", 4, (LPDWORD)((void *)&n), NULL);
}

#endif

2 changes: 1 addition & 1 deletion cli/rs232.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ int RS232_OpenComport(char *);
int RS232_SendByte(unsigned char);
//int RS232_ReadByte();
void RS232_CloseComport();
void RS232_cputs(const char *);
void RS232_enableDTR();
void RS232_disableDTR();
void RS232_enableRTS();
void RS232_disableRTS();
void RS232_send_magic();

#ifdef __cplusplus
} /* extern "C" */
Expand Down

0 comments on commit 20de981

Please sign in to comment.