2015-01-13 02:02:56 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include "py/mpconfig.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Core UART functions to implement for a port
|
|
|
|
*/
|
|
|
|
|
2016-01-07 17:43:07 +00:00
|
|
|
#if MICROPY_MIN_USE_STM32_MCU
|
|
|
|
typedef struct {
|
|
|
|
volatile uint32_t SR;
|
|
|
|
volatile uint32_t DR;
|
|
|
|
} periph_uart_t;
|
2020-02-27 04:36:53 +00:00
|
|
|
#define USART1 ((periph_uart_t *)0x40011000)
|
2016-01-07 17:43:07 +00:00
|
|
|
#endif
|
|
|
|
|
2015-01-13 02:02:56 +00:00
|
|
|
// Receive single character
|
2015-02-13 15:26:53 +00:00
|
|
|
int mp_hal_stdin_rx_chr(void) {
|
2015-01-13 02:02:56 +00:00
|
|
|
unsigned char c = 0;
|
2020-02-27 04:36:53 +00:00
|
|
|
#if MICROPY_MIN_USE_STDOUT
|
2015-01-13 02:02:56 +00:00
|
|
|
int r = read(0, &c, 1);
|
|
|
|
(void)r;
|
2020-02-27 04:36:53 +00:00
|
|
|
#elif MICROPY_MIN_USE_STM32_MCU
|
2016-01-07 17:43:07 +00:00
|
|
|
// wait for RXNE
|
|
|
|
while ((USART1->SR & (1 << 5)) == 0) {
|
|
|
|
}
|
|
|
|
c = USART1->DR;
|
2020-02-27 04:36:53 +00:00
|
|
|
#endif
|
2015-01-13 02:02:56 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send string of given length
|
2015-02-13 15:26:53 +00:00
|
|
|
void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
|
2020-02-27 04:36:53 +00:00
|
|
|
#if MICROPY_MIN_USE_STDOUT
|
2015-01-13 02:02:56 +00:00
|
|
|
int r = write(1, str, len);
|
|
|
|
(void)r;
|
2020-02-27 04:36:53 +00:00
|
|
|
#elif MICROPY_MIN_USE_STM32_MCU
|
2016-01-07 17:43:07 +00:00
|
|
|
while (len--) {
|
|
|
|
// wait for TXE
|
|
|
|
while ((USART1->SR & (1 << 7)) == 0) {
|
|
|
|
}
|
|
|
|
USART1->DR = *str++;
|
|
|
|
}
|
2020-02-27 04:36:53 +00:00
|
|
|
#endif
|
2015-01-13 02:02:56 +00:00
|
|
|
}
|