Rev Author Line No. Line
1661 kaklik 1  
2  
3 /** INCLUDES *******************************************************/
4  
5  
6 #include "GenericTypeDefs.h"
7 #include "Compiler.h"
8 #include "HardwareProfile.h"
9 #include "USB/usb.h"
10 #include "usbavrcmd.h"
11  
12 #include "user.h"
13 #include <math.h>
14  
15 #include <stdlib.h>
16 #include <string.h>
17 #include <ctype.h>
18  
19 #if defined (UBW)
20 #include <delays.h>
21 #include <i2c.h>
22 #include <EEP.h>
23  
24 #elif defined (UBW32)
25 #include <peripheral/i2c.h>
26 #include <dee_emulation/dee_emulation_pic32.h>
27  
28 #endif
29  
30  
31 /********************************************************************
32 * Section B: EP0 Buffer Space
33 *******************************************************************/
34 extern volatile CTRL_TRF_SETUP SetupPkt; // 8-byte only
35 extern volatile BYTE CtrlTrfData[USB_EP0_BUFF_SIZE];
36  
37 #pragma udata
38  
39 WORD_VAL FilterCrossOver[8];
40 float FilterSwitchOver[7];
41  
42 WORD_VAL LPFCrossOver[8];
43 float LPFSwitchOver[7];
44  
45 #pragma code
46  
47 void USBCheckUBWRequest(void)
48 {
49 unsigned char paddle_byte;
50 unsigned int eep_adr;
51 WORD_VAL w;
52 unsigned int i, j;
53 unsigned char IO_data;
54  
55 #if defined (UBW32)
56 unsigned int value;
57 unsigned int number_of_filter_bytes;
58 #else
59 unsigned char number_of_filter_bytes;
60 #endif
61  
62  
63  
64 if(SetupPkt.RequestType == USB_SETUP_TYPE_STANDARD_BITFIELD) return; // only deals with non-standard, ie user requests
65  
66 command = SetupPkt.bRequest; // save the command of the user request from the setuppacket
67 // it will be used for subsequent data transfer
68  
69 switch(command)
70 {
71  
72 case CMD_GET_CW_KEY:
73  
74 paddle_byte = 0x22; // 00100010
75 if (!PADDLE_DIT) paddle_byte &= 0xdf; // 11011111
76 if (!PADDLE_DAH) paddle_byte &= 0xfd; // 11111101
77 if (RXTX) paddle_byte |= 0x10; // PB4 is the ATTINY PTT_OUT pin 00010000
78 else paddle_byte &= 0xef; // 11101111
79  
80 replybuf[0] = paddle_byte;
81 inPipes[0].pSrc.bRam = (BYTE*)&replybuf[0]; // Set Source
82 inPipes[0].info.bits.ctrl_trf_mem = USB_EP0_RAM; // Set memory type
83 inPipes[0].wCount.v[0] = 1; // Set data count
84 inPipes[0].info.bits.busy = 1;
85 break;
86  
87 case CMD_SET_USRP1: // Set RxTx and get Paddle status
88 paddle_byte = 0x22; // 00100010
89 if ( SetupPkt.W_Value.v[0] ){
90 RXTX = 1; // Set PTT
91 paddle_byte |= 0x10; // 00010000
92 }
93 else{
94 RXTX = 0;
95 paddle_byte &= 0xef; // 11101111
96 };
97  
98  
99 if (!PADDLE_DIT) paddle_byte &= 0xdf; // 11011111
100 if (!PADDLE_DAH) paddle_byte &= 0xfd; // 11111101
101  
102 replybuf[0] = paddle_byte;
103 inPipes[0].pSrc.bRam = (BYTE*)&replybuf[0]; // Set Source
104 inPipes[0].info.bits.ctrl_trf_mem = USB_EP0_RAM; // Set memory type
105 inPipes[0].wCount.v[0] = 1; // Set data count
106 inPipes[0].info.bits.busy = 1;
107 break;
108  
109 case CMD_SET_FREQ_REG: // Control Transfer Rx of data from HOST->UBW
110 case CMD_SET_LO_SM:
111 case CMD_SET_FREQ:
112 case CMD_SET_XTAL:
113 case CMD_SET_STARTUP:
114 case CMD_SET_PPM:
115  
116 wCount = SetupPkt.wLength;
117 outPipes[0].wCount.Val = SetupPkt.wLength;
118 outPipes[0].pFunc = &Read_Command_Into_Buffer;
119 outPipes[0].pDst.bRam = (BYTE*) &command_buffer[current_command_in].data[0];
120 outPipes[0].info.bits.busy = 1;
121 break;
122  
123 case CMD_GET_PIN: // read ports
124 IO_data = 0x0d; // 00001101
125 if (PADDLE_DAH) IO_data |= 0x02; // PB1
126 if (BPF_S0_STATE) IO_data |= 0x10; // PB4
127 if (BPF_S1_STATE) IO_data |= 0x20; // PB5
128  
129 replybuf[0] = IO_data;
130 inPipes[0].pSrc.bRam = (BYTE*)&replybuf[0]; // Set Source
131 inPipes[0].info.bits.ctrl_trf_mem = USB_EP0_RAM; // Set memory type
132 inPipes[0].wCount.v[0] = 1; // Set data count
133 inPipes[0].info.bits.busy = 1;
134 break;
135  
136 case CMD_SET_PORT: // set ports
137 IO_data = SetupPkt.W_Value.v[0];
138 if (!abpf_flag){
139 if (IO_data & 0x10) BPF_S0 = 1;
140 else BPF_S0 = 0;
141 if (IO_data & 0x20) BPF_S1 = 1;
142 else BPF_S1 = 0;
143 };
144 break;
145  
146 case CMD_SET_IO: // set i/o bits
147 IO_data = SetupPkt.W_Index.v[0] & 0x03; // only 2 bits active 00000011
148  
149 if (!abpf_flag) { // abpf inactive
150 if (IO_data & 0x01) BPF_S0 = 1; // Simulate IO_PIN1 in ATTINY
151 else BPF_S0 = 0;
152 if (IO_data & 0x02) BPF_S1 = 1; // Simulate IO_PIN2 in ATTINY
153 else BPF_S1 = 0;
154 }
155  
156 IO_data = BPF_S0_STATE; // read back status of BPF select pins
157 if (BPF_S1_STATE) IO_data |= 0x02;
158 else IO_data &= 0x01;
159  
160 replybuf[0] = IO_data;
161 replybuf[1] = 0x00;
162 inPipes[0].pSrc.bRam = (BYTE*)&replybuf[0]; // Set Source
163 inPipes[0].info.bits.ctrl_trf_mem = USB_EP0_RAM; // Set memory type
164 inPipes[0].wCount.v[0] = 2; // Set data count
165 inPipes[0].info.bits.busy = 1;
166 break;
167  
168 case CMD_GET_IO:
169  
170 IO_data = BPF_S0_STATE; // status of BPF select pins
171 if (BPF_S1_STATE) IO_data |= 0x02;
172 else IO_data &= 0x01;
173  
174 replybuf[0] = IO_data;
175 replybuf[1] = 0x00;
176 inPipes[0].pSrc.bRam = (BYTE*)&replybuf[0]; // Set Source
177 inPipes[0].info.bits.ctrl_trf_mem = USB_EP0_RAM; // Set memory type
178 inPipes[0].wCount.v[0] = 2; // Set data count
179 inPipes[0].info.bits.busy = 1;
180 break;
181  
182 case CMD_SET_FILTER: // Set and Read filter crossover points
183 i = SetupPkt.W_Index.Val;
184 if (i < 256){ // Rx Filter
185 number_of_filter_bytes = NUM_BPF * 2;
186 inPipes[0].pSrc.bRam = (BYTE*)&FilterCrossOver[0]; // Set source
187  
188 if (i < NUM_BPF) {
189 FilterCrossOver[i].Val = SetupPkt.W_Value.Val;
190 if (i != (NUM_BPF-1)) FilterSwitchOver[i] = Cross2Switch(FilterCrossOver[i]);
191 else abpf_flag = SetupPkt.W_Value.Val;
192 }
193 }
194 else{ // Tx filter
195 number_of_filter_bytes = 16;
196 inPipes[0].pSrc.bRam = (BYTE*)&LPFCrossOver[0]; // Set source
197  
198 j = i - 256;
199 if ( j < 8){
200 LPFCrossOver[j].Val = SetupPkt.W_Value.Val;
201 if (j != 7) LPFSwitchOver[j] = Cross2Switch(LPFCrossOver[j]);
202 };
203 };
204  
205 // now that a filter value of filter enable/disable value is set, update the EEPROM
206 #if defined (UBW)
207 for (i = 0; i < NUM_BPF; i++){
208 Write_b_eep((i * 2 + F_CROSS_OVER +1), FilterCrossOver[i].v[0]);
209 Busy_eep();
210 Write_b_eep((i * 2 + F_CROSS_OVER + 1 + 1), FilterCrossOver[i].v[1]);
211 Busy_eep();
212 }
213 #elif defined (UBW32)
214 for (i =0; i < NUM_BPF; i++) DataEEWrite( FilterCrossOver[i].Val , (i + F_CROSS_OVER +1));
215 #endif
216  
217 #if defined (UBW)
218 for (j = 0; j < 8; j++){
219 Write_b_eep(( j * 2 + F_CROSS_OVER +17), LPFCrossOver[j].v[0]);
220 Busy_eep();
221 Write_b_eep(( j * 2 + F_CROSS_OVER +17 + 1), LPFCrossOver[j].v[1]);
222 Busy_eep();
223 }
224 Write_b_eep(F_CROSS_OVER, F_CROSS_OVER_VALUE);
225 Busy_eep();
226 #elif defined (UBW32)
227 for (j = 0; j < 8; j++){
228 DataEEWrite( (unsigned int)LPFCrossOver[j].Val , (j + F_CROSS_OVER +9));
229 };
230 DataEEWrite(F_CROSS_OVER_VALUE, F_CROSS_OVER);
231 #endif
232  
233  
234 inPipes[0].wCount.v[0] = number_of_filter_bytes; // Set data count
235 inPipes[0].info.bits.ctrl_trf_mem = USB_EP0_RAM; // Set memory type
236 inPipes[0].info.bits.busy = 1;
237 break;
238  
239 case CMD_SET_SI570: // SI570: write byte from register index
240 i2c_adr = SetupPkt.W_Value.v[0];
241 #if defined (UBW)
242 IdleI2C();
243 StartI2C();
244 IdleI2C();
245 WriteI2C( i2c_adr <<1);
246 WriteI2C( SetupPkt.W_Value.v[1]); // register = byte 3
247 WriteI2C( SetupPkt.W_Index.v[0]); // value to write to register
248 StopI2C();
249 IdleI2C();
250 #elif defined (UBW32)
251 IdleI2C1();
252 StartI2C1();
253 IdleI2C1();
254 MasterWriteI2C1( i2c_adr <<1);
255 MasterWriteI2C1( SetupPkt.W_Value.v[1]); // register = byte 3
256 MasterWriteI2C1( SetupPkt.W_Index.v[0]); // value to write to register
257 StopI2C1();
258 IdleI2C1();
259 #endif
260  
261 replybuf[0] = 0; // just say no errors
262  
263 inPipes[0].pSrc.bRam = (BYTE*)&replybuf[0]; // Set Source
264 inPipes[0].info.bits.ctrl_trf_mem = USB_EP0_RAM; // Set memory type
265 inPipes[0].wCount.v[0] = 1; // Set data count
266 inPipes[0].info.bits.busy = 1;
267 break;
268  
269  
270 case CMD_GET_LO_SM: // return sub mul
271 for (i=0; i<4; i++) replybuf[i] = f_sub.bytes[i];
272 for (i=0; i<4; i++) replybuf[i+4] = f_mul.bytes[i];
273 inPipes[0].pSrc.bRam = (BYTE*)&replybuf[0]; // Set Source
274 inPipes[0].info.bits.ctrl_trf_mem = USB_EP0_RAM; // Set memory type
275 inPipes[0].wCount.v[0] = 8; // Set data count
276 inPipes[0].info.bits.busy = 1;
277 break;
278  
279  
280 case CMD_GET_FREQ: // return set freq, ie no offset/mul
281 avr_freq.qw = set_frequency * (double)(1L << 21);
282 inPipes[0].pSrc.bRam = (BYTE*)&avr_freq.bytes[0]; // Set Source
283 inPipes[0].info.bits.ctrl_trf_mem = USB_EP0_RAM; // Set memory type
284 inPipes[0].wCount.v[0] = 4; // Set data count
285 inPipes[0].info.bits.busy = 1;
286 break;
287  
288 case CMD_GET_PPM: // return smooth tune in ppm
289 w.Val = Smooth_double * 1000000L;
290 inPipes[0].pSrc.bRam = (BYTE*)&w.v[0]; // Set Source
291 inPipes[0].info.bits.ctrl_trf_mem = USB_EP0_RAM; // Set memory type
292 inPipes[0].wCount.v[0] = 2; // Set data count
293 inPipes[0].info.bits.busy = 1;
294 break;
295  
296 case CMD_GET_STARTUP: // return init startup freq
297  
298 #if defined(UBW)
299 for (i=0; i<4; i++) avr_freq.bytes[i] = Read_b_eep(i + F_INIT_FREQ +1);
300 #elif defined(UBW32)
301 for (i=0; i<4; i++) {
302 DataEERead(&value, (i + F_INIT_FREQ +1));
303 avr_freq.bytes[i] = value;
304 };
305 #endif
306  
307 inPipes[0].pSrc.bRam = (BYTE*)&avr_freq.bytes[0]; // Set Source
308 inPipes[0].info.bits.ctrl_trf_mem = USB_EP0_RAM; // Set memory type
309 inPipes[0].wCount.v[0] = 4; // Set data count
310 inPipes[0].info.bits.busy = 1;
311 break;
312  
313 case CMD_GET_XTAL: // return fcryst
314 inPipes[0].pSrc.bRam = (BYTE*)&fcryst_freq.bytes[0]; // Set Source
315 inPipes[0].info.bits.ctrl_trf_mem = USB_EP0_RAM; // Set memory type
316 inPipes[0].wCount.v[0] = 4; // Set data count
317 inPipes[0].info.bits.busy = 1;
318 break;
319  
320 case CMD_GET_SI570: // SI570: read out frequency control registers
321 i2c_adr = SetupPkt.W_Value.v[0];
322 ReadRegs();
323 inPipes[0].pSrc.bRam = (BYTE*)&registers[0]; // Set Source
324 inPipes[0].info.bits.ctrl_trf_mem = USB_EP0_RAM; // Set memory type
325 inPipes[0].wCount.v[0] = 6; // Set data count
326 inPipes[0].info.bits.busy = 1;
327 break;
328  
329 case CMD_GET_I2C_ERR: // return # of i2c errors, set to zero always
330 replybuf[0] = 0; // No errors :)
331 inPipes[0].pSrc.bRam = (BYTE*)&replybuf[0]; // Set Source
332 inPipes[0].info.bits.ctrl_trf_mem = USB_EP0_RAM; // Set memory type
333 inPipes[0].wCount.v[0] = 1; // Set data count
334 inPipes[0].info.bits.busy = 1;
335 break;
336  
337  
338 case CMD_SET_I2C_ADDR: // Reset EEPROM to default values if 255
339 // Otherwise just set i2c address
340  
341 if (SetupPkt.W_Value.v[0] == 0xff){
342 #if defined (UBW)
343 Write_b_eep(F_CAL_DONE, 0xff);
344 Busy_eep();
345 Write_b_eep(F_INIT_FREQ, 0xff);
346 Busy_eep();
347 Write_b_eep(F_SMOOTH, 0xff);
348 Busy_eep();
349 Write_b_eep(F_SUB_MUL, 0xff);
350 Busy_eep();
351 Write_b_eep(F_CROSS_OVER, 0xff);
352 Busy_eep();
353 #elif defined (UBW32)
354 DataEEWrite( (unsigned int) 0xff, F_CAL_DONE);
355 DataEEWrite( (unsigned int) 0xff, F_INIT_FREQ);
356 DataEEWrite( (unsigned int) 0xff, F_SMOOTH);
357 DataEEWrite( (unsigned int) 0xff, F_SUB_MUL);
358 DataEEWrite( (unsigned int) 0xff, F_CROSS_OVER);
359 #endif
360  
361 i2c_adr = 0x55;
362 }
363 else {
364 i2c_adr = SetupPkt.W_Value.v[0];
365 };
366  
367 replybuf[0] = 0x00;
368 inPipes[0].pSrc.bRam = (BYTE*)&replybuf[0]; // Set Source
369 inPipes[0].info.bits.ctrl_trf_mem = USB_EP0_RAM; // Set memory type
370 inPipes[0].wCount.v[0] = 1; // Set data count
371 inPipes[0].info.bits.busy = 1;
372 break;
373  
374  
375  
376 case CMD_GET_VERSION: // return version 15.10
377 replybuf[0] = VERSION_MINOR;
378 replybuf[1] = VERSION_MAJOR;
379 inPipes[0].pSrc.bRam = (BYTE*)&replybuf[0]; // Set Source
380 inPipes[0].info.bits.ctrl_trf_mem = USB_EP0_RAM; // Set memory type
381 inPipes[0].wCount.v[0] = 2; // Set data count
382 inPipes[0].info.bits.busy = 1;
383 break;
384  
385 default: // Command not supported
386  
387 replybuf[0] = 0xff;
388 inPipes[0].pSrc.bRam = (BYTE*)&replybuf[0] ; // Set Source
389 inPipes[0].info.bits.ctrl_trf_mem = USB_EP0_RAM; // Set memory type
390 inPipes[0].wCount.v[0] = 1; // Set data count
391 inPipes[0].info.bits.busy = 1;
392 break;
393 }//end switch
394  
395  
396 }
397  
398  
399 void Read_Command_Into_Buffer(void)
400 {
401  
402 if (command_count >= COMMAND_BUFFER_SIZE){ // buffer full
403 current_command_out++; // discards oldest command
404 if (current_command_out >= COMMAND_BUFFER_SIZE) current_command_out = 0;
405 command_count--; // now one command less (discarded)
406 };
407 command_buffer[current_command_in].command = command;
408 command_buffer[current_command_in].wCount = wCount;
409 // data already copied to command_buffer before this outpipe pFunc is called
410 current_command_in++;
411 if (current_command_in >= COMMAND_BUFFER_SIZE) current_command_in = 0;
412 command_count++;
413 }