forked from espruino/Espruino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjshardware.cpp
More file actions
279 lines (217 loc) · 6.73 KB
/
Copy pathjshardware.cpp
File metadata and controls
279 lines (217 loc) · 6.73 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
/*
* 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
* ----------------------------------------------------------------------------
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#define CPLUSPLUS
extern "C" {
#include "jshardware.h"
#include "jsutils.h"
#include "jsparse.h"
#include "jsinteractive.h"
}
#define MBED_PINS (32*5)
// --------------------------------------------------- MBED DEFS
#include "mbed.h"
Timer systemTime;
unsigned int systemTimeHigh;
bool systemTimeWasHigh;
serial_t mbedSerial[USART_COUNT];
gpio_t mbedPins[MBED_PINS];
extern "C" {
// ---------------------------------------------------
void mbedSerialIRQ(uint32_t id, SerialIrq event) {
IOEventFlags device = EV_SERIAL1; // TODO: device
if (event == RxIrq) {
if (serial_readable(&mbedSerial[id]))
jshPushIOCharEvent(device, (char)serial_getc(&mbedSerial[id]));
}
if (event == TxIrq) {
int c = jshGetCharToTransmit(device);
if (c >= 0) {
serial_putc(&mbedSerial[id], c);
} else
serial_irq_set(&mbedSerial[id], TxIrq, 0);
}
}
// ----------------------------------------------------------------------------
// for non-blocking IO
void jshInit() {
jshInitDevices();
systemTimeWasHigh = false;
systemTimeHigh = 0;
systemTime.start();
int i;
for (i=0;i<MBED_PINS;i++) {
gpio_init(&mbedPins[i], (PinName)(P0_0+i), PIN_INPUT);
}
for (i=0;i<USART_COUNT;i++) {
serial_init(&mbedSerial[i], USBTX, USBRX); // FIXME Pin
serial_irq_handler(&mbedSerial[i], &mbedSerialIRQ, i);
// serial_irq_set(&mbedSerial[i], RxIrq, 1); // FIXME Rx IRQ just crashes when called
}
}
void jshKill() {
}
void jshIdle() {
// hack in order to get console device set correctly
static bool inited = false;
if (!inited) {
inited = true;
jsiOneSecondAfterStartup();
}
/*static bool foo = false;
foo = !foo;
jshPinSetValue(LED1_PININDEX, foo);*/
while (serial_readable(&mbedSerial[0])>0)
jshPushIOCharEvent(EV_SERIAL1, serial_getc(&mbedSerial[0]));
}
// ----------------------------------------------------------------------------
int jshGetSerialNumber(unsigned char *data, int maxChars) {
const char *code = "HelloWorld12";
strncpy((char *)data, code, maxChars);
return strlen(code);
}
// ----------------------------------------------------------------------------
void jshInterruptOff() {
}
void jshInterruptOn() {
}
void jshDelayMicroseconds(int microsec) {
wait_us(microsec);
}
void jshPinSetState(Pin pin, JshPinState state) {
}
void jshPinSetValue(Pin pin, bool value) {
gpio_dir(&mbedPins[pin], PIN_OUTPUT);
gpio_write(&mbedPins[pin], value);
}
bool jshPinGetValue(Pin pin) {
gpio_dir(&mbedPins[pin], PIN_INPUT);
return gpio_read(&mbedPins[pin]);
}
bool jshIsPinValid(Pin pin) {
return pin>=0 && pin<MBED_PINS;
return false;
}
bool jshIsDeviceInitialised(IOEventFlags device) { return true; }
bool jshIsUSBSERIALConnected() {
return false;
}
JsSysTime jshGetTimeFromMilliseconds(JsVarFloat ms) {
return (JsSysTime)(ms*1000);
}
JsVarFloat jshGetMillisecondsFromTime(JsSysTime time) {
return ((JsVarFloat)time)/1000;
}
JsSysTime jshGetSystemTime() {
// Check for timer overflows
unsigned int t = systemTime.read_us();
/*bool high = t>>31;
if (high != systemTimeWasHigh) {
if (!high) systemTimeHigh++;
systemTimeWasHigh = high;
} */
// FIXME - time after 30 minutes!
return ((JsSysTime)t);// + ((JsSysTime)systemTimeHigh)<<32;
}
void jshSetSystemTime(JsSysTime time) {
}
// ----------------------------------------------------------------------------
JsVarFloat jshPinAnalog(Pin pin) {
JsVarFloat value = 0;
return value;
}
JshPinFunction jshPinAnalogOutput(Pin pin, JsVarFloat value, JsVarFloat freq, JshAnalogOutputFlags flags) { // if freq<=0, the default is used
return JSH_NOTHING;
}
void jshPinPulse(Pin pin, bool value, JsVarFloat time) {
}
IOEventFlags jshPinWatch(Pin pin, bool shouldWatch) {
return EV_NONE;
}
bool jshGetWatchedPinState(IOEventFlags device) {
return false;
}
bool jshIsEventForPin(IOEvent *event, Pin pin) {
return false;
}
void jshUSARTSetup(IOEventFlags device, JshUSARTInfo *inf) {
}
/** Kick a device into action (if required). For instance we may need
* to set up interrupts */
void jshUSARTKick(IOEventFlags device) {
int id = 0; // TODO: device
int c = jshGetCharToTransmit(device);
if (c >= 0) {
serial_irq_set(&mbedSerial[id], TxIrq, 1);
serial_putc(&mbedSerial[id], c);
}
}
void jshSPISetup(IOEventFlags device, JshSPIInfo *inf) {
}
/** Send data through the given SPI device (if data>=0), and return the result
* of the previous send (or -1). If data<0, no data is sent and the function
* waits for data to be returned */
int jshSPISend(IOEventFlags device, int data) {
}
/** Send 16 bit data through the given SPI device. */
void jshSPISend16(IOEventFlags device, int data) {
jshSPISend(device, data>>8);
jshSPISend(device, data&255);
}
/** Set whether to send 16 bits or 8 over SPI */
void jshSPISet16(IOEventFlags device, bool is16) {
}
/** Set whether to use the receive interrupt or not */
void jshSPISetReceive(IOEventFlags device, bool isReceive) {
}
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) {
}
/// 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() {
}
void jshUtilTimerReschedule(JsSysTime period) {
}
void jshUtilTimerStart(JsSysTime period) {
}
JsVarFloat jshReadTemperature() { return NAN; };
JsVarFloat jshReadVRef() { return NAN; };
unsigned int jshGetRandomNumber() { return rand(); }
bool jshFlashGetPage(uint32_t addr, uint32_t *startAddr, uint32_t *pageSize) {
return false;
}
JsVar *jshFlashGetFree() {
// not implemented, or no free pages.
return 0;
}
void jshFlashErasePage(uint32_t addr) {
}
void jshFlashRead(void *buf, uint32_t addr, uint32_t len) {
}
void jshFlashWrite(void *buf, uint32_t addr, uint32_t len) {
}
unsigned int jshSetSystemClock(JsVar *options) {
return 0;
}
// ----------------------------------------------------------------------------
} // extern C