logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

uart_serial.c (2981B)


  1. // Copyright 2024 Stefan Kerkmann (@KarlK90)
  2. // Copyright 2021 QMK
  3. // SPDX-License-Identifier: GPL-2.0-or-later
  4. #include "uart.h"
  5. #include "gpio.h"
  6. #include "chibios_config.h"
  7. #include <hal.h>
  8. #ifndef UART_DRIVER
  9. # define UART_DRIVER SD1
  10. #endif
  11. #ifndef UART_TX_PIN
  12. # define UART_TX_PIN A9
  13. #endif
  14. #ifndef UART_TX_PAL_MODE
  15. # ifdef USE_GPIOV1
  16. # define UART_TX_PAL_MODE PAL_MODE_ALTERNATE_PUSHPULL
  17. # else
  18. # define UART_TX_PAL_MODE 7
  19. # endif
  20. #endif
  21. #ifndef UART_RX_PIN
  22. # define UART_RX_PIN A10
  23. #endif
  24. #ifndef UART_RX_PAL_MODE
  25. # ifdef USE_GPIOV1
  26. # define UART_RX_PAL_MODE PAL_MODE_INPUT
  27. # else
  28. # define UART_RX_PAL_MODE 7
  29. # endif
  30. #endif
  31. #ifndef UART_CTS_PIN
  32. # define UART_CTS_PIN A11
  33. #endif
  34. #ifndef UART_CTS_PAL_MODE
  35. # ifdef USE_GPIOV1
  36. # define UART_CTS_PAL_MODE PAL_MODE_INPUT
  37. # else
  38. # define UART_CTS_PAL_MODE 7
  39. # endif
  40. #endif
  41. #ifndef UART_RTS_PIN
  42. # define UART_RTS_PIN A12
  43. #endif
  44. #ifndef UART_RTS_PAL_MODE
  45. # ifdef USE_GPIOV1
  46. # define UART_RTS_PAL_MODE PAL_MODE_ALTERNATE_PUSHPULL
  47. # else
  48. # define UART_RTS_PAL_MODE 7
  49. # endif
  50. #endif
  51. #ifndef UART_CR1
  52. # define UART_CR1 0
  53. #endif
  54. #ifndef UART_CR2
  55. # define UART_CR2 0
  56. #endif
  57. #ifndef UART_CR3
  58. # define UART_CR3 0
  59. #endif
  60. #ifndef UART_WRDLEN
  61. # define UART_WRDLEN 3
  62. #endif
  63. #ifndef UART_STPBIT
  64. # define UART_STPBIT 0
  65. #endif
  66. #ifndef UART_PARITY
  67. # define UART_PARITY 0
  68. #endif
  69. #ifndef UART_ATFLCT
  70. # define UART_ATFLCT 0
  71. #endif
  72. #if defined(MCU_KINETIS)
  73. static SerialConfig serialConfig = {SERIAL_DEFAULT_BITRATE};
  74. #elif defined(WB32F3G71xx) || defined(WB32FQ95xx)
  75. static SerialConfig serialConfig = {
  76. SERIAL_DEFAULT_BITRATE, UART_WRDLEN, UART_STPBIT, UART_PARITY, UART_ATFLCT,
  77. };
  78. #else
  79. static SerialConfig serialConfig = {
  80. SERIAL_DEFAULT_BITRATE,
  81. UART_CR1,
  82. UART_CR2,
  83. UART_CR3,
  84. };
  85. #endif
  86. void uart_init(uint32_t baud) {
  87. static bool is_initialised = false;
  88. if (is_initialised) {
  89. return;
  90. }
  91. is_initialised = true;
  92. #if defined(MCU_KINETIS)
  93. serialConfig.sc_speed = baud;
  94. #else
  95. serialConfig.speed = baud;
  96. #endif
  97. #if defined(USE_GPIOV1)
  98. palSetLineMode(UART_TX_PIN, UART_TX_PAL_MODE);
  99. palSetLineMode(UART_RX_PIN, UART_RX_PAL_MODE);
  100. #else
  101. palSetLineMode(UART_TX_PIN, PAL_MODE_ALTERNATE(UART_TX_PAL_MODE) | PAL_OUTPUT_TYPE_PUSHPULL | PAL_OUTPUT_SPEED_HIGHEST);
  102. palSetLineMode(UART_RX_PIN, PAL_MODE_ALTERNATE(UART_RX_PAL_MODE) | PAL_OUTPUT_TYPE_PUSHPULL | PAL_OUTPUT_SPEED_HIGHEST);
  103. #endif
  104. sdStart(&UART_DRIVER, &serialConfig);
  105. }
  106. void uart_write(uint8_t data) {
  107. sdPut(&UART_DRIVER, data);
  108. }
  109. uint8_t uart_read(void) {
  110. return (uint8_t)sdGet(&UART_DRIVER);
  111. }
  112. void uart_transmit(const uint8_t *data, uint16_t length) {
  113. sdWrite(&UART_DRIVER, data, length);
  114. }
  115. void uart_receive(uint8_t *data, uint16_t length) {
  116. sdRead(&UART_DRIVER, data, length);
  117. }
  118. bool uart_available(void) {
  119. return !sdGetWouldBlock(&UART_DRIVER);
  120. }