-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathExternal.c
More file actions
6259 lines (6017 loc) · 200 KB
/
External.c
File metadata and controls
6259 lines (6017 loc) · 200 KB
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
/***********************************************************************************************************************
PicoMite MMBasic
External.c
<COPYRIGHT HOLDERS> Geoff Graham, Peter Mather
Copyright (c) 2021, <COPYRIGHT HOLDERS> All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the distribution.
3. The name MMBasic be used when referring to the interpreter in any documentation and promotional material and the original copyright message be displayed
on the console at startup (additional copyright messages may be added).
4. All advertising materials mentioning features or use of this software must display the following acknowledgement: This product includes software developed
by the <copyright holder>.
5. Neither the name of the <copyright holder> nor the names of its contributors may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDERS> AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDERS> BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
************************************************************************************************************************/ \
#include "MMBasic_Includes.h"
/**
* @file External.c
* @author Geoff Graham, Peter Mather
* @brief Source for I/O MMBasic commands and functions
*/
/*
* @cond
* The following section will be excluded from the documentation.
*/
#include "Hardware_Includes.h"
#include "hardware/watchdog.h"
#include "hardware/i2c.h"
#include "pico/stdlib.h"
#include "hardware/clocks.h"
#include "hardware/pwm.h"
// #include "hardware/gpio.h"
#include "hardware/adc.h"
#include "hardware/structs/systick.h"
#include "hardware/structs/pwm.h"
#include "hardware/structs/pads_bank0.h"
#include "hardware/structs/adc.h"
#include "hardware/dma.h"
#include <hardware/structs/ioqspi.h>
#include <hardware/sync.h>
#define ANA_AVERAGE 10
#define ANA_DISCARD 2
extern MMFLOAT FDiv(MMFLOAT a, MMFLOAT b);
extern MMFLOAT FMul(MMFLOAT a, MMFLOAT b);
extern MMFLOAT FSub(MMFLOAT a, MMFLOAT b);
const char *PinFunction[] = {
"OFF",
"AIN",
"DIN",
"FIN",
"PER",
"CIN",
"INTH",
"INTL",
"DOUT",
"HEARTBEAT",
"INTB",
"UART0TX",
"UART0RX",
"UART1TX",
"UART1RX",
"I2C0SDA",
"I2C0SCL",
"I2C1SDA",
"I2C1SCL",
"SPI0RX",
"SPI0TX",
"SPI0SCK",
"SPI1RX",
"SPI1TX",
"SPI1SCK",
"IR",
"INT1",
"INT2",
"INT3",
"INT4",
"PWM0A",
"PWM0B",
"PWM1A",
"PWM1B",
"PWM2A",
"PWM2B",
"PWM3A",
"PWM3B",
"PWM4A",
"PWM4B",
"PWM5A",
"PWM5B",
"PWM6A",
"PWM6B",
"PWM7A",
"PWM7B",
"ADCRAW",
#ifdef rp2350
"PWM8A",
"PWM8B",
"PWM9A",
"PWM9B",
"PWM10A",
"PWM10B",
"PWM11A",
"PWM11B",
#endif
"PIO0",
#ifdef rp2350
"PIO1",
"PIO2",
"FFIN",
#ifdef PICOMITE
"KEYBOARD"
#endif
#else
"PIO1"
#endif
};
;
volatile int ExtCurrentConfig[NBRPINS + 1];
volatile int INT1Value, INT1InitTimer, INT1Timer;
volatile int INT2Value, INT2InitTimer, INT2Timer;
volatile int INT3Value, INT3InitTimer, INT3Timer;
volatile int INT4Value, INT4InitTimer, INT4Timer;
volatile int64_t INT1Count, INT2Count, INT3Count, INT4Count;
uint64_t uSecoffset = 0;
uint32_t pinmask = 0;
volatile uint64_t IRoffset = 0;
void *IrDev, *IrCmd;
volatile char IrVarType;
volatile char IrState, IrGotMsg;
int IrBits, IrCount;
unsigned char *IrInterrupt;
#define CALLBACK_ONESHOT 64
#define ONESHOT_STATE_IDLE 0
#define ONESHOT_STATE_PREDELAY 1
#define ONESHOT_STATE_PULSE 2
#define ONESHOT_STATE_QUIESCENT 3
volatile int oneshot_active = 0;
volatile int oneshot_state = ONESHOT_STATE_IDLE;
volatile int oneshot_trigger_pin = 0;
volatile int oneshot_output_pin = 0;
volatile int oneshot_trigger_rising = 1;
volatile int oneshot_prepulse_us = 0;
volatile int oneshot_pulse_us = 0;
volatile int oneshot_quiescent_us = 0;
volatile int oneshot_retriggerable = 0;
volatile int oneshot_output_idle_level = 0;
volatile uint64_t oneshot_ignored_triggers = 0;
volatile alarm_id_t oneshot_alarm_id = -1;
int last_adc = 99;
volatile int CallBackEnabled = 0;
uint8_t IRpin = 99;
uint8_t PWM0Apin = 99;
uint8_t PWM1Apin = 99;
uint8_t PWM2Apin = 99;
uint8_t PWM3Apin = 99;
uint8_t PWM4Apin = 99;
uint8_t PWM5Apin = 99;
uint8_t PWM6Apin = 99;
uint8_t PWM7Apin = 99;
#ifdef rp2350
uint8_t PWM8Apin = 99;
uint8_t PWM9Apin = 99;
uint8_t PWM10Apin = 99;
uint8_t PWM11Apin = 99;
#endif
uint8_t PWM0Bpin = 99;
uint8_t PWM1Bpin = 99;
uint8_t PWM2Bpin = 99;
uint8_t PWM3Bpin = 99;
uint8_t PWM4Bpin = 99;
uint8_t PWM5Bpin = 99;
uint8_t PWM6Bpin = 99;
uint8_t PWM7Bpin = 99;
#ifdef rp2350
uint8_t PWM8Bpin = 99;
uint8_t PWM9Bpin = 99;
uint8_t PWM10Bpin = 99;
uint8_t PWM11Bpin = 99;
#endif
uint8_t UART1RXpin = 99;
uint8_t UART1TXpin = 99;
uint8_t UART0TXpin = 99;
uint8_t UART0RXpin = 99;
uint8_t SPI1TXpin = 99;
uint8_t SPI1RXpin = 99;
uint8_t SPI1SCKpin = 99;
uint8_t SPI0TXpin = 99;
uint8_t SPI0RXpin = 99;
uint8_t SPI0SCKpin = 99;
uint8_t I2C1SDApin = 99;
uint8_t I2C1SCLpin = 99;
uint8_t I2C0SDApin = 99;
uint8_t I2C0SCLpin = 99;
uint8_t slice0 = 0, slice1 = 0, slice2 = 0, slice3 = 0, slice4 = 0, slice5 = 0, slice6 = 0, slice7 = 0;
#ifdef rp2350
uint8_t slice8 = 0, slice9 = 0, slice10 = 0, slice11 = 0;
bool fast_timer_active = false;
volatile uint64_t INT5Count, INT5Value, INT5InitTimer, INT5Timer;
#ifdef PICOMITE
int LocalKeyDown[7];
#endif
#endif
bool dmarunning = false;
bool ADCDualBuffering = false;
uint32_t ADCmax = 0;
int ADCopen = 0;
char *ADCInterrupt;
volatile MMFLOAT *volatile a1float = NULL, *volatile a2float = NULL, *volatile a3float = NULL, *volatile a4float = NULL;
volatile int ADCpos = 0;
float frequency;
uint32_t ADC_dma_chan = ADC_DMA;
uint32_t ADC_dma_chan2 = ADC_DMA2;
short *ADCbuffer = NULL;
void PWMoff(int slice);
volatile uint8_t *adcint = NULL;
uint8_t *adcint1 = NULL;
uint8_t *adcint2 = NULL;
MMFLOAT ADCscale[4], ADCbottom[4];
extern void mouse0close(void);
void MIPS16 oneshot_disable(void)
{
alarm_id_t id;
int callback_state;
int trigger_pin;
int output_pin;
int idle_level;
int was_active;
mT4IntEnable(0);
id = oneshot_alarm_id;
oneshot_alarm_id = -1;
callback_state = CallBackEnabled;
trigger_pin = oneshot_trigger_pin;
output_pin = oneshot_output_pin;
idle_level = oneshot_output_idle_level;
was_active = oneshot_active;
oneshot_active = 0;
oneshot_state = ONESHOT_STATE_IDLE;
oneshot_trigger_pin = 0;
oneshot_output_pin = 0;
oneshot_trigger_rising = 1;
oneshot_prepulse_us = 0;
oneshot_pulse_us = 0;
oneshot_quiescent_us = 0;
oneshot_retriggerable = 0;
oneshot_output_idle_level = 0;
oneshot_ignored_triggers = 0;
mT4IntEnable(1);
if (id >= 0)
cancel_alarm(id);
if (trigger_pin > 0)
ExtCurrentConfig[trigger_pin] &= (~EXT_COM_RESERVED);
if (output_pin > 0)
ExtCurrentConfig[output_pin] &= (~EXT_COM_RESERVED);
if (trigger_pin > 0)
{
if (callback_state == CALLBACK_ONESHOT)
gpio_set_irq_enabled_with_callback(PinDef[trigger_pin].GPno, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, false, &gpio_callback);
else if (callback_state & CALLBACK_ONESHOT)
gpio_set_irq_enabled(PinDef[trigger_pin].GPno, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, false);
}
CallBackEnabled &= (~CALLBACK_ONESHOT);
if (was_active && output_pin > 0 && (ExtCurrentConfig[output_pin] & (~EXT_COM_RESERVED)) == EXT_DIG_OUT)
{
if ((int)gpio_get_out_level(PinDef[output_pin].GPno) != idle_level)
PinSetBit(output_pin, LATINV);
}
}
int64_t __not_in_flash_func(oneshot_alarm_handler)(alarm_id_t id, void *user_data)
{
if (!oneshot_active || id != oneshot_alarm_id)
return 0;
if (oneshot_state == ONESHOT_STATE_PREDELAY)
{
PinSetBit(oneshot_output_pin, LATINV);
if (oneshot_pulse_us <= 0)
{
PinSetBit(oneshot_output_pin, LATINV);
if (oneshot_quiescent_us > 0)
{
oneshot_state = ONESHOT_STATE_QUIESCENT;
oneshot_alarm_id = add_alarm_in_us(oneshot_quiescent_us, oneshot_alarm_handler, NULL, true);
if (oneshot_alarm_id < 0)
{
oneshot_state = ONESHOT_STATE_IDLE;
oneshot_alarm_id = -1;
}
}
else
{
oneshot_state = ONESHOT_STATE_IDLE;
oneshot_alarm_id = -1;
}
return 0;
}
oneshot_state = ONESHOT_STATE_PULSE;
oneshot_alarm_id = add_alarm_in_us(oneshot_pulse_us, oneshot_alarm_handler, NULL, true);
if (oneshot_alarm_id < 0)
{
PinSetBit(oneshot_output_pin, LATINV);
oneshot_state = ONESHOT_STATE_IDLE;
oneshot_alarm_id = -1;
}
return 0;
}
if (oneshot_state == ONESHOT_STATE_PULSE)
{
PinSetBit(oneshot_output_pin, LATINV);
if (oneshot_quiescent_us > 0)
{
oneshot_state = ONESHOT_STATE_QUIESCENT;
oneshot_alarm_id = add_alarm_in_us(oneshot_quiescent_us, oneshot_alarm_handler, NULL, true);
if (oneshot_alarm_id < 0)
{
oneshot_state = ONESHOT_STATE_IDLE;
oneshot_alarm_id = -1;
}
}
else
{
oneshot_state = ONESHOT_STATE_IDLE;
oneshot_alarm_id = -1;
}
return 0;
}
if (oneshot_state == ONESHOT_STATE_QUIESCENT)
{
oneshot_state = ONESHOT_STATE_IDLE;
oneshot_alarm_id = -1;
}
return 0;
}
// Vector to CFunction routine called every command (ie, from the BASIC interrupt checker)
uint64_t readusclock(void)
{
return time_us_64() - uSecoffset;
}
void writeusclock(uint64_t timeset)
{
uSecoffset = time_us_64() - (uint64_t)timeset;
}
uint64_t readIRclock(void)
{
return time_us_64() - IRoffset;
}
void writeIRclock(void)
{
IRoffset = time_us_64();
}
#ifdef rp2350
const uint8_t PINMAP[48] = {1, 2, 4, 5, 6, 7, 9, 10, 11, 12, 14, 15, 16, 17, 19, 20, 21, 22, 24, 25, 26, 27, 29, 41, 42, 43, 31, 32, 34, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62};
int codemap(int pin)
{
#ifdef PICOMITEWEB
if (pin > (rp2350a ? 29 : 47) || pin < 0 || pin == 23 || pin == 24 || pin == 25 || pin == 29)
error("Invalid GPIO");
#else
if (pin > (rp2350a ? 29 : 47) || pin < 0)
error("Invalid GPIO");
#endif
return (int)PINMAP[pin];
}
#else
const uint8_t PINMAP[30] = {1, 2, 4, 5, 6, 7, 9, 10, 11, 12, 14, 15, 16, 17, 19, 20, 21, 22, 24, 25, 26, 27, 29, 41, 42, 43, 31, 32, 34, 44};
int codemap(int pin)
{
#ifdef PICOMITEWEB
if (pin > 29 || pin < 0 || pin == 23 || pin == 24 || pin == 25 || pin == 29)
error("Invalid GPIO");
#else
if (pin > 29 || pin < 0)
error("Invalid GPIO");
#endif
return (int)PINMAP[pin];
}
#endif
int codecheck(unsigned char *line)
{
if ((line[0] == 'G' || line[0] == 'g') && (line[1] == 'P' || line[1] == 'p'))
{
line += 2;
if (isnamestart(*line) || *line == '.')
return 1;
if (isdigit(*line) && !isnamechar(line[1]))
{
return 0;
}
line++;
if (!(isdigit(*line)))
return 2;
line++;
if (isnamechar(*line))
return 3;
}
else
return 4;
return 0;
}
// Last pin value computed by getpinarg, stored before validation.
// Used by mm.info(PIN) to retrieve the raw pin number even if IsInvalidPin fires.
int g_pinarg_result = 0;
// Parse a pin argument that may use GPnn or physical pin notation.
// Accepts a pointer to the argument text; returns the physical pin number.
// Supports: literal GPn/GPnn, numeric literals, numeric variables/expressions,
// string literals like "gp1", string variables, and functions returning strings or numbers.
// Validates the result with IsInvalidPin() and throws StandardError(9) if invalid.
int getpinarg(unsigned char *arg)
{
int pin = 0;
g_pinarg_result = 0;
skipspace(arg);
// First check for unquoted literal GPn/GPnn (the outlier case that
// evaluate cannot handle since GPn is not a valid expression)
if (codecheck(arg) == 0)
{
pin = codemap(getinteger(arg + 2));
}
else
{
// Use evaluate with T_NOTYPE to handle all other cases
MMFLOAT f;
long long int i64;
unsigned char *s;
int t = T_NOTYPE;
evaluate(arg, &f, &i64, &s, &t, false);
if (t & T_INT)
pin = (int)i64;
else if (t & T_NBR)
pin = (int)FloatToInt64(f);
else if (t & T_STR)
{
// String result - extract C string and check for GPnn format
int len = *s;
if (len < 3 || len > MAXVARLEN)
error("Invalid pin string");
unsigned char temp[MAXVARLEN + 1];
memcpy(temp, s + 1, len);
temp[len] = 0;
if (codecheck(temp) == 0)
pin = codemap(atoi((char *)&temp[2]));
else
error("Invalid pin string");
}
else
error("Syntax");
}
g_pinarg_result = pin;
if (IsInvalidPin(pin))
StandardError(9);
return pin;
}
#ifdef rp2350
void __not_in_flash_func(on_pwm_wrap_1)(void)
{
pwm_clear_irq(0);
INT5Count++;
}
#endif
void SoftReset(int code)
{
_excep_code = code;
#ifdef USBKEYBOARD
USBenabled = false;
uSec(50000); // wait for outstanding requests to complete
#endif
watchdog_enable(1, 1);
while (1)
{
}
}
#if LOWRAM
void PinSetBit(int pin, unsigned int offset)
{
#else
void __not_in_flash_func(PinSetBit)(int pin, unsigned int offset)
{
#endif
switch (offset)
{
case LATCLR:
gpio_set_pulls(PinDef[pin].GPno, false, false);
gpio_pull_down(PinDef[pin].GPno);
gpio_put(PinDef[pin].GPno, GPIO_PIN_RESET);
return;
case LATSET:
gpio_set_pulls(PinDef[pin].GPno, false, false);
gpio_pull_up(PinDef[pin].GPno);
gpio_put(PinDef[pin].GPno, GPIO_PIN_SET);
return;
case LATINV:
gpio_xor_mask64((uint64_t)1 << PinDef[pin].GPno);
return;
case TRISSET:
gpio_set_dir(PinDef[pin].GPno, GPIO_IN);
gpio_set_input_enabled(PinDef[pin].GPno, true);
uSec(2);
return;
case TRISCLR:
gpio_set_dir(PinDef[pin].GPno, GPIO_OUT);
gpio_set_input_enabled(PinDef[pin].GPno, false);
gpio_set_drive_strength(PinDef[pin].GPno, GPIO_DRIVE_STRENGTH_8MA);
uSec(2);
return;
case CNPUSET:
gpio_set_pulls(PinDef[pin].GPno, true, false);
return;
case CNPDSET:
gpio_set_pulls(PinDef[pin].GPno, false, true);
return;
case CNPUCLR:
case CNPDCLR:
gpio_set_pulls(PinDef[pin].GPno, false, false);
return;
case ODCCLR:
gpio_set_dir(PinDef[pin].GPno, GPIO_OUT);
gpio_put(PinDef[pin].GPno, GPIO_PIN_RESET);
uSec(2);
return;
case ODCSET:
gpio_set_pulls(PinDef[pin].GPno, true, false);
gpio_set_dir(PinDef[pin].GPno, GPIO_IN);
uSec(2);
return;
case ANSELCLR:
gpio_set_function(PinDef[pin].GPno, GPIO_FUNC_SIO);
gpio_set_dir(PinDef[pin].GPno, GPIO_IN);
return;
case ANSELSET:
gpio_set_dir(PinDef[pin].GPno, GPIO_IN);
gpio_disable_pulls(PinDef[pin].GPno);
gpio_set_input_enabled(PinDef[pin].GPno, false);
adc_select_input(PinDef[pin].ADCpin);
return;
default:
error("Unknown PinSetBit command");
}
}
int IsInvalidPin(int pin)
{
#ifdef rp2350
if (pin < 1 || pin > (rp2350a ? 44 : NBRPINS))
return true;
#else
if (pin < 1 || pin > NBRPINS)
return true;
#endif
if (PinDef[pin].mode & UNUSED)
return true;
return false;
}
#if LOWRAM
void ExtSet(int pin, int val)
{
#else
void __not_in_flash_func(ExtSet)(int pin, int val)
{
#endif
if (ExtCurrentConfig[pin] == EXT_NOT_CONFIG || ExtCurrentConfig[pin] == EXT_DIG_OUT /* || ExtCurrentConfig[pin] == EXT_OC_OUT*/)
{
PinSetBit(pin, val ? LATSET : LATCLR);
if (ExtCurrentConfig[pin] == EXT_NOT_CONFIG)
{
pinmask |= (1 << PinDef[pin].GPno);
if (val)
pinmask |= (1 << PinDef[pin].GPno);
else
pinmask &= (~(1 << PinDef[pin].GPno));
gpio_set_input_enabled(PinDef[pin].GPno, false);
last_adc = 99;
}
// INTEnableInterrupts();
}
else if (ExtCurrentConfig[pin] == EXT_CNT_IN)
{ // allow the user to zero the count
if (pin == Option.INT1pin)
INT1Count = val;
if (pin == Option.INT2pin)
INT2Count = val;
if (pin == Option.INT3pin)
INT3Count = val;
if (pin == Option.INT4pin)
INT4Count = val;
}
else
error("Pin %/| is not an output", pin, pin);
}
/* @endcond */
#if defined(PICOMITEVGA) && !defined(rp2350)
void cmd_sync(void)
{
#else
void __not_in_flash_func(cmd_sync)(void)
{
#endif
uint64_t i;
static uint64_t synctime = 0, endtime = 0;
getcsargs(&cmdline, 3);
if (synctime && argc == 0)
{
while (time_us_64() < endtime)
{
if (synctime - time_us_64() > 2000)
CheckAbort();
}
endtime += synctime;
}
else
{
if (argc == 0)
error("sync not initialised");
i = getint(argv[0], 0, 0x7FFFFFFFFFFFFFFF);
if (i)
{
if (argc == 3)
{
if (checkstring(argv[2], (unsigned char *)"U"))
{
i *= 1;
}
else if (checkstring(argv[2], (unsigned char *)"M"))
{
i *= 1000;
}
else if (checkstring(argv[2], (unsigned char *)"S"))
{
i *= 1000000;
}
}
synctime = i;
endtime = time_us_64() + synctime;
}
else
{
synctime = endtime = 0;
}
}
}
// this is invoked as a command (ie, pin(3) = 1)
// first get the argument then step over the closing bracket. Search through the rest of the command line looking
// for the equals sign and step over it, evaluate the rest of the command and set the pin accordingly
void cmd_pin(void)
{
int pin, value;
pin = getpinarg(cmdline);
while (*cmdline && tokenfunction(*cmdline) != op_equal)
cmdline++;
if (!*cmdline)
SyntaxError();
++cmdline;
if (!*cmdline)
SyntaxError();
value = getinteger(cmdline);
ExtSet(pin, value);
}
/*
* @cond
* The following section will be excluded from the documentation.
*/
void ClearPin(int pin)
{
if (pin == IRpin)
IRpin = 99;
if (pin == PWM0Apin)
PWM0Apin = 99;
if (pin == PWM1Apin)
PWM1Apin = 99;
if (pin == PWM2Apin)
PWM2Apin = 99;
if (pin == PWM3Apin)
PWM3Apin = 99;
if (pin == PWM4Apin)
PWM4Apin = 99;
if (pin == PWM5Apin)
PWM5Apin = 99;
if (pin == PWM6Apin)
PWM6Apin = 99;
if (pin == PWM7Apin)
PWM7Apin = 99;
#ifdef rp2350
if (pin == PWM8Apin)
PWM8Apin = 99;
if (pin == PWM9Apin)
PWM9Apin = 99;
if (pin == PWM10Apin)
PWM10Apin = 99;
if (pin == PWM11Apin)
PWM11Apin = 99;
#endif
if (pin == PWM0Bpin)
PWM0Bpin = 99;
if (pin == PWM1Bpin)
PWM1Bpin = 99;
if (pin == PWM2Bpin)
PWM2Bpin = 99;
if (pin == PWM3Bpin)
PWM3Bpin = 99;
if (pin == PWM4Bpin)
PWM4Bpin = 99;
if (pin == PWM5Bpin)
PWM5Bpin = 99;
if (pin == PWM6Bpin)
PWM6Bpin = 99;
if (pin == PWM7Bpin)
PWM7Bpin = 99;
#ifdef rp2350
if (pin == PWM8Bpin)
PWM8Bpin = 99;
if (pin == PWM9Bpin)
PWM9Bpin = 99;
if (pin == PWM10Bpin)
PWM10Bpin = 99;
if (pin == PWM11Bpin)
PWM11Bpin = 99;
#endif
if (pin == UART0TXpin)
UART0TXpin = 99;
if (pin == UART0RXpin)
UART0RXpin = 99;
if (pin == UART1RXpin)
UART1RXpin = 99;
if (pin == UART1TXpin)
UART1TXpin = 99;
if (pin == SPI1TXpin)
SPI1TXpin = 99;
if (pin == SPI1RXpin)
SPI1RXpin = 99;
if (pin == SPI1SCKpin)
SPI1SCKpin = 99;
if (pin == SPI0TXpin)
SPI0TXpin = 99;
if (pin == SPI0RXpin)
SPI0RXpin = 99;
if (pin == SPI0SCKpin)
SPI0SCKpin = 99;
if (pin == I2C1SDApin)
I2C1SDApin = 99;
if (pin == I2C1SCLpin)
I2C1SCLpin = 99;
if (pin == I2C0SDApin)
I2C0SDApin = 99;
if (pin == I2C0SCLpin)
I2C0SCLpin = 99;
}
/****************************************************************************************************************************
Configure an I/O pin
*****************************************************************************************************************************/
void MIPS16 ExtCfg(int pin, int cfg, int option)
{
int i, tris = 0, ana = 0, edge;
CheckPin(pin, CP_IGNORE_INUSE | CP_IGNORE_RESERVED);
if (cfg >= EXT_DS18B20_RESERVED)
{
ExtCurrentConfig[pin] |= cfg; // don't do anything except set the config type
return;
}
ClearPin(pin); // disable the link to any special functions
if (pin == Option.INT1pin)
{
if (CallBackEnabled == 2)
gpio_set_irq_enabled_with_callback(PinDef[pin].GPno, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, false, &gpio_callback);
else
gpio_set_irq_enabled(PinDef[pin].GPno, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, false);
CallBackEnabled &= (~2);
}
if (pin == Option.INT2pin)
{
if (CallBackEnabled == 4)
gpio_set_irq_enabled_with_callback(PinDef[pin].GPno, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, false, &gpio_callback);
else
gpio_set_irq_enabled(PinDef[pin].GPno, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, false);
CallBackEnabled &= (~4);
}
if (pin == Option.INT3pin)
{
if (CallBackEnabled == 8)
gpio_set_irq_enabled_with_callback(PinDef[pin].GPno, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, false, &gpio_callback);
else
gpio_set_irq_enabled(PinDef[pin].GPno, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, false);
CallBackEnabled &= (~8);
}
if (pin == Option.INT4pin)
{
if (CallBackEnabled == 16)
gpio_set_irq_enabled_with_callback(PinDef[pin].GPno, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, false, &gpio_callback);
else
gpio_set_irq_enabled(PinDef[pin].GPno, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, false);
CallBackEnabled &= (~16);
}
#ifdef rp2350
if (pin == FAST_TIMER_PIN && ExtCurrentConfig[pin] == EXT_FAST_TIMER)
{
slice0 = 0;
pwm_set_irq1_enabled(0, false);
}
#endif
// make sure any pullups/pulldowns are removed in case we are changing from a digital input
gpio_disable_pulls(PinDef[pin].GPno);
// disable ADC if we are changing from a analogue input
if (ExtCurrentConfig[pin] == EXT_ANA_IN || ExtCurrentConfig[pin] == EXT_ADCRAW)
PinSetBit(pin, ANSELCLR);
for (i = 0; i < NBRINTERRUPTS; i++)
if (inttbl[i].pin == pin)
inttbl[i].pin = 0; // start off by disable a software interrupt (if set) on this pin
gpio_set_input_enabled(PinDef[pin].GPno, false);
gpio_deinit(PinDef[pin].GPno);
gpio_set_input_hysteresis_enabled(PinDef[pin].GPno, true);
if (cfg != EXT_NOT_CONFIG)
gpio_init(PinDef[pin].GPno);
switch (cfg)
{
case EXT_NOT_CONFIG:
tris = 1;
ana = 1;
// gpio_init(PinDef[pin].GPno);
// gpio_set_input_hysteresis_enabled(PinDef[pin].GPno,true);
gpio_set_input_enabled(PinDef[pin].GPno, false);
gpio_deinit(PinDef[pin].GPno);
switch (ExtCurrentConfig[pin])
{ // Disable the pin numbers used by the special function code
case EXT_IR:
IRpin = 99;
break;
case EXT_PWM0A:
PWM0Apin = 99;
break;
case EXT_PWM1A:
PWM1Apin = 99;
break;
case EXT_PWM2A:
PWM2Apin = 99;
break;
case EXT_PWM3A:
PWM3Apin = 99;
break;
case EXT_PWM4A:
PWM4Apin = 99;
break;
case EXT_PWM5A:
PWM5Apin = 99;
break;
case EXT_PWM6A:
PWM6Apin = 99;
break;
case EXT_PWM7A:
PWM7Apin = 99;
break;
#ifdef rp2350
case EXT_PWM8A:
PWM8Apin = 99;
break;
case EXT_PWM9A:
PWM9Apin = 99;
break;
case EXT_PWM10A:
PWM10Apin = 99;
break;
case EXT_PWM11A:
PWM11Apin = 99;
break;
#endif
case EXT_PWM0B:
PWM0Bpin = 99;
break;
case EXT_PWM1B:
PWM1Bpin = 99;
break;
case EXT_PWM2B:
PWM2Bpin = 99;
break;
case EXT_PWM3B:
PWM3Bpin = 99;
break;
case EXT_PWM4B:
PWM4Bpin = 99;
break;
case EXT_PWM5B:
PWM5Bpin = 99;
break;
case EXT_PWM6B:
PWM6Bpin = 99;
break;
case EXT_PWM7B:
PWM7Bpin = 99;
break;
#ifdef rp2350
case EXT_PWM8B:
PWM8Bpin = 99;
break;
case EXT_PWM9B:
PWM9Bpin = 99;
break;
case EXT_PWM10B:
PWM10Bpin = 99;
break;
case EXT_PWM11B:
PWM11Bpin = 99;
break;
#endif
case EXT_UART0TX:
UART0TXpin = 99;
break;
case EXT_UART0RX:
UART0RXpin = 99;
break;
case EXT_UART1TX:
UART1TXpin = 99;
break;
case EXT_UART1RX:
UART1RXpin = 99;
break;
case EXT_I2C0SDA:
I2C0SDApin = 99;
break;
case EXT_I2C0SCL:
I2C0SCLpin = 99;
break;
case EXT_I2C1SDA:
I2C1SDApin = 99;
break;
case EXT_I2C1SCL:
I2C1SCLpin = 99;
break;
case EXT_SPI0RX:
SPI0RXpin = 99;
break;
case EXT_SPI0TX:
SPI0TXpin = 99;
break;
case EXT_SPI0SCK:
SPI0SCKpin = 99;
break;
case EXT_SPI1RX:
SPI1RXpin = 99;
break;
case EXT_SPI1TX:
SPI1TXpin = 99;
break;
case EXT_SPI1SCK:
SPI1SCKpin = 99;
break;
}
break;
#ifdef rp2350
case EXT_ADCRAW:
case EXT_ANA_IN:
if (!(PinDef[pin].mode & ANALOG_IN))
StandardError(8);
if (pin <= 44 && rp2350a == 0)
StandardError(8);
if (pin > 44 && rp2350a)
StandardError(8);
tris = 1;
ana = 0;
break;
#else
case EXT_ADCRAW:
case EXT_ANA_IN:
if (!(PinDef[pin].mode & ANALOG_IN))
StandardError(8);
tris = 1;
ana = 0;
break;
#endif
case EXT_CNT_IN: