Configure GPIO pin for SPI

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

Configure GPIO pin for SPI

1,579 Views
dekel_gilboa
Contributor II

I'm trying to use LPSPI in interrupt mode(i found an samples for polling mode transfer and receive). I understood that i'm suppose to configure GPIO pin for that(correct me if i'm wrong) but i didn't manage to do so. Can you show me how to do so with some references or code examples for how to configure a GPIO pin and enable interrupts.

Thx, Dekel

5 Replies

1,291 Views
PetrS
NXP TechSupport
NXP TechSupport

Hi,

not sure which example you are pointing to. Probably you meant non-SDK called LPSPI_S32K144.

In this example you can see how pins have to be configured for SPI functionality, see PORT_Init() function.

If you want to use interrupt instead of polling flags, then enabled respective interrupt enable bit in LPSPI_IER register,  enable interrupt within NVIC module and add interrupt handler.

So for example to use receive data interrupt use 

LPSPI1->IER   = 0x00000002;   /* RDIE enabled */

// enable LPSPI1 interrupt within NVIC
S32_NVIC->ICPR[0] = 1 << (LPSPI1_IRQn % 32); /* ch0: clr any pending IRQ*/
S32_NVIC->ISER[0] = 1 << (LPSPI1_IRQn % 32); /* ch0: enable IRQ */
S32_NVIC->IP[LPSPI1_IRQn] = 0xA; /* ch0: priority 10 of 0-15*/

void LPSPI1_IRQHandler()
{
   uint16_t recieve = 0;

   

   if(LPSPI1->SR & LPSPI_SR_RDF_MASK)
   {
      recieve= LPSPI1->RDR; /* Read received data */
      //LPSPI1->SR = LPSPI_SR_RDF_MASK; /* Clear RDF flag */

      LPSPI1_transmit_16bits(tx_16bits); /* Transmit half word (16 bits) on LPSPI1 */
   }

}

BR, Petr

1,291 Views
dekel_gilboa
Contributor II

Hi,

Thank you for quick responding. You assumed right(i meant non-SDK LPSPI, forgot to mention it). 

I need also to use transmit data interrupts, so I have to configure the appropriate bit like this(I check the Reference manual):

LPSPI1->IER   = 0x00000003; /*Both receive and transmit interrupts enable*/

1. is that correct ?

2. Is there anything else i need to configure in order to support transmit data interrupt ? in the NVIC maybe ?

3. I understand that void LPSPI1_IRQHandler() is the interrupt handler. i didn't understand how how use it in order to

   read(or write in case of interrupt handler to transmit) in interrupted mode. Do i have to call that function every time i have to read data ? (even though it doesn't make sense).

If you need anymore details let met know.

Thx, 

Dekel

0 Kudos

1,291 Views
PetrS
NXP TechSupport
NXP TechSupport

Hi Dekel,

1. yes, this is correct. By setting IER->TDIE bit the interrupt (LPSPI1_IRQHandler) is called each time the SR->TDF is set, meaning there is empty space in the TXFIFO and you can fill data.

2. no, nothing else is needed. There is single interrupt vector for the module and so LPSPI1_IRQHandler is called whenever any of SR's flag is set, assuming its interrupt enable bit is set in IER register. Within this handler you should check which flag is causing the interrupt and do desired action.

3. LPSPI1_IRQHandler is interrupt handler, so core jumps to this function when enabled interrupt is requested by LPSPI module.

BR, Petr

1,291 Views
dekel_gilboa
Contributor II

Hi Petr,

Thx again for responding. I'm Still trying to figure out How to use the interrupt handler in order to transmit and receive data.

One way came in my mind is like:

   uint16_t data[] = {0x0001, 0x0002, 0x0003};//Just a typical data

   uint16_t recieve = 0;

   int i =0;

void main(void)

{

   PORT_init();//Init IER and some more stuff.

   while(1)

   {

         if(i > sizeof(data) / sizeof(data[0]) - 1)//3 - 1 = 2

         {

               i = 0;

         }

         if(recieve == 0xAA)

         {

               //Do something

         }
   }

}

void LPSPI1_IRQHandler()
{

   if(LPSPI1->SR & LPSPI_SR_RDF_MASK)//Data receive required
   {
      recieve= LPSPI1->RDR; /* Read received data */
      LPSPI1->SR = LPSPI_SR_RDF_MASK; /* Clear RDF flag */
   }

   if(LPSPI1->SR & LPSPI_SR_TDF_MASK)//Data transmit required
   {
      LPSPI1->TDR = data[i++];/* Transmit data */
      LPSPI1->SR = LPSPI_SR_TDF_MASK;/* Clear TDF flag */
   }
}

1. Is there a more elegant way to receive or transmit data in interrupted mode ?  like in polling(with void LPSPI1_transmit_16bits (uint8_t send)  and uint8_t LPSPI1_receive_16bits)(void))

2. is that even a correct way to handle interrupts ? (In my example

Thx,

Dekel

0 Kudos

1,291 Views
PetrS
NXP TechSupport
NXP TechSupport

Hi Dekel,

once you enable TX interrupt you immediately enter interrupt as TXFIFO is empty. So enabling the TX interrupt is usually last step before you want to send data. Within interrupt you fill TXFIFO and check how many bytes must be transmitted further, if none disable TX interrupt.

For instance you can refer to SDK's LPSPI driver implementation.

BR, Petr 

0 Kudos