boards.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use strict";
  2. const log = require("./logger");
  3. const delay = require("./utilities").delay;
  4. const FLASH_MODES = {
  5. qio: 0,
  6. qout: 1,
  7. dio: 2,
  8. dout: 3
  9. };
  10. const FLASH_FREQUENCIES = {
  11. "40m": 0,
  12. "26m": 1,
  13. "20m": 2,
  14. "80m": 0xf
  15. };
  16. const FLASH_SIZES = {
  17. "4m": 0x00,
  18. "2m": 0x10,
  19. "8m": 0x20,
  20. "16m": 0x30,
  21. "32m": 0x40,
  22. "16m-c1": 0x50,
  23. "32m-c1": 0x60,
  24. "32m-c2": 0x70
  25. };
  26. class EspBoard {
  27. constructor(port) {
  28. this.port = port;
  29. }
  30. portSet(options) {
  31. return new Promise((resolve, reject) => {
  32. log.info("Setting port", options);
  33. this.port.set(options, (err, result) => {
  34. if (err) {
  35. reject(err);
  36. }
  37. resolve(result);
  38. });
  39. });
  40. }
  41. flashInfoAsBytes() {
  42. let buffer = new ArrayBuffer(2);
  43. let dv = new DataView(buffer);
  44. dv.setUint8(0, FLASH_MODES[this.flashMode]);
  45. dv.setUint8(1, FLASH_SIZES[this.flashSize] + FLASH_FREQUENCIES[this.flashFrequency]);
  46. return new Buffer(buffer);
  47. }
  48. resetIntoBootLoader() {
  49. throw new Error("Must define bootloader reset instructions");
  50. }
  51. }
  52. /**
  53. * Tested: Adafruit Feather Huzzah
  54. * Needs testing: Adafruit Huzzah, SparkFun Thing, SparkFun Thing Dev Board
  55. */
  56. class Esp12 extends EspBoard {
  57. constructor(port) {
  58. super(port);
  59. this.flashFrequency = "80m";
  60. this.flashMode = "qio";
  61. this.flashSize = "32m";
  62. }
  63. resetIntoBootLoader() {
  64. // RTS - Request To Send
  65. // DTR - Data Terminal Ready
  66. // NOTE: Must set values at the same time.
  67. log.info("Resetting board");
  68. return this.portSet({rts: true, dtr:false})
  69. .then(() => delay(5))
  70. .then(() => this.portSet({rts: false, dtr: true}))
  71. .then(() => delay(50))
  72. .then(() => this.portSet({rts: false, dtr: false}));
  73. }
  74. }
  75. module.exports = {
  76. Esp12: Esp12
  77. };