This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

SPI interfacing with gyroscope

Other Parts Discussed in Thread: HALCOGEN

Hi all,

I am currently having difficulty in getting the X,Y,Z  of gyroscope's data output. The gyroscope I am using is L3G4200D. The Microcontroller board is Hercules RM42x LaunchPad Evaluation Kit. I attached a sample code of what I did below. From my understanding, writing 0x8f should return 0xd3. This is used to check if the SPI connection is successful. Currently it is returning 0.

Thank you all in advance for all the help.

Regards,

Yuan Fa

#include "sys_common.h"
#include "system.h"


#include "spi.h"
#include "stdio.h"

void main(void) {
	
	spiDAT1_t dataconfig1_t;

	dataconfig1_t.CS_HOLD = FALSE;

	dataconfig1_t.WDEL = TRUE;

	dataconfig1_t.DFSEL = SPI_FMT_0;

	dataconfig1_t.CSNR = 0xFE;

	uint16 TX_Data_Master[16] = { 0x8F,0x00 };

	uint16 TX_Data_Master2[16] = { 0x00 };

	uint16 RX_Data_Master[16] = { 0 };

	_enable_IRQ();

	spiInit();

	spiSendData(spiREG2, &dataconfig1_t, 16, TX_Data_Master);
	printf("Value at p1: %d\n", *RX_Data_Master);

	spiSendAndGetData(spiREG2, &dataconfig1_t, 16, TX_Data_Master2,RX_Data_Master);

	printf("Value at p1: %d\n", *RX_Data_Master);

	while (1);

}


  • Hello Yuan Fa,

    We have received your post.  One of our experts will get back to you shortly.  However, we may not have experience with your specific sensor.  Have you otherwise validated that your sensor is working properly?  Perhaps validated by a known working system?

    Regards, Brian

  • Yuan Fa,

    Could you share your project so I can have a look if the SPI is correctly configured?
    Mainly I need the HalCoGen project.

  • Hi Yuan,

    It seems like you want the RM42 to be the SPI master and the gyroscope to be the SPI slave. I am combining information from two threads together. In the other thread for the same issue you stated the following connections:

    3.3v to VCC

    GND to GND

    SPI2SOMI to SDA

    SPI2CS0 to CS

    SPI2CLK to SCL

    SPI2SIMO to SDO

    Please connect SPI2SIMO to SDI pin and connect SPI2SOMI to SDO pin.

    Also, in HALCoGen SPI2 tab, do not enable output direction on the SPI2SOMI pin. This is slave-output-master-input, so it should be made input pin for RM42.

    Please try these changes and let me know.

    Regards, Sunil

  • Hi Jean-Marc,

    Thanks for the help again. I attached the whole workspace for you to inspect.

    0726.workspace_robot.rar

    regards,

    Yuan Fa

  • Hi Sunil,

    Made the suggested changes and I still get 0. I was wondering, do I need to use SPI as master to send the CS and clk to the spi2 which was what I did not do. I only find SPI1CS0 and SPI1CLK pins, but SPI1SIMO and SPI1SOM1 pins was not found. After connecting SPI1CS0 to SPI2CS0 and SPI1CLK to SPI2CLK, my output was still 0.

    +++updates 1++

    I found the spi1simo, spi1somi and spi1ena pin. it have a jumper header attached to it, I wonder why.

    Thanks for your help.

    Regards,

    Yuan Fa

  • Yuan Fa,

    Thank you for sharing your code.

    You are using SPI2 to communicate with your Gyroscope.

        spiSendAndGetData(spiREG2, &dataconfig1_t, 16, TX_Data_Master2,RX_Data_Master);

    In Halcogen SP2 Global Tab, the SPI2 is not set as Master.
    You have to check this box. Only Master can generate the clock and control the CS.

    For information, here are the pin mapping for SPI2 on the PZ package.

    SPI2CLK     71
    SPI2CS0      23
    SPI2SOMI    69
    SPI2SIMO     70

  • Hi Jean-Marc,

    Actually I set the SPI2 to master before from the first post, just that I am doing trial and error and change it to slave.

    What can I do what the pin mapping information? In the gyro data sheet, the  WHO_AM_I registor is 0x0f and will return 0xd3, how do I achieve this. I thought  spiSendAndGetData(spiREG2, &dataconfig1_t, 16, TX_Data_Master2,RX_Data_Master); will do the trick and RX_Data_Master will return the value of 0xD3.

    Thanks for the help.


    Regards,

    Yuanfa

  • Hi Brian,

    Thanks for your help. I just need to understand how to get the  WHO_AM_I read-only register to return 0xd3.

    WHO_AM_I address is 0x0f. After that I can slowly find out how to get the other information.

    Regards,

    Yuanfa

  • Hi guys,

    i have just tested the gyroscope with an Arduino uno board and it is working. But i still could not get it work on hercules launch pad. I will continue to work out this problem.  Hope there will still be help and suggestion along the way.

    Regards,

    Yuanfa

  • Yuanfa,

    Apparently, you were using Halcogen 3.08.00
    A bug was present in the void spi2HighLevelInterrupt(void) and void spi2LowLevelInterrupt(void)
    It has been fixed in version 03.08.01. You can download this release from Halcogen->Help->Check for Update.

    I also found some mistake in your main.c file.

    I've attached a new project that should work for your gyroscope.

    The mistake were:

    dataconfig1_t.CS_HOLD = TRUE;

    uint16 TX_Data_Master2[2] = { 0x8F,0x00 };                                            // To be used with Char Len = 8

    spiSendAndGetData(spiREG2, &dataconfig1_t, 2, TX_Data_Master2, RX_Data_Master);      // Char Len = 8 in SPIFORMAT0

    With this definition, the SPI has to be configured with a character length of 8 bits. Chip select has to be kept low during the full transmission, in this case 2 bytes for a total of 16 bits.

    Another option is to configure the SPI as 16bits character length. In this case you have to use the following:

    uint16 TX_Data_Master1[1] = { 0x8F00 };                                                // To be used with Char Len = 16

    spiSendAndGetData(spiREG2, &dataconfig1_t, 1, TX_Data_Master1, RX_Data_Master);      // Char Len = 16 in SPIFORMAT0

    The received data will be a 16bits data. The top 8 bits are irrelevant, the bottom 8 bits will be the ID of your gyroscope.

    Please have a try with the attached code and let me know the result.

    7140.workspace_robot.zip

  • Hi Jean-Marc,

    Thanks to your help. Your solution worked. I got 0xd3 from the gyroscope. I saw how u did that i miss out was to check the spiREG2 status, setting it to 8 bit charlen for the dataformat  and setting CS hold to true. I should be able to continue from here, should there be any more problems i will create a new topic.

    You are awesome.

    Regards,

    Yuanfa