uart.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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_ALTERNATE_OPENDRAIN);
  26. palSetLineMode(SD1_RX_PIN, PAL_MODE_ALTERNATE_OPENDRAIN);
  27. #else
  28. palSetLineMode(SD1_TX_PIN, PAL_MODE_ALTERNATE(SD1_TX_PAL_MODE) | PAL_OUTPUT_TYPE_OPENDRAIN);
  29. palSetLineMode(SD1_RX_PIN, PAL_MODE_ALTERNATE(SD1_RX_PAL_MODE) | PAL_OUTPUT_TYPE_OPENDRAIN);
  30. #endif
  31. sdStart(&SERIAL_DRIVER, &serialConfig);
  32. }
  33. }
  34. void uart_write(uint8_t data) { sdPut(&SERIAL_DRIVER, c); }
  35. uint8_t uart_read(void) {
  36. msg_t res = sdGet(&SERIAL_DRIVER);
  37. return (uint8_t)res;
  38. }
  39. void uart_transmit(const uint8_t *data, uint16_t length) { sdWrite(&SERIAL_DRIVER, data, length); }
  40. void uart_receive(uint8_t *data, uint16_t length) { sdRead(&SERIAL_DRIVER, data, length); }
  41. bool uart_available(void) { return !sdGetWouldBlock(&SERIAL_DRIVER); }