summaryrefslogtreecommitdiff
path: root/tools/atmega324u_usbhid/reset.c
blob: 63252e1760d588c86deef645bbe6a5ee87d1bdc7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include "hid.h"


void sendstr(char * tosend)
{
    rawhid_send(0, tosend, strlen(tosend),1000);
}

#define NUM_DEVICE_IDS 3
uint16_t dev_ids[] = {0x0481, 0x0480, 0x0482};

int main (int argc, char *argv[])
{
    // C-based example is 16C0:0480:FFAB:0200
    int r,d,w;
    for (d=0; d<NUM_DEVICE_IDS; d++)
    {
        r = rawhid_open(1, 0x16C0, dev_ids[d], 0xFFAB, 0x0200);
        if (r > 0)
            break;
    }
    if (r <= 0) {
        printf("no rawhid device found, already in bootloader ?\n");
        return 0;
    }
    sendstr("r"); // clear the buffer

    //now wait until device is gone

    for (w=0; w < 12; w++)
    {
        r = rawhid_open(1, 0x16C0, dev_ids[d], 0xFFAB, 0x0200);
        if (r > 0)
        {
            rawhid_close(r);
            sleep(1);
        }
        else
        {
            printf("device gone, reboot finished\n");
            sleep(1);
            return 0;
        }
    }
    printf("device still there, reboot did not work ?\n");
    return -1;
}