-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjshardware.cpp
868 lines (707 loc) · 18.9 KB
/
jshardware.cpp
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
/*
* This file is part of Espruino, a JavaScript interpreter for Microcontrollers
*
* Copyright (C) 2013 Gordon Williams <[email protected]>
*
* 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
* ----------------------------------------------------------------------------
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
//#define CPLUSPLUS
#ifdef __cplusplus
extern "C" {
#endif
#include "jstimer.h"
#include "jshardware.h"
#include "jsutils.h"
#include "jsparse.h"
#include "jsinteractive.h"
//#include "serial_api.h"
#ifdef __cplusplus
}
#endif
// --------------------------------------------------- MBED DEFS
#include "mbed.h"
uint32_t lastSystemTime;
uint32_t nowTime;
uint64_t systemTime;
Ticker utilTicker;
jsGpio *mbedGpios;
#if DEVICE_SERIAL
jsSerial *mbedSerials;
#endif
#if DEVICE_ANALOGIN
jsAnalogIn *mbedAnalogIns;
#endif
#if DEVICE_ANALOGOUT
jsAnalogOut *mbedAnalogOuts;
#endif
#if DEVICE_PWMOUT
jsPwmout *mbedPwmouts;
#endif
#if DEVICE_INTERRUPTIN
jsInterruptIn *mbedInterruptIns;
#endif
#if DEVICE_SPI
jsSpi *mbedSpis;
#endif
static long long DEVICE_INITIALISED_FLAGS = 0L;
#ifdef __cplusplus
extern "C" {
#endif
//---------------mbed gpio
bool findGpioInMbedGpios(jsGpio **jsgpio,Pin pin){
*jsgpio = mbedGpios;
while(*jsgpio){
if((*jsgpio)->gpio.pin == pin) return true;
(*jsgpio) = (*jsgpio) ->next;
}
return false;
}
void addToMbedGpios(jsGpio *jsgpio){
jsgpio->next = mbedGpios;
mbedGpios = jsgpio;
}
bool jshGetPinStateIsManual(Pin pin) {
jsGpio *jsgpio;
if(findGpioInMbedGpios(&jsgpio,pin))
return jsgpio ->isStateManual;
return false;
}
void jshSetPinStateIsManual(Pin pin, bool manual) {
jsGpio *jsgpio;
if(findGpioInMbedGpios(&jsgpio,pin))
jsgpio ->isStateManual = manual;
}
void jshPinSetState(Pin pin, JshPinState state) {
bool out = JSHPINSTATE_IS_OUTPUT(state);
bool pullup = state==JSHPINSTATE_GPIO_OUT_OPENDRAIN || state==JSHPINSTATE_GPIO_IN_PULLUP || state==JSHPINSTATE_USART_IN;
bool pulldown = state==JSHPINSTATE_GPIO_IN_PULLDOWN;
bool opendrain = JSHPINSTATE_IS_OPENDRAIN(state);
jsGpio *jsgpio;
if(!findGpioInMbedGpios(&jsgpio,pin)){
jsgpio = (jsGpio*)malloc(sizeof(jsGpio));
if(!jsgpio) return ;
gpio_init(&jsgpio ->gpio, pin);
jsgpio->isStateManual = false;
addToMbedGpios(jsgpio);
}
if(out){
gpio_dir(&jsgpio ->gpio, PIN_OUTPUT);
#ifndef HASNOOPENDRAIN
if(opendrain) gpio_mode(&jsgpio ->gpio,OpenDrain);
#endif
}else {
gpio_dir(&jsgpio ->gpio, PIN_INPUT);
if(pullup){
gpio_mode(&jsgpio ->gpio,PullUp);
}else if(pulldown){
#ifndef HASNOPULLDOWN
gpio_mode(&jsgpio ->gpio,PullDown);
#endif
}else {
gpio_mode(&jsgpio ->gpio,PullDefault);
}
}
}
//------------------digitalOut/In
void jshPinSetValue(Pin pin, bool value) {
jsGpio *jsgpio;
if(findGpioInMbedGpios(&jsgpio,pin)){
gpio_write(&jsgpio ->gpio, value);
}
}
bool jshPinGetValue(Pin pin) {
jsGpio *jsgpio;
if(findGpioInMbedGpios(&jsgpio,pin))
return gpio_read(&jsgpio ->gpio);
return false;
}
//------------------device initialised or not
void jshSetDeviceInitialised(IOEventFlags device, bool isInit) {
long long mask = 1L << (int)device;
if (isInit) {
DEVICE_INITIALISED_FLAGS |= mask;
} else {
DEVICE_INITIALISED_FLAGS &= ~mask;
}
}
bool jshIsDeviceInitialised(IOEventFlags device) {
long long mask = 1L << (int)device;
return (DEVICE_INITIALISED_FLAGS & mask) != 0L;
}
// --------------------mbed serial
bool findSerialInMbedSerials(jsSerial **jsserial,IOEventFlags device){
#if DEVICE_SERIAL
*jsserial = mbedSerials;
while(*jsserial){
if((*jsserial)->flag == device) return true;
(*jsserial) = (*jsserial) ->next;
}
#endif
return false;
}
JshSerialDeviceState *getSerialDeviceStates(IOEventFlags device){
jsSerial *jsserial;
if(findSerialInMbedSerials(&jsserial,device)){
return &jsserial->state;
}
return 0;
}
void mbedSerialIRQ(uint32_t id, SerialIrq event) {
#if DEVICE_SERIAL
IOEventFlags device = (IOEventFlags)id;
jsSerial *jsserial;
if(findSerialInMbedSerials(&jsserial,device)){
if (event == RxIrq) {
if (serial_readable(&jsserial->serial)){
//jshPinOutput(LED1, !jshPinGetValue(LED1));
jshPushIOCharEvent(device, (char)serial_getc(&jsserial->serial));
}
}
if (event == TxIrq) {
int c = jshGetCharToTransmit(device);
if (c >= 0) {
serial_putc(&jsserial->serial, c);
} else
serial_irq_set(&jsserial->serial, TxIrq, 0);
}
}
#endif
}
void addToMbedSerials(jsSerial *jsserial){
#if DEVICE_SERIAL
jsserial->next = mbedSerials;
mbedSerials = jsserial;
#endif
}
void jshResetSerial() {
#if DEVICE_SERIAL
jsSerial *jsserial;
if(!jshIsDeviceInitialised(EV_STDIOSERIAL)){
jsserial = (jsSerial *)malloc(sizeof(jsSerial));
if(!jsserial) return ;
addToMbedSerials(jsserial);
serial_init(&jsserial->serial,USBTX, USBRX);
jsserial->state = SDS_NONE;
jsserial->flag = EV_STDIOSERIAL;
serial_irq_handler(&jsserial->serial, &mbedSerialIRQ, EV_STDIOSERIAL);
serial_irq_set(&jsserial->serial, RxIrq, 1);
jshSetDeviceInitialised(EV_STDIOSERIAL, true);
}
#endif
}
void jshUSARTSetup(IOEventFlags device, JshUSARTInfo *inf) {
#if DEVICE_SERIAL
if (!DEVICE_IS_USART(device)) return;
jsSerial *jsserial;
if(jshIsDeviceInitialised(device)){
findSerialInMbedSerials(&jsserial,device);
}else{
jshSetDeviceInitialised(device, true);
jsserial = (jsSerial *)malloc(sizeof(jsSerial));
if(!jsserial) return ;
addToMbedSerials(jsserial);
}
serial_init(&jsserial->serial, inf->pinTX,inf->pinRX);
jsserial->flag = device;
jsserial->state = SDS_NONE;
jshSetFlowControlEnabled(device, inf->xOnXOff);
serial_baud(&jsserial->serial,inf->baudRate);
serial_format(&jsserial->serial,inf->bytesize,(SerialParity)inf->parity,inf->stopbits);
serial_irq_handler(&jsserial->serial, &mbedSerialIRQ, (uint32_t)device);
serial_irq_set(&jsserial->serial, RxIrq, 1);
#endif
}
/** Kick a device into action (if required). For instance we may need
* to set up interrupts */
void jshUSARTKick(IOEventFlags device) {
#if DEVICE_SERIAL
jsSerial *jsserial;
findSerialInMbedSerials(&jsserial,device);
int c = jshGetCharToTransmit(device);
if (c >= 0) {
serial_irq_set(&jsserial->serial, TxIrq, 1);
serial_putc(&jsserial->serial, c);
}
#endif
}
//------------------AnalogOut/In
#if DEVICE_ANALOGIN
bool findAnalogInInMbedAnalogIns(jsAnalogIn **jsanalogIn,Pin pin){
*jsanalogIn = mbedAnalogIns;
while(*jsanalogIn){
if((*jsanalogIn)->pin == pin) return true;
(*jsanalogIn) = (*jsanalogIn) ->next;
}
return false;
}
void addToMbedAnalogIns(jsAnalogIn *jsanalogIn){
jsanalogIn->next = mbedAnalogIns;
mbedAnalogIns = jsanalogIn;
}
#endif
//todo : should change a name like jshPinAnalogRead()
JsVarFloat jshPinAnalog(Pin pin) {
#if DEVICE_ANALOGIN
if(pin == NC) return 0;
jshSetPinStateIsManual(pin, false);
jsAnalogIn *jsanalogIn;
if(!findAnalogInInMbedAnalogIns(&jsanalogIn,pin)){
jsanalogIn = (jsAnalogIn*)malloc(sizeof(jsAnalogIn));
if(!jsanalogIn) return 0;
analogin_init(&jsanalogIn->analogIn, pin);
jsanalogIn->pin = pin;
addToMbedAnalogIns(jsanalogIn);
}
return (double)analogin_read(&jsanalogIn->analogIn);
#endif
return 0;
}
int jshPinAnalogFast(Pin pin){
#if DEVICE_ANALOGIN
if(pin == NC) return 0;
jshSetPinStateIsManual(pin, false);
jsAnalogIn *jsanalogIn;
if(!findAnalogInInMbedAnalogIns(&jsanalogIn,pin)){
jsanalogIn = (jsAnalogIn*)malloc(sizeof(jsAnalogIn));
if(!jsanalogIn) return 0;
analogin_init(&jsanalogIn->analogIn, pin);
jsanalogIn->pin = pin;
addToMbedAnalogIns(jsanalogIn);
}
return analogin_read_u16(&jsanalogIn->analogIn);
#endif
return 0;
}
#if DEVICE_ANALOGOUT
bool findAnalogOutMbedAnalogOuts(jsAnalogOut **jsanalogOut,Pin pin){
*jsanalogOut = mbedAnalogOuts;
while(*jsanalogOut){
if((*jsanalogOut)->pin == pin) return true;
(*jsanalogOut) = (*jsanalogOut) ->next;
}
return false;
}
void addToMbedAnalogOuts(jsAnalogOut *jsanalogOut){
jsanalogOut->next = mbedAnalogOuts;
mbedAnalogOuts = jsanalogOut;
}
#endif
#if DEVICE_PWMOUT
bool findPwmoutMbedPwmouts(jsPwmout **jspwmout,Pin pin){
*jspwmout = mbedPwmouts;
while(*jspwmout){
if((*jspwmout)->pin == pin) return true;
(*jspwmout) = (*jspwmout) ->next;
}
return false;
}
void addToMbedPwmouts(jsPwmout *jspwmout){
jspwmout->next = mbedPwmouts;
mbedPwmouts = jspwmout;
}
#endif
void jshPinAnalogOutput(Pin pin, JsVarFloat value, JsVarFloat freq) {// if freq<=0, the default is used
if(value < 0) value = 0;
if(value > 1) value = 1;
if(pin == NC) return ;
if(freq <= 0){ //using DAC to deal with it
#if DEVICE_ANALOGOUT
jshSetPinStateIsManual(pin, false);
jsAnalogOut *jsanalogOut;
if(!findAnalogOutMbedAnalogOuts(&jsanalogOut,pin)){
jsanalogOut = (jsAnalogOut*)malloc(sizeof(jsAnalogOut));
if(!jsanalogOut) return ;
analogout_init(&jsanalogOut->analogOut, pin);
jsanalogOut->pin = pin;
addToMbedAnalogOuts(jsanalogOut);
}
analogout_write(&jsanalogOut->analogOut,(float)value);
#endif
}else { //using PWM to deal with it
#if DEVICE_PWMOUT
jshSetPinStateIsManual(pin, false);
jsPwmout *jspwmout;
if(!findPwmoutMbedPwmouts(&jspwmout,pin)){
jspwmout = (jsPwmout*)malloc(sizeof(jsPwmout));
if(!jspwmout) return ;
pwmout_init(&jspwmout->pwmout, pin);
jspwmout->pin = pin;
addToMbedPwmouts(jspwmout);
}
pwmout_period(&jspwmout->pwmout,(float)(1 / freq));
pwmout_write(&jspwmout->pwmout,(float)value);
#endif
}
}
//---------------InterruptIn
#if DEVICE_INTERRUPTIN
bool findGpioIrqInMbedGpioIrqs(jsInterruptIn **jsgpioIrq,Pin pin){
*jsgpioIrq = mbedInterruptIns;
while(*jsgpioIrq){
if((*jsgpioIrq)->pin == pin) return true;
(*jsgpioIrq) = (*jsgpioIrq) ->next;
}
return false;
}
void addToMbedGpioIrqs(jsInterruptIn *jsgpioIrq){
jsgpioIrq->next = mbedInterruptIns;
mbedInterruptIns = jsgpioIrq;
}
void pinIrqHandler(uint32_t id, gpio_irq_event event) {
jshPushIOWatchEvent(EV_EXTI,id);
}
#endif
/// Set a callback function to be called when an event occurs
void jshSetEventCallback(Pin pin, JshEventCallbackCallback callback) {
#if DEVICE_INTERRUPTIN
jsInterruptIn *jsgpioIrq;
if(findGpioIrqInMbedGpioIrqs(&jsgpioIrq,pin)){
jsgpioIrq->callback = callback;
}
#endif
}
bool callWatchPinEventCallback(Pin pin,bool state){
#if DEVICE_INTERRUPTIN
jsInterruptIn *jsgpioIrq;
if(findGpioIrqInMbedGpioIrqs(&jsgpioIrq,pin)){
if(jsgpioIrq->callback){
jsgpioIrq->callback(state);
return true;
}
}
#endif
return false;
}
IOEventFlags jshPinWatch(Pin pin, bool shouldWatch) {
#if DEVICE_INTERRUPTIN
if (jshIsPinValid(pin)) {
if(pin == NC) return EV_NONE;
//jsiConsolePrintf("pin = %d\n",pin);
jsInterruptIn *jsgpioIrq;
if(!findGpioIrqInMbedGpioIrqs(&jsgpioIrq,pin)){
//jsiConsolePrintf("111\n");
jsgpioIrq = (jsInterruptIn*)malloc(sizeof(jsInterruptIn));
if(!jsgpioIrq) return EV_NONE;
jsgpioIrq->pin = pin;
jsgpioIrq->callback = 0;
addToMbedGpioIrqs(jsgpioIrq);
}
if(shouldWatch){
if (!jshGetPinStateIsManual(pin)){
//jsiConsolePrintf("222\n");
jshPinSetState(pin,JSHPINSTATE_GPIO_IN_PULLUP);
}
}
if(shouldWatch){
gpio_irq_init(&jsgpioIrq->gpioIrq, pin, pinIrqHandler, pin);
gpio_irq_set(&jsgpioIrq->gpioIrq, IRQ_FALL, 1);
gpio_irq_set(&jsgpioIrq->gpioIrq, IRQ_RISE, 1);
}else{
gpio_irq_set(&jsgpioIrq->gpioIrq, IRQ_FALL, 0);
gpio_irq_set(&jsgpioIrq->gpioIrq, IRQ_RISE, 0);
}
return (IOEventFlags)(~(EV_EXTI + pin) + 1);
}else jsExceptionHere(JSET_ERROR, "Invalid pin!");
#endif
return EV_NONE;
}
//-----------------spi
bool findSpiInMbedSpis(jsSpi **jsspi,IOEventFlags device){
#if DEVICE_SPI
*jsspi = mbedSpis;
while(*jsspi){
if((*jsspi)->flag == device) return true;
(*jsspi) = (*jsspi) ->next;
}
#endif
return false;
}
void addToMbedSpis(jsSpi *jsspi){
#if DEVICE_SPI
jsspi->next = mbedSpis;
mbedSpis = jsspi;
#endif
}
void jshSPISetup(IOEventFlags device, JshSPIInfo *inf) {
#if DEVICE_SPI
jsSpi *jsspi;
if(jshIsDeviceInitialised(device)){
findSpiInMbedSpis(&jsspi,device);
}else{
jshSetDeviceInitialised(device, true);
jsspi = (jsSpi *)malloc(sizeof(jsSpi));
if(!jsspi) return ;
addToMbedSpis(jsspi);
}
spi_init(&jsspi->spi, inf->pinMOSI, inf->pinMISO, inf->pinSCK, NC);
spi_format(&jsspi->spi, inf->bits, inf->spiMode, 0);
spi_frequency(&jsspi->spi, inf->baudRate);
jsspi->flag = device;
#endif
}
int jshSPISend(IOEventFlags device, int data) {
#if DEVICE_SPI
if(data < 0) return -1;
if(jshIsDeviceInitialised(device)){
jsSpi *jsspi;
findSpiInMbedSpis(&jsspi,device);
return spi_master_write(&jsspi->spi, data);
}
return -1;
#endif
}
void jshSPIWait(IOEventFlags device){
}
void jshSPISet16(IOEventFlags device, bool is16){
}
void jshSPISend16(IOEventFlags device, int data){
}
// for non-blocking IO
void jshInit() {
//init some device vars
mbedGpios = 0;
#if DEVICE_SERIAL
mbedSerials = 0;
#endif
#if DEVICE_ANALOGIN
mbedAnalogIns = 0;
#endif
#if DEVICE_ANALOGOUT
mbedAnalogOuts = 0;
#endif
#if DEVICE_PWMOUT
mbedPwmouts = 0;
#endif
#if DEVICE_INTERRUPTIN
mbedInterruptIns = 0;
#endif
#if DEVICE_SPI
mbedSpis = 0;
#endif
//reset serial
jshResetSerial();
//init system time
lastSystemTime = us_ticker_read();
systemTime = 0;
}
void jshReset(){
//free and reinit some device vars
DEVICE_INITIALISED_FLAGS = 0;
if(mbedGpios){
jsGpio *jsgpio = mbedGpios;
while(mbedGpios){
mbedGpios = mbedGpios->next;
free(jsgpio);
jsgpio = mbedGpios;
}
mbedGpios = 0;
}
#if DEVICE_SERIAL
if(mbedSerials){
jsSerial *jsserial = mbedSerials;
while(mbedSerials){
mbedSerials = mbedSerials->next;
free(jsserial);
jsserial = mbedSerials;
}
mbedSerials = 0;
}
#endif
#if DEVICE_ANALOGIN
if(mbedAnalogIns){
jsAnalogIn *jsanalogIn = mbedAnalogIns;
while(mbedAnalogIns){
mbedAnalogIns = mbedAnalogIns->next;
free(jsanalogIn);
jsanalogIn = mbedAnalogIns;
}
mbedAnalogIns = 0;
}
#endif
#if DEVICE_ANALOGOUT
if(mbedAnalogOuts){
jsAnalogOut *jsanalogOut = mbedAnalogOuts;
while(mbedAnalogOuts){
mbedAnalogOuts = mbedAnalogOuts->next;
free(jsanalogOut);
jsanalogOut = mbedAnalogOuts;
}
mbedAnalogOuts = 0;
}
#endif
#if DEVICE_PWMOUT
if(mbedPwmouts){
jsPwmout *jspwmout = mbedPwmouts;
while(mbedPwmouts){
mbedPwmouts = mbedPwmouts->next;
free(jspwmout);
jspwmout = mbedPwmouts;
}
mbedPwmouts = 0;
}
#endif
#if DEVICE_INTERRUPTIN
if(mbedInterruptIns){
jsInterruptIn *jsinterruptIn = mbedInterruptIns;
while(mbedInterruptIns){
mbedInterruptIns = mbedInterruptIns->next;
free(jsinterruptIn);
jsinterruptIn = mbedInterruptIns;
}
mbedInterruptIns = 0;
}
#endif
//reset serial
jshResetSerial();
//init system time
lastSystemTime = us_ticker_read();
systemTime = 0;
}
void jshIdle() {
#ifdef USB
static bool wasUSBConnected = false;
bool USBConnected = jshIsUSBSERIALConnected();
if (wasUSBConnected != USBConnected) {
wasUSBConnected = USBConnected;
if (USBConnected)
jsiSetConsoleDevice(EV_USBSERIAL);
else {
if (jsiGetConsoleDevice() == EV_USBSERIAL)
jsiSetConsoleDevice(DEFAULT_CONSOLE_DEVICE);
jshTransmitClearDevice(EV_USBSERIAL); // clear the transmit queue
}
}
#else
jsiSetConsoleDevice(DEFAULT_CONSOLE_DEVICE);
#endif
/*while (serial_readable(&mbedSerial[0])>0)
jshPushIOCharEvent(EV_SERIAL1, serial_getc(&mbedSerial[0]));*/
}
// ----------------------------------------------------------------------------
//todo:the function shouldn't be implemented in mbed.
int jshGetSerialNumber(unsigned char *data, int maxChars) {
return 0;
}
// ----------------------------------------------------------------------------
void jshInterruptOff() {
__disable_irq();
}
void jshInterruptOn() {
__enable_irq();
}
void jshDelayMicroseconds(int microsec) {
wait_us(microsec);
}
bool jshIsUSBSERIALConnected() {
return false;
}
JsSysTime jshGetTimeFromMilliseconds(JsVarFloat ms) {
return (JsSysTime)(ms*1000);
}
JsVarFloat jshGetMillisecondsFromTime(JsSysTime time) {
return ((JsVarFloat)time)/1000;
}
JsSysTime jshGetSystemTime() {
int t ;
nowTime = us_ticker_read();
if(nowTime >= lastSystemTime) t = nowTime - lastSystemTime;
else t = (int)(0x100000000LL - lastSystemTime + nowTime) ;
systemTime += t;
lastSystemTime = nowTime;
return systemTime;
}
void jshSetSystemTime(JsSysTime time) {
systemTime = time;
lastSystemTime = time;
}
// ----------------------------------------------------------------------------
void jshPinPulse(Pin pin, bool pulsePolarity, JsVarFloat pulseTime) {
// ---- USE TIMER FOR PULSE
if (!jshIsPinValid(pin)) {
jsExceptionHere(JSET_ERROR, "Invalid pin!");
return;
}
if (pulseTime<=0) {
// just wait for everything to complete
jstUtilTimerWaitEmpty();
return;
} else {
// find out if we already had a timer scheduled
UtilTimerTask task;
if (!jstGetLastPinTimerTask(pin, &task)) {
// no timer - just start the pulse now!
jshPinOutput(pin, pulsePolarity);
task.time = jshGetSystemTime();
}
// Now set the end of the pulse to happen on a timer
//jsiConsolePrintf("out the at time : %f\n",pulseTime);
jstPinOutputAtTime(task.time + jshGetTimeFromMilliseconds(pulseTime), &pin, 1, !pulsePolarity);
}
}
//zzl
JshPinState jshPinGetState(Pin pin){
return (JshPinState)0;
}
JshPinFunction jshGetCurrentPinFunction(Pin pin){
return (JshPinFunction)0;
}
void jshSetOutputValue(JshPinFunction func, int value){
}
//zzl
void jshEnableWatchDog(JsVarFloat timeout){
}
void jshI2CSetup(IOEventFlags device, JshI2CInfo *inf) {
}
void jshI2CWrite(IOEventFlags device, unsigned char address, int nBytes, const unsigned char *data, bool sendStop) {
}
void jshI2CRead(IOEventFlags device, unsigned char address, int nBytes, unsigned char *data, bool sendStop) {
}
void jshSaveToFlash() {
}
//todo:fix me in mbed
void jshLoadFromFlash() {
/*
unsigned int dataSize = jsvGetMemoryTotal() * sizeof(JsVar);
jsiConsolePrintf("Loading from flash...\n");
JsVar *firstData = jsvLock(1);
uint32_t *basePtr = (uint32_t *)firstData;
jsvUnLock(firstData);
memcpy(basePtr, (int*)FLASH_SAVED_CODE_START, dataSize);
*/
}
//todo:fix me in mbed
bool jshFlashContainsCode() {
//return (*(int*)FLASH_MAGIC_LOCATION) == (int)FLASH_MAGIC;
return false;
}
/// Enter simple sleep mode (can be woken up by interrupts). Returns true on success
bool jshSleep(JsSysTime timeUntilWake) {
//__WFI(); // Wait for Interrupt
return true;
}
void jshUtilTimerDisable() {
utilTicker.detach();
}
void jshUtilTimerReschedule(JsSysTime period) {
//jsiConsolePrintf("restart:: %d\n",period);
utilTicker.attach_us(jstUtilTimerInterruptHandler,period);
}
void jshUtilTimerStart(JsSysTime period) {
//jsiConsolePrintf("start:: %d\n",period);
utilTicker.attach_us(jstUtilTimerInterruptHandler,period);
}
JsVarFloat jshReadTemperature() { return NAN; };
JsVarFloat jshReadVRef() { return NAN; };
// ----------------------------------------------------------------------------
#ifdef __cplusplus
}
#endif