1

I'm trying to control AD7091-8 using Arduino Due.

This IC calls for 16 bit SPI R/W. I'd read that I can change the transmit and receive bits of Arduino DUE by changing a register (bits 4-7 of SPI_CSR).

I was able to change the SPI of Due from 8 to 16 bits. Confirmed this by looking at SCLK and MOSI in the scope. However, the data I want to transfer is becoming intermittent.

I want to send 0x1000. On 8 bits, using the line

SPI.transfer(10,0x10,SPI_LAST);

I really can see this being sent on MOSI.

However, when I'm on 16 bits, the line

SPI.transfer(10,0x1000,SPI_LAST);

It's either becoming 0x0000 or 0x0008. I'm losing the bit or it's becoming LSB_FIRST. Not MSB_FIRST.

Below is the actual code.

#include <SPI.h>
#include "C:\Users\Smart Surface 2\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.11\system\CMSIS\Device\ATMEL\sam3xa\include\component\component_spi.h"
#include "C:\Users\Smart Surface 2\AppData\Local\Arduino15\packages\arduino\hardware\sam\1.6.11\system\CMSIS\Device\ATMEL\sam3xa\include\instance\instance_spi0.h"

#define CS 10

void setup() {

  pinMode(CONV,OUTPUT);

  Serial.begin(9600);
  SPI.begin(CS);
  SPI.setDataMode(CS, SPI_MODE0);
  SPI.setClockDivider(CS,21);  

  REG_SPI0_WPMR=0x53504900;
  REG_SPI0_CR=0x1;
  SPI0->SPI_CSR[0]=0x00001580; 

  SPI.setBitOrder(MSBFIRST);
  delay(1);
}

void loop() {

  SPI.transfer(CS,0x1000,SPI_LAST);

  int a = SPI.transfer(CS,0x0000, SPI_LAST);
  delay(1000);

  Serial.println(a);
}

How can I make my data reliable and the order still with MSB first?

1 Answer 1

2

Found the problem on the SPI library.

When I do the command SPI.transfer, the data is stored on a byte type variable. And I'm passing a 16 bit data on it.

I've changed line 84 of SPI.h to

uint16_t transfer(byte _pin, uint16_t _data, SPITransferMode _mode = SPI_LAST);

so it will return and receive 16 bit data.

Also have to change the corresponding function on SPI.cpp (line 174)

uint16_t SPIClass::transfer(byte _pin, uint16_t _data, SPITransferMode _mode)

so they will match.

I've copied and renamed the SPI library that I've changed so it won't affect other codes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.