| 207 | }; |
| 208 | |
| 209 | void xs_serial_constructor(xsMachine *the) |
| 210 | { |
| 211 | modSerial serial = NULL; |
| 212 | uint32_t baud; |
| 213 | const struct modZephyrSerial *port = NULL; |
| 214 | uint8_t format; |
| 215 | |
| 216 | xsmcVars(1); |
| 217 | |
| 218 | xsmcGet(xsVar(0), xsArg(0), xsID_port); |
| 219 | port = modZephyrGetSerial(xsmcToString(xsVar(0))); |
| 220 | if (!port) |
| 221 | xsUnknownError("invalid port"); |
| 222 | if (!device_is_ready(port->device)) |
| 223 | xsUnknownError("uart not ready"); |
| 224 | |
| 225 | xsmcGet(xsVar(0), xsArg(0), xsID_baud); |
| 226 | baud = xsmcToInteger(xsVar(0)); |
| 227 | if ((baud <= 0) || (baud > 20000000)) |
| 228 | xsRangeError("invalid baud"); |
| 229 | |
| 230 | builtinInitializeTarget(the); |
| 231 | |
| 232 | format = builtinInitializeFormat(the, kIOFormatBuffer); |
| 233 | if ((kIOFormatBuffer != format) && (kIOFormatNumber != format)) |
| 234 | xsUnknownError("unsupported format"); |
| 235 | |
| 236 | struct uart_config uart_cfg = { |
| 237 | .baudrate = baud, |
| 238 | .parity = UART_CFG_PARITY_NONE, |
| 239 | .stop_bits = UART_CFG_STOP_BITS_1, |
| 240 | .data_bits = UART_CFG_DATA_BITS_8, |
| 241 | .flow_ctrl = UART_CFG_FLOW_CTRL_NONE, |
| 242 | }; |
| 243 | |
| 244 | if (uart_configure(port->device, &uart_cfg) < 0) |
| 245 | xsUnknownError("UART configure failed"); |
| 246 | |
| 247 | serial = c_calloc(1, sizeof(modSerialRecord)); |
| 248 | if (!serial) |
| 249 | xsUnknownError("no memory"); |
| 250 | |
| 251 | xsmcSetHostData(xsThis, serial); |
| 252 | xsSetHostHooks(xsThis, &xsSerialHooks); |
| 253 | |
| 254 | serial->the = the; |
| 255 | serial->obj = xsThis; |
| 256 | serial->device = port->device; |
| 257 | serial->format = format; |
| 258 | serial->useCount = 1; |
| 259 | |
| 260 | ring_buf_init(&serial->rxRingBuffer, MODDEF_SERIAL_RX_BUFSIZE, serial->rxBuffer); |
| 261 | ring_buf_init(&serial->txRingBuffer, MODDEF_SERIAL_TX_BUFSIZE, serial->txBuffer); |
| 262 | |
| 263 | uart_irq_callback_user_data_set(serial->device, serial_uart_isr, serial); |
| 264 | |
| 265 | uart_irq_rx_enable(serial->device); |
| 266 |
nothing calls this directly
no test coverage detected