You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.6 KiB

  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 = {SERIAL_DEFAULT_BITRATE, SD1_CR1, SD1_CR2, SD1_CR3};
  19. void uart_init(uint32_t baud) {
  20. static bool is_initialised = false;
  21. if (!is_initialised) {
  22. is_initialised = true;
  23. serialConfig.speed = baud;
  24. #if defined(USE_GPIOV1)
  25. palSetLineMode(SD1_TX_PIN, PAL_MODE_STM32_ALTERNATE_OPENDRAIN);
  26. palSetLineMode(SD1_RX_PIN, PAL_MODE_STM32_ALTERNATE_OPENDRAIN);
  27. #else
  28. palSetLineMode(SD1_TX_PIN, PAL_MODE_ALTERNATE(SD1_TX_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN);
  29. palSetLineMode(SD1_RX_PIN, PAL_MODE_ALTERNATE(SD1_RX_PAL_MODE) | PAL_STM32_OTYPE_OPENDRAIN);
  30. #endif
  31. sdStart(&SERIAL_DRIVER, &serialConfig);
  32. }
  33. }
  34. void uart_putchar(uint8_t c) { sdPut(&SERIAL_DRIVER, c); }
  35. uint8_t uart_getchar(void) {
  36. msg_t res = sdGet(&SERIAL_DRIVER);
  37. return (uint8_t)res;
  38. }
  39. bool uart_available(void) { return !sdGetWouldBlock(&SERIAL_DRIVER); }