Go Down

Topic: Converting SPI code from an UNO to the Due (Read 404 times) previous topic - next topic

fishbulb

Hello friends,

I am trying to get some code to work on my new Due but I am hitting a wall. I think this is likely straightforward and so I thought I would see if someone can explain it to me.

I have this running nicely on my Uno.

http://www.instructables.com/id/Arduino-Tutorial-ADNS-9800-Laser-Mouse-Traveled-Di/

It basically allows me to access the distance traveled from an optical mouse chip. Obviously it works through the SPI protocol. The nice thing about the tutorial is that it also has a Fritzing layout of the hardware wiring diagram (I am ignoring the display and just using the serial monitor for now). I think transferring it from the Uno to the Due is given me trouble both in tweaking the code and where to send the chip info to.

Here's what I had on the Uno.

Slave Select to Pin 10
Mosi to Pin 11
SK (clock) to pin 13
Miso to pin 12
Motion (MOT) to pin 2
Voltage in (5V) to +5V pin
Analog Ground to GND
Digital Ground to GND



On the Due I am making most of the connections to SPI header Just to the left of the Atmel chip. I found this confusing as the Due documentation said to place them on the header with ICSP written below it.  

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

Nevertheless, I put them on SPI. When viewed such that the word SPI is normally oriented, I placed the wires on the following pins.

Upper left Pin = MISO
Upper Right Pin = Voltage In (VI)
Middle Left pin = SC
Middle Right pin = MOSI
Lower Right Pin = DG (Digital Ground)

I then placed the analog ground to a random ground on the board.
I placed the motion pin from the chip to pin 2 on the Due because that's where it was on the Uno. Does this seem right?
I placed the SS of slave select on pin 10 because that's where it was on the Uno.

Given that I tried to keep things similar between the two boards, should this work to run the same code. The behavior that I get now is that it automatically increase the x and y displacement infinitely. So it does not appear to be behaving as it did on the UNO.

The code is un-altered from the link above. I tried to insert it here but it exceed the character limit. I can post it separately if that helps.

Thank you so much for any help as it is obviously much appreciated!
FB

Magician

I noticed +5V, DUE is 3.3V devise. Make sure you don't apply more than 3.3, it's not tolerant.  Have a data spec. of the chip?

fishbulb

Hi Magician,

Thanks for the help. The chip and data sheet can both be found here.

https://www.tindie.com/products/jkicklighter/adns-9800-optical-laser-sensor/

I believe the chip by default operates at 3.3V. To make it work on the Uno, I had to convert it to 5V by cutting a couple traces and making a solder bridge elsewhere. To use it at 3.3V, my understanding is that I didn't need to modify it at all and thus I just wired it as is.

Thanks again and let me know if there's anything else I can add to clarify things.

Best,
FB

Magician

It's better if you post your altered code.
Code: [Select]
  attachInterrupt(0, UpdatePointer, FALLING);
 
  SPI.begin();
  SPI.setDataMode(SPI_MODE3);
  SPI.setBitOrder(MSBFIRST);
  SPI.setClockDivider(8);

Brief checking with data sheet,  2 MHz clock maximum, I'd set SPI.setClockDivider at least 42, DUE running at 84 MHz.
And also, interrupt 0, I know on UNO it's pin 2 , but I'm not sure if it same on DUE.

fishbulb

Thank you again Magician. The code I am using is below.

Code: [Select]
#include <SPI.h>
#include <avr/pgmspace.h>

// Registers
#define REG_Product_ID                           0x00
#define REG_Revision_ID                          0x01
#define REG_Motion                               0x02
#define REG_Delta_X_L                            0x03
#define REG_Delta_X_H                            0x04
#define REG_Delta_Y_L                            0x05
#define REG_Delta_Y_H                            0x06
#define REG_SQUAL                                0x07
#define REG_Pixel_Sum                            0x08
#define REG_Maximum_Pixel                        0x09
#define REG_Minimum_Pixel                        0x0a
#define REG_Shutter_Lower                        0x0b
#define REG_Shutter_Upper                        0x0c
#define REG_Frame_Period_Lower                   0x0d
#define REG_Frame_Period_Upper                   0x0e
#define REG_Configuration_I                      0x0f
#define REG_Configuration_II                     0x10
#define REG_Frame_Capture                        0x12
#define REG_SROM_Enable                          0x13
#define REG_Run_Downshift                        0x14
#define REG_Rest1_Rate                           0x15
#define REG_Rest1_Downshift                      0x16
#define REG_Rest2_Rate                           0x17
#define REG_Rest2_Downshift                      0x18
#define REG_Rest3_Rate                           0x19
#define REG_Frame_Period_Max_Bound_Lower         0x1a
#define REG_Frame_Period_Max_Bound_Upper         0x1b
#define REG_Frame_Period_Min_Bound_Lower         0x1c
#define REG_Frame_Period_Min_Bound_Upper         0x1d
#define REG_Shutter_Max_Bound_Lower              0x1e
#define REG_Shutter_Max_Bound_Upper              0x1f
#define REG_LASER_CTRL0                          0x20
#define REG_Observation                          0x24
#define REG_Data_Out_Lower                       0x25
#define REG_Data_Out_Upper                       0x26
#define REG_SROM_ID                              0x2a
#define REG_Lift_Detection_Thr                   0x2e
#define REG_Configuration_V                      0x2f
#define REG_Configuration_IV                     0x39
#define REG_Power_Up_Reset                       0x3a
#define REG_Shutdown                             0x3b
#define REG_Inverse_Product_ID                   0x3f
#define REG_Motion_Burst                         0x50
#define REG_SROM_Load_Burst                      0x62
#define REG_Pixel_Burst                          0x64

byte initComplete=0;
byte testctr=0;
unsigned long currTime;
unsigned long timer;
unsigned long pollTimer;
volatile int xydat[2];
volatile byte movementflag=0;
const int ncs = 10;

extern const unsigned short firmware_length;
extern prog_uchar firmware_data[];

void setup() {
  Serial.begin(9600);
 
  pinMode (ncs, OUTPUT);
 
  //attachInterrupt(0, UpdatePointer, FALLING);
 
  SPI.begin();
  SPI.setDataMode(SPI_MODE3);
  SPI.setBitOrder(MSBFIRST);
  SPI.setClockDivider(8);

  performStartup(); 
  Serial.println("ADNS9800testPolling");
  dispRegisters();
  delay(5000);
  initComplete=9;

}

void adns_com_begin(){
  digitalWrite(ncs, LOW);
}

void adns_com_end(){
  digitalWrite(ncs, HIGH);
}

byte adns_read_reg(byte reg_addr){
  adns_com_begin();
 
  // send adress of the register, with MSBit = 0 to indicate it's a read
  SPI.transfer(reg_addr & 0x7f );
  delayMicroseconds(100); // tSRAD
  // read data
  byte data = SPI.transfer(0);
 
  delayMicroseconds(1); // tSCLK-NCS for read operation is 120ns
  adns_com_end();
  delayMicroseconds(19); //  tSRW/tSRR (=20us) minus tSCLK-NCS

  return data;
}

void adns_write_reg(byte reg_addr, byte data){
  adns_com_begin();
 
  //send adress of the register, with MSBit = 1 to indicate it's a write
  SPI.transfer(reg_addr | 0x80 );
  //sent data
  SPI.transfer(data);
 
  delayMicroseconds(20); // tSCLK-NCS for write operation
  adns_com_end();
  delayMicroseconds(100); // tSWW/tSWR (=120us) minus tSCLK-NCS. Could be shortened, but is looks like a safe lower bound
}

void adns_upload_firmware(){
  // send the firmware to the chip, cf p.18 of the datasheet
  Serial.println("Uploading firmware...");
  // set the configuration_IV register in 3k firmware mode
  adns_write_reg(REG_Configuration_IV, 0x02); // bit 1 = 1 for 3k mode, other bits are reserved
 
  // write 0x1d in SROM_enable reg for initializing
  adns_write_reg(REG_SROM_Enable, 0x1d);
 
  // wait for more than one frame period
  delay(10); // assume that the frame rate is as low as 100fps... even if it should never be that low
 
  // write 0x18 to SROM_enable to start SROM download
  adns_write_reg(REG_SROM_Enable, 0x18);
 
  // write the SROM file (=firmware data)
  adns_com_begin();
  SPI.transfer(REG_SROM_Load_Burst | 0x80); // write burst destination adress
  delayMicroseconds(15);
 
  // send all bytes of the firmware
  unsigned char c;
  for(int i = 0; i < firmware_length; i++){
    c = (unsigned char)pgm_read_byte(firmware_data + i);
    SPI.transfer(c);
    delayMicroseconds(15);
  }
  adns_com_end();
  }


void performStartup(void){
  adns_com_end(); // ensure that the serial port is reset
  adns_com_begin(); // ensure that the serial port is reset
  adns_com_end(); // ensure that the serial port is reset
  adns_write_reg(REG_Power_Up_Reset, 0x5a); // force reset
  delay(50); // wait for it to reboot
  // read registers 0x02 to 0x06 (and discard the data)
  adns_read_reg(REG_Motion);
  adns_read_reg(REG_Delta_X_L);
  adns_read_reg(REG_Delta_X_H);
  adns_read_reg(REG_Delta_Y_L);
  adns_read_reg(REG_Delta_Y_H);
  // upload the firmware
  adns_upload_firmware();
  delay(10);
  //enable laser(bit 0 = 0b), in normal mode (bits 3,2,1 = 000b)
  // reading the actual value of the register is important because the real
  // default value is different from what is said in the datasheet, and if you
  // change the reserved bytes (like by writing 0x00...) it would not work.
  byte laser_ctrl0 = adns_read_reg(REG_LASER_CTRL0);
  adns_write_reg(REG_LASER_CTRL0, laser_ctrl0 & 0xf0 );
 
  delay(1);

  Serial.println("Optical Chip Initialized");
  }

void UpdatePointer(void){
  if(initComplete==9){

    digitalWrite(ncs,LOW);
    xydat[0] = (int)adns_read_reg(REG_Delta_X_L);
    xydat[1] = (int)adns_read_reg(REG_Delta_Y_L);
    digitalWrite(ncs,HIGH);     

    //movementflag=1;
    }
  }

void dispRegisters(void){
  int oreg[7] = {
    0x00,0x3F,0x2A,0x02  };
  char* oregname[] = {
    "Product_ID","Inverse_Product_ID","SROM_Version","Motion"  };
  byte regres;

  digitalWrite(ncs,LOW);

  int rctr=0;
  for(rctr=0; rctr<4; rctr++){
    SPI.transfer(oreg[rctr]);
    delay(1);
    Serial.println("---");
    Serial.println(oregname[rctr]);
    Serial.println(oreg[rctr],HEX);
    regres = SPI.transfer(0);
    Serial.println(regres,BIN); 
    Serial.println(regres,HEX); 
    delay(1);
  }
  digitalWrite(ncs,HIGH);
}


int convTwosComp(int b){
  //Convert from 2's complement
  if(b & 0x80){
    b = -1 * ((b ^ 0xff) + 1);
    }
  return b;
  }
 
  int xDistance = 0;
  int yDistance = 0;
 
  void loop() {
  currTime = millis();
 
  if(currTime > timer){   
    Serial.println(testctr++);
    timer = currTime + 2000;
    }
   
  if(currTime > pollTimer){
    UpdatePointer();
    xydat[0] = convTwosComp(xydat[0]);
    xydat[1] = convTwosComp(xydat[1]);
      if(xydat[0] != 0 || xydat[1] != 0){
       
        xDistance = xDistance + xydat[0];
        yDistance = yDistance + xydat[1];
       
        Serial.print("x = ");
        Serial.print(xDistance);
        Serial.print(" | ");
        Serial.print("y = ");
        Serial.println(yDistance);
        }
    pollTimer = currTime + 10;
    }
   
  }

Magician

Due attachInterrupt:
Quote
The Arduino Due board has powerful interrupt capabilities that allows you to attach an interrupt function on all available pins. You can directly specify the pin number in attachInterrupt().

http://arduino.cc/en/Reference/attachInterrupt
And,  try to change SPI.setClockDivider(8); to SPI.setClockDivider(42);
Do you have a scope? W/o one debugging interface may be very tricky.

Go Up