Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Altera_Forum
Honored Contributor I
841 Views

Nios SPI read

Hi everybody, 

I'm using the alt_avalon_spi_command Altera function to write on a DAC and i have problem when i tried to read what i have wrote before. The DAC requiert 1 instruction byte (r/w+7 adress bit) and 2 byte for write routine. 

 

So when i tried to read in any register i have de same value CBD814(14 is 0x10 + adress) i don't understant what is the problem.  

This is my C code: 

 

 

void write_dac84h84_spi(){ 

int i; 

alt_u8 wdata[49][3]={{0x00,0x01,0x8C},{0x01,0x01,0x00},{0x02,0x00,0x52},{0x03,0xF0,0x01},{0x04,0xFF,0xFF},{0x05,0x86,0x60},{0x06,0x43,0x00},{0x07,0xFF,0xFF},{0x08,0x00,0x00},{0x09,0x80,0x00},{0x0A,0x00,0x00},{0x0B,0x00,0x00},{0x0C,0x05,0xA6},{0x0D,0x05,0xA6},{0x0E,0x05,0xA6},{0x0F,0x05,0xA6}, 

{0x10,0x00,0x00},{0x11,0x00,0x00},{0x12,0x00,0x00},{0x13,0x00,0x00},{0x14,0x00,0x00},{0x15,0x00,0x00},{0x16,0x00,0x00},{0x17,0x00,0x00},{0x18,0x34,0x62},{0x19,0x20,0xF4},{0x1A,0xEC,0x00},{0x1B,0x08,0x00},{0x1C,0x00,0x00},{0x1D,0x00,0x00},{0x1E,0x11,0x88},{0x1F,0x11,0x44},{0x20,0x14,0x00}, 

{0x22,0x1B,0x1B},{0x23,0x00,0x1F},{0x24,0x10,0x00},{0x25,0x7A,0x7A},{0x26,0xB6,0xB6},{0x27,0xEA,0xEA},{0x28,0x45,0x45},{0x29,0x1A,0x1A},{0x2A,0x16,0x16},{0x2B,0xAA,0xAA},{0x2C,0xC6,0xC6},{0x2D,0x00,0x00},{0x2E,0x00,0x00},{0x2F,0x00,0x00},{0x30,0x61,0xA8},{0x7F,0x00,0x01},};  

 

for(i=0;i<=48;i++){  

alt_avalon_spi_command(DAC34_SPI_BASE,0,3,wdata[i],0,NULL,NULL);  

}  

return;} 

 

 

 

 

 

void read_dac84h84_spi(){ 

 

 

alt_u8 adress=0x04; 

alt_u8 rdata[2]; 

 

 

alt_avalon_spi_command(DAC34_SPI_BASE,0,1,(0x10+adress),2,rdata,NULL);  

printf("Registre %d : %X%X\n",adress,rdata); 

return;} 

 

Is there anyone who have successfull read with the function?? Maybe i miss something 

 

Best regards
0 Kudos
5 Replies
Altera_Forum
Honored Contributor I
48 Views

"14 is 0x10..." ? 14 is 0x0E...

Altera_Forum
Honored Contributor I
48 Views

You've probably got a mistake somewhere in all those numbers. You need to specify what DAC chip you are using if anyone is to make sense of it. Try splitting this into multiple lines with full comments documenting the intent of each line. You may find the mistake on your own just by following this method.

Altera_Forum
Honored Contributor I
48 Views

Hi Galfonz, 

I'm using a DAC34H84 of Texas instruments.  

The write spi function does not have any problem. 

{0x00,0x01,0x8C} ===> 0x00 is the instruction byte (w+adress register); 0x01 is the MSB of the 16 bits of data and 0x8C is the LSB. 

So look only this function  

 

void read_dac84h84_spi(){ 

 

 

alt_u8 adress=0x04; 

alt_u8 rdata[2]; 

 

 

alt_avalon_spi_command(DAC34_SPI_BASE,0,1,(0x10+ad ress),2,rdata,NULL); arg0=master spi id, arg1= slave spi id, arg2=write lentgh,arg3=write data pointer,arg4=read lentgh,arg5=read data pointer,arg6=flags 

 

printf("Register %d : %X%X\n",adress,rdata);//  

return;} 

Best regards
Altera_Forum
Honored Contributor I
48 Views

Try: alt_avalon_spi_command(DAC34_SPI_BASE,0,1,(0x80+adress),2,rdata,NULL) 

 

The datasheet for the DAC, Table 1. Instruction Byte of the Serial Interface says: 

Bit7: R/W Identifies the following data transfer cycle as a read or write operation. A high indicates a read 

operation from DAC34H84 and a low indicates a write operation to DAC34H84.
Altera_Forum
Honored Contributor I
48 Views

Hi Rodo 

 

in fact , you are right, is 0x80 but the result is the same, it didn't read the right value but only replace CBD814 to CBD884.  

So this CBD8 remain the same for all the register. 

And i have verify with the DAC34H84 Gui software, and after a write operation, i can read the register value via usb on the computer and there are right.But nothing with the read spi function. 

So i don't know what's wrong!!!!!