frisbone
Posts: 21
Joined: Sun Mar 10, 2013 9:41 pm

Re: Help with MMA8452Q over I²C

Sat Mar 16, 2013 3:26 pm

Very disappointed that my RPi is the cause. I hate wasting money even if it is only $14. Maybe one day I'll use it on a real Microcontroller in an arduino environment. How does the ADXL345 compare to the MMA8452Q?

Finally got my I2C lines on a scope by the time you all diagnosed it. Here is my pic of the following command:

i2cget -y 1 0x1d 0x0d

http://www.flickr.com/photos/[email protected] ... hotostream

wallasey
Posts: 26
Joined: Fri Nov 16, 2012 5:10 pm

Re: Help with MMA8452Q over I²C

Sun Mar 17, 2013 11:48 am

Hi,

I've been able to add i2c repeating starts to my Perl modules. Posting here as I too Googled myself into depression over this before stumbling across a solution.

Details at

http://raspberry.znix.com/2013/03/raspb ... t-i2c.html

Cheers

Mark

frisbone
Posts: 21
Joined: Sun Mar 10, 2013 9:41 pm

Re: Help with MMA8452Q over I²C

Sun Mar 17, 2013 10:53 pm

Thanks wallasey. I was able to install the perl package you referenced and can indeed get an answer to my WHOAMI register query now. For everyone else, I've attached my test perl script. If you aren't familiar with perl - just type "perl am_test.pl 13" in whatever directory you stored it. I get back 42 (0x2A) as expected.

Not a fan of having to use perl modules for this but it does make it clear to me that this can't be a hardware issue if a perl module can get around it. It seems more like its a deficiency in the I2C driver used for the built-in functions (i2cget), python library, and C library (wiringPi). I'm unfamiliar with the structure and how dependent C and python are on lower-level system functions - but clearly the perl approach is somehow implementing its own low-level handling. I'll look into it more when I get time. However - it seems like we can work with this in perl at least.

Ian
Attachments
am_test.zip
(526 Bytes) Downloaded 156 times

photomankc
Posts: 80
Joined: Fri Aug 24, 2012 12:58 pm

Re: Help with MMA8452Q over I²C

Mon Mar 18, 2013 1:27 pm

Very interesting! I had thought it was being held as a limitation of the Bcom chip but that definitely says otherwise.

wallasey
Posts: 26
Joined: Fri Nov 16, 2012 5:10 pm

Re: Help with MMA8452Q over I²C

Mon Mar 18, 2013 9:25 pm

Don't like using Perl! Shame on you :-)
Anyhow, it requires C to implement the solution. I contributed it to the bcm2835 library - which I then wrapped in Perl. You can view the C source at http://cpansearch.perl.org/src/MDOOTSON ... /bcm2835.c, You are interested in the bcm2835_i2c_read_register_rs function.

This is somewhere between a hack and a supported method. The Broadcom manual describes a method of sending a repeated start as a way of supporting 10 bit addressing. I just thought it would work perfectly well for this purpose too.

Note - make sure you have version 0.23 of the modules - there was a bug in the 0.22 release.

perl -MHiPi -e'print $HiPi::VERSION .qq(\n);'

The wiring Pi library accesses the /dev/i2c-x devices for its i2c implementation - which is of course the correct way to do it. You just won't be able to implement this feature that way - it needs a kernel driver update.

In trawling around for a fix for this, reading the general Linux Kernel docs for i2c implementation, it recommends using repeated starts for multiple messages in your driver implementation. So whether this ever gets implemented in the kernel driver probably depends on whether it is considered a horrible hack or utilising a supported feature ( as per 10 bit addressing instructions).

I'm thinking of implementing a user-space filesystem using fuse so my Perl can access this capability using ioctl just as it would access the kernel device drivers. Then anything can access the capability with a slight change to its i2c device driver access code.

Better if it were in the kernel, of course. Glad it helped.

Cheers

Mark

wallasey
Posts: 26
Joined: Fri Nov 16, 2012 5:10 pm

Re: Help with MMA8452Q over I²C

Tue Mar 19, 2013 2:41 pm

[quote="frisbone"]Not a fan of having to use perl modules for this/quote]

I took this to heart - and used it as an opportunity to try out some executable creation techniques.
The upshot is I've now included a command line utility with the distribution - so once installed you don't have to get involved with Perl at all.

It isn't quick executing a process just to read or write a few bytes of info, but I think it will satisfy many uses until something better comes along.

Details at http://raspberry.znix.com/2013/03/i2c-r ... r-all.html.

Cheers

Mark

frisbone
Posts: 21
Joined: Sun Mar 10, 2013 9:41 pm

Re: Help with MMA8452Q over I²C

Tue Mar 19, 2013 2:52 pm

Well I'm appreciative of your effort but regretful that my comment caused it. :lol:

Nothing against Perl - I just plan to use C for my project for a variety of reasons.

jnivard
Posts: 37
Joined: Tue Dec 18, 2012 1:27 pm

Re: Help with MMA8452Q over I²C

Mon Apr 01, 2013 8:36 pm

Hai

I did order the same chip, so it is a problem I read


How is the ADXL345 doing Is it working on the Q2w extension board?

Kind regards John

jnivard
Posts: 37
Joined: Tue Dec 18, 2012 1:27 pm

Re: Help with MMA8452Q over I²C

Tue Apr 02, 2013 7:37 pm

Hai
My MMA8452Q is comming next week and I see I will have a lot of trubles
Do we have any success or can I give up this chip

John

frisbone
Posts: 21
Joined: Sun Mar 10, 2013 9:41 pm

Re: Help with MMA8452Q over I²C

Wed Apr 03, 2013 10:45 am

See my post earlier. I am having no problems at all now with wallasey's upgrade to the BCM library which he uses in his perl package. I attached a perl script that shows that it works. I have also been reading/writing just fine linking with just the "C" library.

As he says - if you use the 10 bit addressing capability you can emulate a repeated start and it seems to work just fine.

jnivard
Posts: 37
Joined: Tue Dec 18, 2012 1:27 pm

Re: Help with MMA8452Q over I²C

Wed Apr 03, 2013 11:47 am

Hai

I am new in this field and starting with a PI and the board of Quick2Wire. So I wonder how this will work or not at all. As a starter I do not understand the sollution

Who can I ask by the Quick2wire people to integrat it in the python i2c solution?

John

frisbone
Posts: 21
Joined: Sun Mar 10, 2013 9:41 pm

Re: Help with MMA8452Q over I²C

Wed Apr 03, 2013 12:01 pm

John,

Whether its perl or python they all still use a broadcom library that can be modified to support a solution for this board. The python library for I2C needs to be modified to add a modified read operation to support a "repeated start" using 10 bit addressing as is outlined earlier. If all of this sounds foreign to you than you can try to contact the developers of whatever library you are using and asking if they can implement this (refer to this thread) to support your device. Assuming you are stuck on using python.

There may be other posters on this thread that are Python people that can help you out more. For Perl & "C" I can certainly guide you as others can on how to work with this device. Its very doable if you are a developer - if you aren't, it might be tough to get someone else to implement it for you.

Hope that helps,

Best Regard, Ian

anjz
Posts: 2
Joined: Thu Apr 04, 2013 4:50 pm

Re: Help with MMA8452Q over I²C

Fri Apr 05, 2013 8:20 am

I've been reading this thread and I would like to obtain data from the MMA8452Q using the bcm2835 C library.
As I can see the function bcm2835_i2c_read_register_rs should be used.
But I have written a little code just for retrieving the value of the whoami register and it is not working. Can someone help me?

Here is my little code. It compiles fine, but it doesn't reach the printf when I run it.

Code: Select all

#include <bcm2835.h>
#include <stdio.h>

int main(int argc, char **argv)
{

        char *buf;
        char whoami;
        whoami = 0x0D;
        bcm2835_init();
        bcm2835_i2c_begin();
        bcm2835_i2c_set_baudrate(100000);
        bcm2835_i2c_setSlaveAddress(0x1D);
        bcm2835_i2c_read_register_rs(&whoami,buf,1);

        printf("WHOAMI: %X\n",buf);

        bcm2835_close();

        return 0;
}

Thanks

frisbone
Posts: 21
Joined: Sun Mar 10, 2013 9:41 pm

Re: Help with MMA8452Q over I²C

Fri Apr 05, 2013 10:17 am

Three things:

1. You aren't allocating memory for your return buffer - you just declared a pointer - who knows where its pointing. ==> char buf[read_len]; instead of char *buf

2. You are printing out the pointer to the buffer and not the contents of the buffer: *buf, buf

3. Make sure 0x1d is your slave device and its not 0x1c

i2c_detect -y 1 ==> should tell you which it is.

Other than that on the surface it looks ok - will have to compare against my code later today to be sure.

Ian

anjz
Posts: 2
Joined: Thu Apr 04, 2013 4:50 pm

Re: Help with MMA8452Q over I²C

Fri Apr 05, 2013 11:17 am

Thank you!

That was it.

jnivard
Posts: 37
Joined: Tue Dec 18, 2012 1:27 pm

Re: Help with MMA8452Q over I²C

Fri Apr 05, 2013 3:53 pm

The GPIO python lib of Q2W has a sollution for this problem

Kind regards

wallasey
Posts: 26
Joined: Fri Nov 16, 2012 5:10 pm

Re: Help with MMA8452Q over I²C

Fri Apr 05, 2013 11:51 pm

jnivard wrote:The GPIO python lib of Q2W has a sollution for this problem
Interesting. I can't see any evidence of a fix in the sourcecode on GitHub. It would be interesting to get confirmation that it does actually work.

jnivard
Posts: 37
Joined: Tue Dec 18, 2012 1:27 pm

Re: Help with MMA8452Q over I²C

Sat Apr 06, 2013 6:01 am

I recieved the following remark:

Hi John. You can use the quick2wire API to perform 10-bit addressing. Although it's not directly supported, you can add support without changing the API.

This function will set the 10bit addressing flag on an i2c_msg structure:

def 10bit(m):
m.flags |= I2C_M_TEN


With this you can mark reads and writes as using 10-bit addressing like this:

i2c.transaction(
10bit(writing_bytes(1,2,3,4)),
10bit(reading(2))

Going by what I could find with a bit of googling, the Quick2Wire API supports "repeated start" mode naturally: just pass multiple i2c_msgs to the I2CMaster.transaction method and all the transfers will be done with no stop/start in between.

--Nat

wallasey
Posts: 26
Joined: Fri Nov 16, 2012 5:10 pm

Re: Help with MMA8452Q over I²C

Sat Apr 06, 2013 12:28 pm

jnivard wrote:I
This function will set the 10bit addressing flag on an i2c_msg structure:

def 10bit(m):
m.flags |= I2C_M_TEN


With this you can mark reads and writes as using 10-bit addressing like this:

i2c.transaction(
10bit(writing_bytes(1,2,3,4)),
10bit(reading(2))

Going by what I could find with a bit of googling, the Quick2Wire API supports "repeated start" mode naturally: just pass multiple i2c_msgs to the I2CMaster.transaction method and all the transfers will be done with no stop/start in between.

--Nat
Well, it doesn't work then. That is essentially the root of the problem. If this worked there would be no problem with any code that uses ioctl to access /dev/i2c-x. Nothing can work(for this particular issue) with the existing kernel i2c drivers (Rasbian Wheezy 2013-02-09). If it did, this entire thread would be discussing a spurious non-existent issue. You've been side tracked a bit by the 10-bit addressing references. The references to 10-bit addressing are here only because they point to a section in the Broadcom manual that suggests how you might be able to 'hack' a repeated start. It has nothing really to do with 10 bit addressing. Using the 10 bit address flag in your ioctl structures won't solve this issue at all.

As far as I can see, the Quick2Wire code uses the standard ioctl structures to read/write to the device file. In that case. it doesn't matter how many messages you pass at once within the ioctl data structure. On the current implementation within Raspberry Pi, there will be a stop signal sent after each message. This is contrary to the recommended implementation for i2c_transfer but there you go. It's early days for Rasbian / RPi

Whether your external device works OK with this is down to the individual device. Most devices expect repeated starts ( that's how the conversations are described within their datasheets ) but as they don't actively do anything when a stop signal is received ( other than know the transfer is over) everything works fine. The particular devices discussed here actively reset their register (read from) address to 0x00 on receipt of a stop signal so you're stuck in that case. You cannot set a particular register to read from.

Two workarounds discussed in this thread

1. The hack in bcm2835 library - manages to produce the required repeated start.

2. Simply read multiple bytes starting at address 0x00 and figure out which byte you are interested in.

In either case, what it means is that you can probably use any of these devices on the Raspberry Pi either by using the bcm2835 directly in C or wrapped for some other language -

or -- you can use any existing library for Python or whatever provided that the device will let you read a stream of bytes starting at address 0x00 . You would have to check the device datasheet for that.

Hope it helps

Mark

anton1r
Posts: 5
Joined: Wed Mar 27, 2013 5:49 pm

Re: Help with MMA8452Q over I²C

Sat May 18, 2013 4:52 pm

Hi guys (apologies for the thread revival but the query is directly related),

I've installed the HiPi packages and been able to run a couple of the commands outlined in this link

http://raspberrypi.znix.com/hipidocs/co ... ls_i2c.htm

I've tried the sample am_test perl and get an error "Undefined subroutine &HiPi::Utils::drop_permissions_id called at /usr/local/lib/perl/5.14.2/HiPi/Utils.pm line 364"

I was wanting to use C anyway so I carried on regardless.

I tried the bit of c code posted by anjz with wallasey's changes (well what I think are the correct changes) and I get more errors.

"/tmp/ccLQquTi.o in function 'main':
read.c (.text+0x1c): undefined reference to `bcm2835_init'
read.c (.text+0x20): undefined reference to `bcm2835_i2c_begin'
...
collect2:ld return 1 exit status"

I guess there is something up with the packages (and their location) but I installed the bcm2835 packages from http://www.airspayce.com/mikem/bcm2835/ following the instructions in the INSTALL file. Even the examples files under bcm2835-1.25/examples won't run with the same sort of errors.

Some assistance here please people? I'll be eternally grateful! :oops:

anton1r
Posts: 5
Joined: Wed Mar 27, 2013 5:49 pm

Re: Help with MMA8452Q over I²C

Sun May 19, 2013 9:02 am

http://www.raspberrypi.org/phpBB3/viewt ... 33&t=16451

Used the link above to sort it. Wasn't including my link files in the compile.

Example c works perfectly now.

wallasey
Posts: 26
Joined: Fri Nov 16, 2012 5:10 pm

Re: Help with MMA8452Q over I²C

Tue May 21, 2013 3:18 am

anton1r wrote:Hi guys (apologies for the thread revival but the query is directly related),

I've installed the HiPi packages and been able to run a couple of the commands outlined in this link

http://raspberrypi.znix.com/hipidocs/co ... ls_i2c.htm

I've tried the sample am_test perl and get an error "Undefined subroutine &HiPi::Utils::drop_permissions_id called at /usr/local/lib/perl/5.14.2/HiPi/Utils.pm line 364"
drop_permissions_id bug fixed in latest modules release
http://raspberry.znix.com/2013/05/hipi- ... lease.html

Glad you were able to sort out your C compilation anyway.

panosch89
Posts: 1
Joined: Wed Nov 29, 2017 2:11 pm

Re: Help with MMA8452Q over I²C

Wed Nov 29, 2017 2:28 pm

PiBurgh wrote:
Thu Mar 14, 2013 2:22 am
OK, here's an update. Since the I2C on the GPIO has its own pull-up resistors, I did two things:

1) ripped the pull-up resistors off the sparkfun BoB (unsoldered R1 & R2) - same behavior.

2) I implemented i2c-0 on P5 on the RPi which has no pull-ups and use my other MMA8452Q that still had its pull-ups intact. - same behavior.

In mad frustration I simultaneously placed an order for an ADXL345. In seconds of its arrival I had it wired up on the breadboard and it instantly (well, once I figured out CS had to be pulled high and SA0 pulled low) started communicating with the RPi. After I verified this with i2cdetect and i2cdump (which now returns non-zero values from the appropriate registers), I got a Python program running that plots the X, Y, Z values in real time. No sweat. I can post my Python script, but that wasn't the issue.

Still don't know what's wrong with the MMA8452Q. Don't think I'll be able to sort it out without an oscilloscope or i2c monitor that can watch the SDA line to see what's happening there.

Ideas welcome...

Hello,
I know it's quite a while since you opened the topic and posted...
I'm a newbie to Raspberry, Python etc. I've been trying to use an MMA8452 accelerometer mounted on an adafruit perma-proto hat on a raspberry pi3.
Unfortunately I'm running out of time and haven't yet managed to reach my goal. I can communicate with the accelerometer and have it print values of acceleration (instant values.)
However due to my luck in skills I can't manage to set the appropriate sampling rate and range in which I want it to operate. Also a code that plots X,Y,Z values in real time would be extremely helpful.

In case you can post your program (or somebody else can help) it would be much appreciated

Thank you in advance
Panos

Return to “Automation, sensing and robotics”