Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++
12590 Discussions

ADXL345(SPI) in Nios II(Cyclone)

RBMK
Beginner
496 Views

im trying to interface the ADXL345(SPI) with Nios II system of my Cyclone board i write something but im stuck, i only read "ff"'s, any can giveme a hand witht that?

This is called the epic bare-bones ADXL345 ISP code for Arduino:

#include <SPI.h>

unsigned char values[10];

void setup() { 
  SPI.begin();
  SPI.setDataMode(SPI_MODE3);
  Serial.begin(9600);

  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);

  digitalWrite(10, LOW);
  SPI.transfer(0x31);
  SPI.transfer(0x01);
  SPI.transfer(0x2D);
  SPI.transfer(0x08);
  digitalWrite(10, HIGH);
}

void loop() {
  digitalWrite(10, LOW);
  SPI.transfer(0x80 | 0x32 | 0x40);

  for(int i = 0; i < 6; i++)
    values[i] = SPI.transfer(0x00);

  digitalWrite(10, HIGH);

  Serial.print(((int)values[1] << 8) | (int)values[0], DEC);
  Serial.print(", ");
  Serial.print(((int)values[3] << 8) | (int)values[2], DEC);
  Serial.print(", ");
  Serial.println(((int)values[5] << 8) | (int)values[4], DEC);      
  delay(10); 
}

I know that it works, i test it. Now, this is what i write for the FPGA:

#include <stdio.h>
#include <stdint.h>
#include "system.h"
#include "altera_avalon_spi.h"
#include "altera_avalon_spi_regs.h"

unsigned char values[10];
int x, y, z;

uint8_t SPI_transfer(uint8_t data) {
    int status;

    IORD_ALTERA_AVALON_SPI_RXDATA(SPI_0_BASE);

    do status = IORD_ALTERA_AVALON_SPI_STATUS(SPI_0_BASE);
    while((status & ALTERA_AVALON_SPI_STATUS_TRDY_MSK) == 0);

    IOWR_ALTERA_AVALON_SPI_TXDATA(SPI_0_BASE, data);

    do status = IORD_ALTERA_AVALON_SPI_STATUS(SPI_0_BASE);
    while((status & ALTERA_AVALON_SPI_STATUS_RRDY_MSK) == 0);

    return IORD_ALTERA_AVALON_SPI_RXDATA(SPI_0_BASE);
}

int main() {
    printf("Nios II ADXL345 SPI\n");

    SPI_transfer(0x31);
    SPI_transfer(0x01);
    SPI_transfer(0x2D);
    SPI_transfer(0x08);

    while(1) {
        SPI_transfer(0x80 | 0x32 | 0x40);

        int i;
        for(i = 0; i < 6; i++)
            values[i] = SPI_transfer(0x00);

        x = ((int)values[1] << 8) | (int)values[0];
        y = ((int)values[3] << 8) | (int)values[2];
        z = ((int)values[5] << 8) | (int)values[4];

        printf("x = %d", x);
        printf("  y = %d", y);
        printf("  z = %d\n", z);

        usleep(1000000);
    }

    return 0;
}

Thats also called the crap that only reads "ff"'s registers. As you can see, i dont handle the SS/CS line there, i belive is done by the SPI_transfer internal functions, i dont have a osiloscope, so the best that i can do is connect a led to ground and each pin, i see that MISO is down most of the time exept for a peek on each cycle, so i suspect that is ok. The SS line is full time on (the led) but since the down times are too short, im not sure if its working or not. Btw working with SCK at 125khz. I also was thinking that can be the IO voltage levels, in Arduino is 5v, here on the FPGA is 3v3. But the datasheet(adxl345) says that there should be no problem also in both cases im powering the sensor with 3.3v. Also, on my SPI component settings (from QSYS) i select the SPI mode 3 , thats what works with Arduino. Any can give me a hand with this?

0 Kudos
1 Reply
EricMunYew_C_Intel
Moderator
457 Views

Can you set the clock phase and polarity to 1. 


0 Kudos
Reply