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

Function xs_serial_read

modules/io/serial/pico/serial.c:272–316  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

270}
271
272void xs_serial_read(xsMachine *the)
273{
274 Serial serial = xsmcGetHostDataValidate(xsThis, (void *)&xsSerialHooks);
275 int available = fifo_length(&serial->rx_fifo);
276
277 if (kIOFormatNumber == serial->format) {
278 uint8_t byte;
279
280 if (0 != fifo_get(&serial->rx_fifo, &byte))
281 xsUnknownError("no data");
282
283 xsmcSetInteger(xsResult, byte);
284 }
285 else {
286 uint8_t *buffer;
287 int requested;
288 xsUnsignedValue byteLength;
289 uint8_t allocate = 1;
290
291 if (0 == xsmcArgc)
292 requested = available;
293 else if (xsReferenceType == xsmcTypeOf(xsArg(0))) {
294 xsResult = xsArg(0);
295 xsmcGetBufferWritable(xsResult, (void **)&buffer, &byteLength);
296 requested = (int)byteLength;
297 allocate = 0;
298 }
299 else
300 requested = xsmcToInteger(xsArg(0));
301
302 if ((requested <= 0) || (requested > available))
303 xsUnknownError("invalid");
304
305 if (allocate)
306 buffer = xsmcSetArrayBuffer(xsResult, NULL, requested);
307
308 while (requested--) {
309 if (0 != fifo_get(&serial->rx_fifo, buffer++))
310 xsUnknownError("no data");
311 }
312 }
313
314 serial->rxInterruptEnabled = (NULL != serial->onReadable);
315 uart_irq();
316}
317
318void xs_serial_write(xsMachine *the)
319{

Callers

nothing calls this directly

Calls 3

fifo_lengthFunction · 0.85
fifo_getFunction · 0.85
uart_irqFunction · 0.85

Tested by

no test coverage detected