omap_8250.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) Siemens AG, 2019-2022
  3. *
  4. * Authors:
  5. * Gao Nian <nian.gao@siemens.com>
  6. * Chao Zeng <chao.zeng@siemens.com>
  7. *
  8. * This file is subject to the terms and conditions of the MIT License. See
  9. * COPYING.MIT file in the top-level directory.
  10. */
  11. #include <unistd.h>
  12. #include <stdlib.h>
  13. #include <termios.h>
  14. #include <stdbool.h>
  15. #include <stdio.h>
  16. #include <stdint.h>
  17. #include <stdarg.h>
  18. #include <string.h>
  19. #include <linux/serial.h>
  20. #include <linux/types.h>
  21. #include <sys/ioctl.h>
  22. #include <fcntl.h>
  23. #include "switchserialmode.h"
  24. #define GET_UART_RTS_ACTIVE_LOGIC(flags) (!((flags) & SER_RS485_RTS_ON_SEND))
  25. typedef struct omap_8250 {
  26. struct serial_rs485 r485Conf;
  27. int32_t uartHandle;
  28. int8_t (*ttyuart_set_rs485conf)(struct omap_8250 *);
  29. } omap_8250_ops_t;
  30. omap_8250_ops_t omap_8250_ops;
  31. static int8_t ttyuart_get_rs485conf(struct omap_8250 *ops)
  32. {
  33. int32_t ret = ioctl(ops->uartHandle, TIOCGRS485, &(ops->r485Conf));
  34. if (ret < 0) {
  35. perror("Error");
  36. return ERROR;
  37. }
  38. return SUCCESS;
  39. }
  40. static int8_t ttyuart_set_rs485conf(struct omap_8250 *ops)
  41. {
  42. int32_t ret = ioctl(ops->uartHandle, TIOCSRS485, ops->r485Conf);
  43. if (ret < 0) {
  44. perror("Error");
  45. return ERROR;
  46. }
  47. return SUCCESS;
  48. }
  49. static int8_t ttyuart_init(uint8_t *uartdev)
  50. {
  51. int32_t fd = open(uartdev, O_RDWR);
  52. if (fd < 0) {
  53. printf("Open tty uart failed\n");
  54. return ERROR;
  55. }
  56. omap_8250_ops.uartHandle = fd;
  57. omap_8250_ops.ttyuart_set_rs485conf = ttyuart_set_rs485conf;
  58. if (SUCCESS != ttyuart_get_rs485conf(&omap_8250_ops)) {
  59. printf("Get configuration fail\n");
  60. return ERROR;
  61. }
  62. return SUCCESS;
  63. }
  64. static void ttyuart_release(void)
  65. {
  66. /*write conf to the device*/
  67. if (SUCCESS != omap_8250_ops.ttyuart_set_rs485conf(&omap_8250_ops)) {
  68. printf("Set configuration fail\n");
  69. }
  70. close(omap_8250_ops.uartHandle);
  71. }
  72. static void ttyuart_print_mode(void)
  73. {
  74. const uint8_t *mode = NULL;
  75. const uint8_t *activelogic = NULL;
  76. struct serial_rs485 rs485Conf = omap_8250_ops.r485Conf;
  77. if (!(rs485Conf.flags & SER_RS485_ENABLED)) {
  78. mode = "rs232";
  79. activelogic = "";
  80. }
  81. else {
  82. if (rs485Conf.flags & SER_RS485_RX_DURING_TX) {
  83. mode = "rs422";
  84. activelogic = "";
  85. }
  86. else {
  87. mode = "rs485";
  88. activelogic = GET_UART_RTS_ACTIVE_LOGIC(rs485Conf.flags) ? "active-logic(high)" \
  89. : "active-logic(low)";
  90. }
  91. }
  92. printf("%s %s\n", mode, activelogic);
  93. }
  94. static void ttyuart_switch_mode(uint8_t *mode)
  95. {
  96. struct serial_rs485 *rs485Conf = (struct serial_rs485 *)&(omap_8250_ops.r485Conf);
  97. memset(rs485Conf, 0, sizeof(rs485Conf));
  98. /*
  99. The latest kernel has already handle these flags.Its could be deleted.
  100. For the previous compatability, keep this settings
  101. */
  102. rs485Conf->flags &= ~(SER_RS485_RTS_ON_SEND);
  103. rs485Conf->flags |= (SER_RS485_RTS_AFTER_SEND);
  104. if (0 == strcasecmp(mode, "rs485")) {
  105. rs485Conf->flags |= SER_RS485_ENABLED;
  106. }
  107. else if (0 == strcasecmp(mode, "rs422")) {
  108. rs485Conf->flags |= SER_RS485_ENABLED | SER_RS485_RX_DURING_TX;
  109. }
  110. else if (0 == strcasecmp(mode, "rs232")) {
  111. memset(rs485Conf, 0, sizeof(rs485Conf));
  112. }
  113. }
  114. static void ttyuart_set_rs485_hold_time(uint8_t holdtime)
  115. {
  116. printf("Set RS485 hold time not supported\n");
  117. }
  118. static void ttyuart_set_rs485_setup_time(uint8_t holdtime)
  119. {
  120. printf("Set RS485 setup time not supported\n");
  121. }
  122. serial_ops_t ttyuart_ops = {
  123. .devName = "/dev/ttyX30",
  124. .init = ttyuart_init,
  125. .rs485HoldTime = ttyuart_set_rs485_hold_time,
  126. .rs485SetupTime = ttyuart_set_rs485_setup_time,
  127. .setMode = ttyuart_switch_mode,
  128. .getMode = ttyuart_print_mode,
  129. .release = ttyuart_release,
  130. };