uart_core.c 909 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include <unistd.h>
  2. #include "py/mpconfig.h"
  3. /*
  4. * Core UART functions to implement for a port
  5. */
  6. #if MICROPY_MIN_USE_STM32_MCU
  7. typedef struct {
  8. volatile uint32_t SR;
  9. volatile uint32_t DR;
  10. } periph_uart_t;
  11. #define USART1 ((periph_uart_t*)0x40011000)
  12. #endif
  13. // Receive single character
  14. int mp_hal_stdin_rx_chr(void) {
  15. unsigned char c = 0;
  16. #if MICROPY_MIN_USE_STDOUT
  17. int r = read(0, &c, 1);
  18. (void)r;
  19. #elif MICROPY_MIN_USE_STM32_MCU
  20. // wait for RXNE
  21. while ((USART1->SR & (1 << 5)) == 0) {
  22. }
  23. c = USART1->DR;
  24. #endif
  25. return c;
  26. }
  27. // Send string of given length
  28. void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
  29. #if MICROPY_MIN_USE_STDOUT
  30. int r = write(1, str, len);
  31. (void)r;
  32. #elif MICROPY_MIN_USE_STM32_MCU
  33. while (len--) {
  34. // wait for TXE
  35. while ((USART1->SR & (1 << 7)) == 0) {
  36. }
  37. USART1->DR = *str++;
  38. }
  39. #endif
  40. }