update uart to remove dependency on machine uart module

This commit is contained in:
hathach 2017-12-29 21:25:43 +07:00
parent 178925640b
commit a021a9e5f0
3 changed files with 40 additions and 31 deletions

View File

@ -69,16 +69,6 @@ hal_uart_error_t hal_uart_char_write(NRF_UART_Type * p_instance, uint8_t ch) {
}
hal_uart_error_t hal_uart_char_read(NRF_UART_Type * p_instance, uint8_t * ch) {
// p_instance->ERRORSRC = 0;
// while (p_instance->EVENTS_RXDRDY != 1) {
// // Wait for RXD data.
// }
//
// p_instance->EVENTS_RXDRDY = 0;
// *ch = p_instance->RXD;
//
// return p_instance->ERRORSRC;
while ( !fifo_read(_ff_uart, ch) ) {
// wait for fifo data
}

View File

@ -30,8 +30,9 @@
#include "py/mpstate.h"
#include "py/mphal.h"
#include "py/mperrno.h"
#include "uart.h"
#include "hal_uart.h"
#define UART_INSTANCE UART_BASE(0)
FIL* boot_output_file;
// this table converts from HAL_StatusTypeDef to POSIX errno
@ -46,36 +47,36 @@ NORETURN void mp_hal_raise(HAL_StatusTypeDef status) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(mp_hal_status_to_errno_table[status])));
}
void mp_hal_set_interrupt_char(int c) {
}
#if (MICROPY_PY_BLE_NUS == 0)
int mp_hal_stdin_rx_chr(void) {
for (;;) {
if (MP_STATE_PORT(pyb_stdio_uart) != NULL && uart_rx_any(MP_STATE_PORT(pyb_stdio_uart))) {
return uart_rx_char(MP_STATE_PORT(pyb_stdio_uart));
if ( hal_uart_available(UART_INSTANCE) ) {
uint8_t ch;
hal_uart_char_read(UART_INSTANCE, &ch);
return (int) ch;
}
}
return 0;
}
bool mp_hal_stdin_any(void)
{
return uart_rx_any(MP_STATE_PORT(pyb_stdio_uart));
bool mp_hal_stdin_any(void) {
return hal_uart_available(UART_INSTANCE);
}
void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
if (MP_STATE_PORT(pyb_stdio_uart) != NULL) {
uart_tx_strn(MP_STATE_PORT(pyb_stdio_uart), str, len);
}
while(len--) {
hal_uart_char_write(UART_INSTANCE, *str++);
}
}
void mp_hal_stdout_tx_strn_cooked(const char *str, mp_uint_t len) {
if (MP_STATE_PORT(pyb_stdio_uart) != NULL) {
uart_tx_strn_cooked(MP_STATE_PORT(pyb_stdio_uart), str, len);
while(len--){
if (*str == '\n') {
hal_uart_char_write(UART_INSTANCE, '\r');
}
hal_uart_char_write(UART_INSTANCE, *str++);
}
}
#endif

View File

@ -29,18 +29,36 @@
#include "py/obj.h"
#include "py/runtime.h"
#include "mphalport.h"
#include "uart.h"
#include "pins.h"
#include "hal_uart.h"
void serial_init(void) {
uart_init0();
// uart_init0();
//
// mp_obj_t args[2] = {
// MP_OBJ_NEW_SMALL_INT(0),
// MP_OBJ_NEW_SMALL_INT(115200),
// };
// MP_STATE_PORT(pyb_stdio_uart) = machine_hard_uart_type.make_new((mp_obj_t)&machine_hard_uart_type, MP_ARRAY_SIZE(args), 0, args);
mp_obj_t args[2] = {
MP_OBJ_NEW_SMALL_INT(0),
MP_OBJ_NEW_SMALL_INT(115200),
hal_uart_init_t param =
{
.id = 0,
.rx_pin = &MICROPY_HW_UART1_RX,
.tx_pin = &MICROPY_HW_UART1_TX,
.rts_pin = NULL,
.cts_pin = NULL,
.flow_control = MICROPY_HW_UART1_HWFC ? true : false,
.use_parity = false,
.baud_rate = HAL_UART_BAUD_115K2,
.irq_priority = 6,
.irq_num = UARTE0_UART0_IRQn
};
MP_STATE_PORT(pyb_stdio_uart) = machine_hard_uart_type.make_new((mp_obj_t)&machine_hard_uart_type, MP_ARRAY_SIZE(args), 0, args);
hal_uart_init( UART_BASE(0), &param);
}
bool serial_connected(void) {
return true;
}