-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.c
3007 lines (2716 loc) · 75.5 KB
/
user.c
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*********************************************************************
*
* Microchip USB C18 Firmware Version 1.0
*
*********************************************************************
* FileName: user.c
* Dependencies: See INCLUDES section below
* Processor: PIC18
* Compiler: C18 2.30.01+
* Company: Microchip Technology, Inc.
*
* Software License Agreement
*
* The software supplied herewith by Microchip Technology Incorporated
* (the “Company”) for its PICmicro® Microcontroller is intended and
* supplied to you, the Company’s customer, for use solely and
* exclusively on Microchip PICmicro Microcontroller products. The
* software is owned by the Company and/or its supplier, and is
* protected under applicable copyright laws. All rights are reserved.
* Any use in violation of the foregoing restrictions may subject the
* user to criminal sanctions under applicable laws, as well as to
* civil liability for the breach of the terms and conditions of this
* license.
*
* THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
* WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
* TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
* IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
* CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
*
* Author Date Comment
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Rawin Rojvanit 11/19/04 Original.
* Brian Schmalz 03/15/06 Added user code to impliment
* firmware version D v1.0 for UBW
* project. See www.greta.dhs.org/UBW
* Brian Schmalz 05/04/06 Starting version 1.1, which will
* include several fixes. See website.
* BPS 06/21/06 Starting v1.2 -
* - Fixed problem with I packets (from T command) filling up TX buffer
* and not letting any incoming commands be received. (strange)
* - Adding several commands - Analog inputs being the biggest set.
* - Also Byte read/Byte write (PEEK/POKE) anywhere in memory
* - Individual pin I/O and direction
* BPS 08/16/06 v1.3 - Fixed bug with USB startup
* BPS 09/09/06 v1.4 - Starting 1.4
* - Fixed Microchip bug with early silicon - UCONbits.PKTDIS = 0;
* - Adding BO and BC commands for parallel output to graphics pannels
* BPS 12/06/06 v1.4 - More work on 1.4
* - Re-wrote all I/O buffering code for increased speed and functionality
* - Re-wrote error handling code
* - Added delays to BC/BO commands to help Corey
* BPS 01/06/07 v1.4 - Added RC command for servos
* BPS 03/07/07 v1.4.1 - Changed blink rate for SFE
* BPS 05/24/07 v1.4.2 - Fixed RC command bug - it
* wouldn't shut off.
* BPS 08/28/07 v1.4.3 - Allowed UBW to run without
* usb connected.
* BPS 02/12/10 v1.4.4 - Fixed bug in C command
* all 10 analogs now enabled
* BPS 04/04/10 v1.4.5 - Fixed bug in PI command
* now just sends 1 or 0 back
* BPS/JA 06/11/10 v1.4.6 - Added F (Frequency) command
* BPS 02/11/11 v1.4.7 - Added USB buffer checking code
* so that you can't overflow the TX buf
* Also switched to USB_INTERRUPT rather
* than polling.
* BPS 02/20/11 v1.4.7 - Rebuilt c018i_BL.o to remove
* extra GOTO command at 0x0000
* BPS 02/27/11 v1.4.8 - Fixed BS command so it can take
* all binary data
* BPS 03/28/11 v1.4.8 - No functionality change, just
* modified main.c, linker scripts, and
* project file to allow for easy conversion
* to compiling without the bootloader support.
* (i.e. stand-alone). Simply comment out
* #define PROGRAMMABLE_WITH_USB_MCHPUSB_BOOTLOADER
* in HardwareProfile_UBW.h and set your
* proper crystal speed in config bits in main.c
* Also remove the /uPROGRAMMABLE_WITH_USB_MCHPUSB_BOOTLOADER
* from the Project->Build Options->Project->MPLINK Linker->Use Alternate Settings
* command line.
* BPS 05/07/11 v1.4.9 - Added fourth paramter (optional) to
* "F" command - duty cycle, from 1 to 99 as a percentage
*
********************************************************************/
/** I N C L U D E S **********************************************************/
#include <p18cxxx.h>
#include <usart.h>
#include <stdio.h>
#include <ctype.h>
#include <delays.h>
#include "Usb\usb.h"
#include "Usb\usb_function_cdc.h"
#include "GenericTypeDefs.h"
#include "Compiler.h"
#include "usb_config.h"
#include "Usb\usb_device.h"
#include "HardwareProfile.h"
#include "user.h"
/** D E F I N E S ********************************************************/
#define kUSART_TX_BUF_SIZE 32 // In bytes
#define kUSART_RX_BUF_SIZE 32 // In bytes
#define kISR_FIFO_A_DEPTH 3
#define kISR_FIFO_D_DEPTH 3
#define kPR2_RELOAD 250 // For 1ms TMR2 tick
#define kCR 0x0D
#define kLF 0x0A
//#define F_COMMAND_EXTRA_CYCLES 18 // Number of instructions between ISR fire and reload
#define F_COMMAND_EXTRA_CYCLES 20 // Number of instructions between ISR fire and reload
#define F_COMMAND_MIN_CYCLES 40 // Number of instructions of entire ISR
#define F_COMMAND_PRESCALE_CUT_OFF 200 // Hz below which we use the F_COMMAND_PRESCALE prescale
#define F_COMMAND_PRESCALE 128 //
/** V A R I A B L E S ********************************************************/
#pragma udata access fast_vars
// Rate variable - how fast does interrupt fire to capture inputs?
near unsigned int time_between_updates;
near volatile unsigned int ISR_D_RepeatRate; // How many 1ms ticks between Digital updates
near volatile unsigned char ISR_D_FIFO_in; // In pointer
near volatile unsigned char ISR_D_FIFO_out; // Out pointer
near volatile unsigned char ISR_D_FIFO_length; // Current FIFO depth
near volatile unsigned int ISR_A_RepeatRate; // How many 1ms ticks between Analog updates
near volatile unsigned char ISR_A_FIFO_in; // In pointer
near volatile unsigned char ISR_A_FIFO_out; // Out pointer
near volatile unsigned char ISR_A_FIFO_length; // Current FIFO depth
near volatile unsigned char AnalogEnable; // Maximum ADC channel to convert
// This byte has each of its bits used as a seperate error flag
near unsigned char error_byte;
// RC servo variables
// First the main array of data for each servo
near unsigned char g_RC_primed_ptr;
near unsigned char g_RC_next_ptr;
near unsigned char g_RC_timing_ptr;
// Used only in LowISR
near unsigned int D_tick_counter;
near unsigned int A_tick_counter;
near unsigned char A_cur_channel;
// Used in high ISR for F command
near unsigned char FCommandReloadValueOn_high;
near unsigned char FCommandReloadValueOn_low;
near unsigned char FCommandReloadValueOff_high;
near unsigned char FCommandReloadValueOff_low;
near unsigned char FCommandPrescaleOn;
near unsigned char FCommandPrescaleOff;
near unsigned char FCommandPrescaleCount;
near unsigned char FCommandPinHigh;
// ROM strings
const rom char st_OK[] = {"OK\r\n"};
const rom char st_LFCR[] = {"\r\n"};
const rom char st_version[] = {"UBW FW D Bersion 1.4.9\r\n"};
#pragma udata ISR_buf=0x100
volatile unsigned int ISR_A_FIFO[12][kISR_FIFO_A_DEPTH]; // Stores the most recent analog conversions
volatile unsigned char ISR_D_FIFO[3][kISR_FIFO_D_DEPTH]; // FIFO of actual data
volatile tRC_state g_RC_state[kRC_DATA_SIZE]; // Stores states for each pin for RC command
volatile unsigned int g_RC_value[kRC_DATA_SIZE]; // Stores reload values for TMR0
#pragma udata com_tx_buf = 0x200
// USB Transmit buffer for packets (back to PC)
unsigned char g_TX_buf[kTX_BUF_SIZE];
unsigned char g_RX_command_buf[kRX_COMMAND_BUF_SIZE];
#pragma udata com_rx_buf = 0x700
// USB Receiving buffer for commands as they come from PC
unsigned char g_RX_buf[kRX_BUF_SIZE];
// These variables are in normal storage space
#pragma udata
// USART Receiving buffer for data coming from the USART
unsigned char g_USART_RX_buf[kUSART_RX_BUF_SIZE];
// USART Transmit buffer for data going to the USART
unsigned char g_USART_TX_buf[kUSART_TX_BUF_SIZE];
// These are used for the Fast Parallel Output routines
unsigned char g_BO_init;
unsigned char g_BO_strobe_mask;
unsigned char g_BO_wait_mask;
unsigned char g_BO_wait_delay;
unsigned char g_BO_strobe_delay;
// Pointers to USB transmit (back to PC) buffer
unsigned char g_TX_buf_in;
unsigned char g_TX_buf_out;
// Pointers to USB receive (from PC) buffer
unsigned char g_RX_buf_in;
unsigned char g_RX_buf_out;
// In and out pointers to our USART input buffer
unsigned char g_USART_RX_buf_in;
unsigned char g_USART_RX_buf_out;
// In and out pointers to our USART output buffer
unsigned char g_USART_TX_buf_in;
unsigned char g_USART_TX_buf_out;
// Normally set to TRUE. Able to set FALSE to not send "OK" message after packet recepetion
BOOL g_ack_enable;
volatile unsigned char FCommandPortAMask = 0;
volatile unsigned char FCommandPortBMask = 0;
volatile unsigned char FCommandPortCMask = 0;
#ifdef __18F4550
volatile unsigned char FCommandPortDMask = 0;
volatile unsigned char FCommandPortEMask = 0;
#endif
/** P R I V A T E P R O T O T Y P E S ***************************************/
void BlinkUSBStatus (void); // Handles blinking the USB status LED
BOOL SwitchIsPressed (void); // Check to see if the user (PRG) switch is pressed
void parse_packet (void); // Take a full packet and dispatch it to the right function
signed char extract_digit (signed short long * acc, unsigned char digits); // Pull a character out of the packet
void parse_R_packet (void); // R for resetting UBW
void parse_C_packet (void); // C for configuring I/O and analog pins
void parse_CX_packet (void); // CX For configuring serial port
void parse_O_packet (void); // O for output digital to pins
void parse_I_packet (void); // I for input digital from pins
void parse_V_packet (void); // V for printing version
void parse_A_packet (void); // A for requesting analog inputs
void parse_T_packet (void); // T for setting up timed I/O (digital or analog)
void parse_PI_packet (void); // PI for reading a single pin
void parse_PO_packet (void); // PO for setting a single pin state
void parse_PD_packet (void); // PD for setting a pin's direction
void parse_MR_packet (void); // MR for Memory Read
void parse_MW_packet (void); // MW for Memory Write
void parse_TX_packet (void); // TX for transmitting serial
void parse_RX_packet (void); // RX for receiving serial
void parse_RC_packet (void); // RC is for outputing RC servo pulses
void parse_BO_packet (void); // BO sends data to fast parallel output
void parse_BC_packet (void); // BC configures fast parallel outputs
void parse_BS_packet (void); // BS sends binary data to fast parallel output
void parse_CU_packet (void); // CU configures UBW (system wide parameters)
void parse_SS_packet (void); // SS Send SPI
void parse_RS_packet (void); // RS Receive SPI
void parse_CS_packet (void); // CS Configure SPI
void parse_SI_packet (void); // SI Send I2C
void parse_RI_packet (void); // RI Receive I2C
void parse_CI_packet (void); // CI Configure I2C
void parse_F_packet (void); // F Frequency out
void check_and_send_TX_data (void); // See if there is any data to send to PC, and if so, do it
void print_ack (void); // Print "OK" after packet is parsed
int _user_putc (char c); // Our UBS based stream character printer
/** D E C L A R A T I O N S **************************************************/
#pragma code
#pragma interruptlow low_ISR
void low_ISR(void)
{
#if defined(USB_INTERRUPT)
USBDeviceTasks();
#endif
// Do we have a Timer2 interrupt? (1ms rate)
if (PIR1bits.TMR2IF)
{
// Clear the interrupt
PIR1bits.TMR2IF = 0;
// The most time critical part of this interrupt service routine is the
// handling of the RC command's servo output pulses.
// Each time we get this interrupt, we look to see if the next pin on the
// list has a value greater than zero. If so, we arm set it high and set
// it's state to PRIMED. Then we advance the pointers to the next pair.
if (kPRIMED == g_RC_state[g_RC_primed_ptr])
{
// This is easy, throw the value into the timer
TMR0H = g_RC_value[g_RC_primed_ptr] >> 8;
TMR0L = g_RC_value[g_RC_primed_ptr] & 0xFF;
// Then make sure the timer's interrupt enable is set
INTCONbits.TMR0IE = 1;
// And be sure to clear the flag too
INTCONbits.TMR0IF = 0;
// Turn on Timer0
T0CONbits.TMR0ON = 1;
// And set this pin's state to timing
g_RC_state[g_RC_primed_ptr] = kTIMING;
// Remember which pin is now timing
g_RC_timing_ptr = g_RC_primed_ptr;
}
if (kWAITING == g_RC_state[g_RC_next_ptr])
{
// If the value is zero, then shut this pin off
// otherwise, prime it for sending a pulse
if (0 == g_RC_value[g_RC_next_ptr])
{
g_RC_state[g_RC_next_ptr] = kOFF;
}
else
{
// Set the bit high
if (g_RC_next_ptr < 8)
{
bitset (LATA, g_RC_next_ptr & 0x7);
}
else if (g_RC_next_ptr < 16)
{
bitset (LATB, g_RC_next_ptr & 0x7);
}
else
{
bitset (LATC, g_RC_next_ptr & 0x7);
}
// Set the state to primed so we know to do next
g_RC_state[g_RC_next_ptr] = kPRIMED;
// And remember which pin is primed
g_RC_primed_ptr = g_RC_next_ptr;
}
}
// And always advance the main pointer
// NOTE: we need to skip RA6, RA7, and RC3, RC4, and RC5
// (Because UBW doesn't bring those pins out to headers)
g_RC_next_ptr++;
if (6 == g_RC_next_ptr)
{
g_RC_next_ptr = 8;
}
else if (19 == g_RC_next_ptr)
{
g_RC_next_ptr = 22;
}
else if (kRC_DATA_SIZE == g_RC_next_ptr)
{
g_RC_next_ptr = 0;
}
// See if it's time to fire off an I packet
if (ISR_D_RepeatRate > 0)
{
D_tick_counter++;
if (D_tick_counter >= ISR_D_RepeatRate)
{
D_tick_counter = 0;
// Tell the main code to send an I packet
if (ISR_D_FIFO_length < kISR_FIFO_D_DEPTH)
{
// And copy over our port values
ISR_D_FIFO[0][ISR_D_FIFO_in] = PORTA;
ISR_D_FIFO[1][ISR_D_FIFO_in] = PORTB;
ISR_D_FIFO[2][ISR_D_FIFO_in] = PORTC;
ISR_D_FIFO_in++;
if (ISR_D_FIFO_in >= kISR_FIFO_D_DEPTH)
{
ISR_D_FIFO_in = 0;
}
ISR_D_FIFO_length++;
}
else
{
// Stop the madness! Something is wrong, we're
// not getting our packets out. So kill the
// timer.
ISR_D_RepeatRate = 0;
}
}
}
// See if it's time to fire off an A packet
if ((ISR_A_RepeatRate > 0) && (AnalogEnable > 0))
{
A_tick_counter++;
if (A_tick_counter >= ISR_A_RepeatRate)
{
A_tick_counter = 0;
// Tell the main code to send an A packet
if (ISR_A_FIFO_length < kISR_FIFO_A_DEPTH)
{
ISR_A_FIFO_in++;
if (ISR_A_FIFO_in >= kISR_FIFO_A_DEPTH)
{
ISR_A_FIFO_in = 0;
}
ISR_A_FIFO_length++;
}
else
{
// Stop the madness! Something is wrong, we're
// not getting our packets out. So kill the A
// packets.
ISR_A_RepeatRate = 0;
}
}
}
// See if it's time to start analog conversions
if (AnalogEnable > 0)
{
// Set the channel to zero to start off with
A_cur_channel = 0;
ADCON0 = (A_cur_channel << 2) + 1;
// Clear the interrupt
PIR1bits.ADIF = 0;
// And make sure to always use low priority.
IPR1bits.ADIP = 0;
// Set the interrupt enable
PIE1bits.ADIE = 1;
// Make sure it's on!
ADCON0bits.ADON = 1;
// And tell the A/D to GO!
ADCON0bits.GO_DONE = 1;
}
}
// Do we have an analog interrupt?
if (PIR1bits.ADIF)
{
// Clear the interrupt
PIR1bits.ADIF = 0;
// Read out the value that we just converted, and store it.
ISR_A_FIFO[A_cur_channel][ISR_A_FIFO_in] =
(unsigned int)ADRESL
|
((unsigned int)ADRESH << 8);
// Incriment the channel and write the new one in
A_cur_channel++;
if (A_cur_channel >= AnalogEnable)
{
// We're done, so just sit and wait
// Turn off our interrupts though.
PIE1bits.ADIE = 0;
}
else
{
// Update the channel number
ADCON0 = (A_cur_channel << 2) + 1;
// And start the next conversion
ADCON0bits.GO_DONE = 1;
}
}
// Do we have a TMR0 interrupt? (RC command)
// TMR0 is in 16 bit mode, and counts up to FFFF and overflows, generating
// this interrupt.
if (INTCONbits.TMR0IF)
{
// Turn off Timer0
T0CONbits.TMR0ON = 0;
// Clear the interrupt
INTCONbits.TMR0IF = 0;
// And disable it
INTCONbits.TMR0IE = 0;
// Only do our stuff if the pin is in the proper state
if (kTIMING == g_RC_state[g_RC_timing_ptr])
{
// All we need to do is clear the pin and change its state to kWAITING
if (g_RC_timing_ptr < 8)
{
bitclr (LATA, g_RC_timing_ptr & 0x7);
}
else if (g_RC_timing_ptr < 16)
{
bitclr (LATB, g_RC_timing_ptr & 0x7);
}
else
{
bitclr (LATC, g_RC_timing_ptr & 0x7);
}
g_RC_state[g_RC_timing_ptr] = kWAITING;
}
}
}
#pragma interrupt high_ISR
void high_ISR(void)
{
// Check if the timer 3 interrupt has fired
if (PIR2bits.TMR3IF)
{
// Clear the interrupt flag
PIR2bits.TMR3IF = 0;
if (FCommandPinHigh)
{
//reload the timer
TMR3H = FCommandReloadValueOn_high;
TMR3L = FCommandReloadValueOn_low;
// apply the current prescale
FCommandPrescaleCount++;
if (FCommandPrescaleCount >= FCommandPrescaleOn)
{
// We're going to go low here so record that fact
FCommandPinHigh = FALSE;
// Toggle the output pin state
LATA = LATA & ~FCommandPortAMask;
LATB = LATB & ~FCommandPortBMask;
LATC = LATC & ~FCommandPortCMask;
#ifdef __18F4550
LATD = LATD & ~FCommandPortDMask;
LATE = LATE & ~FCommandPortEMask;
#endif
TMR3H = FCommandReloadValueOff_high;
TMR3L = FCommandReloadValueOff_low;
FCommandPrescaleCount = 0;
}
}
else
{
//reload the timer
TMR3H = FCommandReloadValueOff_high;
TMR3L = FCommandReloadValueOff_low;
// apply the current prescale
FCommandPrescaleCount++;
if (FCommandPrescaleCount >= FCommandPrescaleOff)
{
// We're going to go high here so record that fact
FCommandPinHigh = TRUE;
// Toggle the output pin state
LATA = LATA | FCommandPortAMask;
LATB = LATB | FCommandPortBMask;
LATC = LATC | FCommandPortCMask;
#ifdef __18F4550
LATD = LATD | FCommandPortDMask;
LATE = LATE | FCommandPortEMask;
#endif
TMR3H = FCommandReloadValueOn_high;
TMR3L = FCommandReloadValueOn_low;
FCommandPrescaleCount = 0;
}
}
}
}
void UserInit(void)
{
char i, j;
// Make all of 3 digital inputs
LATA = 0x00;
TRISA = 0xFF;
// Turn all analog inputs into digital inputs
ADCON1 = 0x0F;
// Turn off the ADC
ADCON0bits.ADON = 0;
// Turn off our own idea of how many analog channels to convert
AnalogEnable = 0;
CMCON = 0x07; // Comparators as digital inputs
// Make all of PORTB inputs
LATB = 0x00;
TRISB = 0xFF;
// Make all of PORTC inputs
LATC = 0x00;
TRISC = 0xFF;
#ifdef __18F4550
// Make all of PORTD and PORTE inputs too
LATD = 0x00;
TRISD = 0xFF;
LATE = 0x00;
TRISE = 0xFF;
#endif
// Initalize LED I/Os to outputs
mInitAllLEDs();
// Initalize switch as an input
mInitAllSwitches();
// Start off always using "OK" acknoledge.
g_ack_enable = TRUE;
// Use our own special output function for STDOUT
stdout = _H_USER;
// Initalize all of the ISR FIFOs
ISR_A_FIFO_out = 0;
ISR_A_FIFO_in = 0;
ISR_A_FIFO_length = 0;
ISR_D_FIFO_out = 0;
ISR_D_FIFO_in = 0;
ISR_D_FIFO_length = 0;
// Make sure that our timer stuff starts out disabled
ISR_D_RepeatRate = 0;
ISR_A_RepeatRate = 0;
D_tick_counter = 0;
A_tick_counter = 0;
A_cur_channel = 0;
// Now init our registers
// The prescaler will be at 16
T2CONbits.T2CKPS1 = 1;
T2CONbits.T2CKPS0 = 1;
// We want the TMR2 post scaler to be a 3
T2CONbits.T2OUTPS3 = 0;
T2CONbits.T2OUTPS2 = 0;
T2CONbits.T2OUTPS1 = 1;
T2CONbits.T2OUTPS0 = 0;
// Set our reload value
PR2 = kPR2_RELOAD;
// Set up the Analog to Digital converter
// Clear out the FIFO data
for (i = 0; i < 12; i++)
{
for (j = 0; j < kISR_FIFO_A_DEPTH; j++)
{
ISR_A_FIFO[i][j] = 0;
}
}
// Inialize USB TX and RX buffer management
g_RX_buf_in = 0;
g_RX_buf_out = 0;
g_TX_buf_in = 0;
g_TX_buf_out = 0;
// And the USART TX and RX buffer management
g_USART_RX_buf_in = 0;
g_USART_RX_buf_out = 0;
g_USART_TX_buf_in = 0;
g_USART_TX_buf_out = 0;
// Clear out the RC servo output pointer values
g_RC_primed_ptr = 0;
g_RC_next_ptr = 0;
g_RC_timing_ptr = 0;
// Clear the RC data structure
for (i = 0; i < kRC_DATA_SIZE; i++)
{
g_RC_value[i] = 0;
g_RC_state[i] = kOFF;
}
// Enable TMR0 for our RC timing operation
T0CONbits.PSA = 1; // Do NOT use the prescaler
T0CONbits.T0CS = 0; // Use internal clock
T0CONbits.T08BIT = 0; // 16 bit timer
INTCONbits.TMR0IF = 0; // Clear the interrupt flag
INTCONbits.TMR0IE = 0; // And clear the interrupt enable
INTCON2bits.TMR0IP = 0; // Low priority
// Enable interrupt priorities
RCONbits.IPEN = 1;
T2CONbits.TMR2ON = 0;
PIE1bits.TMR2IE = 1;
IPR1bits.TMR2IP = 0;
INTCONbits.GIEH = 1; // Turn high priority interrupts on
INTCONbits.GIEL = 1; // Turn low priority interrupts on
// Turn on the Timer2
T2CONbits.TMR2ON = 1;
T3CONbits.TMR3ON = 0;
}//end UserInit
/******************************************************************************
* Function: void ProcessIO(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: In this function, we check for a new packet that just
* arrived via USB. We do a few checks on the packet to see
* if it is worthy of us trying to interpret it. If it is,
* we go and call the proper function based upon the first
* character of the packet.
* NOTE: We need to see everything in one packet (i.e. we
* won't treat the USB data as a stream and try to find our
* start and end of packets within the stream. We just look
* at the first character of each packet for a command and
* check that there's a CR as the last character of the
* packet.
*
* Note: None
*****************************************************************************/
void ProcessIO(void)
{
static char last_command[64] = {0};
static BOOL in_esc = FALSE;
static char esc_sequence[3] = {0};
static BOOL in_cr = FALSE;
static BYTE last_fifo_size;
unsigned char tst_char;
static unsigned char button_state = 0;
static unsigned int button_ctr = 0;
char i;
BOOL done = FALSE;
unsigned char rx_bytes = 0;
unsigned char byte_cnt = 0;
static BOOL gotBS = FALSE; // True if we have detected BS command
static BYTE BSlen = 0; // Number of total bytes of BS data
BlinkUSBStatus();
// Check for any new I packets (from T command) ready to go out
while (ISR_D_FIFO_length > 0)
{
// Spit out an I packet first
parse_I_packet ();
// Then upate our I packet fifo stuff
ISR_D_FIFO_out++;
if (ISR_D_FIFO_out == kISR_FIFO_D_DEPTH)
{
ISR_D_FIFO_out = 0;
}
ISR_D_FIFO_length--;
}
// Check for a new A packet (from T command) ready to go out
while (ISR_A_FIFO_length > 0)
{
// Spit out an A packet first
parse_A_packet ();
// Then update our A packet fifo stuff
ISR_A_FIFO_out++;
if (ISR_A_FIFO_out == kISR_FIFO_A_DEPTH)
{
ISR_A_FIFO_out = 0;
}
ISR_A_FIFO_length--;
}
// Bail from here if we're not 'plugged in' to a PC or we're suspended
if(
(USBDeviceState < CONFIGURED_STATE)
||
(USBSuspendControl==1)
)
{
return;
}
// Pull in some new data if there is new data to pull in
// And we aren't waiting for the current move to finish
rx_bytes = getsUSBUSART((char *)g_RX_command_buf, 64);
if (rx_bytes > 0)
{
for(byte_cnt = 0; byte_cnt < rx_bytes; byte_cnt++)
{
tst_char = g_RX_command_buf[byte_cnt];
// Check for BS command binary dataa
if (gotBS && BSlen > 0)
{
g_RX_buf[g_RX_buf_in] = tst_char;
g_RX_buf_in++;
BSlen--;
}
// Check to see if we are in a CR/LF situation
else if (
!in_cr
&&
(
kCR == tst_char
||
kLF == tst_char
)
)
{
in_cr = TRUE;
g_RX_buf[g_RX_buf_in] = kCR;
g_RX_buf_in++;
// At this point, we know we have a full packet
// of information from the PC to parse
// Now, if we've gotten a full command (user send <CR>) then
// go call the code that deals with that command, and then
// keep parsing. (This allows multiple small commands per packet)
// Copy the new command over into the 'up-arrow' buffer
for (i=0; i<g_RX_buf_in; i++)
{
last_command[i] = g_RX_buf[i];
}
parse_packet ();
gotBS = FALSE;
BSlen = 0;
g_RX_buf_in = 0;
g_RX_buf_out = 0;
}
else if (tst_char == 27 && in_esc == FALSE)
{
in_esc = TRUE;
esc_sequence[0] = 27;
esc_sequence[1] = 0;
esc_sequence[2] = 0;
}
else if (
in_esc == TRUE
&&
tst_char == 91
&&
esc_sequence[0] == 27
&&
esc_sequence[1] == 0
)
{
esc_sequence[1] = 91;
}
else if (
in_esc == TRUE
&&
tst_char == 65
&&
esc_sequence[0] == 27
&&
esc_sequence[1] == 91
)
{
esc_sequence[0] = 0;
esc_sequence[1] = 0;
esc_sequence[2] = 0;
in_esc = FALSE;
// We got an up arrow (d27, d91, d65) and now copy over last command
for (i = 0; g_RX_buf[i] != kCR; i++)
{
g_RX_buf[i] = last_command[i];
}
g_RX_buf_in = i;
g_RX_buf[g_RX_buf_in] = 0;
// Also send 'down arrow' to counter act the affect of the up arrow
printf((far rom char *)"\x1b[B\x1b[1K\x1b[0G");
printf((far rom char *)"%s", g_RX_buf);
}
else if (tst_char == 8 && g_RX_buf_in > 0)
{
// Handle the backspace thing
g_RX_buf_in--;
g_RX_buf[g_RX_buf_in] = 0x00;
printf((far rom char *)" \b");
}
else if (
tst_char != kCR
&&
tst_char != kLF
&&
tst_char >= 32
)
{
esc_sequence[0] = 0;
esc_sequence[1] = 0;
esc_sequence[2] = 0;
in_esc = FALSE;
// Only add a byte if it is not a CR or LF
g_RX_buf[g_RX_buf_in] = tst_char;
in_cr = FALSE;
g_RX_buf_in++;
// Check for BS command
if (g_RX_buf_in == 3)
{
if (
(g_RX_buf[0] == 'B')
&&
(g_RX_buf[1] == 'S')
)
{
gotBS = TRUE;
}
}
// Check for BS length
if (gotBS && BSlen == 0)
{
if (
(g_RX_buf_in == 5)
&&
(g_RX_buf[4] == ',')
)
{
BSlen = g_RX_buf[3] - '0';
}
else if (
(g_RX_buf_in == 6)
&&
(g_RX_buf[5] == ',')
)
{
BSlen = (g_RX_buf[3] - '0') * 10;
BSlen += (g_RX_buf[4] - '0');
}
}
}
// Check for buffer wraparound
if (kRX_BUF_SIZE == g_RX_buf_in)
{
bitset (error_byte, kERROR_BYTE_RX_BUFFER_OVERRUN);
g_RX_buf_in = 0;
g_RX_buf_out = 0;
}
}
}
// Check for any errors logged in error_byte that need to be sent out
if (error_byte)
{
if (bittst (error_byte, 0))
{
// Unused as of yet
printf ((far rom char *)"!0 \r\n");
}
if (bittst (error_byte, kERROR_BYTE_TX_BUF_OVERRUN))
{
printf ((far rom char *)"!2 Err: TX Buffer overrun\r\n");
}
if (bittst (error_byte, kERROR_BYTE_RX_BUFFER_OVERRUN))
{
printf ((far rom char *)"!3 Err: RX Buffer overrun\r\n");
}
if (bittst (error_byte, kERROR_BYTE_MISSING_PARAMETER))
{
printf ((far rom char *)"!4 Err: Missing parameter(s)\r\n");
}
if (bittst (error_byte, kERROR_BYTE_PRINTED_ERROR))
{
// We don't need to do anything since something has already been printed out
//printf ((rom char *)"!5\r\n");
}
if (bittst (error_byte, kERROR_BYTE_PARAMETER_OUTSIDE_LIMIT))
{
printf ((far rom char *)"!6 Err: Invalid paramter value\r\n");
}
if (bittst (error_byte, kERROR_BYTE_EXTRA_CHARACTERS))
{
printf ((far rom char *)"!7 Err: Extra parmater\r\n");
}
error_byte = 0;
}
// Go send any data that needs sending to PC
check_and_send_TX_data ();
}
// This is our replacement for the standard putc routine
// This enables printf() and all related functions to print to
// the UBS output (i.e. to the PC) buffer
int _user_putc (char c)
{
BYTE OldPtr = g_TX_buf_in;
// Check to see if adding this byte will cause us to be full
OldPtr++;
if (kTX_BUF_SIZE == OldPtr)
{
OldPtr = 0;
}
// If so, then wait until some bytes go away first and make room
if (OldPtr == g_TX_buf_out)
{
check_and_send_TX_data();
}
// Copy the character into the output buffer
g_TX_buf[g_TX_buf_in] = c;