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

Function xs_serial_constructor

modules/io/serial/esp32/serial.c:92–227  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

90#define kTransmitTreshold (10) // could be build-time or runtime configurable
91
92void xs_serial_constructor(xsMachine *the)
93{
94 Serial serial;
95 int baud;
96 uint8_t hasReadable, hasWritable, format;
97 xsSlot *onReadable, *onWritable;
98 esp_err_t err;
99 uart_config_t uartConfig = {0};
100 int uart = 0;
101 uint32_t transmit = UART_PIN_NO_CHANGE, receive = UART_PIN_NO_CHANGE;
102
103 xsmcVars(1);
104
105 if (xsmcGet(xsVar(0), xsArg(0), xsID_transmit)) {
106 transmit = builtinGetPin(the, &xsVar(0));
107 if (!builtinIsPinFree(transmit))
108 xsUnknownError("in use");
109 }
110
111 if (xsmcGet(xsVar(0), xsArg(0), xsID_receive)) {
112 receive = builtinGetPin(the, &xsVar(0));
113 if (!builtinIsPinFree(receive))
114 xsUnknownError("in use");
115 }
116 else if (UART_PIN_NO_CHANGE == transmit)
117 xsUnknownError("invalid");
118
119 if (xsmcGet(xsVar(0), xsArg(0), xsID_port)) {
120 uart = builtinGetSignedInteger(the, &xsVar(0));
121 if ((uart < 0) || (uart >= UART_NUM_MAX))
122 xsRangeError("invalid port");
123 }
124
125 xsmcGet(xsVar(0), xsArg(0), xsID_baud);
126 baud = xsmcToInteger(xsVar(0));
127 if ((baud <= 0) || (baud > 20000000))
128 xsRangeError("invalid baud");
129
130 onReadable = builtinGetCallback(the, xsID_onReadable);
131 onWritable = builtinGetCallback(the, xsID_onWritable);
132
133 hasReadable = (UART_PIN_NO_CHANGE != receive) && onReadable;
134 hasWritable = (UART_PIN_NO_CHANGE != transmit) && onWritable;
135
136 builtinInitializeTarget(the);
137
138 format = builtinInitializeFormat(the, kIOFormatBuffer);
139 if ((kIOFormatNumber != format) && (kIOFormatBuffer != format))
140 xsRangeError("invalid format");
141
142 if (uart_is_driver_installed(uart))
143 xsRangeError("port in use");
144
145 uartConfig.baud_rate = baud;
146 uartConfig.data_bits = UART_DATA_8_BITS;
147 uartConfig.parity = UART_PARITY_DISABLE;
148 uartConfig.stop_bits = UART_STOP_BITS_1;
149 uartConfig.flow_ctrl = UART_HW_FLOWCTRL_DISABLE;

Callers

nothing calls this directly

Calls 4

builtinGetSignedIntegerFunction · 0.85
builtinGetCallbackFunction · 0.85
builtinInitializeTargetFunction · 0.85
builtinInitializeFormatFunction · 0.85

Tested by

no test coverage detected