SPI problem on Example code

When run, the SPI does erratic outputs... Eg SCK only does 1 or 2 cycles, but MOSI does bit-changing, and SS is Low.
This problem is present whenever I choose a library example sketch, which includes code to talk on the SPI interface. Eg DigitalPotControl

While I can write code that works the SPI correctly, none of the supplied examples will work with the SPI interface.
They all have the same result as shown.
Please HELP !

Please see attached pics and code.

StartUp.png - has the 1.5 second delay and 3 pulses
T0_init.png - has the init function.
T1.png - is the first data message
T2.png - is the 2nd data message

Also,
Q1) why is there a 1.5 second delay when the CPU is coming out of RESET ? (on ALL code)
Q2) why is there 3 pulses on the SCK pin when released from RESET ?

My Stats

BN: Arduino Uno
VID: 0x2341
PID: 0x0043

IDE: V2.0.0

DigitalPotControl.ino (2.3 KB)




This could be the bootloader. It also flashes the built in led on the Uno which is attached to pin 13 / SCK

Does the logic analyser recognise and interpret the SPI data stream is you select the appropriate analyse function ?

Thanks for the quick response.

Can the bootloader startup be avoided?
Yes the analyser will decode, but cannot make sense of this.
I will send trace of good spi I wrote from scratch tomorrow.
The question is what is going wrong?

You have also connected a ground wire on your logic analyser to the Arduino ? That could be one source of inexplicable results.
I'd but some delay statements in the code so there is a clear distinction between (a) the bootloader activity on SCK, (b) the initialisation of SPI and the enable pin, and (c) the individual cycles of connecting to the digital pot over SPI.

Hello 6v6gt (that’s a strange name :wink:)

I have attached some tests on the SPI interface.
HelloWorld.ino_0 raw SPI commands – works Ok – see HW_SPI-0.png and HW-SPI-01.png
HelloWorld.ino_begin library SPI commands – FAILS ! – see HW_SPI-begin.png
HelloWorld.ino_begin_trans library SPI commands with BeginTransaction call – works Ok – see HW_SPI-begin_trans.png

It seems that the SPI can not operate when SPRn bits (in SPCR) are 00 or 01. Must be 10 or 11 to work !?
My cpu clock is 16MHz.

The speed value in SPISettings does Not change the baud rate…why not ?

What is the correct way to set the baud rate and modes etc for SPI ?
Ie how to set variables that are defined in the librarys.
( the only way I can see is to hack the library files…not a good idea as this affects ALL programs that use librarys. )

Thanks.
-Colin

HelloWorld.zip (150 KB)

The problem is that you have configured your logic analyser with a data rate which is too low for the default SPI settings. This below while running your code is 25MHz. The SPI clock is 4MHz and your Logic Analyser appears to be set at 500KHz. What Logic Analyser are you using, incidentally.

Your settings:

image

EDIT
In the meantime I've found something which looks like your logic analyser (at least it appears to have the same model number): Kingst Electronics

Hi,
Can you please post your code?
Can you please post a circuit diagram?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

The problem is not me or my analyser ! (Kingst LA1016 which I have been using successfully for over 2 years ! )

Yesterdays email should have proved that, which I spent HOURS preparing, only to have you say my test is flawed.

Please concentrate on the problem, not the testing methods…

After much testing, I found the issue is around the speed of the SPI.

When SPCR has bits SPR1 & SPR0 set to 0b00 or 0b01 then the SPI fails to run.

When SPCR has bits SPR1 & SPR0 set to 0b10 or 0b11 the SPI works correctly !

However, the speed of the SPI does not change from 250Khz no matter what the settings are.

What is the preferred method to set the SPI clock speed ?

FYI

I also made changes to SPI.h:158

spcr = _BV(SPE) | _BV(MSTR) | ((bitOrder == LSBFIRST) ? _BV(DORD) : 0) |

(dataMode & SPI_MODE_MASK) | ((clockDiv >>1) & SPI_CLOCK_MASK);

I changed the highlighted part in the above line too:- (clockDiv & SPI_CLOCK_MASK);

What is the purpose of the shift ? ( it completely changes the clock calculations before it…)

~WRD0000.jpg

Hello Tom

Here is a copy of the programs I sent to 6v6gt yesterday in this email thread.

(didn’t you receive this ?)

The device is a generic UNO with mega328P and 16MHz xtal.

(FYI it has a led connected to pin13 SCK)

I can write SPI code that works.

I cannot get the supplied examples to run, they all have the same broken response when built.

Eg DigitalPotControl

Whenever I try to use the SPI with github supplied code, it fails. Eg CanBus shield, RadioHead-RF95, etc

Why is my Arduino so different from the rest of the world, which (I assume) can run these programs ?

~WRD0000.jpg

HelloWorld.zip (150 KB)

Hi,
Can you please post your code in code tags and your schematic as a jpg?
Its not encouraged to sent files zipped up, some platforms have problems opening them for a start.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

I don’t have a schematic. Arduino is listed as “UNO R3 CH340G+MEGA328P Chip 16Mhz“

I try to post the code using code tags.

Colin McDonough
Digital Switching Systems Aust
Ph 07-3290-4115
Mb 0409-600-773

~WRD0000.jpg

posted some pics and diagrams

HelloWorld.ino (1.8 KB)
HelloWorld_0.ino (1.7 KB)
HelloWorld_begin.ino (1.7 KB)
HelloWorld_begin_trans.ino (1.7 KB)




Found this schematic, which seems to be accurate to my board.

My approach was, I believe, perfectly reasonable.
You have presented a concrete failure case which I attempted to duplicate. I used my logic analyser but could not repeat your error so I looked further. Clear from you screen shots was that the sampling frequency was far too low at 500KHz for a signal clocked at 4MHz. So your testing method is flawed and I expect you correct your error and repeat the tests. This because it is difficult to have confidence in the rest of your findings, each of which you supported with logic analyser traces, and may, for the same reason, be equally flawed. Further, it would be unusual (but not impossible) to find a fundamental error in a mature library like SPI.

You should find information about SPI configuration settings here: SPI - Arduino Reference

Also here: SPI Register Description : Arduino / ATmega328p - Arnab Kumar Das

Hacking the SPI library is a bad idea.

If you still believe there is a specific error in the SPI library then post a minimal-reproducible code sample together with a statement of expected and actual results.

PS
This problem first started when I tried to build a Mega2560 and CANbus shield, which has the same SPI problem. I was using IDE 1.8.x and was hoping updating to IDE v2.0 may fix things, but it didnt.

Actually it was my fault...
I hadn't changed that setting for ages and after checking was. v embarresed.

So apologies to you and forum :frowning:

If you can, please delete this whole topic.
Thanx for being observant.

~WRD0000.jpg

Hi,

Sorry, you seem to have misunderstood, can you post a schematic of how you have connected the controller to the rest of your hardware, the power supply and your SPI connections.
Not the controller schematic itself.

PLEASE post your code.
To add code please click this link;

Tom... :smiley: :+1: :coffee: :australia:

While wearing sackcloth and ashes, look up the Nyquist–Shannon sampling theorem - Wikipedia.
tl;dr The sampling frequency must be at least twice the signal frequency.

Oh, and delete post #11

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.