uart.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* Copyright 2021
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. */
  16. #include "uart.h"
  17. #include "quantum.h"
  18. static SerialConfig serialConfig = {
  19. SERIAL_DEFAULT_BITRATE,
  20. SD1_CR1,
  21. SD1_CR2,
  22. SD1_CR3
  23. };
  24. void uart_init(uint32_t baud) {
  25. static bool is_initialised = false;
  26. if (!is_initialised) {
  27. is_initialised = true;
  28. serialConfig.speed = baud;
  29. #if defined(USE_GPIOV1)
  30. palSetLineMode(SD1_TX_PIN, PAL_MODE_STM32_ALTERNATE_OPENDRAIN);
  31. palSetLineMode(SD1_RX_PIN, PAL_MODE_STM32_ALTERNATE_OPENDRAIN);
  32. #else
  33. palSetLineMode(SD1_TX_PIN, PAL_MODE_ALTERNATE(SD1_TX_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN);
  34. palSetLineMode(SD1_RX_PIN, PAL_MODE_ALTERNATE(SD1_RX_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN);
  35. #endif
  36. sdStart(&SERIAL_DRIVER, &serialConfig);
  37. }
  38. }
  39. void uart_putchar(uint8_t c) {
  40. sdPut(&SERIAL_DRIVER, c);
  41. }
  42. uint8_t uart_getchar(void) {
  43. msg_t res = sdGet(&SERIAL_DRIVER);
  44. return (uint8_t)res;
  45. }
  46. bool uart_available(void) {
  47. return !sdGetWouldBlock(&SERIAL_DRIVER);
  48. }