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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
|
#include <stdlib.h>
#include <avr/io.h>
#include <stdint.h>
#include <avr/interrupt.h>
#include <inttypes.h>
#include <avr/pgmspace.h>
#include "usbn2mc.h"
#define F_CPU 16000000UL
#include <util/delay.h>
#include "../usbn2mc/main/usbnapi.h"
#include "usbn2mc.h"
#include "common.h"
#include "protocol.h"
#include "wait.h"
#include "io.h"
#include "adc.h"
#include "i2c.h"
#include "spi.h"
#include "pwm.h"
#include "uart.h"
#include "93c46.h"
#include "debug.h"
//#define DEBUG 1
SIGNAL (SIG_INTERRUPT4)
{
USBNInterrupt ();
}
SIGNAL (SIG_UART0_RECV)
{
}
/* internal timer */
SIGNAL (SIG_OVERFLOW0)
{
common_scheduler ();
TCNT0 = octopus.latency_timer;
}
void
USBCommandRX (char *buf)
{
int i;
#ifdef DEBUG
debug_write("get \0");
debug_SendHex(buf[1]);
debug_write("\r\n");
#endif
/* a paket can be max 255 bytes */
if (octopus.long_rx_cmd == 1)
{
#ifdef DEBUG
debug_write("get next\r\n");
#endif
for (i = 0; i < 64; i++)
request[octopus.long_rx_index + i] = buf[i];
octopus.long_rx_index = octopus.long_rx_index + i;
if (octopus.long_rx_index >= octopus.long_rx_bytes)
{
octopus.long_rx_cmd = 0;
USBMessageIn (request);
}
}
else
{
#ifdef DEBUG
debug_write("get and execute\r\n\0");
#endif
octopus.long_rx_index = 0;
for (i = 0; i < 64; i++)
request[octopus.long_rx_index + i] = buf[i];
octopus.long_rx_index = i;
if ((unsigned int) buf[1] < 64)
{
octopus.long_rx_cmd = 0;
USBMessageIn (request);
}
else
{
octopus.long_rx_cmd = 1;
octopus.long_rx_bytes = (unsigned int) buf[1];
}
}
}
/* is called when received data from pc */
void
USBMessageIn (char *buf)
{
octopus.long_index = 0;
octopus.long_running = 0;
STATUS_LED_on;
#ifdef DEBUG
debug_write("parser\r\n\0");
#endif
int check = ((int) buf[0] >> 4) & 0x0F;
switch (check)
{
case 0:
if (buf[0] == CMD_EXTERNAL_DEVICE)
{
switch (buf[2])
{
case CMD_EXTERNAL_93C46:
flash_93c46_parser (buf);
break;
default:
;
}
}
else
common_parser (buf);
break;
case 1:
io_parser (buf);
break;
case 2:
adc_parser (buf);
break;
case 3:
i2c_parser (buf);
break;
case 4:
spi_parser (buf);
break;
case 5:
pwm_parser (buf);
break;
case 6:
uart_parser (buf);
break;
default:
answer[0] = buf[0];
answer[1] = RSP_UNKOWN_CMD;
answer[2] = 0;
CommandAnswer (3);
}
STATUS_LED_off;
}
void
CommandAnswer (unsigned int length)
{
int i;
// if first packet of a lang message
if (length > 64 && octopus.long_running == 0)
{
octopus.long_index = 0;
octopus.long_bytes = length;
octopus.long_running = 1;
length = 64;
}
USBNWrite (FIFOTXC1, FLUSH);
for (i = 0; i < length; i++)
USBNWrite (TXD1, answer[octopus.long_index + i]);
/* control togl bit */
if (octopus.datatogl == 1)
{
USBNWrite (FIFOTXC1, TX_LAST + TX_EN + TX_TOGL);
octopus.datatogl = 0;
}
else
{
USBNWrite (FIFOTXC1, TX_LAST + TX_EN);
octopus.datatogl = 1;
}
}
void
CommandAnswerRest (void)
{
if (octopus.long_running == 1)
{
if (octopus.long_index < octopus.long_bytes)
{
int dif = octopus.long_bytes - octopus.long_index;
octopus.long_index = octopus.long_index + 64;
if (dif > 64)
CommandAnswer (64);
else
{
/* last packet */
CommandAnswer (dif);
octopus.long_running = 0;
}
}
}
}
int
main (void)
{
DDRB = 0xFF;
PORTB = 0xFF;
int interf;
int conf;
STATUS_LED_off;
USBNInit ();
// setup your device
USBNDeviceVendorID (0x1781); // 0x0400 is the number from national
USBNDeviceProductID (0x0c65); // add your product id
USBNDeviceBCDDevice (0x0001); // you can use it as version e.g. version 1.02
char lang[] = { 0x09, 0x04, 0x00 };
_USBNAddStringDescriptor (lang); // language descriptor
/* Attention!!! Descriptors must be a factor of 8 (error in the stack) */
USBNDeviceManufacture ("EmbeddedProjects");
USBNDeviceProduct ("OctopusUSB Interface Converter and I/O Extension");
USBNDeviceSerialNumber ("20081108");
conf = USBNAddConfiguration ();
USBNConfigurationPower (conf, 100);
interf = USBNAddInterface (conf, 0);
USBNAlternateSetting (conf, interf, 0);
/* communication */
USBNAddInEndpoint (conf, interf, 1, 0x01, BULK, 64, 0, &CommandAnswerRest);
USBNAddOutEndpoint (conf, interf, 1, 0x01, BULK, 64, 0, &USBCommandRX);
USBNInitMC ();
octopus.datatogl = 0;
octopus.long_rx_cmd = 0;
/* start usb chip */
USBNStart ();
/* UARTInit2(38400,8,'N',1); */
#ifdef DEBUG
debug_init();
#endif
/* hello world led pattern */
DDRC = 0xFF;
PORTC = 0xAA;
delay_250ms ();
PORTC = 0xD5;
delay_250ms ();
PORTC = 0x80;
// testpin
//PORTB = 0xFF;
//PORTB = 0x00;
while (1);
return 0;
}
|