SPI Output Problem

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Sophie_Wang
Level 4
Level 4
Distributor - Zenitron(GC)
25 replies posted 10 replies posted 5 replies posted

Hi Cypress Friend,

I am not so familiar to P6 MCU, I want to use the SPI but I spent several days and just failed.

I attached the wave form and my code, would you guys kindly help to tell what should I do to get my desired wave.

This is what I want. P1

pastedImage_0.png

This is the problem:

pastedImage_1.png

0 Likes
1 Solution
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

Although I don't know if this is what you want,

but with following modification the output wave seems to be similar to what you wanted.

main loop

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

int main(void)

{

    uint16_t tx_data[4] = { 0x4444, 0x0000, 0x6666, 0x0000 } ;

    uint16_t tx_index = 0 ;

   

    // __enable_irq(); /* Enable global interrupts. */

    /* Enable CM4.  CY_CORTEX_M4_APPL_ADDR must be updated if CM4 memory layout is changed. */

    // Cy_SysEnableCM4(CY_CORTEX_M4_APPL_ADDR);

    init_hardware() ;

   

    splash() ;

                   

    for(;;)

    {

        if (SPI_GetTxFifoStatus() & CY_SCB_SPI_TX_NOT_FULL) {

            SPI_Write(tx_data[tx_index]) ;

            tx_index = (tx_index + 1) % 4 ;

        }

#if 0 /* for debug */

        if (SPI_GetRxFifoStatus() & CY_SCB_SPI_RX_NOT_EMPTY) {

            rx_data = SPI_Read() ;

            snprintf(str, STR_LEN, "Received 0x%04X\n", rx_data) ;

            print(str) ;

        }

        CyDelay(100) ;

#endif

    }

}

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

moto

View solution in original post

7 Replies
Sophie_Wang
Level 4
Level 4
Distributor - Zenitron(GC)
25 replies posted 10 replies posted 5 replies posted

I don't know how to attach my code. so I paste it here.

    SPI_Start();

    SPI_SetActiveSlaveSelect(CY_SCB_SPI_SLAVE_SELECT1);

   

    Cy_SysLib_Delay(10);

       

    for(;;)

    {

        if (SPI_GetTxFifoStatus() & CY_SCB_SPI_TX_NOT_FULL)

        {

            if (swap)

            {

                SPI_Write(0x4444);

            }

            else

            {

                SPI_Write(0x6666);                          

            } 

            swap = !swap;

        }

        if (SPI_GetTxFifoStatus() & CY_SCB_SPI_TX_NOT_FULL)

        {     

                SPI_Write(0x0000);

        }

    }

0 Likes

Hi,

you better setup the SPI to use a proper txFifoTriggerLevel and check in the main loop for this value.

In your case this would be 8byte lower than the FIFO  site and then you should write both 0x4444/0x6666 and 0x0000 into the SPI FIFO .

Because with your given code, in case the FIFO is full it will write either 0x4444/0x6666 or 0x0000 into the FIFO whenever there is space in the FIFO, but which of those values is written is basically random, as your code will switch between both if statements and then execute that case when the FIFO gets empty. It does not check for one and wait till the FIFO is empty (which would also be bad practice, as it will block your code execution).

Code would look like this:

        if (SPI_GetTxFifoStatus() & CY_SCB_SPI_TX_TRIGGER)

        {

            if (swap)

            {

                SPI_Write(0x4444);

            }

            else

            {

                SPI_Write(0x6666);                         

            }

            swap = !swap;

            SPI_Write(0x0000);

        }

regards,

Achim

Sophie_Wang
Level 4
Level 4
Distributor - Zenitron(GC)
25 replies posted 10 replies posted 5 replies posted

AchimE_41

Hi Achim,

Thanks for your reply, but I don't see trigger set up in SPI component in PSoC 6. Besides, I need to connect the SPI component Output pins to Smart I/O component. Could you give a code example for spi trigger?

Regards

sophie

0 Likes
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

FYI, I tried following

main_cm0p.c

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

#include "project.h"

#include "stdio.h"

#define STR_LEN 64

char str[STR_LEN+1] ;

void print(char *str)

{

    UART_PutString(str) ;

}

void cls(void)

{

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

    CyDelay(100) ;

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

    CyDelay(100) ;

}

void splash(void)

{

    cls() ;

    print("PSoC 6 SPI Test ") ;

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

    print(str) ;

}

void init_hardware(void)

{

    __enable_irq(); /* Enable global interrupts. */

    UART_Start() ;

    SPI_Start() ;

}

int main(void)

{

    int i = 0 ;

    uint16_t tx_data[2] = { 0x4444, 0x6666 } ;

    uint16_t rx_data = 0 ;

    uint16_t dummy = 0x0000 ;

  

    // __enable_irq(); /* Enable global interrupts. */

    /* Enable CM4.  CY_CORTEX_M4_APPL_ADDR must be updated if CM4 memory layout is changed. */

    // Cy_SysEnableCM4(CY_CORTEX_M4_APPL_ADDR);

    init_hardware() ;

  

    splash() ;

    for(;;)

    {

        snprintf(str, STR_LEN, "Sending 0x%04X : ", tx_data) ;

        print(str) ;

        SPI_Write(tx_data) ;

        while(SPI_IsBusBusy()) ;

        SPI_Write(dummy) ;

        while(SPI_IsBusBusy()) ;

        i = (i + 1)%2 ; /* i = 0, 1, 0, 1... */

        rx_data = SPI_Read() ;

        snprintf(str, STR_LEN, "Received 0x%04X\n", rx_data) ;

        print(str) ;

        dummy = SPI_Read() ;

        CyDelay(1000) ;

    }

}

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

Tera Term log

TeraTerm-log.JPG

moto

Sophie_Wang
Level 4
Level 4
Distributor - Zenitron(GC)
25 replies posted 10 replies posted 5 replies posted

MoTa_728816

Hi Motoo,

Thanks a lot for your great help! I changed my code based on your demo. It's a little slower than I expected. I want SPI continuesly output waveform like I pasted above. Your reply is very inspiring. Thanks。

And I paste my code and wave based on your code below.

   

    for(;;)

    {

            if (swap)

            {

                SPI_Write(0x4444);

            }

            else

            {

                SPI_Write(0x6666);                        

            }

            swap = !swap;

            while(SPI_IsBusBusy());

            SPI_Write(0x0000);

            while(SPI_IsBusBusy());

    }

1111.png

lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

Although I don't know if this is what you want,

but with following modification the output wave seems to be similar to what you wanted.

main loop

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

int main(void)

{

    uint16_t tx_data[4] = { 0x4444, 0x0000, 0x6666, 0x0000 } ;

    uint16_t tx_index = 0 ;

   

    // __enable_irq(); /* Enable global interrupts. */

    /* Enable CM4.  CY_CORTEX_M4_APPL_ADDR must be updated if CM4 memory layout is changed. */

    // Cy_SysEnableCM4(CY_CORTEX_M4_APPL_ADDR);

    init_hardware() ;

   

    splash() ;

                   

    for(;;)

    {

        if (SPI_GetTxFifoStatus() & CY_SCB_SPI_TX_NOT_FULL) {

            SPI_Write(tx_data[tx_index]) ;

            tx_index = (tx_index + 1) % 4 ;

        }

#if 0 /* for debug */

        if (SPI_GetRxFifoStatus() & CY_SCB_SPI_RX_NOT_EMPTY) {

            rx_data = SPI_Read() ;

            snprintf(str, STR_LEN, "Received 0x%04X\n", rx_data) ;

            print(str) ;

        }

        CyDelay(100) ;

#endif

    }

}

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

moto

Sophie_Wang
Level 4
Level 4
Distributor - Zenitron(GC)
25 replies posted 10 replies posted 5 replies posted

Hi Moto,

Thanks very much. I resolved this problem.

br

sophie