Skip to content

Commit

Permalink
migrate serial from uart to uarte
Browse files Browse the repository at this point in the history
  • Loading branch information
hathach committed Sep 25, 2018
1 parent 9017c9d commit 2f0e0bd
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 64 deletions.
1 change: 0 additions & 1 deletion ports/nrf/boards/feather_nrf52832/mpconfigboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions ports/nrf/common-hal/busio/UART.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down Expand Up @@ -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) {
Expand All @@ -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"));
}

Expand Down Expand Up @@ -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"));
}

Expand Down
34 changes: 19 additions & 15 deletions ports/nrf/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 4 additions & 9 deletions ports/nrf/mphalport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
#define mp_hal_ticks_ms() ((mp_uint_t) ticks_ms)
#define mp_hal_delay_us(us) NRFX_DELAY_US((uint32_t) (us))

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))

#endif
10 changes: 0 additions & 10 deletions ports/nrf/nrfx_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 24 additions & 19 deletions ports/nrf/supervisor/serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

const nrfx_err_t err = nrfx_uart_init(&serial_instance, &config, NULL);
if (err == NRFX_SUCCESS)
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
};

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
}

Expand Down

0 comments on commit 2f0e0bd

Please sign in to comment.