4 Replies Latest reply on Feb 17, 2020 4:55 PM by ChHs_4614611

    PSOC 4100 SPI slave issue

    ChHs_4614611

      I have a SPI slave mode problem with interrupt mode.

      When I use polling communication is normal. I can receive SPI Master  frame and response slave frame data.

      But, I try interrupt mode. it Will be stuck in the interrupt sub function.

      The program strucks on this line in debug mode. -->  while (PACKET_SIZE != SPI_SpiUartGetRxBufferSize()) 

       

       

      The blow is my PSoc creator setting and program screenshot.

      spi4.jpg

      spi5.jpg

       

      my program screenshot

      1. ISR function

      spi7.jpg

      2. Enable interrupt

      spi8.jpg

      3. ISR function

      spi6.jpg

        • 1. Re: PSOC 4100 SPI slave issue
          MoTa_728816

          Hi,

           

          I think that the interrupt of SPI happens each byte.

          So I would do instead of gathering the whole packet,

          pick up each byte in the ISR and when the buffer index reaches to PACKET_SIZE set the flag.

           

          ==================

          CY_ISR(SPI_ISR)

          {

              if ((SPI_GetRxInterruptSourceMasked() & SPI_INTR_RX_NOT_EMPTY) != 0) {

                 

                  if (0u != SPI_SpiUartGetRxBufferSize()) {

                      MasterFrame[rx_buf_index] = (uint8_t) SPI_SpiUartReadRxData() ;

                      rx_buf_index++ ;

                  }

                 

                  /* Clear interrupt flag */

                  SPI_ClearRxInterruptSource(SPI_INTR_RX_NOT_EMPTY) ;

                 

                  if (rx_buf_index == PACKET_SIZE) {

                      rx_buf_index = 0 ;

                      spi_flag = AS_TRUE ;

                  }

              }  

          ==================

           

          Following is my trial with CY8CKIT-149

           

          schematic

          001-schematic.JPG

          pins

          Note: I connected following 4 pairs using jumper wires

          SPI:miso_s - SPIM:miso_m

          SPI:mosi_s - SPIM:mosi_m

          SPI:sclk_s   - SPIM:sclk_m

          SPI:ss_s  - SPIM:ss0_m

          002-pins.JPG

          main.c

          =================

          #include "project.h"

          #include "stdio.h"

           

          #define AS_TRUE    (1u)

          #define AS_FALUSE  (0u)

          #define PACKET_SIZE 10

          volatile int spi_flag = 0 ;

          volatile int rx_buf_index = 0 ;

           

          uint8_t MasterFrame[PACKET_SIZE] = { 0u } ;

           

          #if 0

          CY_ISR(SPI_ISR)

          {

              uint8_t FrameIndex = 0 ;

             

              if ((SPI_GetRxInterruptSourceMasked() & SPI_INTR_RX_NOT_EMPTY) != 0) {

                  /* Waits for the end of the data transfer */

                  while(PACKET_SIZE != SPI_SpiUartGetRxBufferSize()) {

                      ;

                  }

                 

                  while(0u != SPI_SpiUartGetRxBufferSize()) {

                      MasterFrame[FrameIndex] = (uint8_t) SPI_SpiUartReadRxData() ;

                      FrameIndex++ ;

                  }

                 

                  /* Clear rx buffer */

                  SPI_SpiUartClearRxBuffer() ;

                 

                  /* Clear interrupt flag */

                  SPI_ClearRxInterruptSource(SPI_INTR_RX_NOT_EMPTY) ;

                 

                  spi_flag = AS_TRUE ;

              }  

          }

          #else

          CY_ISR(SPI_ISR)

          {

              if ((SPI_GetRxInterruptSourceMasked() & SPI_INTR_RX_NOT_EMPTY) != 0) {

                 

                  if (0u != SPI_SpiUartGetRxBufferSize()) {

                      MasterFrame[rx_buf_index] = (uint8_t) SPI_SpiUartReadRxData() ;

                      rx_buf_index++ ;

                  }

                 

                  /* Clear interrupt flag */

                  SPI_ClearRxInterruptSource(SPI_INTR_RX_NOT_EMPTY) ;

                 

                  if (rx_buf_index == PACKET_SIZE) {

                      rx_buf_index = 0 ;

                      spi_flag = AS_TRUE ;

                  }

              }  

          }   

          #endif

           

          #define STR_LEN 64

          char    str[STR_LEN+1] ;

          void    print(char *str)

          {

              UART_UartPutString(str) ;

          }

           

          void   printc(char c)

          {

              UART_UartPutChar(c) ;

          }

           

          void cls(void)

          {

              print("\033c") ; /* reset */

              CyDelay(20) ;

              print("\033[2J") ; /* clear screen */

              CyDelay(20) ;

          }

           

          void splash(void)

          {

              cls() ;

              print("PSoC 4100S Plus SPI test") ;

              snprintf(str, STR_LEN, "(%s %s)\n", __DATE__, __TIME__) ;

              print(str) ;

          }

           

          void init_hardware(void)

          {

              CyGlobalIntEnable; /* Enable global interrupts. */

           

              UART_Start() ;

           

              splash() ;

             

              SPIM_Start() ;

             

              isr_1_ClearPending() ;

              isr_1_StartEx(SPI_ISR) ;

              SPI_Start() ;

          }

           

          void clear_MasterFrame(void)

          {

              int i = 0 ;

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

                  MasterFrame[i] = 0 ;

              }

          }

           

          void send_packet(void)

          {

              uint8_t tx_buffer[PACKET_SIZE] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' } ;

              SPIM_SpiUartClearTxBuffer() ;

              SPIM_SpiUartPutArray(tx_buffer, PACKET_SIZE) ;

          }

           

          void dump_rx_buffer(void)

          {

              int i = 0 ;

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

                  printc(MasterFrame[i]) ;

              }

              print("\n\r") ;

          }

           

          int main(void)

          {  

              init_hardware() ;

           

           

              send_packet() ;

             

              for(;;) {

                  if (spi_flag) {

                      spi_flag = 0 ;

                      dump_rx_buffer() ;

                      clear_MasterFrame() ;

                      CyDelay(1000) ;

                     send_packet() ;

                  }

              }

          }

          =================

           

          Tera Term log

          000-TeraTerm-log.JPG

           

          moto

          1 of 1 people found this helpful
          • 2. Re: PSOC 4100 SPI slave issue
            ChHs_4614611

            Hi Moto,

             

             

            My mcu type is CY8C4125LQI-483 and PSoc creator 4.2

            I can not find SPI interrupt In the schematic.

            • 3. Re: PSOC 4100 SPI slave issue
              MoTa_728816

              Hi,

               

              I'm sorry I miss-read PSoC 4100 SPI Slave as PSoC 4100S Plus. (It's Monday)

               

              Anyway, could you try to configure your SPI as

              003-4125-spi.JPG

               

              moto

              P.S. Attached is my test sample for CY8C4125LQI-483.

              As we have only 2 SCBs, I deleted SPIM, which I used to generate test data.

              The project is compile-able, but as I don't have board with this device, I have not tested it.

              1 of 1 people found this helpful
              • 4. Re: PSOC 4100 SPI slave issue
                ChHs_4614611

                Dear Moto,

                 

                I try your sample it working in interrupt mode.

                Thank you for your help.