Skip to content

Commit 2f0e0bd

Browse files
committed
migrate serial from uart to uarte
1 parent 9017c9d commit 2f0e0bd

File tree

6 files changed

+57
-64
lines changed

6 files changed

+57
-64
lines changed

ports/nrf/boards/feather_nrf52832/mpconfigboard.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
#define MICROPY_HW_UART_RX NRF_GPIO_PIN_MAP(0, 8)
3232
#define MICROPY_HW_UART_TX NRF_GPIO_PIN_MAP(0, 6)
33-
#define MICROPY_HW_UART_HWFC (0)
3433

3534
#define PORT_HEAP_SIZE (32 * 1024)
3635
#define CIRCUITPY_AUTORELOAD_DELAY_MS 500

ports/nrf/common-hal/busio/UART.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,13 @@ void common_hal_busio_uart_construct (busio_uart_obj_t *self,
9191
}
9292

9393
nrfx_uarte_config_t config = {
94-
.pseltxd = (tx == mp_const_none) ? NRF_UART_PSEL_DISCONNECTED : tx->number,
95-
.pselrxd = (rx == mp_const_none) ? NRF_UART_PSEL_DISCONNECTED : rx->number,
96-
.pselcts = NRF_UART_PSEL_DISCONNECTED,
97-
.pselrts = NRF_UART_PSEL_DISCONNECTED,
94+
.pseltxd = (tx == mp_const_none) ? NRF_UARTE_PSEL_DISCONNECTED : tx->number,
95+
.pselrxd = (rx == mp_const_none) ? NRF_UARTE_PSEL_DISCONNECTED : rx->number,
96+
.pselcts = NRF_UARTE_PSEL_DISCONNECTED,
97+
.pselrts = NRF_UARTE_PSEL_DISCONNECTED,
9898
.p_context = self,
99-
.hwfc = NRF_UART_HWFC_DISABLED,
100-
.parity = (parity == PARITY_NONE) ? NRF_UART_PARITY_EXCLUDED : NRF_UART_PARITY_INCLUDED,
99+
.hwfc = NRF_UARTE_HWFC_DISABLED,
100+
.parity = (parity == PARITY_NONE) ? NRF_UARTE_PARITY_EXCLUDED : NRF_UARTE_PARITY_INCLUDED,
101101
.baudrate = get_nrf_baud(baudrate),
102102
.interrupt_priority = 7
103103
};
@@ -132,8 +132,8 @@ void common_hal_busio_uart_construct (busio_uart_obj_t *self,
132132
}
133133

134134
bool common_hal_busio_uart_deinited(busio_uart_obj_t *self) {
135-
return (nrf_uarte_rx_pin_get(self->uarte.p_reg) == NRF_UART_PSEL_DISCONNECTED) &&
136-
(nrf_uarte_tx_pin_get(self->uarte.p_reg) == NRF_UART_PSEL_DISCONNECTED);
135+
return (nrf_uarte_rx_pin_get(self->uarte.p_reg) == NRF_UARTE_PSEL_DISCONNECTED) &&
136+
(nrf_uarte_tx_pin_get(self->uarte.p_reg) == NRF_UARTE_PSEL_DISCONNECTED);
137137
}
138138

139139
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) {
145145

146146
// Read characters.
147147
size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t len, int *errcode) {
148-
if ( nrf_uarte_rx_pin_get(self->uarte.p_reg) == NRF_UART_PSEL_DISCONNECTED ) {
148+
if ( nrf_uarte_rx_pin_get(self->uarte.p_reg) == NRF_UARTE_PSEL_DISCONNECTED ) {
149149
mp_raise_ValueError(translate("No RX pin"));
150150
}
151151

@@ -191,7 +191,7 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t
191191

192192
// Write characters.
193193
size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data, size_t len, int *errcode) {
194-
if ( nrf_uarte_tx_pin_get(self->uarte.p_reg) == NRF_UART_PSEL_DISCONNECTED ) {
194+
if ( nrf_uarte_tx_pin_get(self->uarte.p_reg) == NRF_UARTE_PSEL_DISCONNECTED ) {
195195
mp_raise_ValueError(translate("No TX pin"));
196196
}
197197

ports/nrf/mphalport.c

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,36 +27,40 @@
2727

2828
#include <string.h>
2929

30-
#include "mphalport.h"
30+
#include "py/mphal.h"
3131
#include "py/mpstate.h"
32-
32+
#include "py/gc.h"
3333

3434
#if (MICROPY_PY_BLE_NUS == 0)
3535

3636
#if !defined( NRF52840_XXAA)
3737
int mp_hal_stdin_rx_chr(void) {
38-
uint8_t data = 0;
39-
40-
while (!nrfx_uart_rx_ready(&serial_instance));
41-
42-
const nrfx_err_t err = nrfx_uart_rx(&serial_instance, &data, sizeof(data));
43-
if (err == NRFX_SUCCESS)
44-
NRFX_ASSERT(err);
45-
38+
uint8_t data;
39+
nrfx_uarte_rx(&serial_instance, &data, 1);
4640
return data;
4741
}
4842

4943
bool mp_hal_stdin_any(void) {
50-
return nrfx_uart_rx_ready(&serial_instance);
44+
return nrf_uarte_event_check(serial_instance.p_reg, NRF_UARTE_EVENT_RXDRDY);
5145
}
5246

5347
void mp_hal_stdout_tx_strn(const char *str, mp_uint_t len) {
54-
if (len == 0)
48+
if (len == 0) {
5549
return;
50+
}
5651

57-
const nrfx_err_t err = nrfx_uart_tx(&serial_instance, (uint8_t*)str, len);
58-
if (err == NRFX_SUCCESS)
59-
NRFX_ASSERT(err);
52+
// EasyDMA can only access SRAM
53+
uint8_t * tx_buf = (uint8_t*) str;
54+
if ( !nrfx_is_in_ram(str) ) {
55+
tx_buf = (uint8_t *) gc_alloc(len, false, false);
56+
memcpy(tx_buf, str, len);
57+
}
58+
59+
nrfx_uarte_tx(&serial_instance, tx_buf, len);
60+
61+
if ( !nrfx_is_in_ram(str) ) {
62+
gc_free(tx_buf);
63+
}
6064
}
6165

6266
#else

ports/nrf/mphalport.h

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,16 @@
3131
#include <stdint.h>
3232

3333
#include "lib/utils/interrupt_char.h"
34-
#include "nrfx_uart.h"
34+
#include "nrfx_uarte.h"
3535
#include "py/mpconfig.h"
3636

37-
extern nrfx_uart_t serial_instance;
37+
extern nrfx_uarte_t serial_instance;
3838

3939
extern volatile uint64_t ticks_ms;
4040

41-
static inline mp_uint_t mp_hal_ticks_ms(void) {
42-
return ticks_ms;
43-
}
41+
#define mp_hal_ticks_ms() ((mp_uint_t) ticks_ms)
42+
#define mp_hal_delay_us(us) NRFX_DELAY_US((uint32_t) (us))
4443

45-
int mp_hal_stdin_rx_chr(void);
46-
void mp_hal_stdout_tx_str(const char *str);
4744
bool mp_hal_stdin_any(void);
48-
void mp_hal_delay_ms(mp_uint_t ms);
49-
#define mp_hal_delay_us(us) NRFX_DELAY_US((uint32_t) (us))
5045

5146
#endif

ports/nrf/nrfx_config.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,8 @@
4545
#define NRFX_TWIM_DEFAULT_CONFIG_HOLD_BUS_UNINIT 0
4646

4747
// UART
48-
#if 0
49-
#define NRFX_UART_ENABLED 1
50-
#define NRFX_UART0_ENABLED 1
51-
#else
5248
#define NRFX_UARTE_ENABLED 1
5349
#define NRFX_UARTE0_ENABLED 1
54-
#endif
55-
56-
#define NRFX_UART_DEFAULT_CONFIG_IRQ_PRIORITY 7
57-
#define NRFX_UART_DEFAULT_CONFIG_HWFC NRF_UART_HWFC_DISABLED
58-
#define NRFX_UART_DEFAULT_CONFIG_PARITY NRF_UART_PARITY_EXCLUDED
59-
#define NRFX_UART_DEFAULT_CONFIG_BAUDRATE NRF_UART_BAUDRATE_115200
6050

6151
// PWM
6252
#define NRFX_PWM0_ENABLED 1

ports/nrf/supervisor/serial.c

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include "mphalport.h"
27+
#include "py/mphal.h"
2828

2929
#if MICROPY_PY_BLE_NUS
3030
#include "ble_uart.h"
3131
#else
3232
#include "nrf_gpio.h"
33+
#include "nrfx_uarte.h"
3334
#endif
3435

35-
#if !defined( NRF52840_XXAA)
36+
#if !defined(NRF52840_XXAA)
3637

37-
#define INST_NO 0
38-
39-
nrfx_uart_t serial_instance = NRFX_UART_INSTANCE(INST_NO);
38+
uint8_t serial_received_char;
39+
nrfx_uarte_t serial_instance = NRFX_UARTE_INSTANCE(0);
4040

4141
void serial_init(void) {
4242
#if MICROPY_PY_BLE_NUS
@@ -45,22 +45,27 @@ void serial_init(void) {
4545
;
4646
}
4747
#else
48-
nrfx_uart_config_t config = NRFX_UART_DEFAULT_CONFIG;
49-
config.pseltxd = MICROPY_HW_UART_TX;
50-
config.pselrxd = MICROPY_HW_UART_RX;
51-
config.hwfc = MICROPY_HW_UART_HWFC ? NRF_UART_HWFC_ENABLED : NRF_UART_HWFC_DISABLED;
52-
#ifdef MICROPY_HW_UART_CTS
53-
config.pselcts = MICROPY_HW_UART_CTS;
54-
#endif
55-
#ifdef MICROPY_HW_UART_RTS
56-
config.pselrts = MICROPY_HW_UART_RTS;
57-
#endif
58-
59-
const nrfx_err_t err = nrfx_uart_init(&serial_instance, &config, NULL);
60-
if (err == NRFX_SUCCESS)
48+
nrfx_uarte_config_t config = {
49+
.pseltxd = MICROPY_HW_UART_TX,
50+
.pselrxd = MICROPY_HW_UART_RX,
51+
.pselcts = NRF_UARTE_PSEL_DISCONNECTED,
52+
.pselrts = NRF_UARTE_PSEL_DISCONNECTED,
53+
.p_context = NULL,
54+
.hwfc = NRF_UARTE_HWFC_DISABLED,
55+
.parity = NRF_UARTE_PARITY_EXCLUDED,
56+
.baudrate = NRF_UARTE_BAUDRATE_115200,
57+
.interrupt_priority = 7
58+
};
59+
60+
nrfx_uarte_uninit(&serial_instance);
61+
const nrfx_err_t err = nrfx_uarte_init(&serial_instance, &config, NULL); // no callback for blocking mode
62+
63+
if (err != NRFX_SUCCESS) {
6164
NRFX_ASSERT(err);
65+
}
6266

63-
nrfx_uart_rx_enable(&serial_instance);
67+
// enabled receiving
68+
nrf_uarte_task_trigger(serial_instance.p_reg, NRF_UARTE_TASK_STARTRX);
6469
#endif
6570
}
6671

0 commit comments

Comments
 (0)