KurtE
Senior Member+
As I have been testing several Serial port related issues associated with the new beta board which now adds Serial4 and Serial5. Right now I am needing to update some of my own code/libraries to support them, and I was wondering if it makes sense to add some of the support for Half duplex directly into the HardwareSerial code.
Example Setup code for my Bioloid (Robotis AX12) support.
Current code also supports Serial3... Now to add 4/5. More question for other thread, but figure out what #ifdef to use to know if I have Serial4 and Serial5...
But it would be great if instead, I could use a different, MODE on Serial1.begin(1000000, SERIAL_8N1_HALFDUPLEX);
And it would handle the changing of the UARTS c1 register and currently I then enable PU on the ports TX register. Could use bit 6 for this.
Currently my own code is not as complete as it should be as I don't currently have any support for handling the non default TX pin for the UART, and there are no calls to query what the TX pin currently is...
Also when I am doing the half duplex, I need to set/clear the UART_C3_TXDIR to set the TX pin as input or output. Current code to setRX looks like:
I can probably clean this up in my own code to hold onto pointer of the UARTx_C3 register, but would be nice if I could add a method to do this internal to serial class. Something like: setHalfDuplexDirection(bool output=true);
If this makes sense, I would obviously be willing to do the work and issue Pull request.
Thoughts?
Kurt
Example Setup code for my Bioloid (Robotis AX12) support.
Code:
void ax12Init(long baud, Stream* pstream, int direction_pin ){
// Need to enable the PU resistor on the TX pin
s_paxStream = pstream;
s_direction_pin = direction_pin; // save away.
// Lets do some init here
if (s_paxStream == &Serial) {
Serial.begin(baud);
}
if (s_paxStream == (Stream*)&Serial1) {
Serial1.begin(baud);
#if defined(KINETISK) || defined(__MKL26Z64__)
if (s_direction_pin == -1) {
UART0_C1 |= UART_C1_LOOPS | UART_C1_RSRC;
CORE_PIN1_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3) | PORT_PCR_PE | PORT_PCR_PS; // pullup on output pin;
} else {
Serial1.transmitterEnable(s_direction_pin);
}
#elif defined(TEENSYDUINO)
// Handle on Teensy2...
if (s_direction_pin != -1) {
Serial1.transmitterEnable(s_direction_pin);
}
#endif
}
#ifdef SERIAL_PORT_HARDWARE1
if (s_paxStream == &Serial2) {
Serial2.begin(baud);
#if defined(KINETISK) || defined(__MKL26Z64__)
if (s_direction_pin == -1) {
UART1_C1 |= UART_C1_LOOPS | UART_C1_RSRC;
CORE_PIN10_CONFIG = PORT_PCR_DSE | PORT_PCR_SRE | PORT_PCR_MUX(3) | PORT_PCR_PE | PORT_PCR_PS; // pullup on output pin;
} else {
Serial2.transmitterEnable(s_direction_pin);
}
#endif
}
#endif
...
But it would be great if instead, I could use a different, MODE on Serial1.begin(1000000, SERIAL_8N1_HALFDUPLEX);
And it would handle the changing of the UARTS c1 register and currently I then enable PU on the ports TX register. Could use bit 6 for this.
Currently my own code is not as complete as it should be as I don't currently have any support for handling the non default TX pin for the UART, and there are no calls to query what the TX pin currently is...
Also when I am doing the half duplex, I need to set/clear the UART_C3_TXDIR to set the TX pin as input or output. Current code to setRX looks like:
Code:
#if defined(KINETISK) || defined(__MKL26Z64__)
// Teensy 3.1
if (s_paxStream == (Stream*)&Serial1) {
UART0_C3 &= ~UART_C3_TXDIR;
}
if (s_paxStream == (Stream*)&Serial2) {
UART1_C3 &= ~UART_C3_TXDIR;
}
if (s_paxStream == (Stream*)&Serial3) {
UART2_C3 &= ~UART_C3_TXDIR;
}
If this makes sense, I would obviously be willing to do the work and issue Pull request.
Thoughts?
Kurt