uart.c 2.1 KB

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