-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjshardware.h
392 lines (329 loc) · 11.8 KB
/
jshardware.h
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
/*
* 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/.
*
* ----------------------------------------------------------------------------
* Hardware interface Layer
* NOTE: The definitions of these functions are inside:
* targets/{target}/jshardware.c
* ----------------------------------------------------------------------------
*/
#ifndef JSHARDWARE_H_
#define JSHARDWARE_H_
#include "jsutils.h"
#include "jsvar.h"
#include "jsdevices.h"
#include "jspin.h"
#include "jstimer.h"
//for mbed library
#include "serial_api.h"
#if DEVICE_ANALOGIN
#include "analogin_api.h"
#endif
#if DEVICE_ANALOGOUT
#include "analogout_api.h"
#endif
#if DEVICE_PWMOUT
#include "pwmout_api.h"
#endif
#if DEVICE_INTERRUPTIN
#include "gpio_irq_api.h"
#endif
#if DEVICE_SPI
#include "spi_api.h"
#endif
//---------------------------------------------------------for struct ----------
typedef struct mbedGpio{
gpio_t gpio;
struct mbedGpio *next;
bool isStateManual;
}jsGpio;
typedef enum {
SDS_NONE,
SDS_XOFF_PENDING = 1,
SDS_XON_PENDING = 2,
SDS_XOFF_SENT = 4, // sending XON clears this
SDS_FLOW_CONTROL_XON_XOFF = 8, // flow control enabled
} PACKED_FLAGS JshSerialDeviceState;
typedef struct mbedSerial{
serial_t serial;
IOEventFlags flag; //use it to decide diferrent device
JshSerialDeviceState state;
struct mbedSerial *next;
}jsSerial;
#if DEVICE_ANALOGIN
typedef struct mbedAnalogIn{
analogin_t analogIn;
Pin pin;
struct mbedAnalogIn *next;
}jsAnalogIn;
#endif
#if DEVICE_ANALOGOUT
typedef struct mbedAnalogOut{
dac_t analogOut;
Pin pin;
struct mbedAnalogOut *next;
}jsAnalogOut;
#endif
#if DEVICE_PWMOUT
typedef struct mbedPwmout{
pwmout_t pwmout;
Pin pin;
struct mbedPwmout *next;
}jsPwmout;
#endif
#if DEVICE_INTERRUPTIN
// Functions that can be called in an IRQ when a pin changes state
typedef void(*JshEventCallbackCallback)(bool state);
typedef struct mbedInterruptIn{
gpio_irq_t gpioIrq;
Pin pin;
JshEventCallbackCallback callback;
struct mbedInterruptIn *next;
}jsInterruptIn;
#endif
#if DEVICE_SPI
typedef struct mbedSpi{
spi_t spi;
IOEventFlags flag; //use it to decide diferrent device
struct mbedSpi *next;
}jsSpi;
#endif
//----------------------
#define DEFAULT_CONSOLE_DEVICE EV_STDIOSERIAL
#ifndef ARM
#define ARM
#endif
void jshInit();
/// Is the pin state manual (has the user asked us explicitly to change it?)
bool jshGetPinStateIsManual(Pin pin);
/// Set whether the pin state is manual (has the user asked us explicitly to change it?)
void jshSetPinStateIsManual(Pin pin, bool manual);
void jshReset(); // When 'reset' is called - we try and put peripherals back to their power-on state
void jshIdle(); // stuff to do on idle
/// Get this IC's serial number. Passed max # of chars and a pointer to write to. Returns # of chars
int jshGetSerialNumber(unsigned char *data, int maxChars);
bool jshIsUSBSERIALConnected(); // is the serial device connected?
JshSerialDeviceState *getSerialDeviceStates(IOEventFlags device);
/// Get the system time (in ticks)
JsSysTime jshGetSystemTime();
/// Set the system time (in ticks) - this should only be called rarely as it could mess up things like jsinteractive's timers!
void jshSetSystemTime(JsSysTime time);
/// Convert a time in Milliseconds to one in ticks
JsSysTime jshGetTimeFromMilliseconds(JsVarFloat ms);
/// Convert ticks to a time in Milliseconds
JsVarFloat jshGetMillisecondsFromTime(JsSysTime time);
// software IO functions...
void jshInterruptOff();
void jshInterruptOn();
void jshDelayMicroseconds(int microsec);
void jshPinSetValue(Pin pin, bool value);
bool jshPinGetValue(Pin pin);
// ------
typedef enum {
JSHPINSTATE_UNDEFINED,
JSHPINSTATE_GPIO_OUT,
JSHPINSTATE_GPIO_OUT_OPENDRAIN,
JSHPINSTATE_GPIO_IN,
JSHPINSTATE_GPIO_IN_PULLUP,
JSHPINSTATE_GPIO_IN_PULLDOWN,
JSHPINSTATE_ADC_IN,
JSHPINSTATE_AF_OUT,
JSHPINSTATE_AF_OUT_OPENDRAIN,
JSHPINSTATE_USART_IN,
JSHPINSTATE_USART_OUT,
JSHPINSTATE_DAC_OUT,
JSHPINSTATE_I2C,
JSHPINSTATE_MASK = NEXT_POWER_2(JSHPINSTATE_I2C)-1,
JSHPINSTATE_PIN_IS_ON = JSHPINSTATE_MASK+1,
} PACKED_FLAGS JshPinState;
#define JSHPINSTATE_IS_OUTPUT(state) ( \
(state)==JSHPINSTATE_GPIO_OUT || \
(state)==JSHPINSTATE_GPIO_OUT_OPENDRAIN || \
(state)==JSHPINSTATE_AF_OUT || \
(state)==JSHPINSTATE_AF_OUT_OPENDRAIN || \
(state)==JSHPINSTATE_USART_OUT || \
(state)==JSHPINSTATE_DAC_OUT || \
(state)==JSHPINSTATE_I2C || \
0)
#define JSHPINSTATE_IS_OPENDRAIN(state) ( \
(state)==JSHPINSTATE_GPIO_OUT_OPENDRAIN || \
(state)==JSHPINSTATE_AF_OUT_OPENDRAIN || \
(state)==JSHPINSTATE_I2C || \
0)
/// Set a callback function to be called when an event occurs
void jshSetEventCallback(Pin pin, JshEventCallbackCallback callback);
bool callWatchPinEventCallback(Pin pin,bool state);
/// Set the pin state
void jshPinSetState(Pin pin, JshPinState state);
/** Get the pin state (only accurate for simple IO - won't return JSHPINSTATE_USART_OUT for instance).
* Note that you should use JSHPINSTATE_MASK as other flags may have been added */
JshPinState jshPinGetState(Pin pin);
// Returns an analog value between 0 and 1
JsVarFloat jshPinAnalog(Pin pin);
/// Returns a quickly-read analog value in the range 0-65535
int jshPinAnalogFast(Pin pin);
void jshPinAnalogOutput(Pin pin, JsVarFloat value, JsVarFloat freq); // if freq<=0, the default is used
void jshPinPulse(Pin pin, bool value, JsVarFloat time);
bool jshCanWatch(Pin pin); ///< Can the given pin be watched? it may not be possible because of conflicts
IOEventFlags jshPinWatch(Pin pin, bool shouldWatch); // start watching pin - return the EXTI associated with it
/// Given a Pin, return the current pin function associated with it
JshPinFunction jshGetCurrentPinFunction(Pin pin);
/// Given a pin function, set that pin to the 16 bit value (used mainly for DACs and PWM)
void jshSetOutputValue(JshPinFunction func, int value);
/// Enable watchdog with a timeout in seconds
void jshEnableWatchDog(JsVarFloat timeout);
/** Check the pin associated with this EXTI - return true if it is a 1 */
bool jshGetWatchedPinState(IOEventFlags device);
//bool jshIsEventForPin(Pin pin);
/** Is the given device initialised? */
bool jshIsDeviceInitialised(IOEventFlags device);
#define DEFAULT_BAUD_RATE 9600
#define DEFAULT_BYTESIZE 8
#define DEFAULT_PARITY 0
#define DEFAULT_STOPBITS 1
typedef struct {
int baudRate; // FIXME uint32_t ???
Pin pinRX;
Pin pinTX;
unsigned char bytesize;
unsigned char parity; // 0=none, 1=odd, 2=even
unsigned char stopbits;
bool xOnXOff; // XON XOFF flow control?
} PACKED_FLAGS JshUSARTInfo;
static inline void jshUSARTInitInfo(JshUSARTInfo *inf) {
inf->baudRate = DEFAULT_BAUD_RATE;
inf->pinRX = NC;
inf->pinTX = NC;
inf->bytesize = DEFAULT_BYTESIZE;
inf->parity = DEFAULT_PARITY; // PARITY_NONE = 0, PARITY_ODD = 1, PARITY_EVEN = 2 FIXME: enum?
inf->stopbits = DEFAULT_STOPBITS;
inf->xOnXOff = false;
}
void jshResetSerial();
/** Set up a UART, if pins are -1 they will be guessed */
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);
typedef enum {
SPIF_CPHA = 1,
SPIF_CPOL = 2,
SPIF_SPI_MODE_0 = 0,
SPIF_SPI_MODE_1 = SPIF_CPHA,
SPIF_SPI_MODE_2 = SPIF_CPOL,
SPIF_SPI_MODE_3 = SPIF_CPHA | SPIF_CPOL,
/* Mode CPOL CPHA
0 0 0
1 0 1
2 1 0
3 1 1
*/
} PACKED_FLAGS JshSPIFlags;
typedef enum {
SPIB_DEFAULT,
SPIB_MAXIMUM, // baudRate is the maximum we'll choose
SPIB_MINIMUM,// baudRate is the minimum we'll choose
} PACKED_FLAGS JshBaudFlags;
typedef struct {
int baudRate;
int bits;
Pin pinSCK;
Pin pinMISO;
Pin pinMOSI;
int spiMode;
//bool spiMSB; // MSB first?
} PACKED_FLAGS JshSPIInfo;
static inline void jshSPIInitInfo(JshSPIInfo *inf) {
inf->baudRate = 1000000;
inf->bits = 8;
inf->pinSCK = NC;
inf->pinMISO = NC;
inf->pinMOSI = NC;
inf->spiMode = 0;
//inf->spiMSB = true; // MSB first is default
}
/** Set up SPI, if pins are -1 they will be guessed */
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);
/** Set whether to send 16 bits or 8 over SPI */
void jshSPISet16(IOEventFlags device, bool is16);
/** Wait until SPI send is finished, and flush all received data */
void jshSPIWait(IOEventFlags device);
typedef struct {
Pin pinSCL;
Pin pinSDA;
char slaveAddr; // or -1 if it is master!
int bitrate;
// timeout?
} PACKED_FLAGS JshI2CInfo;
static inline void jshI2CInitInfo(JshI2CInfo *inf) {
inf->pinSCL = NC;
inf->pinSDA = NC;
inf->slaveAddr = (char)-1; // master
inf->bitrate = 50000; // Is what we used - shouldn't it be 100k?
}
/** Set up I2C, if pins are -1 they will be guessed */
void jshI2CSetup(IOEventFlags device, JshI2CInfo *inf);
/** Addresses are 7 bit - that is, between 0 and 0x7F. sendStop is whether to send a stop bit or not */
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);
/// Save contents of JsVars into Flash
void jshSaveToFlash();
/// Load contents of JsVars from Flash
void jshLoadFromFlash();
/// Returns true if flash contains something useful
bool jshFlashContainsCode();
/// Enter simple sleep mode (can be woken up by interrupts). Returns true on success
bool jshSleep(JsSysTime timeUntilWake);
/// Utility timer handling functions ------------------------------
/// Start the timer and get it to interrupt after 'period'
void jshUtilTimerStart(JsSysTime period);
/// Reschedult the timer (it should already be running) to interrupt after 'period'
void jshUtilTimerReschedule(JsSysTime period);
/// Stop the timer
void jshUtilTimerDisable();
// ---------------------------------------------- LOW LEVEL
#ifdef ARM
// ----------------------------------------------------------------------------
// SYSTICK
// On SYSTick interrupt, call this
void jshDoSysTick();
#ifdef USB
// Kick the USB SysTick watchdog - we need this to see if we have disconnected or not
void jshKickUSBWatchdog();
#endif
#endif // ARM
#ifdef STM32
// push a byte into SPI buffers
void jshSPIPush(IOEventFlags device, uint16_t data);
#endif
// the temperature from the internal temperature sensor
JsVarFloat jshReadTemperature();
// The voltage that a reading of 1 from `analogRead` actually represents
JsVarFloat jshReadVRef();
#ifdef STM32F3
#define SPI_I2S_SendData SPI_I2S_SendData16
#define SPI_I2S_ReceiveData SPI_I2S_ReceiveData16
#endif
#ifdef STM32F4
#define WAIT_UNTIL_N_CYCLES 10000000
#else
#define WAIT_UNTIL_N_CYCLES 2000000
#endif
#define WAIT_UNTIL(CONDITION, REASON) { \
int timeout = 10000000; \
while (!(CONDITION) && !jspIsInterrupted() && (timeout --)); \
if (timeout <= 0 || jspIsInterrupted()) { jsExceptionHere(JSET_INTERNALERROR, "Timeout on "REASON); } \
}
#endif /* JSHARDWARE_H_ */