SPI Example of KL46Z

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

SPI Example of KL46Z

Jump to solution
1,851 Views
xdong
Contributor I

I am trying to use FRDM-KL46Z board to read sensor data from SPI. I tried to follow the SPI example in SDK( frdmkl46z_spi_interrupt_b2b_transfer_master). I found that in the main function the program checks whether the received data is same as sent data, where I could not understand. For real application, I think the received data should be the return value from the sensor (i.e. register status or sensor data). I would like to ask whether I have any misunderstanding of the example code. How am I supposed to change the code to be able to receive the return value from the sensor.

Thanks in advance,

Labels (1)
Tags (1)
0 Kudos
1 Solution
1,607 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Xinjun Dong,

  What's the SDK version you are using? I am using the SDK_2.4.1_FRDM-KL46Z.zip, which is downloaded from this link:

https://mcuxpresso.nxp.com/en/select 

  Besides, please make sure your FRDM-KL46 just connect the USB cable, then no other connections.

  I also use the MCUXPresso IDE, and the semihost test like, please check my result, all SPI data is wrong.

  I find your test result even success, so that is very strange if you connect nothing, I can't believe it.

pastedImage_1.png

  Please clean your project, and give me a connect photo about your FRDM board.

 

Wish it helps you!

Have a great day,
Kerry

 

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

 

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

View solution in original post

0 Kudos
9 Replies
1,607 Views
xdong
Contributor I

Kerry,

Thank you for the reply. The problem now is that no matter what I sent through SPI using the example in SDK, the values in rx buffer are always same as the tx, so I am not able to test with the sensor. I was wondering in the project, whether there are anything settings I need to change to make the SPI be able to communication with other devices.

Thanks!

0 Kudos
1,607 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Xinjun Dong,

  You said:"no matter what I sent through SPI using the example in SDK, the values in rx buffer are always same as the tx"

  How do you test it, please give me more details about your testing result, how do you figure out that the RX buffer is the same as TX?

  Do you test the SPI bus wave with logic analyzer? Then you will find the MISO data, what's the data your save send to the master? If you send the other data, I don't think the RX and Tx will be same.

  Please double check the SPI bus data again, and check your slave send out data.

Wish it helps you!

Have a great day,
Kerry

 

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

 

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos
1,607 Views
xdong
Contributor I

I imported the frdmkl46z_spi_interrupt_b2b_transfer_master project in the SDK into MCUXpresso IDE. In addition, I did not connect anything to any pins on FRDM-KL46Z. Following is part of the code in the main function.

 for (i = 0; i < BUFFER_SIZE; i++)
    {
        srcBuff[i] = i;
    }

    /* Send to slave */
    xfer.txData = srcBuff;
    xfer.rxData = destBuff;
    xfer.dataSize = BUFFER_SIZE;
    SPI_MasterTransferCreateHandle(EXAMPLE_SPI_MASTER, &handle, masterCallback, NULL);
    SPI_MasterTransferNonBlocking(EXAMPLE_SPI_MASTER, &handle, &xfer);

    while (masterFinished != true)
    {
    }

    /* Check if the data is right */
    for (i = 0; i < BUFFER_SIZE; i++)
    {
        if (srcBuff[i] != destBuff[i])
        {
            err++;
            PRINTF("The %d is wrong! data is %d\n\r", i, destBuff[i]);
        }
    }
    if (err == 0)
    {
        PRINTF("Succeed!\n\r");
    }

As you may figure out, the txData is 0-(BUFFER_SIZE - 1). Meanwhile, the program itself is expecting the rxData would be same as the txData. Indeed, after running the program, the values in destBuff is same as those in srcBuff, which will not happen if I did not connect anything to the board. You may have a try since it is pretty straightforward. I suspect that some settings in the project connect the MOSI and MISO bus internally, but I did not find such setting.

Thank you for the help!

0 Kudos
1,607 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Xinjun Dong,

   This is the debug result and the printf result:

    You can find the the destBuff is wrong, the printf result is also wrong:   pastedImage_1.png

You can also give your test result if you connect nothing, just the master, whether your printf result is also ok? I can't believe it, please try it again.

Wish it helps you!

Have a great day,
Kerry

 

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

 

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos
1,607 Views
xdong
Contributor I

Kerry,

Thank you for the reply.

Nxp1.png

Above is the screenshot after the program is run. You may see the values in destBuff are same as srcBuff, and there is no error message. Thank you for your help.

Best

0 Kudos
1,608 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Xinjun Dong,

  What's the SDK version you are using? I am using the SDK_2.4.1_FRDM-KL46Z.zip, which is downloaded from this link:

https://mcuxpresso.nxp.com/en/select 

  Besides, please make sure your FRDM-KL46 just connect the USB cable, then no other connections.

  I also use the MCUXPresso IDE, and the semihost test like, please check my result, all SPI data is wrong.

  I find your test result even success, so that is very strange if you connect nothing, I can't believe it.

pastedImage_1.png

  Please clean your project, and give me a connect photo about your FRDM board.

 

Wish it helps you!

Have a great day,
Kerry

 

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

 

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos
1,607 Views
xdong
Contributor I

I think it is the board I am using has certain problem. I switched to another problem, and the problem seemed to be solved. Thank you for the help!

0 Kudos
1,607 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Thanks for your updated information.

If you have the new question in the future, welcome to create the new question post.

Best Regards,

Kerry

0 Kudos
1,607 Views
kerryzhou
NXP TechSupport
NXP TechSupport

HI Xinjun Dong ,

  frdmkl46z_spi_interrupt_b2b_transfer_master in the SDK just give you an example about the KL46 SPI module, teach you how to use the SPI to do the communication.

  For the detail application usage, you totally can designed it by yourself to meet your app's demand.

  If you still have questions about it, please kindly let me know.

Wish it helps you!

Have a great day,
Kerry

 

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

 

- We are following threads for 7 weeks after the last post, later replies are ignored
Please open a new thread and refer to the closed one, if you have a related question at a later point in time.
-------------------------------------------------------------------------------

0 Kudos