-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
1101 lines (918 loc) · 36.7 KB
/
main.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
/*******************************************************************************
FileName: main.c
Processor: PIC18F14K50
Hardware: uLI4 - JH&MP 2016
Complier: Microchip C18
Author: Jan Horacek
*/
/** INCLUDES ******************************************************************/
#include "Compiler.h"
#include "GenericTypeDefs.h"
#include "HardwareProfile.h"
#include "ringBuffer.h"
#include "usart.h"
#include "usb.h"
#include "usb_config.h"
#include "usb_device.h"
#include "usb_function_cdc.h"
#include "main.h"
/** CONFIGURATION *************************************************************/
// CONFIG1L
#pragma config CPUDIV = NOCLKDIV// CPU System Clock Selection bits (No CPU System Clock divide)
#pragma config USBDIV = ON // USB Clock Selection bit (USB clock comes from the OSC1/OSC2 divided by 2)
//
// CONFIG1H //
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config PLLEN = ON // 4 X PLL Enable bit (Oscillator multiplied by 4)
#pragma config PCLKEN = ON // Primary Clock Enable bit (Primary clock enabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
//
// CONFIG2L //
#pragma config PWRTEN = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = SBORDIS // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
#pragma config BORV = 30 // Brown-out Reset Voltage bits (VBOR set to 3.0 V nominal)
//
// CONFIG2H //
#pragma config WDTEN = ON // Watchdog Timer Enable bit (WDT is enabled)
#pragma config WDTPS = 16 // Watchdog Timer Postscale Select bits (1:16) ~ 64 ms timeout
//
// CONFIG3H //
#pragma config HFOFST = OFF // HFINTOSC Fast Start-up bit (The system clock is held off until the HFINTOSC is stable.)
#pragma config MCLRE = ON // MCLR Pin Enable bit (MCLR pin enabled; RA3 input pin disabled)
//
// CONFIG4L //
#pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = ON // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
#pragma config BBSIZ = ON // Boot Block Size Select bit (2kW boot block size)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
//
#pragma config CP0 = OFF // Code Protection bit (Block 0 not code-protected)
#pragma config CP1 = OFF // Code Protection bit (Block 1 not code-protected)
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot block not code-protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM not code-protected)
#pragma config WRT0 = OFF // Table Write Protection bit (Block 0 not write-protected)
#pragma config WRT1 = OFF // Table Write Protection bit (Block 1 not write-protected)
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot block not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
#pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 not protected from table reads executed in other blocks)
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot block not protected from table reads executed in other blocks)
/** DEFINES *******************************************************************/
#define msg_len(buf,start) (((buf).data[((start) + 1) & (buf).max] & 0x0F)+3)
#define USB_last_message_len ringDistance(ring_USB_datain, last_start, ring_USB_datain.ptr_e)
#define USART_last_message_len ringDistance(ring_USART_datain, USART_last_start, ring_USART_datain.ptr_e)
#define RESET_BUS current_dev.reacted = FALSE; \
current_dev.timeout = 1; \
current_dev.index = 1; \
active_devices = 0; \
dirty_devices = 0; \
RCSTAbits.CREN = 0; \
PIE1bits.RCIE = 0;
#define IsRACKRound (current_dev.round == ROUND_RACK)
#define USB_MAX_TIMEOUT 10 // 100 ms
#define USART_MAX_TIMEOUT 50 // 500 us
#define DEVICE_COUNT 32 // XpressNET device count
#define NI_TIMEOUT 12 // normal inquiery timeout = 120 us
#define MLED_IN_MAX_TIMEOUT 5 // 50 ms
#define MLED_OUT_MAX_TIMEOUT 5 // 50 ms
#define PWR_LED_SHORT_COUNT 15 // 150 ms
#define PWR_LED_LONG_COUNT 40 // 400 ms
#define PWR_LED_FERR_COUNT 10 // status led indicates >10 framing errors
#define TIMEOUT_ERR_TIMEOUT 20 // 200 ms
/** VARIABLES *****************************************************************/
#pragma udata
char USB_Out_Buffer[32];
// USB -> USART ring buffer
volatile ring_generic ring_USB_datain;
// USART -> USB ring buffer
volatile ring_generic ring_USART_datain;
#pragma idata
// XpressNET device currently being requested
volatile current current_dev = { 0, 0, 0, 0 };
// time between 2 bytes received from USB
// increment every 100 us -> 100 ms timeout = 1 000
volatile BYTE usb_timeout = 0;
// time between 2 bytes received from USART
// increment every 100 us -> 100 ms timeout = 1 000
volatile WORD usart_timeout = 0;
volatile BYTE USART_last_start = 0;
// 10 ms timer counter
volatile WORD ten_ms_counter = 0;
// callback being called after byte is sent to USART
void (*volatile sent_callback)(void) = NULL;
// ondex of byte in ring_USB_datain to be sent to USART
volatile BYTE usart_to_send = 0;
volatile BOOL usart_last_byte_sent = FALSE;
volatile BOOL usb_configured = FALSE;
volatile UINT32 active_devices = 0;
volatile UINT32 dirty_devices = 0;
volatile alive keep_alive = { 0, 0, 0, 0 };
volatile BYTE mLED_In_Timeout = 2 * MLED_IN_MAX_TIMEOUT;
volatile BYTE mLED_Out_Timeout = 2 * MLED_OUT_MAX_TIMEOUT;
// Power led blinks pwr_led_status times, then stays blank for some time
// and then repeats the whole cycle. This lets user to see software status.
volatile BYTE pwr_led_base_timeout = PWR_LED_SHORT_COUNT;
volatile BYTE pwr_led_base_counter = 0;
volatile BYTE pwr_led_status_counter = 0;
volatile BYTE pwr_led_status = 2;
volatile port_history sense_hist = { 0, 0 };
volatile master_waiting master_send_waiting = { 0 };
volatile BYTE timeout_err_counter = TIMEOUT_ERR_TIMEOUT;
/** PRIVATE PROTOTYPES ********************************************************/
void YourHighPriorityISRCode();
void YourLowPriorityISRCode();
// general functions
void user_init(void);
void initialize_system(void);
void init_devices(void);
BYTE calc_parity(BYTE data);
void check_device_data_to_USB(void);
// USB functions
void USB_send(void);
void USB_receive(void);
void dump_buf_to_USB(ring_generic* buf);
void USBDeviceTasks(void);
void parse_command_for_master(BYTE start, BYTE len);
BOOL USB_send_master_data(BYTE first, BYTE second, BYTE third);
void USB_buffer_status(void);
// USART (XpressNET) functions
void USART_receive_interrupt(void);
void USART_check_timeouts(void);
void USART_send_next_frame(void);
void USART_send_rest_of_message(void);
void USART_request_next_device(void);
void USART_ni_sent(void);
void USART_send(void);
/** VECTOR REMAPPING **********************************************************/
#if defined(__18CXX)
#define REMAPPED_RESET_VECTOR_ADDRESS 0x00
#define REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS 0x08
#define REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS 0x18
#pragma code REMAPPED_HIGH_INTERRUPT_VECTOR = REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS
void Remapped_High_ISR(void) {
_asm goto YourHighPriorityISRCode _endasm
}
#pragma code REMAPPED_LOW_INTERRUPT_VECTOR = REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS
void Remapped_Low_ISR(void) {
_asm goto YourLowPriorityISRCode _endasm
}
#pragma code
//These are your actual interrupt handling routines.
#pragma interrupt YourHighPriorityISRCode
//Check which interrupt flag caused the interrupt.
//Service the interrupt
//Clear the interrupt flag
//Etc.
void YourHighPriorityISRCode() {
#if defined(USB_INTERRUPT)
USBDeviceTasks();
#endif
// USART send interrupt
if ((PIE1bits.TXIE) && (PIR1bits.TXIF)) {
if (sent_callback) { sent_callback(); }
}
if ((PIE1bits.RCIE) && (PIR1bits.RCIF)) {
USART_receive_interrupt();
}
} //This return will be a "retfie fast", since this is in a #pragma interrupt section
#pragma interruptlow YourLowPriorityISRCode
void YourLowPriorityISRCode() {
//Check which interrupt flag caused the interrupt.
//Service the interrupt
//Clear the interrupt flag
//Etc.
// Timer2 on 10 us
if ((PIE1bits.TMR2IE) && (PIR1bits.TMR2IF)) {
// USART currently requested device timeout
if ((current_dev.timeout > 0) && (current_dev.timeout < NI_TIMEOUT)) { current_dev.timeout++; }
// XpressNET direction is turned to "IN" as soon as possible after
// last byte was sent to XpressNET.
// This is done independently on any callbacks. This needs to be done really fast!
if ((usart_last_byte_sent) && (TXSTAbits.TRMT)) { XPRESSNET_DIR = XPRESSNET_IN; }
// Detection of USART device answering normal inquiry.
if ((!BAUDCONbits.RCIDL) && (!current_dev.reacted) && (XPRESSNET_DIR == XPRESSNET_IN)) {
// receiver detected start bit -> wait for all data
current_dev.reacted = TRUE;
current_dev.timeout = 0; // device answered -> provide long window
usart_timeout = 0;
}
// usart receive timeout
if (usart_timeout < USART_MAX_TIMEOUT) usart_timeout++;
if (ten_ms_counter < 1000) {
ten_ms_counter++;
} else {
ten_ms_counter = 0;
// 10 ms overflow:
// usb receive timeout
if (usb_timeout < USB_MAX_TIMEOUT) usb_timeout++;
// keep-alive
if ((keep_alive.send) && (keep_alive.send_timer < KA_SEND_INTERVAL)) {
keep_alive.send_timer++;
if (keep_alive.send_timer == KA_SEND_INTERVAL) {
keep_alive.send_timer = 0;
master_send_waiting.bits.keep_alive = TRUE;
}
}
if ((keep_alive.receive) && (keep_alive.receive_timer < KA_RECEIVE_MAX)) {
keep_alive.receive_timer++;
if (keep_alive.receive_timer == KA_RECEIVE_MAX) {
// computer crashed -> turn the bus off
mPwrControlPin = mPwrControlOff;
RESET_BUS;
keep_alive.receive_timer = 0;
keep_alive.receive = FALSE;
master_send_waiting.bits.status = TRUE;
}
}
#ifndef DEBUG
// mLEDIn timeout
if (mLED_In_Timeout < 2 * MLED_IN_MAX_TIMEOUT) {
mLED_In_Timeout++;
if (mLED_In_Timeout == MLED_IN_MAX_TIMEOUT) {
mLED_In_On();
}
}
// mLEDOut timeout
if ((mLED_Out_Timeout < 2 * MLED_OUT_MAX_TIMEOUT) && (usb_configured)) {
mLED_Out_Timeout++;
if (mLED_Out_Timeout == MLED_OUT_MAX_TIMEOUT) {
mLED_Out_Off();
}
}
#endif
// pwrLED toggling
pwr_led_base_counter++;
if (pwr_led_base_counter >= pwr_led_base_timeout) {
pwr_led_base_counter = 0;
pwr_led_status_counter++;
if (pwr_led_status_counter == 2 * pwr_led_status) {
// wait between cycles
pwr_led_base_timeout = PWR_LED_LONG_COUNT;
mLED_Pwr_Off();
} else if (pwr_led_status_counter > 2 * pwr_led_status) {
// new base cycle
pwr_led_base_timeout = PWR_LED_SHORT_COUNT;
pwr_led_status_counter = 0;
mLED_Pwr_On();
} else {
mLED_Pwr_Toggle();
}
}
// sense history
if (sense_hist.state != mSense) {
if (sense_hist.timeout < PORT_TIMEOUT) {
sense_hist.timeout++;
if (sense_hist.timeout >= PORT_TIMEOUT) {
sense_hist.state = mSense;
if (!mSense) { RESET_BUS; }
sense_hist.timeout = 0;
master_send_waiting.bits.status = TRUE;
}
}
} else {
sense_hist.timeout = 0;
}
if (timeout_err_counter < TIMEOUT_ERR_TIMEOUT) { timeout_err_counter++; }
// end of 10 ms counter
}
PIR1bits.TMR2IF = 0; // reset overflow flag
}
} //This return will be a "retfie", since this is in a #pragma interruptlow section
#endif
/** DECLARATIONS **************************************************************/
#pragma code
void main(void) {
initialize_system();
while (1) {
#if defined(USB_INTERRUPT)
if (USB_BUS_SENSE && (USBGetDeviceState() == DETACHED_STATE)) {
USBDeviceAttach();
}
#endif
// Normal inquiry answer timeout.
// This function is not placed in interrupt to serve interrupt as
// fast as possible.
if ((current_dev.timeout >= NI_TIMEOUT) && (mPwrControl) && (sense_hist.state)) {
// device did not answer in 120 us
#ifdef RACK_ENABLE
if (IsRACKRound) {
// device did not answer request for acknowledgement
if ((dirty_devices >> current_dev.index) & 0b1) {
// for second time -> device is not active
dirty_devices &= ~((UINT32)1 << current_dev.index);
active_devices &= ~((UINT32)1 << current_dev.index);
master_send_waiting.bits.active_devices = TRUE;
} else {
// for first time -> notice
dirty_devices |= ((UINT32)1 << current_dev.index);
}
}
#endif
current_dev.timeout = 0;
USART_send_next_frame();
}
// Transmission to USART ended.
// This function is not placed in interrupt to serve interrupt as
// fast as possible.
if ((usart_last_byte_sent) && (TXSTAbits.TRMT)) {
usart_last_byte_sent = 0;
if (sent_callback) { sent_callback(); }
}
USB_receive();
USB_send();
USART_check_timeouts();
CDCTxService();
if ((master_send_waiting.all) && (USART_last_start == ring_USART_datain.ptr_e)
&& (!current_dev.reacted)) {
// Data are not being received -> check output buffers.
/* The `reacted` part of if is important -- it ensures this part
* of code is not called in case of potential interrupt in next few
* microseconds. This is important for check_device_data_to_USB func.
*/
check_device_data_to_USB();
USART_last_start = ring_USART_datain.ptr_e;
}
// clear watchdog timer
ClrWdt();
} //end while
} //end main
void initialize_system(void) {
ADCON1 = 0x0F;
ADCON0 = 0;
init_devices();
user_init();
USBDeviceInit();
USARTInit();
}
void user_init(void) {
// init ring buffers
ringBufferInit(ring_USB_datain, 32);
ringBufferInit(ring_USART_datain, 32);
// switch off AD convertors (USART is not working when not switched off manually)
ANSEL = 0x00;
ANSELH = 0x00;
// enable PORTA and PORTB pull-ups (bacause of USART reading)
INTCON2bits.RABPU = 0;
// Initialize all of the LED pins
mInitAllLEDs();
mLED_Pwr_On();
mLED_In_On();
mLED_Out_On();
mInitPwrControl;
mPwrControlPin = mPwrControlOff;
mInitSense;
// setup timer2 on 100 us
T2CONbits.T2CKPS = 0b01; // prescaler 4x
PR2 = 30; // setup timer period register to interrupt every 10 us
TMR2 = 0x00; // reset timer counter
PIR1bits.TMR2IF = 0; // reset overflow flag
PIE1bits.TMR2IE = 1; // enable timer2 interrupts
IPR1bits.TMR2IP = 0; // timer2 interrupt low level
//
RCONbits.IPEN = 1; // enable high and low priority interrupts
INTCONbits.PEIE = 1; // Enable peripheral interrupts
INTCONbits.GIE = 1; // enable global interrupts
INTCONbits.GIEH = 1;
INTCONbits.GIEL = 1;
INTCONbits.RABIE = 0; // enable port interrupts
INTCON2bits.RABIP = 1; // interrupt in high level
// interrupt is fired on port change
T2CONbits.TMR2ON = 1; // enable timer2
}
// ******************************************************************************************************
// ************** USB Callback Functions ****************************************************************
// ******************************************************************************************************
// The USB firmware stack will call the callback functions USBCBxxx() in response to certain USB related
// events. For example, if the host PC is powering down, it will stop sending out Start of Frame (SOF)
// packets to your device. In response to this, all USB devices are supposed to decrease their power
// consumption from the USB Vbus to <2.5mA each. The USB module detects this condition (which according
// to the USB specifications is 3+ms of no bus activity/SOF packets) and then calls the USBCBSuspend()
// function. You should modify these callback functions to take appropriate actions for each of these
// conditions. For example, in the USBCBSuspend(), you may wish to add code that will decrease power
// consumption from Vbus to <2.5mA (such as by clock switching, turning off LEDs, putting the
// microcontroller to sleep, etc.). Then, in the USBCBWakeFromSuspend() function, you may then wish to
// add code that undoes the power saving things done in the USBCBSuspend() function.
// The USBCBSendResume() function is special, in that the USB stack will not automatically call this
// function. This function is meant to be called from the application firmware instead. See the
// additional comments near the function.
void USBCBSuspend(void) {
#if defined(__C30__)
USBSleepOnSuspend();
#endif
usb_configured = FALSE;
mLED_Out_On();
ringClear((ring_generic*)&ring_USART_datain);
ringClear((ring_generic*)&ring_USB_datain);
}
void USBCBWakeFromSuspend(void) {
usb_configured = TRUE;
mLED_Out_Off();
}
void USBCB_SOF_Handler(void) {
}
void USBCBErrorHandler(void) {
}
void USBCBCheckOtherReq(void) {
USBCheckCDCRequest();
}
void USBCBStdSetDscHandler(void) {
// Must claim session ownership if supporting this request
}
void USBCBInitEP(void) {
CDCInitEP();
usb_configured = TRUE;
mLED_Out_Off();
}
void USBCBSendResume(void) {
}
#if defined(ENABLE_EP0_DATA_RECEIVED_CALLBACK)
void USBCBEP0DataReceived(void) {
}
#endif
BOOL USER_USB_CALLBACK_EVENT_HANDLER(USB_EVENT event, void* pdata, WORD size) {
switch (event) {
case EVENT_CONFIGURED:
USBCBInitEP();
break;
case EVENT_SET_DESCRIPTOR:
USBCBStdSetDscHandler();
break;
case EVENT_EP0_REQUEST:
USBCBCheckOtherReq();
break;
case EVENT_SOF:
USBCB_SOF_Handler();
break;
case EVENT_SUSPEND:
USBCBSuspend();
break;
case EVENT_RESUME:
USBCBWakeFromSuspend();
break;
case EVENT_BUS_ERROR:
USBCBErrorHandler();
break;
case EVENT_TRANSFER:
Nop();
break;
default:
break;
}
return TRUE;
}
////////////////////////////////////////////////////////////////////////////////
/* CHECKING USART IN TIMEOUT
* This function is called in main loop, timing is not very critical.
* It checks for timeouts from XpressNET devices. For example, when device sends
* only part of the message, this timeout ensures clearing of master`s buffers
* with unfinished message.
*/
void USART_check_timeouts(void) {
// check for timeout
if (((USART_last_start != ring_USART_datain.ptr_e) || (current_dev.reacted))
&& (usart_timeout >= USART_MAX_TIMEOUT) && (!current_dev.finished)) {
// the (!current_dev.finished) condition guarantees us this if will
// not be entered after the message was received
// disable receive interrupt, so it does not interfere with this function
PIE1bits.RCIE = 0;
// delete last incoming message and wait for next message
ring_USART_datain.ptr_e = USART_last_start;
if (ring_USART_datain.ptr_e == ring_USART_datain.ptr_b) ring_USART_datain.empty = TRUE;
usart_timeout = 0;
current_dev.reacted = FALSE;
// inform PC about timeout
if (timeout_err_counter == TIMEOUT_ERR_TIMEOUT) {
timeout_err_counter = 0;
USB_send_master_data(0x01, 0x02, 0x03);
}
// send next message to XpressNET
USART_send_next_frame();
}
}
////////////////////////////////////////////////////////////////////////////////
/* RECEIVING DATA FROM XPRESSNET DEVICES
* This function is called in high-priority interrupt, be careful about
* interferences with main code!
* This function must be as fast as possible!
*/
void USART_receive_interrupt(void) {
// We do not check xor in this function intentionally.
// XOR should be checked in PC.
static nine_data received = { 0, 0 };
BYTE tmp, parity;
usart_timeout = 0;
received = USARTReadByte();
if (current_dev.finished) {
// next byte was received after the end of message -> probably
// bad length -> increase timeout to let the device transfer
// all the data
current_dev.timeout = NI_TIMEOUT / 2;
return;
}
current_dev.reacted = TRUE;
current_dev.timeout = 0;
#ifdef RACK_ENABLE
if (!((active_devices >> current_dev.index) & 0b1)) {
active_devices |= ((UINT32)1 << current_dev.index);
master_send_waiting.bits.active_devices = TRUE;
}
dirty_devices &= ~((UINT32)1 << current_dev.index);
#endif
if (ringFreeSpace(ring_USART_datain) < 2) {
// reset buffer and wait for next message
ring_USART_datain.ptr_e = USART_last_start;
if (ring_USART_datain.ptr_e == ring_USART_datain.ptr_b) ring_USART_datain.empty = TRUE;
return;
}
// The content of function "ringAddByte" is inlined to this function (because of speed).
if (USART_last_start == ring_USART_datain.ptr_e) {
// first byte -> add call byte before first byte
// parity function is inlined (because of speed)
parity = 0;
if ((tmp = current_dev.index) & 0b1) parity = !parity;
if ((tmp = tmp >> 1) & 0b1) parity = !parity;
if ((tmp = tmp >> 1) & 0b1) parity = !parity;
if ((tmp = tmp >> 1) & 0b1) parity = !parity;
if ((tmp = tmp >> 1) & 0b1) parity = !parity;
ring_USART_datain.data[ring_USART_datain.ptr_e] = current_dev.index + (0b11 << 5) + (parity << 7);
ring_USART_datain.ptr_e = (ring_USART_datain.ptr_e + 1) & ring_USART_datain.max;
}
ring_USART_datain.data[ring_USART_datain.ptr_e] = received.data;
ring_USART_datain.ptr_e = (ring_USART_datain.ptr_e + 1) & ring_USART_datain.max;
ring_USART_datain.empty = FALSE;
if (USART_last_message_len >= msg_len(ring_USART_datain, USART_last_start)) {
#ifdef RACK_ENABLE
if (IsRACKRound) {
ring_USART_datain.ptr_e = USART_last_start;
if (ring_USART_datain.ptr_e == ring_USART_datain.ptr_b) ring_USART_datain.empty = TRUE;
} else {
USART_last_start = ring_USART_datain.ptr_e;
}
#else
USART_last_start = ring_USART_datain.ptr_e;
#endif
current_dev.finished = TRUE;
// whole message received -> wait a few microseconds and send next data
current_dev.timeout = NI_TIMEOUT / 2;
}
#ifndef DEBUG
// toggle LED
if (mLED_In_Timeout >= 2 * MLED_IN_MAX_TIMEOUT) {
mLED_In_Off();
mLED_In_Timeout = 0;
}
#endif
}
////////////////////////////////////////////////////////////////////////////////
// Check for data in ring_USART_datain and send complete data to USB.
void USB_send(void) {
BYTE len = msg_len(ring_USART_datain, ring_USART_datain.ptr_b);
// check for USB ready
if (!mUSBUSARTIsTxTrfReady()) return;
if (((ringLength(ring_USART_datain)) >= 3) && (ringLength(ring_USART_datain) >= len)) {
// send message
ringSerialize((ring_generic*)&ring_USART_datain, (BYTE*)USB_Out_Buffer, ring_USART_datain.ptr_b, len);
putUSBUSART(USB_Out_Buffer, len);
ringRemoveFrame((ring_generic*)&ring_USART_datain, len);
}
}
////////////////////////////////////////////////////////////////////////////////
/* Receive data from USB and add it to ring_USB_datain.
*/
void USB_receive(void) {
static BYTE last_start = 0;
BYTE xor, i;
BYTE received_len;
BOOL parity;
if ((USBDeviceState < CONFIGURED_STATE) || (USBSuspendControl)) return;
if (mUSBUSARTIsTxTrfReady()) {
// ring_USB_datain overflow check
if (ringFull(ring_USB_datain)) {
// delete last message
ring_USB_datain.ptr_e = last_start;
if (ring_USB_datain.ptr_b == ring_USB_datain.ptr_e) ring_USART_datain.empty = TRUE;
// inform PC about full buffer
USB_send_master_data(0x01, 0x06, 0x07);
return;
}
received_len = getsUSBUSART((ring_generic*)&ring_USB_datain, ringFreeSpace(ring_USB_datain));
if (received_len == 0) {
// check for timeout
if ((usb_timeout >= USB_MAX_TIMEOUT) && (last_start != ring_USB_datain.ptr_e)) {
ring_USB_datain.ptr_e = last_start;
usb_timeout = 0;
if (ring_USB_datain.ptr_e == ring_USB_datain.ptr_b) ring_USB_datain.empty = TRUE;
// inform PC about timeout
USB_send_master_data(0x01, 0x01, 0x00);
}
return;
}
// some data received ...
usb_timeout = 0;
// data received -> parse data
// at least 3 bytes must be in buffer to start parsing
// (call byte + header byte + xor)
while ((ringDistance(ring_USB_datain, last_start, ring_USB_datain.ptr_e) >= 3)
&& (USB_last_message_len >= msg_len(ring_USB_datain, last_start))) {
// while message received
// check for parity
for (i = 0, parity = 0; i < 8; i++)
if ((ring_USB_datain.data[last_start] >> i) & 1)
parity = !parity;
if (parity != 0) {
// parity error
ringRemoveFromMiddle((ring_generic*)&ring_USB_datain, last_start, msg_len(ring_USB_datain, last_start));
USB_send_master_data(0x01, 0x08, 0x09);
return;
}
// check for xor
for (i = 0, xor = 0; i < msg_len(ring_USB_datain, last_start) - 2; i++)
xor ^= ring_USB_datain.data[(i + last_start + 1) & ring_USB_datain.max];
if (xor != ring_USB_datain.data[(i + last_start + 1) & ring_USB_datain.max]) {
// xor error
// delete content in the middle of ring buffer
ringRemoveFromMiddle((ring_generic*)&ring_USB_datain, last_start, msg_len(ring_USB_datain, last_start));
USB_send_master_data(0x01, 0x07, 0x06);
return;
}
// xor ok -> parse data
if (((ring_USB_datain.data[last_start] >> 5) & 0b11) == 0b01) {
parse_command_for_master(last_start, msg_len(ring_USB_datain, last_start));
// remove message from buffer -> do not move last_start
// (message moves in the buffer itself)
ringRemoveFromMiddle((ring_generic*)&ring_USB_datain, last_start, msg_len(ring_USB_datain, last_start));
} else {
if (!sense_hist.state) {
ringRemoveFromMiddle((ring_generic*)&ring_USB_datain, last_start, msg_len(ring_USB_datain, last_start));
USB_send_master_data(0x01, 0x09, 0x08);
return;
}
if (!mPwrControl) {
ringRemoveFromMiddle((ring_generic*)&ring_USB_datain, last_start, msg_len(ring_USB_datain, last_start));
USB_send_master_data(0x01, 0x0A, 0x0B);
return;
}
last_start = (last_start + msg_len(ring_USB_datain, last_start)) & ring_USB_datain.max;
}
}
#ifndef DEBUG
// toggle LED
if (mLED_Out_Timeout >= 2 * MLED_OUT_MAX_TIMEOUT) {
mLED_Out_On();
mLED_Out_Timeout = 0;
}
#endif
}
}
////////////////////////////////////////////////////////////////////////////////
/* Parse data intended for master.
*/
void parse_command_for_master(BYTE start, BYTE len) {
BYTE db1 = ring_USB_datain.data[(start + 2) & ring_USB_datain.max];
if ((db1 >> 4) == 0xA) {
// set master status
mPwrControlPin = !(db1 & 0b1);
RCSTAbits.CREN = (db1 & 0b1);
PIE1bits.RCIE = (db1 & 0b1);
if (!RCSTAbits.CREN) { RESET_BUS; }
keep_alive.send = ((db1 >> 3) & 0b1);
keep_alive.receive = ((db1 >> 2) & 0b1);
keep_alive.receive_timer = 0;
keep_alive.send_timer = 0;
master_send_waiting.bits.status = TRUE;
} else if (db1 == 0xA2) {
// tansistor status request
master_send_waiting.bits.status = TRUE;
} else if (db1 == 0x80) {
// version request
if (mUSBUSARTIsTxTrfReady()) {
USB_Out_Buffer[0] = 0xA0;
USB_Out_Buffer[1] = 0x13;
USB_Out_Buffer[2] = 0x80;
USB_Out_Buffer[3] = VERSION_HW;
USB_Out_Buffer[4] = VERSION_SW;
USB_Out_Buffer[5] = USB_Out_Buffer[1] ^ USB_Out_Buffer[2] ^ USB_Out_Buffer[3] ^ USB_Out_Buffer[4];
putUSBUSART(USB_Out_Buffer, 6);
}
} else if (db1 == 0x81) {
// response request
if (mUSBUSARTIsTxTrfReady()) {
USB_Out_Buffer[0] = 0xA0;
USB_Out_Buffer[1] = 0x01;
USB_Out_Buffer[2] = 0x04;
USB_Out_Buffer[3] = 0x05;
putUSBUSART(USB_Out_Buffer, 4);
}
} else if (db1 == 0x82) {
// active device list request
master_send_waiting.bits.active_devices = TRUE;
} else if (db1 == 0x05) {
// keep-alive
keep_alive.receive_timer = 0;
}
}
////////////////////////////////////////////////////////////////////////////////
/* SEND NEXT DATA TO XPRESSNET DEVICE.
* This function checks if message is present in USB->USART buffer. If yes,
* the mesasge is sent to device. Otherwise, next device is requested with
* normal inquiry.
* This function must be called from main loop. We must ensure that this
* function is not called when data are received, but not yet parsed. Otherwise,
* it could send out data intended for uLI-master!
*/
void USART_send_next_frame(void) {
BYTE ring_length = ringDistance(ring_USB_datain, ring_USB_datain.ptr_b, ring_USB_datain.ptr_e);
// check if there is a message from PC to be sent to XpressNET
if ((ring_length >= 3) && (ring_length >= msg_len(ring_USB_datain, ring_USB_datain.ptr_b))) {
// yes -> send the message
usart_to_send = (ring_USB_datain.ptr_b + 1) & ring_USB_datain.max;
XPRESSNET_DIR = XPRESSNET_OUT;
current_dev.reacted = FALSE; // we do not want USART timeout to overflow
current_dev.finished = FALSE;
sent_callback = &(USART_send_rest_of_message);
usart_last_byte_sent = 0;
USARTWriteByte(1, ring_USB_datain.data[ring_USB_datain.ptr_b]);
PIE1bits.TXIE = 1;
} else {
// no -> send normal inquiry to next XpressNET device
USART_request_next_device();
}
}
/* SEND REST OF MESSAGE TO USART.
* This fnction is called as callback (from interrupt!) after a byte is
* sent to USART. It sends next byte. After last byte is sent,
* USART_request_next_device is called as callback.
*/
void USART_send_rest_of_message(void) {
USARTWriteByte(0, ring_USB_datain.data[usart_to_send]);
usart_to_send = (usart_to_send + 1) & ring_USB_datain.max;
if (usart_to_send == ((ring_USB_datain.ptr_b + msg_len(ring_USB_datain, ring_USB_datain.ptr_b)) & ring_USB_datain.max)) {
// last byte sending
ring_USB_datain.ptr_b = usart_to_send; // whole message sent
if (ring_USB_datain.ptr_b == ring_USB_datain.ptr_e) { ring_USB_datain.empty = TRUE; }
sent_callback = &(USART_request_next_device);
usart_last_byte_sent = 1;
PIE1bits.TXIE = 0;
} else {
// other-than-last byte sending
sent_callback = &(USART_send_rest_of_message);
usart_last_byte_sent = 0;
PIE1bits.TXIE = 1;
}
}
////////////////////////////////////////////////////////////////////////////////
// request next XpressNET device
void USART_request_next_device(void) {
UINT32 tmp = 0;
// 1) pick next device
current_dev.index++;
if (current_dev.index >= DEVICE_COUNT) {
current_dev.round++;
if (current_dev.round >= ROUND_MAX) { current_dev.round = 0; }
current_dev.index = 1; // 0 == broadcast (not a device)
}
#ifdef RACK_ENABLE
// Are we supposed to send request for acknowledgement (RACK)?
// Which device are we supposed to send RACK to?
if (IsRACKRound) {
tmp = active_devices >> current_dev.index;
if (tmp == 0) {
// all active devices requested in this round
current_dev.round = 0;
current_dev.index = 1;
} else {
// at least one active device has not been requested in
// this round yet -> find it and request it
while (!(tmp & 0b1)) {
tmp = tmp >> 1;
current_dev.index++;
}
}
}
#endif
// 2) request current device
RCSTAbits.CREN = 1; // enable USART RX -- to be sure (because of overrun error)
PIE1bits.RCIE = 1;
current_dev.timeout = 0;
current_dev.reacted = FALSE;
current_dev.finished = FALSE;
XPRESSNET_DIR = XPRESSNET_OUT;
sent_callback = &(USART_ni_sent);
PIE1bits.TXIE = 0;
usart_timeout = 0;
#ifdef RACK_ENABLE
USARTWriteByte(1, calc_parity(current_dev.index + ((!IsRACKRound) << 6))); // send normal inquiry or request acknowledgement
#else
USARTWriteByte(1, calc_parity(current_dev.index + (0x40))); // send normal inquiry
#endif
usart_last_byte_sent = TRUE;
}
////////////////////////////////////////////////////////////////////////////////
// Debug function: dump buffer to USB
void dump_buf_to_USB(ring_generic* buf) {
int i;
for (i = 0; i <= buf->max; i++)
USB_Out_Buffer[i] = buf->data[i];
putUSBUSART(USB_Out_Buffer, buf->max + 1);
}
////////////////////////////////////////////////////////////////////////////////
void init_devices(void) {
current_dev.index = 0;
current_dev.timeout = 1; // this will cause the processor to send first normal inquiry after some time
current_dev.reacted = FALSE;
}
////////////////////////////////////////////////////////////////////////////////
// Calculate parity and return BYTE with the leftmost parity bit (even parity).
BYTE calc_parity(BYTE data) {
BYTE i, result, parity;
parity = 0;
result = data;