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;
}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.