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_TX NRF_GPIO_PIN_MAP(0, 6)
#define MICROPY_HW_UART_HWFC (0)
#define PORT_HEAP_SIZE (32 * 1024)
#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 = {
.pseltxd = (tx == mp_const_none) ? NRF_UART_PSEL_DISCONNECTED : tx->number,
.pselrxd = (rx == mp_const_none) ? NRF_UART_PSEL_DISCONNECTED : rx->number,
.pselcts = NRF_UART_PSEL_DISCONNECTED,
.pselrts = NRF_UART_PSEL_DISCONNECTED,
.pseltxd = (tx == mp_const_none) ? NRF_UARTE_PSEL_DISCONNECTED : tx->number,
.pselrxd = (rx == mp_const_none) ? NRF_UARTE_PSEL_DISCONNECTED : rx->number,
.pselcts = NRF_UARTE_PSEL_DISCONNECTED,
.pselrts = NRF_UARTE_PSEL_DISCONNECTED,
.p_context = self,
.hwfc = NRF_UART_HWFC_DISABLED,
.parity = (parity == PARITY_NONE) ? NRF_UART_PARITY_EXCLUDED : NRF_UART_PARITY_INCLUDED,
.hwfc = NRF_UARTE_HWFC_DISABLED,
.parity = (parity == PARITY_NONE) ? NRF_UARTE_PARITY_EXCLUDED : NRF_UARTE_PARITY_INCLUDED,
.baudrate = get_nrf_baud(baudrate),
.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) {
return (nrf_uarte_rx_pin_get(self->uarte.p_reg) == NRF_UART_PSEL_DISCONNECTED) &&
(nrf_uarte_tx_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_UARTE_PSEL_DISCONNECTED);
}
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.
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"));
}
@ -191,7 +191,7 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
// Write characters.
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"));
}

View File

@ -27,36 +27,40 @@
#include <string.h>
#include "mphalport.h"
#include "py/mphal.h"
#include "py/mpstate.h"
#include "py/gc.h"
#if (MICROPY_PY_BLE_NUS == 0)
#if !defined( NRF52840_XXAA)
int mp_hal_stdin_rx_chr(void) {
uint8_t data = 0;
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);
uint8_t data;
nrfx_uarte_rx(&serial_instance, &data, 1);
return data;
}
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) {
if (len == 0)
if (len == 0) {
return;
}
const nrfx_err_t err = nrfx_uart_tx(&serial_instance, (uint8_t*)str, len);
if (err == NRFX_SUCCESS)
NRFX_ASSERT(err);
// EasyDMA can only access SRAM
uint8_t * tx_buf = (uint8_t*) str;
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

View File

@ -31,21 +31,16 @@
#include <stdint.h>
#include "lib/utils/interrupt_char.h"
#include "nrfx_uart.h"
#include "nrfx_uarte.h"
#include "py/mpconfig.h"
extern nrfx_uart_t serial_instance;
extern nrfx_uarte_t serial_instance;
extern volatile uint64_t ticks_ms;
static inline mp_uint_t mp_hal_ticks_ms(void) {
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_ticks_ms() ((mp_uint_t) ticks_ms)
#define mp_hal_delay_us(us) NRFX_DELAY_US((uint32_t) (us))
bool mp_hal_stdin_any(void);
#endif

View File

@ -45,18 +45,8 @@
#define NRFX_TWIM_DEFAULT_CONFIG_HOLD_BUS_UNINIT 0
// UART
#if 0
#define NRFX_UART_ENABLED 1
#define NRFX_UART0_ENABLED 1
#else
#define NRFX_UARTE_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
#define NRFX_PWM0_ENABLED 1

View File

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