Rev Author Line No. Line
1661 kaklik 1 /********************************************************************
2 FileName: user.c
3 Dependencies: See INCLUDES section
4 Processor: PIC18 or PIC24 USB Microcontrollers
5 Hardware: The code is natively intended to be used on the following
6 hardware platforms: PICDEM™ FS USB Demo Board,
7 PIC18F87J50 FS USB Plug-In Module, or
8 Explorer 16 + PIC24 USB PIM. The firmware may be
9 modified for use on other USB platforms by editing the
10 HardwareProfile.h file.
11 Complier: Microchip C18 (for PIC18) or C30 (for PIC24)
12 * Company: Microchip Technology, Inc.
13 *
14 * Software License Agreement
15  
16 The software supplied herewith by Microchip Technology Incorporated
17 (the “Company”) for its PIC® Microcontroller is intended and
18 supplied to you, the Company’s customer, for use solely and
19 exclusively on Microchip PIC Microcontroller products. The
20 software is owned by the Company and/or its supplier, and is
21 protected under applicable copyright laws. All rights are reserved.
22 * Any use in violation of the foregoing restrictions may subject the
23 * user to criminal sanctions under applicable laws, as well as to
24 * civil liability for the breach of the terms and conditions of this
25 * license.
26 *
27 * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
28 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
29 * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
30 * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
31 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
32 CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
33  
34 ********************************************************************
35 File Description:
36  
37 Change History:
38 Rev Date Description
39 1.0 11/19/2004 Initial release
40 2.1 02/26/2007 Updated for simplicity and to use common
41 coding style
42 ********************************************************************/
43  
44 /** INCLUDES *******************************************************/
45 #include "usb.h"
46  
47 #include "HardwareProfile.h"
48 #include "user.h"
49 #include "usbavrcmd.h"
50 #include <math.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <ctype.h>
54  
55 #if defined(__18CXX)
56 #include <delays.h>
57 #include <i2c.h>
58 #include <eep.h>
59 #elif defined(__PIC32MX__)
60 #include <peripheral/i2c.h>
61 #include <dee_emulation/dee_emulation_pic32.h>
62 #endif
63  
64 #if defined (UBW)
65 #pragma romdata dataEEPROM=0xF00000
66 // F_CAL_DONE, 4 bytes cal data, F_INIT_FREQ, 4 bytes freq, F_SMOOTH, 2 bytes
67 // F_SUB_MUL, 4 bytes sub and 4 bytes mul
68 // F_CROSS_OVER, 16 bytes or 8 words of 7 cross over points and 1 flag for BPF
69 // followed by 16 bytes or 8 words of 7 cross over points and 1 flag for LPF
70 // F_BLINK_LED, 1 byte boolean
71 rom unsigned char init_data[] = {0xff, 0,0,0,0, 0xff, 0,0,0,0, 0xff, 0,0,
72 0xff, 0,0,0,0,0,0,0,0,
73 0xff, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,
74 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
75 TRUE};
76 #endif
77  
78 /** V A R I A B L E S ********************************************************/
79 #pragma udata
80 BYTE old_SW;
81  
82  
83 BYTE i2c_adr;
84 BYTE command;
85 BYTE replybuf[8];
86 WORD wCount;
87 BYTE abpf_flag;
88  
89 COMMAND_BUFFER_t command_buffer[COMMAND_BUFFER_SIZE];
90 BYTE current_command_in, current_command_out;
91 BYTE command_count;
92  
93 avr_freq_t avr_freq, fcryst_freq; // avr freq [MHz]*2^21
94 // fcryst freq [Mhz]*2^24
95 unsigned short R137, R135 = 0;
96 unsigned char registers[6];
97 unsigned char tempBuf[8];
98 unsigned char counter;
99 double delta_rfreq;
100 double rfreq, Old_rfreq;
101 double fcryst_double, Old_freq_double, Smooth_double;
102  
103 double set_frequency;
104 avr_freq_t f_mul;
105 offset_t f_sub;
106 unsigned char validCombo;
107  
108  
109  
110 #if defined(__18F14K50) || defined(__18F13K50) || defined(__18LF14K50) || defined(__18LF13K50)
111 #pragma udata usbram2
112 #elif defined(__18F2455) || defined(__18F2550) || defined(__18F4455) || defined(__18F4550)\
113 || defined(__18F4450) || defined(__18F2450)\
114 || defined(__18F2458) || defined(__18F2453) || defined(__18F4558) || defined(__18F4553)
115 #pragma udata USB_VARIABLES=0x500
116 #else
117 #pragma udata
118 #endif
119  
120  
121 #pragma udata
122  
123 BOOL blinkStatusValid = TRUE;
124  
125  
126 /** P R I V A T E P R O T O T Y P E S ***************************************/
127  
128 void BlinkUSBStatus(void);
129 BOOL SwitchIsPressed(void);
130 void ServiceRequests(void);
131  
132  
133  
134  
135  
136 /** D E C L A R A T I O N S **************************************************/
137 #pragma code
138  
139 float Cross2Switch(WORD_VAL val){ // convert from 11.5 bit format in [Mhz]
140 float whole, fraction;
141 whole = (float) (val.Val >> 5);
142 fraction = ((float) (val.Val & 0x001f)) / 32.0;
143 return (whole + fraction);
144 }
145  
146 WORD_VAL Switch2Cross(float val){ // convert from float to 11.5 bit format [Mhz]
147 WORD_VAL w;
148 unsigned int i;
149 i = val;
150 w.Val = i * 32.0;
151 w.Val += (val - (float) i) * 32.0;
152 return (w);
153 }
154  
155  
156 void UserInit(void)
157 {
158 WORD_VAL w;
159  
160 //#if defined (UBW)
161 // unsigned char i;
162 //#elif
163 unsigned int i;
164 unsigned int value;
165 //#endif
166  
167 #if defined(UBW)
168 // Port A - RA0 BPF_S0, RA1 BPF_S1, RA2 RXTX, RA3-5 LPF0-2
169 LATA = 0x00;
170 TRISA = 0x00; // 00000000
171  
172 // Turn all analog inputs into digital inputs
173 ADCON1 = 0x0F;
174 // Turn off the ADC
175 ADCON0bits.ADON = 0;
176 CMCON = 0x07; // Comparators as digital inputs
177 // RB0-1 for i2c, RB6-7 Paddle dit/dah, RB2-5 LPF 3-6
178 LATB = 0x00;
179 TRISB = 0xc3; // 11000011
180 INTCON2bits.RBPU = 0; // enable RB weak internal pullup
181 // Make all of PORTC inputs
182 LATC = 0x00;
183 TRISC = 0xFF;
184  
185 mInitAllLEDs();
186 mInitSwitch();
187 old_SW = UserSW;
188  
189 #elif defined(UBW32)
190  
191 // gO through each I/O register, setting them all to digital i/o
192 // and making none of them open drain and turning off all pullups and
193 // setting all of the latches to zero. We have PORTA through PORTG on
194 // this chip. That's 7 total.
195  
196  
197 LATA = 0x0000;
198 TRISA = 0x0000;
199 ODCA = 0x0000;
200 LATB = 0x0000;
201 TRISB = 0x0000;
202 ODCB = 0x0000;
203 LATC = 0x0000;
204 TRISC = 0x0000;
205 ODCC = 0x0000;
206 LATD = 0x0000;
207 TRISD = 0x0000;
208 ODCD = 0x0000;
209 LATE = 0x0000;
210 TRISE = 0x0000;
211 ODCE = 0x0000;
212 LATF = 0x0000;
213 TRISF = 0x0030; // RF4-5 paddle input
214 ODCF = 0x0000;
215 CNPUE = 0x060000; // Pull up on CNPUE17-18, corresponding to RF4-5
216 LATG = 0x0000;
217 TRISG = 0x0000;
218 ODCG = 0x0000;
219  
220 //Initialize all of the LED pins
221 mInitAllLEDs();
222  
223 mInitAllSwitches();
224 old_SW = UserSW;
225  
226 // Initialize Data EEPROM Emulation
227 if (DataEEInit()) {
228 mLED_4_On(); // Error occured
229 }
230 else {
231 mLED_4_Off();
232 };
233  
234 #endif
235  
236 i2c_adr = DEFAULT_I2C_ADDRESS;
237  
238 // check for previous calibration, which sets fcryst, the actual crystal freq
239 #if defined (UBW)
240 if (Read_b_eep(F_CAL_DONE) != F_CAL_DONE_VALUE){ // cal not done before, use default
241 fcryst_freq.qw = (double) DEFAULT_FCRYST * (double) (1L << 24); // 114.285 Mhz
242 }
243 else { // cal done before, read into fcryst
244 for (i=0; i<4; i++) fcryst_freq.bytes[i] = Read_b_eep(i + F_CAL_DONE +1);
245 };
246  
247 #elif defined (UBW32)
248 DataEERead(&value, F_CAL_DONE);
249 if ( value != F_CAL_DONE_VALUE){ // cal not done before, use default
250 fcryst_freq.qw = (double) DEFAULT_FCRYST * (double) (1L << 24); // 114.285 Mhz
251 }
252 else { // cal done before, read into fcryst
253 DataEERead(&value, (F_CAL_DONE +1));
254 fcryst_freq.qw = value;
255 };
256 #endif
257  
258 // Now that fcryst is checked, set it first so that it can be used by startup freq setting
259  
260 fcryst_double = (double) fcryst_freq.qw / (double) (1L << 24);
261 validCombo = 1;
262 command_count = 0;
263 current_command_in = 0;
264 current_command_out = 0;
265 Old_freq_double = 0;
266  
267 // check for previous startup freq setting
268 #if defined (UBW)
269 if (Read_b_eep(F_INIT_FREQ) != F_INIT_FREQ_VALUE){ // not set before, use default
270 avr_freq.qw = (double) DEFAULT_INIT_FREQ * (double) (1L << 21);
271 }
272 else { // startup freq set before, read into avr
273 for (i=0; i<4; i++) avr_freq.bytes[i] = Read_b_eep(i + F_INIT_FREQ +1);
274 };
275  
276 #elif defined (UBW32)
277 DataEERead(&value, F_INIT_FREQ);
278 if ( value != F_INIT_FREQ_VALUE){ // not set before, use default
279 avr_freq.qw = (double) DEFAULT_INIT_FREQ * (double) (1L << 21);
280 }
281 else { // set before, read
282 DataEERead(&value, (F_INIT_FREQ +1));
283 avr_freq.qw = value;
284 };
285  
286 #endif
287  
288  
289  
290 // check for previous smooth setting
291 #if defined (UBW)
292 if (Read_b_eep(F_SMOOTH) != F_SMOOTH_VALUE){ // not set before, use default
293 Smooth_double = (double) DEFAULT_SMOOTH / 1000000L; // in ppm
294 }
295 else { // set before, read
296 for (i=0; i<2; i++) w.v[i] = Read_b_eep(i + F_SMOOTH +1);
297 Smooth_double = (double) w.Val / 1000000L;
298 };
299  
300 #elif defined (UBW32)
301 DataEERead(&value, F_SMOOTH);
302 if ( value != F_SMOOTH_VALUE){ // not set before, use default
303 Smooth_double = (double) DEFAULT_SMOOTH / 1000000L;
304 }
305 else { // set before, read
306 DataEERead(&value, (F_SMOOTH +1));
307 w.Val = value;
308 Smooth_double = (double) w.Val / 1000000L;
309 }
310 #endif
311  
312 // check for previous sub mul setting
313 #if defined (UBW)
314 if (Read_b_eep(F_SUB_MUL) != F_SUB_MUL_VALUE){ // not set before, use default
315 f_sub.qw = (double) DEFAULT_SUB * (double) (1L << 21);
316 f_mul.qw = (double) DEFAULT_MUL * (double) (1L << 21);
317 }
318 else { // startup freq set before, read into avr
319 for (i=0; i<4; i++) f_sub.bytes[i] = Read_b_eep(i + F_SUB_MUL +1);
320 for (i=0; i<4; i++) f_mul.bytes[i] = Read_b_eep(i + F_SUB_MUL +5);
321 };
322  
323 #elif defined (UBW32)
324 DataEERead(&value, F_SUB_MUL);
325 if ( value != F_SUB_MUL_VALUE){ // not set before, use default
326 f_sub.qw = (double) DEFAULT_SUB * (double) (1L << 21);
327 f_mul.qw = (double) DEFAULT_MUL * (double) (1L << 21);
328 }
329 else { // set before, read
330 DataEERead(&value, (F_SUB_MUL +1));
331 f_sub.qw = value;
332 DataEERead(&value, (F_SUB_MUL +2));
333 f_mul.qw = value;
334 };
335  
336 #endif
337  
338  
339  
340  
341  
342 // Check for Cross Over Points
343 #if defined (UBW)
344 if (Read_b_eep(F_CROSS_OVER) != F_CROSS_OVER_VALUE){ // not set before, use default
345  
346 #if defined (YAS)
347 FilterSwitchOver[0] = (2.4 - DEFAULT_SUB) * DEFAULT_MUL * 4.0; // default BPF switchover points
348 FilterSwitchOver[1] = (8.5 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
349 FilterSwitchOver[2] = (19.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
350 FilterSwitchOver[3] = (19.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
351 FilterSwitchOver[4] = (19.0 - DEFAULT_SUB) * DEFAULT_MUL* 4.0;
352 FilterSwitchOver[5] = (19.0 - DEFAULT_SUB) * DEFAULT_MUL* 4.0;
353 FilterSwitchOver[6] = (19.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
354 #else
355 FilterSwitchOver[0] = (2.4 - DEFAULT_SUB) * DEFAULT_MUL * 4.0; // default BPF switchover points
356 FilterSwitchOver[1] = (8.5 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
357 FilterSwitchOver[2] = (19.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
358 #endif
359 for (i = 0; i < (NUM_BPF - 1); i++) FilterCrossOver[i] = Switch2Cross(FilterSwitchOver[i]);
360 FilterCrossOver[(NUM_BPF-1)].Val = 1; // Enabled
361 abpf_flag = 1;
362  
363 #if defined (K5OOR)
364 LPFSwitchOver[0] = (2.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
365 LPFSwitchOver[1] = (4.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
366 LPFSwitchOver[2] = (7.45 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
367 LPFSwitchOver[3] = (15.0 - DEFAULT_SUB) * DEFAULT_MUL* 4.0;
368 LPFSwitchOver[4] = (21.5 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
369 LPFSwitchOver[5] = (30.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
370 LPFSwitchOver[6] = (30.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
371 #elif defined (ALEX)
372 LPFSwitchOver[0] = (2.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
373 LPFSwitchOver[1] = (4.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
374 LPFSwitchOver[2] = (9.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
375 LPFSwitchOver[3] = (11.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
376 LPFSwitchOver[4] = (14.5 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
377 LPFSwitchOver[5] = (20.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
378 LPFSwitchOver[6] = (30.0 - DEFAULT_SUB) * DEFAULT_MUL* 4.0;
379 #elif defined (MARC)
380 LPFSwitchOver[0] = (2.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
381 LPFSwitchOver[1] = (4.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
382 LPFSwitchOver[2] = (8.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
383 LPFSwitchOver[3] = (11.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
384 LPFSwitchOver[4] = (14.5 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
385 LPFSwitchOver[5] = (18.2 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
386 LPFSwitchOver[6] = (21.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
387 #else
388 #error "Must define an LPF configuration."
389 #endif
390  
391 for (i = 0; i < 7; i++) LPFCrossOver[i] = Switch2Cross(LPFSwitchOver[i]);
392 LPFCrossOver[7].Val = 1; // Enabled
393 }
394 else { // set before, read
395 for (i = 0; i < NUM_BPF; i++){
396 w.v[0] = Read_b_eep(2 * i + F_CROSS_OVER +1);
397 w.v[1] = Read_b_eep(2 * i + 1 + F_CROSS_OVER + 1);
398 FilterCrossOver[i].Val = w.Val;
399 };
400  
401 abpf_flag = FilterCrossOver[(NUM_BPF-1)].v[0];
402  
403 for (i = 0; i < 8; i++){
404 w.v[0] = Read_b_eep(2 * i + F_CROSS_OVER +17);
405 w.v[1] = Read_b_eep(2 * i + 1 + F_CROSS_OVER + 17);
406 LPFCrossOver[i].Val = w.Val;
407 };
408  
409 }
410  
411 #elif defined (UBW32)
412 DataEERead(&value, F_CROSS_OVER);
413 if ( value != F_CROSS_OVER_VALUE){ // not set before, use default
414 FilterSwitchOver[0] = (2.4 - DEFAULT_SUB) * DEFAULT_MUL * 4.0; // default BPF switchover points
415 FilterSwitchOver[1] = (8.5 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
416 FilterSwitchOver[2] = (19.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
417 for (i = 0; i < 3; i++) FilterCrossOver[i] = Switch2Cross(FilterSwitchOver[i]);
418 FilterCrossOver[3].Val = 1; // Enabled
419 abpf_flag = 1;
420  
421 #if defined (K5OOR)
422 LPFSwitchOver[0] = (2.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
423 LPFSwitchOver[1] = (4.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
424 LPFSwitchOver[2] = (7.45 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
425 LPFSwitchOver[3] = (15.0 - DEFAULT_SUB) * DEFAULT_MUL* 4.0;
426 LPFSwitchOver[4] = (21.5 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
427 LPFSwitchOver[5] = (30.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
428 LPFSwitchOver[6] = (30.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
429 #elif defined (ALEX)
430 LPFSwitchOver[0] = (2.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
431 LPFSwitchOver[1] = (4.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
432 LPFSwitchOver[2] = (9.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
433 LPFSwitchOver[3] = (11.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
434 LPFSwitchOver[4] = (14.5 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
435 LPFSwitchOver[5] = (20.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
436 LPFSwitchOver[6] = (30.0 - DEFAULT_SUB) * DEFAULT_MUL* 4.0;
437 #elif defined (MARC)
438 LPFSwitchOver[0] = (2.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
439 LPFSwitchOver[1] = (4.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
440 LPFSwitchOver[2] = (8.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
441 LPFSwitchOver[3] = (11.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
442 LPFSwitchOver[4] = (14.5 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
443 LPFSwitchOver[5] = (18.2 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
444 LPFSwitchOver[6] = (21.0 - DEFAULT_SUB) * DEFAULT_MUL * 4.0;
445 #else
446 #error "Must define an LPF configuration."
447 #endif
448  
449 for (i = 0; i < 7; i++) LPFCrossOver[i] = Switch2Cross(LPFSwitchOver[i]);
450 LPFCrossOver[7].Val = 1; // Enabled
451  
452  
453 }
454 else { // set before, read
455 for (i=0; i< NUM_BPF; i++) {
456 DataEERead(&value, (i + F_CROSS_OVER +1));
457 FilterCrossOver[i].Val = value;
458 };
459  
460  
461 abpf_flag = FilterCrossOver[(NUM_BPF-1)].Val;
462  
463 for (i=0; i<8; i++) {
464 DataEERead(&value, (i + F_CROSS_OVER +9));
465 LPFCrossOver[i].Val = value;
466 };
467 };
468 #endif // UBW32
469  
470 for (i = 0; i < (NUM_BPF-1); i++) FilterSwitchOver[i] = Cross2Switch(FilterCrossOver[i]);
471 for (i = 0; i < 7; i++) LPFSwitchOver[i] = Cross2Switch(LPFCrossOver[i]);
472  
473 // End initialising filter switchover points
474  
475 #if defined(UBW)
476 blinkStatusValid = Read_b_eep(F_BLINK_LED);
477 #elif defined (UBW32)
478 DataEERead(&value, F_BLINK_LED);
479 blinkStatusValid = value;
480 #endif
481  
482 #if defined (UBW)
483 OpenI2C(MASTER, SLEW_ON);// Initialize I2C module
484 SSPADD = 48; //400kHz Baud clock(9) @16MHz
485 //100kHz Baud clock(39) @16MHz
486  
487 #elif defined (UBW32)
488 OpenI2C1(I2C_ON, ( GetPeripheralClock() / 400000UL - 2) );
489 #endif
490  
491 // IF we don't reset Si570 on startup, it will not hang if Si570 not connected
492 #if defined (INIT_SI570_ON_STARTUP)
493 Reset_Si570();
494 #endif
495  
496 // check for previous startup freq setting, if set, then set Si570 to startup freq
497 #if defined (UBW32)
498 DataEERead(&value, F_INIT_FREQ);
499 if ( value == F_INIT_FREQ_VALUE){
500 #else
501 if (Read_b_eep(F_INIT_FREQ) == F_INIT_FREQ_VALUE){
502 #endif
503 // avr_freq has been setup by the reading of the startup freq
504 set_frequency = (double) avr_freq.qw / (double)(1L << 21);
505 SetFrequency(set_frequency);
506 };
507  
508 }//end UserInit
509  
510  
511 /******************************************************************************
512 * Function: void ProcessIO(void)
513 *
514 * PreCondition: None
515 *
516 * Input: None
517 *
518 * Output: None
519 *
520 * Side Effects: None
521 *
522 * Overview: This function is a place holder for other user routines.
523 * It is a mixture of both USB and non-USB tasks.
524 *
525 * Note: None
526 *****************************************************************************/
527 void ProcessIO(void)
528 {
529 if (SwitchIsPressed()){
530 blinkStatusValid = !blinkStatusValid; // toggle blink led
531 #if defined(UBW)
532 Write_b_eep(F_BLINK_LED, blinkStatusValid);
533 #elif defined(UBW32)
534 DataEEWrite(blinkStatusValid, F_BLINK_LED);
535 #endif
536 };
537  
538 //Blink the LEDs according to the USB device status
539 if(blinkStatusValid) BlinkUSBStatus();
540 else mLED_Both_Off();
541 // User Application USB tasks
542 if((USBDeviceState < ADDRESS_STATE)||(USBSuspendControl==1)) return;
543  
544 //respond to any USB commands that might have come over the bus
545 ServiceRequests();
546  
547  
548 }//end ProcessIO
549  
550  
551 /******************************************************************************
552 * Function: void ServiceRequests(void)
553 *
554 * PreCondition: None
555 *
556 * Input: None
557 *
558 * Output: None
559 *
560 * Side Effects: USB traffic can be generated
561 *
562 * Overview: This function takes in the commands from the PC from the
563 * application and executes the commands requested
564 *
565 * Note: None
566 *****************************************************************************/
567 void ServiceRequests(void)
568 {
569 BYTE command_to_process;
570  
571  
572 if (command_count > 0) { // there is command in buffer
573 command_to_process = command_buffer[current_command_out].command;
574  
575 switch (command_to_process){
576 case CMD_SET_FREQ_REG:
577 Set_Register_Handler();
578 break;
579 case CMD_SET_LO_SM:
580 Set_Sub_Mul_Handler();
581 break;
582 case CMD_SET_FREQ:
583 Set_Freq_Handler();
584 break;
585 case CMD_SET_XTAL:
586 Set_Cal_Handler();
587 break;
588 case CMD_SET_STARTUP:
589 Set_Init_Freq_Handler();
590 break;
591 case CMD_SET_PPM:
592 Set_Smooth_Handler();
593 break;
594 };
595 // end switch
596 current_command_out++;
597 if (current_command_out >= COMMAND_BUFFER_SIZE) current_command_out = 0;
598 command_count--;
599 } // end command_count > 0
600  
601  
602 }//end ServiceRequests
603  
604 /********************************************************************
605 * Function: void BlinkUSBStatus(void)
606 *
607 * PreCondition: None
608 *
609 * Input: None
610 *
611 * Output: None
612 *
613 * Side Effects: None
614 *
615 * Overview: BlinkUSBStatus turns on and off LEDs
616 * corresponding to the USB device state.
617 *
618 * Note: mLED macros can be found in HardwareProfile.h
619 * USBDeviceState is declared and updated in
620 * usb_device.c.
621 *******************************************************************/
622 void BlinkUSBStatus(void)
623 {
624 static WORD led_count=0;
625  
626 if(led_count == 0)led_count = 10000U;
627 led_count--;
628  
629  
630 if(USBSuspendControl == 1)
631 {
632 if(led_count==0)
633 {
634 mLED_1_Toggle();
635 mLED_2 = mLED_1;
636 }//end if
637 }
638 else
639 {
640 if(USBDeviceState == DETACHED_STATE)
641 {
642 mLED_1_Off(); mLED_2_Off();
643 }
644 else if(USBDeviceState == ATTACHED_STATE)
645 {
646 mLED_1_On(); mLED_2_On();
647 }
648 else if(USBDeviceState == POWERED_STATE)
649 {
650 mLED_1_On(); mLED_2_Off();
651 }
652 else if(USBDeviceState == DEFAULT_STATE)
653 {
654 mLED_1_Off(); mLED_2_On();
655 }
656 else if(USBDeviceState == ADDRESS_STATE)
657 {
658 if(led_count == 0)
659 {
660 mLED_1_Toggle();
661 mLED_2_Off();
662 }//end if
663 }
664 else if(USBDeviceState == CONFIGURED_STATE)
665 {
666 if(led_count==0)
667 {
668 mLED_1_Toggle();
669 mLED_2 = !mLED_1;
670  
671 }//end if
672 }//end if(...)
673 }//end if(UCONbits.SUSPND...)
674  
675 }//end BlinkUSBStatus
676  
677  
678 /******************************************************************************
679 * Function: BOOL SwitchIsPressed(void)
680 *
681 * PreCondition: None
682 *
683 * Input: None
684 *
685 * Output: BOOL - TRUE if the SW2 was pressed and FALSE otherwise
686 *
687 * Side Effects: None
688 *
689 * Overview: returns TRUE if the SW2 was pressed and FALSE otherwise
690 *
691 * Note: None
692 *****************************************************************************/
693  
694 BOOL SwitchIsPressed(void)
695 {
696 if(UserSW != old_SW)
697 {
698 old_SW = UserSW; // Save new value
699 if(UserSW == 0) // If pressed
700 return TRUE; // Was pressed
701 }//end if
702 return FALSE; // Was not pressed
703 }//end SwitchIsPressed
704  
705 void Reset_Si570()
706 {
707 #if defined (UBW)
708 StartI2C(); //Reset Si570 to Startup
709 IdleI2C();
710 WriteI2C(i2c_adr << 1);
711 WriteI2C(135); //REG 135
712 WriteI2C(0x01); // reset
713 StopI2C();
714 IdleI2C();
715 #elif defined (UBW32)
716 StartI2C1(); //Reset Si570 to Startup
717 IdleI2C1();
718 MasterWriteI2C1(i2c_adr << 1);
719 MasterWriteI2C1(135); //REG 135
720 MasterWriteI2C1(0x01); // reset
721 StopI2C1();
722 IdleI2C1();
723 #endif
724  
725 }
726  
727 void ReadRegs()
728 {
729 unsigned int i;
730  
731 for(i=0;i<6;i++)
732 {
733  
734 #if defined (UBW)
735 StartI2C();
736 IdleI2C();
737 WriteI2C(i2c_adr << 1);
738 WriteI2C(i+7); //specify register
739 RestartI2C();
740 IdleI2C();
741 WriteI2C(i2c_adr << 1 | 0x01);
742 registers[i] = ReadI2C();
743 StopI2C();
744 IdleI2C();
745 #elif defined (UBW32)
746 StartI2C1();
747 IdleI2C1();
748 MasterWriteI2C1(i2c_adr << 1);
749 MasterWriteI2C1(i+7);
750 RestartI2C1();
751 IdleI2C1();
752 MasterWriteI2C1(i2c_adr << 1 | 0x01);
753 registers[i] = MasterReadI2C1();
754 StopI2C1();
755 IdleI2C1();
756 #endif
757 }
758 }
759  
760  
761 void Freeze () {
762 Prep_rd(137); //get current value
763 #if defined (UBW)
764 R137 = ReadI2C();
765 #elif defined (UBW32)
766 R137 = MasterReadI2C1();
767 #endif
768 R137 = R137 | 0x10; //turn on freeze
769 WriteBk();
770 }
771  
772 void Unfreeze () {
773 Prep_rd(137);
774 #if defined (UBW)
775 R137 = ReadI2C();
776 #elif defined (UBW32)
777 R137 = MasterReadI2C1();
778 #endif
779 R137 = R137 & 0xEF;
780 WriteBk();
781 }
782  
783 void WriteBk () { //Write back
784 #if defined (UBW)
785 StopI2C();
786 IdleI2C();
787 StartI2C();
788 IdleI2C();
789 WriteI2C(i2c_adr<<1);
790 WriteI2C(137); //REG
791 WriteI2C(R137); // new data
792 StopI2C();
793 IdleI2C();
794 #elif defined (UBW32)
795 StopI2C1();
796 IdleI2C1();
797 StartI2C1();
798 IdleI2C1();
799 MasterWriteI2C1(i2c_adr<<1);
800 MasterWriteI2C1(137); //REG
801 MasterWriteI2C1(R137); // new data
802 StopI2C1();
803 IdleI2C1();
804 #endif
805 }
806  
807 void Prep_rd (unsigned short r) { // get ready to read
808 #if defined (UBW)
809 StartI2C();
810 IdleI2C();
811 WriteI2C(i2c_adr<<1);
812 WriteI2C(r); //REG
813 RestartI2C();
814 IdleI2C();
815 WriteI2C(i2c_adr<<1 | 0x01);
816 #elif defined (UBW32)
817 StartI2C1();
818 IdleI2C1();
819 MasterWriteI2C1(i2c_adr<<1);
820 MasterWriteI2C1(r); //REG
821 RestartI2C1();
822 IdleI2C1();
823 MasterWriteI2C1(i2c_adr<<1 | 0x01);
824 #endif
825 }
826  
827 void NewF () {
828  
829 Prep_rd(135);
830  
831 #if defined (UBW)
832 R135 = ReadI2C();
833 R135 |= 0x40; // set New Data bit
834 StopI2C();
835 IdleI2C();
836 StartI2C();
837 IdleI2C();
838 WriteI2C(i2c_adr<<1);
839 WriteI2C(135); //REG
840 WriteI2C(R135);
841 StopI2C();
842 IdleI2C();
843 #elif defined (UBW32)
844 R135 = MasterReadI2C1();
845 R135 |= 0x40; // set New Data bit
846 StopI2C1();
847 IdleI2C1();
848 StartI2C1();
849 IdleI2C1();
850 MasterWriteI2C1(i2c_adr<<1);
851 MasterWriteI2C1(135); //REG
852 MasterWriteI2C1(R135);
853 StopI2C1();
854 IdleI2C1();
855 #endif
856 }
857  
858  
859  
860 void RunFreqProg(double f)
861 {
862 double rfreq_fraction;
863 unsigned long rfreq_integer_part;
864 unsigned long rfreq_fraction_part;
865 const float FDCO_MAX = 5670; //MHz
866 const float FDCO_MIN = 4850;
867  
868 // Register finding the lowest DCO frequenty - code from Fred
869 unsigned char xHS_DIV;
870 unsigned int xN1;
871 unsigned int xN;
872  
873 // Registers to save the found dividers
874 unsigned char sHS_DIV=0;
875 unsigned char sN1=0;
876 unsigned int sN=0; // Total dividing
877 unsigned int N0; // Total divider needed (N1 * HS_DIV)
878  
879 // Find the total division needed.
880 // It is always one too low (not in the case reminder is zero, reminder not used here).
881  
882 N0 = FDCO_MIN / (float) f;
883 sN = 11*128;
884 for(xHS_DIV = 11; xHS_DIV > 3; xHS_DIV--)
885 {
886 // Skip the unavailable divider's
887 if (xHS_DIV == 8 || xHS_DIV == 10)
888 continue;
889  
890 // Calculate the needed low speed divider
891 xN1 = N0 / xHS_DIV + 1;
892  
893 if (xN1 > 128)
894 continue;
895  
896 // Skip the unavailable divider's
897 if (xN1 != 1 && (xN1 & 1) == 1)
898 xN1 += 1;
899  
900 xN = xHS_DIV * xN1;
901 if (sN > xN)
902 {
903 sN = xN;
904 sN1 = xN1;
905 sHS_DIV = xHS_DIV;
906 }
907 };
908  
909 validCombo = 0;
910  
911 if (sHS_DIV == 0) return; // no valid dividers found
912  
913 rfreq = f * (double) sN; // DCO freq
914 if ((float)rfreq > FDCO_MAX) return; // calculated DCO freq > max
915  
916 validCombo = 1;
917  
918 // rfreq is a 38 bit number, MSB 10 bits integer portion, and LSB 28 fraction
919 // in the Si570 registers, tempBuf[1] has 6 bits, and tempBuf[2] has 4 bits of the integer portion
920  
921 rfreq /= fcryst_double; // DCO divided by fcryst
922 rfreq_integer_part = rfreq;
923 rfreq_fraction = rfreq - rfreq_integer_part;
924 rfreq_fraction_part = rfreq_fraction * (1L << 28);
925  
926 sHS_DIV -= 4;
927 sN1 -= 1;
928 tempBuf[0] = (sHS_DIV << 5) | (sN1 >> 2);
929 tempBuf[1] = (sN1 & 3) << 6;
930 tempBuf[1] |= ((rfreq_integer_part >> 4) & 0x3f);
931 tempBuf[2] = ((rfreq_integer_part & 0x0f) << 4) | (rfreq_fraction_part >> 24);
932 tempBuf[3] = rfreq_fraction_part >> 16;
933 tempBuf[4] = rfreq_fraction_part >> 8;
934 tempBuf[5] = rfreq_fraction_part;
935  
936 }
937  
938  
939 void SetNewFreq()
940 { int i;
941 double freq_double;
942 double delta_freq;
943  
944 if(validCombo)
945 {
946  
947 Freeze(); // freeze DCO
948  
949 for (i=7; i<=12; i++){ //Data to Si570
950 #if defined (UBW)
951 StartI2C();
952 IdleI2C();
953 WriteI2C(i2c_adr<<1);
954 WriteI2C(i); //specify register
955 WriteI2C(tempBuf[i-7]); // new data to registers
956 StopI2C();
957 IdleI2C();
958 #elif defined (UBW32)
959 StartI2C1();
960 IdleI2C1();
961 MasterWriteI2C1(i2c_adr<<1);
962 MasterWriteI2C1(i); //specify register
963 MasterWriteI2C1(tempBuf[i-7]); // new data to registers
964 StopI2C1();
965 IdleI2C1();
966 #endif
967 }
968 Unfreeze (); // thaw (unfreeze)
969  
970 // check for smooth tune range
971 freq_double = Freq_From_Register(fcryst_double);
972  
973 if (freq_double >= Old_freq_double) delta_freq = freq_double - Old_freq_double;
974 else delta_freq = Old_freq_double - freq_double;
975  
976 if (((delta_rfreq / Old_rfreq ) > Smooth_double) || (delta_freq > 0.5)){
977 NewF (); // indicate new freq. This will cause a pause in the Si570 output
978 Old_rfreq = rfreq;
979 Old_freq_double = freq_double;
980 };
981  
982 // set filters, using set freq without offset and multiplier
983  
984 if (abpf_flag) Set_BPF((float) set_frequency);
985 Set_LPF((float)set_frequency);
986  
987 }; // valid combo
988 }
989  
990 double Freq_From_Register(double fcryst){ // side effects: rfreq and delta_rfreq are set
991 double freq_double;
992 unsigned char n1;
993 unsigned char hsdiv;
994 unsigned long rfreq_integer_portion, rfreq_fraction_portion;
995  
996 // Now find out the current rfreq and freq
997  
998 hsdiv = ((tempBuf[0] & 0xE0) >> 5) + 4;
999 n1 = ((tempBuf[0] & 0x1f ) << 2 ) + ((tempBuf[1] & 0xc0 ) >> 6 );
1000 // if(n1 == 0) n1 = 1;
1001 // else if((n1 & 1) !=0) n1 += 1;
1002 n1 += 1;
1003  
1004 rfreq_integer_portion = ((unsigned long)(tempBuf[1] & 0x3f)) << 4 |
1005 ((unsigned long)(tempBuf[2] & 0xf0)) >> 4;
1006  
1007 rfreq_fraction_portion = ((unsigned long) (tempBuf[2] & 0x0f)) << 24;
1008 rfreq_fraction_portion += ((unsigned long)(tempBuf[3])) << 16;
1009 rfreq_fraction_portion += ((unsigned long)(tempBuf[4])) << 8;
1010 rfreq_fraction_portion += ((unsigned long)(tempBuf[5]));
1011  
1012 rfreq = (double)rfreq_integer_portion + ((double)rfreq_fraction_portion / (1L << 28));
1013  
1014 if (rfreq >= Old_rfreq) delta_rfreq = rfreq - Old_rfreq;
1015 else delta_rfreq = Old_rfreq - rfreq;
1016  
1017 freq_double = fcryst * rfreq / (double) hsdiv / (double) n1;
1018 return (freq_double);
1019 }
1020  
1021  
1022  
1023 void Set_BPF(float freq){ // note the freq used is the Si570 freq
1024  
1025 #if defined(YAS)
1026 if (freq < FilterSwitchOver[0]) {BPF_S2 = 0;BPF_S1 = 0; BPF_S0 = 0;}
1027 else if (freq < FilterSwitchOver[1]) {BPF_S2 = 0; BPF_S1=0;BPF_S0=1;}
1028 else if (freq < FilterSwitchOver[2]) {BPF_S2 = 0;BPF_S1=1;BPF_S0=0;}
1029 else if (freq < FilterSwitchOver[3]) {BPF_S2 = 0; BPF_S1 = 1; BPF_S0 = 1;}
1030 else if (freq < FilterSwitchOver[4]) {BPF_S2 = 1; BPF_S1 = 0; BPF_S0 = 0;}
1031 else if (freq < FilterSwitchOver[5]) {BPF_S2 = 1; BPF_S1 = 0; BPF_S0 = 1;}
1032 else if (freq < FilterSwitchOver[6]) {BPF_S2 = 1; BPF_S1 = 1; BPF_S0 = 0;}
1033 else {BPF_S2 = 1;BPF_S1=1; BPF_S0=1;};
1034  
1035 #else
1036 if (freq < FilterSwitchOver[0]) {BPF_S1 = 0; BPF_S0 = 0;}
1037 else if (freq < FilterSwitchOver[1]) {BPF_S1=0;BPF_S0=1;}
1038 else if (freq < FilterSwitchOver[2]) {BPF_S1=1;BPF_S0=0;}
1039 else {BPF_S1=1; BPF_S0=1;};
1040 #endif
1041 }
1042  
1043 void Set_LPF(float freq){
1044  
1045 unsigned char LPF_select;
1046  
1047 LPF_0 = 0;
1048 LPF_1 = 0;
1049 LPF_2 = 0;
1050  
1051 #if defined(YAS)
1052 if (freq < LPFSwitchOver[0]) {LPF_0 = 0; LPF_1 = 0; LPF_2 = 0; LPF_select = 0x01;}
1053 else if (freq < LPFSwitchOver[1]) {LPF_0 = 1; LPF_1 = 0; LPF_2 = 0; LPF_select = 0x02;}
1054 else if (freq < LPFSwitchOver[2]) {LPF_0 = 0; LPF_1 = 1; LPF_2 = 0; LPF_select = 0x04;}
1055 else if (freq < LPFSwitchOver[3]) {LPF_0 = 1; LPF_1 = 1; LPF_2 = 0; LPF_select = 0x08;}
1056 else if (freq < LPFSwitchOver[4]) {LPF_0 = 0; LPF_1 = 0; LPF_2 = 1; LPF_select = 0x10;}
1057 else if (freq < LPFSwitchOver[5]) {LPF_0 = 1; LPF_1 = 0; LPF_2 =1; LPF_select = 0x20;}
1058 else if (freq < LPFSwitchOver[6]) {LPF_0 = 0; LPF_1 = 1; LPF_2 = 1; LPF_select = 0x40;}
1059 else {LPF_0 = 1; LPF_1 = 1; LPF_2 = 1; LPF_select = 0x80;};
1060  
1061 #else
1062 LPF_3 = 0;
1063 LPF_4 = 0;
1064 LPF_5 = 0;
1065 LPF_6 = 0;
1066  
1067 if (freq < LPFSwitchOver[0]) {LPF_0 = 1; LPF_select = 0x01;}
1068 else if (freq < LPFSwitchOver[1]) {LPF_1 = 1; LPF_select = 0x02;}
1069 else if (freq <= LPFSwitchOver[2]) {LPF_2 = 1; LPF_select = 0x04;}
1070 else if (freq < LPFSwitchOver[3]) {LPF_3 = 1; LPF_select = 0x08;}
1071 else if (freq < LPFSwitchOver[4]) {LPF_4 = 1; LPF_select = 0x10;}
1072 else if (freq < LPFSwitchOver[5]) {LPF_5 = 1; LPF_select = 0x20;}
1073 else if (freq < LPFSwitchOver[6]) {LPF_6 = 1; LPF_select = 0x40;}
1074 else {LPF_6 = 1; LPF_select = 0x80;};
1075  
1076 #endif
1077  
1078  
1079  
1080 // Now use i2c bus to switch LPF
1081 #if defined (UBW)
1082 StartI2C();
1083 IdleI2C();
1084 WriteI2C(PCF8574 << 1);
1085 WriteI2C(LPF_select);
1086 StopI2C();
1087 IdleI2C();
1088 #elif defined (UBW32)
1089 StartI2C1();
1090 IdleI2C1();
1091 MasterWriteI2C1(PCF8574 << 1);
1092 MasterWriteI2C1(LPF_select);
1093 StopI2C1();
1094 IdleI2C1();
1095 #endif
1096  
1097  
1098 }
1099  
1100 void SetFrequency(double f)
1101 {
1102 // introduce the offset and mul here
1103 RunFreqProg((f - ((double)f_sub.qw / (double)(1L << 21))) * (double)f_mul.qw/(double)(1L <<21));
1104 SetNewFreq();
1105 }
1106  
1107 void Set_Freq_Handler(void){ // 4 byte freq value in avr_freq format
1108 BYTE i;
1109  
1110 if (command_buffer[current_command_out].wCount == 4){
1111 for (i=0; i<4; i++) avr_freq.bytes[i] = command_buffer[current_command_out].data[i];
1112 set_frequency = (double) avr_freq.qw / (double)(1L << 21);
1113 SetFrequency(set_frequency);
1114 }
1115 }
1116  
1117 void Set_Register_Handler(void){ // 6 byte register value
1118 unsigned char i;
1119  
1120 if (command_buffer[current_command_out].wCount == 6){
1121 for (i=0; i<6; i++) tempBuf[i] = command_buffer[current_command_out].data[i];
1122 set_frequency = Freq_From_Register(DEFAULT_FCRYST);
1123 SetFrequency(set_frequency);
1124 }
1125 }
1126  
1127 void Set_Cal_Handler(void){
1128  
1129 // 4 bytes of fcryst freq in avr_freq format
1130 int i;
1131  
1132 if (command_buffer[current_command_out].wCount == 4){
1133  
1134 for (i=0; i<4; i++)fcryst_freq.bytes[i] = command_buffer[current_command_out].data[i];
1135 fcryst_double = (double) fcryst_freq.qw / (double)(1L << 24);
1136  
1137 #if defined(UBW)
1138 for (i=0; i<4; i++){
1139 Write_b_eep (i+F_CAL_DONE+1, fcryst_freq.bytes[i]);
1140 Busy_eep ();
1141 };
1142 #elif defined (UBW32)
1143 DataEEWrite( (unsigned int) fcryst_freq.qw, (F_CAL_DONE + 1));
1144 #endif
1145  
1146 #if defined (UBW)
1147 Write_b_eep(F_CAL_DONE, F_CAL_DONE_VALUE);
1148 Busy_eep();
1149 #elif defined (UBW32)
1150 DataEEWrite(F_CAL_DONE_VALUE, F_CAL_DONE);
1151 #endif
1152 };
1153 }
1154  
1155 void Set_Init_Freq_Handler(void)
1156 {
1157 #if defined (UBW)
1158 unsigned char i;
1159 #else
1160 unsigned int i;
1161 #endif
1162  
1163 if (command_buffer[current_command_out].wCount == 4){
1164 for (i=0; i<4; i++) avr_freq.bytes[i] = command_buffer[current_command_out].data[i];
1165 #if defined (UBW)
1166 for (i=0; i<4; i++){
1167 Write_b_eep((i + F_INIT_FREQ +1), avr_freq.bytes[i]);
1168 Busy_eep();
1169 };
1170 #elif defined (UBW32)
1171 DataEEWrite( (unsigned int) avr_freq.qw, (F_INIT_FREQ +1));
1172 #endif
1173  
1174 #if defined (UBW)
1175 Write_b_eep(F_INIT_FREQ, F_INIT_FREQ_VALUE);
1176 Busy_eep();
1177 #elif defined (UBW32)
1178 DataEEWrite(F_INIT_FREQ_VALUE, F_INIT_FREQ);
1179 #endif
1180 };
1181 }
1182  
1183 void Set_Sub_Mul_Handler(void)
1184 {
1185 #if defined (UBW)
1186 unsigned char i;
1187 #else
1188 unsigned int i;
1189 #endif
1190  
1191 avr_freq_t old_f_mul;
1192 offset_t old_f_sub;
1193 double filter_value;
1194  
1195  
1196 if (command_buffer[current_command_out].wCount == 8){
1197  
1198 old_f_sub = f_sub; // save old values first
1199 old_f_mul = f_mul;
1200  
1201 for (i=0; i<4; i++) f_sub.bytes[i] = command_buffer[current_command_out].data[i];
1202 for (i=0; i<4; i++) f_mul.bytes[i] = command_buffer[current_command_out].data[i+4];
1203 #if defined (UBW)
1204 for (i=0; i<4; i++){
1205 Write_b_eep((i + F_SUB_MUL +1), f_sub.bytes[i]);
1206 Busy_eep();
1207 };
1208 for (i=0; i<4; i++){
1209 Write_b_eep((i + F_SUB_MUL +5), f_mul.bytes[i]);
1210 Busy_eep();
1211 };
1212 #elif defined (UBW32)
1213 DataEEWrite( (unsigned int) f_sub.qw, (F_SUB_MUL +1));
1214 DataEEWrite( (unsigned int) f_mul.qw, (F_SUB_MUL +5));
1215 #endif
1216  
1217 #if defined (UBW)
1218 Write_b_eep(F_SUB_MUL, F_SUB_MUL_VALUE);
1219 Busy_eep();
1220 #elif defined (UBW32)
1221 DataEEWrite(F_SUB_MUL_VALUE, F_SUB_MUL);
1222 #endif
1223  
1224 // Now update the filter switchover points as well
1225 for (i = 0; i < (NUM_BPF - 1); i++){
1226 // get back filter_value in Mhz
1227 filter_value = FilterSwitchOver[i] / (old_f_mul.qw / ((double) (1L << 21))) / 4 + (old_f_sub.qw / ((double) (1L << 21)));
1228 // now convert to new translated values
1229 FilterSwitchOver[i] = (filter_value - (f_sub.qw / (double)(1L << 21))) * (f_mul.qw / (double)(1L << 21)) * 4;
1230 FilterCrossOver[i] = Switch2Cross(FilterSwitchOver[i]);
1231 };
1232 for (i = 0; i < 7; i++){
1233 // get back filter_value in Mhz
1234 filter_value = LPFSwitchOver[i] / (old_f_mul.qw / ((double) (1L << 21))) / 4 + (old_f_sub.qw / ((double) (1L << 21)));
1235 // now convert to new translated values
1236 LPFSwitchOver[i] = (filter_value - (f_sub.qw / (double)(1L << 21))) * (f_mul.qw / (double)(1L << 21)) * 4;
1237 LPFCrossOver[i] = Switch2Cross(LPFSwitchOver[i]);
1238 };
1239 }; // if wCount == 8
1240 }
1241  
1242  
1243  
1244  
1245 void Set_Smooth_Handler(void)
1246 {
1247 WORD_VAL w;
1248 unsigned int i;
1249  
1250 if (command_buffer[current_command_out].wCount == 2){ // 2 bytes of Smooth Tune value in ppm
1251 w.v[0] = command_buffer[current_command_out].data[0];
1252 w.v[1] = command_buffer[current_command_out].data[1];
1253  
1254 Smooth_double = (double) w.Val / 1000000L;
1255  
1256 #if defined (UBW)
1257 for (i=0; i<2; i++){
1258 Write_b_eep (i+F_SMOOTH+1, w.v[i]);
1259 Busy_eep ();
1260 };
1261  
1262 #elif defined (UBW32)
1263 DataEEWrite( (unsigned int) w.Val, (i + F_SMOOTH + 1));
1264 #endif
1265  
1266 #if defined (UBW)
1267 Write_b_eep(F_SMOOTH, F_SMOOTH_VALUE);
1268 Busy_eep();
1269 #elif defined (UBW32)
1270 DataEEWrite( F_SMOOTH_VALUE, F_SMOOTH);
1271 #endif
1272  
1273 }
1274 }
1275  
1276  
1277  
1278 /** EOF user.c ***************************************************************/