Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SPI + Bluetooth crash nRF52840 #1222

Open
urish opened this issue Sep 30, 2018 · 16 comments

Comments

Projects
None yet
5 participants
@urish
Copy link

commented Sep 30, 2018

Calling the write() method of an SPI object while Bluetooth is active crashes the firmware.

Here is a minimal code that reproduces the issue:

import board
import busio
from ubluepy import Peripheral

p = Peripheral()
p.advertise()

spi = busio.SPI(board.P0_22, MOSI=board.P1_00)
while not spi.try_lock():
    pass
spi.write(bytes([0xff]))

If you comment out the last line (the call to spi.write()), then it doesn't crash anymore. Tested on the nRF52840 USB Dongle.

@tannewt tannewt added this to the 4.0.0 - Bluetooth milestone Oct 1, 2018

@jerryneedell

This comment has been minimized.

Copy link
Collaborator

commented Oct 3, 2018

FYI - I can reproduce this on my nrf52840_dongle but it does not fail on a feather_nrf52832

@jerryneedell

This comment has been minimized.

Copy link
Collaborator

commented Oct 6, 2018

A bit more information -- I was able to successfully mount my SDCard -- SPI device -- and then initiate a BLE advertisement and it runs OK - however if I then access the SDCard the system resets as above.

These tests were don an a PCA10056 nRF52840-DK - confirms that the issue occurs on both the Dongle and the DK.

@urish

This comment has been minimized.

Copy link
Author

commented Oct 6, 2018

I am observing the same as @jerryneedell

@urish

This comment has been minimized.

Copy link
Author

commented Oct 6, 2018

Positive outcome from this issue - a blog post describing how to optimize the performance of CircuitPython code:

https://medium.com/@urish/embedded-python-cranking-performance-knob-up-to-eleven-df31a5940a63

@tannewt tannewt modified the milestones: 4.0.0 - Bluetooth, 4.0 Alpha Oct 9, 2018

@dhalbert

This comment has been minimized.

Copy link
Collaborator

commented Oct 10, 2018

I spent some time on this, but didn't discover the root cause. I tried other pins, including ones that are marked as definitely safe for high-speed I/O. It seems like there's a reset when the SPI write is attempted. Nothing appears on the SPI pins.

@jerryneedell

This comment has been minimized.

Copy link
Collaborator

commented Oct 10, 2018

Since it does not occur on the nrf52832, It could be related to the s140 SD or something in the nrf52840 code. Both are differences.

@jerryneedell

This comment has been minimized.

Copy link
Collaborator

commented Oct 11, 2018

just for fun - I tried accessing an I2C device while BLE was active on a PCA10059 Dongle and it worked fine.

@dhalbert

This comment has been minimized.

Copy link
Collaborator

commented Oct 11, 2018

I also tried I2C, and also UART, and neither caused a problem. It seems to be SPI in particular. I looked at the code and I don't see anything obvious. I changed the HardFault_Handler() to hang (as it does in atmel-samd) instead of just returning and maybe I can catch the problem. Will report back.

The SoftDevice documentation describes how it reserves a few peripherals for its own use (like TIMER0) or provides only a restricted API to a shared peripheral, but I don't see any conflicts.

@dhalbert

This comment has been minimized.

Copy link
Collaborator

commented Oct 12, 2018

This BLE + SPIM crash sounds quite similar to this issue: https://devzone.nordicsemi.com/f/nordic-q-a/33982/sdk-15-software-crash-during-spi-session/130572. I'm inquiring.

@jerryneedell

This comment has been minimized.

Copy link
Collaborator

commented Oct 12, 2018

Not the most encouraging response to your inquiry :-(

@arturo182

This comment has been minimized.

Copy link
Collaborator

commented Oct 12, 2018

Have you tried building a DEBUG build, it has asserts enabled, maybe that helps.

@jerryneedell

This comment has been minimized.

Copy link
Collaborator

commented Oct 12, 2018

I disabled SPIM3 in nrfx_config.h and the problem seems to have been resolved. I can now run a blue advertise and then mount my SPI SD Card

diff --git a/ports/nrf/nrfx_config.h b/ports/nrf/nrfx_config.h
index f217cb053..a9bee8912 100644
--- a/ports/nrf/nrfx_config.h
+++ b/ports/nrf/nrfx_config.h
@@ -27,9 +27,9 @@
 
 // Enable SPIM2 and SPIM3 (if available)
 #define NRFX_SPIM2_ENABLED 1
-#ifdef NRF52840_XXAA
-    #define NRFX_SPIM3_ENABLED 1
-#endif
+//#ifdef NRF52840_XXAA
+//    #define NRFX_SPIM3_ENABLED 1
+//#endif

@tannewt

This comment has been minimized.

Copy link
Collaborator

commented Feb 5, 2019

Can we close this?

@dhalbert

This comment has been minimized.

Copy link
Collaborator

commented Feb 5, 2019

Right now we’re not using SPIM3, which is only a workaround. Still to re-test and debug the primary problem.

@tannewt

This comment has been minimized.

Copy link
Collaborator

commented Feb 5, 2019

Ok, I'm moving to 4.x. We don't need it for 4.0.0

@tannewt tannewt modified the milestones: 4.0.0 - Bluetooth, 4.x Feb 5, 2019

@dhalbert

This comment has been minimized.

Copy link
Collaborator

commented Feb 5, 2019

Fine to defer!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.