Getting started with SPI for the X2

I have a couple sensors (gyro and accelerometer) that I want to hook up to the X2 using SPI. It looks like most of the programming is taken care of by the high-level C wrapper functions provided by Pololu for the X2, but I’m confused about the hardware connections. I just can’t figure out how to hook up the SPI lines between the board and the sensors. I’ve looked through the schematics and the forum posts on SPI, but I’m still confused. (Part of the confusion is because I’ve never actually used SPI before!) Any tips to get me started? Thanks!

Hello,

I see Adam (nexisnet) is already helping you with this on the other thread, so I just want to add that we do not recommend using the hardware SPI on the X2 unless you know what you are doing. The SPI is used to program the mega644 and to send commands between it and the mega168, and you could interfere with that unless you sufficiently disconnect your hardware (typically using the slave select lines).

- Jan

What about I2C instead? Is there some kind of hardware support for I2C on the X2?

In any case, I’m thinking about emulating I2C in software using the Peter Fleury code that Adam mentioned in that other thread. This should allow me to continue using the LCD while also avoiding the hardware SPI, as you recommend. What are your thoughts on this approach?

Hello.

There is no hardware support for using the I2C lines on the X2’s auxiliary mega168 microcontroller, but the main mega644 microcontroller has built-in support for hardware I2C using lines PC0 and PC1. As you’ve noted, these lines are also LCD data lines, so I’m not sure how well it would work to use them simultaneously for both the LCD and I2C. I know you could use them for I2C between LCD updates (this will not adversely affect the LCD because the LCD control lines will be idle while you’re using the I2C), but I’m not sure if LCD updates would produce unwanted I2C communications.

I have no experience with software I2C, but if you can spare the MCU cycles I think it would work just fine.

- Ben