-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPeripheralSettingsAndMacros.c
3185 lines (2295 loc) · 74.1 KB
/
PeripheralSettingsAndMacros.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
#include "PeripheralSettingsAndMacros.h"
#include <plib.h>
#include <xc.h>
#include <time.h>
volatile int PWM1_Value = 10;
volatile int PWM2_Value = 100;
volatile int PWM1_DC = 50;
volatile int PWM2_DC = 50;
volatile int PWM1_DC_Value;
volatile int PWM2_DC_Value;
volatile int clock_scale_down = 10; // 10 fold slower execution
volatile int clock_scale_down_count = 0; // 10 fold slower execution
int DoAdjustPID = 0;
int DutyCyclePWM1 = 50;
BOOL DoReadUART2 = FALSE;
int32_t HyperADC_RX32 = 0;
BOOL IsDataReady = FALSE;
uint32_t CurrentFrequency_T1 = 0;
void _mon_putc(char c)
{
// if printf output is set to __XC_UART = 2, UART2
U2TXREG = c;
while (U2STAbits.UTXBF);
// if printf output is set to __XC_UART = 1, UART1
// U1TXREG = c;
// while (U1STAbits.UTXBF);
}
void InitSystem()
{
SYSTEMConfigPerformance(SYS_FREQ);
mOSCSetPBDIV(OSC_PB_DIV_1); // Peripheral BUS clock is equal to SYS_FREQ, maximum possible
INTEnableSystemMultiVectoredInt(); // Must
mBMXDisableDRMWaitState();
CheKseg0CacheOn();
InitUART1();
InitUART2(); // print with UART2
__XC_UART = 2;
InitSPI1(200000L);
InitSPI2Slave();
printf("System speed %ld Hz\n", (long) SYS_FREQ);
printf("Peripheral clock speed %ld Hz\n", (long) GetPeripheralClock());
// SetupDebugGPIOPins();
}
void SystemReset()
{
/* The following code illustrates a software Reset */
// assume interrupts are disabled
// assume the DMA controller is suspended
// assume the device is locked
/* perform a system unlock sequence */
// starting critical sequence
SYSKEY = 0x00000000; //write invalid key to force lock
SYSKEY = 0xAA996655; //write key1 to SYSKEY
SYSKEY = 0x556699AA; //write key2 to SYSKEY
// OSCCON is now unlocked
/* set SWRST bit to arm reset */
RSWRSTSET = 1;
/* read RSWRST register to trigger reset */
unsigned int dummy;
dummy = RSWRST;
/* prevent any unwanted code execution until reset occurs*/
NOP();
NOP();
NOP();
NOP();
while(1) NOP();
}
void SetupDebugGPIOPins()
{
// uint32_t *data0;
// uint32_t data_len = 1024 * 64;
//
// uint32_t bmx_sz = BMXDRMSZ;
// uint32_t bmx_pfm_sz = BMXPFMSZ;
//
//// data0 = (uint32_t *)AllocateMaxPossibleMemory(&data_len);
//
//
// printf("data_len %d\n", data_len);
//
// printf("BMXDRMSZ %X\n", bmx_sz);
// printf("BMXPFMSZ %X\n", bmx_pfm_sz);
//
//
//
// printf("BMXDUDBA %X\n", BMXDUDBA);
// printf("BMXDUPBA %X\n", BMXDUPBA);
//
//
//
// printf("BMXDKPBA %X\n", BMXDKPBA);
// mPORTDSetPinsDigitalOut(BIT_0);
// mPORTBSetPinsDigitalOut(BIT_14);
//
// mPORTGSetPinsDigitalOut(BIT_9);
// mPORTGSetPinsDigitalOut(BIT_8);
//
//
mPORTASetPinsDigitalOut(BIT_0 | BIT_1);
mPORTAClearBits(BIT_0 | BIT_1);
//
// mPORTDSetPinsDigitalOut(BIT_2);
//
// mPORTGClearBits(BIT_8);
// mPORTGClearBits(BIT_9);
// CNPDGbits.CNPDG8 = 0;
// CNPDGbits.CNPDG9 = 0;
// mPORTDClearBits(BIT_0);
printf("Debug pin setup... Disable this in the production version....\n");
}
void RtccSetup()
{
RtccInit();
RtccSetup();
}
void WaitMS(unsigned int ms)
{
unsigned int count = 0;
unsigned int i = 0;
while(count++ < ms)
{
i = 0;
while(i++ < MS_LOOP)
{
NOP();
}
}
}
int InitUART1()
{
int uart_id = 0;
PPS_Unlock();
U1RXRbits.U1RXR = 2; // 0b0010; // RF4 input J11.46
RPF5Rbits.RPF5R = 3; //0b0011; TX output J11.48
PPS_Lock();
uint32_t brate;
brate = 115200L;
// brate = 500000L;
// brate = 460800;//921600L/2L;
// brate = 38400L;
UARTConfigure((UART_MODULE)uart_id, UART_ENABLE_PINS_TX_RX_ONLY);
UARTSetFifoMode((UART_MODULE)uart_id,(UART_FIFO_MODE) (UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY));
UARTSetLineControl((UART_MODULE)uart_id,(UART_LINE_CONTROL_MODE) (UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1));
UARTSetDataRate((UART_MODULE)uart_id, GetPeripheralClock(), brate);
UARTEnable((UART_MODULE)uart_id, (UART_ENABLE_MODE) UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));
return uart_id;
}
int InitUART2()
{
int uart_id = 1;
mPORTASetPinsDigitalIn(BIT_14); // U2RX J10.35
mPORTASetPinsDigitalOut(BIT_15); // U2TX J10.36
PPS_Unlock();
U2RXRbits.U2RXR = 0b1101; // 0b0010; // RA14 input J10.35
RPA15Rbits.RPA15R = 0b0001; // U2TX UTX J10.36
PPS_Lock();
unsigned int brate;
brate = 115200L;//115200L * 1L;
UARTConfigure((UART_MODULE)uart_id, UART_ENABLE_HIGH_SPEED | UART_ENABLE_PINS_TX_RX_ONLY);
UARTSetFifoMode((UART_MODULE)uart_id,(UART_FIFO_MODE) (UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY));
UARTSetLineControl((UART_MODULE)uart_id,(UART_LINE_CONTROL_MODE) (UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1));
UARTSetDataRate((UART_MODULE)uart_id, GetPeripheralClock(), brate);
SetPriorityIntU2(UART_INT_PR3);
mU2ClearAllIntFlags();
mU2RXIntEnable(0);
UARTEnable((UART_MODULE)uart_id, (UART_ENABLE_MODE) UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));
return uart_id;
}
void MapSPI1MasterPins()
{
printf("Mapping SPI 1 Master Pins\n");
UnlockPPS();
SDI1Rbits.SDI1R = 0b1010; //10; // 0b1010 // RPC4 // SDI1 // J10.44
RPD0Rbits.RPD0R = 0b1000; // // SDO1 // J10.43
RPB2Rbits.RPB2R = 0b0111; //7; //0b0111; // SS2 // J10.47
LockPPS();
mPORTDSetPinsDigitalOut(BIT_10);// SCK1 out
mPORTCSetPinsDigitalIn(BIT_4);// SDI1 in
CNPDCbits.CNPDC4 = 1; // pull down
mPORTDSetPinsDigitalOut(BIT_0);// SDO1 out
mPORTBSetPinsDigitalOut(BIT_2);// SS1 out
mPORTBClearBits(BIT_2); // set high by default}
CNPUBbits.CNPUB2 = 1; // pull up
}
void MapSPI1SlavePins()
{
printf("Mapping SPI 1 Slave Pins (SPI 1 Slave mode may not function properly!!)\n");
UnlockPPS();
// output pin SDO1
RPD0Rbits.RPD0R = 0b1000; // // SDO1 // RD0 // J10.43
// input pins SDI1 and SS1
SDI1Rbits.SDI1R = 0b1010; // 0b1010 // RPC4 // SDI1
// SDI1Rbits.SDI1R = 0b0010; // 0b0010 // RPF5 // SDI1 // J10.52
SS1Rbits.SS1R = 0b1111; // RB2 // SS1 // J10.46
LockPPS();
mPORTDSetPinsDigitalIn(BIT_10);// SCK1 out J10.41
// SPI1 at J10.44
mPORTCSetPinsDigitalIn(BIT_4);// SDI1 in J10.44
mPORTCClearBits(BIT_4);
CNPDCbits.CNPDC4 = 1; // pull up
// SDI1 at J10.52
//
// mPORTFSetPinsDigitalIn(BIT_5);// SDI1 in J10.52
// mPORTFClearBits(BIT_5);
// CNPDFbits.CNPDF5 = 1;
mPORTDSetPinsDigitalOut(BIT_0);// SDO1 out // J10.43
// mPORTDSetPinsDigitalIn(BIT_9);// SS1 out
// mPORTDClearBits(BIT_9); // set high by default}
// CNPDDbits.CNPDD9 = 1; // pull down
mPORTBSetPinsDigitalIn(BIT_2);// SS1 in J10.46
mPORTBClearBits(BIT_2); // set high by default}
// CNPUBbits.CNPUB2 = 1;
CNPDBbits.CNPDB2 = 1;
}
void MapSPI2MasterPins()
{
printf("Mapping SPI 2 Master Pins\n");
PPS_Unlock();
SDI2Rbits.SDI2R = 1; // RPG7 = 0b0001 // SDI2
RPG8Rbits.RPG8R = 6; //0b0110; // SDO2
RPG9Rbits.RPG9R = 6; //0b0110; // SS2
PPS_Lock();
mPORTGSetPinsDigitalOut(BIT_6);// SCK2 out
mPORTGSetPinsDigitalIn(BIT_7);// SDI2 in J10.24
CNPDGbits.CNPDG7 = 1; // pull down
mPORTGSetPinsDigitalOut(BIT_8);// SDO2 out
mPORTGSetPinsDigitalOut(BIT_9);// SS2 out
CNPDGbits.CNPDG9 = 1; // pull down
}
void MapSPI2SlavePins()
{
printf("Mapping SPI 2 Slave Pins\n");
PPS_Unlock();
SDI2Rbits.SDI2R = 1; // RPG7 = 0b0001 // SDI2 SDI2 in J10.24
RPG8Rbits.RPG8R = 6; //0b0110; // SDO2
SS2Rbits.SS2R = 1;// RPG9 input
PPS_Lock();
mPORTGSetPinsDigitalIn(BIT_6);// SCK2 in J10.23
mPORTGSetPinsDigitalIn(BIT_7);// SDI2 in J10.24
// CNPDGbits.CNPDG7 = 1; // pull down
mPORTGClearBits(BIT_7);
mPORTGSetPinsDigitalOut(BIT_8);// SDO2 out
// CNPDGbits.CNPDG7 = 1;
mPORTGSetPinsDigitalIn(BIT_9);// SS2 in // RG9 // J10.26
CNPDGbits.CNPDG9 = 1; // pull down
mPORTGClearBits(BIT_9); // set low by default
}
//
//int SetPWMDutyCycle(float duty_cycle)
//{
// PWM1_DC_Value = (PR2 + 1) * ((float)duty_cycle / 100.0);
// return PWM1_DC_Value;
//}
int SetPWMDutyCycle(int pwm_number, int duty_cycle)
{
int dval;
if(pwm_number == 1)
{
OC1RS = (PR2 + 1) * ((float)duty_cycle / 100.0); //J10.20
return OC1RS;
}
if(pwm_number == 2)
{
OC2RS = (PR2 + 1) * ((float)duty_cycle / 100.0);
return OC2RS;
}
if(pwm_number == 3)
{
OC3RS = (PR2 + 1) * ((float)duty_cycle / 100.0);
return OC3RS;
}
if(pwm_number == 4)
{
OC4RS = (PR2 + 1) * ((float)duty_cycle / 100.0);
return OC4RS;
}
if(pwm_number == 5)
{
OC5RS = (PR2 + 1) * ((float)duty_cycle / 100.0);
return OC5RS;
}
}
void TurnOffPWM(int pwm_number)
{
switch(pwm_number)
{
case 1:
OC1CONbits.ON = 0;
break;
case 2:
OC2CONbits.ON = 0;
break;
case 3:
OC3CONbits.ON = 0;
break;
case 4:
OC4CONbits.ON = 0;
break;
case 5:
OC5CONbits.ON = 0;
break;
}
}
void TurnOnPWM(int pwm_number)
{
switch(pwm_number)
{
case 1:
OC1CONbits.ON = 1;
break;
case 2:
OC2CONbits.ON = 1;
break;
case 3:
OC3CONbits.ON = 1;
break;
case 4:
OC4CONbits.ON = 1;
break;
case 5:
OC5CONbits.ON = 1;
break;
}
}
void InitPWM(int pwm_frequency, int duty_cycle)
{
int samplerate = pwm_frequency;
PPS_Unlock();
RPD1Rbits.RPD1R = 0b1100; // OC1 // J11.20
PPS_Lock();
OC1CON = 0x0006;
PR2 = ((int)SYS_FREQ/ (pwm_frequency))-1;
OC1RS = (PR2+1) * ((float)duty_cycle /100.0);
T2CONbits.TCKPS = 0b101;
mT2SetIntPriority(7);
mT2ClearIntFlag();
mT2IntEnable(1);
T2CONbits.ON = 1;
OC1CONbits.ON = 1;
}
void InitPWM_v3(int pwm_frequency, int duty_cycle)
{
int samplerate = pwm_frequency;
PPS_Unlock();
RPD1Rbits.RPD1R = 0b1100; // OC1
// RPD2Rbits.RPD2R = 0b1011; // OC3
PPS_Lock();
OC1CON = 0x0006;
PR2 = ((int)SYS_FREQ/ (pwm_frequency*256))-1;
int val = ((int)SYS_FREQ/ (pwm_frequency*1))-1;
int dc = (PR2+1) * ((float)duty_cycle /100.0);
// OC1RS = (PR2+1) * ((float)duty_cycle /100.0);
// OC2RS = ((PR2+1) * PWM2_DC) /(int)100;
SetDCOC1PWM(dc);
OpenOC1(OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0); // init OC1 module, T2 =source
// OpenOC2(OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0); // init OC2 module, T2 =source(you could do more OCx)
OpenTimer2(T2_ON | T2_PS_1_1 | T2_SOURCE_INT, val); // init Timer2 mode and period reg (PR2)
// PR2 = ((int)SYS_FREQ/ (pwm_frequency*256))-1;
// T2CONSET = 0x8000;
// OC1CONSET = 0x8000;
// printf("PR2 == %d", PR2, OC1RS, OC2RS);
// SetDCOC1PWM(PWM1_Value);
// SetDCOC2PWM(PWM2_Value);
//T2_32BIT_MODE_ON
mT2SetIntPriority(7); // you don't have to use ipl7, but make sure INT definition is the same as your choice here
mT2ClearIntFlag(); // make sure no int will be pending until 7200 counts from this point.
mT2IntEnable(1); // allow T2 int
}
void InitTimer1(uint32_t freq) // freq in Hz
{
CurrentFrequency_T1 = freq;
T1CONbits.ON = 0;
PR1 = (uint32_t)SYS_FREQ/ ((uint32_t)((2*freq)*( (uint32_t)128)) )-1;
T1CONbits.TCKPS = 0b11; // 256
mT1SetIntPriority(3); // this timer is a low priority event
mT1ClearIntFlag();
mT1IntEnable(1);
T1CONbits.ON = 1;
printf("Timer-1 set to %d Hz\n", freq);
}
uint32_t GetTimer1_Freqency()
{
return CurrentFrequency_T1;
}
void InitTimer4(uint32_t freq) // freq in Hz
{
T4CONbits.ON = 0;
T4CONbits.T32 = 1;
PR4 = (uint32_t)SYS_FREQ/ ((uint32_t)((1*freq)*( (uint32_t)512)) )-1;
// PR4 = 2;
T4CONbits.TCKPS = 0b111; // 256
mT45SetIntPriority(3); // this timer is a low priority event
mT45ClearIntFlag();
mT45IntEnable(1);
T4CONbits.ON = 1;
printf("Timer-4 set to %d Hz\n", freq);
}
void InitPWM_v2(int sample_rate)
{
PPS_Unlock();
RPD1Rbits.RPD1R = 0b1100; // OC1
RPD2Rbits.RPD2R = 0b1011; // OC3
PPS_Lock();
// mPORT
int samplerate = sample_rate;
int PR2 = PBUS_CLOCK/samplerate-1;
OpenOC1(OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0); // init OC1 module, T2 =source
OpenOC2(OC_ON | OC_TIMER2_SRC | OC_PWM_FAULT_PIN_DISABLE, 0, 0); // init OC2 module, T2 =source(you could do more OCx)
OpenTimer2(T2_ON | T2_PS_1_1 | T2_SOURCE_INT, PBUS_CLOCK/samplerate); // init Timer2 mode and period reg (PR2)
SetDCOC1PWM(PWM1_Value);
SetDCOC2PWM(PWM2_Value);
//T2_32BIT_MODE_ON
mT2SetIntPriority(7); // you don't have to use ipl7, but make sure INT definition is the same as your choice here
mT2ClearIntFlag(); // make sure no int will be pending until 7200 counts from this point.
mT2IntEnable(1); // allow T2 int
}
void InitSPI1Slave()
{
// printf("Mapping SPI 1 Slave Pins\n");
MapSPI1SlavePins();
SpiChnEnable(SPI_CHANNEL1, 0);
SpiChnConfigure(SPI_CHANNEL1, (SpiConfigFlags)(SPI_CONFIG_CKE_REV | SPI_CONFIG_MODE8 | SPI_CONFIG_ON));
SpiChnEnable(SPI_CHANNEL1, 1);
SPI1CONbits.ON = 1;
}
void InitSPI2Slave()
{
// printf("Mapping SPI 2 Slave Pins\n");
MapSPI2SlavePins();
SpiChnEnable(SPI_CHANNEL2, 0);
SpiChnConfigure(SPI_CHANNEL2, (SpiConfigFlags)(SPI_CONFIG_CKE_REV | SPI_CONFIG_MODE8 | SPI_CONFIG_ON));
SpiChnEnable(SPI_CHANNEL2, 1);
SPI2CONbits.ON = 1;
}
void InitSPI1(uint32_t freq_hz)
{
int baud_rate = 4;//SPI_BRG_VAL(freq_hz);
baud_rate = SpiBrgVal(SYS_FREQ, freq_hz);
printf("baud_rate == %d\n", baud_rate);
MapSPI1MasterPins();
SPI1CONbits.ON = 0;
SpiChnConfigure(SPI_CHANNEL1, (SpiConfigFlags)(SPI_CONFIG_MSTEN | SPI_CONFIG_CKE_REV |
SPI_CONFIG_MODE8 | SPI_CONFIG_ON
));
SpiChnSetBrg(SPI_CHANNEL1, baud_rate);
SpiChnEnable(SPI_CHANNEL1, 1);
SPI1CONbits.ON = 1;
}
void InitSPI2(uint32_t freq_hz)
{
int baud_rate = SPI_BRG_VAL(freq_hz);
// printf("Mapping SPI 2 Master Pins\n");
MapSPI2MasterPins();
SpiChnConfigure(SPI_CHANNEL2, (SpiConfigFlags)(SPI_CONFIG_MSTEN | SPI_CONFIG_MSSEN |
SPI_CONFIG_MODE8 | SPI_CONFIG_ON |
0));
SpiChnSetBrg(SPI_CHANNEL2, baud_rate);
SpiChnEnable(SPI_CHANNEL2, 1);
}
void SplitFloat2Ints(float fval, int *int_val, int *frac_val)
{
*int_val = (int) fval;
*frac_val = (int) ( (float)(fval - (float) *int_val)*1000.0);
// if(fval >= 0)
// {
// *frac_val = (int) ( (float)(fval - (float) *int_val)*1000);
// }
// else
// {
// *frac_val = (int) ( (float)(-fval + (float) *int_val)*1000);
// }
}
uint32_t GetDataBuffer(UART_MODULE id, char *buffer, uint32_t max_size)
{
uint32_t num_char;
num_char = 0;
while(num_char < max_size)
{
uint8_t character;
while(!UARTReceivedDataIsAvailable(id))
;
character = UARTGetDataByte(id);
if(character == '\r')
break;
*buffer = character;
buffer++;
num_char++;
}
return num_char;
}
void ReadWriteUART2()
{
int count = 0;
int data_rx;
int max_buf0_len = 100;
char buffer0[100];
int idx = 0;
// U2STAbits.URXISEL = 1;
//
// while(1)
// {
//
//
//
//// if(DoReadUART2)
// {
// int val = ReadUART2();
//
// printf("UART2 data %c\n", (char) val);
// }
// }
// while(1)
// {
//// while(DataRdyUART2());
//// while(DataRdyUART2()==0);
//
// data_rx = ReadUART2();
//// buffer0[14] = 0;
//
//// putsUART2(buffer)
//// printf("%-3d:\t received data\t %c", count++, buffer0);
// printf("%-3d:\t received data\t %c\n", count++, (char)data_rx);
//
// }
//
// UARTStartAutoDataRateDetect(1);
int k = 0;
char buffer[20];
while(1)
{
// while(BusyUART2());
k = 0;
for(k = 0; k < 10; k++)
{
// while (U2STAbits.UTXBF);
// U2TXREG = c;
// if(UARTDataRateDetected(1) != 0)
{
// int val = ReadUART2(1);
// UART_DATA val = UARTGetData(1);
// while(BusyUART2());
//
// while(DataRdyUART2());
// while(U2STAbits. == 0);
// while(U2STAbits.URXDA!=0);
while(U2STAbits.URXDA)
{
data_rx = UARTGetDataByte(1);
// getsUART2(9, buffer0, 100);
// buffer0[9] = 0;
// printf("buffer\t %s\n",buffer0);
if(idx < max_buf0_len)
{
buffer0[idx] = data_rx;
idx++;
}
else{
idx = 0;
int ix = 0;
for(ix = 0; ix < max_buf0_len; ix++)
{
printf("%-d:\tRead UART2 data %d\n", count++, (char)(buffer0[ix]));
// U2TXREG = 'M';
// while(U2STAbits.UTXBF);
}
}
}
}
}
// buffer[k] = 0;
// printf("%-d:\tRead UART2 data %c\n", count++, data_rx);
// int ix;
// for(ix = 0; ix < 1; ix++)
// {
// while(BusyUART2());
//
//// WriteUART2('V');
// U2TXREG = 'M';
// while(U2STAbits.UTXBF);
//
// }
// printf("%-d:\tRead UART2 data %c\n", count++, (char) val.data8bit);
// printf("%-d:\tRead UART2 data %c\n", count++, (char) val);
// WaitMS(1);
}
}
inline BOOL IsDataWaitingUART2()
{
return U2STAbits.URXDA != 0;
}
inline BOOL IsDataWaitingUART1()
{
return U1STAbits.URXDA != 0;
}
BOOL ReadCommandFromUART2(int *command, int length)
{
/*
Command is always 3 bytes of information
* byte 1 : target ID
* byte 2: 1-> write 0 Read
* byte 3: byte to be written
*/
int idx = 0;
int k;
while(U2STAbits.URXDA && (idx < length))
{
int val = (int) UARTGetDataByte(1);
command[idx] = (char) val;
idx++;
k = 0; while(k++ < 1000);
printf("Command received %d\n", val);
}
// U2STAbits.URXDA = 0;
int count = 0;
if(command[0] > 0 && (command[1] == 0 || command[1] == 1) && idx >= 3)
{
// printf("******************************* Sending Acknowledgment ***************************\n");
while(UARTTransmitterIsReady(1) == FALSE);
// UARTSendDataByte(1 , 0xAA);
// UARTSendDataByte(1 , 0xAA);
// WRITE_TO_UART2(0xAA);
// WRITE_TO_UART2(command[1]);
// WRITE_TO_UART2(command[2]);
//
// while(UARTTransmitterIsReady(1) == FALSE);
// UARTSendDataByte(1, command[0]);
//
// while(UARTTransmitterIsReady(1) == FALSE);
// UARTSendDataByte(1, command[1]);
//
// while(UARTTransmitterIsReady(1) == FALSE);
// UARTSendDataByte(1, command[2]);
return TRUE;
}
return FALSE;
}
BOOL ReadCommandFromUART1(int *command, int length)
{
/*
Command is always 3 bytes of information
* byte 1 : target ID
* byte 2: 1-> write 0 Read
* byte 3: byte to be written
*/
int idx = 0;
int k;
if(!U1STAbits.URXDA)
{
return FALSE;
}
while(U1STAbits.URXDA && (idx < length))
{
int val = (int) UARTGetDataByte(0);
command[idx] = (char) val;
idx++;
k = 0; while(k++ < 1000);
// printf("Command received %d\n", val);
}
// U2STAbits.URXDA = 0;
int count = 0;
if(command[0] > 0 && (command[1] == 0 || command[1] == 1) && idx >= 3)
{
// printf("******************************* Sending Acknowledgment ***************************\n");
while(UARTTransmitterIsReady(0) == FALSE);
return TRUE;
}
return FALSE;
}
BOOL ReadCommandFromSPI2Slave(int *command, int length)
{
/*
Command is always 3 bytes of information
* byte 1 : target ID
* byte 2: 1-> write 0 Read
* byte 3: byte to be written
*/
int idx = 0;
int dummy = 0;
int k;
if(SPI2STATbits.SPIRBF)
{
while(SPI2STATbits.SPIRBF && (idx < length))
{
printf("Reading spi command....\n");
int val = SPI2_SLAVE_READ();
SPI2_SLAVE_WRITE(val, dummy);
command[idx] = (char) val;
idx++;
k = 0; while(k++ < 1000);
printf("Commane received %d\n", val);
}
}
// U2STAbits.URXDA = 0;
int count = 0;
if(command[0] > 0 && (command[1] == 0 || command[1] == 1) && idx >= 3)
{
// printf("******************************* Sending Acknowledgment ***************************\n");
while(UARTTransmitterIsReady(1) == FALSE);
// UARTSendDataByte(1 , 0xAA);
// UARTSendDataByte(1 , 0xAA);
// WRITE_TO_UART2(0xAA);
// WRITE_TO_UART2(command[1]);
// WRITE_TO_UART2(command[2]);
//
// while(UARTTransmitterIsReady(1) == FALSE);
// UARTSendDataByte(1, command[0]);
//
// while(UARTTransmitterIsReady(1) == FALSE);
// UARTSendDataByte(1, command[1]);
//
// while(UARTTransmitterIsReady(1) == FALSE);
// UARTSendDataByte(1, command[2]);
return TRUE;
}
return FALSE;
}
// reverses a string 'str' of length 'len'
void reverse(char *str, int len)
{
int i=0, j=len-1, temp;
while (i<j)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++; j--;
}
}
// Converts a given integer x to string str[]. d is the number
// of digits required in output. If d is more than the number
// of digits in x, then 0s are added at the beginning.
int intToStr(int x, char str[], int d)