uart.c 2.1 KB

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