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.

66 lines
1.9 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. #if defined(WB32F3G71xx)
  19. static SerialConfig serialConfig = {SERIAL_DEFAULT_BITRATE, SD1_WRDLEN, SD1_STPBIT, SD1_PARITY, SD1_ATFLCT};
  20. #else
  21. static SerialConfig serialConfig = {SERIAL_DEFAULT_BITRATE, SD1_CR1, SD1_CR2, SD1_CR3};
  22. #endif
  23. void uart_init(uint32_t baud) {
  24. static bool is_initialised = false;
  25. if (!is_initialised) {
  26. is_initialised = true;
  27. serialConfig.speed = baud;
  28. #if defined(USE_GPIOV1)
  29. palSetLineMode(SD1_TX_PIN, PAL_MODE_ALTERNATE_OPENDRAIN);
  30. palSetLineMode(SD1_RX_PIN, PAL_MODE_ALTERNATE_OPENDRAIN);
  31. #else
  32. palSetLineMode(SD1_TX_PIN, PAL_MODE_ALTERNATE(SD1_TX_PAL_MODE) | PAL_OUTPUT_TYPE_OPENDRAIN);
  33. palSetLineMode(SD1_RX_PIN, PAL_MODE_ALTERNATE(SD1_RX_PAL_MODE) | PAL_OUTPUT_TYPE_OPENDRAIN);
  34. #endif
  35. sdStart(&SERIAL_DRIVER, &serialConfig);
  36. }
  37. }
  38. void uart_write(uint8_t data) {
  39. sdPut(&SERIAL_DRIVER, c);
  40. }
  41. uint8_t uart_read(void) {
  42. msg_t res = sdGet(&SERIAL_DRIVER);
  43. return (uint8_t)res;
  44. }
  45. void uart_transmit(const uint8_t *data, uint16_t length) {
  46. sdWrite(&SERIAL_DRIVER, data, length);
  47. }
  48. void uart_receive(uint8_t *data, uint16_t length) {
  49. sdRead(&SERIAL_DRIVER, data, length);
  50. }
  51. bool uart_available(void) {
  52. return !sdGetWouldBlock(&SERIAL_DRIVER);
  53. }