fopetesl
Posts: 44
Joined: Tue Oct 20, 2015 8:08 am

MMA7455 SPI pain :(

Mon Dec 14, 2015 6:49 pm

I've hacked away at this. RTFMd again and again.
I think I've got it right but I cannot get MMA7455 to respond.

Code: Select all

// Author: Mike McCauley
// Copyright (C) 2012 Mike McCauley
// $Id: RF22.h,v 1.21 2012/05/30 01:51:25 mikem Exp $
#include <bcm2835.h>
#include <stdio.h>
int main(int argc, char **argv)
{
    // If you call this, it will not actually access the GPIO
// Use for testing
//        bcm2835_set_debug(1);
      if (!bcm2835_init())
        return 1;
    bcm2835_spi_begin();
    bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST);      // The default
    bcm2835_spi_setDataMode(BCM2835_SPI_MODE0);                   // The default
    bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_65536); // The default
//    bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_32); // ~7MHz
    bcm2835_spi_chipSelect(BCM2835_SPI_CS0);                      // The default
    bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW);      // the default
// Send a byte to the slave and simultaneously read a byte back from the slave
// If you tie MISO to MOSI, you should read back what was sent
//    uint8_t send_data = 0x23;
    uint8_t send_data = ((0x16 << 1) ^ 0x80);  /// MCTL register @ 0x16 (B7 SET = Write to a Register)
    uint8_t read_data = bcm2835_spi_transfer(send_data);
    printf("Sent to SPI(1): 0x%02X. Read back from SPI: 0x%02X.\n", send_data, read_data);
//    if (send_data != read_data)
//      printf("Do you have the loopback from MOSI to MISO connected?\n");

    send_data = 0x45;  /* MCTL register: Continuous measurement; 2g range; 4Wire; no INT1 */
    read_data = bcm2835_spi_transfer(send_data);
    printf("Sent to SPI(2): 0x%02X. Read back from SPI: 0x%02X.\n", send_data, read_data);

// Now read STATUS until data ready....
// while(1) {
	send_data = (0x09 << 1) & 0x7f; // STATUS Reg address 0x09 B7 RESET == Read a Register
	uint8_t status = bcm2835_spi_transfer(send_data); // STATUS Reg: (B0 SET == Data Ready)
    printf("Sent to SPI(3): 0x%02X. Read back from SPI: 0x%02X.\n", send_data, status);
	status = bcm2835_spi_transfer((uint8_t)0x0); // Dummy byte to get result. /**/
    printf("Sent to SPI(4): 0x00. Read back from SPI: 0x%02X.\n", status);
//  }

    bcm2835_spi_end();
    bcm2835_close();
    return 0;
}
I've put a scope on the signal lines and the Pi lines toggle as expected but the MMA SD0 line always shows zero when CS0 is low.
The CS0 line toggles low only when the Pi is transmitting on MOSI line so I guess the toggle is activated in software.

Where's my mistake?

All credit to original author, Mike McCauley. His test loop worked fine.

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: MMA7455 SPI pain :(

Mon Dec 14, 2015 6:54 pm

Could you link to the actual module you are using?

Could you detail the connections you have made between the Pi and the module?

Could you post a photo of the connections?

fopetesl
Posts: 44
Joined: Tue Oct 20, 2015 8:08 am

Re: MMA7455 SPI pain :(

Mon Dec 14, 2015 7:03 pm

It's one of these, Joan:
http://www.ebay.co.uk/itm/5V-3-3V-MMA74 ... SwAKxWVExJ

I'll have to do the wiring & pics in the morning. Carer's duty calls.
Thanks for the quick response.
Peter

Edit: Try to attach but files must be <= 64Kb! So have resized but now too small??

Re-Edit: 3rd pic didn't include. Try again...
Attachments
MMA PCB cables - Copy.JPG
MMA PCB cables - Copy.JPG (48.67 KiB) Viewed 796 times
RbPi2 connector to MMA - Copy.JPG
RbPi2 connector to MMA - Copy.JPG (43.51 KiB) Viewed 796 times
RbPi2 to MMA7455 PCB.jpg
RbPi2 to MMA7455 PCB.jpg (58.84 KiB) Viewed 796 times

n8frogg
Posts: 1
Joined: Sun Jan 31, 2016 6:36 am

Re: MMA7455 SPI pain :(

Sun Jan 31, 2016 6:38 am

Did you figure out how to access your MMA7455 via SPI?

fopetesl
Posts: 44
Joined: Tue Oct 20, 2015 8:08 am

Re: MMA7455 SPI pain :(

Mon Feb 01, 2016 11:05 am

n8frogg wrote:Did you figure out how to access your MMA7455 via SPI?
Yes, sorted thanks.
If I can find time I'll add some code so others might understand.
Like most things, simple when you know how.
Briefly

Code: Select all

    char buf[] = { DEVID | REG_READ, 0x00}; // Read DeviceID of ADXL345
    bcm2835_spi_transfern(buf, sizeof(buf));
// buf[1] will now have the data that was read from the slave...
    printf("Device ID: %02X \n\n", buf[1]);
Note that you need to add, in this case, "0x00" into the buf[] to allow the extra byte for return from MMA7455.

However, I have left the MMA device and am now using an ADXL345 since the MMA is too slow a bandwidth for my needs.

Return to “Beginners”