Go Down

Topic: Arduino due spi problem and ide1.5.3 problem (Read 1 time) previous topic - next topic


Hello everybody,
yesterday i ran into 2 strange problems
Let me explain

I have 2 as5048a mag encoders which i have installed on my telescope and i use due to control them

seperetly they work fine.

So i deviced a small circuit that plug on the spi header (near the sam) and distributes the signals to and from the encoders

The problem is that the 1st encoder uses as cs pin 4 and the second pin10. when the cs lines are pluged in only the one encoder works(on pin 10)
the other is not. if i switch the other encoder to cs pin10 it works also but not together.

I think this has to do with the extented spi behaviour and the chip select
Can somebody point me to the right direction. below is a copy of my code.
its bad but its on testing yet

Code: [Select]

#include [color=#006699]"SPI.h"[/color]

[color=#7E7E7E]//AS5048A READ DATA PROGRAM[/color]
[color=#7E7E7E]//www.AleyRobotics.com 2013[/color]
# define SPI_NOP 0x0000
# define CLEAR_ERR_FG 0x0001
# define AGC 0x3ffd
# define MAG 0x3ffe
# define ANGLE 0x3fff
# define CMD_READ 0b1000000000000000
# define CMD_WRITE 0b1100000000000000

  [color=#CC6600]unsigned[/color] [color=#CC6600]short[/color] agc1=0;
  [color=#CC6600]unsigned[/color] [color=#CC6600]short[/color] agc2=0;
  [color=#CC6600]unsigned[/color] [color=#CC6600]short[/color] agc=0;
  [color=#CC6600]unsigned[/color] [color=#CC6600]short[/color] mag1=0, mag2=0,mag=0,useless=0;
  [color=#CC6600]unsigned[/color] [color=#CC6600]short[/color] result2= 0;
  [color=#CC6600]unsigned[/color] [color=#CC6600]short[/color] result1 = 0,result3=0,result4=0;
  [color=#CC6600]unsigned[/color] [color=#CC6600]short[/color]  resultA = 0, resultB=0;
  [color=#CC6600]float[/color] angA=0, angB=0;
[color=#CC6600]void[/color] [color=#CC6600][b]setup[/b][/color] ()

[color=#CC6600]pinMode[/color](4,[color=#006699]OUTPUT[/color]);[color=#7E7E7E]//define ss pin as output[/color]
[color=#CC6600]SPI[/color].[color=#CC6600]begin[/color](4);[color=#7E7E7E]// initialize spi bus with pin4 as ss and clock divisor 8 so sclk is 84/8=10.5Mhz[/color]
[color=#CC6600]pinMode[/color](10,[color=#006699]OUTPUT[/color]);[color=#7E7E7E]//define ss pin as output[/color]
[color=#CC6600]SPI[/color].[color=#CC6600]begin[/color](10);[color=#7E7E7E]// initialize spi bus with pin4 as ss and clock divisor 8 so sclk is 84/8=10.5Mhz[/color]
[color=#7E7E7E]//SPI.setClockDivider(8); [/color]


[color=#CC6600]void[/color] [color=#CC6600][b]loop[/b][/color] ()

[color=#CC6600]void[/color] read_angle()

   result1 = [color=#CC6600]SPI[/color].transfer(10,(CMD_READ|ANGLE),[color=#006699]SPI_CONTINUE[/color]);
   result2 = [color=#CC6600]SPI[/color].transfer(10,(CMD_READ|ANGLE),[color=#006699]SPI_CONTINUE[/color]);
 result3 = [color=#CC6600]SPI[/color].transfer(4,(CMD_READ|ANGLE),[color=#006699]SPI_CONTINUE[/color]);
   result4 = [color=#CC6600]SPI[/color].transfer(4,(CMD_READ|ANGLE),SPI_LAST); 
  resultA= result1|result2;[color=#7E7E7E]//original resolution (FULL 14 bit resolution)[/color]


second problem is with the new ide 1.5.3.
My program compiles uploads but it doesnt do nothing. If i comment out the spi initialization at setup serial monitor works
I tried the nightly build but the problem persist.

When i use 1.5.2 it work

I have no idea what is wrong.


Hello alexandrosroidis,

I don't see that you are declaring pins 4 and 10 in your sketch. In order for Due to handle those pins as CS,
you have to initialize them as follows:

Code: [Select]
void setup(){
  // initialize the bus for a device on pin 4
  // initialize the bus for a device on pin 10

For more information about how to set multiple SIP slaves using Arduino Due, please, read the Arduino extended SPI reference for Due. Here the link:


I recommend you to fix your SPI problem first and then try to solve your IDE 1.5.3 later on. Anyway, verify when using IDE 1.5.3 that your selected board is the Due
and not a different one like the UNO. I happened to me that when I upload a new IDE, in my excitement, I use to forget to set the board.



1.5.3 had an issue with the SPI.begin(slaveSelectPin) function, the version with no argument worked fine.

The current nightly builds have this fixed or if you prefer you can add three lines to the begin function in libraries/SPI/arch/sam/SPI.cpp so it looks like this:

Code: [Select]

void SPIClass::begin(uint8_t _pin) {
    uint32_t spiPin = BOARD_PIN_TO_SPI_PIN(_pin);

    SPI_Configure(spi, id, SPI_MR_MSTR | SPI_MR_PS | SPI_MR_MODFDIS);//2

    // Default speed set to 4Mhz
    setClockDivider(_pin, 21);
    setDataMode(_pin, SPI_MODE0);
    setBitOrder(_pin, MSBFIRST);


I solved the problem
It has to do with the predefined cs pins of the due . cs pin4 is the cs0.
so i switch 4 with 52 and everything works ok

I dont know why this is so , and actually i dont care as long as its working like it should
I for the spi extended reference i had already read it obviously to start using the multiple slave mode of the spi
Some people think they are very smart and others not

Anyway thanks for everything

In the nightly mac version donwloaded on 20.sept.13 extended SPI on all three pins (4, 10, 52) now works fine !

Even it took many hours with the logic analyzer to find this thread and fix it ;)

Thanks Arduino team :)


there is an other ugly bug in the SPI library.

Working with 3 different slaves on the predefined CS pins works fine, as long as only ONE of them is used in the sketch.
At the moment of the second SPI.begin(dPOTI_nCS) the DAC_nCS goes low !?! and keeps low,
the following writes to the DAC don't happen, MOSI and CLK are not changed any more.
And during the setup the CS lines are going low too, and are just updated when the first SPI.transfer is initiated.

A bugfix would be great :)

#define DAC_nCS 10
#define dPOTI_nCS 52

    // DAC
    SPI.setClockDivider(DAC_nCS, 32);
    SPI.setBitOrder(DAC_nCS, MSBFIRST);
    SPI.setDataMode(DAC_nCS, SPI_MODE1);
    dacWrite(DAC_cmd_SetupDcenRefRegister, DAC_cannel_ALL, DAC_dcnrefmode_AlloneOn);
    dacWrite(DAC_cmd_PowerDownUp, DAC_cannel_ALL, DAC_powermode_NormalOn);

    // Digital Poti

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131