summaryrefslogtreecommitdiff
path: root/components/external/espruino/targets/rtthread/jshardware.c
blob: 934040e5f013abd18b3ecca546a8455a4b7850fb (plain)
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
/*
 * 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>
 #include <unistd.h>
 #include <sys/time.h>
 #include <signal.h>
 #include <inttypes.h>

#include "jshardware.h"
#include "jsutils.h"
#include "jsparse.h"
#include "jsinteractive.h"
#include "jspininfo.h"

#include <rtthread.h>

// ----------------------------------------------------------------------------
// for non-blocking IO

void reset_terminal_mode()
{
}

void set_conio_terminal_mode()
{
}

struct
{
    struct rt_semaphore sem;
    rt_device_t device;
} dev4js;

rt_err_t es_rx_ind(rt_device_t dev, rt_size_t size)
{
    rt_sem_release(&dev4js.sem);

    return RT_EOK;
}

static int kbhit(void)
{
    while (rt_sem_take(&dev4js.sem, RT_WAITING_FOREVER) != RT_EOK);

    return 1;
}

static char getch()
{
    unsigned char c;

    if (rt_device_read(dev4js.device, 0, &c, 1) == 1) {
        if (c=='\3') exit(0); // ctrl-c
        return c;
    }
}

void jshInit() {
    rt_sem_init(&dev4js.sem, "jssem", 0, RT_IPC_FLAG_PRIO);
}

void jshKill() {
}

void jshIdle() {
  //while (kbhit()) {
  //  jshPushIOCharEvent(EV_USBSERIAL, (char)getch());
  //}
}

// ----------------------------------------------------------------------------

int jshGetSerialNumber(unsigned char *data, int maxChars) {
  long initialSerial = 0;
  long long serial = 0xDEADDEADDEADDEADL;

  memcpy(&data[0], &initialSerial, 4);
  memcpy(&data[4], &serial, 8);
  return 12;
}

unsigned int jshGetRegistrationCode() {
  unsigned int code = 0xFFFFFFFF;
  return code;
}

void jshSetRegistrationCode(unsigned int code) {
}

// ----------------------------------------------------------------------------

void jshInterruptOff() {
}

void jshInterruptOn() {
}

void jshDelayMicroseconds(int microsec) {
  int ms = (microsec + 999) / 1000;

  rt_thread_sleep(rt_tick_from_millisecond(ms)); // don't sleep much if we have watches - we need to keep polling them
}

bool jshGetPinStateIsManual(Pin pin) {
  return false;
}

void jshSetPinStateIsManual(Pin pin, bool manual) {
}

void jshPinSetState(Pin pin, JshPinState state) {
}

JshPinState jshPinGetState(Pin pin) {
  return JSHPINSTATE_UNDEFINED;
}

void jshPinSetValue(Pin pin, bool value) {
}

bool jshPinGetValue(Pin pin) {

  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() {
  //struct timeval tm;
  //gettimeofday(&tm, 0);
  //return tm.tv_sec*1000000L + tm.tv_usec;
  return rt_tick_get() * (1000 / RT_TICK_PER_SECOND) * 1000;
}

// ----------------------------------------------------------------------------

bool jshPinInput(Pin pin) {
  bool value = false;

  jsError("Invalid pin!");
  return value;
}

JsVarFloat jshPinAnalog(Pin pin) {
  JsVarFloat value = 0;
  jsError("Analog is not supported on this device.");
  return value;
}


void jshPinOutput(Pin pin, bool value) {
}

bool jshPinOutputAtTime(JsSysTime time, Pin pin, bool value) {
 // FIXME
}

void jshPinAnalogOutput(Pin pin, JsVarFloat value, JsVarFloat freq) { // if freq<=0, the default is used
}

void jshPinPulse(Pin pin, bool value, JsVarFloat time) {
}

void jshPinWatch(Pin pin, bool shouldWatch) {
}

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) {
}

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) {
}

void jshI2CSetup(IOEventFlags device, JshI2CInfo *inf) {
}

void jshI2CWrite(IOEventFlags device, unsigned char address, int nBytes, const unsigned char *data) {
}

void jshI2CRead(IOEventFlags device, unsigned char address, int nBytes, unsigned char *data) {
}


void jshSaveToFlash() {

}

void jshLoadFromFlash() {

}

bool jshFlashContainsCode() {

}

/// Enter simple sleep mode (can be woken up by interrupts). Returns true on success
bool jshSleep(JsSysTime timeUntilWake) {
  bool hasWatches = false;
  int ms = (hasWatches ? 1000 : (10*1000)) / 1000;

  rt_thread_sleep(rt_tick_from_millisecond(ms)); // don't sleep much if we have watches - we need to keep polling them
  return true;
}