c - Reading data from a SPI register and storing into array -
i'm trying write code spi slave device reads data miso line.
here register map spi device.

basically code looks @ status register , checks 2 conditions. 
is
rrdybit set? (which indicates there data in rx register)is
roebit not set? (which indicates recieve over-run error has not occured)
if these conditions met put recieved char array buf. 
i sending device characters 'a', 'b' , 'c'.
when recieved 'c' break loop , print out contents of `buf'.
my question is, buf contains 'c', '0', '0'.
is there wrong logic?
char ibuf[32]; int chr; int ptr;  while(1) { //keep checking    //if rrdy set , roe not 1    if (((*spi_status_reg >> 7) & 1) && (!(*spi_status_reg >> 3) & 1)) {      //macro read char rx register      chr = iord_altera_avalon_spi_rxdata(spi_1_slave_base);      ibuf[ptr] = chr; // store     ptr ++;      //if 'c' exit     if(chr == 'c') {       printf("got c\r\n");       break;     }   } }  printf("exit\r\n");  (int i= 0; i<3; i++) {   printf("buffer %x\r\n", ibuf[i]); }      
you have applied boolean not operator (!) incorrectly in second condition in if.
instead of doing !booleanvalue doing !integervalue true time (*spi_status_reg  >> 3) non-zero value.
try !((*spi_status_reg  >> 3) & 1) instead.
(or possibly ~(*spi_status_reg  >> 3) & 1, less readable)
see c not operator applied int? , http://en.cppreference.com/w/c/language/operator_precedence. latter shows ! has greater precedence &, why explicit brackets required.
fyi: shifting 1 better shifting register value because compiler can optimize value, (probably saving constant; e.g. (*spi_status_reg & (1 << 7)) compiler replace (1 << 7) constant 128 @ compile time)
Comments
Post a Comment