nrf52840: no SPI communication when using BAT pin to power

Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
User avatar
RainySun
 
Posts: 73
Joined: Thu Nov 18, 2021 2:55 pm

Re: nrf52840: no SPI communication when using BAT pin to pow

Post by RainySun »

Added feature.
Itsybitsy displays voltage reading on OLED and runs the program generator.
I was able to see the voltage reading on OLED but do not see the output of the function generator when powered using BAT pin.
Connected the itsy bitsy to USB cable, and I was able to read voltage, and see the output of the function generator.

Code: Select all

import time
import board
import displayio
import terminalio
import adafruit_displayio_ssd1306
import cedargrove_ad9833  # Update CS pin, and MCLK
import adafruit_ads1x15.ads1115 as ADS
from adafruit_display_text import label
from adafruit_ads1x15.analog_in import AnalogIn

# from analogio import AnalogIn
from digitalio import DigitalInOut, Direction, Pull

# establish class instance with chip_select pin D6
wave_gen = cedargrove_ad9833.AD9833(select="D7")


# OLED Connection
displayio.release_displays()

# Using I2C connection
i2c = board.I2C()
display_bus = displayio.I2CDisplay(i2c, device_address=0x3C)  # OLED Address
ads = ADS.ADS1115(i2c, address=0x48)  # ADC module ADDRESS

# OLED Dimension
WIDTH = 128
HEIGHT = 32
BORDER = 1

display = adafruit_displayio_ssd1306.SSD1306(display_bus, width=WIDTH, height=HEIGHT)

view = displayio.Group()
display.show(view)

text_area = label.Label(terminalio.FONT, text="", color=0xFFFFFF, x=1, y=10 // 2 - 1)
text1_area = label.Label(terminalio.FONT, text="", color=0xFFFFFF, x=1, y=30 // 2 - 1)

view.append(text_area)
view.append(text1_area)


# Create single-ended input on channel 0
chan = AnalogIn(ads, ADS.P0)
ads.gain = 2 / 3

# Chirp Signal Output
# establish initial parameters
begin_freq = 200000  # fixed or sweep starting frequency (Hz)
end_freq = 250000  # sweep ending freque
inc_freq = 100  # sweep freqency step size (Hz)
wave_type = "sine"  # sine, triangle, or square waveform

wave_gen.reset()  # reset and stop the wave generator; reset all registers
wave_gen.wave_type = wave_type  # load the waveform type value
time.sleep(0.1)

# Initialize Press button to start chirping.
switch = DigitalInOut(board.D2)
switch.direction = Direction.INPUT
switch.pull = Pull.UP

while True:
    # Run Chirp when switch is pressed
    if not switch.value:
        analog_in1 = 0
        FreqResonant = 0
        total = 0
        # analogfinal = 0  # Averaging analog data
        # set begin freq value to reduce start-up noise
        wave_gen.update_freq(begin_freq)
        wave_gen.start()  # start the wave generator
        for i in range(begin_freq, end_freq, inc_freq):
            wave_gen.update_freq(i)  # load the next frequency value
            analog_in2 = chan.voltage
            if analog_in2 > analog_in1:
                analog_in1 = analog_in2
                FreqResonant = i
        wave_gen.stop()  # stop the wave generator
        resistor = 0.1369 * analog_in1 
        text_area.text = "R = " + str(resistor)
        text1_area.text = "V =" + str(analog_in1)
        time.sleep(0.05)
    else:
        text_area.text = "Connected"
        text1_area.text = " "


User avatar
adafruit2
 
Posts: 22148
Joined: Fri Mar 11, 2005 7:36 pm

Re: nrf52840: no SPI communication when using BAT pin to pow

Post by adafruit2 »

oof this is not as easy to debug as we'd like cause the library is out of the circuitpython bundle system - so you'll have to edit the library for the ad833 on your own. we'd start by replacing the SPI bus init with a 'bitbang' SPI bus
it's slower but isn't using the hardware SPI port on the nrf which has been a little finicky
https://docs.circuitpython.org/en/lates ... index.html

User avatar
danhalbert
 
Posts: 4654
Joined: Tue Aug 08, 2017 12:37 pm

Re: nrf52840: no SPI communication when using BAT pin to pow

Post by danhalbert »

We have seen this bug before, in other circumstances: https://github.com/adafruit/circuitpython/issues/5233
Also: https://devzone.nordicsemi.com/f/nordic ... ed-to-vbus

The problem appears to be that the SPIM3 peripheral (one of the SPI peripherals) in the nRF52840 does not work properly if VBUS is not energized when you attempt to use SPIM3. In CIrcuitPython, we allocate SPIM3 first to you when you ask for a `busio.SPI()` object. That's because the SPIM3 peripheral can run at 32 MHz, whereas the other SPIM peripherals can run only at 8 MHz.

The workaround is to allocate a `busio.SPI()` first that you do not use. Pick some pins that you will not be using. I am not sure which pins you are not using, but let's say D0 and D1. Pick other pins if necessary.

Add this line before the ADS instantiation:

Code: Select all

unused_spim3 = busio.SPI(clock=board.D0, MOSI=board.D1)   ## add this line
# Then the ADS instantiation will use some other SPIM.
ads = ADS.ADS1115(i2c, address=0x48)  # ADC module ADDRESS

User avatar
RainySun
 
Posts: 73
Joined: Thu Nov 18, 2021 2:55 pm

Re: nrf52840: no SPI communication when using BAT pin to pow

Post by RainySun »

Hey danhalbert,
Unfortunately, it did not work even after using the unused_simp3 code that you provided; D0 and D1 are unused, so I kept it same.
I was still able to run the signal generator when powered using microUSB but does not work when powered using BAT pin.
I guess I will have to make it permanent to only run when powered using microUSB.
I hope to try Itsybitsy M4, and see if i get the same issue.

User avatar
danhalbert
 
Posts: 4654
Joined: Tue Aug 08, 2017 12:37 pm

Re: nrf52840: no SPI communication when using BAT pin to pow

Post by danhalbert »

The issue is pretty clearly a hardware bug (or at best a quirk) with the nRF52840 chip. No workaround has been suggested by Nordic yet.

Could you upload your code (as a .zip or .txt file is fine)? The unused spim3 thing should work, I believe, so I can double-check you rcode.

User avatar
RainySun
 
Posts: 73
Joined: Thu Nov 18, 2021 2:55 pm

Re: nrf52840: no SPI communication when using BAT pin to pow

Post by RainySun »

Attached is a code.txt file.
Everything works normally when connected to microUSB port.
When powered using BAT pin, only the SPI (signal generator) doesn't work
Attachments
code.txt
(2.99 KiB) Downloaded 6 times

User avatar
danhalbert
 
Posts: 4654
Joined: Tue Aug 08, 2017 12:37 pm

Re: nrf52840: no SPI communication when using BAT pin to pow

Post by danhalbert »

Move this line

Code: Select all

unused_spim3 = busio.SPI(clock=board.D0, MOSI=board.D1)  # add this line
to be before this line:

Code: Select all

wave_gen = cedargrove_ad9833.AD9833(select="D7")
That way the first SPI allocated will be the SPIM3 one.

I misread your program the first time, and said it should go before the I2C line. But that doesn't matter.

User avatar
RainySun
 
Posts: 73
Joined: Thu Nov 18, 2021 2:55 pm

Re: nrf52840: no SPI communication when using BAT pin to pow

Post by RainySun »

That resolved the issue!!!
Thank you DanHalbert :)

Locked
Please be positive and constructive with your questions and comments.

Return to “Itsy Bitsy Boards”