s12g128 SPI interrupt handler

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

s12g128 SPI interrupt handler

744 Views
sinanatilla
Contributor II

1) I have got a problem with ISP interrupt routine. MCU goes to interrupt routine even if theres no data being receieved.

 

2) And I am sending 1 byte data to the slave then the slave sends 4 byte response, so I miss 3 bytes. How can I get all the data, I want to do this in 8 bit data mode, if I use 16 bit mode then I will miss 2 byte.

 

 

 

 

void interrupt VectorNumber_Vspi0 SPI0_ISR (void) {

 

if(SPI0SR_SPIF==1){ 

       gelen[gel]= SPI0DRL;

       gel++;

       if(gel==5){

        gel=0;

       }

}

}

 

void main(void)

{

    

/* PLL Configuration */

   CPMUCLKS_PSTP   = 0;        

    CPMUCLKS_PLLSEL = 1;          /* Enable the PLL to allow write to divider registers */

    CPMUSYNR        = 0x58;       /* Set the multiplier register */

    CPMUPOSTDIV = 0x00;           /* Set the post divider register */

    CPMUPLL = 0x00;               /* Set the PLL frequency modulation */

    while(!CPMUFLG_LOCK) {        /* Wait until the PLL is within the desired tolerance of the target frequency */

    }

    CPMUPROT=0x00;                /* Enable protection of clock configuration registers */

                                                                               

   

    DDR0AD=0xFF; //Analog ports output

    DDR1AD=0xFF; //Analog ports output

   

    /* Initialise the SPI */

    SPI0CR2 = 0x10;             /* 8-Bit transfer width; SS pin is used by the SPI                          */

    SPI0BR =  0xAA;             /* SPI clock = Bus Clock divided by 2048 = 15.625kHz                        */

    SPI0CR1 = 0x56;             /* SPI enabled; Master mode; Active high clocks; Slave Select output enabled*/

    //SPI0CR1_CPOL =0;  //SPI Clock Polarity Bit

    SPI0CR2_XFRW =1;

    SPI0CR1_LSBFE=0;  //LSB

    SPI0CR1_CPHA =0; //SPI Clock Phase Bit

   

   // SPI0CR1_MSTR =0;  //Slave mode

  //  SPI0CR1_SSOE =0; //Slave Select

   

    SPI0CR1_SPIE =1; //SPI interrup enable bit

   

    UART1_Init();

   

      __asm CLI;  //Enable interrupts globally

   

    //send_string_1("Worked !!!\r\n");

   }

         

 

 

        

  

    for(;;)

    {

        

          while(SPI0SR_SPTEF==0); /* Wait till transmit Tx_Data register is empty                  */

          SPI0DRL = 0x00;    /* Write Tx_Data to transmit Tx_Data register                    */

           /* Writing SPIDR after reading SPISR with SPTEF=1 clears SPTEF   */ 

     

  

   put_char_1(gelen[i]);

   i++;

      put_char_1(gelen[i]);

   i++;

   put_char_1(gelen[i]);

   i++;

      put_char_1(gelen[i]);

   i++;

      put_char_1(gelen[i]);  

   i=0;

 

 

        /* Delay to seperate SPI transmissions */

        for (dummy_counter_1 = 0; dummy_counter_1 < 100; dummy_counter_1++)

        {

           for (dummy_counter_2 = 0; dummy_counter_2 < 100; dummy_counter_2++)

            {            ;

            }

        }

    }

}

Labels (1)
2 Replies

434 Views
kef2
Senior Contributor IV
  • 1) I have got a problem with ISP interrupt routine. MCU goes to interrupt routine even if theres no data being receieved.

It's normal. SPIF on the master side is transfer complete interrupt. Flag is set and interrupt flagged once byte/word transfer is complete. You need to disable this interrupt when you have nothing to send/receive. SPTEF is transfer buffer empty interrupt. Same here, you need to disable it while nothing to send.

  • 2) And I am sending 1 byte data to the slave then the slave sends 4 byte response, so I miss 3 bytes. How can I get all the data, I want to do this in 8 bit data mode, if I use 16 bit mode then I will miss 2 byte.

SPI data is clocked. There's common clock wire for both receive and send. Master is driving clock wire. To receive you have to send something. Before you send dummy byte, slave has to put something to its SPI transfer buffer.

434 Views
sinanatilla
Contributor II

Edward thank you for the reply

I don't have any further questions for this as I have figured out the problem.

I have tried many ways to solve the problem for example, I tried to send 0xFF before valid data (0x00) and I tried the same thing after i sent 0xFF. But it is still the same, there is no change. How can I provide a continuous clock generation?

0 Kudos