migrate serial from uart to uarte

This commit is contained in:
hathach 2018-09-25 16:14:44 +07:00
parent 9017c9d29a
commit 2f0e0bdcaf
6 changed files with 57 additions and 64 deletions

View File

@ -30,7 +30,6 @@
#define MICROPY_HW_UART_RX NRF_GPIO_PIN_MAP(0, 8) #define MICROPY_HW_UART_RX NRF_GPIO_PIN_MAP(0, 8)
#define MICROPY_HW_UART_TX NRF_GPIO_PIN_MAP(0, 6) #define MICROPY_HW_UART_TX NRF_GPIO_PIN_MAP(0, 6)
#define MICROPY_HW_UART_HWFC (0)
#define PORT_HEAP_SIZE (32 * 1024) #define PORT_HEAP_SIZE (32 * 1024)
#define CIRCUITPY_AUTORELOAD_DELAY_MS 500 #define CIRCUITPY_AUTORELOAD_DELAY_MS 500

View File

@ -91,13 +91,13 @@ void common_hal_busio_uart_construct (busio_uart_obj_t *self,
} }
nrfx_uarte_config_t config = { nrfx_uarte_config_t config = {
.pseltxd = (tx == mp_const_none) ? NRF_UART_PSEL_DISCONNECTED : tx->number, .pseltxd = (tx == mp_const_none) ? NRF_UARTE_PSEL_DISCONNECTED : tx->number,
.pselrxd = (rx == mp_const_none) ? NRF_UART_PSEL_DISCONNECTED : rx->number, .pselrxd = (rx == mp_const_none) ? NRF_UARTE_PSEL_DISCONNECTED : rx->number,
.pselcts = NRF_UART_PSEL_DISCONNECTED, .pselcts = NRF_UARTE_PSEL_DISCONNECTED,
.pselrts = NRF_UART_PSEL_DISCONNECTED, .pselrts = NRF_UARTE_PSEL_DISCONNECTED,
.p_context = self, .p_context = self,
.hwfc = NRF_UART_HWFC_DISABLED, .hwfc = NRF_UARTE_HWFC_DISABLED,
.parity = (parity == PARITY_NONE) ? NRF_UART_PARITY_EXCLUDED : NRF_UART_PARITY_INCLUDED, .parity = (parity == PARITY_NONE) ? NRF_UARTE_PARITY_EXCLUDED : NRF_UARTE_PARITY_INCLUDED,
.baudrate = get_nrf_baud(baudrate), .baudrate = get_nrf_baud(baudrate),
.interrupt_priority = 7 .interrupt_priority = 7
}; };
@ -132,8 +132,8 @@ void common_hal_busio_uart_construct (busio_uart_obj_t *self,
} }
bool common_hal_busio_uart_deinited(busio_uart_obj_t *self) { bool common_hal_busio_uart_deinited(busio_uart_obj_t *self) {
return (nrf_uarte_rx_pin_get(self->uarte.p_reg) == NRF_UART_PSEL_DISCONNECTED) && return (nrf_uarte_rx_pin_get(self->uarte.p_reg) == NRF_UARTE_PSEL_DISCONNECTED) &&
(nrf_uarte_tx_pin_get(self->uarte.p_reg) == NRF_UART_PSEL_DISCONNECTED); (nrf_uarte_tx_pin_get(self->uarte.p_reg) == NRF_UARTE_PSEL_DISCONNECTED);
} }
void common_hal_busio_uart_deinit(busio_uart_obj_t *self) { void common_hal_busio_uart_deinit(busio_uart_obj_t *self) {
@ -145,7 +145,7 @@ void common_hal_busio_uart_deinit(busio_uart_obj_t *self) {
// Read characters. // Read characters.
size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t len, int *errcode) { size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t len, int *errcode) {
if ( nrf_uarte_rx_pin_get(self->uarte.p_reg) == NRF_UART_PSEL_DISCONNECTED ) { if ( nrf_uarte_rx_pin_get(self->uarte.p_reg) == NRF_UARTE_PSEL_DISCONNECTED ) {
mp_raise_ValueError(translate("No RX pin")); mp_raise_ValueError(translate("No RX pin"));
} }
@ -191,7 +191,7 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
// Write characters. // Write characters.
size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data, size_t len, int *errcode) { size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data, size_t len, int *errcode) {
if ( nrf_uarte_tx_pin_get(self->uarte.p_reg) == NRF_UART_PSEL_DISCONNECTED ) { if ( nrf_uarte_tx_pin_get(self->uarte.p_reg) == NRF_UARTE_PSEL_DISCONNECTED ) {
mp_raise_ValueError(translate("No TX pin")); mp_raise_ValueError(translate("No TX pin"));
} }

View File

@ -27,36 +27,40 @@
#include <string.h> #include <string.h>
#include "mphalport.h" #include "py/mphal.h"
#include "py/mpstate.h" #include "py/mpstate.h"
#include "py/gc.h"
#if (MICROPY_PY_BLE_NUS == 0) #if (MICROPY_PY_BLE_NUS == 0)
#if !defined( NRF52840_XXAA) #if !defined( NRF52840_XXAA)
int mp_hal_stdin_rx_chr(void) { int mp_hal_stdin_rx_chr(void) {
uint8_t data = 0; uint8_t data;
nrfx_uarte_rx(&serial_instance, &data, 1);
while (!nrfx_uart_rx_ready(&serial_instance));
const nrfx_err_t err = nrfx_uart_rx(&serial_instance, &data, sizeof(data));
if (err == NRFX_SUCCESS)
NRFX_ASSERT(err);
return data; return data;
} }
bool mp_hal_stdin_any(void) { bool mp_hal_stdin_any(void) {
return nrfx_uart_rx_ready(&serial_instance); return nrf_uarte_event_check(serial_instance.p_reg, NRF_UARTE_EVENT_RXDRDY);
} }
void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) { void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
if (len == 0) if (len == 0) {
return; return;
}
const nrfx_err_t err = nrfx_uart_tx(&serial_instance, (uint8_t*)str, len); // EasyDMA can only access SRAM
if (err == NRFX_SUCCESS) uint8_t * tx_buf = (uint8_t*) str;
NRFX_ASSERT(err); if ( !nrfx_is_in_ram(str) ) {
tx_buf = (uint8_t *) gc_alloc(len, false, false);
memcpy(tx_buf, str, len);
}
nrfx_uarte_tx(&serial_instance, tx_buf, len);
if ( !nrfx_is_in_ram(str) ) {
gc_free(tx_buf);
}
} }
#else #else

View File

@ -31,21 +31,16 @@
#include <stdint.h> #include <stdint.h>
#include "lib/utils/interrupt_char.h" #include "lib/utils/interrupt_char.h"
#include "nrfx_uart.h" #include "nrfx_uarte.h"
#include "py/mpconfig.h" #include "py/mpconfig.h"
extern nrfx_uart_t serial_instance; extern nrfx_uarte_t serial_instance;
extern volatile uint64_t ticks_ms; extern volatile uint64_t ticks_ms;
static inline mp_uint_t mp_hal_ticks_ms(void) { #define mp_hal_ticks_ms() ((mp_uint_t) ticks_ms)
return ticks_ms;
}
int mp_hal_stdin_rx_chr(void);
void mp_hal_stdout_tx_str(const char *str);
bool mp_hal_stdin_any(void);
void mp_hal_delay_ms(mp_uint_t ms);
#define mp_hal_delay_us(us) NRFX_DELAY_US((uint32_t) (us)) #define mp_hal_delay_us(us) NRFX_DELAY_US((uint32_t) (us))
bool mp_hal_stdin_any(void);
#endif #endif

View File

@ -45,18 +45,8 @@
#define NRFX_TWIM_DEFAULT_CONFIG_HOLD_BUS_UNINIT 0 #define NRFX_TWIM_DEFAULT_CONFIG_HOLD_BUS_UNINIT 0
// UART // UART
#if 0
#define NRFX_UART_ENABLED 1
#define NRFX_UART0_ENABLED 1
#else
#define NRFX_UARTE_ENABLED 1 #define NRFX_UARTE_ENABLED 1
#define NRFX_UARTE0_ENABLED 1 #define NRFX_UARTE0_ENABLED 1
#endif
#define NRFX_UART_DEFAULT_CONFIG_IRQ_PRIORITY 7
#define NRFX_UART_DEFAULT_CONFIG_HWFC NRF_UART_HWFC_DISABLED
#define NRFX_UART_DEFAULT_CONFIG_PARITY NRF_UART_PARITY_EXCLUDED
#define NRFX_UART_DEFAULT_CONFIG_BAUDRATE NRF_UART_BAUDRATE_115200
// PWM // PWM
#define NRFX_PWM0_ENABLED 1 #define NRFX_PWM0_ENABLED 1

View File

@ -24,19 +24,19 @@
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#include "mphalport.h" #include "py/mphal.h"
#if MICROPY_PY_BLE_NUS #if MICROPY_PY_BLE_NUS
#include "ble_uart.h" #include "ble_uart.h"
#else #else
#include "nrf_gpio.h" #include "nrf_gpio.h"
#include "nrfx_uarte.h"
#endif #endif
#if !defined( NRF52840_XXAA) #if !defined(NRF52840_XXAA)
#define INST_NO 0 uint8_t serial_received_char;
nrfx_uarte_t serial_instance = NRFX_UARTE_INSTANCE(0);
nrfx_uart_t serial_instance = NRFX_UART_INSTANCE(INST_NO);
void serial_init(void) { void serial_init(void) {
#if MICROPY_PY_BLE_NUS #if MICROPY_PY_BLE_NUS
@ -45,22 +45,27 @@ void serial_init(void) {
; ;
} }
#else #else
nrfx_uart_config_t config = NRFX_UART_DEFAULT_CONFIG; nrfx_uarte_config_t config = {
config.pseltxd = MICROPY_HW_UART_TX; .pseltxd = MICROPY_HW_UART_TX,
config.pselrxd = MICROPY_HW_UART_RX; .pselrxd = MICROPY_HW_UART_RX,
config.hwfc = MICROPY_HW_UART_HWFC ? NRF_UART_HWFC_ENABLED : NRF_UART_HWFC_DISABLED; .pselcts = NRF_UARTE_PSEL_DISCONNECTED,
#ifdef MICROPY_HW_UART_CTS .pselrts = NRF_UARTE_PSEL_DISCONNECTED,
config.pselcts = MICROPY_HW_UART_CTS; .p_context = NULL,
#endif .hwfc = NRF_UARTE_HWFC_DISABLED,
#ifdef MICROPY_HW_UART_RTS .parity = NRF_UARTE_PARITY_EXCLUDED,
config.pselrts = MICROPY_HW_UART_RTS; .baudrate = NRF_UARTE_BAUDRATE_115200,
#endif .interrupt_priority = 7
};
const nrfx_err_t err = nrfx_uart_init(&serial_instance, &config, NULL); nrfx_uarte_uninit(&serial_instance);
if (err == NRFX_SUCCESS) const nrfx_err_t err = nrfx_uarte_init(&serial_instance, &config, NULL); // no callback for blocking mode
if (err != NRFX_SUCCESS) {
NRFX_ASSERT(err); NRFX_ASSERT(err);
}
nrfx_uart_rx_enable(&serial_instance); // enabled receiving
nrf_uarte_task_trigger(serial_instance.p_reg, NRF_UARTE_TASK_STARTRX);
#endif #endif
} }