/
jshardware.c
3125 lines (2842 loc) · 104 KB
/
jshardware.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
/*
* This file is part of Espruino, a JavaScript interpreter for Microcontrollers
*
* Copyright (C) 2013 Gordon Williams <gw@pur3.co.uk>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* ----------------------------------------------------------------------------
* Platform Specific part of Hardware interface Layer
* ----------------------------------------------------------------------------
*/
#ifdef USB
#ifdef LEGACY_USB
#include "legacy_usb.h"
#else
#include "usbd_cdc_hid.h"
#endif
#endif
#include "stm32_compat.h"
#include "jshardware.h"
#include "jstimer.h"
#include "jsutils.h"
#include "jsparse.h"
#include "jsinteractive.h"
#include "jswrap_io.h"
#include "jsspi.h"
#include "jsflags.h"
#ifdef ESPRUINOBOARD
// STM32F1 boards should work with this - but for some reason they crash on init
#define USE_RTC
#endif
#ifdef PICO
#define USE_RTC
#endif
#ifdef ESPRUINOWIFI
#define USE_RTC
#endif
#define IRQ_PRIOR_SPI 1 // we want to be very sure of not losing SPI (this is handled quickly too)
#define IRQ_PRIOR_SYSTICK 2
#define IRQ_PRIOR_USART 6 // a little higher so we don't get lockups of something tries to print
#define IRQ_PRIOR_MED 7
#define IRQ_PRIOR_LOW 15
#ifdef USE_RTC
#include "jswrap_date.h" // for non-F1 calendar -> days since 1970 conversion
// TODO: could jshRTCPrescaler (and the hardware prescaler) be modified on SysTick, to calibrate the LSI against the HSE?
unsigned short jshRTCPrescaler;
unsigned short jshRTCPrescalerReciprocal; // (JSSYSTIME_SECOND << RTC_PRESCALER_RECIPROCAL_SHIFT) / jshRTCPrescaler;
#define RTC_PRESCALER_RECIPROCAL_SHIFT 10
#define RTC_INITIALISE_TICKS 8 // SysTicks before we initialise the RTC - we need to wait until the LSE starts up properly
#define JSSYSTIME_EXTRA_BITS 8 // extra bits we shove on under the RTC (we try and get these from SysTick)
#define JSSYSTIME_SECOND_SHIFT 20
#define JSSYSTIME_SECOND (1<<JSSYSTIME_SECOND_SHIFT) // Random value we chose - the accuracy we're allowing (1 microsecond)
JsSysTime jshGetRTCSystemTime();
#else
#define jshGetRTCSystemTime jshGetSystemTime
#endif
static JsSysTime jshGetTimeForSecond();
// The amount of systicks for one second depends on the clock speed
#define SYSTICKS_FOR_ONE_SECOND (1+(CLOCK_SPEED_MHZ*1000000/SYSTICK_RANGE))
// see jshPinWatch/jshGetWatchedPinState
Pin watchedPins[16];
// Whether a pin is being used for soft PWM or not
BITFIELD_DECL(jshPinSoftPWM, JSH_PIN_COUNT);
#ifdef STM32F1
// F1 can't do opendrain pullup, so we do it manually!
BITFIELD_DECL(jshPinOpendrainPullup, JSH_PIN_COUNT);
#endif
#define SPI_ENABLED (ESPR_SPI_COUNT>0)
#define I2C_ENABLED (ESPR_I2C_COUNT>0)
#define USART_ENABLED (ESPR_USART_COUNT>0)
#if SPI_ENABLED
// simple 4 byte buffers for SPI
#define JSH_SPIBUF_MASK 3 // 4 bytes
volatile unsigned char jshSPIBufHead[ESPR_SPI_COUNT];
volatile unsigned char jshSPIBufTail[ESPR_SPI_COUNT];
volatile unsigned char jshSPIBuf[ESPR_SPI_COUNT][4]; // 4 bytes packed into an int
Pin jshNeoPixelPin = PIN_UNDEFINED; ///< The currently setup Neopixel pin (set by jswrap_neopixel). This is reset to PIN_UNDEFINED if we think anything could have messed it up
#endif
#ifdef USB
JsSysTime jshLastWokenByUSB = 0;
volatile unsigned char jshUSBReceiveLastActive = 0; ///< How many systicks since USB was actively requesting data?
#define JSH_USB_MAX_INACTIVITY_TICKS 10 ///< How many systicks before we start throwing away/deleting EV_USBSERIAL data
#endif
#if USART_ENABLED
/* On STM32 there's no 7 bit UART mode, so
* we much just fake it by using an 8 bit UART
* and then masking off the top bit */
unsigned char jsh7BitUART;
bool jshIsSerial7Bit(IOEventFlags device) {
assert(ESPR_USART_COUNT<=8);
return jsh7BitUART & (1<<(device-EV_SERIAL1));
}
void jshSetIsSerial7Bit(IOEventFlags device, bool is7Bit) {
assert(ESPR_USART_COUNT<=8);
if (is7Bit) jsh7BitUART |= (1<<(device-EV_SERIAL1));
else jsh7BitUART &= ~(1<<(device-EV_SERIAL1));
}
#endif
// ----------------------------------------------------------------------------
// PINS
static ALWAYS_INLINE uint8_t pinToEVEXTI(Pin ipin) {
JsvPinInfoPin pin = pinInfo[ipin].pin;
return (uint8_t)(EV_EXTI0+(pin-JSH_PIN0));
/*if (pin==JSH_PIN0 ) return EV_EXTI0;
if (pin==JSH_PIN1 ) return EV_EXTI1;
if (pin==JSH_PIN2 ) return EV_EXTI2;
if (pin==JSH_PIN3 ) return EV_EXTI3;
if (pin==JSH_PIN4 ) return EV_EXTI4;
if (pin==JSH_PIN5 ) return EV_EXTI5;
if (pin==JSH_PIN6 ) return EV_EXTI6;
if (pin==JSH_PIN7 ) return EV_EXTI7;
if (pin==JSH_PIN8 ) return EV_EXTI8;
if (pin==JSH_PIN9 ) return EV_EXTI9;
if (pin==JSH_PIN10) return EV_EXTI10;
if (pin==JSH_PIN11) return EV_EXTI11;
if (pin==JSH_PIN12) return EV_EXTI12;
if (pin==JSH_PIN13) return EV_EXTI13;
if (pin==JSH_PIN14) return EV_EXTI14;
if (pin==JSH_PIN15) return EV_EXTI15;
jsExceptionHere(JSET_INTERNALERROR, "pinToEVEXTI");
return EV_NONE;*/
}
static ALWAYS_INLINE uint16_t stmPin(Pin ipin) {
JsvPinInfoPin pin = pinInfo[ipin].pin;
return (uint16_t)(1 << (pin-JSH_PIN0));
/* if (pin==JSH_PIN0 ) return GPIO_Pin_0;
if (pin==JSH_PIN1 ) return GPIO_Pin_1;
if (pin==JSH_PIN2 ) return GPIO_Pin_2;
if (pin==JSH_PIN3 ) return GPIO_Pin_3;
if (pin==JSH_PIN4 ) return GPIO_Pin_4;
if (pin==JSH_PIN5 ) return GPIO_Pin_5;
if (pin==JSH_PIN6 ) return GPIO_Pin_6;
if (pin==JSH_PIN7 ) return GPIO_Pin_7;
if (pin==JSH_PIN8 ) return GPIO_Pin_8;
if (pin==JSH_PIN9 ) return GPIO_Pin_9;
if (pin==JSH_PIN10) return GPIO_Pin_10;
if (pin==JSH_PIN11) return GPIO_Pin_11;
if (pin==JSH_PIN12) return GPIO_Pin_12;
if (pin==JSH_PIN13) return GPIO_Pin_13;
if (pin==JSH_PIN14) return GPIO_Pin_14;
if (pin==JSH_PIN15) return GPIO_Pin_15;
jsExceptionHere(JSET_INTERNALERROR, "stmPin");
return GPIO_Pin_0;*/
}
static ALWAYS_INLINE uint32_t stmExtI(Pin ipin) {
JsvPinInfoPin pin = pinInfo[ipin].pin;
#ifdef STM32F3
return (uint32_t)(pin-JSH_PIN0); // STM32F3 is different - thanks @Frida!
#else
return (uint32_t)(1 << (pin-JSH_PIN0));
#endif
/* if (pin==JSH_PIN0 ) return EXTI_Line0;
if (pin==JSH_PIN1 ) return EXTI_Line1;
if (pin==JSH_PIN2 ) return EXTI_Line2;
if (pin==JSH_PIN3 ) return EXTI_Line3;
if (pin==JSH_PIN4 ) return EXTI_Line4;
if (pin==JSH_PIN5 ) return EXTI_Line5;
if (pin==JSH_PIN6 ) return EXTI_Line6;
if (pin==JSH_PIN7 ) return EXTI_Line7;
if (pin==JSH_PIN8 ) return EXTI_Line8;
if (pin==JSH_PIN9 ) return EXTI_Line9;
if (pin==JSH_PIN10) return EXTI_Line10;
if (pin==JSH_PIN11) return EXTI_Line11;
if (pin==JSH_PIN12) return EXTI_Line12;
if (pin==JSH_PIN13) return EXTI_Line13;
if (pin==JSH_PIN14) return EXTI_Line14;
if (pin==JSH_PIN15) return EXTI_Line15;
jsExceptionHere(JSET_INTERNALERROR, "stmExtI");
return EXTI_Line0;*/
}
static ALWAYS_INLINE GPIO_TypeDef *stmPort(Pin pin) {
JsvPinInfoPort port = pinInfo[pin].port&JSH_PORT_MASK;
return (GPIO_TypeDef *)((char*)GPIOA + (port-JSH_PORTA)*0x0400);
/*if (port == JSH_PORTA) return GPIOA;
if (port == JSH_PORTB) return GPIOB;
if (port == JSH_PORTC) return GPIOC;
if (port == JSH_PORTD) return GPIOD;
if (port == JSH_PORTE) return GPIOE;
if (port == JSH_PORTF) return GPIOF;
#if defined(STM32F4)
if (port == JSH_PORTG) return GPIOG;
if (port == JSH_PORTH) return GPIOH;
#endif
jsExceptionHere(JSET_INTERNALERROR, "stmPort");
return GPIOA;*/
}
static ALWAYS_INLINE uint8_t stmPinSource(JsvPinInfoPin ipin) {
JsvPinInfoPin pin = pinInfo[ipin].pin;
return (uint8_t)(pin-JSH_PIN0);
/*if (pin==JSH_PIN0 ) return GPIO_PinSource0;
if (pin==JSH_PIN1 ) return GPIO_PinSource1;
if (pin==JSH_PIN2 ) return GPIO_PinSource2;
if (pin==JSH_PIN3 ) return GPIO_PinSource3;
if (pin==JSH_PIN4 ) return GPIO_PinSource4;
if (pin==JSH_PIN5 ) return GPIO_PinSource5;
if (pin==JSH_PIN6 ) return GPIO_PinSource6;
if (pin==JSH_PIN7 ) return GPIO_PinSource7;
if (pin==JSH_PIN8 ) return GPIO_PinSource8;
if (pin==JSH_PIN9 ) return GPIO_PinSource9;
if (pin==JSH_PIN10) return GPIO_PinSource10;
if (pin==JSH_PIN11) return GPIO_PinSource11;
if (pin==JSH_PIN12) return GPIO_PinSource12;
if (pin==JSH_PIN13) return GPIO_PinSource13;
if (pin==JSH_PIN14) return GPIO_PinSource14;
if (pin==JSH_PIN15) return GPIO_PinSource15;
jsExceptionHere(JSET_INTERNALERROR, "stmPinSource");
return GPIO_PinSource0;*/
}
static ALWAYS_INLINE uint8_t stmPortSource(Pin pin) {
JsvPinInfoPort port = pinInfo[pin].port&JSH_PORT_MASK;
return (uint8_t)(port-JSH_PORTA);
/*#ifdef STM32API2
if (port == JSH_PORTA) return EXTI_PortSourceGPIOA;
if (port == JSH_PORTB) return EXTI_PortSourceGPIOB;
if (port == JSH_PORTC) return EXTI_PortSourceGPIOC;
if (port == JSH_PORTD) return EXTI_PortSourceGPIOD;
if (port == JSH_PORTE) return EXTI_PortSourceGPIOE;
if (port == JSH_PORTF) return EXTI_PortSourceGPIOF;
#if defined(STM32F4)
if (port == JSH_PORTG) return EXTI_PortSourceGPIOG;
if (port == JSH_PORTH) return EXTI_PortSourceGPIOH;
#endif
jsExceptionHere(JSET_INTERNALERROR, "stmPortSource");
return EXTI_PortSourceGPIOA;
#else
if (port == JSH_PORTA) return GPIO_PortSourceGPIOA;
if (port == JSH_PORTB) return GPIO_PortSourceGPIOB;
if (port == JSH_PORTC) return GPIO_PortSourceGPIOC;
if (port == JSH_PORTD) return GPIO_PortSourceGPIOD;
if (port == JSH_PORTE) return GPIO_PortSourceGPIOE;
if (port == JSH_PORTF) return GPIO_PortSourceGPIOF;
if (port == JSH_PORTG) return GPIO_PortSourceGPIOG;
jsExceptionHere(JSET_INTERNALERROR, "stmPortSource");
return GPIO_PortSourceGPIOA;
#endif*/
}
static ADC_TypeDef *stmADC(JsvPinInfoAnalog analog) {
if (analog & JSH_ANALOG1) return ADC1;
#ifdef ADC2
if (analog & JSH_ANALOG2) return ADC2;
#endif
#ifdef ADC3
if (analog & JSH_ANALOG3) return ADC3;
#endif
#ifdef ADC4
if (analog & JSH_ANALOG4) return ADC4;
#endif
jsExceptionHere(JSET_INTERNALERROR, "stmADC");
return ADC1;
}
static uint8_t stmADCChannel(JsvPinInfoAnalog analog) {
switch (analog & JSH_MASK_ANALOG_CH) {
#ifndef STM32F3XX
case JSH_ANALOG_CH0 : return ADC_Channel_0;
#endif
case JSH_ANALOG_CH1 : return ADC_Channel_1;
case JSH_ANALOG_CH2 : return ADC_Channel_2;
case JSH_ANALOG_CH3 : return ADC_Channel_3;
case JSH_ANALOG_CH4 : return ADC_Channel_4;
case JSH_ANALOG_CH5 : return ADC_Channel_5;
case JSH_ANALOG_CH6 : return ADC_Channel_6;
case JSH_ANALOG_CH7 : return ADC_Channel_7;
case JSH_ANALOG_CH8 : return ADC_Channel_8;
case JSH_ANALOG_CH9 : return ADC_Channel_9;
case JSH_ANALOG_CH10 : return ADC_Channel_10;
case JSH_ANALOG_CH11 : return ADC_Channel_11;
case JSH_ANALOG_CH12 : return ADC_Channel_12;
case JSH_ANALOG_CH13 : return ADC_Channel_13;
case JSH_ANALOG_CH14 : return ADC_Channel_14;
case JSH_ANALOG_CH15 : return ADC_Channel_15;
case JSH_ANALOG_CH16 : return ADC_Channel_16;
case JSH_ANALOG_CH17 : return ADC_Channel_17;
default: jsExceptionHere(JSET_INTERNALERROR, "stmADCChannel"); return 0;
}
}
#ifdef STM32API2
static uint8_t functionToAF(JshPinFunction func) {
#if defined(STM32F401xx) || defined(STM32F411xx)
assert(JSH_AF0==0 && JSH_AF15==15); // check mapping is right
return func & JSH_MASK_AF;
#elif defined(STM32F4) || defined(STM32F2)
switch (func & JSH_MASK_TYPE) {
case JSH_SPI1 : return GPIO_AF_SPI1;
case JSH_SPI2 : return GPIO_AF_SPI2;
case JSH_SPI3 : return GPIO_AF_SPI3;
case JSH_I2C1 : return GPIO_AF_I2C1;
case JSH_I2C2 : return GPIO_AF_I2C2;
case JSH_I2C3 : return GPIO_AF_I2C3;
case JSH_TIMER1 : return GPIO_AF_TIM1;
case JSH_TIMER2 : return GPIO_AF_TIM2;
case JSH_TIMER3 : return GPIO_AF_TIM3;
case JSH_TIMER4 : return GPIO_AF_TIM4;
case JSH_TIMER5 : return GPIO_AF_TIM5;
case JSH_TIMER8 : return GPIO_AF_TIM8;
case JSH_TIMER9 : return GPIO_AF_TIM9;
case JSH_TIMER10 : return GPIO_AF_TIM10;
case JSH_TIMER11 : return GPIO_AF_TIM11;
case JSH_TIMER12 : return GPIO_AF_TIM12;
case JSH_USART1 : return GPIO_AF_USART1;
case JSH_USART2 : return GPIO_AF_USART2;
case JSH_USART3 : return GPIO_AF_USART3;
case JSH_USART4 : return GPIO_AF_UART4;
case JSH_USART5 : return GPIO_AF_UART5;
case JSH_USART6 : return GPIO_AF_USART6;
default: jsExceptionHere(JSET_INTERNALERROR, "functionToAF");return 0;
}
#else // will be F3
switch (func & JSH_MASK_AF) {
case JSH_AF0 : return GPIO_AF_0;
case JSH_AF1 : return GPIO_AF_1;
case JSH_AF2 : return GPIO_AF_2;
case JSH_AF3 : return GPIO_AF_3;
case JSH_AF4 : return GPIO_AF_4;
case JSH_AF5 : return GPIO_AF_5;
case JSH_AF6 : return GPIO_AF_6;
case JSH_AF7 : return GPIO_AF_7;
case JSH_AF8 : return GPIO_AF_8;
case JSH_AF9 : return GPIO_AF_9;
case JSH_AF10 : return GPIO_AF_10;
case JSH_AF11 : return GPIO_AF_11;
case JSH_AF12 : return GPIO_AF_12;
//case JSH_AF13 : return GPIO_AF_13;
case JSH_AF14 : return GPIO_AF_14;
case JSH_AF15 : return GPIO_AF_15;
default: jsExceptionHere(JSET_INTERNALERROR, "functionToAF");return 0;
}
#endif
}
#endif
static uint64_t DEVICE_INITIALISED_FLAGS = 0L;
bool jshIsDeviceInitialised(IOEventFlags device) {
uint64_t mask = 1ULL << (int)device;
return (DEVICE_INITIALISED_FLAGS & mask) != 0L;
}
void jshSetDeviceInitialised(IOEventFlags device, bool isInit) {
uint64_t mask = 1ULL << (int)device;
if (isInit) {
DEVICE_INITIALISED_FLAGS |= mask;
} else {
DEVICE_INITIALISED_FLAGS &= ~mask;
}
}
void *setDeviceClockCmd(JshPinFunction device, FunctionalState cmd) {
device = device&JSH_MASK_TYPE;
void *ptr = 0;
if (0){
#if defined(USART1) && ESPR_USART_COUNT>=1
} else if (device == JSH_USART1) {
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, cmd);
ptr = USART1;
#endif
#if defined(USART2) && ESPR_USART_COUNT>=2
} else if (device == JSH_USART2) {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, cmd);
ptr = USART2;
#endif
#if defined(USART3) && ESPR_USART_COUNT>=3
} else if (device == JSH_USART3) {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, cmd);
ptr = USART3;
#endif
#if defined(UART4) && ESPR_USART_COUNT>=4
} else if (device == JSH_USART4) {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, cmd);
ptr = UART4;
#endif
#if defined(UART5) && ESPR_USART_COUNT>=5
} else if (device == JSH_USART5) {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, cmd);
ptr = UART5;
#endif
#if defined(USART6) && ESPR_USART_COUNT>=6
} else if (device == JSH_USART6) {
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, cmd);
ptr = USART6;
#endif
#if ESPR_SPI_COUNT >= 1
} else if (device==JSH_SPI1) {
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, cmd);
ptr = SPI1;
#endif
#if ESPR_SPI_COUNT >= 2
} else if (device==JSH_SPI2) {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, cmd);
ptr = SPI2;
#endif
#if ESPR_SPI_COUNT >= 3
} else if (device==JSH_SPI3) {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, cmd);
ptr = SPI3;
#endif
#if ESPR_I2C_COUNT >= 1
} else if (device==JSH_I2C1) {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, cmd);
/* Seems some F103 parts require this reset step - some hardware problem */
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);
ptr = I2C1;
#endif
#if ESPR_I2C_COUNT >= 2
} else if (device==JSH_I2C2) {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, cmd);
/* Seems some F103 parts require this reset step - some hardware problem */
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE);
ptr = I2C2;
#endif
#if ESPR_I2C_COUNT >= 3
} else if (device==JSH_I2C3) {
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C3, cmd);
/* Seems some F103 parts require this reset step - some hardware problem */
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C3, ENABLE);
RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C3, DISABLE);
ptr = I2C3;
#endif
} else if (device==JSH_TIMER1) {
ptr = TIM1;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, cmd);
} else if (device==JSH_TIMER2) {
ptr = TIM2;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, cmd);
} else if (device==JSH_TIMER3) {
ptr = TIM3;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, cmd);
} else if (device==JSH_TIMER4) {
ptr = TIM4;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, cmd);
#ifdef TIM5
} else if (device==JSH_TIMER5) {
ptr = TIM5;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, cmd);
#endif
#ifdef TIM6
} else if (device==JSH_TIMER6) { // Not used for outputs
ptr = TIM6;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, cmd);
#endif
#ifdef TIM7
} else if (device==JSH_TIMER7) { // Not used for outputs
ptr = TIM7;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7, cmd);
#endif
#ifdef TIM8
} else if (device==JSH_TIMER8) {
ptr = TIM8;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, cmd);
#endif
#ifdef TIM9
} else if (device==JSH_TIMER9) {
ptr = TIM9;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM9, cmd);
#endif
#ifdef TIM10
} else if (device==JSH_TIMER10) {
ptr = TIM10;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM10, cmd);
#endif
#ifdef TIM11
} else if (device==JSH_TIMER11) {
ptr = TIM11;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM11, cmd);
#endif
#ifdef TIM12
} else if (device==JSH_TIMER12) {
ptr = TIM12;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM12, cmd);
#endif
#ifdef TIM13
} else if (device==JSH_TIMER13) {
ptr = TIM13;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM13, cmd);
#endif
#ifdef TIM14
} else if (device==JSH_TIMER14) {
ptr = TIM14;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, cmd);
#endif
#ifdef TIM15
} else if (device==JSH_TIMER15) {
ptr = TIM15;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM15, cmd);
#endif
#ifdef TIM16
} else if (device==JSH_TIMER16) {
ptr = TIM16;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM16, cmd);
#endif
#ifdef TIM17
} else if (device==JSH_TIMER17) {
ptr = TIM17;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM17, cmd);
#endif
} else {
jsExceptionHere(JSET_INTERNALERROR, "setDeviceClockCmd: Unknown Device %d", (int)device);
}
return ptr;
}
TIM_TypeDef* getTimerFromPinFunction(JshPinFunction device) {
switch (device&JSH_MASK_TYPE) {
case JSH_TIMER1:
return TIM1;
case JSH_TIMER2:
return TIM2;
case JSH_TIMER3:
return TIM3;
case JSH_TIMER4:
return TIM4;
#ifndef STM32F3
case JSH_TIMER5:
return TIM5;
#endif
#ifdef TIM6
case JSH_TIMER6: // Not used for outputs
return TIM6;
#endif
#ifdef TIM7
case JSH_TIMER7:
return TIM7;
#endif
#ifdef TIM8
case JSH_TIMER8:
return TIM8;
#endif
#ifdef TIM9
case JSH_TIMER9:
return TIM9;
#endif
#ifdef TIM10
case JSH_TIMER10:
return TIM10;
#endif
#ifdef TIM11
case JSH_TIMER11:
return TIM11;
#endif
#ifdef TIM12
case JSH_TIMER12:
return TIM12;
#endif
#ifdef TIM13
case JSH_TIMER13:
return TIM13;
#endif
#ifdef TIM14
case JSH_TIMER14:
return TIM14;
#endif
#ifdef TIM15
case JSH_TIMER15:
return TIM15;
#endif
#ifdef TIM16
case JSH_TIMER16:
return TIM16;
#endif
#ifdef TIM17
case JSH_TIMER17:
return TIM17;
#endif
}
return 0;
}
#if USART_ENABLED
USART_TypeDef* getUsartFromDevice(IOEventFlags device) {
switch (device) {
case EV_SERIAL1 : return USART1;
#if ESPR_USART_COUNT>=2 && defined(USART2)
case EV_SERIAL2 : return USART2;
#endif
#if ESPR_USART_COUNT>=3 && defined(USART3)
case EV_SERIAL3 : return USART3;
#endif
#if ESPR_USART_COUNT>=4 && defined(UART4)
case EV_SERIAL4 : return UART4;
#endif
#if ESPR_USART_COUNT>=5 && defined(UART5)
case EV_SERIAL5 : return UART5;
#endif
#if ESPR_USART_COUNT>=6
case EV_SERIAL6 : return USART6;
#endif
default: return 0;
}
}
#endif
#if SPI_ENABLED
SPI_TypeDef* getSPIFromDevice(IOEventFlags device) {
switch (device) {
case EV_SPI1 : return SPI1;
#if ESPR_SPI_COUNT>=2
case EV_SPI2 : return SPI2;
#endif
#if ESPR_SPI_COUNT>=3
case EV_SPI3 : return SPI3;
#endif
default: return 0;
}
}
#endif
#if I2C_ENABLED
I2C_TypeDef* getI2CFromDevice(IOEventFlags device) {
switch (device) {
case EV_I2C1 : return I2C1;
#if ESPR_I2C_COUNT>=2
case EV_I2C2 : return I2C2;
#endif
#if ESPR_I2C_COUNT>=3
case EV_I2C3 : return I2C3;
#endif
default: return 0;
}
}
#endif
unsigned int jshGetTimerFreq(TIM_TypeDef *TIMx) {
// TIM2-7, 12-14 on APB1, everything else is on APB2
RCC_ClocksTypeDef clocks;
RCC_GetClocksFreq(&clocks);
// This (oddly) looks the same on F1/2/3/4. It's probably not
bool APB1 = TIMx==TIM2 || TIMx==TIM3 || TIMx==TIM4 ||
#ifndef STM32F3
TIMx==TIM5 ||
#endif
#ifdef TIM6
TIMx==TIM6 ||
#endif
#ifdef TIM7
TIMx==TIM7 ||
#endif
#ifdef TIM12
TIMx==TIM12 ||
#endif
#ifdef TIM13
TIMx==TIM13 ||
#endif
#ifdef TIM14
TIMx==TIM14 ||
#endif
false;
unsigned int freq = APB1 ? clocks.PCLK1_Frequency : clocks.PCLK2_Frequency;
// If APB1 clock divisor is 1x, this is only 1x
if (clocks.HCLK_Frequency == freq)
return freq;
// else it's 2x (???)
return freq*2;
}
#ifdef SPI_ENABLED
static unsigned int jshGetSPIFreq(SPI_TypeDef *SPIx) {
RCC_ClocksTypeDef clocks;
RCC_GetClocksFreq(&clocks);
bool APB2 = SPIx == SPI1;
return APB2 ? clocks.PCLK2_Frequency : clocks.PCLK1_Frequency;
}
#endif
static ALWAYS_INLINE unsigned int getSystemTimerFreq() {
return SystemCoreClock;
}
// ----------------------------------------------------------------------------
volatile unsigned int ticksSinceStart = 0;
#ifdef USE_RTC
// Average time between SysTicks
volatile unsigned int expectedSysTickTime=0,averageSysTickTime=0, smoothAverageSysTickTime=0;
// last system time there was a systick
volatile JsSysTime lastSysTickTime=0, smoothLastSysTickTime=0;
// whether we have slept since the last SysTick
bool hasSystemSlept;
#else
volatile JsSysTime SysTickMajor = SYSTICK_RANGE;
#endif
static bool jshIsRTCUsingLSE() {
return (RCC->BDCR & (RCC_RTCCLKSource_LSE|RCC_RTCCLKSource_LSI)) == RCC_RTCCLKSource_LSE;
}
static bool jshIsRTCAlreadySetup(bool andRunning) {
if ((RCC->BDCR & RCC_BDCR_RTCEN) == 0)
return false; // RTC was off - return false
if (!andRunning) return true;
// Check what we're running the RTC off and make sure that it's running!
if (jshIsRTCUsingLSE())
return RCC_GetFlagStatus(RCC_FLAG_LSERDY) == SET;
else
return RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == SET;
}
void jshSetupRTCPrescalerValue(unsigned int prescale) {
#ifdef USE_RTC
jshRTCPrescaler = (unsigned short)prescale;
jshRTCPrescalerReciprocal = (unsigned short)((((unsigned int)JSSYSTIME_SECOND) << RTC_PRESCALER_RECIPROCAL_SHIFT) / jshRTCPrescaler);
#ifdef STM32F1
RTC_SetPrescaler(jshRTCPrescaler - 1U);
RTC_WaitForLastTask();
#else
RTC_InitTypeDef RTC_InitStructure;
RTC_StructInit(&RTC_InitStructure);
RTC_InitStructure.RTC_AsynchPrediv = 0;
RTC_InitStructure.RTC_SynchPrediv = (uint32_t)(jshRTCPrescaler-1);
RTC_InitStructure.RTC_HourFormat = RTC_HourFormat_24;
RTC_Init(&RTC_InitStructure);
#endif
RTC_WaitForSynchro();
#endif
}
#ifdef USE_RTC
static uint32_t jshGetDefaultSysTickTime() {
return (unsigned int)(((JsVarFloat)jshGetTimeForSecond() * (JsVarFloat)SYSTICK_RANGE) / (JsVarFloat)getSystemTimerFreq());
}
#endif
int jshGetRTCPrescalerValue(bool calibrate) {
#ifdef USE_RTC
if (calibrate)
return (int)((long long)averageSysTickTime * (long long)jshRTCPrescaler / (long long)jshGetDefaultSysTickTime());
return jshRTCPrescaler;
#else
return 0;
#endif
}
#ifdef USE_RTC
void jshSetupRTCPrescaler(bool isUsingLSI) {
if (isUsingLSI) {
#ifdef STM32F1
jshSetupRTCPrescalerValue(40000); // 40kHz for LSI on F1 parts
#else
jshSetupRTCPrescalerValue(32000); // 32kHz for LSI on F4
#endif
} else {
jshSetupRTCPrescalerValue(32768); // 32.768kHz for LSE
}
}
void jshSetupRTC(bool isUsingLSI) {
RCC_RTCCLKConfig(isUsingLSI ? RCC_RTCCLKSource_LSI : RCC_RTCCLKSource_LSE); // set clock source to low speed internal
RCC_RTCCLKCmd(ENABLE); // enable RTC (in backup domain)
RTC_WaitForSynchro();
jshSetupRTCPrescaler(isUsingLSI);
}
#endif
void jshResetRTCTimer() {
#ifdef USE_RTC
// work out initial values for RTC
expectedSysTickTime = jshGetDefaultSysTickTime();
averageSysTickTime = smoothAverageSysTickTime = expectedSysTickTime;
lastSysTickTime = smoothLastSysTickTime = jshGetRTCSystemTime();
#endif
}
void jshDoSysTick() {
/* Handle the delayed Ctrl-C -> interrupt behaviour (see description by EXEC_CTRL_C's definition) */
if (execInfo.execute & EXEC_CTRL_C_WAIT)
execInfo.execute = (execInfo.execute & ~EXEC_CTRL_C_WAIT) | EXEC_INTERRUPTED;
if (execInfo.execute & EXEC_CTRL_C)
execInfo.execute = (execInfo.execute & ~EXEC_CTRL_C) | EXEC_CTRL_C_WAIT;
if (ticksSinceStart!=0xFFFFFFFF)
ticksSinceStart++;
#ifdef USB // if USB was connected but we haven't been able to send any data
if (jshUSBReceiveLastActive < 255)
jshUSBReceiveLastActive++;
#endif
#ifdef USE_RTC
if (ticksSinceStart==RTC_INITIALISE_TICKS) {
// Use LSI if the LSE hasn't stabilised
bool isUsingLSI = RCC_GetFlagStatus(RCC_FLAG_LSERDY)==RESET;
bool wasUsingLSI = !jshIsRTCUsingLSE();
// If the RTC is already doing the right thing, do nothing
if (isUsingLSI != wasUsingLSI) {
// We just set the RTC up, so we have to reset the
// backup domain again to change sources :(
#ifdef STM32F1
uint32_t time = RTC_GetCounter();
#else
RTC_TimeTypeDef time;
RTC_DateTypeDef date;
RTC_GetTime(RTC_Format_BIN, &time);
RTC_GetDate(RTC_Format_BIN, &date);
#endif
RCC_BackupResetCmd(ENABLE);
RCC_BackupResetCmd(DISABLE);
if (!isUsingLSI) {
RCC_LSEConfig(RCC_LSE_ON); // reset would have turned LSE off
#ifndef STM32F1
while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET);
#endif
}
#ifndef STM32F1
RTC_WaitForSynchro();
#endif
jshSetupRTC(isUsingLSI);
#ifdef STM32F1
RTC_SetCounter(time);
#else
RTC_SetDate(RTC_Format_BIN, &date);
RTC_SetTime(RTC_Format_BIN, &time);
RTC_WaitForSynchro();
#endif
}
// Disable RTC clocks depending on what we decided...
if (isUsingLSI) {
// LSE is not working - turn it off and use LSI
RCC_LSEConfig(RCC_LSE_OFF); // disable low speed external oscillator
} else {
// LSE working! Yay! turn LSI off now
RCC_LSICmd(DISABLE); // disable low speed internal oscillator
}
}
JsSysTime time = jshGetRTCSystemTime();
if (!hasSystemSlept && ticksSinceStart>RTC_INITIALISE_TICKS) {
/* Ok - slightly crazy stuff here. So the normal jshGetSystemTime is now
* working off of the SysTick again to get the accuracy. But this means
* that we can't just change lastSysTickTime to the current time, because
* there will be an apparent glitch if SysTick happens when measuring the
* length of a pulse.
*/
smoothLastSysTickTime = smoothLastSysTickTime + smoothAverageSysTickTime; // we MUST advance this by what we assumed it was going to advance by last time!
// work out the 'real' average sysTickTime
JsSysTime diff = time - lastSysTickTime;
// saturate...
if (diff < 0) diff = 0;
if (diff > 0xFFFFFFFF) diff = 0xFFFFFFFF;
// and work out average without overflow (averageSysTickTime*3+diff)/4
averageSysTickTime = (averageSysTickTime>>1) +
(averageSysTickTime>>2) +
((unsigned int)diff>>2);
// what do we expect the RTC time to be on the next SysTick?
JsSysTime nextSysTickTime = time + (JsSysTime)averageSysTickTime;
// Now the smooth average is the average of what we had, and what we need to get back in line with the actual time
diff = nextSysTickTime - smoothLastSysTickTime;
// saturate...
if (diff < 0) diff = 0;
if (diff > 0xFFFFFFFF) diff = 0xFFFFFFFF;
smoothAverageSysTickTime = (smoothAverageSysTickTime>>1) +
(smoothAverageSysTickTime>>2) +
((unsigned int)diff>>2);
} else {
hasSystemSlept = false;
smoothLastSysTickTime = time;
smoothAverageSysTickTime = averageSysTickTime;
// and don't touch the real average
}
lastSysTickTime = time;
#else
SysTickMajor += SYSTICK_RANGE;
#endif
/* One second after start, call jsinteractive. This is used to swap
* to USB (if connected), or the Serial port. */
if (ticksSinceStart == SYSTICKS_FOR_ONE_SECOND) {
jsiOneSecondAfterStartup();
}
}
// ----------------------------------------------------------------------------
void jshInterruptOff() {
// jshPinSetValue(LED4_PININDEX,1);
__disable_irq();
}
void jshInterruptOn() {
__enable_irq();
// jshPinSetValue(LED4_PININDEX,0);
}
/// Are we currently in an interrupt?
bool jshIsInInterrupt() {
return (SCB->ICSR & SCB_ICSR_VECTACTIVE_Msk) != 0;
}
//int JSH_DELAY_OVERHEAD = 0;
int JSH_DELAY_MULTIPLIER = 1;
void jshDelayMicroseconds(int microsec) {
int iter = (int)(((long long)microsec * (long long)JSH_DELAY_MULTIPLIER) >> 10);
// iter -= JSH_DELAY_OVERHEAD;
if (iter<0) iter=0;
while (iter--) __NOP();
}
void jshPinSetState(Pin pin, JshPinState state) {
// if this is about to mess up the neopixel output, so reset our var so we know to re-init
if (pin == jshNeoPixelPin)
jshNeoPixelPin = PIN_UNDEFINED;
/* Make sure we kill software PWM if we set the pin state
* after we've started it */
if (BITFIELD_GET(jshPinSoftPWM, pin)) {
BITFIELD_SET(jshPinSoftPWM, pin, 0);
jstPinPWM(0,0,pin);
}
#ifdef STM32F1
BITFIELD_SET(jshPinOpendrainPullup, pin, state==JSHPINSTATE_GPIO_OUT_OPENDRAIN_PULLUP);
#endif
GPIO_InitTypeDef GPIO_InitStructure;
bool out = JSHPINSTATE_IS_OUTPUT(state);
bool af = JSHPINSTATE_IS_AF(state);
bool pullup = JSHPINSTATE_IS_PULLUP(state);
bool pulldown = JSHPINSTATE_IS_PULLDOWN(state);
bool opendrain = JSHPINSTATE_IS_OPENDRAIN(state);
if (out) {
#ifdef STM32API2
if (af) GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
else if (state==JSHPINSTATE_DAC_OUT) GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
else GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = opendrain ? GPIO_OType_OD : GPIO_OType_PP;
#else
if (af) GPIO_InitStructure.GPIO_Mode = opendrain ? GPIO_Mode_AF_OD : GPIO_Mode_AF_PP;
else GPIO_InitStructure.GPIO_Mode = opendrain ? GPIO_Mode_Out_OD : GPIO_Mode_Out_PP;
#endif
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
} else {
#ifdef STM32API2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
if (af) GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
#else
GPIO_InitStructure.GPIO_Mode = pulldown ? GPIO_Mode_IPD : (pullup ? GPIO_Mode_IPU : GPIO_Mode_IN_FLOATING);
#endif
if (state==JSHPINSTATE_ADC_IN) GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
}
#ifdef STM32API2
GPIO_InitStructure.GPIO_PuPd = pulldown ? GPIO_PuPd_DOWN : (pullup ? GPIO_PuPd_UP : GPIO_PuPd_NOPULL);
#endif
GPIO_InitStructure.GPIO_Pin = stmPin(pin);
GPIO_Init(stmPort(pin), &GPIO_InitStructure);
}
JshPinState jshPinGetState(Pin pin) {
GPIO_TypeDef* port = stmPort(pin);
uint16_t pinn = stmPin(pin);
int pinNumber = pinInfo[pin].pin;
bool isOn = (port->ODR&pinn) != 0;
#ifdef STM32F1
unsigned int crBits = ((pinNumber < 8) ? (port->CRL>>(pinNumber*4)) : (port->CRH>>((pinNumber-8)*4))) & 15;
unsigned int mode = crBits &3;
unsigned int cnf = (crBits>>2)&3;
if (mode==0) { // input
if (cnf==0) return JSHPINSTATE_ADC_IN;
else if (cnf==1) return JSHPINSTATE_GPIO_IN;
else /* cnf==2, 3=reserved */
return isOn ? JSHPINSTATE_GPIO_IN_PULLUP : JSHPINSTATE_GPIO_IN_PULLDOWN;
} else { // output
if (cnf&2) // af