jshardware.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * This file is part of Espruino, a JavaScript interpreter for Microcontrollers
  3. *
  4. * Copyright (C) 2013 Gordon Williams <gw@pur3.co.uk>
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. *
  10. * ----------------------------------------------------------------------------
  11. * Platform Specific part of Hardware interface Layer
  12. * ----------------------------------------------------------------------------
  13. */
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <stdio.h>
  17. #include <unistd.h>
  18. #include <sys/time.h>
  19. #include <signal.h>
  20. #include <inttypes.h>
  21. #include "jshardware.h"
  22. #include "jsutils.h"
  23. #include "jsparse.h"
  24. #include "jsinteractive.h"
  25. #include "jspininfo.h"
  26. #include <rtthread.h>
  27. // ----------------------------------------------------------------------------
  28. // for non-blocking IO
  29. void reset_terminal_mode()
  30. {
  31. }
  32. void set_conio_terminal_mode()
  33. {
  34. }
  35. struct
  36. {
  37. struct rt_semaphore sem;
  38. rt_device_t device;
  39. } dev4js;
  40. rt_err_t es_rx_ind(rt_device_t dev, rt_size_t size)
  41. {
  42. rt_sem_release(&dev4js.sem);
  43. return RT_EOK;
  44. }
  45. static int kbhit(void)
  46. {
  47. while (rt_sem_take(&dev4js.sem, RT_WAITING_FOREVER) != RT_EOK);
  48. return 1;
  49. }
  50. static char getch()
  51. {
  52. unsigned char c;
  53. if (rt_device_read(dev4js.device, 0, &c, 1) == 1) {
  54. if (c=='\3') exit(0); // ctrl-c
  55. return c;
  56. }
  57. }
  58. void jshInit() {
  59. rt_sem_init(&dev4js.sem, "jssem", 0, RT_IPC_FLAG_PRIO);
  60. }
  61. void jshKill() {
  62. }
  63. void jshIdle() {
  64. //while (kbhit()) {
  65. // jshPushIOCharEvent(EV_USBSERIAL, (char)getch());
  66. //}
  67. }
  68. // ----------------------------------------------------------------------------
  69. int jshGetSerialNumber(unsigned char *data, int maxChars) {
  70. long initialSerial = 0;
  71. long long serial = 0xDEADDEADDEADDEADL;
  72. memcpy(&data[0], &initialSerial, 4);
  73. memcpy(&data[4], &serial, 8);
  74. return 12;
  75. }
  76. unsigned int jshGetRegistrationCode() {
  77. unsigned int code = 0xFFFFFFFF;
  78. return code;
  79. }
  80. void jshSetRegistrationCode(unsigned int code) {
  81. }
  82. // ----------------------------------------------------------------------------
  83. void jshInterruptOff() {
  84. }
  85. void jshInterruptOn() {
  86. }
  87. void jshDelayMicroseconds(int microsec) {
  88. int ms = (microsec + 999) / 1000;
  89. rt_thread_sleep(rt_tick_from_millisecond(ms)); // don't sleep much if we have watches - we need to keep polling them
  90. }
  91. bool jshGetPinStateIsManual(Pin pin) {
  92. return false;
  93. }
  94. void jshSetPinStateIsManual(Pin pin, bool manual) {
  95. }
  96. void jshPinSetState(Pin pin, JshPinState state) {
  97. }
  98. JshPinState jshPinGetState(Pin pin) {
  99. return JSHPINSTATE_UNDEFINED;
  100. }
  101. void jshPinSetValue(Pin pin, bool value) {
  102. }
  103. bool jshPinGetValue(Pin pin) {
  104. return false;
  105. }
  106. bool jshIsDeviceInitialised(IOEventFlags device) { return true; }
  107. bool jshIsUSBSERIALConnected() {
  108. return false;
  109. }
  110. JsSysTime jshGetTimeFromMilliseconds(JsVarFloat ms) {
  111. return (JsSysTime)(ms*1000);
  112. }
  113. JsVarFloat jshGetMillisecondsFromTime(JsSysTime time) {
  114. return ((JsVarFloat)time)/1000;
  115. }
  116. JsSysTime jshGetSystemTime() {
  117. //struct timeval tm;
  118. //gettimeofday(&tm, 0);
  119. //return tm.tv_sec*1000000L + tm.tv_usec;
  120. return rt_tick_get() * (1000 / RT_TICK_PER_SECOND) * 1000;
  121. }
  122. // ----------------------------------------------------------------------------
  123. bool jshPinInput(Pin pin) {
  124. bool value = false;
  125. jsError("Invalid pin!");
  126. return value;
  127. }
  128. JsVarFloat jshPinAnalog(Pin pin) {
  129. JsVarFloat value = 0;
  130. jsError("Analog is not supported on this device.");
  131. return value;
  132. }
  133. void jshPinOutput(Pin pin, bool value) {
  134. }
  135. bool jshPinOutputAtTime(JsSysTime time, Pin pin, bool value) {
  136. // FIXME
  137. }
  138. void jshPinAnalogOutput(Pin pin, JsVarFloat value, JsVarFloat freq) { // if freq<=0, the default is used
  139. }
  140. void jshPinPulse(Pin pin, bool value, JsVarFloat time) {
  141. }
  142. void jshPinWatch(Pin pin, bool shouldWatch) {
  143. }
  144. bool jshGetWatchedPinState(IOEventFlags device) {
  145. return false;
  146. }
  147. bool jshIsEventForPin(IOEvent *event, Pin pin) {
  148. return false;
  149. }
  150. void jshUSARTSetup(IOEventFlags device, JshUSARTInfo *inf) {
  151. }
  152. /** Kick a device into action (if required). For instance we may need
  153. * to set up interrupts */
  154. void jshUSARTKick(IOEventFlags device) {
  155. }
  156. void jshSPISetup(IOEventFlags device, JshSPIInfo *inf) {
  157. }
  158. /** Send data through the given SPI device (if data>=0), and return the result
  159. * of the previous send (or -1). If data<0, no data is sent and the function
  160. * waits for data to be returned */
  161. int jshSPISend(IOEventFlags device, int data) {
  162. }
  163. /** Send 16 bit data through the given SPI device. */
  164. void jshSPISend16(IOEventFlags device, int data) {
  165. }
  166. /** Set whether to send 16 bits or 8 over SPI */
  167. void jshSPISet16(IOEventFlags device, bool is16) {
  168. }
  169. void jshI2CSetup(IOEventFlags device, JshI2CInfo *inf) {
  170. }
  171. void jshI2CWrite(IOEventFlags device, unsigned char address, int nBytes, const unsigned char *data) {
  172. }
  173. void jshI2CRead(IOEventFlags device, unsigned char address, int nBytes, unsigned char *data) {
  174. }
  175. void jshSaveToFlash() {
  176. }
  177. void jshLoadFromFlash() {
  178. }
  179. bool jshFlashContainsCode() {
  180. }
  181. /// Enter simple sleep mode (can be woken up by interrupts). Returns true on success
  182. bool jshSleep(JsSysTime timeUntilWake) {
  183. bool hasWatches = false;
  184. int ms = (hasWatches ? 1000 : (10*1000)) / 1000;
  185. rt_thread_sleep(rt_tick_from_millisecond(ms)); // don't sleep much if we have watches - we need to keep polling them
  186. return true;
  187. }