Pages: [1]   Go Down
Author Topic: Arduino due spi problem and ide1.5.3 problem  (Read 802 times)
0 Members and 1 Guest are viewing this topic.
Greece,Crete
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Greece is not for sale
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Quote

Code:
[quote]

#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][b]Serial[/b][/color].[color=#CC6600]begin[/color](19200);

[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]SPI[/color].setDataMode(4,SPI_MODE0);
[color=#CC6600]SPI[/color].setBitOrder([color=#006699]MSBFIRST[/color]);
[color=#CC6600]SPI[/color].[color=#CC6600]setClockDivider[/color](8);
[color=#CC6600]SPI[/color].transfer(4,(CMD_READ|CLEAR_ERR_FG));
[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=#CC6600]SPI[/color].setDataMode(10,SPI_MODE0);
[color=#CC6600]SPI[/color].setBitOrder([color=#006699]MSBFIRST[/color]);
[color=#7E7E7E]//SPI.setClockDivider(8); [/color]
[color=#CC6600]SPI[/color].transfer(10,(CMD_READ|CLEAR_ERR_FG));

[color=#7E7E7E]//SPI.transfer(4,(CMD_READ|SPI_NOP),SPI_CONTINUE);[/color]
}

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




[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); 
 
 
  result1=result1<<8;
  resultA= result1|result2;[color=#7E7E7E]//original resolution (FULL 14 bit resolution)[/color]
  
result3=result3<<8;
resultB=result3|result4;
 
 
 resultA&=0b0011111111111111;
 resultB&=0b0011111111111111;
  [color=#7E7E7E]//result=result>>2;[/color]
  [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]"ANGLE1="[/color]);
  angA=([color=#CC6600]float[/color])(resultA)*0.02197;
  [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color](angA);
  [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]"ANGLE2="[/color]);
  angB=([color=#CC6600]float[/color])(resultB)*0.02197;
  [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color](angB);
  
  [color=#7E7E7E]//Serial1.println(ang);[/color]
 [color=#CC6600]delay[/color](100);
  
}
  [color=#7E7E7E]//-----------[/color]




[/quote]
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.
Logged

Venezuela
Offline Offline
Sr. Member
****
Karma: 14
Posts: 478
Ground.......ground........always ground
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
void setup(){
  // initialize the bus for a device on pin 4
  SPI.begin(4);
  // initialize the bus for a device on pin 10
  SPI.begin(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:

http://arduino.cc/en/Reference/DueExtendedSPI

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.

Regards!
Logged

Nashville Tennessee, USA
Offline Offline
Sr. Member
****
Karma: 15
Posts: 366
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
void SPIClass::begin(uint8_t _pin) {
    uint32_t spiPin = BOARD_PIN_TO_SPI_PIN(_pin);

    initCb();//1
    SPI_Configure(spi, id, SPI_MR_MSTR | SPI_MR_PS | SPI_MR_MODFDIS);//2
    SPI_Enable(spi);//3
    PIO_Configure(
        g_APinDescription[spiPin].pPort,
        g_APinDescription[spiPin].ulPinType,
        g_APinDescription[spiPin].ulPin,
        g_APinDescription[spiPin].ulPinConfiguration);

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

Greece,Crete
Offline Offline
Newbie
*
Karma: 0
Posts: 2
Greece is not for sale
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-wink

Thanks Arduino team smiley
michael
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 8
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

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 smiley


#define DAC_nCS 10
#define dPOTI_nCS 52

    // DAC
    SPI.begin(DAC_nCS);
    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
    delay(1);
    SPI.begin(dPOTI_nCS);
Logged

Pages: [1]   Go Up
Jump to: