Skip to content

[components][drivers] fix posix tty, and add more baudrate #8683

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions components/drivers/include/drivers/serial.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@
#define BAUD_RATE_230400 230400
#define BAUD_RATE_460800 460800
#define BAUD_RATE_500000 500000
#define BAUD_RATE_576000 576000
#define BAUD_RATE_921600 921600
#define BAUD_RATE_1000000 1000000
#define BAUD_RATE_1152000 1152000
#define BAUD_RATE_1500000 1500000
#define BAUD_RATE_2000000 2000000
#define BAUD_RATE_2500000 2500000
#define BAUD_RATE_3000000 3000000
#define BAUD_RATE_3500000 3500000
#define BAUD_RATE_4000000 4000000

#define DATA_BITS_5 5
#define DATA_BITS_6 6
Expand Down
78 changes: 73 additions & 5 deletions components/drivers/serial/serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,11 @@ static rt_err_t rt_serial_open(struct rt_device *dev, rt_uint16_t oflag)
/* get open flags */
dev->open_flag = oflag & 0xff;

#ifdef RT_USING_PINCTRL
/* initialize iomux in DM */
rt_pin_ctrl_confs_apply_by_name(dev, RT_NULL);
#endif

/* initialize the Rx/Tx structure according to open flag */
if (serial->serial_rx == RT_NULL)
{
Expand Down Expand Up @@ -909,7 +914,7 @@ static rt_ssize_t rt_serial_write(struct rt_device *dev,
}
}

#if defined(RT_USING_POSIX_TERMIOS) && !defined(RT_USING_SMART)
#if defined(RT_USING_POSIX_TERMIOS)
struct speed_baudrate_item
{
speed_t speed;
Expand All @@ -928,9 +933,16 @@ static const struct speed_baudrate_item _tbl[] =
{B230400, BAUD_RATE_230400},
{B460800, BAUD_RATE_460800},
{B500000, BAUD_RATE_500000},
{B576000, BAUD_RATE_576000},
{B921600, BAUD_RATE_921600},
{B1000000, BAUD_RATE_1000000},
{B1152000, BAUD_RATE_1152000},
{B1500000, BAUD_RATE_1500000},
{B2000000, BAUD_RATE_2000000},
{B2500000, BAUD_RATE_2500000},
{B3000000, BAUD_RATE_3000000},
{B3500000, BAUD_RATE_3500000},
{B4000000, BAUD_RATE_4000000},
};

static speed_t _get_speed(int baudrate)
Expand Down Expand Up @@ -1002,6 +1014,32 @@ static void _tc_flush(struct rt_serial_device *serial, int queue)
}

}

static inline int _termio_to_termios(const struct termio *termio,
struct termios *termios)
{
termios->c_iflag = termio->c_iflag;
termios->c_oflag = termio->c_oflag;
termios->c_cflag = termio->c_cflag;
termios->c_lflag = termio->c_lflag;
termios->c_line = termio->c_line;
rt_memcpy(termios->c_cc, termio->c_cc, NCC);

return 0;
}

static inline int _termios_to_termio(const struct termios *termios,
struct termio *termio)
{
termio->c_iflag = (unsigned short)termios->c_iflag;
termio->c_oflag = (unsigned short)termios->c_oflag;
termio->c_cflag = (unsigned short)termios->c_cflag;
termio->c_lflag = (unsigned short)termios->c_lflag;
termio->c_line = termios->c_line;
rt_memcpy(termio->c_cc, termios->c_cc, NCC);

return 0;
}
#endif /* RT_USING_POSIX_TERMIOS */

static rt_err_t rt_serial_control(struct rt_device *dev,
Expand Down Expand Up @@ -1058,10 +1096,21 @@ static rt_err_t rt_serial_control(struct rt_device *dev,
}
break;
#ifdef RT_USING_POSIX_STDIO
#if defined(RT_USING_POSIX_TERMIOS) && !defined(RT_USING_SMART)
#if defined(RT_USING_POSIX_TERMIOS)
case TCGETA:
case TCGETS:
{
struct termios *tio = (struct termios*)args;
struct termios *tio, tmp;

if (cmd == TCGETS)
{
tio = (struct termios*)args;
}
else
{
tio = &tmp;
}

if (tio == RT_NULL) return -RT_EINVAL;

tio->c_iflag = 0;
Expand Down Expand Up @@ -1092,17 +1141,34 @@ static rt_err_t rt_serial_control(struct rt_device *dev,
tio->c_cflag |= (PARODD | PARENB);

cfsetospeed(tio, _get_speed(serial->config.baud_rate));

if (cmd == TCGETA)
{
_termios_to_termio(tio, args);
}
}
break;

case TCSETAW:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

旧的case需要保留,但可以做个兼容性修正。termio应该是废弃的标准。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

termio 是 SVR4 标准的规范,和 POSIX termios 的主要区别是结构体的类型重名了。而我觉得这些不该是 serial 这层处理的。

因为 termios 是 POSIX 针对终端的规范。在 TTY 这一层,类似 termio 这种非 POSIX 标准的兼容性差异都会被处理。所以有了 tty 这个更高层的协议,驱动是不会看到 termio 这个结构体传下来的。

所以对于支持 POSIX 终端的 smart,serial 这边根本不用考虑这个问题。至于没有 TTY 的 RTOS 里面,个人认为没必要做得那么全,反正本来也没有支持全部的控制命令。

case TCSETAF:
case TCSETA:
case TCSETSW:
case TCSETSF:
case TCSETS:
{
int baudrate;
struct serial_configure config;
struct termios *tio, tmp;

if ((cmd >= TCSETA) && (cmd <= TCSETA + 2))
{
_termio_to_termios(args, &tmp);
tio = &tmp;
}
else
{
tio = (struct termios*)args;
}

struct termios *tio = (struct termios*)args;
if (tio == RT_NULL) return -RT_EINVAL;

config = serial->config;
Expand Down Expand Up @@ -1139,6 +1205,7 @@ static rt_err_t rt_serial_control(struct rt_device *dev,
serial->ops->configure(serial, &config);
}
break;
#ifndef RT_USING_TTY
case TCFLSH:
{
int queue = (int)(rt_ubase_t)args;
Expand All @@ -1149,6 +1216,7 @@ static rt_err_t rt_serial_control(struct rt_device *dev,
break;
case TCXONC:
break;
#endif /*RT_USING_TTY*/
#endif /*RT_USING_POSIX_TERMIOS*/
case TIOCSWINSZ:
{
Expand Down
9 changes: 9 additions & 0 deletions components/drivers/serial/serial_tty.c
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,15 @@ static int serial_tty_ioctl(struct lwp_tty *tp, rt_ubase_t cmd, rt_caddr_t data,
int error;
switch (cmd)
{
case TCSETS:
case TCSETSW:
case TCSETSF:
RT_ASSERT(tp->t_devswsoftc);
struct serial_tty_context *softc = (struct serial_tty_context *)(tp->t_devswsoftc);
struct rt_serial_device *serial = softc->parent;
struct termios *termios = (struct termios *)data;
rt_device_control(&(serial->parent), cmd, termios);
error = -ENOIOCTL;
default:
/**
* Note: for the most case, we don't let serial layer handle ioctl,
Expand Down
2 changes: 1 addition & 1 deletion libcpu/aarch64/common/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ if GetDepend('RT_USING_OFW') == False:
SrcRemove(src, ['setup.c', 'cpu_psci.c', 'psci.c'])

if GetDepend('RT_USING_PIC') == True:
SrcRemove(src, ['gicv3.c', 'gic.c', 'gtimer.c'])
SrcRemove(src, ['gicv3.c', 'gic.c', 'gtimer.c', 'interrupt.c'])

group = DefineGroup('libcpu', src, depend = [''], CPPPATH = CPPPATH)

Expand Down
8 changes: 8 additions & 0 deletions libcpu/aarch64/common/context_gcc.S
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@
#include "asm-fpu.h"
#include "armv8.h"

#ifndef RT_USING_SMP
.bss
.align 3
rt_interrupt_from_thread: .comm 8, 8
rt_interrupt_to_thread: .comm 8, 8
rt_thread_switch_interrupt_flag: .comm 8, 8
#endif

.text
.weak rt_hw_cpu_id_set
.type rt_hw_cpu_id_set, @function
Expand Down
14 changes: 0 additions & 14 deletions libcpu/aarch64/common/interrupt.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@
#include "gicv3.h"
#include "ioremap.h"

#ifndef RT_USING_SMP
/* Those variables will be accessed in ISR, so we need to share them. */
rt_ubase_t rt_interrupt_from_thread = 0;
rt_ubase_t rt_interrupt_to_thread = 0;
rt_ubase_t rt_thread_switch_interrupt_flag = 0;
#endif

#ifndef RT_USING_PIC

/* exception and interrupt handler table */
struct rt_irq_desc isr_table[MAX_HANDLERS];

Expand Down Expand Up @@ -87,9 +78,6 @@ void rt_hw_interrupt_init(void)

/* init interrupt nest, and context in thread sp */
rt_atomic_store(&rt_interrupt_nest, 0);
rt_interrupt_from_thread = 0;
rt_interrupt_to_thread = 0;
rt_thread_switch_interrupt_flag = 0;
#else
rt_uint64_t gic_cpu_base;
rt_uint64_t gic_dist_base;
Expand Down Expand Up @@ -417,8 +405,6 @@ void rt_hw_ipi_handler_install(int ipi_vector, rt_isr_handler_t ipi_isr_handler)
}
#endif

#endif /* RT_USING_PIC */

#if defined(FINSH_USING_MSH) && defined(RT_USING_INTERRUPT_INFO)
int list_isr()
{
Expand Down
Loading