baudrate.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * baud_t baudrate (unsigned rate)
  11. *
  12. * convert integer baud rate to system constant or bitmap;
  13. *
  14. *
  15. * Contributor(s):
  16. * Nathaniel Houghton <nhoughto@qca.qualcomm.com>
  17. * Charles Maier <cmaier@qca.qualcomm.com>
  18. *
  19. *--------------------------------------------------------------------*/
  20. #ifndef BAUDRATE_SOURCE
  21. #define BAUDRATE_SOURCE
  22. #include <termios.h>
  23. #include <errno.h>
  24. #include "../tools/error.h"
  25. signed baudrate (unsigned baud, speed_t * speed)
  26. {
  27. static struct baud
  28. {
  29. unsigned baud;
  30. speed_t code;
  31. }
  32. bauds [] =
  33. {
  34. {
  35. 0,
  36. B0
  37. },
  38. {
  39. 50,
  40. B50
  41. },
  42. {
  43. 75,
  44. B75
  45. },
  46. {
  47. 110,
  48. B110
  49. },
  50. {
  51. 134,
  52. B134
  53. },
  54. {
  55. 150,
  56. B150
  57. },
  58. {
  59. 200,
  60. B200
  61. },
  62. {
  63. 300,
  64. B300
  65. },
  66. {
  67. 600,
  68. B600
  69. },
  70. {
  71. 1200,
  72. B1200
  73. },
  74. {
  75. 1800,
  76. B1800
  77. },
  78. {
  79. 2400,
  80. B2400
  81. },
  82. {
  83. 4800,
  84. B4800
  85. },
  86. {
  87. 9600,
  88. B9600
  89. },
  90. {
  91. 19200,
  92. B19200
  93. },
  94. {
  95. 38400,
  96. B38400
  97. },
  98. {
  99. 57600,
  100. B57600
  101. },
  102. {
  103. 115200,
  104. B115200
  105. },
  106. #ifdef B230400
  107. {
  108. 230400,
  109. B230400
  110. },
  111. #endif
  112. #ifdef B460800
  113. {
  114. 460800,
  115. B460800
  116. },
  117. #endif
  118. #ifdef B500000
  119. {
  120. 500000,
  121. B500000
  122. },
  123. #endif
  124. #ifdef B921600
  125. {
  126. 921600,
  127. B921600
  128. },
  129. #endif
  130. };
  131. signed lower = 0;
  132. signed upper = sizeof (bauds) / sizeof (struct baud);
  133. while (lower < upper)
  134. {
  135. signed index = (lower + upper) >> 1;
  136. signed order = baud - bauds [index].baud;
  137. if (order < 0)
  138. {
  139. upper = index - 0;
  140. continue;
  141. }
  142. if (order > 0)
  143. {
  144. lower = index + 1;
  145. continue;
  146. }
  147. * speed = bauds [index].code;
  148. return (0);
  149. }
  150. return (- 1);
  151. }
  152. #endif