summaryrefslogtreecommitdiff
path: root/digital/dev2/src/usb_serial_isp/main.c
blob: 45e0c7c6cbd1b4660d4d28e8f04fd59eb7eaeea3 (plain)
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* main.c */
/* dev2 - Multi-purpose development board using USB and Ethernet. {{{
 *
 * Copyright (C) 2009 Nicolas Schodet
 *
 * APBTeam:
 *        Web: http://apbteam.org/
 *      Email: team AT apbteam DOT org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */
#include "common.h"

#include "modules/usb/usb.h"
#include "modules/isp/isp_frame.h"
#include "io.h"

#include <avr/wdt.h>

#include "descriptors.h"
#include "common/serial.h"
#include "common/usb_isp.h"
#include "common/gpio.h"
#include "common/select.h"

HANDLES_EVENT (USB_Connect);
HANDLES_EVENT (USB_Disconnect);
HANDLES_EVENT (USB_ConfigurationChanged);
HANDLES_EVENT (USB_UnhandledControlPacket);

volatile uint8_t usb_connected, usb_configured, gpio_mode;

int
main (void)
{
    /* Disable watchdog if enabled by bootloader/fuses. */
    MCUSR &= ~(1 << WDRF);
    wdt_disable ();
    /* Disable Clock Division. */
    SetSystemClockPrescaler (0);
    /* Initialise hardware. */
    select_init ();
    /* Initialize USB Subsystem. */
    USB_Init ();
    /* Main loop. */
    while (1)
      {
	if (usb_connected)
	    USB_USBTask ();
	if (usb_configured)
	  {
	    serial_task ();
	    if (!gpio_mode)
		usb_isp_task ();
	    else
		gpio_task ();
	  }
      }
}

EVENT_HANDLER (USB_Connect)
{
    usb_connected = 1;
}

EVENT_HANDLER (USB_Disconnect)
{
    usb_connected = 0;
    usb_configured = 0;
}

EVENT_HANDLER (USB_ConfigurationChanged)
{
    /* Setup Rx and Tx Endpoints for the first port. */
    Endpoint_ConfigureEndpoint (SERIAL_TX_EPNUM, EP_TYPE_BULK,
				ENDPOINT_DIR_IN, SERIAL_TX_EPSIZE,
				ENDPOINT_BANK_SINGLE);
    Endpoint_ConfigureEndpoint (SERIAL_RX_EPNUM, EP_TYPE_BULK,
				ENDPOINT_DIR_OUT, SERIAL_RX_EPSIZE,
				ENDPOINT_BANK_SINGLE);
    /* Setup Rx and Tx Endpoints for the second port.
     * Port shared between ISP and GPIO, use control packet to switch. */
    Endpoint_ConfigureEndpoint (ISP_TX_EPNUM, EP_TYPE_BULK,
				ENDPOINT_DIR_IN, ISP_TX_EPSIZE,
				ENDPOINT_BANK_SINGLE);
    Endpoint_ConfigureEndpoint (ISP_RX_EPNUM, EP_TYPE_BULK,
				ENDPOINT_DIR_OUT, ISP_RX_EPSIZE,
				ENDPOINT_BANK_SINGLE);
    /* Start tasks. */
    usb_configured = 1;
    gpio_mode = 0;
}

EVENT_HANDLER (USB_UnhandledControlPacket)
{
    /* Process TS specific control requests. */
    switch (bRequest)
      {
	/* Switch back to DFU mode. */
      case 0:
	if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_VENDOR |
			      REQREC_DEVICE))
	  {
	    Endpoint_ClearSetupReceived ();
            /* Send acknowledgement and wait for it to be sent. */
	    Endpoint_ClearSetupIN ();
	    while (!Endpoint_IsSetupINReady ())
		;
	    USB_ShutDown ();
            /* Jump to bootloader. */
	    ((void (*) (void)) 0x3000) ();
	  }
	break;
	/* Select output. */
      case 1:
	if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_VENDOR |
			      REQREC_DEVICE))
	  {
	    /* Selector parameter. */
	    uint8_t output = Endpoint_Read_Byte ();
	    Endpoint_ClearSetupReceived ();
	    /* Select output. */
	    serial_uninit ();
	    if (gpio_mode)
		gpio_uninit ();
	    select_out (output);
	    if (select_active (output))
		serial_init ();
	    /* Disable GPIO mode. */
	    gpio_mode = 0;
            /* Send acknowledgement. */
	    Endpoint_ClearSetupIN ();
	  }
	break;
	/* Set serial parameters. */
      case 0x70:
	if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_VENDOR |
			      REQREC_DEVICE))
	  {
	    Endpoint_ClearSetupReceived ();
	    /* Set serial parameters. */
	    struct serial_parameters_t params;
	    Endpoint_Read_Control_Stream_LE (&params, sizeof (params));
	    serial_set_params (&params);
	    /* Send acknowledgement. */
	    Endpoint_ClearSetupIN ();
	  }
	break;
	/* Set GPIO. */
      case 0x80:
	if (bmRequestType == (REQDIR_HOSTTODEVICE | REQTYPE_VENDOR |
			      REQREC_DEVICE))
	  {
	    /* GPIO parameter. */
	    uint8_t ddr = Endpoint_Read_Byte ();
	    uint8_t port = Endpoint_Read_Byte ();
	    Endpoint_ClearSetupReceived ();
	    /* Set GPIO. */
	    DDRD = ddr;
	    PORTD = port;
	    /* Also switch to GPIO mode. */
	    gpio_mode = 1;
            /* Send acknowledgement. */
	    Endpoint_ClearSetupIN ();
	  }
	break;
      }
}