Martin Dusek

GB60 SPI problem

Discussion created by Martin Dusek on Jul 25, 2009
Latest reply on Jul 26, 2009 by Martin Dusek

Hello,

this is my interrupt routine I use to communicate in master mode:

 


interrupt VectorNumber_Vspi1 SPIISR(void) {  if(SPI1S_SPTEF && SPI1C1_SPTIE) { // sending data service    if(SPI_STEP_SEND == SPI_START_STATUS) {      SPI_COUNTER_SENT = 0;      SPI_STEP_SEND = SPI_DATA_TRANSMISION_STATUS;    } else {      SPI_COUNTER_SENT++;      }          if(SPI_DATA_DIRECTION) { // SPI is sending data, send our data from SPI_DATA buffer      SPI1S;      SPI1D = SPI_DATA[SPI_COUNTER_SENT];    } else { // SPI is receiving data, send 0xFF      SPI1S;      SPI1D = 0xFF;    }        if(SPI_COUNTER_SENT >= SPI_LENGTH - 1) {      SPI1C1 &= 0b11011111;      SPI_STEP_SEND = SPI_READY_STATUS;    }  }  if(SPI1S_SPRF && SPI1C1_SPIE) { // receiving data service    if(SPI_STEP_RECEIVE == SPI_START_STATUS) {      SPI_COUNTER_RECEIVED = 0;      SPI_STEP_RECEIVE = SPI_DATA_TRANSMISION_STATUS;    } else {      SPI_COUNTER_RECEIVED++;      }        if(!SPI_DATA_DIRECTION) { // SPI is receiving data, store data into the buffer      SPI1S_SPRF;      SPI_DATA[SPI_COUNTER_RECEIVED] = SPI1D;      } else { // SPI is sending data, flush received data      SPI1S_SPRF;      SPI1D;    }        if(SPI_COUNTER_RECEIVED >= SPI_LENGTH - 1) {      SPI1C1 &= 0b01111111;      SPI_STEP_RECEIVE = SPI_READY_STATUS;    }  }}

 

When I send data through SPI it means I send data and flush received data. When I receive data it means I receive data and send 0xFF.

 

I have problem, because data read is not serviced everytime it should be. For example I run function that writes data:

 

 


unsigned char SPIWrite(unsigned char *bufferS, word length) {  if(!SPIWaitForIdle() || !length) {    return 0;  }  SPI_STEP_SEND = SPI_START_STATUS;  SPI_STEP_RECEIVE = SPI_START_STATUS;  SPI_LENGTH = length;  SPI_DATA = bufferS;  SPI_DATA_DIRECTION = 1;  SPI1C1 = 0b11110000;  return 1; }

 

Writing data is OK, but receiving data is never ended (transaction never ends with SPI_STEP_RECEIVE = SPI_READ_STATUS). Data receiving is serviced only for the first time it should be serviced in ISR, no more times.

 

What is wrong with my ISR?

 

Thanks

 

Martin

Outcomes