MCPcopy Create free account
hub / github.com/Moddable-OpenSource/moddable / xs_serial_constructor

Function xs_serial_constructor

modules/io/serial/esp/serial.c:75–176  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

73};
74
75void xs_serial_constructor(xsMachine *the)
76{
77 Serial serial;
78 int baud;
79 uint8_t format;
80 xsSlot *onReadable, *onWritable;
81
82 if (!builtinIsPinFree(kTXPin) || !builtinIsPinFree(kRXPin))
83 xsUnknownError("in use");
84
85 xsmcVars(1);
86
87 xsmcGet(xsVar(0), xsArg(0), xsID_baud);
88 baud = xsmcToInteger(xsVar(0));
89 if ((baud < 0) || (baud > 20000000))
90 xsRangeError("invalid baud");
91
92 onReadable = builtinGetCallback(the, xsID_onReadable);
93 onWritable = builtinGetCallback(the, xsID_onWritable);
94
95 builtinInitializeTarget(the);
96
97 format = builtinInitializeFormat(the, kIOFormatBuffer);
98 if ((kIOFormatNumber != format) && (kIOFormatBuffer != format))
99 xsRangeError("invalid format");
100
101 serial = c_malloc((onReadable || onWritable) ? sizeof(SerialRecord) : offsetof(SerialRecord, the));
102 if (!serial)
103 xsRangeError("no memory");
104
105 xsmcSetHostData(xsThis, serial);
106
107 serial->obj = xsThis;
108 xsRemember(serial->obj);
109 xsSetHostHooks(xsThis, (xsHostHooks *)&xsSerialHooks);
110
111 serial->format = format;
112 serial->hasReadable = onReadable ? 1 : 0;
113 serial->hasWritable = onWritable ? 1 : 0;
114 if (onReadable || onWritable) {
115 serial->the = the;
116
117 if (onReadable) {
118 serial->isReadable = 0;
119 serial->onReadable = onReadable;
120 }
121
122 if (onWritable) {
123 serial->isWritable = 0;
124 serial->onWritable = onWritable;
125 }
126 }
127
128 ETS_UART_INTR_DISABLE();
129
130// pinMode(3, SPECIAL);
131 GPC(kRXPin) = (GPC(kRXPin) & (0xF << GPCI)); // SOURCE(GPIO) | DRIVER(NORMAL) | INT_TYPE(UNCHANGED) | WAKEUP_ENABLE(DISABLED)
132 GPEC = (1 << kRXPin); // disable

Callers

nothing calls this directly

Calls 4

builtinGetCallbackFunction · 0.85
builtinInitializeTargetFunction · 0.85
builtinInitializeFormatFunction · 0.85

Tested by

no test coverage detected