interfacing of accelerometer using SPI

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

cross mob
Anonymous
Not applicable

I'm currently trying to interface accelerometer LIS3DH to the PSoC4 BLE. I selected spi master full duplex mode in PSoC creator.Can someone please tell me whether it's the correct one. And what's the difference between the normal mode and SCB mode?

   

Also, the registers of the accelerometer are to be configured by writing values into them. So do I have to write my own function for writing into registers or are there already some APIs in the creator.

   

I'm also confused about the HSIOM mentioned in the datasheet.Are we supposed to configure those registers?

   

Finally, pin assignments in '.cydwr' file are different than those given in the datasheet.

   

(e.g:

   

in the datasheet: MOSI=P0.0, MISO=P0.1, SCLK=P0.3 and

   

in the .cydwr file: MOSI=P0.0, MISO=P0.2, SCLK=P0.1)

   

Would I have to change the pin assignment in cydwr file?

0 Likes
1 Solution
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Sorry, I wasn't quite clear. Trust the fitter in the .cydwr file.

   

 

   

Bob

View solution in original post

0 Likes
17 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Welcome in the forum!

   

Finally, pin assignments in '.cydwr' file are different than those given in the datasheet.

   

Which datasheet are you referring to? (Link)

   

I could not find a reference to P0.0. Best is to assign the pins at your needs and PSoCs capabilities.

   

 

   

 

   

So do I have to write my own function for writing into registers

   

Yes, SPI devices are quite different, so there is no general API possible.

   

 

   

I'm also confused about the HSIOM mentioned in the datasheet.Are we supposed to configure those registers?

   

No, for standard usage not required.

   

 

   

And what's the difference between the normal mode and SCB mode?

   

There are Serial Control Blocks (SCBs) from which (internally) the SPI is built. Additionally there are Universal Digital Blocks (UDBs) from which an SPI interface can be build as well. UDBs are quite more versatile, so best practice is to save them as long as possible.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Thanks a lot Mr. Marlowe for your help. Here's the link to the datasheet I referred (page number 14-15).

   

https://www.google.co.in/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cad=rja&uact=8&ved=0ahUKEwir-L_j7a...

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Now I see. Trust the pin assignments. In the datasheet some alternatives are listed which the fitter in creator will care for.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Ok. So should I trust the assignments from the datasheet or the .cydwr file?

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Sorry, I wasn't quite clear. Trust the fitter in the .cydwr file.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Ok.Thank you again for your time, Mr. Marlowe.

0 Likes
chsi_1572801
Level 3
Level 3
First like received

Not sure if it would be of any use but I have the  LIS3DH working on the I2S bus on a PRoC chip. The cypress example remote control code base was a great example for doing this. 

0 Likes
Anonymous
Not applicable

ok..i will surely give a look at that. Thank you.

0 Likes
Anonymous
Not applicable

I'm still having difficulty in running my spi code.I selected the SPI master full duplex macro component.What does the component indicate-the mode in which PSoC is going to operate(master) or the mode in which accelerometer is going to operate(slave)? So accordingly should I select SPI slave full duplex macro component if it's the latter one? Also I'm confused about which function serves the purpose of writing/reading across SPI; is it SPI_WriteTxData/ SPI_ReadRxData or MOSI_Write/MISO_Read?

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Master configuration is right. MOSI_Write() would try to access the pin directly which will not work because there is a direct signal connection. There is a (bit lengthy) list of APIs in the SPI datasheet. You should wait until all bytes are transferred (sent) before you access the received data. Set the internal buffers large enough to keep a transaction.

   

 

   

Bob

lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

I've been trying for a while to get SPI worked.But I'm still having difficulties in it. So side by side I've started working on I2C protocol (LIS3DH supports that too).I'm facing some problems there too. I'm using uart to test the code.So I added 'put string' statements at various points in I2CMasterSendStart function so as to track the error.It's showing the bus error, which according to the datasheet, is due to the misplaced start and stop conditions and because of that it's not returning the error status and blocking the further steps of main code.

   

Attached herewith is the workspace of the same.In the code, 0x33 is the address of the accelerometer and 0x0f is the sub address that is to be read(identity register, which has the value 0x33). Code is not working. What could possibly be wrong?

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Byte I2C interface is quite simple: After setting up the component and starting it you use

   

    I2C_MasterSendStart(DeviceAddress,I2C_WRITE_XFER_MODE);    // Initialize a transaction for writing
    I2C_MasterWrite(Register);                // Indicate which register you want to write to
    I2C_MasterWriteByte(Value);                // Write to register
    I2C_MasterSendStop();                    // End of transaction

   

When you want to read from a device you use (example for reading two bytes

   

    I2C_MasterSendStart(DeviceAddress,I2C_WRITE_XFER_MODE);    // Initialize a transaction for writing
    I2C_MasterWrite(Register);                // Indicate which register you want to write to
    I2C_MasterSendRestart(DeviceAddress,I2C_READ_XFER_MODE);
    I2C_MasterReadByte(I2C_ACK_DATA);            // Read from register
    I2C_MasterReadByte(I2C_NAK_DATA);            // Read from register, last byte is NAKed
    I2C_MasterSendStop();                    // End of transaction

   

Not too difficult. Keep in mind that most of the APIs (except those for reading a byte) return a status byte which, when non-zero indicate an error condition.

   

Bob

0 Likes
Anonymous
Not applicable

Thank you for the help.I'm confused about few more things- What exactly are the values of I2C_ACK_DATA and I2C_NAK_DATA.

   

I can't find them defined in any of the generated files.

   

Also, do I2C_MasterWrite() and I2C_MasterWriteByte() functions mean the same? Because I'm not able to find MasterWrite() function separately.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Search for the names in the .h files, it is essential to use them or the equivalents when using an I2C component based on SCB.

   

Open the component datasheet and search for "NAK" to get the basic name.

   

Do not forget that your component name is "I2C_1" and not "I2C" as in my explanation.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

yes, I'm extremely sorry that I didn't search properly. I made the changes you suggested.Can the function' I2C_MasterWriteByte ()' be used for sending the register address? I'm still not getting any output. It's still showing the 'bus error'.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Post your actual project. Which board do you use?

   

 

   

Bob

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

I'm using psoc Cy8ckit-042-ble (cy8c4247lqi-bl483). I2C_MasterSendStart is not returning the status and it's blocking further program.

0 Likes