SPI 3 wires

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

SPI 3 wires

768 Views
r58737
NXP Employee
NXP Employee

Hello ,

Is it possible to get SPI 3 wires driver on the Freedom K46.

I Look at the documentation and only see information about the 4 wires spec.

I already implemented the 4 wires and look at the 3 wires possibility( in 3 wires the MOSI line is bidirectionnal and during reading the data is put on the MOSI line).

My actual SPI 4 wires readings :

byte SpiReadReg2(uint8_t addr) {

    MySPIPtr = SM1_Init(NULL);

    SPI0_BR=0x22;

    dataforspi.spi_addr = addr+128;

    dataforspi.spi_val = 0XFF;

    SPI_CS2_ClrVal();

    // send and receive of address and value

    (void)SM1_ReceiveBlock(MySPIPtr, &dataforspi, sizeof(spiData));

    (void)SM1_SendBlock(MySPIPtr, &dataforspi, sizeof(spiData));

    // wait for receiving of the block of data

    while(!SM1_GetBlockReceivedStatus(SM1_DeviceData)){};

    SPI_CS2_SetVal();

    return dataforspi.spi_val;

}

Thanks.

0 Kudos
1 Reply

517 Views
Hui_Ma
NXP TechSupport
NXP TechSupport

Hi,

Unfortunately, there is no KL46 SPI bidirectional mode driver code.

Please check below validation code for KL25 SPI mode, which test SPI communication using SPI bidirectional mode within SPI0 and SPI1 modules.

Wish it helps.

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

void spi0_bid_master_spi1_bid_slave(void)   

{

       global_pass_count = 0;

    enable_m0irq(10);

        enable_m0irq(11);

        PORTD_PCR6 &= ~PORTD_PCR6_MUX_MASK;

    PORTD_PCR6 |= PORTD_PCR6_MUX(1)|PORTD_PCR6_DSE_MASK;  //Use PTB15 as test result

    GPIOD_PDDR |= 0x0040;                              //Pin is configured for general purpose output

     GPIOD_PDOR &= 0xffffffbf;                     //PTB15 Output 0

       PORTD_PCR3 &= ~PORTD_PCR3_MUX_MASK;

    PORTD_PCR3 |= PORTD_PCR3_MUX(1)|PORTD_PCR3_DSE_MASK;  //Use PTB15 as test result

    GPIOD_PDDR |= 0x00008;                              //Pin is configured for general purpose output

     GPIOD_PDOR &= 0xfffffff7;

       

//    PORTB_PCR15 = PORT_PCR_MUX(1)|PORT_PCR_DSE_MASK;  //Use PTB15 as test result

//    GPIOB_PDDR |= 0x8000;                              //Pin is configured for general purpose output

//     GPIOB_PDOR &= 0xffff7fff;                           //PTB15 Output 0

//

//       PORTB_PCR16 = PORT_PCR_MUX(1)|PORT_PCR_DSE_MASK;  //Use PTB16 as test result

//    GPIOB_PDDR |= 0x10000;                              //Pin is configured for general purpose output

//     GPIOB_PDOR &= 0xfffeffff;

     //printf("SPI bidirection function test master model\n");

    SPI0_C1 |= SPI0_C1_MSTR_MASK;

       

    SPI0_BR = 0x43;  //SPPR = 4, SPR = 3, bps div = (SPPR+1)*2^(SPR+1) = 80,----Tspi--6.4us   

    //SPI0_BR = 0x30;//bps div = (SPPR+1)*2^(SPR+1) = 8--------0.64us   

    SPI0_C1 |= SPI0_C1_SSOE_MASK|SPI0_C1_CPHA_MASK|SPI0_C1_CPOL_MASK|SPI0_C1_LSBFE_MASK;

    SPI0_C1 &= (~SPI0_C1_SPIE_MASK);   //Disable RX interrrupt

      //SPI0_C1 |= SPI1C1_SPIE_MASK;       //enable RX interrrupt          

    SPI0_C1 &= (~SPI0_C1_SPTIE_MASK);       //Disable the transmit interrupt

     //SPI0_C1 |= SPIC1_SPTIE_MASK;       //Enable the transime interrupt

    SPI0_C2 |= SPI0_C2_MODFEN_MASK|SPI0_C2_SPMIE_MASK;;

    SPI0_C1 |= SPI0_C1_SPE_MASK;

        SPI0_M = 0x56;

      //set single wire mode

      SPI0_C2 |= SPI0_C2_SPC0_MASK;     

 

      //Cash: Only MOSI can be used in BID mode, set MOSI input/output

      SPI0_C2 |= SPI0_C2_BIDIROE_MASK;   //MOSI is used in master as I/O

               

        SPI1_C1 &= (~SPI1_C1_MSTR_MASK);

       

    //SPI0_BR = 0x43;  //SPPR = 4, SPR = 3, bps div = (SPPR+1)*2^(SPR+1) = 80,----Tspi--6.4us   

    //SPI0_BR = 0x30;//bps div = (SPPR+1)*2^(SPR+1) = 8--------0.64us   

    SPI1_C1 |= SPI1_C1_SSOE_MASK|SPI1_C1_CPHA_MASK|SPI1_C1_CPOL_MASK|SPI1_C1_LSBFE_MASK;

    SPI1_C1 &= (~SPI1_C1_SPIE_MASK);   //Disable RX interrrupt

      //SPI0_C1 |= SPI1C1_SPIE_MASK;       //enable RX interrrupt          

    SPI1_C1 &= (~SPI1_C1_SPTIE_MASK);       //Disable the transmit interrupt

     //SPI0_C1 |= SPIC1_SPTIE_MASK;       //Enable the transime interrupt

    SPI1_C2 |= SPI1_C2_MODFEN_MASK|SPI1_C2_SPMIE_MASK;

    SPI1_C1 |= SPI1_C1_SPE_MASK;

      SPI1_M = 0x5E;

       //set single wire mode

      SPI1_C2 |= SPI1_C2_SPC0_MASK;

       //set MISO input

      SPI1_C2 &= ~SPI1_C2_BIDIROE_MASK;   //MISO is used in slave mode as input

                             

    spi0_send8(0x0E);        

    rd = spi0_get8();        //Cash: as in the bid I/O mode, the send and the read data should be the same~

        rd1 = spi1_get8();

        printf("SPI0_rd: %02x\r\n", rd&0xFF);

        printf("SPI1_rd: %02x\r\n", rd1&0xFF);

      spi0_send8(0x1E);

      rd = spi0_get8();         //the send and the read data should be the same~

        rd1 = spi1_get8();

        printf("SPI0_rd: %02x\r\n", rd&0xFF);

        printf("SPI1_rd: %02x\r\n", rd1&0xFF);

      spi0_send8(0x2E);

      rd = spi0_get8();         //the send and the read data should be the same~

        rd1 = spi1_get8();

        printf("SPI0_rd: %02x\r\n", rd&0xFF);

        printf("SPI1_rd: %02x\r\n", rd1&0xFF);

      spi0_send8(0x3E);

      rd = spi0_get8();         //the send and the read data should be the same~

        rd1 = spi1_get8();

        printf("SPI0_rd: %02x\r\n", rd&0xFF);

        printf("SPI1_rd: %02x\r\n", rd1&0xFF);      

      spi0_send8(0x4E);

      rd = spi0_get8();         //the send and the read data should be the same~

        rd1 = spi1_get8();

        printf("SPI0_rd: %02x\r\n", rd&0xFF);

        printf("SPI1_rd: %02x\r\n", rd1&0xFF);       

      spi0_send8(0x5E);

      rd = spi0_get8();        //the send and the read data should be the same~

        rd1 = spi1_get8();

        printf("SPI0_rd: %02x\r\n", rd&0xFF);

        printf("SPI1_rd: %02x\r\n", rd1&0xFF);       

        

//        while( global_pass_count <= 5 ){

//       

//        rd = spi1_get8();

//

//        global_pass_count++;

//     }

         global_pass_count = 0;

    SPI1_C2 |= SPI0_C2_BIDIROE_MASK;       //MISO is used in slave mode as I/O---output

       //set MOSI input

      SPI0_C2 &= ~SPI0_C2_BIDIROE_MASK;     //MOSI is used in master mode as input

   

    while(1)

    {

                global_pass_count++;

                while ((SPI1_S & SPI1_S_SPTEF_MASK) == 0);

               

                SPI1_D = global_pass_count;                       //0111_1000----MSBF

        rd=spi0_send_read(0xFF);            //0011_0101----MSBF

                printf("SPI0_rd: %02x\r\n", rd&0xFF);

        //spi1_send8(global_pass_count);

        //spi0_send8(0x5E);         //there should no waveforms on MOSI----

          //rd = spi0_get8();         //MOSI used as input, the rd data must be zero~~

         //printf("Transaction complete!!\n\n");

        /* we can set the MOSI to 0xff, to test the input function after make sure no waveform out */

    }

}

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


Have a great day,
best regards,

Ma Hui

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos