Skip to content

Commit d9c05c5

Browse files
committed
CP console over UART doesn't work
1 parent 88e50b8 commit d9c05c5

File tree

14 files changed

+81
-95
lines changed

14 files changed

+81
-95
lines changed

.gitmodules

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@
6363
[submodule "frozen/Adafruit_CircuitPython_Crickit"]
6464
path = frozen/Adafruit_CircuitPython_Crickit
6565
url = https://github.com/adafruit/Adafruit_CircuitPython_Crickit
66-
[submodule "ports/nordic/nrfx"]
67-
path = ports/nordic/nrfx
68-
url = https://github.com/adafruit/nrfx.git
6966
[submodule "lib/tinyusb"]
7067
path = lib/tinyusb
7168
url = https://github.com/hathach/tinyusb.git

boards/pca10056/mpconfigboard.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
#pragma once
88

9-
#include "nrfx/hal/nrf_gpio.h"
10-
119
#define MICROPY_HW_BOARD_NAME "PCA10056 nRF52840-DK"
1210
#define MICROPY_HW_MCU_NAME "nRF52840"
1311

@@ -23,6 +21,9 @@
2321
#define DEFAULT_UART_BUS_RX (&pin_P1_01)
2422
#define DEFAULT_UART_BUS_TX (&pin_P1_02)
2523

24+
#define CIRCUITPY_CONSOLE_UART_TX (&pin_P0_06)
25+
#define CIRCUITPY_CONSOLE_UART_RX (&pin_P0_08)
26+
2627
// Flash operation mode is determined by MICROPY_QSPI_DATAn pin configuration.
2728
// A pin config is valid if it is defined and its value is not 0xFF.
2829
// Quad mode: If all DATA0 --> DATA3 are valid

main.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -997,20 +997,19 @@ int __attribute__((used)) main(void) {
997997
common_hal_nvm_bytearray_set_bytes(&common_hal_mcu_nvm_obj, 0, &value_out, 1);
998998
#endif
999999

1000+
printk("serial_early_init\r\n");
1001+
10001002
// Start the debug serial
10011003
serial_early_init();
1002-
console_write(NULL, "serial_early_init\r\n", strlen("serial_early_init\r\n"));
10031004

10041005
mp_hal_stdout_tx_str(line_clear);
10051006

10061007
// Wait briefly to give a reset window where we'll enter safe mode after the reset.
10071008
if (get_safe_mode() == SAFE_MODE_NONE) {
1008-
console_write(NULL, "wait_for_safe_mode_reset\r\n", strlen("wait_for_safe_mode_reset\r\n"));
10091009
set_safe_mode(wait_for_safe_mode_reset());
10101010
}
10111011
printk("safe_mode: %d\r\n", get_safe_mode());
10121012

1013-
console_write(NULL, "stack_init\r\n", strlen("stack_init\r\n"));
10141013

10151014
stack_init();
10161015

ports/nordic/common-hal/busio/I2C.c

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

1717
#include "nrfx_twim.h"
1818
#include "nrfx_spim.h"
19-
#include "nrf_gpio.h"
2019

2120
// all TWI instances have the same max size
2221
// 16 bits for 840, 10 bits for 810, 8 bits for 832

ports/nordic/common-hal/busio/SPI.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include "py/runtime.h"
1313

1414
#include "nrfx_spim.h"
15-
#include "nrf_gpio.h"
1615

1716
#ifndef NRFX_SPIM3_ENABLED
1817
#define NRFX_SPIM3_ENABLED (0)
@@ -63,7 +62,7 @@ static bool never_reset[MP_ARRAY_SIZE(spim_peripherals)];
6362

6463
// Separate RAM area for SPIM3 transmit buffer to avoid SPIM3 hardware errata.
6564
// https://infocenter.nordicsemi.com/index.jsp?topic=%2Ferrata_nRF52840_Rev2%2FERR%2FnRF52840%2FRev2%2Flatest%2Fanomaly_840_198.html
66-
static uint8_t *spim3_transmit_buffer = (uint8_t *)SPIM3_BUFFER_RAM_START_ADDR;
65+
// static uint8_t *spim3_transmit_buffer = (uint8_t *)SPIM3_BUFFER_RAM_START_ADDR;
6766

6867
void spi_reset(void) {
6968
for (size_t i = 0; i < MP_ARRAY_SIZE(spim_peripherals); i++) {
@@ -142,8 +141,8 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self, const mcu_pin_obj_t *
142141
mp_raise_ValueError(MP_ERROR_TEXT("All SPI peripherals are in use"));
143142
}
144143

145-
nrfx_spim_config_t config = NRFX_SPIM_DEFAULT_CONFIG(NRFX_SPIM_PIN_NOT_USED, NRFX_SPIM_PIN_NOT_USED,
146-
NRFX_SPIM_PIN_NOT_USED, NRFX_SPIM_PIN_NOT_USED);
144+
nrfx_spim_config_t config = NRFX_SPIM_DEFAULT_CONFIG(NRF_SPIM_PIN_NOT_CONNECTED, NRF_SPIM_PIN_NOT_CONNECTED,
145+
NRF_SPIM_PIN_NOT_CONNECTED, NRF_SPIM_PIN_NOT_CONNECTED);
147146

148147
config.frequency = baudrate_to_spim_frequency(self->spim_peripheral->max_frequency);
149148

@@ -241,11 +240,11 @@ bool common_hal_busio_spi_write(busio_spi_obj_t *self, const uint8_t *data, size
241240
while (len > 0) {
242241
size_t chunk_size = MIN(len, self->spim_peripheral->max_xfer_size);
243242
uint8_t *chunk = next_chunk;
244-
if (is_spim3) {
245-
// If SPIM3, copy into unused RAM block, and do DMA from there.
246-
memcpy(spim3_transmit_buffer, chunk, chunk_size);
247-
chunk = spim3_transmit_buffer;
248-
}
243+
// if (is_spim3) {
244+
// // If SPIM3, copy into unused RAM block, and do DMA from there.
245+
// memcpy(spim3_transmit_buffer, chunk, chunk_size);
246+
// chunk = spim3_transmit_buffer;
247+
// }
249248
const nrfx_spim_xfer_desc_t xfer = NRFX_SPIM_XFER_TX(chunk, chunk_size);
250249
if (nrfx_spim_xfer(&self->spim_peripheral->spim, &xfer, 0) != NRFX_SUCCESS) {
251250
return false;
@@ -269,11 +268,11 @@ bool common_hal_busio_spi_transfer(busio_spi_obj_t *self, const uint8_t *data_ou
269268
while (len > 0) {
270269
const uint8_t *chunk_out = next_chunk_out;
271270
size_t chunk_size = MIN(len, self->spim_peripheral->max_xfer_size);
272-
if (is_spim3) {
273-
// If SPIM3, copy into unused RAM block, and do DMA from there.
274-
memcpy(spim3_transmit_buffer, chunk_out, chunk_size);
275-
chunk_out = spim3_transmit_buffer;
276-
}
271+
// if (is_spim3) {
272+
// // If SPIM3, copy into unused RAM block, and do DMA from there.
273+
// memcpy(spim3_transmit_buffer, chunk_out, chunk_size);
274+
// chunk_out = spim3_transmit_buffer;
275+
// }
277276
const nrfx_spim_xfer_desc_t xfer =
278277
NRFX_SPIM_SINGLE_XFER(next_chunk_out, chunk_size,
279278
next_chunk_in, chunk_size);

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

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
#include "py/stream.h"
1616

1717
#include "nrfx_uarte.h"
18-
#include "nrf_gpio.h"
18+
#include <stdatomic.h>
1919
#include <string.h>
2020

2121
// expression to examine, and return value in case of failing
2222
#define _VERIFY_ERR(_exp) \
2323
do { \
2424
uint32_t _err = (_exp); \
25-
if (NRFX_SUCCESS != _err) { \
25+
while (NRFX_SUCCESS != _err) { \
2626
mp_raise_msg_varg(&mp_type_RuntimeError, MP_ERROR_TEXT("error = 0x%08lX"), _err); \
2727
} \
2828
} while (0)
@@ -36,6 +36,17 @@ static nrfx_uarte_t nrfx_uartes[] = {
3636
#endif
3737
};
3838

39+
static void *irq_handlers[] = {
40+
#if NRFX_CHECK(NRFX_UARTE0_ENABLED)
41+
NRFX_UARTE_INST_HANDLER_GET(0),
42+
#endif
43+
#if NRFX_CHECK(NRFX_UARTE1_ENABLED)
44+
NRFX_UARTE_INST_HANDLER_GET(1),
45+
#endif
46+
};
47+
48+
49+
3950
static bool never_reset[NRFX_UARTE0_ENABLED + NRFX_UARTE1_ENABLED];
4051

4152
static uint32_t get_nrf_baud(uint32_t baudrate) {
@@ -80,8 +91,8 @@ static void uart_callback_irq(const nrfx_uarte_event_t *event, void *context) {
8091

8192
switch (event->type) {
8293
case NRFX_UARTE_EVT_RX_DONE:
83-
if (ringbuf_num_empty(&self->ringbuf) >= event->data.rxtx.bytes) {
84-
ringbuf_put_n(&self->ringbuf, event->data.rxtx.p_data, event->data.rxtx.bytes);
94+
if (ringbuf_num_empty(&self->ringbuf) >= event->data.rx.length) {
95+
ringbuf_put_n(&self->ringbuf, event->data.rx.p_buffer, event->data.rx.length);
8596
// keep receiving
8697
(void)nrfx_uarte_rx(self->uarte, &self->rx_char, 1);
8798
} else {
@@ -100,7 +111,7 @@ static void uart_callback_irq(const nrfx_uarte_event_t *event, void *context) {
100111
// Possible Error source is Overrun, Parity, Framing, Break
101112
// uint32_t errsrc = event->data.error.error_mask;
102113

103-
ringbuf_put_n(&self->ringbuf, event->data.error.rxtx.p_data, event->data.error.rxtx.bytes);
114+
ringbuf_put_n(&self->ringbuf, event->data.error.rx.p_buffer, event->data.error.rx.length);
104115

105116
// Keep receiving
106117
(void)nrfx_uarte_rx(self->uarte, &self->rx_char, 1);
@@ -151,9 +162,11 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
151162

152163
// Find a free UART peripheral.
153164
self->uarte = NULL;
165+
void *handler = NULL;
154166
for (size_t i = 0; i < MP_ARRAY_SIZE(nrfx_uartes); i++) {
155167
if ((nrfx_uartes[i].p_reg->ENABLE & UARTE_ENABLE_ENABLE_Msk) == 0) {
156168
self->uarte = &nrfx_uartes[i];
169+
handler = irq_handlers[i];
157170
break;
158171
}
159172
}
@@ -173,19 +186,25 @@ void common_hal_busio_uart_construct(busio_uart_obj_t *self,
173186
bool hwfc = rts != NULL || cts != NULL;
174187

175188
nrfx_uarte_config_t config = {
176-
.pseltxd = (tx == NULL) ? NRF_UARTE_PSEL_DISCONNECTED : tx->number,
177-
.pselrxd = (rx == NULL) ? NRF_UARTE_PSEL_DISCONNECTED : rx->number,
178-
.pselcts = (cts == NULL) ? NRF_UARTE_PSEL_DISCONNECTED : cts->number,
179-
.pselrts = (rts == NULL) ? NRF_UARTE_PSEL_DISCONNECTED : rts->number,
189+
.txd_pin = (tx == NULL) ? NRF_UARTE_PSEL_DISCONNECTED : tx->number,
190+
.rxd_pin = (rx == NULL) ? NRF_UARTE_PSEL_DISCONNECTED : rx->number,
191+
.cts_pin = (cts == NULL) ? NRF_UARTE_PSEL_DISCONNECTED : cts->number,
192+
.rts_pin = (rts == NULL) ? NRF_UARTE_PSEL_DISCONNECTED : rts->number,
193+
.skip_gpio_cfg = false,
194+
.skip_psel_cfg = false,
180195
.p_context = self,
181196
.baudrate = get_nrf_baud(baudrate),
182197
.interrupt_priority = NRFX_UARTE_DEFAULT_CONFIG_IRQ_PRIORITY,
183-
.hal_cfg = {
198+
.config = {
184199
.hwfc = hwfc ? NRF_UARTE_HWFC_ENABLED : NRF_UARTE_HWFC_DISABLED,
185200
.parity = (parity == BUSIO_UART_PARITY_NONE) ? NRF_UARTE_PARITY_EXCLUDED : NRF_UARTE_PARITY_INCLUDED
186201
}
187202
};
188203

204+
// Manually connect the UART IRQ through Zephyr.
205+
irq_connect_dynamic(nrfx_get_irq_number(self->uarte->p_reg), NRFX_UARTE_DEFAULT_CONFIG_IRQ_PRIORITY,
206+
nrfx_isr, handler, 0);
207+
189208
_VERIFY_ERR(nrfx_uarte_init(self->uarte, &config, uart_callback_irq));
190209

191210
// Init buffer for rx
@@ -337,7 +356,7 @@ size_t common_hal_busio_uart_write(busio_uart_obj_t *self, const uint8_t *data,
337356
RUN_BACKGROUND_TASKS;
338357
}
339358

340-
(*errcode) = nrfx_uarte_tx(self->uarte, tx_buf, len);
359+
(*errcode) = nrfx_uarte_tx(self->uarte, tx_buf, len, 0);
341360
_VERIFY_ERR(*errcode);
342361
(*errcode) = 0;
343362

ports/nordic/common-hal/microcontroller/Pin.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include "shared-bindings/microcontroller/Pin.h"
88
#include "shared-bindings/digitalio/DigitalInOut.h"
99

10-
#include "nrf_gpio.h"
1110
#include "py/mphal.h"
1211

1312
#include "nrf/pins.h"

ports/nordic/nrfx

Lines changed: 0 additions & 1 deletion
This file was deleted.

ports/nordic/peripherals/nrf/pins.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <stdint.h>
1313
#include <stdbool.h>
1414

15+
#include <hal/nrf_gpio.h>
16+
1517
typedef struct {
1618
mp_obj_base_t base;
1719
// These could be squeezed to fewer bits if more fields are needed.

prj.conf

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
CONFIG_HEAP_MEM_POOL_SIZE=65536
2-
CONFIG_CONSOLE_SUBSYS=y
3-
CONFIG_CONSOLE_GETCHAR=y
42
CONFIG_LOG=y
3+
CONFIG_FLASH=y
4+
CONFIG_FLASH_MAP=y
55

66
CONFIG_RING_BUFFER=y
7+
CONFIG_NRFX_UARTE0=y
8+
CONFIG_NRFX_UARTE1=y
9+
CONFIG_DYNAMIC_INTERRUPTS=y

0 commit comments

Comments
 (0)