Skip to contentSkip to conversation

Multidrop support

PATCH In review Rodolfo Giometti 3 participants 7 messages Sep 12, 2019 – Sep 13, 2019

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

Here are two patches to introduce multidrop support.

First one defines new termios bits useful to enable multidrop support, while the second one effectively implements multidrop for the Atmel/Microchip serial controllers.

Of course other serial drivers can do similar steps if the hardware they control can do similar operations.

Rodolfo

#1 lore raw

PATCH 1/2 tty: add bits to manage multidrop mode

From: Rodolfo Giometti <[email protected]>

Multidrop mode differentiates the data characters and the address characters. Data is transmitted with the parity bit to 0 and addresses are transmitted with the parity bit to 1. However this usually slow down communication by adding a delay between the first byte and the others.

This patch defines two non-stadard bits PARMD (that enables multidrop) and SENDA (that marks the next transmitted byte as address) that can be used to completely remove the delay during transmission by correctly managing the parity bit generation in hardware.

A simple example code about how to set up it is reported below:

    struct termios term;

    tcgetattr(fd, &term);

    /* Transmission: enable parity multidrop and mark 1st byte as address */
    term.c_cflag |= PARENB | CMSPAR | PARMD | SENDA;
    /* Reception: enable parity multidrop and parity check */
    term.c_iflag |= PARENB | PARMD | INPCK;

    tcsetattr(fd, TCSADRAIN, &term);

After that we can start 9 bits data transmission.

---
 include/linux/tty.h                 | 2 ++
 include/uapi/asm-generic/termbits.h | 2 ++
 2 files changed, 4 insertions(+)
include/linux/tty.h +2 -0
index bfa4e2ee94a9..66a25294f125 100644
@@ -168,6 +168,8 @@ struct tty_bufhead {
 #define C_CIBAUD(tty)	_C_FLAG((tty), CIBAUD)
 #define C_CRTSCTS(tty)	_C_FLAG((tty), CRTSCTS)
 #define C_CMSPAR(tty)	_C_FLAG((tty), CMSPAR)
+#define C_PARMD(tty)	_C_FLAG((tty), PARMD)
+#define C_SENDA(tty)	_C_FLAG((tty), SENDA)
 
 #define L_ISIG(tty)	_L_FLAG((tty), ISIG)
 #define L_ICANON(tty)	_L_FLAG((tty), ICANON)
include/uapi/asm-generic/termbits.h +2 -0
index 2fbaf9ae89dd..ead5eaebdd3b 100644
@@ -141,6 +141,8 @@ struct ktermios {
 #define HUPCL	0002000
 #define CLOCAL	0004000
 #define CBAUDEX 0010000
+#define PARMD   040000000
+#define SENDA   0100000000
 #define    BOTHER 0010000
 #define    B57600 0010001
 #define   B115200 0010002
Signature · 1 line
2.17.1
maintainer
#2 lore raw

Re: [PATCH 1/2] tty: add bits to manage multidrop mode

include/uapi/asm-generic/termbits.h struct ktermios { · line 148

Quoted 13 lines · Rodolfo Giometti
include/uapi/asm-generic/termbits.h +2 -0
index 2fbaf9ae89dd..ead5eaebdd3b 100644
@@ -141,6 +141,8 @@ struct ktermios {
 #define HUPCL  0002000
 #define CLOCAL 0004000
 #define CBAUDEX 0010000
+#define PARMD   040000000
+#define SENDA   0100000000
 #define    BOTHER 0010000
 #define    B57600 0010001
 #define   B115200 0010002

If you add these to the asm-generic version of this file, please also add them to
the architecture specific ones that don't use asm-generic/termbits.h:
alpha, ia64, mips, parisc, powerpc, sparc, and xtensa.

Please pick values that are the same everywhere and do not clash with any of the existing bits of any of the above (this is probably the case, but I have not checked)

Arnd

#3 lore raw

Re: [PATCH 1/2] tty: add bits to manage multidrop mode

In reply to Rodolfo Giometti

Hi Rodolfo,

You could also add these mailing lists:
- [email protected]
- [email protected]
- [email protected]

For reference, I've dug the conversation we had 2 years ago:
0/2 : https://marc.info/?t=149192176100002&r=1&w=2
1/2 : https://marc.info/?t=149192176300001&r=1&w=2
2/2 : https://marc.info/?t=149192176700001&r=1&w=2

And, like I said at that time, one problem I see with this implementation (setting a SENDA bit that automatically unsets itself) will break the way tcgetattr() is working:
https://marc.info/?l=linux-serial&m=149209522108027&w=2

Le 12/09/2019 à 10:40, Rodolfo Giometti a écrit :

Quoted 62 lines

From: Rodolfo Giometti <[email protected]>

Multidrop mode differentiates the data characters and the address characters. Data is transmitted with the parity bit to 0 and addresses are transmitted with the parity bit to 1. However this usually slow down communication by adding a delay between the first byte and the others.

This patch defines two non-stadard bits PARMD (that enables multidrop) and SENDA (that marks the next transmitted byte as address) that can be used to completely remove the delay during transmission by correctly managing the parity bit generation in hardware.

A simple example code about how to set up it is reported below:

    struct termios term;

    tcgetattr(fd, &term);

    /* Transmission: enable parity multidrop and mark 1st byte as address */
    term.c_cflag |= PARENB | CMSPAR | PARMD | SENDA;
    /* Reception: enable parity multidrop and parity check */
    term.c_iflag |= PARENB | PARMD | INPCK;

    tcsetattr(fd, TCSADRAIN, &term);

After that we can start 9 bits data transmission.

---
 include/linux/tty.h                 | 2 ++
 include/uapi/asm-generic/termbits.h | 2 ++
 2 files changed, 4 insertions(+)
include/linux/tty.h +2 -0
index bfa4e2ee94a9..66a25294f125 100644
@@ -168,6 +168,8 @@ struct tty_bufhead {
 #define C_CIBAUD(tty)	_C_FLAG((tty), CIBAUD)
 #define C_CRTSCTS(tty)	_C_FLAG((tty), CRTSCTS)
 #define C_CMSPAR(tty)	_C_FLAG((tty), CMSPAR)
+#define C_PARMD(tty)	_C_FLAG((tty), PARMD)
+#define C_SENDA(tty)	_C_FLAG((tty), SENDA)
 
 #define L_ISIG(tty)	_L_FLAG((tty), ISIG)
 #define L_ICANON(tty)	_L_FLAG((tty), ICANON)
include/uapi/asm-generic/termbits.h +2 -0
index 2fbaf9ae89dd..ead5eaebdd3b 100644
@@ -141,6 +141,8 @@ struct ktermios {
 #define HUPCL	0002000
 #define CLOCAL	0004000
 #define CBAUDEX 0010000
+#define PARMD   040000000
+#define SENDA   0100000000
 #define    BOTHER 0010000
 #define    B57600 0010001
 #define   B115200 0010002
#4 lore raw

Re: [PATCH 1/2] tty: add bits to manage multidrop mode

Quoted 6 lines · Richard Genoud

Hi Rodolfo,

You could also add these mailing lists:
- [email protected]
- [email protected]
- [email protected]

OK. No problem!

Quoted 4 lines

For reference, I've dug the conversation we had 2 years ago:
0/2 : https://marc.info/?t=149192176100002&r=1&w=2
1/2 : https://marc.info/?t=149192176300001&r=1&w=2
2/2 : https://marc.info/?t=149192176700001&r=1&w=2

Thanks.

Quoted 4 lines

And, like I said at that time, one problem I see with this implementation (setting a SENDA bit that automatically unsets itself) will break the way tcgetattr() is working:
https://marc.info/?l=linux-serial&m=149209522108027&w=2

I see... however the problem here is that this attribute is not sticky and it is reset by the controller after the first byte as been sent. Here is how Atmel/Microchip multidrop works (from the SAMA5D3's datasheet):

44.7.3.9 Multidrop Mode
If the value 0x6 or 0x07 is written to the PAR field in the US_MR, the USART runs in Multidrop Mode. This mode differentiates the data characters and the address characters. Data is transmitted with the parity bit to 0 and addresses are transmitted with the parity bit to 1.
...
The transmitter sends an address byte (parity bit set) when SENDA is written to in the US_CR. In this case, the next byte written to the US_THR is transmitted as an address. Any character written in the US_THR without having written the command SENDA is transmitted normally with the parity to 0.

So, if we have a 4 bytes message where the first one is the address byte we can use multidrop by setting the SENDA bit at the beginning and then by just invoking a write() with the four bytes message. The controller automatically will set the 9th bit to 1 for the first byte and then to 0 for the following three bytes.

To do so we need a syscall to tell to the controller when the address bit (SENDA) must be set, each time, before sending the message.

In this implementation I used the syscall within tcsetattr() function but if this is not right, then I have to use another one... maybe a custom ioctl? Or can you please suggest a suitable solution? :-)

Thanks for your help,

Rodolfo Giometti

Signature · 4 lines
GNU/Linux Solutions                  e-mail: [email protected]
Linux Device Driver                          [email protected]
Embedded Systems                     phone:  +39 349 2432127
UNIX programming                     skype:  rodolfo.giometti
#5 lore raw

Re: [PATCH 1/2] tty: add bits to manage multidrop mode

Le 12/09/2019 à 14:47, Rodolfo Giometti a écrit :

Quoted 43 lines · Richard Genoud
Quoted 6 lines

Hi Rodolfo,

You could also add these mailing lists:
- [email protected]
- [email protected]
- [email protected]

OK. No problem!

Quoted 4 lines

For reference, I've dug the conversation we had 2 years ago:
0/2 : https://marc.info/?t=149192176100002&r=1&w=2
1/2 : https://marc.info/?t=149192176300001&r=1&w=2
2/2 : https://marc.info/?t=149192176700001&r=1&w=2

Thanks.

Quoted 4 lines

And, like I said at that time, one problem I see with this implementation (setting a SENDA bit that automatically unsets itself) will break the way tcgetattr() is working:
https://marc.info/?l=linux-serial&m=149209522108027&w=2

I see... however the problem here is that this attribute is not sticky and it is reset by the controller after the first byte as been sent. Here is how Atmel/Microchip multidrop works (from the SAMA5D3's datasheet):

44.7.3.9 Multidrop Mode
If the value 0x6 or 0x07 is written to the PAR field in the US_MR, the USART runs in Multidrop Mode. This mode differentiates the data characters and the address characters. Data is transmitted with the parity bit to 0 and addresses are transmitted with the parity bit to 1.
...
The transmitter sends an address byte (parity bit set) when SENDA is written to in the US_CR. In this case, the next byte written to the US_THR is transmitted as an address. Any character written in the US_THR without having written the command SENDA is transmitted normally with the parity to 0.

So, if we have a 4 bytes message where the first one is the address byte we can use multidrop by setting the SENDA bit at the beginning and then by just invoking a write() with the four bytes message. The controller automatically will set the 9th bit to 1 for the first byte and then to 0 for the following three bytes.

Quoted 6 lines

To do so we need a syscall to tell to the controller when the address bit (SENDA) must be set, each time, before sending the message.

In this implementation I used the syscall within tcsetattr() function but if this is not right, then I have to use another one... maybe a custom ioctl? Or can you please suggest a suitable solution? :-)

What's bothering me with this implementation, is that it's too close to the hardware. Some other micro-controllers may not have the SENDA bit. For instance, i.MX6ULL:
--->8----
To transmit 9-bit RS-485 frames, user need to enable parity (PREN=1) to enable
trasmitting the ninth data bit, set 8-bit data word size (WS=1), and write TXB8
(UMCR[2]) as the 9 th bit (bit [8]) to be transmitted (write '0' to TXB8 to transmit a data
frame, write '1' to transmit a address frame). The other data bit [7:0] is written to TxFIFO
by writing to the UTXD same as normal RS-232 operation. ---8<----
And the TXB8 bit doesn't seem to clear itself automatically.

I maybe wrong, but I think that writing a line discipline for multidrop mode would be more versatile.

Regards,
Richard

Quoted 5 lines

Thanks for your help,

Rodolfo Giometti

#6 lore raw

PATCH 2/2 tty serial: add multidrop support for atmel serial controllers

In reply to Rodolfo Giometti

From: Rodolfo Giometti <[email protected]>

This patch adds multidrop support for atmel serial controllers and it has been tested by using a SAMA5D3 CPU.

---
 drivers/tty/serial/atmel_serial.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
drivers/tty/serial/atmel_serial.c +10 -4
index 0b4f36905321..b30b4a336907 100644
@@ -2178,7 +2178,7 @@ static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
 {
 	struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
 	unsigned long flags;
-	unsigned int old_mode, mode, imr, quot, baud, div, cd, fp = 0;
+	unsigned int old_mode, mode, mdrop, imr, quot, baud, div, cd, fp = 0;
 
 	/* save the current mode register */
 	mode = old_mode = atmel_uart_readl(port, ATMEL_US_MR);
@@ -2211,9 +2211,11 @@ static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
 
 	/* parity */
 	if (termios->c_cflag & PARENB) {
-		/* Mark or Space parity */
+		/* Mark, Space or Multidrop parity */
 		if (termios->c_cflag & CMSPAR) {
-			if (termios->c_cflag & PARODD)
+			if (termios->c_cflag & PARMD)
+				mode |= ATMEL_US_PAR_MULTI_DROP;
+			else if (termios->c_cflag & PARODD)
 				mode |= ATMEL_US_PAR_MARK;
 			else
 				mode |= ATMEL_US_PAR_SPACE;
@@ -2221,8 +2223,11 @@ static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
 			mode |= ATMEL_US_PAR_ODD;
 		else
 			mode |= ATMEL_US_PAR_EVEN;
+
 	} else
 		mode |= ATMEL_US_PAR_NONE;
+	mdrop = termios->c_cflag & SENDA ? ATMEL_US_SENDA : 0;
+	termios->c_cflag &= ~SENDA; /* SENDA bit must be cleared once used */
 
 	spin_lock_irqsave(&port->lock, flags);
 
@@ -2363,7 +2368,8 @@ static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
 	if (!(port->iso7816.flags & SER_ISO7816_ENABLED))
 		atmel_uart_writel(port, ATMEL_US_BRGR, quot);
 	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
-	atmel_uart_writel(port, ATMEL_US_CR, ATMEL_US_TXEN | ATMEL_US_RXEN);
+	atmel_uart_writel(port, ATMEL_US_CR,
+			  mdrop | ATMEL_US_TXEN | ATMEL_US_RXEN);
 	atmel_port->tx_stopped = false;
 
 	/* restore interrupts */
Signature · 1 line
2.17.1