Skip to content

Commit 39c72ad

Browse files
authored
Update Serial.cpp to support 9-bit functionality on UART
Add two functions and the supporting 9N1 case logic to allow for turning on 9-bit support and sending commands. The second argument, bool, allows for turning on or off the 9th bit, also known as the wake bit. Serial1.begin(19200, SERIAL_9N1); Serial1.write_9bit(0x81, true); Serial1.write_9bit(&temp[1], false, len - 1);
1 parent 90e4f56 commit 39c72ad

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

cores/arduino/Serial.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,11 @@ void UART::begin(unsigned long baudrate, uint16_t config) {
260260
uart_cfg.parity = UART_PARITY_ODD;
261261
uart_cfg.stop_bits = UART_STOP_BITS_2;
262262
break;
263+
case SERIAL_9N1:
264+
uart_cfg.data_bits = UART_DATA_BITS_9;
265+
uart_cfg.parity = UART_PARITY_OFF;
266+
uart_cfg.stop_bits = UART_STOP_BITS_1;
267+
break;
263268
}
264269

265270
uart_cfg.p_callback = UART::WrapperCallback;
@@ -335,4 +340,28 @@ size_t UART::write_raw(uint8_t* c, size_t len) {
335340
i++;
336341
}
337342
return len;
338-
}
343+
}
344+
345+
/* -------------------------------------------------------------------------- */
346+
size_t UART::write_9bit(uint8_t c, bool wake) {
347+
/* -------------------------------------------------------------------------- */
348+
uint16_t bit = 0x00;
349+
if (wake) {bit = 0x100;}
350+
uart_ctrl.p_reg->TDRHL = 0xFC00 + bit + c;
351+
while (uart_ctrl.p_reg->SSR_b.TEND == 0) {}
352+
return 1;
353+
}
354+
355+
/* -------------------------------------------------------------------------- */
356+
size_t UART::write_9bit(uint8_t* c, bool wake, size_t len) {
357+
/* -------------------------------------------------------------------------- */
358+
size_t i = 0;
359+
uint16_t bit = 0x00;
360+
if (wake) {bit = 0x100;}
361+
while (i < len) {
362+
uart_ctrl.p_reg->TDRHL = *(c+i) + 0xFC00 + bit;
363+
while (uart_ctrl.p_reg->SSR_b.TEND == 0) {}
364+
i++;
365+
}
366+
return len;
367+
}

0 commit comments

Comments
 (0)